import cv2
import time
import argparse
import os
import torch
import posenet
import json
import math
import numpy as np
import torch.nn as nn

from netvlad import NetVLAD
from netvlad import EmbedNet

from torch.autograd import Variable
from sklearn.decomposition import PCA

parser = argparse.ArgumentParser()
parser.add_argument('--model', type=int, default=101)
parser.add_argument('--scale_factor', type=float, default=1.0)
parser.add_argument('--notxt', action='store_true')
parser.add_argument('--video_dir', type=str, default='./video_in')
parser.add_argument('--image_dir', type=str, default='./test_in')
parser.add_argument('--output_dir', type=str, default='./test_out')
args = parser.parse_args()

def _pairwise_distances(embeddings, squared=False):
    """Compute the 2D matrix of distances between all the embeddings.
    Args:
        embeddings: Variable of shape (batch_size, embed_dim)
        squared: Boolean. If true, output is the pairwise squared euclidean distance matrix.
                 If false, output is the pairwise euclidean distance matrix.
    Returns:
        pairwise_distances: Variable of shape (batch_size, batch_size)
    """

    # Get the dot product between all embeddings
    # shape (batch_size, batch_size)
    dot_product = torch.mm(embeddings, embeddings.t())

    # Get squared L2 norm for each embedding. We can just take the diagonal of `dot_product`.
    # This also provides more numerical stability (the diagonal of the result will be exactly 0).
    # shape (batch_size,)
    square_norm = torch.diag(dot_product)

    # Compute the pairwise distance matrix as we have:
    # ||a - b||^2 = ||a||^2  - 2 <a, b> + ||b||^2
    # shape (batch_size, batch_size)
    distances = torch.unsqueeze(square_norm, 0) - 2.0 * dot_product + torch.unsqueeze(square_norm, 1)

    # Because of computation errors, some distances might be negative so we put everything >= 0.0
    distances = torch.clamp(distances, min=0.0)

    if not squared:
        # Not sure if needed for pytorch but does not harm
        # Because the gradient of sqrt is infinite when distances == 0.0 (ex: on the diagonal)
        # we need to add a small epsilon where distances == 0.0
        mask = torch.eq(distances, 0.0).float()
        distances = distances + mask * 1e-16

        distances = torch.sqrt(distances)

        # Correct the epsilon added: set the distances on the mask to be exactly 0.0
        distances = distances * (1.0 - mask)

    return distances

def _get_anchor_positive_triplet_mask(labels):
    """Return a 2D mask where mask[a, p] is True iff a and p are distinct and have same label.
    Args:
        labels: torch.Tensor with shape [batch_size]
    Returns:
        mask: Varieble with torch.ByteTensor with shape [batch_size, batch_size]
    """
    # Check that i and j are distinct
    indices_equal = torch.eye(labels.size(0)).bool()
    if labels.is_cuda:
        indices_equal = indices_equal.cuda()
    indices_not_equal = ~indices_equal

    # Check if labels[i] == labels[j]
    # Uses broadcasting where the 1st argument has shape (1, batch_size) and the 2nd (batch_size, 1)
    labels_equal = torch.eq(torch.unsqueeze(labels, 0), torch.unsqueeze(labels, 1))

    # Combine the two masks
    mask = indices_not_equal & labels_equal

    return mask

def _get_anchor_negative_triplet_mask(labels):
    """Return a 2D mask where mask[a, n] is True iff a and n have distinct labels.
    Args:
        labels: torch.Tensor with shape [batch_size]
    Returns:
        mask: Variable with torch.ByteTensor with shape [batch_size, batch_size]
    """
    # Check if labels[i] != labels[k]
    # Uses broadcasting where the 1st argument has shape (1, batch_size) and the 2nd (batch_size, 1)
    labels_equal = torch.eq(torch.unsqueeze(labels, 0), torch.unsqueeze(labels, 1))

    mask = ~labels_equal

    return mask

def _get_triplet_mask(labels):
    """Return a 3D mask where mask[a, p, n] is True iff the triplet (a, p, n) is valid.
    A triplet (i, j, k) is valid if:
        - i, j, k are distinct
        - labels[i] == labels[j] and labels[i] != labels[k]
    Args:
        labels: torch.Tensor with shape [batch_size]
    """
    # Check that i, j and k are distinct
    indices_equal = torch.eye(labels.size(0)).byte()
    if labels.is_cuda:
        indices_equal = indices_equal.cuda()
    indices_not_equal = ~indices_equal
    i_not_equal_j = torch.unsqueeze(indices_not_equal, 2)
    i_not_equal_k = torch.unsqueeze(indices_not_equal, 1)
    j_not_equal_k = torch.unsqueeze(indices_not_equal, 0)

    distinct_indices = (i_not_equal_j & i_not_equal_k) & j_not_equal_k

    # Check if labels[i] == labels[j] and labels[i] != labels[k]
    label_equal = torch.eq(torch.unsqueeze(labels, 0), torch.unsqueeze(labels, 1))
    #if labels.is_cuda:
    #    label_equal = label_equal.cuda()
    i_equal_j = torch.unsqueeze(label_equal, 2)
    i_equal_k = torch.unsqueeze(label_equal, 1)

    valid_labels = i_equal_j & (~i_equal_k)

    # Combine the two masks
    mask = distinct_indices & valid_labels.type(torch.uint8)

    return mask

def batch_all_triplet_loss(labels, embeddings, margin, squared=False):
    """Build the triplet loss over a batch of embeddings.
    We generate all the valid triplets and average the loss over the positive ones.
    Args:
        labels: Variable with labels of the batch, of size (batch_size,)
        embeddings: Variable with tensor of shape (batch_size, embed_dim)
        margin: margin for triplet loss
        squared: Boolean. If true, output is the pairwise squared euclidean distance matrix.
                 If false, output is the pairwise euclidean distance matrix.
    Returns:
        triplet_loss: scalar tensor containing the triplet loss
    """
    # Get the pairwise distance matrix
    pairwise_dist = _pairwise_distances(embeddings, squared=squared)

    anchor_positive_dist = torch.unsqueeze(pairwise_dist, 2)
    anchor_negative_dist = torch.unsqueeze(pairwise_dist, 1)

    # Compute a 3D tensor of size (batch_size, batch_size, batch_size)
    # triplet_loss[i, j, k] will contain the triplet loss of anchor=i, positive=j, negative=k
    # Uses broadcasting where the 1st argument has shape (batch_size, batch_size, 1)
    # and the 2nd (batch_size, 1, batch_size)
    triplet_loss = anchor_positive_dist - anchor_negative_dist + margin

    # Put to zero the invalid triplets
    # (where label(a) != label(p) or label(n) == label(a) or a == p)
    mask = _get_triplet_mask(labels)
    mask = Variable(mask.float())
    triplet_loss = mask * triplet_loss

    # Remove negative losses (i.e. the easy triplets)
    triplet_loss = torch.clamp(triplet_loss, min=0.0)

    # Count number of positive triplets (where triplet_loss > 0)
    valid_triplets = torch.gt(triplet_loss, 1e-16)
    num_positive_triplets = valid_triplets.sum().float()
    num_valid_triplets = mask.sum()
    fraction_positive_triplets = num_positive_triplets / (num_valid_triplets + 1e-16)

    # Get final mean triplet loss over the positive valid triplets
    triplet_loss = triplet_loss.sum() / (num_positive_triplets + 1e-16)

    return triplet_loss, fraction_positive_triplets

def batch_hard_triplet_loss(labels, embeddings, margin, squared=False):
    """Build the triplet loss over a batch of embeddings.
    For each anchor, we get the hardest positive and hardest negative to form a triplet.
    Args:
        labels: Variable with labels of the batch, of size (batch_size,)
        embeddings: Variable with tensor of shape (batch_size, embed_dim)
        margin: margin for triplet loss
        squared: Boolean. If true, output is the pairwise squared euclidean distance matrix.
                 If false, output is the pairwise euclidean distance matrix.
    Returns:
        triplet_loss: scalar tensor containing the triplet loss
    """
    # Get the pairwise distance matrix
    pairwise_dist = _pairwise_distances(embeddings, squared=squared)

    # For each anchor, get the hardest positive
    # First, we need to get a mask for every valid positive (they should have same label)
    mask_anchor_positive = _get_anchor_positive_triplet_mask(labels)
    mask_anchor_positive = Variable(mask_anchor_positive.float())

    # We put to 0 any element where (a, p) is not valid (valid if a != p and label(a) == label(p))
    anchor_positive_dist = mask_anchor_positive * pairwise_dist

    # shape (batch_size, 1)
    hardest_positive_dist, _ = torch.max(anchor_positive_dist, dim=1, keepdim=True)

    # For each anchor, get the hardest negative
    # First, we need to get a mask for every valid negative (they should have different labels)
    mask_anchor_negative = _get_anchor_negative_triplet_mask(labels)
    mask_anchor_negative = Variable(mask_anchor_negative.float())

    # We add the maximum value in each row to the invalid negatives (label(a) == label(n))
    max_anchor_negative_dist, _ = torch.max(pairwise_dist, dim=1, keepdim=True)
    anchor_negative_dist = pairwise_dist + max_anchor_negative_dist * (1.0 - mask_anchor_negative)

    # shape (batch_size,)
    hardest_negative_dist, _ = torch.min(anchor_negative_dist, dim=1, keepdim=True)

    # Combine biggest d(a, p) and smallest d(a, n) into final triplet loss
    triplet_loss = torch.clamp(hardest_positive_dist - hardest_negative_dist + margin, min=0.0)

    # Get final mean triplet loss
    triplet_loss = triplet_loss.mean()

    return triplet_loss

class LSTM(nn.Module):

    def __init__(self, input_dim, hidden_dim, batch_size, output_dim=1,
                    num_layers=2):
        super(LSTM, self).__init__()
        self.input_dim = input_dim
        self.hidden_dim = hidden_dim
        self.batch_size = batch_size
        self.num_layers = num_layers

        # Define the LSTM layer
        self.lstm = nn.LSTM(self.input_dim, self.hidden_dim, self.num_layers, batch_first=True, bidirectional=True)

        # Define the output layer
        self.linear = nn.Linear(self.hidden_dim*2, output_dim)

    def init_hidden(self):
        # This is what we'll initialise our hidden state as
        return (torch.zeros(self.num_layers*2, self.batch_size, self.hidden_dim),
                torch.zeros(self.num_layers*2, self.batch_size, self.hidden_dim))

    def forward(self, input):
        # Forward pass through LSTM layer
        # shape of lstm_out: [input_size, batch_size, hidden_dim]
        # shape of self.hidden: (a, b), where a and b both
        # have shape (num_layers, batch_size, hidden_dim).
        input = input.float()
        lstm_out, self.hidden = self.lstm(input.view(len(input), self.batch_size, -1))
        feature_map = (lstm_out[-1].view(self.batch_size, -1))
        feature_map = feature_map.view(feature_map.size(0),feature_map.size(1),1,1)
        # Only take the output from the final timetep
        # Can pass on the entirety of lstm_out to the next layer if it is a seq2seq prediction
        y_pred = self.linear(lstm_out[-1].view(self.batch_size, -1))

        return y_pred,feature_map

def distance(x1, y1, x2, y2):
    result = math.sqrt( math.pow(x1 - x2, 2) + math.pow(y1 - y2, 2))
    return result

def dotproduct(x1, y1, x2, y2):
    result = x1*x2 + y1*y2
    return result

def crossproduct(ax, ay, bx, by, cx, cy):
    a = ax*by+bx*cy+cx*ay
    b = ay*bx+by*cx+cy*ax
    return a-b

def arccos(n):
    result = math.acos(n)
    return result

def arcsin(n):
    result = math.asin(n)
    return result

def absolutevalue(x1, y1, x2, y2):
    v1 = math.sqrt(math.pow(x1, 2) + math.pow(y1, 2))
    v2 = math.sqrt(math.pow(x2, 2) + math.pow(y2, 2))
    result = v1*v2
    return result

