import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable

sentences = [ "i like dog", "i love coffee", "i hate milk"]

word_list = " ".join(sentences).split()
print(word_list)
word_list = list(set(word_list))
print(word_list)
word_dict = {w: i for i, w in enumerate(word_list)}
print(word_dict)
number_dict = {i: w for i, w in enumerate(word_list)}
print(number_dict)
n_class = len(word_dict) # number of Vocabulary

# NNLM Parameter
n_step = 2 # n-1 in paper
n_hidden = 2 # h in paper
m = 2 # m in paper

def make_batch(sentences):
    input_batch = []
    target_batch = []

    for sen in sentences:
        word = sen.split()
        input = [word_dict[n] for n in word[:-1]]
        target = word_dict[word[-1]]

        input_batch.append(input)
        target_batch.append(target)

    return input_batch, target_batch

input_batch, target_batch = make_batch(sentences)
input_batch = Variable(torch.LongTensor(input_batch))
target_batch = Variable(torch.LongTensor(target_batch))
print(input_batch, target_batch)

input_size = n_step * m
hidden_size = n_hidden

class NNLM(nn.Module):
    def __init__(self, input_size, hidden_size, n_class):
        super(NNLM, self).__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.n_class = n_class
        self.emb = nn.Embedding(n_class, m)
        self.fc1 = nn.Linear(self.input_size, self.hidden_size)
        self.fc2 = nn.Linear(self.hidden_size, self.n_class)
    def forward(self, x):
        x = self.emb(x)
        x = x.view(-1, self.input_size)
        x = self.fc1(x)
        x = torch.tanh(x)
        output = self.fc2(x)
        return output

model = NNLM(input_size, hidden_size, n_class)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# Training
for epoch in range(5000):
    output = model(input_batch)
    loss = criterion(output, target_batch)
    if (epoch + 1)%1000 == 0:
        print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.6f}'.format(loss))
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

# Predict
predict = model(input_batch).data.max(1, keepdim=True)[1]

print(predict)
# Test
print([sen.split()[:2] for sen in sentences], '->', [number_dict[n.item()] for n in predict.squeeze()])
#squeeze 去掉维度值为1的维度 item:返回张量的值


from pathlib import Path

def read_daily_input(day, separator='\n'):
    input_path = "../inputs/"+day+".txt"
    path = Path(__file__).parent / input_path

    with path.open() as f:
        data = f.read()

    data = data.split(separator)

    return [i for i in data if len(i)!=0]


import cv2
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import multivariate_normal
from matplotlib.patches import Ellipse

UBIT = 'damirtha'
np.random.seed(sum([ord(c) for c in UBIT]))

def applySIFT(image):

    sift = cv2.xfeatures2d.SIFT_create()

    keypointsImage, descriptorImage = sift.detectAndCompute(image,None)

    return keypointsImage, descriptorImage

def getGoodMatches(descriptorImage1, descriptorImage2):

    bf = cv2.BFMatcher()
    matches = bf.knnMatch(descriptorImage1, descriptorImage2, k=2)

    goodList = []
    good = []

    for m,n in matches:
        if m.distance < 0.75*n.distance:
            goodList.append([m])
            good.append(m)

    return goodList, good

def getInliers(mask, num=10):
    matchesMask = mask.ravel().tolist()
    indices = []
    for ind in range(len(matchesMask)):
        if matchesMask[ind] == 1:
            indices.append(ind)
    matchesMask = [0]*len(matchesMask)
    np.random.shuffle(indices)
    indices = indices[:num]
    for ind in indices:
            matchesMask[ind] = 1
    return matchesMask

image1=cv2.imread('Images/tsucuba_left.jpg')
image2=cv2.imread('Images/tsucuba_right.jpg')

keypointsImage1, descriptorImage1 = applySIFT(image1)
keypointsImage2, descriptorImage2 = applySIFT(image2)

Image1Keypoints=cv2.drawKeypoints(image1,keypointsImage1,None)
cv2.imwrite('Results/task2_sift1.jpg',Image1Keypoints)
Image2Keypoints=cv2.drawKeypoints(image2,keypointsImage2,None)
cv2.imwrite('Results/task2_sift2.jpg',Image2Keypoints)

goodList, good = getGoodMatches(descriptorImage1, descriptorImage2)

imagePlot = cv2.drawMatchesKnn(image1,keypointsImage1,image2,keypointsImage2,goodList,None,flags=2)
cv2.imwrite('Results/task2_matches_knn.jpg',imagePlot)

ptsImage1 = np.int32(np.round([keypointsImage1[m.queryIdx].pt for m in good]).reshape(-1,1,2))
ptsImage2 = np.int32(np.round([keypointsImage2[m.trainIdx].pt for m in good]).reshape(-1,1,2))

F, mask = cv2.findFundamentalMat(ptsImage1,ptsImage2,cv2.RANSAC,1)

matchesMask = getInliers(mask, 10)
inlierImage = cv2.drawMatches(image1,keypointsImage1,image2,keypointsImage2,
                              good,None,matchesMask = matchesMask,flags = 2)

ptsImage1 = ptsImage1[np.array(matchesMask).ravel() == 1]
ptsImage2 = ptsImage2[np.array(matchesMask).ravel() == 1]

h ,w, d = image1.shape
for i in range(len(ptsImage1)):

    color = tuple(np.random.randint(0,255,3).tolist())

    line2 = cv2.computeCorrespondEpilines(ptsImage1[i], 1, F)

    line1 = cv2.computeCorrespondEpilines(ptsImage2[i], 2, F)

    # Compute 2 sample points on each line for plotting
    p1 = map(int , [0,-line1.ravel()[2]/line1.ravel()[1]])
    p2 = map(int, [w, -(line1.ravel()[2]+line1.ravel()[0]*w)/line1.ravel()[1]])
    p3 = map(int , [0,-line2.ravel()[2]/line2.ravel()[1]])
    p4 = map(int, [w, -(line2.ravel()[2]+line2.ravel()[0]*w)/line2.ravel()[1]])

    image1EpipolarLines = cv2.line(image1, tuple(p1), tuple(p2), color,1)
    image2EpipolarLines = cv2.line(image2, tuple(p3), tuple(p4), color,1)

    image1Final = cv2.circle(image1EpipolarLines, tuple(ptsImage1[i].ravel()), 2, color,5)
    image2Final = cv2.circle(image2EpipolarLines, tuple(ptsImage2[i].ravel()), 2, color,5)

print('Fundamental Matrix:')
print(F)

cv2.imwrite('Results/task2_epi_right.jpg',image2Final)
cv2.imwrite('Results/task2_epi_left.jpg',image1Final)

stereo = cv2.StereoBM_create(numDisparities=64, blockSize=21)
disparity = stereo.compute(cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY),cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY))

plt.imsave('Results/task2_disparity.jpg',disparity, cmap='gray')

Reconnaissance d_objets avec le descripteur SIFT-TP2/main.py


class AIChatBot:
	def __init__(self, std_id, op,a,b):
		self.std_id = std_id
		self.op = op
		self.a =a
		self.b= b
	def showSubjectName(self):
		print('AI for robot system')
	def showStudentYear(self):
		print(self.std_id[0])
	def calculator(self):
		if self.op == '+':
			o = self.a+self.b
		elif self.op=='-':
			o = self.a-self.b
		print(o)
while(1):
	i = input()
	if i == 'subject':
		AI = AIChatBot(1,2,3,4)
		AI.showSubjectName()
	if i == 'year':
		j=input()
		AI = AIChatBot(j,'a','b','c')
		AI.showStudentYear()
	if i == 'calc':
		j=input()
		a=int(input())
		b=int(input())
		AI = AIChatBot('a',j,a,b)
		AI.calculator()


"""versi3

Revision ID: c42d72b6832a
Revises:
Create Date: 2020-01-13 15:15:16.663281

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'c42d72b6832a'
down_revision = None
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('app_user',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('username', sa.String(), nullable=True),
    sa.Column('password', sa.String(), nullable=True),
    sa.Column('nama', sa.String(), nullable=True),
    sa.Column('jabatan', sa.String(), nullable=True),
    sa.Column('email', sa.String(), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('email'),
    sa.UniqueConstraint('username')
    )
    op.create_table('dokter',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('dokter_id', sa.String(), nullable=True),
    sa.Column('nama', sa.String(), nullable=True),
    sa.Column('statusPegawai', sa.String(), nullable=True),
    sa.Column('notelp', sa.Integer(), nullable=True),
    sa.Column('alamat', sa.String(), nullable=True),
    sa.Column('spesialis', sa.String(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_dokter_dokter_id'), 'dokter', ['dokter_id'], unique=True)
    op.create_index(op.f('ix_dokter_nama'), 'dokter', ['nama'], unique=False)
    op.create_index(op.f('ix_dokter_notelp'), 'dokter', ['notelp'], unique=False)
    op.create_table('lab',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('nama', sa.String(), nullable=True),
    sa.Column('kepalaLab', sa.String(), nullable=True),
    sa.Column('alatTes', sa.String(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_lab_nama'), 'lab', ['nama'], unique=False)
    op.create_table('pasien',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('nama', sa.String(), nullable=True),
    sa.Column('noKTP', sa.String(), nullable=True),
    sa.Column('tglLahir', sa.Date(), nullable=True),
    sa.Column('jenisKelamin', sa.String(), nullable=True),
    sa.Column('alamat', sa.String(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_pasien_alamat'), 'pasien', ['alamat'], unique=False)
    op.create_index(op.f('ix_pasien_nama'), 'pasien', ['nama'], unique=False)
    op.create_index(op.f('ix_pasien_noKTP'), 'pasien', ['noKTP'], unique=True)
    op.create_table('alat',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('lab_id', sa.Integer(), nullable=True),
    sa.Column('nama', sa.String(), nullable=True),
    sa.Column('harga', sa.Float(), nullable=True),
    sa.ForeignKeyConstraint(['lab_id'], ['lab.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_alat_lab_id'), 'alat', ['lab_id'], unique=False)
    op.create_index(op.f('ix_alat_nama'), 'alat', ['nama'], unique=True)
    op.create_table('check_in',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('PasienID', sa.String(), nullable=True),
    sa.Column('Statusregistrasi', sa.String(), nullable=True),
    sa.Column('metodebayar', sa.String(), nullable=True),
    sa.Column('statusbayar', sa.String(), nullable=True),
    sa.Column('waktuMulai', sa.DateTime(), nullable=True),
    sa.Column('waktuSelesai', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['PasienID'], ['pasien.noKTP'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_check_in_PasienID'), 'check_in', ['PasienID'], unique=False)
    op.create_table('perawat',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('lab_id', sa.Integer(), nullable=True),
    sa.Column('nama', sa.String(), nullable=True),
    sa.Column('statusPegawai', sa.String(), nullable=True),
    sa.Column('notelp', sa.Integer(), nullable=True),
    sa.Column('alamat', sa.String(), nullable=True),
    sa.ForeignKeyConstraint(['lab_id'], ['lab.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_perawat_lab_id'), 'perawat', ['lab_id'], unique=False)
    op.create_index(op.f('ix_perawat_nama'), 'perawat', ['nama'], unique=False)
    op.create_table('tesLab',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('CheckInID', sa.Integer(), nullable=True),
    sa.Column('PerawatID', sa.Integer(), nullable=True),
    sa.Column('alatID', sa.String(), nullable=True),
    sa.Column('dokterPJ', sa.String(), nullable=True),
    sa.Column('noUrut', sa.Integer(), autoincrement=True, nullable=True),
    sa.Column('statusVerif', sa.String(), nullable=True),
    sa.Column('hasil', sa.TEXT(), nullable=True),
    sa.Column('waktuMulai', sa.DateTime(), nullable=True),
    sa.Column('waktuSelesai', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['CheckInID'], ['check_in.id'], ),
    sa.ForeignKeyConstraint(['PerawatID'], ['perawat.id'], ),
    sa.ForeignKeyConstraint(['alatID'], ['alat.nama'], ),
    sa.ForeignKeyConstraint(['dokterPJ'], ['dokter.dokter_id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_tesLab_CheckInID'), 'tesLab', ['CheckInID'], unique=False)
    op.create_table('dokterLab',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('teslabID', sa.Integer(), nullable=True),
    sa.Column('dokterID', sa.String(), nullable=True),
    sa.ForeignKeyConstraint(['dokterID'], ['dokter.dokter_id'], ),
    sa.ForeignKeyConstraint(['teslabID'], ['tesLab.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('dokterLab')
    op.drop_index(op.f('ix_tesLab_CheckInID'), table_name='tesLab')
    op.drop_table('tesLab')
    op.drop_index(op.f('ix_perawat_nama'), table_name='perawat')
    op.drop_index(op.f('ix_perawat_lab_id'), table_name='perawat')
    op.drop_table('perawat')
    op.drop_index(op.f('ix_check_in_PasienID'), table_name='check_in')
    op.drop_table('check_in')
    op.drop_index(op.f('ix_alat_nama'), table_name='alat')
    op.drop_index(op.f('ix_alat_lab_id'), table_name='alat')
    op.drop_table('alat')
    op.drop_index(op.f('ix_pasien_noKTP'), table_name='pasien')
    op.drop_index(op.f('ix_pasien_nama'), table_name='pasien')
    op.drop_index(op.f('ix_pasien_alamat'), table_name='pasien')
    op.drop_table('pasien')
    op.drop_index(op.f('ix_lab_nama'), table_name='lab')
    op.drop_table('lab')
    op.drop_index(op.f('ix_dokter_notelp'), table_name='dokter')
    op.drop_index(op.f('ix_dokter_nama'), table_name='dokter')
    op.drop_index(op.f('ix_dokter_dokter_id'), table_name='dokter')
    op.drop_table('dokter')
    op.drop_table('app_user')
    # ### end Alembic commands ###


# Imports the Google Cloud client library
from google.cloud import storage
import io
import os

# Imports the Google Cloud client library
from google.cloud import vision

name = "input_folder/gato.jpg"
# Instantiates a client

print(f"Download blob from bucket {name}")
storage_client = storage.Client()
bucket = storage_client.bucket("ml2_bucket")
blob = bucket.blob(name)

# Instantiates a client
print(f"Apply vision API... {name}")
client = vision.ImageAnnotatorClient()
content = blob.download_as_bytes()
image = vision.Image(content=content)

# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations

result = ""
for label in labels:
    result += str(label.description)
    result += "\n"
print(f"write labels {result}...{name}")
print(f"upload result to bucket {name}")
blob_dst = bucket.blob("output_folder/" + name.split('/')[1].split(".")[0] + ".vision.txt")
blob_dst.upload_from_string(result)

blob.delete()
print("Blob {} deleted.".format(name))


"""
censorship module
runs test on input string to see if it contains profanity
"""
from profanity import profanity
from aldict import ascii_leet

def remove_whitespace(string, sub):
    whitespace = [':', ';', '.', ',',
                  '-', '=', '*', '\t',
                  '~', '\\', '|', '\"',
                  '\'', '_', '\n']
    for i in whitespace:
        string = string.replace(i, sub)
    return string

def leet_detect(word):
    for key in ascii_leet:
        if key in word:
            word = word.replace(key, ascii_leet[key])
    return word

def is_clean(inputlist):
    for i in range(0, len(inputlist)):
        word = inputlist[i]
        if word in profanity:
            return False
        test1 = remove_whitespace(word, "")
        if test1 in profanity:
            return False
        test2 = leet_detect(word)
        if test2 in profanity:
            return False
        test3 = leet_detect(test1)
        if test3 in profanity:
            return False
    return True

def censor(tweetvar):
    test1 = test2 = tweetvar.lower()
    test2 = remove_whitespace(test2, " ")
    test1 = test1.split()
    test2 = test2.split()
    if is_clean(test1):
        if is_clean(test2):
            return True
    return False


import socket   # UDP通信のため
from time import sleep
import curses   # これは端末エミュレーションのようなライブラリ

INTERVAL = 0.2

def report(str):
    stdscr.addstr(0, 0, str)
    stdscr.refresh()

if __name__ == "__main__":
    stdscr = curses.initscr()  # 初期化
    curses.noecho()            # エコーなし
    curses.cbreak()            # 入力はバッファされない

    # ステータス受信用のUDPサーバの設定
    local_ip = ''       # '0.0.0.0'と同じ意味．すなわち「全てのネットワークインターフェイスを使う」
    local_port = 8890   # ステータス受信は8890ポート
    socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # ソケットを作成
    socket.bind((local_ip, local_port))   # サーバー側はバインドが必要

    # コマンド送信用の設定
    tello_ip = '192.168.10.1'  # Telloのipアドレス
    tello_port = 8889          # コマンドは8889ポートへ送る
    tello_adderss = (tello_ip, tello_port)  # アドレスを作成

    # 最初に"command"を送ってSDKモードを開始する
    socket.sendto('command'.encode('utf-8'), tello_adderss)

    # Ctrl+Cが押されるまで繰り返す
    try:
        index = 0    # ループ回数
        while True:
            index += 1
            response, ip = socket.recvfrom(1024) # 受信は最大1024バイトまで．受信結果はresponse変数に入る
            if response == 'ok':  # 受信データがokだけだったら再ループ
                continue
            # 受信データを加工
            out = response.decode('utf-8')  # 戻り値はバイナリーなのでデコードしてstr型に変換
            out = out.replace(';', ';\n')
            out = 'Tello State:\n' + out
            report(out)
            sleep(INTERVAL)
    except KeyboardInterrupt:   # Ctrl+Cが押された時の終了処理
        curses.echo()
        curses.nocbreak()
        curses.endwin()


from django.conf import settings # import the settings file

import logging

logger = logging.getLogger(__name__)

def projects_constants(request):

    # AWS Does it this way:
    forwarded_ssl = ('HTTP_X_FORWARDED_SSL' in request.META)

    # Other proxy's do it this way:
    if 'HTTP_X_FORWARDED_PROTO' in request.META:
        forwarded_ssl = (request.META['HTTP_X_FORWARDED_PROTO'] == 'https')

    # is_secure for direct ssl request
    if request.is_secure() or forwarded_ssl:
        static_url = settings.SSL_STATIC_URL
    else:
        static_url = settings.STATIC_URL

    # return the value you want as a dictionnary. you may add multiple values in there.
    return {'GOOGLE_ANALYTICS': settings.GOOGLE_ANALYTICS,
            'GOOGLE_ANALYTICS_ACCOUNT': settings.GOOGLE_ANALYTICS_ACCOUNT ,
            'BASE_URL': settings.BASE_URL,
            'SUPPORT_URL': settings.SUPPORT_URL,
            'STATIC_URL':static_url }


from dataclasses import field
from django import forms
from .models import Order

class OrderForm(forms.ModelForm):
    class Meta:
        model = Order
        fields = [
            'first_name',
            'last_name',
            'phone',
            'email',
            'address1',
            'address2',
            'country',
            'state',
            'city',
            'pincode',
            'order_note',
        ]
    def __init__(self,*args, **kwargs):
        super(OrderForm,self).__init__(*args,**kwargs)
        for field in self.fields:
            self.fields[field].widget.attrs["class"]="form-control"


# Leonardo Balestra
from random import randint

min = 0
max = 500
x = -5
N = []
qtd = 0

print("\n************************************************************************")
print("**** Jogo 1 - Aprendendo Lógica em Python - FATEC-SP  - Prof. Banin ****")
print("************************************************************************")
print("************************************************************************")
print("***************** Descubra o número que estou pensando *****************")
print("************************************************************************")

while min > x or x > max:
    x = int(input("\nDigite um número entre 0 e 500: "))

y = randint(min,max)

if x == y:
    qtd = qtd + 1
    N.append(x)

else:
    while x != y:

        if min <= x and x <= max:
            if x > y:
                qtd = qtd + 1
                N.append(x)
                print("O número é menor!")
                x = int(input("Tente novamente (Seu número anterior foi %d): " %(x)))
            else:
                qtd = qtd + 1
                N.append(x)
                print("O número é maior!")
                x = int(input("Tente novamente (Seu número anterior foi %d): " %(x)))
        else:
            print("O número deve estar entre %d e %d!" %(min,max))
            x = int(input("Tente novamente (Seu número anterior foi %d): " %(x)))

    qtd = qtd + 1
    N.append(x)

print("\nParabéns!! O numero correto é %d!" %y)
print("\nHouve %d tentativas, foram elas:\n" %(qtd))
print(N)


import torch
import torch.nn as nn
import torch.nn.functional as F
from equtils import DataLoad
import numpy as np

#class containig the model architecture, and methods for loading data and training.
class EQ:

    def __init__(self):
        self.r = 25   #radius of gaussian distribution
        self.dimensions = [30,30,30]

    #makes a regular grid of size 'dimensions' and the region defined by 'region' which contains maximum and minium of latitude,longitude and depth.
    def makeGrid(self,region, dimensions):
        grid = np.empty(dimensions)
        depths, depthstep = np.linspace(region[0][0],region[0][1], dimensions[0],endpoint=False,retstep=True)
        self.depths = depths + depthstep/2
        latitudes, latstep = np.linspace(region[1][0],region[1][1], dimensions[1],endpoint=False,retstep=True)
        self.latitudes = latitudes + latstep/2
        longitudes, longstep = np.linspace(region[2][0],region[2][1], dimensions[2],endpoint=False,retstep=True)
        self.longitudes = longitudes + longstep/2

    #depending on hypcenter, returns an array of probability value on the grid.
    def labelProb(self,hypocenter,dimensions):
        '''region [[latitudemin,latitudemax],[longitudemin,longitudemax],[depthmin,depthmax]]
        dimensions: dimension of the discreteblock in which we calculate probability'''
        pArray = np.empty([dimensions[0],dimensions[1],dimensions[2]])
        for i in range(dimensions[0]): #depth
            for j in range(dimensions[1]): #latitude
                for k in range(dimensions[2]): #longitude
                    pArray[i][j][k] = self.pMag(hypocenter,(self.depths[i],self.latitudes[j],self.longitudes[k]))
        return pArray

    #returns probability magnitude on 'point' given 'hypocenter'
    def pMag(self,hypocenter, point):
        d = self.sqdistance(hypocenter, point)
        return np.exp(-d/self.r)

    # returns square of distance between point1 and point2
    def sqdistance(self,point1, point2):
        '''point[0]:depth, point[1]=phi or latitude  point[2]=theta or longitude'''
        R = 6378.137
        p = 2*np.pi/360
        #print("point1 point2")
        #print(point1, point2)
        x1,y1,z1 = (R-point1[0])*np.cos(point1[1]*p)*np.cos(point1[2]*p),(R-point1[0])*np.cos(point1[1]*p)*np.sin(point1[2]*p),(R-point1[0]*np.sin(point1[1]*p))
        x2,y2,z2 = (R-point2[0])*np.cos(point2[1]*p)*np.cos(point2[2]*p),(R-point2[0])*np.cos(point2[1]*p)*np.sin(point2[2]*p),(R-point2[0]*np.sin(point2[1]*p))
        #print(x1,x2,y1,y2,z1,z2)
        xd = x2-x1
        yd = y2-y1
        zd = z2-z1
        sqd = xd*xd + yd*yd + zd*zd
        #print(sqd)
        return sqd

    #preprocess the input to neural network
    def preprocess(self,x):
        return x.view(201, 401)

    # returns neural network.
    def getEqModel(self):

        model = nn.Sequential(
            Lambda(preprocess),
            nn.Conv2d(3, 64, kernel_size=3,padding=1),
            nn.ReLU(),
            nn.Conv2d(64, 64, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.MaxPool2d((1,4)),
            nn.Conv2d(64, 128, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Conv2d(128, 128, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.MaxPool2d((1,4)),
            nn.Conv2d(128, 256, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.MaxPool2d((2,4)),
            nn.Conv2d(256, 512, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.MaxPool2d((3,4)),
            nn.Conv2d(512, 1024, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Conv2d(1024, 1024, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Upsample((4,2)),
            nn.Conv2d(1024, 512, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Upsample((2,2)),
            nn.Conv2d(512, 256, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Upsample((2,2)),
            nn.Conv2d(256, 128, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Conv2d(128, 64, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Conv2d(64, 64, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Conv2d(64, 32, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Conv2d(32, 30, kernel_size=3, padding=1),
            Lambda(lambda x: x.view(-1,30,30))
        )
        return model

    def entropy_loss(self,input,target):
        return torch.nn.functional.binary_cross_entropy(input, target)

    def loss_batch(self,model, loss_func, xb, yb, opt=None):
        loss = loss_func(model(xb), yb)

        if opt is not None:
            loss.backward()
            opt.step()
            opt.zero_grad()

        return loss.item(), len(xb)

    #train routine
    def fit(self,epochs, model, loss_func, opt, train_dl, valid_dl):
        for epoch in range(epochs):
            model.train()
            for xb, yb in train_dl:
                loss_batch(model, loss_func, xb, yb, opt)

            model.eval()
            with torch.no_grad():
                losses, nums = zip(
                    *[loss_batch(model, loss_func, xb, yb) for xb, yb in valid_dl]
                )
            val_loss = np.sum(np.multiply(losses, nums)) / np.sum(nums)

            print(epoch, val_loss)

    #makes a grid
    def setitup(self):
        self.makeGrid(self.region,self.dimensions)
        labelpd = []
        for i in range(len(self.yb)):
            pd = self.labelProb(self.yb[i],self.dimensions)
            print(self.yb[i])
            labelpd.append(pd)
        self.ypd = labelpd

    #loads the data and determines the region.
    def getReadyData(self):
        dt = DataLoad()
        self.xb, self.yb = dt.loadData()
        self.region = dt.getDimensions()

    if __name__ == "main":

        epochs = 10
        lr = 0.0001

        model = getEqModel()
        opt = optim.SGD(model.parameters(),lr=lr)

        train_ds = TensorDataset(self.xb,self.ypd)
        valid_ds = TensorDataset(self.xb,self.ypd)

        train_dl = DataLoader(train_ds, batch_size=8)
        valid_dl = DataLoader(valid_ds, batch_size=16)

        loss_func = entropy_loss

        fit(epochs, model, loss_func, opt, train_dl, valid_dl)


from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.login import LoginManager
from ConfigParser import SafeConfigParser

app = Flask(__name__)
app.config.from_object('config')

# create flask-sqlalchemy database object
db = SQLAlchemy(app)

# use flask-login
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"

# stuff credentials in app config
parser = SafeConfigParser()
found = parser.read('foursquare.ini')
app.config['FS_CLIENT_ID'] = parser.get('foursquare', 'fs_client_id')
app.config['FS_CLIENT_SECRET']= parser.get('foursquare', 'fs_client_secret')
app.config['FS_REDIRECT_URI']= parser.get('foursquare', 'fs_redirect_uri')
app.config['FS_TOKEN_URL'] = parser.get('foursquare', 'fs_token_url')

from weather_app import views, models


''' This file contains tuples of all relevant features which are used to distinguish two repositories '''
from collections import namedtuple
# namedtuple are used as replacement for struct-like objects
# but the attributes are unmutable

# ~~~~~ define the features which are used for training and classification ~~~~~~
IntFeatures = namedtuple('IntFeatures',
                         # 'iNumContributors' -> the url for the contributors doesn't has the actual size
                         'iSubscriberCount '  # addition
                         #'dRepoActivity ' # old name 'dCodeFrequency '
                         #'dCommitIntervals '
                         #'iWatchersCount '  #'iNumBranches '
                         'iOpenIssues '
                         'iDevTime '
                         'iSize'  # addition
                         )

StringFeatures = namedtuple('StringFeatures',
                            'strTitle'
                            'strDescription'
                            'strReadmeContent'
                            'strLanguages'
                            'strFolderNames')


import re, sys

MIN_NUMBER_OF_CLAIMS = 2

def get_points_in_rectangle(line):
    points = []
    m = re.match('\#[0-9]+\s+\@\s+([0-9]+),([0-9]+)\:\s+([0-9]+)x([0-9]+)', line)
    x1 = int(m.group(1)) + 1
    y1 = int(m.group(2)) + 1
    x2 = x1 + int(m.group(3))
    y2 = y1 + int(m.group(4))
    for x in range(x1, x2):
        for y in range (y1, y2):
            points.append((x, y))
    return points

claimed_counts = {}

with open(sys.argv[1], "r") as file:
    for line in file:
        for point in get_points_in_rectangle(line):
            if point in claimed_counts.keys():
                claimed_counts[point] += 1
            else:
                claimed_counts[point] = 1

print(len(list(filter(lambda claim_count: claim_count >= MIN_NUMBER_OF_CLAIMS, claimed_counts.values()))))


from glm import value_ptr
from sys import exit
from OpenGL.GL import (
	GL_COMPUTE_SHADER, GL_VERTEX_SHADER, GL_FRAGMENT_SHADER,
	GL_COMPILE_STATUS, GL_LINK_STATUS,
	GLuint, GL_FALSE,
	glCreateProgram, glDeleteProgram, glLinkProgram, glUseProgram,
	glGetProgramiv, glGetProgramInfoLog,
	glCreateShader, glDeleteShader, glAttachShader, glDetachShader,
	glShaderSource, glCompileShader,
	glGetShaderiv, glGetShaderInfoLog,
	glGetUniformLocation, glUniform1i, glUniform1ui, glUniform1f, glUniform3f, glUniformMatrix4fv
)

def read_file(filepath):
	with open(filepath, 'r') as sourceFile:
		source = sourceFile.readlines()

	return source

class Shader():
	def __init__(self, filename):
		self.id = glCreateProgram()
		source = read_file(filename)
		shaderSources = {}

		try:
			foundComp = source.index('#type compute\n')
			shaderSources[GL_COMPUTE_SHADER] = source[1:]
		except ValueError as VE:
			try:
				foundVert = source.index('#type vertex\n')
				foundFrag = source.index('#type fragment\n')

				shaderSources[GL_VERTEX_SHADER] = source[foundVert + 1:foundFrag - 1]
				shaderSources[GL_FRAGMENT_SHADER] = source[foundFrag + 1:]
			except ValueError as VE:
				print(f"{filename} is not a valid shader file.")
				return

		self.__compile(shaderSources)

	def cleanup(self):
		glDeleteProgram(self.id)

	def __compile(self, shaderSources):
		shaders = []
		for shaderType, shaderSource in shaderSources.items():
			shader = glCreateShader(shaderType)
			glShaderSource(shader, shaderSource)
			glCompileShader(shader)

			retVal = GLuint()
			glGetShaderiv(shader, GL_COMPILE_STATUS, retVal)
			if not retVal:
				log = glGetShaderInfoLog(shader).decode('utf-8')
				glDeleteShader(shader)
				exit("Couldn't compile shader properly.\n" + log)

			glAttachShader(self.id, shader)
			shaders.append(shader)

		glLinkProgram(self.id)

		for shader in shaders:
			glDetachShader(self.id, shader)
			glDeleteShader(shader)

		retVal = GLuint()
		glGetProgramiv(self.id, GL_LINK_STATUS, retVal)
		if not retVal:
			log = glGetProgramInfoLog(self.id).decode('utf-8')
			glDeleteProgram(self.id)
			exit("Couldn't link shader program.\n" + log)

	def bind(self):
		glUseProgram(self.id)

	def set_int(self, name, value):
		location = glGetUniformLocation(self.id, name)
		glUniform1i(location, value)

	def set_uint(self, name, value):
		location = glGetUniformLocation(self.id, name)
		glUniform1ui(location, value)

	def set_float(self, name, value):
		location = glGetUniformLocation(self.id, name)
		glUniform1f(location, value)

	def set_vec3f(self, name, vec):
		location = glGetUniformLocation(self.id, name)
		glUniform3f(location, vec.x, vec.y, vec.z)

	def set_mat4(self, name, matrix):
		location = glGetUniformLocation(self.id, name)
		glUniformMatrix4fv(location, 1, GL_FALSE, value_ptr(matrix))


# Minha resolução:
'''pesos = []
for c in range(0,5):
    peso = float(input('Digite um peso:'))
    pesos.append(peso)

print(f'O maior peso é {max(pesos)}kg')

print(f'O menor peso é {min(pesos)}kg')'''

maior = 0
menor = 0
for c in range(0,5):
    peso = float(input('Digite um peso:'))
    if c == 0:
        maior = peso
        menor = peso
    if peso > maior:
        maior = peso
    if peso < menor:
        menor = peso

print(f'O maior peso é {maior}kg')

print(f'O menor peso é {menor}kg')


from interSensores import sensoresContr
import time
from Senso import Sensores
from JSON_Handle import JSON_Handle
import os
from interBD import interBD
from Mongo import Mongo
import threading
import keyboard
from ultimaLectura import ultimaLectura

class main:
    def __init__(self):
        self.disp = sensoresContr()  # pide datos pero no se
        self.sensores = Sensores()
        self.bandera = 0
        self.dispositivo = ""
        self.mongo = Mongo()
        self.obj = Mongo()
        self.bandera = ""
        self.flag = 0
        self.stop_event = threading.Event()
        self.ultLectura = ultimaLectura

        self.tiempoEspera = 60  # tiempo en segundos
        self.timer_count = 0  # contador de tiempo para borrar historial local

        self.veces = 2

    def contador(self, tiempo):
        for i in range(tiempo, -1, -1):
            time.sleep(1)
        return True

    def detectar_enter(self):
        self.enter_pressed = False
        keyboard.wait("enter")  # espera hasta que se presione Enter
        self.enter_pressed = True

    def lectura(self):
        print("Lectura de sensores")
        if self.bandera2 == 1:  # si esta en conexion
            print("Conexion")
            lista = self.sensores.mostrar()
            if len(lista) >= 1:  # si la lista de sensores tiene objetos, debe ingresarlos a la bd antes de los otros
                passo = 0
                for i in lista:
                    if self.obj.find_one("Sensores", i):
                        pass
                    else:
                        self.obj.insert_one("Sensores", i)
                        passo = 1
                if passo == 1:
                    self.sensores.borrarInfo("Sensores.json")
            print("Empezando..")
            enter_thread = threading.Thread(target=self.detectar_enter)
            enter_thread.start()
            while True:  # tiempo en segundos
                if enter_thread.is_alive():
                    pass
                else:
                    aux = self.sensores.mostrar()
                    if len(aux) >= 1:
                        ubi = len(aux) - 1
                        if self.obj.find_one("Sensores", aux[ubi]):
                            pass
                        else:
                            self.obj.insert_one("Sensores", aux[ubi])
                    print("Enter presionado, deteniendo lectura de sensores")
                    self.main()
                sens, val = self.disp.lectura()
                nombre, id = self.disp.nom.filter("clave", sens)
                sensor = Sensores(nombre[0]['nombre'], val)
                self.sensores.agregar(sensor.to_dict())
                print("|{:<25} {:<4}|".format(nombre[0]['nombre'], val))

                if self.obj.insert_one("Sensores",
                                       sensor.to_dict()) is False:  # si no se inserto, debe cambiar la bandera
                    self.bandera2 = 2
                    print("Se perdio la conexion, guardando solo localmente")
                    ultimoSensor = sensor.to_dict()  # guarda la lecutra donde sucede la desconexion
                    self.sensores.borrarInfo("Sensores.json")  # borra datos para no repetirlos
                    self.sensores.agregar(ultimoSensor)
                    self.lectura()  # debe regresar al metodo para empezar a guardar solo local

        else:  # guarda solo local
            enter_thread = threading.Thread(target=self.detectar_enter)
            enter_thread.start()
            while True:
                print("Escritura..")
                user_input = input()
                if user_input == " ":
                    break
                sens, val = self.disp.lectura()
                nombre, id = self.disp.nom.filter("clave", sens)
                sensor = Sensores(nombre[0]['nombre'], val)
                self.sensores.agregar(sensor.to_dict())
                self.ultLectura.agregar(sensor.to_dict())
                print("|{:<25} {:<4}|".format(nombre[0]['nombre'], val))

                if self.enter_pressed:  # si se ha detectado la pulsación de Enter, romper el ciclo
                    print("Enter presionado, deteniendo lectura de sensores")
                    return self.main()
                # print(nombre[0]['nombre'])

    def ultimaLectura(self):
        sensores = self.sensores.from_json()
        print("|{:<25} {:<5}|".format("Sensor", "Valor"))
        for sens in sensores:
            print("|{:<25} {:<5}|".format(sens.nombre, sens.valor))

    def clear(self):
        os.system("cls" if os.name == "nt" else "clear")

    def menuGeneral(self):
        self.clear()
        print("----------------------------------------------")
        print("Sistema de gestión de dispositivos arduinos")
        if self.veces == 2:
            resultado = interBD().checkarConexionEnUso()
            if resultado:
                self.bandera2 = 1
                self.obj = resultado[0]
                self.obj.conect()
                self.bandera = resultado[1]
                print(f"Datos de conexion: {self.obj.user}-{self.obj.cluster}-{self.obj.bd}------")
                print(f"Estado: {self.bandera}")
                self.veces = 1

            else:
                self.bandera2 = 2
                self.veces = 1
                print("No hay conexion activa")
            self.hiloBorrarPTiempo()
        print(f"----Puerto: {self.disp.puerto}-----")
        print("1. Configurar sensores")
        print("2. Conexiones")
        print("3. Lectura de sensores")
        print("4. Lectura guardada")
        print("5. Salir")
        print("------------------------------------")
        opcion = input("Seleccione una opción: ")
        return opcion

    def hiloBorrarPTiempo(self):
        timer = threading.Timer(self.tiempoEspera, self.hiloBorrarPTiempo)
        timer.start()

        if self.bandera2 == 1:  # si esta en conexion
            self.timer_count += 1  # incrementa el contador de tiempo
            if self.timer_count >= self.tiempoEspera / 60:  # verifica si han pasado 15 minutos
                self.sensores.borrarInfo("Sensores.json")
                print("Se borro historial local")
                self.timer_count = 0  # resetea el contador de tiempo
        else:
            print("Se reinicio el contador")
            self.timer_count = 0

    def main(self):

        opcion = ""

        while opcion != "0":
            opcion = self.menuGeneral()
            if opcion == "1":
                self.disp.mainSensores()
            elif opcion == "2":
                interBD().mainBd()
            elif opcion == "3":
                self.lectura()
            elif opcion == "4":
                self.ultimaLectura()
                input("Presione Enter para continuar...")
            elif opcion == "p":
                contra = input("Contra:")
                if contra == "1234":
                    JSON_Handle().clear_all_files()
                else:
                    return main()
            elif opcion == "0":
                # Salir
                print("Saliendo del sistema...")
                break
            else:
                print("Opción inválida, intente de nuevo.")
                input("Presione Enter para continuar...")

if __name__ == "__main__":
    main().main()


import pickle
import numpy as np
import pandas as pd
import string
import re
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

# Function to process the text
def process_text(text):
    text = text.translate(str.maketrans('', '', string.punctuation))  # Remove punctuation
    text = text.lower()                                               # Convert to lower case
    return text

# Load the trained model and the vectorizer
clf = pickle.load(open('finalized_model.pkl', 'rb'))
vectorizer = pickle.load(open('vectorizer.pkl', 'rb'))

def classify_email(text):
    # Preprocess the text
    text = process_text(text)
    # Transform the text to match the input of the model
    text_vectorized = vectorizer.transform([text])
    # Predict the class of the text
    prediction = clf.predict(text_vectorized)
    # Return the prediction
    if prediction[0] == 1:
        return "This email is SPAM"
    else:
        return "This email is NOT SPAM"

# Load CSV file
data = pd.read_csv('your_file.csv')

# Apply classification to each row of the dataset
data['classification'] = data['text'].apply(classify_email)

# Print classifications
print(data['classification'])


from typing import Any, Mapping
from unittest import mock

import pytest
from flask import Flask
from snapshottest.pytest import PyTestSnapshotTest

from ....funcs import assert_mutation
from ...gql import MUTATION_DELETE_PRODUCT

QUERY = '''
{
  allProducts {
    edges {
      node {
        name
      }
    }
  }
  allProductRelations {
    edges {
      node {
        type_ {
          name
        }
        self_ {
          name
        }
        other {
          name
        }
      }
    }
  }
  allProductFilePaths {
    edges {
      node {
        path
      }
    }
  }
}
'''

HEADERS = {'Authorization': 'Bearer 39d86487d76a84087f1da599c872dac4473e5f07'}  # user1

params = [
    pytest.param(
        {'query': MUTATION_DELETE_PRODUCT, 'variables': {'productId': 1}},
        {'query': QUERY},
        id='delete',
    ),
]

@pytest.mark.parametrize('data_mutation, data_query', params)
@pytest.mark.asyncio
async def test_schema_success(
    app: Flask,
    snapshot: PyTestSnapshotTest,
    data_mutation: Mapping[str, Any],
    data_query: Mapping[str, Any],
    mock_request_backup_db: mock.Mock,
) -> None:
    success = True
    await assert_mutation(
        app,
        snapshot,
        data_mutation,
        HEADERS,
        data_query,
        HEADERS,
        mock_request_backup_db,
        success,
    )

params = [
    pytest.param(
        {'query': MUTATION_DELETE_PRODUCT, 'variables': {'productId': 512}},
        {'query': QUERY},
        id='error',
    ),
]

@pytest.mark.parametrize('data_mutation, data_query', params)
@pytest.mark.asyncio
async def test_schema_error(
    app: Flask,
    snapshot: PyTestSnapshotTest,
    data_mutation: Mapping[str, Any],
    data_query: Mapping[str, Any],
    mock_request_backup_db: mock.Mock,
) -> None:
    success = False
    await assert_mutation(
        app,
        snapshot,
        data_mutation,
        HEADERS,
        data_query,
        HEADERS,
        mock_request_backup_db,
        success,
    )


import re
import sys
import copy
import uuid
import shlex
import getopt
import portion
import ipaddress
from pathlib import Path
from collections import defaultdict
from functools import reduce

f = lambda: defaultdict(f)

def getFromDict(dataDict, mapList):
    return reduce(lambda d, k: d[k], mapList, dataDict)

def setInDict(dataDict, mapList, value):
    getFromDict(dataDict, mapList[:-1])[mapList[-1]] = value

class Parser(object):
    previous_set_headers = {}
    previous_set_values = {}

    def __init__(self):
        self.config_header = []
        self.section_dict = defaultdict(f)

	# parse "config"
    def parse_config(self, fields):
        self.config_header.append(' '.join(fields))

	# parse "edit"
    def parse_edit(self, line):
        self.config_header.append(' '.join(line))

	# parse "set" (key/value)
    def parse_set(self, line):
        values = []
        key = ' '.join([line[0], line[1]])
        values.append(' '.join(line[2:]))
        self.previous_set_values = values
        headers= self.config_header + [key]
        self.previous_set_headers = headers
        setInDict(self.section_dict, headers, values)

	# parse "set" (key/multiline value)
    def parse_set_multiline(self, line):
        self.previous_set_values.append(' '.join(line[0:]))
        setInDict(self.section_dict, self.previous_set_headers, self.previous_set_values)

	# parse "set" (key/value)
    def parse_comment(self, line):
        values = []
        key = ' '.join(['comment', str(uuid.uuid4())])
        values = line
        headers= self.config_header + [key]
        self.previous_set_headers = headers
        setInDict(self.section_dict, headers, values)

	# parse "unset" (key/no value)
    def parse_unset(self, line):
        key = ' '.join([line[0], line[1]])
        values = ' '.join(line[2:])
        headers= self.config_header + [key]
        self.previous_set_headers = headers
        setInDict(self.section_dict, headers, values)

	# parse "next" (for "config" or "edit")
    def parse_next(self, line):
        if not (set(self.config_header)).issubset(set(self.previous_set_headers)):
            if self.previous_set_headers == {} and self.config_header[0] == 'config vdom':
                getFromDict(self.section_dict, ['config global'])
            getFromDict(self.section_dict, self.config_header)
        if len(self.config_header) < 2:
            print("it seems like next here is incorrect")
        self.config_header.pop()

    # parse "end" (for "config" or "edit")
	# in multi vdom configuration, config vdom -> edit vdom_name -> end, without a next, which we need to handle it...
    def parse_end(self, line):
        if not (set(self.config_header)).issubset(set(self.previous_set_headers)):
            getFromDict(self.section_dict, self.config_header)
        if (set(self.config_header)).issubset(set(self.previous_set_headers)) and len(self.config_header) == 2 and self.config_header[0] == 'config vdom':
            self.config_header.pop()    # we need this 2nd pop to close the config vdom wihout next properly
        self.config_header.pop()

	# prase FortiGate configuration
    def parse_text(self, text):
        gen_lines = (line.rstrip() for line in text if line.strip())
        previous_method = None
        for line in gen_lines:
            # if line == 'config user radius':
            #     print('here')
            fields = line.strip().split(' ')

            valid_fields= ['config', 'edit', 'set', 'unset', 'next', 'end']
            if fields[0] in valid_fields:
                method = fields[0]
                previous_method = method
                # call parse function according to the verb
                getattr(Parser, 'parse_' + method)(self, fields)
            elif re.match(r'^\s*#.*', line):    # parse comment, you need to parse this before parse multiline value next
                # but this might cause issue if multiline value happen to startswith a '#' sign
                # multiline value could be detected by checking if previous line value starts with single or double quotes
                # will need more time to work on this ....
                method = 'comment'
                previous_method = method
                # call parse function according to the verb
                getattr(Parser, 'parse_' + method)(self, [line])
            elif previous_method == 'set':      # parse multiline value in set
                getattr(Parser, 'parse_' + 'set_multiline')(self, fields)

        return self.section_dict

empty_str = ''
def niceprint(d, offset = 0, indent = 4):
    if d.get('config global') and d.get('config vdom'):
        for k, v in d.items():
            if v == {}:
                print (k)
        print ('')
        print ('config vdom')
        for k, _ in d['config vdom'].items():
            print (k)
            print ('next')
        print ('end')

        print ('')
        print ('config global')
        niceprint(d['config global'], indent=indent)
        print ('end')

        for k, v in d['config vdom'].items():
            print ('')
            print ('config vdom')
            print (k)
            niceprint(v, indent=indent)
            print ('end')
    else:
        for k, v in d.items():
            if isinstance(v, dict): # sub-section
                fields = k.strip().split(' ')
                method = fields[0]
                print ('{0}{1}'.format(empty_str.rjust(offset), ' '.join(fields)))  # print sub-section header
                niceprint(v, offset + indent, indent=indent)   # print sub-section
                if method == 'config':
                    print (empty_str.rjust(offset) + 'end')     # print sub-section footer
                elif method == 'edit':
                    print (empty_str.rjust(offset) + 'next')    # print sub-section footer
            else:                   # leaf
                if k == '':
                    return
                fields = k.strip().split(' ')
                if len(v):
                    if fields[0] == 'comment':  # for comment
                        print ('{}'.format(v[0]))
                    else:
                        print ('{0}{1} {2}'.format(empty_str.rjust(offset), ' '.join(fields), v[0]))    # print value
                        for xx in range(1, len(v)):     # print multiline value if exists
                            print (v[xx] + '\r')
                else:
                    print ('{0}{1}'.format(empty_str.rjust(offset), ' '.join(fields)))  # print unset without value

def port2portion(rs):   # ignore_source
    try:
        pl = [portion.closedopen(int(r.split(':')[0].split('-')[0]), int(r.split(':')[0].split('-')[1]) + 1) if '-' in r.split(':')[0] else portion.closedopen(int(r.split(':')[0]), int(r.split(':')[0]) + 1) if ':' in r else
portion.closedopen(int(r.split('-')[0]), int(r.split('-')[1]) + 1) if '-' in r else portion.closedopen(int(r), int(r) + 1) for r in rs.split(' ')]
    except ValueError:
        return portion.empty()
    else:
        pr = portion.empty()
        for p in pl:
            pr = pr.union(p)
        return pr

def interface_intersection(intf1, intf2):
    if '"any"' in intf1:
        return intf2
    elif '"any"' in intf2:
        return intf1
    else:
        return list(set(intf1) & set(intf2))

def address_intersection(addr1, addr2, addr1_f, addr2_f):
    if '"all"' in addr1:
        return addr2
    elif '"all"' in addr2:
        return addr1
    else:
        return addr1_f.intersection(addr2_f)

def service_intersection(serv1, serv2, serv1_f, serv2_f):
    if '"ALL"' in serv1:
        return serv2
    elif '"ALL"' in serv2:
        return serv1
    else:
        return {k: serv1_f[k] & serv2_f[k] for k in [k for k in serv1_f if serv1_f [k]!=serv1_f.default_factory()]}

def policy_intersection(pol1_id, pol2_id, pol1, pol2, pol1_f, pol2_f):
    srcintf = interface_intersection(pol1['set srcintf'], pol2['set srcintf'])
    if not srcintf:
        print('{} ^ {} => srcintf intersection is empty'.format(pol1_id, pol2_id))
        return

    dstintf = interface_intersection(pol1['set dstintf'], pol2['set dstintf'])
    if not dstintf:
        print('{} ^ {} => sdstintf intersection is empty'.format(pol1_id, pol2_id))
        return

    srcaddr = address_intersection(pol1['set srcaddr'], pol2['set srcaddr'], pol1_f['set srcaddr'], pol2_f['set srcaddr'])
    if not srcaddr:
        print('{} ^ {} => srcaddr intersection is empty'.format(pol1_id, pol2_id))
        return

    dstaddr = address_intersection(pol1['set dstaddr'], pol2['set dstaddr'], pol1_f['set dstaddr'], pol2_f['set dstaddr'])
    if not dstaddr:
        print('{} ^ {} => dstaddr intersection is empty'.format(pol1_id, pol2_id))
        return

    service = service_intersection(pol1['set service'], pol2['set service'], pol1_f['set service'], pol2_f['set service'])
    if (not service) or (type(service) == dict and ((not service['tcp range list']) and (not service['udp range list']) and (not service['icmp type']))):
        print('{} ^ {} => service intersection is empty'.format(pol1_id, pol2_id))
        return

    print('{} ^ {} => srcintf:{}, dstintf:{}, srcaddr:{}, dstaddr:{}, service:{}'.format(pol1_id, pol2_id, srcintf, dstintf, srcaddr, dstaddr, service))
    return True

def parse_file(path):
	with open(path, encoding='utf-8-sig') as f:
		conf = Parser()
		conf.parse_text(f)
		return conf.section_dict

opts, args = getopt.getopt(sys.argv[1:],'hvi:', ['help', 'vdom='])
version = '20220920'
fgt_config_file = ''
fgt_vdom = ''
verbose = False

for opt, arg in opts:
    if opt in ('-h', '--help'):
        print('***********************************************************************************')
        print('Usage: python FortiGateConfigDenyRuleCheck.py -i FortiGate_Config_File')
        print('')
        print('***********************************************************************************')
        exit(0)
    elif opt == '-v':
        verbose = True
    elif opt == '-i':
        fgt_config_file = arg
    elif opt == '--vdom':
        fgt_vdom = arg

fgt_path = Path(fgt_config_file)
if not fgt_path.is_file:
    print('Please check and specify correct FGT configuration file')
    exit(2)

# load FortiGate Config File, assume there is not vdom
config_all = parse_file(fgt_path)

# config "root"
if fgt_vdom and 'vdom' in config_all and fgt_vdom in config_all['vdom']:
    config_root = config_all['vdom'][fgt_vdom]
elif not fgt_vdom:
    config_root = config_all
else:
    print('No vdom {} found in FortiGate Configuration File, please check ...'.format(fgt_vdom))
    exit(2)

# flatten firewall address
if 'config firewall address' in config_root:
    config_address = config_root['config firewall address']
    config_address_flatten = defaultdict(f)
    # flatten config firewall address (only for subnet, iprannge and type:value)
    for name, value in config_address.items():
        if 'set subnet' in value:
            ipaddr0 = ipaddress.IPv4Network(value['set subnet'][0].replace(' ','/'))
            config_address_flatten[name] = portion.closedopen(int(ipaddr0[0]), int(ipaddr0[-1]) + 1)
        elif 'set start-ip' in value:
            ipaddr1 = ipaddress.ip_network(value['set start-ip'][0])
            ipaddr2 = ipaddress.ip_network(value['set end-ip'][0])
            config_address_flatten[name] = portion.closedopen(int(ipaddr1[0]), int(ipaddr2[0]) + 1)
        else:
            config_address_flatten[name] = ':'.join(value['set type'][0], value['set fqdn'][0])

else:
    print('No "config firewall address" in FortiGate Configuration File, please check ...')
    exit(2)

# flatten firewall addrgrp
if 'config firewall addrgrp' in config_root:
    config_addrgrp = config_root['config firewall addrgrp']
    config_addrgrp_flatten = defaultdict(f)
    # flatten config firewall addrgrp
    for name, value in config_addrgrp.items():
        member_list = shlex.split(value['set member'][0])
        # flatten nested addrgrp member if exists
        addrgrp_member = True
        while addrgrp_member:
            addrgrp_member = False
            for member in member_list:
                if 'edit "{}"'.format(member) in config_addrgrp:
                    addrgrp_member = True
                    member_list.extend(shlex.split(config_addrgrp['edit "{}"'.format(member)]['set member'][0]))
                    break
            if addrgrp_member:
                member_list.remove(member)

        # convert member list to list of ranges
        pl = [config_address_flatten['edit "{}"'.format(member)] for member in member_list]
        pr = portion.empty()
        for p in pl:
            pr = pr.union(p)
        config_addrgrp_flatten[name] = pr

# flatten firewall service custom
if 'config firewall service custom' in config_root:
    config_service = config_root['config firewall service custom']
    config_service_flatten = defaultdict(f)
    # flatten config firewall service custom (only for tcp/udp/icmp)
    for name, value in config_service.items():
        if 'set tcp-portrange' in value or 'set udp-portrange' in value:
            tcp_range_list = port2portion(value.get('set tcp-portrange', [''])[0])
            udp_range_list = port2portion(value.get('set udp-portrange', [''])[0])
            config_service_flatten[name]['tcp range list'] = tcp_range_list
            config_service_flatten[name]['udp range list'] = udp_range_list
        elif 'set protocol' in value and 'ICMP' in value['set protocol']:
            icmp_type = value.get('set icmptype', ['0'])[0]
            # icmp_code = value.get('set icmpcode', ['0'])[0]
            config_service_flatten[name]['icmp type'] = portion.singleton(int(icmp_type))
        else:
            config_service_flatten[name]['tcp range list'] = portion.closedopen(1, 65536)
            config_service_flatten[name]['udp range list'] = portion.closedopen(1, 65536)
            config_service_flatten[name]['icmp type'] = portion.singleton(0)

else:
    print('No "config firewall service custom" in FortiGate Configuration File, please check ...')
    exit(2)

# flatten firewall service group
if 'config firewall service group' in config_root:
    config_servgrp = config_root['config firewall service group']
    config_servgrp_flatten = defaultdict(f)
    # flatten config firewall service group
    for name, value in config_servgrp.items():
        member_list = shlex.split(value['set member'][0])
        # flatten nested servgrp member if exists
        servgrp_member = True
        while servgrp_member:
            servgrp_member = False
            for member in member_list:
                if 'edit "{}"'.format(member) in config_servgrp.items():
                    servgrp_member = True
                    member_list.remove(member)
                    member_list.extend(shlex.split(config_servgrp['edit "{}"'.format(member)]['set member'][0]))
                    break

        # convert member list to list of ranges
        tpl = [config_service_flatten['edit "{}"'.format(member)].get('tcp range list', portion.empty()) for member in member_list]
        upl = [config_service_flatten['edit "{}"'.format(member)].get('udp range list', portion.empty()) for member in member_list]
        ipl = [config_service_flatten['edit "{}"'.format(member)].get('icmp type', portion.empty()) for member in member_list]
        pr = portion.empty()
        for p in tpl:
            pr = pr.union(p)
        config_servgrp_flatten[name]['tcp range list'] = pr
        pr = portion.empty()
        for p in upl:
            pr = pr.union(p)
        config_servgrp_flatten[name]['udp range list'] = pr
        pr = portion.empty()
        for p in ipl:
            pr = pr.union(p)
        config_servgrp_flatten[name]['icmp type'] = pr

# flatten firewall policy
if 'config firewall policy' in config_root:
    config_policy = config_root['config firewall policy']
    config_policy_flatten = defaultdict(f)
    # flatten config firewall policy
    for name, value in config_policy.items():
        if not name.startswith('comment'): # skip comment
            pol = defaultdict(f)
            pol = copy.copy(value)

            # flatten "set srcaddr"
            srcaddr_list = shlex.split(value['set srcaddr'][0])
            srcaddr_flatten = portion.empty()
            for addr in srcaddr_list:
                srcaddr_flatten = srcaddr_flatten.union(config_address_flatten.get('edit "{}"'.format(addr), portion.empty()))
                srcaddr_flatten = srcaddr_flatten.union(config_addrgrp_flatten.get('edit "{}"'.format(addr), portion.empty()))
            pol['set srcaddr'] = srcaddr_flatten

            # flatten "set dstaddr"
            dstaddr_list = shlex.split(value['set dstaddr'][0])
            dstaddr_flatten = portion.empty()
            for addr in dstaddr_list:
                dstaddr_flatten = dstaddr_flatten.union(config_address_flatten.get('edit "{}"'.format(addr), portion.empty()))
                dstaddr_flatten = dstaddr_flatten.union(config_addrgrp_flatten.get('edit "{}"'.format(addr), portion.empty()))
            pol['set dstaddr'] = dstaddr_flatten

            # flatten "set service"
            service_list = shlex.split(value['set service'][0])
            tcp_flatten = portion.empty()
            udp_flatten = portion.empty()
            icmp_flatten = portion.empty()
            for service in service_list:
                tcp_flatten = tcp_flatten.union(config_service_flatten.get('edit "{}"'.format(service), {}).get('tcp range list', portion.empty()))
                tcp_flatten = tcp_flatten.union(config_servgrp_flatten.get('edit "{}"'.format(service), {}).get('tcp range list', portion.empty()))
                udp_flatten = udp_flatten.union(config_service_flatten.get('edit "{}"'.format(service), {}).get('udp range list', portion.empty()))
                udp_flatten = udp_flatten.union(config_servgrp_flatten.get('edit "{}"'.format(service), {}).get('udp range list', portion.empty()))
                icmp_flatten = icmp_flatten.union(config_service_flatten.get('edit "{}"'.format(service), {}).get('icmp type', portion.empty()))
                icmp_flatten = icmp_flatten.union(config_servgrp_flatten.get('edit "{}"'.format(service), {}).get('icmp type', portion.empty()))
            service_flatten = defaultdict(f)
            service_flatten['tcp range list'] = tcp_flatten
            service_flatten['udp range list'] = udp_flatten
            service_flatten['icmp type'] = icmp_flatten
            pol['set service'] = service_flatten

            config_policy_flatten[name] = pol

else:
    print('No "config firewall policy" in FortiGate Configuration File, please check ...')
    exit(2)

for pol1_id, pol1 in config_policy.items():
    if not pol1_id.startswith('comment'): # skip comment
        pol1_before_pol2 = False
        for pol2_id, pol2 in config_policy.items():
            if not pol2_id.startswith('comment'): # skip comment
                if pol2_id == pol1_id:  # when pol2 reach to pol1
                    pol1_before_pol2 = True
                    continue
                else:
                    if pol1_before_pol2:
                        policy_intersection(pol1_id, pol2_id, pol1, pol2, config_policy_flatten[pol1_id], config_policy_flatten[pol2_id])
                    else:
                        policy_intersection(pol2_id, pol1_id, pol2, pol1, config_policy_flatten[pol2_id], config_policy_flatten[pol1_id])

# niceprint(config_policy_flatten)


# Python does not have method overloading by default

a = 5
b = 6
print(a+b)
print(int.__add__(a, b))  # + calls __add__ under the hood

class Student:

    def __init__(self, m1, m2) -> None:
        self.m1 = m1
        self.m2 = m2

    def __add__(self, other):  # Operator overloading
        m1 = self.m1 + other.m1
        m2 = self.m2 + other.m2
        m3 = Student(m1, m2)

        return m3

    def sum(self, a=None, b=None, c=None):  # Method overloading manually
        if a != None and b != None and c != None:
            s = a+b+c
        elif a != None and b != None:
            s = a+b
        else:
            s = a
        return s

s1 = Student(9, 7)
s2 = Student(10, 5)

s3 = s1+s2
print(s3.m1)

print(s1.sum(9, 7))
print(s1.sum(9, 7, 10))

# Method Overriding

class A:

    def show(self):
        print("in A show")

class B(A):

    def show(self):
        print("in B show")

a1 = A()
a1.show()

a2 = B()
a2.show()


import matplotlib.pyplot as plt

meses = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto"]
dolar = [20.46, 20.64, 20.43, 19.91, 20.37, 19.69, 20.13, 20.35]
euro = [23.33, 23.18, 22.98, 21.99, 21.56, 21.10, 21.08, 20.83]

plt.subplot(121)
plt.title("Tipo de cambio Dólar 2022")
plt.xlabel("Meses")
plt.ylabel("Dólar")
plt.plot(meses, dolar, "r*:")
plt.xticks(rotation = 45)

plt.subplot(122)
plt.title("Tipo de cambio Euro 2022")
plt.xlabel("Meses")
plt.ylabel("Euro")
plt.plot(meses, euro, "b<-")
plt.xticks(rotation = 45)
plt.show()

#plt.plot(meses, dolar, "bH", meses, euro, "r>-")
#plt.legend(["Dólar", "Euro"])
#plt.show()


# Basic searching algorithms
# Class for each node in the grid
#Name : Gokul Srinivasan

class Node:
	def __init__(self, row, col, is_obs, h):
		self.row = row        # coordinate
		self.col = col        # coordinate
		self.is_obs = is_obs  # obstacle?
		self.g = None         # cost to come (previous g + moving cost)
		self.h = h            # heuristic
		self.cost = None      # total cost (depend on the algorithm)
		self.parent = None    # previous node

#########################################
def getNeighbours(node, grid, setParent = True):
	row, col = node.row, node.col
	neighbours = [(row, col+1), (row+1, col), (row, col-1), (row-1, col)]
	Neighbours = []
	for i in neighbours:
		if not (i[0]<0 or i[1]<0 or i[0]>=len(grid) or i[1]>=len(grid[0]) or grid[i[0]][i[1]]==1):
			Neighbours.append(Node(i[0],i[1],0,0))

	for k in Neighbours:
		if setParent:
			k.parent = node
	return Neighbours
########################################
def checkifnodeExists(node, node_list):
	for i in node_list:
		if (i.row, i.col) == (node.row, node.col):
			return True
	return False
###########################################
def travel(path,curr):
	x = curr
	while(x.parent):
		path = travel(path, x.parent)
		path.append([x.parent.row,x.parent.col])
		break
	return path
###########################################
def bfs(grid, start, goal):
	'''Return a path found by BFS alogirhm
	   and the number of steps it takes to find it.

	arguments:
	grid - A nested list with datatype int. 0 represents free space while 1 is obstacle.
		   e.g. a 3x3 2D map: [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
	start - The start node in the map. e.g. [0, 0]
	goal -  The goal node in the map. e.g. [2, 2]

	return:
	path -  A nested list that represents coordinates of each step (including start and goal node),
			with data type int. e.g. [[0, 0], [0, 1], [0, 2], [1, 2], [2, 2]]
	steps - Number of steps it takes to find the final solution,
			i.e. the number of nodes visited before finding a path (including start and goal node)

	# >>> from main import load_map
	# >>> grid, start, goal = load_map('test_map.csv')
	# >>> bfs_path, bfs_steps = bfs(grid, start, goal)
	It takes 10 steps to find a path using BFS
	# >>> bfs_path
	[[0, 0], [1, 0], [2, 0], [3, 0], [3, 1]]
	'''
	### YOUR CODE HERE ###
	path = []
	steps = 0
	found = False
	#List of all visited nodes and priority queue
	visited, queue = [],[]
	#Define start node
	Start = Node(start[0], start[1], grid[start[0]][start[1]],0)
	visited.append(Start)
	steps += 1
	#Add the start node to the queue
	queue.append(Start)
	#Start of the search loop
	while queue:
		if found:
			break
		currentNode = queue.pop(0)
		#Get all the valid neighbour node
		for neighbour in getNeighbours(currentNode, grid):
			if not checkifnodeExists(neighbour, visited):
				steps += 1
				visited.append(neighbour)
				#Check if the node is goal or not.
				if ([neighbour.row, neighbour.col] == goal):
					found = True
					#BackTracking
					path = travel(path,neighbour)
					path.append(goal)
					break
				queue.append(neighbour)

	if found:
		print(f"It takes {steps} steps to find a path using BFS")
	else:
		print("No path found")
	return path, steps
#########################################
def dfs(grid, start, goal):
	'''Return a path found by DFS alogirhm
	   and the number of steps it takes to find it.

	arguments:
	grid - A nested list with datatype int. 0 represents free space while 1 is obstacle.
		   e.g. a 3x3 2D map: [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
	start - The start node in the map. e.g. [0, 0]
	goal -  The goal node in the map. e.g. [2, 2]

	return:
	path -  A nested list that represents coordinates of each step (including start and goal node),
			with data type int. e.g. [[0, 0], [0, 1], [0, 2], [1, 2], [2, 2]]
	steps - Number of steps it takes to find the final solution,
			i.e. the number of nodes visited before finding a path (including start and goal node)

	>>> from main import load_map
	>>> grid, start, goal = load_map('test_map.csv')
	>>> dfs_path, dfs_steps = dfs(grid, start, goal)
	It takes 9 steps to find a path using DFS
	>>> dfs_path
	[[0, 0], [0, 1], [0, 2], [1, 2], [2, 2], [2, 3], [3, 3], [3, 2], [3, 1]]
	'''
	### YOUR CODE HERE ###
	def DFS_recursive(visitedNodes, grid, node, step, goal, found):
		path = []
		if not checkifnodeExists(node, visitedNodes):
			visitedNodes.append(node)
			step += 1
			for neighbour in getNeighbours(node, grid):
				if ([neighbour.row, neighbour.col] == goal):
					found = True
					#The goal must also  be counted as step.
					# If the neighbour is not the goal, it will get updated when called again.
					step += 1

					path = travel(path,neighbour)
					path.append(goal)
					return [found, path, step]
				else:
					[found, path, step] = DFS_recursive(visitedNodes, grid, neighbour, step, goal, found)
				if found:
					return [found, path, step]
		return [found, path, step]

	path = []
	steps = 0
	found = False

	visited = []
	Start = Node(start[0], start[1], grid[start[0]][start[1]],0)
	[found, path, steps] = DFS_recursive(visited, grid, Start, steps, goal, found)

	if found:
		print(f"It takes {steps} steps to find a path using DFS")
	else:
		print("No path found")
	return path, steps
###########################################
def Distance(start, goal):
	# Calculates and returns the Manhattan Distance
	# |𝑛𝑜𝑑𝑒 𝑥 − 𝑔𝑜𝑎𝑙 𝑥 | + |𝑛𝑜𝑑𝑒 𝑦 − 𝑔𝑜𝑎𝑙 𝑦 |
	return (abs(goal[0] - start[0]) + abs(goal[1] - start[1]))
###########################################
def dijkstra(grid, start, goal):
	'''Return a path found by Dijkstra alogirhm
	   and the number of steps it takes to find it.

	arguments:
	grid - A nested list with datatype int. 0 represents free space while 1 is obstacle.
		   e.g. a 3x3 2D map: [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
	start - The start node in the map. e.g. [0, 0]
	goal -  The goal node in the map. e.g. [2, 2]

	return:
	path -  A nested list that represents coordinates of each step (including start and goal node),
			with data type int. e.g. [[0, 0], [0, 1], [0, 2], [1, 2], [2, 2]]
	steps - Number of steps it takes to find the final solution,
			i.e. the number of nodes visited before finding a path (including start and goal node)

	>>> from main import load_map
	>>> grid, start, goal = load_map('test_map.csv')
	>>> dij_path, dij_steps = dijkstra(grid, start, goal)
	It takes 10 steps to find a path using Dijkstra
	>>> dij_path
	[[0, 0], [1, 0], [2, 0], [3, 0], [3, 1]]
	'''
	### YOUR CODE HERE ###
	path = []
	steps = 0
	found = False
	visited, priorityqueue = [], []

	Start = Node(start[0], start[1], grid[start[0]][start[1]],0)
	Start.cost, tC, steps = 0,1,-1
	priorityqueue.append(Start)

	while priorityqueue:
		if found:
			break
		priorityqueue = sorted(priorityqueue, key = lambda node: node.cost)
		curr = priorityqueue.pop(0)
		steps += 1
		row , col = curr.row, curr.col
		for a,b in ((row, col+1), (row+1, col), (row, col-1), (row-1, col)):
			if [row, col] == goal:
				found = True
				path = travel(path,curr)
				path.append(goal)
				break
			elif 0 <= a< len(grid) and 0 <= b < len(grid[0]) and found == False:
				newNode = Node(a,b,grid[a][b], h=0)
				if grid[a][b] == 0:
					newNode.parent = curr
					newNode.cost = newNode.parent.cost + tC
					notpresent = True
					for existingNode in priorityqueue:
						if([existingNode.row, existingNode.col] == [newNode.row,newNode.col]):
							if existingNode.cost > newNode.cost:
								notpresent = False
								existingNode.cost = newNode.cost
								break
					if notpresent is True and (a,b) not in visited:
						priorityqueue.append(newNode)
						visited.append((newNode.row, newNode.col))
		if found:
			break

	if found:
		print(f"It takes {steps} steps to find a path using Dijkstra")
	else:
		print("No path found")
	return path, steps
###########################################
def astar(grid, start, goal):
	'''Return a path found by A* alogirhm
	   and the number of steps it takes to find it.

	arguments:
	grid - A nested list with datatype int. 0 represents free space while 1 is obstacle.
		   e.g. a 3x3 2D map: [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
	start - The start node in the map. e.g. [0, 0]
	goal -  The goal node in the map. e.g. [2, 2]

	return:
	path -  A nested list that represents coordinates of each step (including start and goal node),
			with data type int. e.g. [[0, 0], [0, 1], [0, 2], [1, 2], [2, 2]]
	steps - Number of steps it takes to find the final solution,
			i.e. the number of nodes visited before finding a path (including start and goal node)

	>>> from main import load_map
	>>> grid, start, goal = load_map('test_map.csv')
	>>> astar_path, astar_steps = astar(grid, start, goal)
	It takes 7 steps to find a path using A*
	>>> astar_path
	[[0, 0], [1, 0], [2, 0], [3, 0], [3, 1]]
	'''
	### YOUR CODE HERE ###
	path = []
	steps = 0
	found = False
	visited = []
	priorityqueue = []
	Start = Node(start[0], start[1], grid[start[0]][start[1]],h = 0)
	Start.g , Start.cost = Distance(start,start),0
	h = Distance(start,goal)
	priorityqueue.append(Start)

	while priorityqueue:
		if found:
			break
		priorityqueue = sorted(priorityqueue, key = lambda node: node.cost)
		curr = priorityqueue.pop(0)
		steps += 1
		row , col = curr.row, curr.col
		for a,b in ((row, col+1), (row+1, col), (row, col-1), (row-1, col)):
			if [row, col] == goal:
				found = True
				path = travel(path,curr)
				path.append(goal)
				break
			elif 0 <= a< len(grid) and 0 <= b < len(grid[0]) and found == False:
				h = Distance([a,b],goal)
				newNode = Node(a,b,grid[a][b], h)
				if grid[a][b] == 0:
					newNode.parent = curr
					newNode.g = curr.g + Distance([a,b],[row,col])
					newNode.cost = newNode.g + newNode.h
					notpresent = True
					for existingNode in priorityqueue:
						if([existingNode.row, existingNode.col] == [newNode.row,newNode.col]):
							if existingNode.cost > newNode.cost:
								notpresent = False
								existingNode.cost = newNode.cost
								break
					if notpresent is True and (a,b) not in visited:
						priorityqueue.append(newNode)
						visited.append((newNode.row, newNode.col))
		if found:
			break

	if found:
		print(f"It takes {steps} steps to find a path using A*")
	else:
		print("No path found")
	return path, steps
###########################################
# Doctest
if __name__ == "__main__":
	# load doc test
	from doctest import testmod, run_docstring_examples
	# Test all the functions
	testmod()


import pandas as pd

def mytranspose(filename):
	data = pd.read_csv(filename, sep='\t',header=(0))
	cols=['GeneID','CellBarcode','TranscriptNumber']
	data2=data[cols]
	Final_DF=data2.pivot(index='GeneID', columns='CellBarcode', values='TranscriptNumber').fillna('0')
	name=filename.replace(' ', '')[:-4] + "_"
	Final_DF.columns = name + Final_DF.columns
	return Final_DF

# d1 = mytranspose('table.txt')
# d2 = mytranspose('table2.txt')
# result = pd.merge(Merged_DF, table, how='outer', left_index=True, right_index=True)
# result.fillna('0', inplace=True)

# print (result)
Merged_DF = pd.DataFrame()

with open("list.txt") as file:
	for line in file:
		line = line.strip() #preprocess line
		table=mytranspose(line)
		Merged_DF=pd.merge(Merged_DF, table, how='outer', left_index=True, right_index=True)
		Merged_DF.fillna('0', inplace=True)

Merged_DF.to_csv("merged.txt", header=True, index = True, sep='\t', mode='a')


"""Swiggy URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from s_admin import views