def xy_to_feature_1(leftshoulder, rightshoulder, lefthip, righthip):

    d_shoulder = distance(leftshoulder[0], leftshoulder[1], rightshoulder[0], rightshoulder[1])
    d_hip = distance(lefthip[0], lefthip[1], righthip[0], righthip[1])

    return d_shoulder/d_hip

def xy_to_feature_2(leftshoulder, rightshoulder, leftelbow, rightelbow) :
    Al = (leftelbow[0]-leftshoulder[0], leftelbow[1]-leftshoulder[1])
    Ar = (rightelbow[0]-rightshoulder[0], rightelbow[1]-rightshoulder[1])

    return ((2*math.pi)-arccos(dotproduct(Al[0], Al[1], Ar[0], Ar[1])/absolutevalue(Al[0], Al[1], Ar[0], Ar[1])))/(2*math.pi)

def xy_to_feature_3(lefthip, righthip, leftknee, rightknee) :

    Ll = (leftknee[0]-lefthip[0], leftknee[1]-lefthip[1])
    Lr = (rightknee[0]-righthip[0], rightknee[1]-righthip[1])

    return (arccos(dotproduct(Ll[0], Ll[1], Lr[0], Lr[1])/absolutevalue(Ll[0], Ll[1], Lr[0], Lr[1])))/(math.pi)

def xy_to_feature_4(lefthip, righthip, leftshoulder, rightshoulder) :
    Pcenterhip = ((lefthip[0]+righthip[0])/2, (lefthip[1]+righthip[1])/2)
    neck = ((leftshoulder[0]+rightshoulder[0])/2, (leftshoulder[1]+rightshoulder[1])/2)
    h = (neck[0]-Pcenterhip[0], neck[1]-Pcenterhip[1])
    x = (1,0)
    return (arccos(dotproduct(h[0], h[1], x[0], x[1])/absolutevalue(h[0], h[1], x[0], x[1])))/(math.pi)

def xy_to_feature_5(leftshoulder, rightshoulder, leftelbow, rightelbow, leftwrist, rightwrist) :
    Al = (leftshoulder[0]-leftelbow[0], leftshoulder[1]-leftelbow[1])
    Ar = (rightshoulder[0]-rightelbow[0], rightshoulder[1]-rightelbow[1])
    Wl = (leftwrist[0]-leftelbow[0], leftwrist[1]-leftelbow[1])
    Wr = (rightwrist[0]-rightelbow[0], rightwrist[1]-rightelbow[1])
    leftelbowangle = (arcsin(crossproduct(leftelbow[0],leftelbow[1],leftshoulder[0],leftshoulder[1],leftwrist[0],leftwrist[1])/absolutevalue(Al[0], Al[1], Wl[0], Wl[1])))/(math.pi)
    rightelbowangle = (arcsin(crossproduct(rightelbow[0],rightelbow[1],rightshoulder[0],rightshoulder[1],rightwrist[0],rightwrist[1])/absolutevalue(Ar[0], Ar[1], Wr[0], Wr[1])))/(math.pi)

    return [leftelbowangle, rightelbowangle]

def xy_to_feature_6(lefthip, righthip, leftknee, rightknee, leftankle, rightankle):

    Ll = (lefthip[0]-leftknee[0], lefthip[1]-leftknee[1])
    Lr = (righthip[0]-rightknee[0], righthip[1]-rightknee[1])

    Cl = (leftankle[0]-leftknee[0], leftankle[1]-leftknee[1])
    Cr = (rightankle[0]-rightknee[0], rightankle[1]-rightknee[1])

    leftkneeangle = (arcsin(crossproduct(leftknee[0],leftknee[1],lefthip[0],lefthip[1],leftankle[0],leftankle[1])/absolutevalue(Ll[0], Ll[1], Cl[0], Cl[1])))/(math.pi)
    rightkneeangle = (arcsin(crossproduct(rightknee[0],rightknee[1],righthip[0],righthip[1],rightankle[0],rightankle[1])/absolutevalue(Lr[0], Lr[1], Cr[0], Cr[1])))/(math.pi)

    return [leftkneeangle, rightkneeangle]

def video2frame(invideofilename, save_path):
    vidcap = cv2.VideoCapture(invideofilename)
    count = 0
    while True:
      success,image = vidcap.read()
      if not success:
          break
      print ('Read a new frame: ', success)
      fname = "{}.jpg".format("{0:05d}".format(count))
      cv2.imwrite(save_path + fname, image) # save frame as JPEG file
      count += 1
    print("{} images are extracted in {}.". format(count, save_path))

def main():

    test_total_class = list()

    posenet_model = posenet.load_model(args.model)
    posenet_model = posenet_model.cuda()
    output_stride = posenet_model.output_stride

    video_filenames = [v.path for v in os.scandir(args.video_dir) if v.is_file() and v.path.endswith(('.mp4'))]

    if args.image_dir:
        if not os.path.exists(args.image_dir):
            os.makedirs(args.image_dir)

    for iv,v in enumerate(video_filenames):
        if not os.path.exists(args.image_dir+'/'+v[10:-4]+'/'):
            os.makedirs(args.image_dir+'/'+v[10:-4]+'/')
        video2frame(v,args.image_dir+'/'+v[10:-4]+'/')

        if args.output_dir:
          if not os.path.exists(args.output_dir+'/'+v[11:-4]+'/'):
            os.makedirs(args.output_dir+'/'+v[11:-4]+'/')

    for iv,v in enumerate(video_filenames):
      filenames = [f.path for f in os.scandir(args.image_dir+'/'+v[11:-4]+'/') if f.is_file() and f.path.endswith(('.png', '.jpg'))]
      for i,f in enumerate(filenames):
          input_image, draw_image, output_scale = posenet.read_imgfile(
              f, scale_factor=args.scale_factor, output_stride=output_stride)

          with torch.no_grad():
              input_image = torch.Tensor(input_image).cuda()

              heatmaps_result, offsets_result, displacement_fwd_result, displacement_bwd_result = posenet_model(input_image)

              pose_scores, keypoint_scores, keypoint_coords = posenet.decode_multiple_poses(
                  heatmaps_result.squeeze(0),
                  offsets_result.squeeze(0),
                  displacement_fwd_result.squeeze(0),
                  displacement_bwd_result.squeeze(0),
                  output_stride=output_stride,
                  max_pose_detections=10,
                  min_pose_score=0.25)

          keypoint_coords *= output_scale

          if args.output_dir:
              draw_image = posenet.draw_skel_and_kp(draw_image, pose_scores, keypoint_scores, keypoint_coords,min_pose_score=0.25, min_part_score=0.25)
              cv2.imwrite(os.path.join(args.output_dir, os.path.relpath(f, args.image_dir)), draw_image)

          if not args.notxt:
              max_score = 0
              max_index = 0
              ignore = 0

              for pi in range(len(pose_scores)):
                  if max_score > pose_scores[pi] :
                      max_index = pi

                  if pose_scores[pi] == 0.:
                      ignore = 1
                      break

              if pose_scores[max_index] != 0. :
                  tmp_data = dict()
                  out_data = dict(image_name=[f[10:-4]])

                  for ki, (s, c) in enumerate(zip(keypoint_scores[max_index, :], keypoint_coords[max_index, :, :])):
                      tmp_data[posenet.PART_NAMES[ki]] = c.tolist()

                  out_data['feature_1'] = xy_to_feature_1(tmp_data['leftShoulder'], tmp_data['rightShoulder'], tmp_data['leftHip'], tmp_data['rightHip'])
                  out_data['feature_2'] = xy_to_feature_2(tmp_data['leftShoulder'], tmp_data['rightShoulder'], tmp_data['leftElbow'], tmp_data['rightElbow'])
                  out_data['feature_3'] = xy_to_feature_3(tmp_data['leftHip'], tmp_data['rightHip'], tmp_data['leftKnee'], tmp_data['rightKnee'])
                  out_data['feature_4'] = xy_to_feature_4(tmp_data['leftHip'], tmp_data['rightHip'], tmp_data['leftShoulder'], tmp_data['rightShoulder'])
                  out_data['feature_5'] = xy_to_feature_5(tmp_data['leftShoulder'], tmp_data['rightShoulder'], tmp_data['leftElbow'], tmp_data['rightElbow'], tmp_data['leftWrist'], tmp_data['rightWrist'])
                  out_data['feature_6'] = xy_to_feature_6(tmp_data['leftHip'], tmp_data['rightHip'], tmp_data['leftKnee'], tmp_data['rightKnee'], tmp_data['leftAnkle'], tmp_data['rightAnkle'])

                  out_data['total_feature'] = list()
                  out_data['total_feature'].extend([out_data['feature_1']])
                  out_data['total_feature'].extend([out_data['feature_2']])
                  out_data['total_feature'].extend([out_data['feature_3']])
                  out_data['total_feature'].extend([out_data['feature_4']])
                  out_data['total_feature'].extend([out_data['feature_5'][0]])
                  out_data['total_feature'].extend([out_data['feature_5'][1]])
                  out_data['total_feature'].extend([out_data['feature_6'][0]])
                  out_data['total_feature'].extend([out_data['feature_6'][1]])

                  test_total_class.append(out_data['total_feature'])

                  if len(test_total_class) is 150 :
                      break

    ################## 여기서부터는 유사도 검색
    base_feature = np.load('/content/drive/My Drive/Colab Notebooks/Train_for_pca_DIY')
    base_feature_label = np.load('/content/drive/My Drive/Colab Notebooks/Train_label_for_pca_DIY')

    class_cnt = 20
    test_total_class = np.array(test_total_class)
    test_total_class = test_total_class.reshape(150,1,8)
    x_test_data = torch.from_numpy(test_total_class)

    back_bone_model = LSTM(8, 32, batch_size=1, output_dim=class_cnt, num_layers=2)
    net_vlad = NetVLAD(num_clusters=40, dim=64, alpha=1.0)

    vlad_model = EmbedNet(back_bone_model, net_vlad)
    vlad_model.load_state_dict(torch.load('/content/drive/My Drive/Colab Notebooks/VLAD_Checkpoint_20200609_Best_DIY_total.pth'), strict=True)

    test_out_feature = vlad_model(x_test_data)
    test_out_feature = np.array(test_out_feature)
    test_out_feature = test_out_feature.reshape(-1,2560)

    pca = PCA(n_components=2)
    X_Train_pca = pca.fit_transform(base_feature)
    X_test_pca = pca.transform(test_out_feature)

    check = np.concatenate((X_Train_pca,X_test_pca),axis=0)

    pairwise_dist_t = _pairwise_distances(torch.from_numpy(check))
    pairwise_dist_t = pairwise_dist_t.cpu().detach().numpy()
    pairwise_dist_sort = np.sort(pairwise_dist_t[-1][:-1])

    final_out_bef = list()
    final_out = list()
    final_score = list()

    for i in range(len(pairwise_dist_t)) :
        if pairwise_dist_sort[0] == pairwise_dist_t[-1][i] :
            final_out_bef.append(i)
            score = 100-(500*pairwise_dist_sort[0])
            if score < 0 :
                final_score.append(0)
            else :
                final_score.append(score)

            break

    for i in range(len(pairwise_dist_t)) :
        if pairwise_dist_sort[1] == pairwise_dist_t[-1][i] :
            final_out_bef.append(i)
            score = 100-(500*pairwise_dist_sort[1])
            if score < 0 :
                final_score.append(0)
            else :
                final_score.append(score)
            break

    for i in range(len(pairwise_dist_t)) :
        if pairwise_dist_sort[2] == pairwise_dist_t[-1][i] :
            final_out_bef.append(i)
            score = 100-(500*pairwise_dist_sort[2])
            if score < 0 :
                final_score.append(0)
            else :
                final_score.append(score)
            break

    final_out.append(base_feature_label[final_out[0]])
    final_out.append(base_feature_label[final_out[1]])
    final_out.append(base_feature_label[final_out[2]])

    print(final_out)
    print(final_score)

if __name__ == "__main__":
    main()

PART_NAMES = [
    "nose", "leftEye", "rightEye", "leftEar", "rightEar", "leftShoulder",
    "rightShoulder", "leftElbow", "rightElbow", "leftWrist", "rightWrist",
    "leftHip", "rightHip", "leftKnee", "rightKnee", "leftAnkle", "rightAnkle"
]