urlpatterns = [
    #path('admin/', admin.site.urls),
    path('',views.admin_login,name='admin_login'),
    path('admin_login_check/',views.admin_login_check,name="admin_login_check"),
    path('admin_home/',views.admin_home,name="admin_home"),
    path('admin_logout/',views.admin_logout,name="admin_logout"),

    #state
    path('open_state/',views.open_state,name="open_state"),
    path('save_state/',views.save_state,name="save_state"),
    path('update_state/',views.update_state,name="update_state"),
    path('update_state_data/',views.update_state_data,name="update_state_data"),
    path('delete_state/',views.delete_state,name='delete_state'),

    #City
    path('open_city/', views.open_city, name="open_city"),
    path('save_city/', views.save_city, name="save_city"),
    # path('update_city/', views.update_city, name="update_city"),
    # path('update_city_data/', views.update_city_data, name="update_city_data"),
    # path('delete_city/', views.delete_city, name='delete_city'),

    #Area
    path('open_area/', views.open_area, name="open_area"),
    path('save_area/', views.save_area, name="save_area"),
    # path('update_area/', views.update_area, name="update_area"),
    # path('update_area_data/', views.update_area_data, name="update_area_data"),
    # path('delete_area/', views.delete_area, name='delete_area'),

    #Type
    path('open_type/', views.open_type, name="open_type"),
    path('save_type/', views.save_type, name="save_type"),

    #restaurant
    path('show_pending_res/',views.pending_res,name="show_pending_res"),

    path('show_approved_res/',views.show_approved_res,name="show_approved_res"),

    path('show_cancel_res/',views.show_cancel_res,name="show_cancel_res"),

    path('approve_res/',views.approve_res,name="approve_res"),
    path('cancel_res/',views.cancel_res,name="cancel_res"),

]