# A) Reescriba el programa que pide al usuario una lista de números e imprime en pantalla el máximo y mínimo de los
# números introducidos al final, cuando el usuario introduce “fin”.
# B) Escriba ahora el programa de modo que almacene los números que el usuario introduzca en una lista y usa las
# funciones max () y min () para calcular los números máximo y mínimo después de que el bucletermine.

# Apartado A)
def busca_valores():
    max = min = 0
    nro = input("Ingresar valor ('fin' para finalizar):")
    while nro != 'fin':
        if max == 0 and min == 0:
            max = min = int(nro)
        if int(nro) > max:
            max = int(nro)
        if int(nro) < min:
            min = int(nro)
        nro = input("Ingresar valor ('fin' para finalizar):")
    print("El valor minimo es el numero: %s" % min)
    print("El valor maximo es el numero: %s" % max)

# Probar por consola.
busca_valores()

# Apartado B)
def busca_max_min():
    lista = []
    valores = input("Ingresar valor ('fin' para finalizar):")
    while valores.lower() != 'fin':
        lista.append(int(valores))
        valores = input("Ingresar valor ('fin' para finalizar):")
    print("El valor minimo es el numero: %s" % min(lista))
    print("El valor maximo es el numero: %s" % max(lista))
    pass

# Probar por consola.
busca_max_min()


from django.db import models

import random

#Levels are represeneted by a tuple (TABLE SIZE, NUMBER OF MINES)
BEGGINNER = (9, 10)
INTERMEDIATE = (16, 40)
ADVANCED = (24, 99)

LEVELS = (
    ('B', 'BEGGINNER'),
    ('I', 'INTERMEDIATE'),
    ('A', 'ADVANCED')
)

LEVELS_DICT = {
    'B': BEGGINNER,
    'I': INTERMEDIATE,
    'A': ADVANCED,
}

class Board(models.Model):
    """
    A new board is created for each game.
    When a board object is created, the game starts.
    """
    level = models.CharField(max_length=1 ,choices=LEVELS)
    finished = models.BooleanField(default=False)
    mines_placed = models.BooleanField(default=False)

    def place_mines(self):
        """
        Place mines and then create the necesary cells to start
        the game
        """
        table_size = LEVELS_DICT[self.level][0]
        mines = LEVELS_DICT[self.level][1]
        number_of_cells = table_size ** 2
        list_of_mines = []

        while len(list_of_mines) < mines:
            x_cordenade = random.randint(1, table_size)
            y_cordenade = random.randint(1, table_size)

            if (x_cordenade, y_cordenade) not in list_of_mines:
                list_of_mines.append((x_cordenade, y_cordenade))

        # Here we create the mined cells
        for i in list_of_mines:
            c = Cell(x_cordenade=i[0],
                     y_cordenade=i[1],
                     board=self,
                     has_mine=True)
            c.save()

        # Here we crete not mined cell
        not_mined_cells = []
        for x in range(1, table_size+1):
            for y in range(1, table_size+1):
                cordenade = (x, y)
                if cordenade not in list_of_mines:
                    c = Cell(x_cordenade=i[0],
                             y_cordenade=i[1],
                             board=self,
                             has_mine=False)
                    c.save()
                    not_mined_cells.append(cordenade)

        self.mines_placed = True
        self.save()

    def get_mines_cordenades(self):
        """
        Return a list of tuples with the mines cordenades
        """
        pass

    @property
    def table_size(self):
        return LEVELS_DICT[self.level][1]

class Cell(models.Model):
    """
    Model to represent a zone where a user can click or mark as mined.
    """
    board = models.ForeignKey('Board', on_delete=models.CASCADE)
    has_mine = models.BooleanField(default=False)
    marked_as_mined = models.BooleanField(default=False)
    x_cordenade = models.IntegerField()
    y_cordenade = models.IntegerField()
    value = models.IntegerField(blank=True, null=True)
    # value field is the number of mines sourronding this cell

    def number_of_adjacent_mines(self):
        """
        Calculate a value of a cell

        Cell-->Current Cell (x_cor, y_cor)
        N -->  North        (x_cor-1, y_cor)
        S -->  South        (x_cor+1, y_cor)
        E -->  East         (x_cor, y_cor+1)
        W -->  West         (x_cor, y_cor-1)
        N.E--> North-East   (x_cor-1, y_cor+1)
        N.W--> North-West   (x_cor-1, y_cor-1)
        S.E--> South-East   (x_cor+1, y_cor+1)
        S.W--> South-West   (x_cor+1, y_cor-1)
        """
        mines_next = 0
        # --- North ---
        if Cell.objects.filter(x_cordenade=self.x_cordenade - 1,
                               y_cordenade=self.y_cordenade,
                               has_mine=True, board=self.board).exists():
            mines_next += 1
        # --- South ---
        if Cell.objects.filter(x_cordenade=self.x_cordenade + 1,
                               y_cordenade=self.y_cordenade,
                               has_mine=True, board=self.board).exists():
            mines_next += 1
        # --- East ---
        if Cell.objects.filter(x_cordenade=self.x_cordenade,
                               y_cordenade=self.y_cordenade + 1,
                               has_mine=True, board=self.board).exists():
            mines_next += 1
        # --- West ---
        if Cell.objects.filter(x_cordenade=self.x_cordenade,
                               y_cordenade=self.y_cordenade - 1,
                               has_mine=True, board=self.board).exists():
            mines_next += 1
        # --- North-East ---
        if Cell.objects.filter(x_cordenade=self.x_cordenade - 1,
                               y_cordenade=self.y_cordenade + 1,
                               has_mine=True, board=self.board).exists():
            mines_next += 1
        # --- North-West ---
        if Cell.objects.filter(x_cordenade=self.x_cordenade - 1,
                               y_cordenade=self.y_cordenade - 1,
                               has_mine=True, board=self.board).exists():
            mines_next += 1
        # --- South-East ---
        if Cell.objects.filter(x_cordenade=self.x_cordenade + 1,
                               y_cordenade=self.y_cordenade + 1,
                               has_mine=True, board=self.board).exists():
            mines_next += 1
        # --- South-West ---
        if Cell.objects.filter(x_cordenade=self.x_cordenade + 1,
                               y_cordenade=self.y_cordenade - 1,
                               has_mine=True, board=self.board).exists():
            mines_next += 1

        return mines_next

    def click_on_cell(self):
        pass


## libs
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

## Keras
from keras.layers import Lambda, Input, Dense, Conv2D, Conv2DTranspose, Flatten, Reshape
from keras.models import Model
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.losses import mse, binary_crossentropy
from keras.utils import plot_model
from keras import backend as K

from sklearn.model_selection import train_test_split

class VAE():

    # reparameterization trick
    # instead of sampling from Q(z|X), sample eps = N(0,I)
    # then z = z_mean + sqrt(var)*eps

    def sampling(self, args):
        """Reparameterization trick by sampling fr an isotropic unit Gaussian.
        # Arguments
            args (tensor): mean and log of variance of Q(z|X)
        # Returns
            z (tensor): sampled latent vector
        """

        z_mean, z_log_var = args
        batch = K.shape(z_mean)[0]
        dim = K.int_shape(z_mean)[1]
        # by default, random_normal has mean=0 and std=1.0
        epsilon = K.random_normal(shape=(batch, dim))
        return z_mean + K.exp(0.5 * z_log_var) * epsilon

    def __init__(self, modelArgs, trainArgs, G, T):

        ## MODEL ______________________________________________________________

        ## Multi-layer Perceptron without convolutions__________________________________
        if modelArgs["nn_architecture"] == "mlp":

            ## 1) build encoder model
            inputs = Input(shape= modelArgs["input_shape"], name='encoder_input')
            x = Dense(128, activation='relu')(inputs)
            x = Dense(64, activation='relu')(x)
            z_mean = Dense(modelArgs["latent_dim"], name='z_mean')(x)
            z_log_var = Dense(modelArgs["latent_dim"], name='z_log_var')(x)

            ## 2) build decoder model
            latent_inputs = Input(shape=(modelArgs["latent_dim"],), name='z_sampling')
            y = Dense(64, activation='relu')(latent_inputs)
            y = Dense(128, activation='relu')(y)
            outputs = Dense(modelArgs["output_shape"], activation='sigmoid')(y)

            # use reparameterization trick to push the sampling out as input
            # note that "output_shape" isn't necessary with the TensorFlow backend
            z = Lambda(self.sampling, output_shape=(modelArgs["latent_dim"],), name='z')([z_mean, z_log_var])

        ## Convolutional Neural Network_________________________________
        if modelArgs["nn_architecture"] == "2D_conv":

            ## 1) build encoder model____________________________________

            inputs = Input(shape=modelArgs["input_shape"], name='encoder_input')
            x = inputs

            for i in range(2):
                modelArgs['filters'] *= 2
                x = Conv2D(filters=modelArgs['filters'], kernel_size=modelArgs['kernel_size'], activation='relu',
                           strides=2, padding='same')(x)

            # shape info needed to build decoder model
            shape = K.int_shape(x)

            # generate latent vector Q(z|X)
            x = Flatten()(x)
            x = Dense(16, activation='relu')(x)
            z_mean = Dense(modelArgs["latent_dim"], name='z_mean')(x)
            z_log_var = Dense(modelArgs["latent_dim"], name='z_log_var')(x)

            ## 2) build decoder model____________________________________

            latent_inputs = Input(shape=(modelArgs["latent_dim"],), name='z_sampling')
            x = Dense(shape[1] * shape[2] * shape[3], activation='relu')(latent_inputs)
            x = Reshape((shape[1], shape[2], shape[3]))(x)

            for i in range(2):
                x = Conv2DTranspose(filters=modelArgs['filters'], kernel_size=modelArgs['kernel_size'],
                                    activation='relu', strides=2, padding='same')(x)
                modelArgs['filters'] //= 2

            outputs = Conv2DTranspose(filters=1, kernel_size=modelArgs['kernel_size'], activation='sigmoid',
                                      padding='same', name='decoder_output')(x)

            # use reparameterization trick to push the sampling out as input
            # note that "output_shape" isn't necessary with the TensorFlow backend
            z = Lambda(self.sampling, output_shape=(modelArgs["output_shape"],), name='z')([z_mean, z_log_var])

        ## INSTANTIATE___________________________________

        ## 1) instantiate encoder model
        encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder')
        encoder.summary()
        # plot_model(encoder, to_file='vae_cnn_encoder.png', show_shapes=True)

        ## 2) instantiate decoder model
        decoder = Model(latent_inputs, outputs, name='decoder')
        decoder.summary()
        # plot_model(decoder, to_file='vae_cnn_decoder.png', show_shapes=True)

        ## 3) instantiate VAE model
        outputs = decoder(encoder(inputs)[2])
        vae = Model(inputs, outputs, name='conv_vae')
        # vae.summary()

        ## MLP: beta =
        ## CNN: beta = (latent 2 / beta 25)

        ## Train and Validation Split _______________________________________________

        x_train, x_test, y_train, y_test = train_test_split(G, T, test_size=trainArgs["data_split"], random_state=1, shuffle=True)

        models = (encoder, decoder)
        data = (x_test, y_test)

        ## RECONSTRUCTION LOSS_______________________

        if trainArgs["loss"] == "mse":

            if modelArgs["nn_architecture"] == "mlp":
                reconstruction_loss = mse(inputs, outputs)
                reconstruction_loss *= modelArgs["input_shape"]

            if modelArgs["nn_architecture"] == "2D_conv":
                reconstruction_loss = mse(K.flatten(inputs), K.flatten(outputs))
                reconstruction_loss *= modelArgs["input_shape"][0] * modelArgs["input_shape"][1]

        if trainArgs["loss"] == "binary_crossentropy":

            if modelArgs["nn_architecture"] == "mlp":
                reconstruction_loss = binary_crossentropy(inputs, outputs)
                reconstruction_loss *= modelArgs["input_shape"]

            if modelArgs["nn_architecture"] == "2D_conv":
                reconstruction_loss = binary_crossentropy(K.flatten(inputs), K.flatten(outputs))
                reconstruction_loss *= modelArgs["input_shape"][0] * modelArgs["input_shape"][1]

        ## KL LOSS _____________________________________________

        kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var)
        kl_loss = K.sum(kl_loss, axis=-1)
        kl_loss *= -0.5

        ## COMPLETE LOSS __________________________________________________

        vae_loss = K.mean(reconstruction_loss + (trainArgs["beta"] * kl_loss))
        vae.add_loss(vae_loss)
        vae.compile(optimizer='adam', metrics=['accuracy'])
        vae.summary()

        ## TRAIN______________________________________________

        # load the autoencoder weights

        if trainArgs["weights"] == "load":

            vae.load_weights("models/weights/vae_mlp_mnist_latent_dim_" + str(modelArgs["latent_dim"]) + ".h5")

        # train the autoencoder

        elif trainArgs["weights"] == "train":

            # Set callback functions to early stop training and save the best model so far
            callbacks = [EarlyStopping(monitor='val_loss', patience= trainArgs["early_stop"]), ModelCheckpoint(
                filepath="models/weights/vae_mlp_mnist_latent_dim_" + str(modelArgs["latent_dim"]) + ".h5",
                save_best_only=True)]

            vae.fit(x_train, epochs= trainArgs["epochs"], batch_size=trainArgs["batch_size"], callbacks=callbacks, validation_data=(x_test, None))
            vae.save_weights("models/weights/vae_mlp_mnist_latent_dim_" + str(modelArgs["latent_dim"]) + ".h5")

        self.model = models
        self.data = data