# fit a mask rcnn on the kangaroo dataset
from os import listdir
from xml.etree import ElementTree
from numpy import zeros
from numpy import asarray
from numpy import expand_dims
from numpy import mean
from mrcnn.utils import Dataset
from mrcnn.config import Config
from mrcnn.model import MaskRCNN
from mrcnn.model import mold_image
from mrcnn.utils import compute_ap
from mrcnn.model import load_image_gt
from glob import glob
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot
from matplotlib.patches import Rectangle
import numpy as np
import cv2
from PIL import Image
import os,sys
from mrcnn.visualize import display_instances
from mrcnn.utils import extract_bboxes

from matplotlib import pyplot
from matplotlib.patches import Rectangle
from mrcnn.model import mold_image
# class that defines and loads the kangaroo dataset
def rot(n):
    n = np.asarray(n).flatten()
    assert(n.size == 3)

    theta = np.linalg.norm(n)
    if theta:
        n /= theta
        K = np.array([[0, -n[2], n[1]], [n[2], 0, -n[0]], [-n[1], n[0], 0]])

        return np.identity(3) + np.sin(theta) * K + (1 - np.cos(theta)) * K @ K
    else:
        return np.identity(3)

def get_bbox(p0, p1):

    v = np.array([
        [p0[0], p0[0], p0[0], p0[0], p1[0], p1[0], p1[0], p1[0]],
        [p0[1], p0[1], p1[1], p1[1], p0[1], p0[1], p1[1], p1[1]],
        [p0[2], p1[2], p0[2], p1[2], p0[2], p1[2], p0[2], p1[2]]
    ])
    e = np.array([
        [2, 3, 0, 0, 3, 3, 0, 1, 2, 3, 4, 4, 7, 7],
        [7, 6, 1, 2, 1, 2, 4, 5, 6, 7, 5, 6, 5, 6]
    ], dtype=np.uint8)

    return v, e