import pandas
import numpy
from collections import Counter
import math
import cmath
from decimal import Decimal
from collections import Counter
name = []
cd = []
coordinates = []
coordinates1 = []
xx = []
latitude = []
longitude = []
fl = []
fln = []
w=0
def checking(fl,fln,name,latitude,longitude,cd,q,w):

    x = Decimal(latitude[q])
    y = Decimal(longitude[q])

    wsp = str(cd[w])
    s = wsp.split(",")

    r = 0.00005
    first = Decimal(s[0])
    second = Decimal(s[1])

    iff = (x - first)
    if2 = (y - second)

    if3 = abs(iff+if2)

    if if3 <= r:

        fln.append(name[w])

datatxt = open('bike_data.txt','r')

df = pandas.read_excel('stacje2.xlsx')
v = numpy.asmatrix(df)

total_rows=len(df.axes[0])
total_cols=len(df.axes[1])

rows = df.shape[0]
b = int(rows)

for i in range(total_rows):
    name.append(v[i,1])

for i in range(total_rows):
    cd.append(v[i,3])

dane = str(datatxt.readlines())
s = dane.split(" ")
length = len(s)

for j in range(length):
    if 'lat=' in s[j] or 'lon=' in s[j]:

         coordinates.append(s[j])

p = 1

k = str(coordinates).split('"')
lengthk = len(k)

for t in range(lengthk):
    if '1'in k[t] or'2'in k[t] or'3'in k[t] or'4'in k[t] or'5'in k[t] or'6'in k[t] or'7'in k[t] or'8'in k[t] or'9'in k[t] or'0' in k[t]:
        coordinates1.append(k[t])

distance = len(coordinates1)
for o in range(distance):
    if p%2==0:
        latitude.append(str(coordinates1[o]))
    if p%2!=0:
        longitude.append(str(coordinates1[o]))
    p=p+1

for q in range(int(len(latitude))):
    w=0

    while (w <int(len(cd))):
        checking(fl,fln,name,latitude,longitude,cd,q,w)

        w=w+1

counted = Counter(fln)
print(counted)


import os
import struct
import xml.etree.ElementTree as ET

from cvtdBusPosition import CvtdBusPosition
from cvtdBusPosition import pack_string

####
# A class containing information about buses and their locations
#
# pos -> A dictionary, where key is route number and value is a list of CvtdBusPosition's, sorted by timestamp
# dirPath -> Path to the directory where position information will be stored, with no final '/' (removed by init)
####
class CvtdBusLocator:
	def __init__(self, dirPath=None):
		self.pos = {}
		self.dirPath = dirPath
		if dirPath:
			while self.dirPath[-1] == '/':
				self.dirPath = self.dirPath[:-1]

	####
	# insert inserts a position into the locator such that CvtdBusPosition arrays are always sorted by timestamp
	#
	# key is the key of the bus into the pos dictionary, the route number
	# position is the CvtdBusPosition object to be inserted
	####
	def insert(self, key, position):
		# Add the key to self.pos if it is not there
		if key not in self.pos:
			self.pos[key] = []

		# If the position list is empty, or if the new timestamp will go at the end, no need to look for the right place to put it
		if len(self.pos[key]) == 0 or position.timestamp > self.pos[key][-1].timestamp:
			self.pos[key].append(position)
		else:
			# Starting at the back of the array, search backwards until we find a place where the past timestamp is greater
			ix = len(self.pos[key]) - 2
			while ix >= 0:
				if position.timestamp > self.pos[key][ix].timestamp:
					# We went back too far, go forward one spot
					self.pos[key].insert(ix+1, position)
					break
				elif position.timestamp == self.pos[key][ix].timestamp:
					# We found an exact match, don't bother adding
					return
				ix -= 1
			if ix == -1:
				self.pos[key].insert(0, position)

	####
	# find_next_after returns the CvtdBusPosition after a given timestamp
	#
	# key is the key of the bus into the pos dictionary
	# timestamp is the timestamp to find next position after
	#
	# return is the CvtdBusPosition, or None if none exists
	####
	def find_next_after(self, key, timestamp):
		ix = 0
		while ix < len(self.pos[key]):
			if self.pos[key][ix].timestamp > timestamp:
				return self.pos[key][ix]
			ix += 1

	####
	# find_next_before returns the CvtdBusPosition before a given timestamp
	#
	# key is the key of the bus into the pos dictionary
	# timestamp is the timestamp to find position before
	#
	# return is the CvtdBusPosition, or None if none exists
	####
	def find_next_before(self, key, timestamp):
		ix = len(self.pos[key])-1
		while ix >= 0:
			if self.pos[key][ix].timestamp < timestamp:
				return self.pos[key][ix]
			ix -= 1

	####
	# find searches for a bus position exactly matching the given timestamp
	#
	# key is the key of the bus into the pos dictionary
	# timestamp is the timestamp to find position for
	#
	# return is the CvtdBusPosition, or None if none exists
	####
	def find(self, key, timestamp):
		try:
			ix = len(self.pos[key])-1
			while ix >= 0:
				if self.pos[key][ix].timestamp == timestamp:
					return self.pos[key][ix]
				ix -= 1
		except KeyError:
			return None

	####
	# read_locator opens all files in self.dirPath and attempts to read them
	####
	def read_locator(self):
		if self.dirPath:
			for filename in os.listdir(self.dirPath):
				# File will be some/path/route_XYZ.dat. We want the XYZ
				rnum = filename.split('/')[-1][6:-4]
				with open("/".join((self.dirPath, filename)), 'rb') as f:
					data = f.read()
				offset = 0
				packsize = struct.calcsize(pack_string)
				while offset + packsize <= len(data):
					b = data[offset:offset+packsize]
					self.insert(rnum, CvtdBusPosition(b))
					offset = offset + packsize

	####
	# write_locator writes all of the buses in the locator to dirPath, i.e. "route_1.dat"
	####
	def write_locator(self):
		if self.dirPath:
			for key in self.pos:
				with open('/'.join((self.dirPath, f"route_{key}.dat")), 'wb') as f:
					for position in self.pos[key]:
						f.write(position.package())

	####
	# append_position_to_file is called to write a single new position to a file
	#
	# key is the route number of the position, or key into self.pos
	# position is the CvtdBusPosition object
	####
	def append_position_to_file(self, key, position):
		if self.dirPath:
			with open('/'.join((self.dirPath, f"route_{key}.dat")), 'ab') as f:
				f.write(position.package())

	####
	# insert_append calls the respective calls on a given position.
	#  NOTE: insert_append should ONLY be called when the position to be added will finish last in the locator
	#
	# TODO: insert into the file if the position is not at the end
	#
	# key is the route number of the position, or key into self.pos
	# position is the CvtdBusPosition object
	####
	def insert_append(self, key, position):
		self.insert(key, position)
		self.append_position_to_file(key, position)


#!/usr/bin/env python

"""
Command line program to call change metdatada in a file

"""
# This file is part of 'gdalutils'
# Copyright (C) 2014 Sam Gillingham
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import sys
import optparse
from lcrimageutils import history
from osgeo import gdal
from osgeo.gdalconst import *

class CmdArgs(object):
  def __init__(self):
    parser = optparse.OptionParser(usage="%prog [options]")
    parser.add_option("-d","--dest", dest="dest", help="Name of file metadata to be written to")
    parser.add_option("-o","--optional", dest="optionals", help="Any optional metadata for this file in the form: -o tag=value",action="append")
    self.parser = parser
    (options, args) = parser.parse_args()
    # Some magic to copy the particular fields into our final object
    self.__dict__.update(options.__dict__)

# Use the class above to create the command args object
cmdargs = CmdArgs()

if not cmdargs.dest or not cmdargs.optionals:
  cmdargs.parser.print_help()
  sys.exit()

ds = gdal.Open(cmdargs.dest,GA_Update)
obj = history.readTreeFromDataset(ds)

for opt in cmdargs.optionals:
  (key,value) = opt.split('=')
  obj.thismeta[key] = value

band = ds.GetRasterBand(1)
metadata = band.GetMetadata()

metadata[history.metadataName] = obj.toString()
band.SetMetadata(metadata)

del ds


import os, sys, re, itertools, subprocess, json, logging
from subprocess import PIPE, run

regex1=r'^LKG = .*$'
log_path="/home/taccuser/MLSEQA_Automation/rocm_components/test.log"
HOME = os.environ['HOME']

def ocl_get_lkgnum():
    res=os.popen("wget http://ocltc.amd.com/cgi-bin/teamcity_opencl_lkg.pl?justcl=1 -q -O - | tee test.log").read()
    match = re.findall(regex1, res)
    with open(log_path, "r") as file:
        for line in file:
            for match in re.finditer(regex1, line, re.S):
                if match:
                    var1 = match.group().split("=")
                    out = var1[1].strip()
                    ocl_get_lkgnum.var = out
                else:
                    print("result not found")

def ocl_get_lkgpkg():
    print(ocl_get_lkgnum.var)
    os.system("cd $HOME && wget -q -o /dev/null --user guest --password guest http://ocltc.amd.com:8111/repository/download/BuildsOpenCLHsaStaging_OpenCLLinuxPro_LinuxX8664ReleaseGfx9LinuxPro/{0}:id/opencl/tests/ocltst.zip".format(ocl_get_lkgnum.var))
    os.system("cd $HOME/ && pwd && unzip -o ocltst.zip")
    os.system("cd $HOME/ocltst/x86_64 && export LD_LIBRARY_PATH=$PWD")

ocl_get_lkgnum()
ocl_get_lkgpkg()


#!/usr/bin/env python
"""
Converts between Probely's Navigation sequences and Selenium.

We can not guarantee that the conversion will be 100% compatible.
"""
import argparse
import json
import re
import uuid
import os
from time import time

items_not_supported = []

def getUUID():
    return str(uuid.uuid4())

def getSeleniumCssAndXPath(targets, target):
    css = None
    xpath = None
    for item in targets:
        if item[1] == 'css:finder':
            css = item[0].replace('css=', '')
        if item[1] == 'xpath:position':
            xpath = item[0].replace('xpath=', '')

    if css is None and target:
        if target.startswith('name='):
            css = '[{}]'.format(target)
        elif target.startswith('id='):
            css = '[{}]'.format(target)
    return (css, xpath)

def convertProbely2Selenium(data, inputFp, outputFp=None):
    name = os.path.basename(inputFp.name)
    obj = {
        'id': getUUID(),
        'version': '2.0',
        'name': name,
        'url': None,
        'tests': [{
            'id': getUUID(),
            'name': name,
            'commands': []
        }],
        'suites': [{
            'id': getUUID(),
            'name': 'Default Suite',
            'persistSession': False,
            'parallel': False,
            'timeout': 300,
            'tests': [getUUID()]
        }],
        'urls': [],
        'plugins': []
    }
    if len(data) == 0:
        raise Exception('No data in Probely recording file to be converted.')

    for idx, item in enumerate(data):
        if idx == 0:
            if item['type'] == 'goto':
                m = re.search('(https?://[^/]+)(/?.*)', item.get('url'))
                url_base = m.group(1)
                url_path = m.group(2)
                obj['url'] = url_base
                obj['urls'].append(url_base)
                obj['tests'][0]['commands'].append({
                    'id': getUUID(),
                    'comment': '',
                    'command': 'open',
                    'target': url_path,
                    'targets': [],
                    'value': ''
                })
                obj['tests'][0]['commands'].append({
                    'id': getUUID(),
                    'comment': '',
                    'command': 'setWindowSize',
                    'target': '{}x{}'.format(item.get('windowWidth', '1800'), item.get('windowHeight', '1200')),
                    'targets': [],
                    'value': ''
                })
            else:
                raise Exception('First item must be a "goto" with an URL')
        else:
            css = 'css={}'.format(item.get('css'))
            xpath = 'xpath={}'.format(item.get('xpath'))
            if item.get('type') == 'click' or item.get('type') == 'bclick':
                obj['tests'][0]['commands'].append({
                    'id': getUUID(),
                    'comment': '',
                    'command': 'click',
                    'target': css,
                    'targets': [
                        [css, 'css:finder'],
                        [xpath, 'xpath:position']
                    ],
                    'value': ''
                })
            elif item.get('type') == 'mouseover':
                obj['tests'][0]['commands'].append({
                    'id': getUUID(),
                    'comment': '',
                    'command': 'mouseOver',
                    'target': css,
                    'targets': [
                        [css, 'css:finder'],
                        [xpath, 'xpath:position']
                    ],
                    'value': ''
                })
            elif item.get('type') == 'fill_value':
                obj['tests'][0]['commands'].append({
                    'id': getUUID(),
                    'comment': '',
                    'command': 'type',
                    'target': css,
                    'targets': [
                        [css, 'css:finder'],
                        [xpath, 'xpath:position']
                    ],
                    'value': item.get('value', '')
                })
            elif item.get('type') == 'press_key' and item.get('value') == 13:
                obj['tests'][0]['commands'].append({
                    'id': getUUID(),
                    'comment': '',
                    'command': 'sendKeys',
                    'target': css,
                    'targets': [
                        [css, 'css:finder'],
                        [xpath, 'xpath:position']
                    ],
                    'value': '${KEY_ENTER}'
                })
            elif item.get('type') == 'change' and item.get('subtype') == 'select':
                obj['tests'][0]['commands'].append({
                    'id': getUUID(),
                    'comment': '',
                    'command': 'select',
                    'target': css,
                    'targets': [
                        [css, 'css:finder'],
                        [xpath, 'xpath:position']
                    ],
                    'value': 'value={}'.format(item.get('value', ''))
                })
            elif item.get('type') == 'change' and item.get('subtype') == 'check':
                obj['tests'][0]['commands'].append({
                    'id': getUUID(),
                    'comment': '',
                    'command': 'click',
                    'target': css,
                    'targets': [
                        [css, 'css:finder'],
                        [xpath, 'xpath:position']
                    ],
                    'value': ''
                })
            elif item.get('type') == 'dbclick':
                obj['tests'][0]['commands'].append({
                    'id': getUUID(),
                    'comment': '',
                    'command': 'doubleClick',
                    'target': css,
                    'targets': [
                        [css, 'css:finder'],
                        [xpath, 'xpath:position']
                    ],
                    'value': ''
                })
            else:
                items_not_supported.append('{} => {}'.format(idx, item.get('type')))

    return obj

def convertSelenium2Probely(data):
    obj = []
    timestamp = int(time()) * 1000
    tests_arr = data.get('tests')
    if tests_arr is None or len(tests_arr) == 0:
        raise Exception('No tests in Selenium file to be converted.')

    commands_arr = tests_arr[0].get('commands')
    if commands_arr is None or len(commands_arr) == 0:
        raise Exception('No commands in Selenium file to be converted.')

    url_base = data.get('url')
    # get first url
    first_command = commands_arr[0].get('command')
    second_command = commands_arr[1].get('command')
    url_path = commands_arr[0].get('target')
    dimensions = commands_arr[1].get('target')
    if first_command != 'open' or second_command != 'setWindowSize':
        raise Exception('Selenium first command must be "open" or second command must be "setWindowSize"')
    if url_path is None:
        raise Exception('Selenium first command must have a "target"')

    windowWidth = 1800
    windowHeight = 1200
    if dimensions:
        m = re.search('([0-9]+)x([0-9]+)', dimensions)
        windowWidth = int(m.group(1))
        windowHeight = int(m.group(2))

    obj.append({
        'type': 'goto',
        'urlType': 'force',
        'url': '{}{}'.format(url_base, url_path),
        'timestamp': timestamp,
        'windowWidth': windowWidth,
        'windowHeight': windowHeight
    })
    for idx, item in enumerate(commands_arr):
        if idx == 0 or idx == 1:
            continue
        else:
            css, xpath = getSeleniumCssAndXPath(item.get('targets'), item.get('target'))
            if css is None:
                continue
            if item.get('command') == 'click':
                timestamp += 1000
                obj.append({
                    'timestamp': timestamp,
                    'type': 'click',
                    'css': css,
                    'xpath': xpath,
                    'value': '',
                    'frame': None
                })
            elif item.get('command') == 'mouseOver':
                timestamp += 1000
                obj.append({
                    'timestamp': timestamp,
                    'type': 'mouseover',
                    'css': css,
                    'xpath': xpath,
                    'value': '',
                    'frame': None
                })
            elif item.get('command') == 'type':
                timestamp += 1000
                obj.append({
                    'timestamp': timestamp,
                    'type': 'fill_value',
                    'css': css,
                    'xpath': xpath,
                    'value': item.get('value', ''),
                    'frame': None
                })
            elif item.get('command') == 'sendKeys' and item.get('value') == '${KEY_ENTER}':
                timestamp += 1000
                obj.append({
                    'timestamp': timestamp,
                    'type': 'press_key',
                    'css': css,
                    'xpath': xpath,
                    'value': 13,
                    'frame': None
                })
            elif item.get('command') == 'select':
                timestamp += 1000
                select_value = item.get('value', '').replace('label=', '').replace('value=', '')
                obj.append({
                    'timestamp': timestamp,
                    'type': 'change',
                    'subtype': 'select',
                    'css': css,
                    'xpath': xpath,
                    'value': select_value,
                    'selected': 1,  # hardcoded, not correct
                    'frame': None
                })
            elif item.get('command') == 'doubleClick':
                timestamp += 1000
                obj.append({
                    'timestamp': timestamp,
                    'type': 'dblclick',
                    'css': css,
                    'xpath': xpath,
                    'value': '',
                    'frame': None
                })
            else:
                items_not_supported.append('{} => {}'.format(idx, item.get('command')))

    return obj

def run():
    parser = argparse.ArgumentParser(description='Converts Probely navigation sequences to selenium or selenium to Probely navigation sequences')
    parser.add_argument('-c', '--convert', help='<probely2selenium>/<selenium2probely>', choices=['probely2selenium', 'selenium2probely'])
    parser.add_argument('-i', '--input', help='Input file', type=argparse.FileType('r'))
    parser.add_argument('-o', '--output', help='Output file', type=argparse.FileType('w'))
    args = parser.parse_args()

    if args.convert == 'probely2selenium' and not args.output.name.endswith('.side'):
        raise Exception('Error: Selenium file name must use ".side" extension')

    input_data = json.load(args.input)

    if args.convert == 'probely2selenium':
        out_data = convertProbely2Selenium(input_data, args.input)
    elif args.convert == 'selenium2probely':
        out_data = convertSelenium2Probely(input_data)

    final_json = json.dumps(out_data, indent=2)
    args.output.write(final_json)

    if len(items_not_supported) > 0:
        print('Warning:')
        print('Items not supported/converted from positions: {}'.format(str(items_not_supported)))

    print('Done.')
    print('File converted to file: {}'.format(args.output.name))

if __name__ == '__main__':
    run()


from flask import Flask, render_template
# import os

app = Flask(__name__)

@app.route('/plot/')
def plot():
    from IPython.core.display import display, HTML
    from string import Template
    import pandas as pd
    import json

    # d3js = HTML('')

    worldmap_data = json.loads(open('data/worldmap.json', 'r').read())
    sites_data_stations = pd.read_csv('data/stations.csv')
    sites_data_temps = pd.read_csv('data/monthly_temps.csv')
    sites_data_temps = sites_data_temps.sort_values(by='ID')

    temps_by_ID = []
    previous_ID = -1
    collected_temps = {}
    for i,row in sites_data_temps.iterrows():
        if (row['ID'] != previous_ID) and (previous_ID != -1):
            temps_by_ID.append(collected_temps)
            collected_temps = {}
        collected_temps[row['month']] = {'ave': row['ave'],
                                        'max': row['max'],
                                        'min': row['min']}
        previous_ID = row['ID']
    temps_by_ID.append(collected_temps)
    site_data_temps_2 = pd.DataFrame({'ID': sites_data_temps['ID'].unique(),
                                    'temps': temps_by_ID})
    # site_data_temps_2.head()

    sites_data = pd.merge(sites_data_stations, site_data_temps_2, on='ID')
    sites_data_dict = sites_data.to_dict(orient='records')

    # html_template = Template('''
    #
    #
<svg width="700" height="500px" id="graph-svg"></svg>

    #
    # ''')

    css_text = open('static/temperature_histories.css','r').read()
    js_text_template = Template(open('static/temperature_histories.js','r').read())
    js_text = js_text_template.safe_substitute({'worldmapdata': json.dumps(worldmap_data),
                                            'sitesdata': json.dumps(sites_data_dict) })
    # display(HTML(html_template.substitute({'css_text': css_text, 'js_text': js_text})))
    return render_template("plot.html",
    css_text=css_text,
    js_text=js_text)

if __name__ == '__main__':
    app.run(debug=True)


import numpy as np     #biblioteca numpy para trabalhar com matriz (img)
import RPi.GPIO as GPIO #usar as saidas do rasp para controlar o servo/ habilita a gpio ndo rasp para deixA la como se fosse um arduino
import cv2 #importa o opencv
from time import sleep

x=0
cap = cv2.VideoCapture(0) #acessa a camera na entrada padrao do rasp = 0

def nothing(x):
    pass

GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
pwm=GPIO.PWM(11,50)
pwm.start(5)

readings=[]
average=0
last_vlue=0

cv2.namedWindow('frame')