class KangarooDataset(Dataset):
    # load the dataset definitions
    def load_dataset(self, dataset_dir):
        # define one class
        self.add_class("dataset", 1, "kangaroo")
        # define data locations
        files = glob(dataset_dir+'/*_image.jpg')
        # find all images
        for idx in range(0,len(files)):
            # extract image id
            image_path=files[idx].split('\\')[-1]
            image_id=image_path.split('.')[-2]
            # skip bad images
            img_path = files[idx]
            ann_path = files[idx]
            # add to dataset
            self.add_image('dataset', image_id=image_id, path=img_path, annotation=ann_path)

    # extract bounding boxes from an annotation file

    def extract_boxes(self, filename):
    #filename is trainval/*/*jpg
        xyz = np.fromfile(filename.replace('_image.jpg', '_cloud.bin'), dtype=np.float32)
        xyz = xyz.reshape([3, -1])
        proj = np.fromfile(filename.replace('_image.jpg', '_proj.bin'), dtype=np.float32)
        proj.resize([3, 4])

        try:
            bbox = np.fromfile(filename.replace('_image.jpg', '_bbox.bin'), dtype=np.float32)
        except FileNotFoundError:
            print('[*] bbox not found.')
            bbox = np.array([], dtype=np.float32)

        bbox = bbox.reshape([-1, 11])

        uv = proj @ np.vstack([xyz, np.ones_like(xyz[0, :])])
        uv = uv / uv[2, :]
        boxes = list()
        for k, b in enumerate(bbox):
            R = rot(b[0:3])
            t = b[3:6]
            sz = b[6:9]
            vert_3D, edges = get_bbox(-sz / 2, sz / 2)
            vert_3D = R @ vert_3D + t[:, np.newaxis]

            vert_2D = proj @ np.vstack([vert_3D, np.ones(vert_3D.shape[1])])
            vert_2D = vert_2D / vert_2D[2, :]

            ignore_in_eval = bool(b[10])
            if ignore_in_eval:
                continue
            else:
                xmax=1914
                ymax=1052
                xl=int(min(vert_2D[0]))
                xr=int(max(vert_2D[0]))
                yb=int(min(vert_2D[1]))
                yu=int(max(vert_2D[1]))
                xmin=max(xl,0)
                xmax=min(xr,xmax)
                ymin=max(yb,0)
                ymax=min(yu,ymax)
                coors = [xmin, ymin, xmax, ymax]
                boxes.append(coors)
        width = int(1914)
        height = int(1052)
        return boxes, width, height
    # load the masks for an image
    def load_mask(self, image_id):
        # get details of image
        info = self.image_info[image_id]
        # define box file location
        path = info['annotation']
        # load XML
        boxes, w, h = self.extract_boxes(path)
        # create one array for all masks, each on a different channel
        masks = zeros([h, w, len(boxes)], dtype='uint8')
        # create masks
        class_ids = list()
        for i in range(len(boxes)):
            box = boxes[i]
            row_s, row_e = box[1], box[3]
            col_s, col_e = box[0], box[2]
            masks[row_s:row_e, col_s:col_e, i] = 1
            class_ids.append(self.class_names.index('kangaroo'))
        return masks, asarray(class_ids, dtype='int32')

    # load an image reference
    def image_reference(self, image_id):
        info = self.image_info[image_id]
        return info['path']