cv2.createTrackbar('Hl','frame',46,255,nothing)
cv2.createTrackbar('Hu','frame',87,255,nothing)
cv2.createTrackbar('Sl','frame',40,255,nothing)
cv2.createTrackbar('Su','frame',140,255,nothing)
cv2.createTrackbar('Vl','frame',0,255,nothing)
cv2.createTrackbar('Vu','frame',255,255,nothing)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    if ret==False:
        x+=1
        if x>10:
            break
        continue

    # Our operations on the frame come here
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    hl = cv2.getTrackbarPos('Hl', 'frame')
    sl = cv2.getTrackbarPos('Sl', 'frame')
    vl = cv2.getTrackbarPos('Vl', 'frame')
    hu = cv2.getTrackbarPos('Hu', 'frame')
    su = cv2.getTrackbarPos('Su', 'frame')
    vu = cv2.getTrackbarPos('Vu', 'frame')

    #lower_color = np.array([46, 40, 0])
    #upper_color = np.array([87, 140, 255])
    lower_color = np.array([hl, sl, vl])
    upper_color = np.array([hu, su, vu])

    mask = cv2.inRange(hsv, lower_color, upper_color)
    kernel = np.ones((5,5), np.uint8)
    mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
    mask = cv2.morphologyEx(mask, cv2.MORPH_DILATE, kernel)

    segimg=cv2.bitwise_and(frame, frame, mask= mask)

    __, contours,__ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    center = []

    for c in contours:
        M = cv2.moments(c)
        try:
            center.append([ int(M['m10']/M['m00']), int(M['m01']/M['m00']) ])

        except ZeroDivisionError:
            pass

        # print(M['m10'], M['m00'], c)

    bat = np.array(center)
    bat = np.mean(bat, axis = 0, dtype=int)
    #print(type(bat))
    #print(bat)
    if type(bat) is np.int32:
        print("Senhor do Universo")
    else:
        cv2.circle(segimg, tuple(bat), 2, (0,255,0), 10)

        bat = float(bat[0])/mask.shape[1]         #map para a resolucao da imagem e posicao no eixo x -> de 0 a 1
        #print(mask.shape)
        readings.append(bat)
        if(len(readings)>=10):
             readings.pop(0)
        average=float(sum(readings))/len(readings)
        MAX = 11
        MIN = 5
        average = average*(MIN-MAX)+MAX

        print(average)

        #if int(average)==last_vlue:
        #    pwm.stop()
        #else:
        #   pwm.start(last_vlue)
        pwm.ChangeDutyCycle(int(average))
        #pwm.stop()
        #last_vlue=int(average)

     # Display the resulting frame

    cv2.imshow('frame',segimg)
    '''cv2.imshow('mask',mask)
    cv2.imshow('hsv',hsv)'''
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()


import sys
sys.path.append('/Users/mohamedkhodeir/Documents/models/research')
sys.path.append('/Users/mohamedkhodeir/Documents/models/research/slim')
from google.protobuf import text_format
from object_detection.builders import box_predictor_builder
from object_detection.protos import box_predictor_pb2
import IPython
box_predictor_text_proto = '''keypoint_predictor {
    min_depth: 0
    max_depth: 0
    num_layers_before_predictor: 0
    use_dropout: false
    dropout_keep_probability: 0.8
    kernel_size: 1
    box_code_size: 4
    apply_sigmoid_to_scores: false
    conv_hyperparams {
      activation: RELU_6,
      regularizer {
        l2_regularizer {
          weight: 0.00004
        }
      }
      initializer {
        truncated_normal_initializer {
          stddev: 0.03
          mean: 0.0
        }
      }
      batch_norm {
        train: true,
        scale: true,
        center: true,
        decay: 0.9997,
        epsilon: 0.001,
      }
    }
}'''
box_predictor_proto = box_predictor_pb2.BoxPredictor()
text_format.Merge(box_predictor_text_proto, box_predictor_proto)

box_predictor_builder.build(argscope_fn=lambda x, y: 'Stuff', box_predictor_config=box_predictor_proto, is_training=True, num_classes=1)


from city.City import City

class StairsBuilding(object):

    def __init__(self, un_pos_x, un_pos_y, un_pos_z, un_size_x, un_size_y):
        unScaleX = un_size_x
        unScaleY = un_size_y

        # Duplicate Element from Modern 1 building.
        obj = bpy.data.objects['_ModernStairsBuildingElement']
        mesh = obj.data
        new_obj = bpy.data.objects.new('ModernStairsBuildingElement', mesh)

        fRand = random()

        if fRand < 0.25:
            # Put it at (un_pos_x - 0.5, un_pos_y - 0.5) * City.UNIT_VALUE
            new_obj.location = ((un_pos_x - 0.5) * City.UNIT_VALUE, (un_pos_y - 0.5) * City.UNIT_VALUE, un_pos_z * City.UNIT_VALUE)
        elif 0.25 <= fRand < 0.5:
            # Put it at (un_pos_x + un_size_x - 0.5, un_pos_y - 0.5) * City.UNIT_VALUE AND rotate 90 degrees z
            new_obj.location = ((un_pos_x + un_size_x - 0.5) * City.UNIT_VALUE, (un_pos_y - 0.5) * City.UNIT_VALUE, un_pos_z * City.UNIT_VALUE)
            new_obj.rotation_euler = (0, 0, math.pi / 2)
        elif 0.5 <= fRand < 0.75:
            # Put it at (un_pos_x + un_size_x - 0.5, un_pos_y + un_size_y - 0.5) * City.UNIT_VALUE AND rotate 180 degrees z
            new_obj.location = ((un_pos_x + un_size_x - 0.5) * City.UNIT_VALUE, (un_pos_y + un_size_y - 0.5) * City.UNIT_VALUE, un_pos_z * City.UNIT_VALUE)
            new_obj.rotation_euler = (0, 0, math.pi)
        else:
            # Put it at (un_pos_x - 0.5, un_pos_y + un_size_y - 0.5) * City.UNIT_VALUE AND rotate 270 degrees z
            new_obj.location = ((un_pos_x - 0.5) * City.UNIT_VALUE, (un_pos_y + un_size_y - 0.5) * City.UNIT_VALUE, un_pos_z * City.UNIT_VALUE)
            new_obj.rotation_euler = (0, 0, math.pi * 3 / 2)

        # Scale Element (unScaleX, unScaleY)
        new_obj.scale = (unScaleX, unScaleY, 1)
        --unScaleX
        --unScaleY
        z_tot = 1 + City.UNIT_VALUE
        new_obj.select = True
        scene.objects.link(new_obj)

        while unScaleX > 0 and unScaleY > 0:

            # Duplicate last placed element AND move it upwards (City.UNIT_VALUE)
            new_obj = bpy.data.objects.new('ModernStairsBuildingElement', mesh)
            new_obj.location = (0, 0, z_tot)
            new_obj.scale = (unScaleX, unScaleY, 1)
            z_tot += City.UNIT_VALUE
            new_obj.select = True
            scene.objects.link(new_obj)

            # Scale Element (unScaleX, unScaleY, 1)
            unScaleX -= 1
            unScaleY -= 1

        # Join all objects and remove doubles
        scene.objects.active = new_obj
        bpy.ops.object.join()
        bpy.ops.object.select_all(action='DESELECT')
        bpy.ops.object.mode_set(mode='EDIT')
        bpy.ops.mesh.select_all(action='SELECT')
        bpy.ops.mesh.remove_doubles()
        bpy.ops.object.mode_set(mode='OBJECT')


# Write a python program to find the square root of the given number
'''find the square rrot using bisection method'''
# using approximation method
# testcase 1
# input: 25
# output: 4.999999999999998
# testcase 2
# input: 49
# output: 6.999999999999991
def main():
    '''finding the perfect square'''
    square = int(input())
    # epsilon and step are initialized
    # don't change these values
    epsilon = 0.01
    #step = 0.1
    # your code starts here
    low = 0
    high = square
    answer = (high+low)/2
    while abs(answer**2-square) >= epsilon:
        #print('low='+str(low)+'high='+str(high)+'answer='+str(answer))
        if answer**2 < square:
            low = answer
        else:
            high = answer
        answer = (high+low)/2
    #pri
    print(str(answer))

if __name__ == "__main__":
    main()


import sqlite3
import requests
import json

#define connection
conn = sqlite3.connect('pokemon_y.db')
c = conn.cursor()

class User_Info:
    def __init__(self, username = None, level = None):
        self.username = username
        self.level = level

class Pokemon:
    def __init__(self, username = None, poke_name = None):
        self.username = username
        self.poke_name = poke_name

class Model:
    def __init__(self):
        pass

    def add_user_info(self, user_info): #add informaton to the userInfo table
        c.execute("""
            INSERT INTO userInfo (username, level) VALUES (?,?)
            """,
            (user_info.username, user_info.level)
        )
        conn.commit()
        c.close()

    def add_pokemon_info (self, pokeinfo): #add pokemon name to pokemon table
        get_species = requests.get("http://pokeapi.co/api/v2/pokemon/{x}".format(x=pokeinfo.poke_name))
        species_json = get_species.json()
        species = species_json['species']['name'] #get species name[12]

        get_habitat = requests.get("http://pokeapi.co/api/v2/pokemon-species/{x}".format(x=pokeinfo.poke_name))
        habitat_json = get_habitat.json()
        habitat = habitat_json['habitat']['name'] #get habitat name[13]
        c.execute("""
            INSERT INTO pokemon (username, poke_name, species_name, habitat_name) VALUES (?,?,?,?)
            """,
            (pokeinfo.username, pokeinfo.poke_name, species, habitat)
        )
        conn.commit() #save

        c.execute("""
            INSERT INTO species (poke_name, species_name, habitat_name) VALUES (?,?,?)
            """,
            (pokeinfo.poke_name, species, habitat)

        )
        conn.commit() #save

        c.execute("""
            INSERT INTO habitat (poke_name, poke_habitat, poke_species) VALUES (?,?,?)
            """,
            (pokeinfo.poke_name, habitat, species)
        )
        conn.commit() #save
        c.close()

    def add_pokemon_species (self, species):
        c.execute("""
            INSERT INTO species (poke_name, species_name, habitat_name) VALUES (?,?,?)
            """,
            (species.poke_name, species.species_name, species.habitat_name)
        )
        conn.commit() #save
        c.close()

    def add_pokemon_habitat (self, habitat):
        c.execute("""
            INSERT INTO habitat (poke_name, habitat_name, species_name) VALUES (?,?,?)
            """,
            (habitat.poke_name, habitat.habitat_name, habitat.species_name)
        )
        conn.commit() #save
        c.close()

    def read_from_userInfo(self, username): #show userInfo table
        c.execute("SELECT * FROM userInfo")
        [print(row) for row in c.fetchall()]
        conn.commit()
        c.close()

    def read_from_pokemon_by_user(self, username):
        c.execute("SELECT poke_name FROM pokemon where poke_name = ?", (username,))
        [print(row) for row in c.fetchall()]

    def read_from_poke_species_by_user(self, username):
        c.execute("SELECT species_name FROM pokemon where species_name = ?", (username,))
        [print(row) for row in c.fetchall()]

    def read_from_poke_habitat_by_user(self, username):
        c.execute("SELECT habitat_name FROM pokemon where habit_name = ?", (username,))
        c.execute("SELECT habitat_name FROM pokemon where habit_name = ?", (username,))
        [print(row) for row in c.fetchall()]

    def delete_user_table(self, deleteuser): #delete row where username = ?
        c.execute("DELETE FROM userInfo WHERE username = ?", (deleteuser,))
        [print(row) for row in c.fetchall()]
        conn.commit() #save
        c.close()

    def update_user_table(self, change_user): #change username
        c.execute("UPDATE userInfo SET username =? WHERE username ='bcho'", (change_user,))
        [print(row) for row in c.fetchall()]
        conn.commit() #save
        c.close()

    def species_count (self, species):
        c.execute("SELECT COUNT(*) FROM pokemon WHERE species = ?", (species,))
        [print(row) for row in c.fetchall()]
        conn.commit()

#c.close()
#conn.close()


import pandas as pd

pd.set_option("display.max_columns",166)
from IPython import get_ipython
ipy = get_ipython()
if ipy is not None:
    ipy.run_line_magic('matplotlib', 'inline')
    ipy.run_line_magic('pylab', 'inline')
import matplotlib.pyplot as plt
plt.style.use('ggplot')

TabelaInfra = ["NU_ANO_CENSO","CO_UF","CO_MUNICIPIO","IN_SALA_PROFESSOR","IN_LABORATORIO_INFORMATICA",
                "IN_LABORATORIO_CIENCIAS","IN_QUADRA_ESPORTES_COBERTA","IN_QUADRA_ESPORTES_DESCOBERTA","IN_COZINHA","IN_BIBLIOTECA",
                "IN_AUDITORIO","IN_PATIO_COBERTO","IN_PATIO_DESCOBERTO","NU_SALAS_EXISTENTES","NU_EQUIP_TV","NU_EQUIP_COPIADORA",
                "NU_EQUIP_IMPRESSORA","NU_EQUIP_SOM","NU_EQUIP_MULTIMIDIA","NU_COMP_ADMINISTRATIVO",
                "NU_COMP_ALUNO","IN_INTERNET","NU_FUNCIONARIOS","IN_ALIMENTACAO"]
ItensInfra = TabelaInfra[3:]
pesos = [20,50,40,50,25,20,40,25,30,15,5,3,10,5,10,4,5,3,20,10,40]
sequencia = list(range(len(ItensInfra)-1))

def agrupamentoTurmas(x):
    result = {
        'QTD_ALUNOS_POR_ESCOLA': x['NU_MATRICULAS'].sum()
    }
    return pd.Series(result)

def agrupamentoMunicipio(x):
    result = {'NU_ANO_CENSO': x['NU_ANO_CENSO'].min(),
              'CO_ENTIDADE': x['CO_ENTIDADE'].min(),
              'MEDIA_QTD_ALUNOS_POR_ESCOLA': x['QTD_ALUNOS_POR_ESCOLA'].mean(),
              'MEDIA_NU_COMP_PARA_ALUNOS': x['NU_COMP_ALUNO'].mean(),
              'COMP_POR_ALUNO(%)': x['NUM_COMP_POR_ALUNO'].mean(),
              'NUM_ESCOLAS_INTERNET_MUNIC': x['IN_INTERNET'].sum(),
              'MEDIA_SALAS_EXISTENTES': x['NU_SALAS_EXISTENTES'].mean(),
              'MEDIA_PONTUACAO': x['PONTUACAO'].mean()
    }
    return pd.Series(result)

def gerarTabelaItensEscolas(ano): # Gera uma tabela para correlacionar com algum índice
    dfEscolas = pd.read_csv('../Dados/escolas'+ano+'.csv',low_memory=False, encoding='ISO-8859-1', sep='|')
    dfEscolas["PONTUACAO"] = sum( list(map(lambda x,y: x * dfEscolas[ItensInfra[y]], pesos, sequencia)) )
    dfEscolas = dfEscolas[['NU_ANO_CENSO', 'CO_ENTIDADE', 'CO_MUNICIPIO' ,'NU_COMP_ALUNO', 'IN_INTERNET', 'NU_SALAS_EXISTENTES', 'PONTUACAO']]
    dfEscolas.to_csv('itensPorEscolas'+ano+'_out.csv','|')
    return dfEscolas

def gerarTabelaQtdAlunosPorEscola(ano): # Gera uma tabela com a qtd de alunos por escola
    dfTurmas = pd.read_csv('../Dados/turmas'+ano+'.csv',low_memory=False, encoding='ISO-8859-1', sep='|')
    dfTurmasAgrupado = dfTurmas.groupby('CO_ENTIDADE')
    tabelaQtdAlunosPorEscola = dfTurmasAgrupado.apply(agrupamentoTurmas)
    tabelaQtdAlunosPorEscola.to_csv('qtdAlunosPorEscola'+ano+'_out.csv','|')
    return tabelaQtdAlunosPorEscola

def mergeTabelas(ano): # Faz o merge de ItensEscolas com QtdAlunosPorEscola
    itensEscolas = pd.read_csv('../Dados/Correlacao/itensPorEscolas'+ano+'_out.csv',low_memory=False, encoding='ISO-8859-1', sep='|')
    qtdAlunosEscola = pd.read_csv('../Dados/Correlacao/qtdAlunosPorEscola'+ano+'_out.csv',low_memory=False, encoding='ISO-8859-1', sep='|')
    mergeItens_QtdAlunos = pd.merge(itensEscolas, qtdAlunosEscola, how='inner', on='CO_ENTIDADE')
    mergeItens_QtdAlunos["NUM_COMP_POR_ALUNO"] = 100 * mergeItens_QtdAlunos['NU_COMP_ALUNO']/mergeItens_QtdAlunos['QTD_ALUNOS_POR_ESCOLA'] #% de Comp por Aluno
    mergeItens_QtdAlunos.to_csv('itens_Por_Escola'+ano+'_out.csv','|')
    tabelaItens_Municipio = agruparMunicipio(ano,mergeItens_QtdAlunos)
    return mergeItens_QtdAlunos, tabelaItens_Municipio

def agruparMunicipio(ano,tabela):  #Tabela para correlacionar com IOEB
    tabelaAgrupada = tabela.groupby('CO_MUNICIPIO')
    tabelaItens_Municipio = tabelaAgrupada.apply(agrupamentoMunicipio)
    tabelaItens_Municipio.to_csv('../Dados/Correlacao/itens_Por_Municipio'+ano+'_out.csv','|')
    return tabelaItens_Municipio

lista_anos = ['2015','2017']

listaTabelasQtdAlunosPorEscola = list(map(lambda x: gerarTabelaQtdAlunosPorEscola(x), lista_anos)) #Roda só um vez, gerando as tabelas

listaTabelasItensEscolas = list(map(lambda x: gerarTabelaItensEscolas(x), lista_anos)) #Roda só um vez, gerando as tabelas

listaItensPorEscola, listaItensPorMunicipio = list(map(lambda x: mergeTabelas(x), lista_anos)) #Roda só um vez, gerando as tabelas


import re
regex = '^[A-Za-z][A-Za-z0-9_]*'
def check(string):
    if(re.search(regex, string)):
        print("Valid Identifier")
    else:
        print("Invalid Identifier")

if __name__ == '__main__' :

    string =raw_input()
    check(string)


# -*- coding: utf-8 -*-
"""
Created on Fri Mar 27 16:55:14 2015

@author: Arthur
"""

import turtle
import random
import time

from unicodedata import normalize
def formatar(txt):
    return normalize('NFKD',txt).encode('ASCII','ignore').decode('ASCII')

if __name__ == '__main__':
    from doctest import testmod
    testmod()

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

rights = 0
wrongs = 0      #número inicial de erros
used = []
victory = 0
defeat = 0

t = turtle.Turtle()
t.speed(10)

turtle1 = turtle.Turtle()
turtle1.speed(10)

turtle2 = turtle.Turtle()
turtle2.speed(10)

tartaruga = turtle.Turtle()
tartaruga.speed(10)

tartaruga2 = turtle.Turtle()
tartaruga2.speed(10)

tartaruga3 = turtle.Turtle()
tartaruga3.speed(10)

tartaruga4 = turtle.Turtle()
tartaruga4.speed(10)

tartaruga4 = turtle.Turtle()
tartaruga4.speed(10)

tartaruga5 = turtle.Turtle()
tartaruga5.speed(10)

tartaruga5 = turtle.Turtle()
tartaruga5.speed(10)

tartaruga6 = turtle.Turtle()
tartaruga6.speed(10)

tartaruga7 = turtle.Turtle()
tartaruga7.speed(10)

tartaruga8 = turtle.Turtle()
tartaruga.speed(10)
tartaruga8.shape("turtle")

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

def draw_spaces():        #desenha as linhas para as palvras
    print("draw_spaces")
    turtle1.shape("turtle")

    for i in range(len(secret_word)):
        if secret_word == " ":
            turtle1.penup()
            turtle1.setpos((-310+i *30,-265))
            turtle1.write(" ",False,font = ("Arial",15))
            turtle1.hideturtle()

        elif secret_word[i] == " ":
            turtle1.penup()
            turtle1.setpos((-310+i *30,-265))
            turtle1.write(" ",False,font = ("Arial",15))
            turtle1.hideturtle()
        else:
            turtle1.penup()
            turtle1.setpos((-310+i *30,-265))
            turtle1.write("_",False,font = ("Arial",15))
            turtle1.hideturtle()

def PlayAgain():
    turtle.setpos(-250,0)
    resposta = turtle.textinput("Jogo da Forca","Jogar de novo?")
    if resposta.isalpha() and len(resposta) > 0:
        if resposta == "sim" or resposta == "SIM" or resposta == "Sim":
            print("a")
            turtle.clear()
            turtle1.clear()
            turtle2.clear()

            t.clear()
            tartaruga.clear()
            tartaruga2.clear()
            tartaruga3.clear()
            tartaruga4.clear()
            tartaruga5.clear()
            tartaruga6.clear()
            tartaruga7.clear()
            turtle.resetscreen()

            window.clearscreen
            jogo = True
#------------------------------------------------------------------------------------------------------------

        elif resposta == "não" or resposta == "Não" or resposta == "NÃO":

            GameOver()     #fim de jogo

            t.clear()
            turtle2.clear()
            tartaruga.clear()
            tartaruga2.clear()
            tartaruga3.clear()
            tartaruga4.clear()
            tartaruga5.clear()
            tartaruga6.clear()
            tartaruga7.clear()

            window.clearscreen()
            jogo == False

#------------------------------------------------------------------------------------------------------------------------------
def YouWin():

    turtle1.setpos(-100,0)
    turtle1.write("You won!", move=False, align="left", font=("Arial", 30, "normal"))
    time.sleep(1)
    turtle1.clear()
    turtle1.hideturtle()

def GameOver():
    turtle1.setpos(-100,0)

    turtle1.write("Game Over", move=False, align="left", font=("Arial", 30, "normal"))
    turtle1.hideturtle()

def Scoreboard():
    t.penup()
    t.setpos(150,50)
    t.pendown()
    t.write("vitórias: %d" % (victory), move=False, align="left", font=("Arial",20, "normal"))
    t.penup()
    t.setpos(150,-30)
    t.write("derrotas: %d" % (defeat), move=False, align="left", font=("Arial",20, "normal"))
    t.hideturtle()
def ChutesMedia():
    Media = len(used) / victory + defeat
    return Media
    tartaruga8.penup
    tartaruga8.setpos(150,-250)
    tartaruga8.write("chutes/palavras: %d" % (Media), move=False, align="left", font=("Arial",20, "normal"))

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

def forca():          #fazer a forca
    window = turtle.Screen()
    window.bgcolor("white")
    window.title("Jogo da Forca")
    tartaruga = turtle.Turtle()
    tartaruga.speed(10)
    tartaruga.penup()
    tartaruga.setpos(-600,-300)
    tartaruga.color("black")
    tartaruga.pendown()
    for i in range (1):
        tartaruga.forward(300)
        tartaruga.backward(150)
        tartaruga.left(90)
        tartaruga.forward(600)
        tartaruga.right(90)
        tartaruga.forward(150)
        tartaruga.right(90)
        tartaruga.forward(90)
        tartaruga.right(90)
        tartaruga.forward(75)
        tartaruga.backward(150)
        tartaruga.forward(75)
        tartaruga.penup()

def head():       #forca
    tartaruga2 = turtle.Turtle()
    tartaruga2.speed(10)
    tartaruga2.penup()
    tartaruga2.setpos(-300,110)
    tartaruga2.color("red")
    tartaruga2.pendown()
    for i in range (1):
        tartaruga2.circle(50)
        tartaruga2.heading()
        tartaruga2.penup()
        tartaruga2.hideturtle()

def body():        #corpo
    tartaruga3 = turtle.Turtle()
    tartaruga3.speed(10)
    tartaruga3.penup()
    tartaruga3.setpos(-300,110)
    tartaruga3.color("red")
    tartaruga3.pendown()
    for i in range (1):
        tartaruga3.right(90)
        tartaruga3.forward(200)
        tartaruga3.penup()
        tartaruga3.hideturtle()

def arm1():          #braço
    tartaruga4 = turtle.Turtle()
    tartaruga4.speed(10)
    tartaruga4.penup()
    tartaruga4.setpos(-300,10)
    tartaruga4.color("red")
    tartaruga4.pendown()
    for i in range (1):
        tartaruga4.right(45)
        tartaruga4.forward(100)
        tartaruga4.penup()
        tartaruga4.hideturtle()

def arm2():            #braço
    tartaruga5 = turtle.Turtle()
    tartaruga5.speed(10)
    tartaruga5.penup()
    tartaruga5.setpos(-300,10)
    tartaruga5.color("red")
    tartaruga5.pendown()
    for i in range (1):
        tartaruga5.right(135)
        tartaruga5.forward(100)
        tartaruga5.penup()
        tartaruga5.hideturtle()

def leg1():         #braço
    tartaruga6 = turtle.Turtle()
    tartaruga6.speed(10)
    tartaruga6.penup()
    tartaruga6.setpos(-300,-90)
    tartaruga6.color("red")
    tartaruga6.pendown()
    for i in range (1):
        tartaruga6.right(45)
        tartaruga6.forward(100)
        tartaruga6.hideturtle()