# define a configuration for the model
class KangarooConfig(Config):
    # define the name of the configuration
    NAME = "kangaroo_cfg"
    # number of classes (background + kangaroo)
    NUM_CLASSES = 1 + 1
    # number of training steps per epoch
    STEPS_PER_EPOCH = 94
# define the prediction configuration
class PredictionConfig(Config):
    # define the name of the configuration
    NAME = "kangaroo_cfg"
    # number of classes (background + kangaroo)
    NUM_CLASSES = 1 + 1
    # simplify GPU config
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

# calculate the mAP for a model on a given dataset
def evaluate_model(dataset, model, cfg):
    APs = list()
    for image_id in dataset.image_ids:
        # load image, bounding boxes and masks for the image id
        image, image_meta, gt_class_id, gt_bbox, gt_mask = load_image_gt(dataset, cfg, image_id, use_mini_mask=False)
        # convert pixel values (e.g. center)
        scaled_image = mold_image(image, cfg)
        # convert image into one sample
        sample = expand_dims(scaled_image, 0)
        # make prediction
        yhat = model.detect(sample, verbose=0)
        # extract results for first sample
        r = yhat[0]
        # calculate statistics, including AP
        AP, _, _, _ = compute_ap(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks'])
        # store
        APs.append(AP)
    # calculate the mean AP across all images
    mAP = mean(APs)
    return mAP
def plot_actual_vs_predicted(image,mask, model, cfg, n_images=1):
    # load image and mask
        # convert pixel values (e.g. center)
    scaled_image = mold_image(image, cfg)
        # convert image into one sample
    sample = expand_dims(scaled_image, 0)
        # make prediction
    yhat = model.detect(sample, verbose=0)[0]
        # define subplot
    pyplot.subplot(n_images, 2, 1)
        # plot raw pixel data
    pyplot.imshow(image)
    pyplot.title('Actual')
        # plot masks
    for j in range(mask.shape[2]):
        pyplot.imshow(mask[:, :, j], cmap='gray', alpha=0.3)
        # get the context for drawing boxes
    pyplot.subplot(n_images, 2, 2)
        # plot raw pixel data
    pyplot.imshow(image)
    pyplot.title('Predicted')
    ax = pyplot.gca()
        # plot each box
#    for box,i in zip(yhat['rois'],range(len(yhat['rois']))):
            # get coordinates
    score=yhat['scores']
    index=np.argmax(score)
    box=yhat['rois'][index]
    y1, x1, y2, x2 = box
            # calculate width and height of the box
    width, height = x2 - x1, y2 - y1
            # create the shape
    rect = Rectangle((x1, y1), width, height, fill=False, color='red')
            # draw the box
    display_txt = '{:0.2f}, car'.format(yhat['scores'][index])
    ax.add_patch(rect)
    ax.text(x1, y1, display_txt, bbox={'facecolor':'red', 'alpha':0.5})
    # show the figure
    pyplot.show()

def bboxcut (image,image_path, model, cut_path, cfg):
    scaled_image = mold_image(image, cfg)
    sample = expand_dims(scaled_image, 0)
    yhat = model.detect(sample, verbose=0)[0]
    score=yhat['scores']
    if not score.size:
        lb=0
    else:
        index=np.argmax(score)
        box=yhat['rois'][index]
        y1, x1, y2, x2 = box
        width, height = x2 - x1, y2 - y1
        xmax=1914
        ymax=1052
        xl=x1
        xr=x2
        yb=y1
        yu=y2
        xl=max(xl,0)
        xr=min(xr,xmax)
        yb=max(yb,0)
        yu=min(yu,ymax)
        file_name=image_path.split('/')[-1]
        folder_name=cut_path+image_path.split('/')[-2]
        if not os.path.exists(folder_name):
            os.makedirs(folder_name)
        if score[index]>=0.95:
            img1=Image.open(image_path)
            imgNew=img1.crop((xl,yb,xr,yu))
            imgNew.save(os.path.join(folder_name,os.path.basename(file_name)))
            lb=1
        else:
            lb=0
    return lb

# prepare train set

# prepare test/val set
# create config
cfg = PredictionConfig()
# define the model
model = MaskRCNN(mode='inference', model_dir='./', config=cfg)
# load model weights
model.load_weights('mask_rcnn_kangaroo_cfg_0004.h5', by_name=True)
#train_mAP = evaluate_model(train_set, model, cfg)
#print("Train mAP: %.3f" % train_mAP)
# plot predictions for train dataset

folder_lj=glob('test/*')    #change dir
path_test = 'test/*/*.jpg'
files_test = glob(path_test)
label=np.zeros((len(files_test),1))
count=0
for j in range(len(folder_lj)):
    folder_name=folder_lj[j].split('\\')[-1]
    train_set = KangarooDataset()
    train_folder_path='test/'+folder_name
    train_set.load_dataset(train_folder_path)
    train_set.prepare()
    print('Train: %d' % len(train_set.image_ids))
    test_img=glob(train_folder_path+'/*.jpg') #change dir
    for i in range(len(test_img)):
        snapshot = test_img[i]
        #guid=snapshot.split('\\')[-2]
        #guid=guid.split('/')[-1]
        idx=snapshot.split('\\')[-1]
        cut_path = 'car/'
        image_path='{}/{}'.format(train_folder_path,idx)
        image = train_set.load_image(i)
        label[count+i]=bboxcut(image,image_path, model, cut_path, cfg)
    count=count+ len(test_img)
names =  []
for k in range(len(files_test)):
    snapshot1 = files_test[k]
    guid=snapshot1.split('\\')[-2]
    guid=guid.split('/')[-1]
    idx1=snapshot1.split('\\')[-1]
    idx1=idx1.split('.')[-2]
    idx1=idx1.split('_')[-2]
    names.append(str(guid+"/"+idx1))

labels = label.astype(int)
names = np.reshape(np.array(names),(len(files_test),1))
final = np.column_stack((names,labels))
np.savetxt('car1129.csv', final, delimiter=',', header = "guid/image,label",fmt='%s',comments ='')


import unittest
from unittest.mock import patch

from services.api.routes.manage import Manage

# pylint: disable=protected-access
class ManageTestCase(unittest.TestCase):

    def setUp(self):
        self.patches = []
        self.mocks = {}

        flask_resource_patch = patch('services.api.routes.manage.Resource')
        self.mocks['flask_resource_mock'] = flask_resource_patch.start()
        self.patches.append(flask_resource_patch)

        creation_service_patch = patch('services.api.routes.manage.CreationService')
        self.mocks['creation_service_mock'] = creation_service_patch.start()
        self.patches.append(creation_service_patch)

        addition_service_patch = patch('services.api.routes.manage.AdditionService')
        self.mocks['addition_service_mock'] = addition_service_patch.start()
        self.patches.append(addition_service_patch)

        removal_service_patch = patch('services.api.routes.manage.RemovalService')
        self.mocks['removal_service_mock'] = removal_service_patch.start()
        self.patches.append(removal_service_patch)

    def tearDown(self):
        for patch_ in self.patches:
            patch_.stop()

    def test_post_returns_creation_service_call(self):
        # Act
        Manage.post()

        # Assert
        self.mocks['creation_service_mock'].return_value.create.assert_called()

    def test_put_returns_addition_service_call(self):
        # Act
        Manage.put()

        # Assert
        self.mocks['addition_service_mock'].return_value.add.assert_called()

    def test_delete_removal_service_call(self):
        # Act
        Manage.delete()

        # Assert
        self.mocks['removal_service_mock'].return_value.remove.assert_called()


from typing import List, Tuple

from pydantic import BaseModel

class BaseMedia(BaseModel):
    username: str
    user_id: str
    media_pk: int
    media_type: int
    caption: str

class CreateMedia(BaseMedia):
    url_list: List[Tuple[int, str]]

class Media(BaseMedia):
    id: int

    class Config:
        orm_mode = True