def leg2():              #perna
    tartaruga7 = turtle.Turtle()
    tartaruga7.speed(10)
    tartaruga7.penup()
    tartaruga7.setpos(-300,-90)
    tartaruga7.color("red")
    tartaruga7.pendown()
    for i in range (1):
        tartaruga7.right(135)
        tartaruga7.forward(100)
        tartaruga7.hideturtle()

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

file = open("entrada.txt", encoding = "utf - 8")    #Abrir a lista com as palavras
lista = file.readlines()
jogo = True
while jogo == True:
    #MediaPalavra()
    Scoreboard()
    window = turtle.Screen()
    window.bgcolor("white")
    window.setup(width=1500,height =700, startx = 0,starty=0)

    a = random.choice(lista)     #escolha aleatória da palavra secreta
    secret_word2 = formatar(a)
    secret_word = secret_word2.lower()
    secret_word = secret_word.strip()

    b = secret_word.count(" ")
    lista.remove(a)

    forca()
    draw_spaces()

    if b > 0:
        rights = rights + b

    while wrongs != 6 and rights != len(secret_word):
        print("rights ", rights)

        print(secret_word)
        guess = turtle.textinput("Jogo da Forca","Guess a letter")

        if len(guess) > 0 and guess.isalpha():

            if guess in used:         #quando se digitar uma letra que já foi usada
                turtle2.setpos(-150,0)
                turtle2.write("Already used", move=False, align="left", font=("Arial",35, "normal"))
                time.sleep(1)
                turtle2.clear()
                turtle2.hideturtle()
            elif guess in secret_word:
                turtle.penup()
                turtle.setpos(0,0)
                used.append(guess)    #adicionar a letra(guess) numa lista(used)

                for i in range(len(secret_word)):  #desenhar a letra se esta for a correta

                    if guess == secret_word[i]:
                        rights += 1
                        turtle.setpos(i *30 - 305, - 265)

                        turtle.pendown()
                        turtle.write(a[i], True, align="center",font = ("Arial",15,"normal"))
                        turtle.penup()
                        turtle.hideturtle()

#-------------------------------------------------------------------------------------------------------------------------
            elif guess not in secret_word:
                turtle2.setpos(-100,0)

                turtle2.write("Wrong", move=False, align="left", font=("Arial",35, "normal"))
                time.sleep(1)
                turtle2.clear()
                turtle2.hideturtle()

                wrongs += 1       #contagem de erro
                used.append(guess)   #adicionar a letra na lista 'used'

                if wrongs < 6:           #condições do número de erros
                    if wrongs == 1:
                        head()
                    elif wrongs == 2:
                        body()
                    elif wrongs == 3:
                        arm1()
                    elif wrongs == 4:
                        arm2()
                    elif wrongs == 5:
                        leg1()

        t.penup()
        t.setpos(100,150)
        t.pendown()
        t.write(used, move=False, align="left", font=("Arial",25, "normal"))
        t.hideturtle

    if rights == len(secret_word):
        rights = 0
        wrongs = 0
        del used[:]
        i = 0
        YouWin()
        victory += 1
        ChutesMedia()
        PlayAgain()

    elif wrongs == 6:
        rights = 0
        wrongs = 0
        del used[:]
        i = 0
        leg2()
        GameOver()
        defeat +=1
        ChutesMedia()
        PlayAgain()
#--------------------------------------------------------------------------------------------------------------
    t.penup()
    t.setpos(100,150)
    t.pendown()
    t.write(used, move=False, align="left", font=("Arial",25, "normal"))
    t.hideturtle

turtle.exitonclick()


from django import forms
from django.forms.util import to_current_timezone
from django.conf import settings
from django.utils import translation
from django.utils.safestring import mark_safe

default_date_format = getattr(settings, 'DATE_INPUT_FORMATS', None)
if default_date_format:
    default_date_format = str(default_date_format[0])

default_time_format = getattr(settings, 'TIME_INPUT_FORMATS', None)
if default_time_format:
    default_time_format = str(default_time_format[0])

def javascript_date_format(python_date_format):
    format = python_date_format.replace(r'%Y', 'yyyy')
    format = format.replace(r'%m', 'mm')
    format = format.replace(r'%d', 'dd')
    if '%' in format:
        format = ''
    if not format:
        format = 'yyyy-mm-dd'
    return format

def javascript_time_format(python_time_format):
    format = python_time_format.replace(r'%H', 'HH')
    format = format.replace(r'%M', 'MM')
    format = format.replace(r'%S', 'SS')
    if '%' in format:
        format = ''
    if not format:
        format = 'HH:MM:SS'
    return format

def add_to_css_class(classes, new_class):
    new_class = new_class.strip()
    if new_class:
        # Turn string into list of classes
        classes = classes.split(" ")
        # Strip whitespace
        classes = [c.strip() for c in classes]
        # Remove empty elements
        classes = filter(None, classes)
        # Test for existing
        if not new_class in classes:
            classes.append(new_class)
            # Convert to string
        classes = u" ".join(classes)
    return classes

def create_prepend_append(**kwargs):
    bootstrap = {}
    bootstrap['append'] = kwargs.pop('append', None)
    bootstrap['prepend'] = kwargs.pop('prepend', None)
    return bootstrap, kwargs

class BootstrapUneditableInput(forms.TextInput):

    def render(self, name, value, attrs=None):
        if attrs is None:
            attrs = {}
        attrs['type'] = 'hidden'
        klass = add_to_css_class(self.attrs.pop('class', ''), 'uneditable-input')
        klass = add_to_css_class(klass, attrs.pop('class', ''))
        base = super(BootstrapUneditableInput, self).render(name, value, attrs)
        return mark_safe(base + u'%s' % (klass, value))

class BootstrapTextInput(forms.TextInput):

    def __init__(self, *args, **kwargs):
        self.bootstrap, kwargs = create_prepend_append(**kwargs)
        super(BootstrapTextInput, self).__init__(*args, **kwargs)

class BootstrapPasswordInput(forms.PasswordInput):

    def __init__(self, *args, **kwargs):
        self.bootstrap, kwargs = create_prepend_append(**kwargs)
        super(BootstrapPasswordInput, self).__init__(*args, **kwargs)

class BootstrapDateInput(forms.DateInput):

    bootstrap = {
        'append': mark_safe(''),
        'prepend': None,
    }

    class Media:
        js = (
            settings.STATIC_URL + 'datepicker/js/bootstrap-datepicker.js',
        )
        lang = translation.get_language().split('-')[0].lower()
        if lang != 'en':
            js = js + (
                settings.STATIC_URL + 'datepicker/js/locales/bootstrap-datepicker.%s.js' % lang,
            )
        js = js + (
            settings.STATIC_URL + 'bootstrap_toolkit/js/init_datepicker.js',
        )
        css = {
            'screen': (
                settings.STATIC_URL + 'datepicker/css/datepicker.css',
            )
        }

    def render(self, name, value, attrs=None):
        if attrs is None:
            attrs = {}
        format = self.format
        if not format:
            format = default_date_format
        attrs['data-date-format'] = javascript_date_format(format)
        attrs['data-date-language'] = translation.get_language().split('-')[0].lower()
        attrs['data-bootstrap-widget'] = 'datepicker'
        return super(BootstrapDateInput, self).render(name, value, attrs)

class BootstrapTimeInput(forms.TimeInput):

    bootstrap = {
        'append': mark_safe(''),
        'prepend': None,
    }

    class Media:
        js = (
            settings.STATIC_URL + 'timepicker/js/bootstrap-timepicker.js',
        )
        lang = translation.get_language().split('-')[0].lower()
        if lang != 'en':
            js = js + (
                settings.STATIC_URL + 'timepicker/js/locales/bootstrap-timepicker.%s.js' % lang,
            )
        js = js + (
            settings.STATIC_URL + 'bootstrap_toolkit/js/init_timepicker.js',
        )
        css = {
            'screen': (
                settings.STATIC_URL + 'timepicker/css/timepicker.css',
            )
        }

    def render(self, name, value, attrs=None):
        if attrs is None:
            attrs = {}
        format = self.format
        if not format:
            format = default_time_format
        attrs['data-time-format'] = javascript_time_format(format)
        attrs['data-time-language'] = translation.get_language().split('-')[0].lower()
        attrs['data-bootstrap-widget'] = 'timepicker'
        return super(BootstrapTimeInput, self).render(name, value, attrs)

class BootstrapDateTimeInput(forms.MultiWidget):

    def __init__(self, attrs=None):
        widgets = (BootstrapDateInput(attrs=attrs),
                   BootstrapTimeInput(attrs=attrs))
        super(BootstrapDateTimeInput, self).__init__(widgets, attrs)

    def decompress(self, value):
        if value:
            value = to_current_timezone(value)
            return [value.date(), value.time().replace(microsecond=0)]
        return [None, None]


import numpy as np
from gfmod import *

# nyquist frequency
wn, Greal, Gimag = np.loadtxt('hello', unpack = True)
giwn = Greal + Gimag*1j
ntau = 10*len(wn)
beta = 100
gtau = gftools.GreenInverseFourier(giwn, ntau, beta, (1, 0, 0))
giwn = gftools.GreenFourier(gtau, len(wn), beta)

tau = np.linspace(0, beta, ntau)
np.savetxt('hello-tau', np.array([tau, gtau]).T)
'''
n = 0
for i in range (10000000):
	n += np.sin(i)
print(n)
'''

"""
niwn = 60
ntau = 121
beta = 20
mu = 0
V = 1
D = 1
nmc = 3
nmeas = 100
Uarr = [1.0, 5.0]
seed = 0

giwn = gftools.SemiCircularGreen(niwn, beta, mu, V, D)
gtau = gftools.GreenInverseFourier(giwn, ntau, beta, (1, 0, 0))

import matplotlib.pyplot as plt
tau = np.linspace(0, beta, len(gtau))
for i, U in enumerate(Uarr):
    solver = qmc.hfqmc(gtau, beta, U, seed)
    gtau_up, err_up, gtau_dw, err_dw = solver.meas(nmc, nmeas)
    plt.errorbar(tau, (gtau_up + gtau_dw)/2.0, (err_up + err_dw)/2, marker = 'o',
      markersize = 5, linewidth = 1, capsize = 5, label = r'$U=%.2f$'%U)
plt.legend(loc = 'best', fontsize = 14, edgecolor = 'None', framealpha = 0.0)
plt.tick_params(which = 'both', direction = 'in', labelsize = 14)
plt.xlabel(r'$\tau$', fontsize = 14)
plt.ylabel(r'$G(\tau)$', fontsize = 14)
plt.show()
"""


'''
归并排序
'''
def merge_sort(nums):
    # print(nums)

    if len(nums) < 2:
        return nums
    mid = len(nums) // 2
    left = merge_sort(nums[:mid])
    right = merge_sort(nums[mid:])
    return merge(left, right)

def merge(left, right):
    new_list = []
    l_index = 0
    r_index = 0
    while l_index < len(left) and r_index < len(right):
        if left[l_index][0] > right[r_index][0]:
            new_list.append(right[r_index])
            r_index += 1
        else:
            new_list.append(left[l_index])
            l_index += 1

    new_list.extend(left[l_index:])
    new_list.extend(right[r_index:])

    print(new_list)
    return new_list

if __name__ == '__main__':
    nums =  [[74,78],[61,63],[46,50],[51,54],[50,50],[60,64],[39,42],[25,27],[91,95]]
    l = merge_sort(nums)
    print(l)


"""Subscriptions forms."""

from django import forms
from django.urls import reverse
from django.utils.translation import gettext_lazy as _

from readthedocs.subscriptions.models import Plan

class PlanForm(forms.Form):

    """Form to create a subscription after the previous one has ended."""

    plan = forms.ChoiceField()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['plan'].choices = [
            (plan.pk, f'{plan.name} (${plan.price})')
            for plan in Plan.objects.filter(published=True).order_by('price')
        ]
        pricing_page = reverse('pricing')
        self.fields['plan'].help_text = _(
            f'Check our pricing page '
            'for more information about each plan.'
        )