import requests
import time
from parsel import Selector

from tech_news.database import create_news

# Requisito 1
def fetch(url):
    time.sleep(1)

    try:
        headers = {"user-agent": "Fake user-agent"}
        response = requests.get(url, headers=headers, timeout=3)
        response.raise_for_status()
    except (requests.ReadTimeout, requests.HTTPError, requests.Timeout):
        return None
    else:
        return response.text

# Requisito 2
def scrape_novidades(html_content):
    selector = Selector(html_content)
    return selector.css("a.cs-overlay-link::attr(href)").getall()

# Requisito 3
def scrape_next_page_link(html_content):
    selector = Selector(html_content)
    return selector.css("a.next.page-numbers::attr(href)").get()

# Requisito 4
def scrape_noticia(html_content):
    selector = Selector(html_content)

    return {
        "url": selector.css("link[rel=canonical]::attr(href)").get(),
        "title": selector.css("h1.entry-title::text").get().strip(),
        "timestamp": selector.css("li.meta-date::text").get(),
        "writer": selector.css("span.author a.url.fn.n::text").get(),
        "comments_count": len(selector.css("ol.comment-list li").getall()),
        "summary": "".join(
            selector.css("div.entry-content > p:nth-of-type(1) *::text")
            .getall()).strip(),
        "tags": selector.css("a[rel=tag]::text").getall(),
        "category": selector.css("span.label::text").get()
    }

# Requisito 5
def get_tech_news(amount):
    url = "https://blog.betrybe.com"
    tech_news = []

    while len(tech_news) <= amount:
        data = fetch(url)
        news_urls = scrape_novidades(data)

        for news_url in news_urls:
            news = scrape_noticia(fetch(news_url))
            tech_news.append(news)

        url = scrape_next_page_link(data)

    create_news(tech_news[:amount])
    return tech_news[:amount]


from tkinter import *
from tkinter import ttk
import datetime
import math
from pathlib import Path
import os, shutil
#---------------------------
import keras
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, Activation
from keras.models import Sequential
from pathlib import Path
from keras.optimizers import SGD,RMSprop,adam
from keras.utils import np_utils
from google_images_download import google_images_download
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import os
import theano
from PIL import Image
from numpy import *
# SKLEARN
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
#-----------------------------
import cv2, time
from keras.models import model_from_json
from pathlib import Path
from keras.preprocessing import image
import numpy as np
from PIL import Image
import tkinter as tk
from tkinter import messagebox

class MainPage:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.titlelbl = tk.Label(self.frame, text = "Easy CNN")
        self.titlelbl.config(font=("Helvetica", 20))
        self.titlelbl.pack()
        self.namelbl = tk.Label(self.frame, text = "made by Bryan Kyritz")
        self.namelbl.config(font=("Helvetica", 10))
        self.namelbl.pack()
        #tensorflow logo
        dir_path = os.path.dirname(os.path.realpath(__file__))
        load = Image.open(dir_path + "\data\pictures\logo.png")

        image = tk.PhotoImage(file=dir_path + "\data\pictures\logo.png")

        self.tfLogoPic = tk.Label(self.frame, image = image)
        self.tfLogoPic.config(image=image)
        self.tfLogoPic.photo = image
        self.tfLogoPic.pack()
        # -----------------------------------------------------------------
        self.titlelbl = tk.Label(self.frame, text = "Menu")
        self.titlelbl.config(font=("Helvetica", 12))
        self.titlelbl.pack()
        self.helpBtn = tk.Button(self.frame, text = 'More Info', width = 25, command = self.help_window)
        self.helpBtn.pack()
        self.trainBtn = tk.Button(self.frame, text = 'Train Model', width = 25, command = self.train_window)
        self.trainBtn.pack()
        self.testBtn = tk.Button(self.frame, text = 'Test Model', width = 25, command = self.test_window)
        self.testBtn.pack()
        self.genCodeBtn = tk.Button(self.frame, text = 'Generate Code', width = 25, command = self.genCode_window)
        self.genCodeBtn.pack()
        self.getDataOnlineBtn = tk.Button(self.frame, text = 'Get Data from online', width = 25, command = self.getDataOnline_window)
        self.getDataOnlineBtn.pack()
        self.getDataFromVideoBtn = tk.Button(self.frame, text = 'Get Data from video', width = 25, command = self.getDataVideo_window)
        self.getDataFromVideoBtn.pack()
        self.analyzeVidBtn = tk.Button(self.frame, text = 'Analyze pre-recorded video', width = 25, command = self.analyzeVid_window)
        self.analyzeVidBtn.pack()
        self.analyzeVidBtn = tk.Button(self.frame, text = 'Analyze image', width = 25, command = self.analyzeImg_window)
        self.analyzeVidBtn.pack()
        self.frame.pack()
    def help_window(self):
        self.newWindow = tk.Toplevel(self.master)
        self.app = HelpPage(self.newWindow)
    def train_window(self):
        self.newWindow = tk.Toplevel(self.master)
        self.app = TrainPage(self.newWindow)
    def test_window(self):
        self.newWindow = tk.Toplevel(self.master)
        self.app = TestPage(self.newWindow)
    def genCode_window(self):
        self.newWindow = tk.Toplevel(self.master)
        self.app = GenCodePage(self.newWindow)
    def getDataOnline_window(self):
        self.newWindow = tk.Toplevel(self.master)
        self.app = GetDataOnlinePage(self.newWindow)
    def getDataVideo_window(self):
        self.newWindow = tk.Toplevel(self.master)
        self.app = GetDataFromVideo(self.newWindow)
    def analyzeVid_window(self):
        self.newWindow = tk.Toplevel(self.master)
        self.app = analyzeVideoPage(self.newWindow)
    def analyzeImg_window(self):
        self.newWindow = tk.Toplevel(self.master)
        self.app = analyzeImagePage(self.newWindow)
class HelpPage:
    def __init__(self, master):
        titleFontSize = 20
        subtitleFontSize = 16
        answerFontSize = 12
        self.master = master
        self.frame = tk.Frame(self.master)
        self.titleMessage = tk.Label(self.master, text="Help")
        self.titleMessage.config(font=("Helvetica", titleFontSize))
        self.titleMessage.pack()

        self.one = tk.Label(self.master,  text="What does this application do?")
        self.one.config(font=("Helvetica", subtitleFontSize))
        self.one.pack()
        self.oneA = tk.Label(self.master, justify=LEFT, text ="    This app was made by Bryan Kyritz in order to make the implication of \n" +
                                                "    Convolutional neural network(CNN) for image detection easy for those who do not know \n" +
                                                "    how to use tensorflow. This app can only detect if a single object is present in an image \n"+
                                                "    or video. Future updates will include the ability to:\n\n"+
                                                "    •Train for multiple objects\n"+
                                                "    •Generate bounding boxes around the objects by implementing RCNN or YOLO\n"+
                                                "    •More customizability (Changing the training settings within the app")
        self.oneA.config(font=("Helvetica", answerFontSize))
        self.oneA.pack()

        self.two = tk.Label(self.master,  text="How does this app work?")
        self.two.config(font=("Helvetica", subtitleFontSize))
        self.two.pack()
        self.twoA = tk.Label(self.master, justify=LEFT, text= "    This app uses Tensorflow, developed by Google, to build a neural netowork which\n"+
                                                    "    is a mathematical model that can learn to detect patterns in order to solve problems.\n"+
                                                    "    The model that is built is a Convolutional Neural Network(CNN) a convolutional neural network \n"+
                                                    "    is a class of deep neural networks, most commonly applied to analyzing visual imagery(computer vision). CNNs use a \n"+
                                                    "    variation of multilayer perceptrons designed to require minimal preprocessing.")
        self.twoA.config(font=("Helvetica", answerFontSize))
        self.twoA.pack()
        self.frame.pack()
class TrainPage:

    nameOfObject = ""
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.title = tk.Label(self.master, text="Train Model \n")
        self.title.config(font=("Helvetica", 20))
        self.title.pack()
        self.msglbl = tk.Label(self.master, text = "*Make sure you put the data into the training folders*")
        self.msglbl.pack()
        self.nameOfObjectlbl = tk.Label(self.master,text = "Enter the object you are training for:")
        self.nameOfObjectlbl.pack()
        self.nameOfObjTrainingtxt = tk.Text(self.master,width = 20, height = 1)
        self.nameOfObjTrainingtxt.pack()
        self.trainBtn = tk.Button(self.master, text="Train", command = self.train)
        self.trainBtn.pack()
        self.frame.pack()
    #------------------------------------------------------------------------------Deletes all files in a directory
    def deleteFilesInDir(self, folder):
        for the_file in os.listdir(folder):
            file_path = os.path.join(folder, the_file)
            try:
                if os.path.isfile(file_path):
                    os.unlink(file_path)
                elif os.path.isdir(file_path):
                    shutil.rmtree(file_path)
            except Exception as e:
                print(e)
    def train(self):
        print("Train Model")

        self.modelName = self.nameOfObjTrainingtxt.get("1.0",END).replace('\n','')
        debug = True
        dir_path = os.path.dirname(os.path.realpath(__file__))
        nameOfObject = self.modelName
        print(dir_path)
        #-----------------------------------------------------------------------Directories being used to get images and save images
        org_Obj_dir = dir_path + r"\training_data\object"
        org_not_Obj_dir = dir_path + r"\training_data\not_object"
        edited_obj_dir = dir_path + r"\training_data\edited_object"
        edited_not_obj_dir = dir_path + r"\training_data\edited_not_object"
        all_data_new = dir_path + r"\training_data\allDataNew"

        #pictures are downsized to 200 by 200 pixels
        img_rows, img_cols = 200, 200

        #number of channels (red, green, blue)
        img_channels = 3

        print("------------------------------------------------------------------")

        self.deleteFilesInDir(all_data_new)
        self.deleteFilesInDir(edited_not_obj_dir)
        self.deleteFilesInDir(edited_obj_dir)
        print("Reset folders")

        listing = os.listdir(org_Obj_dir)
        num_samples_obj = size(listing)
        print("Number of object samples:" + str(num_samples_obj))

        for file in listing:
            im = Image.open(org_Obj_dir + '\\' + file)
            img = im.resize((img_rows, img_cols))
            img.save(edited_obj_dir + '\\' + file, "JPEG")
            img.save(all_data_new + '\\' + file, "JPEG")

        if (debug):
            print("Done editing pictures!")

        # converts images into an array
        imlist = os.listdir(edited_obj_dir)
        im1 = array(Image.open(edited_obj_dir + '\\' + imlist[0]))  # open one image to get size
        m, n = im1.shape[0:2]  # get the size of the images
        imnbr = len(imlist)  # get the number of images

        # -------------------------------------------------------------Edits noncube pictures
        listing = os.listdir(org_not_Obj_dir)
        num_samples_not_obj = size(listing)

        if (debug):
            print("Number of non-object samples:" + str(num_samples_not_obj))

        for file in listing:
            im = Image.open(org_not_Obj_dir + '\\' + file)
            img = im.resize((img_rows, img_cols))
            img.save(edited_not_obj_dir + '\\' + file, "JPEG")
            img.save(all_data_new + '\\' + file, "JPEG")

        if (debug):
            print("Done editing object pictures!")

        # converts images into an array
        imlist2 = os.listdir(edited_not_obj_dir)
        im2 = array(Image.open(edited_not_obj_dir + '\\' + imlist2[0]))  # open one image to get size
        m, n = im2.shape[0:2]  # get the size of the images
        imnbr = len(imlist2)  # get the number of images
        # -------------------------------------------------------------Creates matrix to store all flattened images

        both_lists = imlist + imlist2

        print(len(both_lists))

        try:
            immatrix = array([array(Image.open(all_data_new + '\\' + im3)).flatten()
                          for im3 in both_lists], 'f')  #'f'
        except ValueError:
            print("error in classic spot")
            messagebox.showerror("ERROR", "There is an error with your training data")
            self.deleteFilesInDir(all_data_new)
            self.deleteFilesInDir(edited_not_obj_dir)
            self.deleteFilesInDir(edited_obj_dir)
            return 0
        total_Samples = num_samples_obj + num_samples_not_obj
        label = np.ones((total_Samples,), dtype=int)
        label[0:num_samples_obj] = 1  # object
        label[num_samples_not_obj:] = 0  # not object

        data, Label = shuffle(immatrix, label, random_state=2)
        train_data = [data, Label]

        print(train_data[0].shape)
        print(train_data[1].shape)

        # -------------------------------------------------------------Neural network Settings
        # batch_size to train
        batch_size = 32
        # number of output classes
        nb_classes = 2  # 1 = has cube. 0 = doesnt have cube
        # number of epochs to train
        nb_epoch = 20
        # number of convolutional filters to use
        nb_filters = 32
        # size of pooling area for max pooling
        nb_pool = 2
        # convolution kernel size
        nb_conv = 3
        # -------------------------------------------------------------split X and y into training and testing sets
        (X, y) = (train_data[0], train_data[1])
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=4)

        X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, img_channels)
        X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, img_channels)

        X_train = X_train.astype('float32')
        X_test = X_test.astype('float32')

        X_train /= 255
        X_test /= 255
        if (debug):
            print('X_train shape:', X_train.shape)
            print(X_train.shape[0], 'train samples')
            print(X_test.shape[0], 'test samples')

        Y_train = np_utils.to_categorical(y_train, nb_classes)
        Y_test = np_utils.to_categorical(y_test, nb_classes)
        # -------------------------------------------------------------Neural Network Model
        model = Sequential()

        model.add(Conv2D(nb_filters, (nb_conv, nb_conv),
                         padding='same',
                         input_shape=(img_rows, img_cols, img_channels)))
        convout1 = Activation('relu')
        model.add(convout1)
        model.add(Conv2D(nb_filters, (nb_conv, nb_conv), padding='same'))

        convout2 = Activation('relu')
        model.add(convout2)
        model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
        model.add(Dropout(0.5))

        model.add(Flatten())
        model.add(Dense(128))
        model.add(Activation('relu'))
        model.add(Dropout(0.5))
        model.add(Dense(nb_classes))
        model.add(Activation('softmax'))

        model.compile(loss='binary_crossentropy',
                      optimizer='adadelta',  # 'adam' also an option
                      metrics=['accuracy'])
        # -------------------------------------------------------------Train the Neural network
        model.fit(
            X_train,
            Y_train,
            batch_size=batch_size,
            epochs=nb_epoch,
            verbose=1,
            validation_data=(X_test, Y_test), )

        model.fit(
            X_train,
            Y_train,
            batch_size=batch_size,
            epochs=nb_epoch,
            verbose=1,
            validation_split=0.2, )

        # Save neural network's trained weights and structure
        savelink = dir_path + r"\saved_Weights\model_structure_" + nameOfObject + ".json"

        model_structure = model.to_json()
        f = Path(savelink)
        f.write_text(model_structure)

        saveLink2 = dir_path + r"\saved_Weights\model_weights_" + nameOfObject + ".h5"
        model.save_weights(saveLink2)
        self.deleteFilesInDir(all_data_new)
        self.deleteFilesInDir(edited_not_obj_dir)
        self.deleteFilesInDir(edited_obj_dir)
        messagebox.showinfo("Good News", "Done Training!")
        print("Done!")
class TestPage:
    save = False
    def __init__(self, master):
        titleFontSize = 20
        subtitleFontSize = 16
        answerFontSize = 12
        self.master = master
        self.frame = tk.Frame(self.master)
        self.titleMessage = tk.Label(self.master, text="Testing")
        self.titleMessage.config(font=("Helvetica", titleFontSize))
        self.titleMessage.pack()
        self.trainlbl = tk.Label(self.master, text = "Enter the Name of object:")
        self.trainlbl.pack()
        self.nameOfObjTrainingtxt = tk.Text(self.master,width = 20, height = 1)
        self.nameOfObjTrainingtxt.pack()
        self.askSave = tk.Checkbutton(self.master, text = "Save", command = self.check)
        self.askSave.pack()
        self.cameraBtn = tk.Button(self.master, text="Camera", command = self.camera)
        self.cameraBtn.pack()
        self.frame.pack()

    def check(self):
        if self.save == True:
            self.save = False
        else:
            self.save = True
    def camera(self):

        foundPath = True
        nameOfObject = self.nameOfObjTrainingtxt.get("1.0",END).replace('\n','')

        dir_path = os.path.dirname(os.path.realpath(__file__))

        savelink = dir_path + r"\saved_Weights\model_structure_" + nameOfObject + ".json"
        saveLink2 = dir_path + r"\saved_Weights\model_weights_" + nameOfObject + ".h5"

        print(dir_path)

        class_labels = ["Not Object",nameOfObject]
        #Load the json file that contains the model's structure
        try:
            foundPath = True
            f = Path(savelink)
            model_structure = f.read_text()
        except IOError:
            foundPath = False
            messagebox.showerror("ERROR", "Could not find weights for: "+nameOfObject)

        if foundPath:
            f = Path(savelink)
            model_structure = f.read_text()
            # Recreate the Keras model object from the json data
            model = model_from_json(model_structure)

            # Re-load the model's trained weights
            model.load_weights(saveLink2)

            # -----------------------------------------------------------------------------------Resizes image
            img_rows = 200  # what we converted all the training data to (200x200 image)
            img_cols = 200

            #---------------------------------------------------------SAVING VIDEO INFO
            FILE_OUTPUT = nameOfObject + '.avi'

            if os.path.isfile(FILE_OUTPUT): #deletes old video with thw same name
                os.remove(FILE_OUTPUT)

            video = cv2.VideoCapture(0)
            if self.save == True:
                fourcc = cv2.VideoWriter_fourcc(*'MJPG')
                width = video.get(cv2.CAP_PROP_FRAME_WIDTH)   # float
                height = video.get(cv2.CAP_PROP_FRAME_HEIGHT)
                fps = video.get(cv2.CAP_PROP_FPS)

                out = cv2.VideoWriter(FILE_OUTPUT, fourcc, 20.0, (int(width), int(height)))

            while True:
                check, frame = video.read()
                cv2.imwrite('currentFrame.jpg', frame)

                im = Image.open("currentFrame.jpg")  # image name
                img = im.resize((img_rows, img_cols))
                image_to_test = image.img_to_array(img)
                list_of_images = np.expand_dims(image_to_test, axis=0)
                results = model.predict(list_of_images)
                single_result = results[0]
                most_likely_class_index = int(np.argmax(single_result))
                class_likelihood = single_result[most_likely_class_index]
                class_label = class_labels[most_likely_class_index]

                # Print the result
                print("This is image is a {} - Likelihood: {:2f}".format(class_label, class_likelihood))
                key = cv2.waitKey(10)
                if key == ord('q'):
                    break

                font = cv2.FONT_HERSHEY_SIMPLEX
                bottomLeftCornerOfText = (80, 30)
                fontScale = 0.5
                fontColor = (255, 255, 255)
                lineType = 2

                cv2.putText(frame,str(class_label) + "|" + str(class_likelihood),
                            bottomLeftCornerOfText,
                            font,
                            fontScale,
                            fontColor,
                            lineType)
                # Edit image in here
                if most_likely_class_index == 0:
                    cv2.circle(frame, (50, 50), 20, (0, 0, 255), -1) #not object indicator
                else:
                    cv2.circle(frame, (50, 50), 20, (0, 255, 0), -1) #object indicator

                # Saving image in here
                if self.save == True:  # saves video
                    out.write(frame)
                #-----------------------
                cv2.imshow("Capturing", frame)
            if self.save == True:
                out.release()
                messagebox.showinfo("Good News", "Saved Video!")
            video.release()
            cv2.destroyAllWindows()
class GenCodePage:
    def __init__(self, master):
        titleFontSize = 20
        subtitleFontSize = 16
        answerFontSize = 12
        self.master = master
        self.frame = tk.Frame(self.master)
        self.titleMessage = tk.Label(self.master, text="Generate Code")
        self.titleMessage.config(font=("Helvetica", titleFontSize))
        self.titleMessage.pack()
        self.trainlbl = tk.Label(self.master, text="Enter the Name of object:")
        self.trainlbl.pack()
        self.nameOfObjTrainingtxt = tk.Text(self.master,width = 20, height = 1)
        self.nameOfObjTrainingtxt.pack()
        self.genBtn = tk.Button(self.master, text="Generate", command = self.genCode)
        self.genBtn.pack()

    def genCode(self):
        nameOfObject = self.nameOfObjTrainingtxt.get("1.0", END).replace('\n', '')
        f = open(nameOfObject + "_Generated_Code" + ".txt", "w")
        f.write("from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, Activation\n"+
                "from keras.models import Sequential\n"+
                "from pathlib import Path\n"+
                "from keras.optimizers import SGD,RMSprop,adam\n"+
                "def camera(self):\n" +
                "import cv2, time\n"+
                "from keras.models import model_from_json\n"+
                "from pathlib import Path\n"+
                "from keras.preprocessing import image\n"+
                "import numpy as np\n"+
                "import theano\n" +
                "\tfoundPath = True\n" +
                "\tdir_path = os.path.dirname(os.path.realpath(__file__))\n" +
                "\tsavelink = dir_path + r\"\saved_Weights\model_structure_\" + nameOfObject + \".json\"\n" +
                "\tsaveLink2 = dir_path + r\"\saved_Weights\model_weights_\" + nameOfObject + \".h5\"\n" +
                "\tclass_labels = [\"Not Object\", nameOfObject,]\n" +
                "\ttry:\n" +
                "\t\tfoundPath = True\n" +
                "\t\tf = Path(savelink)\n" +
                "\t\tmodel_structure = f.read_text()\n" +
                "\texcept IOError:\n" +
                "\t\tfoundPath = False\n" +
                "\t\tmessagebox.showerror(\"ERROR\", \"Could not find weights for: \"+nameOfObject)\n" +
                "\tif foundPath:\n" +
                "\t\tf = Path(savelink)\n" +
                "\t\tfmodel_structure = f.read_text()\n" +
                "\t\tmodel = model_from_json(model_structure)\n" +
                "\t\tmodel.load_weights(saveLink2)\n" +
                "\t\timg_rows = 200\n" +
                "\t\timg_cols = 200\n" +
                "\t\tvideo = cv2.VideoCapture(0)\n" +
                #---------------------------------------------------
                "\t\tim = Image.open(\"currentFrame.jpg\")  # image name\n" +
                "\t\timg = im.resize((img_rows, img_cols))\n" +
                "\t\timage_to_test = image.img_to_array(img)\n" +
                "\t\tlist_of_images = np.expand_dims(image_to_test, axis=0)\n" +
                "\t\tresults = model.predict(list_of_images)\n" +
                "\t\tsingle_result = results[0]\n" +
                "\t\tmost_likely_class_index = int(np.argmax(single_result))\n" +
                "\t\tclass_likelihood = single_result[most_likely_class_index]\n" +
                "\t\tclass_label = class_labels[most_likely_class_index]\n" +
                "\t\tprint(\"This is image is a {} - Likelihood: {:2f}\".format(class_label, class_likelihood))\n" +
                "\t\tif most_likely_class_index == 0:\n" +
                "\t\t\treturn False\n" +
                "\t\telse:\n" +
                "\t\t\treturn True\n"
                )
class GetDataOnlinePage:
    saveChoice = 1 #1 = normal   | #2 = different

    def __init__(self, master):
        titleFontSize = 20
        subtitleFontSize = 16
        answerFontSize = 12
        self.master = master
        self.frame = tk.Frame(self.master)
        self.titleMessage = tk.Label(self.master, text="Get Data Online")
        self.titleMessage.config(font=("Helvetica", titleFontSize))
        self.titleMessage.pack()
        self.trainlbl = tk.Label(self.master, text="Enter the Name of object:")
        self.trainlbl.pack()
        self.nameOfObjTrainingtxt = tk.Text(self.master,width = 20, height = 1)
        self.nameOfObjTrainingtxt.pack()
        self.numlbl = tk.Label(self.master, text="How many Images?")
        self.numlbl.pack()
        self.numtxt = tk.Text(self.master,width = 20, height = 1)
        self.numtxt.pack()
        self.saveNorm = tk.Radiobutton(self.master, text="Normal Save(may overide old data)", variable=self.saveChoice, value=1).pack(anchor=W)
        self.saveDif = tk.Radiobutton(self.master, text="Different save", variable=self.saveChoice, value=2).pack(anchor=W)
        self.dirlbl = tk.Label(self.master, text="Directory to save(only if you selected \"Different save\"")
        self.dirlbl.pack()
        self.dirtxt = tk.Text(self.master,width = 20, height = 1)
        self.dirtxt.pack()
        self.genBtn = tk.Button(self.master, text="Get Data", command = self.getData)
        self.genBtn.pack()
    #---------------------------------------------------------------------------------Delets broken downloads
    def deleteBrokenFiles(self, folder):
        counter = 0
        for the_file in os.listdir(folder):
            file_path = os.path.join(folder, the_file)
            ending = file_path[-3:]
            if ending != "jpg":
                counter = counter+1
                if os.path.isfile(file_path):
                    os.unlink(file_path)
                elif os.path.isdir(file_path):
                    shutil.rmtree(file_path)
        print("Deleted " + str(counter) + " broken pictures")
    #----------------------------------------------------------------------------------Downloads and organizes saved images from google images, used to build a dataset
    def getData(self):

        nameOfObject = self.nameOfObjTrainingtxt.get("1.0",END).replace('\n','')
        numLimit = self.numtxt.get("1.0",END).replace('\n','')

        isVal = True
        if nameOfObject == "":
            isVal = False
        if numLimit == "":
            isVal = False

        if isVal:
            self.loadingLbl = tk.Label(self.master, text="Downloading...This may take a while...")
            self.loadingLbl.pack()
            nameOfObject = self.nameOfObjTrainingtxt.get("1.0",END).replace('\n','')
            numLimit = self.numtxt.get("1.0",END).replace('\n','')

            dir_path = os.path.dirname(os.path.realpath(__file__))

            response = google_images_download.googleimagesdownload()
            arguments = {"keywords": nameOfObject, "limit": numLimit, "format": "jpg", "chromedriver": dir_path + r"\data\chromedriver.exe"}
            absolute_image_paths = response.download(arguments)

            self.loadingLbl.destroy()
            folder = dir_path + "\downloads\\" + nameOfObject
            self.deleteBrokenFiles(folder)
            messagebox.showinfo("Good News", "Done Downloading!\nIt is recommended to check the pictures.")
        else:
            if nameOfObject == "":
                messagebox.showerror("ERROR", "Enter the name of the object")
            if numLimit == "":
               messagebox.showerror("ERROR", "Enter the number images")
class GetDataFromVideo:
    save = False
    def __init__(self, master):
        titleFontSize = 20
        subtitleFontSize = 16
        answerFontSize = 12
        self.master = master
        self.frame = tk.Frame(self.master)
        self.titleMessage = tk.Label(self.master, text="Generate images from video")
        self.titleMessage.config(font=("Helvetica", titleFontSize))
        self.titleMessage.pack()
        self.pathlbl = tk.Label(self.master, text="Directory of video")
        self.pathlbl.pack()
        self.pathtxt = tk.Text(self.master,width = 20, height = 1)
        self.pathtxt.pack()
        self.askSaveDir = tk.Checkbutton(self.master, text = "Save in Different Location?", command = self.check)
        self.askSaveDir.pack()
        self.savePathlbl = tk.Label(self.master, text="Save Directory of images")
        self.savePathlbl.pack()
        self.savePathtxt = tk.Text(self.master,width = 20, height = 1)
        self.savePathtxt.pack()
        self.genBtn = tk.Button(self.master, text="Generate", command = self.splitVideo)
        self.genBtn.pack()
    def check(self):
        if self.save == True:
            self.save = False
        else:
            self.save = True
    def splitVideo(self):
        dir_path = os.path.dirname(os.path.realpath(__file__))

        path = self.pathtxt.get("1.0", END).replace('\n', '')

        if path == "":
            messagebox.showerror("ERROR", "Enter in the directory of the video.")
            return 0
        if not os.path.exists(path):
            messagebox.showerror("ERROR", "Video Path does not exist")
            return 0
        #checks if folder exists and if it doesnt, then makes one
        if self.save == True:
            if self.savePathtxt.get("1.0", END).replace('\n', '') == "":
                messagebox.showerror("ERROR", "Enter in another save directory or save in the default folder")
                return 0
            else:
                savePath = self.savePathtxt.get("1.0", END).replace('\n', '')
        else:
            try:
                nameOfFile = path
                index = nameOfFile.rfind("\\")

                nameOfFile = nameOfFile[index+1:-4]
                print(nameOfFile)
                if not os.path.exists(dir_path + '\downloads\\' + nameOfFile):
                    os.makedirs(dir_path+ '\downloads\\' + nameOfFile)
                    print("Made folder for image")
                savePath = dir_path + "\downloads\\" + nameOfFile
            except OSError:
                print('Error: Creating directory of data')

        videoFile = path
        imagesFolder = savePath
        currentFrame = 0

        cap = cv2.VideoCapture(path)
        while (True):
            # Capture frame-by-frame
            ret, frame = cap.read()
            if not ret:
                break
            # Saves image of the current frame in jpg file
            name = imagesFolder +"\\" + nameOfFile + "_"+str(currentFrame) + '.jpg'
            print('Creating...' + name)
            cv2.imwrite(name, frame)

            # To stop duplicate images
            currentFrame += 1

        # When everything done, release the capture
        cap.release()
        cv2.destroyAllWindows()
        messagebox.showinfo("Good News", "Done Saving images! Images saved in: \n"+imagesFolder)
class analyzeVideoPage:
    def __init__(self, master):
        titleFontSize = 20
        subtitleFontSize = 16
        answerFontSize = 12
        self.master = master
        self.frame = tk.Frame(self.master)
        self.titleMessage = tk.Label(self.master, text="Analyze Video")
        self.titleMessage.config(font=("Helvetica", titleFontSize))
        self.titleMessage.pack()
        self.nameOfObjTraininglbl = tk.Label(self.master, text="Name of model")
        self.nameOfObjTraininglbl.pack()
        self.nameOfObjTrainingtxt = tk.Text(self.master,width = 20, height = 1)
        self.nameOfObjTrainingtxt.pack()
        self.pathlbl = tk.Label(self.master, text="Directory of video")
        self.pathlbl.pack()
        self.pathtxt = tk.Text(self.master,width = 20, height = 1)
        self.pathtxt.pack()
        self.genBtn = tk.Button(self.master, text="Generate", command = self.analyze)
        self.genBtn.pack()
    def analyze(self):

        foundPath = True
        nameOfObject = self.nameOfObjTrainingtxt.get("1.0",END).replace('\n','')
        videoPath = self.pathtxt.get("1.0",END).replace('\n','')
        dir_path = os.path.dirname(os.path.realpath(__file__))

        savelink = dir_path + r"\saved_Weights\model_structure_" + nameOfObject + ".json"
        saveLink2 = dir_path + r"\saved_Weights\model_weights_" + nameOfObject + ".h5"

        print(dir_path)

        class_labels = ["Not Object",nameOfObject]
        #Load the json file that contains the model's structure
        try:
            foundPath = True
            f = Path(savelink)
            model_structure = f.read_text()
        except IOError:
            foundPath = False
            messagebox.showerror("ERROR", "Could not find weights for: "+nameOfObject)

        if foundPath:
            f = Path(savelink)
            model_structure = f.read_text()
            # Recreate the Keras model object from the json data
            model = model_from_json(model_structure)

            # Re-load the model's trained weights
            model.load_weights(saveLink2)

            # -----------------------------------------------------------------------------------Resizes image
            img_rows = 200  # what we converted all the training data to (200x200 image)
            img_cols = 200

            #---------------------------------------------------------SAVING VIDEO INFO
            FILE_OUTPUT = nameOfObject + '.avi'

            if os.path.isfile(FILE_OUTPUT): #deletes old video with thw same name
                os.remove(FILE_OUTPUT)

            video = cv2.VideoCapture(videoPath)

            fourcc = cv2.VideoWriter_fourcc(*'MJPG')
            width = video.get(cv2.CAP_PROP_FRAME_WIDTH)   # float
            height = video.get(cv2.CAP_PROP_FRAME_HEIGHT)
            fps = video.get(cv2.CAP_PROP_FPS)

            out = cv2.VideoWriter(FILE_OUTPUT, fourcc, 20.0, (int(width), int(height)))

            while True:
                check, frame = video.read()
                cv2.imwrite('currentFrame.jpg', frame)

                if check:
                    try:
                        im = Image.open("currentFrame.jpg")  # image name
                    except IOError:
                        print("Error")
                        messagebox.showerror("ERROR", "An error ocurred!")
                else:
                    messagebox.showinfo("Good News", "Saved Analyzed Video!")
                    return 0
                img = im.resize((img_rows, img_cols))
                image_to_test = image.img_to_array(img)
                list_of_images = np.expand_dims(image_to_test, axis=0)
                results = model.predict(list_of_images)
                single_result = results[0]
                most_likely_class_index = int(np.argmax(single_result))
                class_likelihood = single_result[most_likely_class_index]
                class_label = class_labels[most_likely_class_index]

                # Print the result
                print("This is image is a {} - Likelihood: {:2f}".format(class_label, class_likelihood))
                key = cv2.waitKey(10)
                if key == ord('q'):
                    break

                font = cv2.FONT_HERSHEY_SIMPLEX
                bottomLeftCornerOfText = (80, 50)
                fontScale = 0.5
                fontColor = (255, 255, 255)
                lineType = 2

                cv2.putText(frame,str(class_label) + "|" + str(class_likelihood),
                            bottomLeftCornerOfText,
                            font,
                            fontScale,
                            fontColor,
                            lineType)
                # Edit image in here
                if most_likely_class_index == 0:
                    cv2.circle(frame, (50, 50), 20, (0, 0, 255), -1) #not object indicator
                else:
                    cv2.circle(frame, (50, 50), 20, (0, 255, 0), -1) #object indicator

                # Saving image in here  # saves video
                out.write(frame)
                #-----------------------
                cv2.imshow("Analyzing", frame)
            out.release()
            messagebox.showinfo("Good News", "Saved Analyzed Video!")
            video.release()
            cv2.destroyAllWindows()
class analyzeImagePage:
    def __init__(self, master):
        titleFontSize = 20
        subtitleFontSize = 16
        answerFontSize = 12
        self.master = master
        self.frame = tk.Frame(self.master)
        self.titleMessage = tk.Label(self.master, text="Analyze Image")
        self.titleMessage.config(font=("Helvetica", titleFontSize))
        self.titleMessage.pack()
        self.nameOfObjTraininglbl = tk.Label(self.master, text="Name of model")
        self.nameOfObjTraininglbl.pack()
        self.nameOfObjTrainingtxt = tk.Text(self.master,width = 20, height = 1)
        self.nameOfObjTrainingtxt.pack()
        self.pathlbl = tk.Label(self.master, text="Directory of image")
        self.pathlbl.pack()
        self.pathtxt = tk.Text(self.master,width = 20, height = 1)
        self.pathtxt.pack()
        self.genBtn = tk.Button(self.master, text="Analyze", command = self.analyze)
        self.genBtn.pack()

    def analyze(self):
        foundPath = True
        nameOfObject = self.nameOfObjTrainingtxt.get("1.0",END).replace('\n','')
        imgPath = self.pathtxt.get("1.0",END).replace('\n','')
        dir_path = os.path.dirname(os.path.realpath(__file__))

        savelink = dir_path + r"\saved_Weights\model_structure_" + nameOfObject + ".json"
        saveLink2 = dir_path + r"\saved_Weights\model_weights_" + nameOfObject + ".h5"

        print(dir_path)

        class_labels = ["Not Object",nameOfObject]
        #Load the json file that contains the model's structure
        try:
            foundPath = True
            f = Path(savelink)
            model_structure = f.read_text()
        except IOError:
            foundPath = False
            messagebox.showerror("ERROR", "Could not find weights for: "+nameOfObject)

        if foundPath:
            f = Path(savelink)
            model_structure = f.read_text()
            # Recreate the Keras model object from the json data
            model = model_from_json(model_structure)

            # Re-load the model's trained weights
            model.load_weights(saveLink2)

            # -----------------------------------------------------------------------------------Resizes image
            img_rows = 200  # what we converted all the training data to (200x200 image)
            img_cols = 200

            #---------------------------------------------------------SAVING Image INFO
            try:
                im = Image.open(imgPath)  # image name
            except IOError:
                print("Error")
                messagebox.showerror("ERROR", "Image not found!")

            img = im.resize((img_rows, img_cols))
            image_to_test = image.img_to_array(img)
            list_of_images = np.expand_dims(image_to_test, axis=0)
            results = model.predict(list_of_images)
            single_result = results[0]
            most_likely_class_index = int(np.argmax(single_result))
            class_likelihood = single_result[most_likely_class_index]
            class_label = class_labels[most_likely_class_index]

            # Print the result
            print("This is image is a {} - Likelihood: {:2f}".format(class_label, class_likelihood))

            font = cv2.FONT_HERSHEY_SIMPLEX
            bottomLeftCornerOfText = (80, 50)
            fontScale = 0.5
            fontColor = (255, 255, 255)
            lineType = 2

            cv2.putText(img,str(class_label) + "|" + str(class_likelihood),
                        bottomLeftCornerOfText,
                        font,
                        fontScale,
                        fontColor,
                        lineType)
            # Edit image in here
            if most_likely_class_index == 0:
                cv2.circle(img, (50, 50), 20, (0, 0, 255), -1) #not object indicator
            else:
                cv2.circle(img, (50, 50), 20, (0, 255, 0), -1) #object indicator

            # Saving image in here  # saves video
            Image.write(img)
            messagebox.showinfo("Good News", "Saved Analyzed Picture!")
def main():
    root = tk.Tk()
    root.minsize(300,300)
    root.title("Easy CNN 1.0")
    dir_path = os.path.dirname(os.path.realpath(__file__))
    root.iconbitmap(dir_path + "\data\pictures\icon.ico")
    root.resizable(False, False)
    center(root)
    app = MainPage(root)
    root.mainloop()
def center(toplevel): #centers the main window
    toplevel.update_idletasks()

    # PyQt way to find the screen resolution
    screen_width = toplevel.winfo_screenwidth()
    screen_height = toplevel.winfo_screenheight()

    x = screen_width/2 - toplevel.winfo_width()/2
    y = screen_height/2 - toplevel.winfo_height()/2
    toplevel.geometry("+%d+%d" % (x, y))

if __name__ == '__main__':
    main()


from flask import Flask, request, jsonify
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import psycopg2
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
#engine = create_engine("sqlite:///infraction.db")
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///infraction.db'
db = SQLAlchemy(app)

#create model

class Infraction( db.Model):
    __tablename__ = 'infraction'
    id = db.Column(Integer, primary_key=True)
    types = db.Column(String(100))
    code = db.Column(Integer)

#db.create_all()
print ("Table infraction cree ...")

# definition des endpoints
@app.route("/api/infraction", methods=["POST"])
def create():

    try:

        new_infraction = Infraction(
            types = request.json['types'],
            code = request.json['code'])

        db.session.add(new_infraction)
        db.session.commit()
        return 'ok'
    except Exception as e:
        print(f" erreur '{e}' ")
        return {"error": "erreur ."}, 500

@app.route("/api/infraction", methods=['GET'])
def get():
    try:

        infraction = db.session.query(Infraction).all()
        if infraction:
            result =[]
            for inf in infraction:
                result.append({"id": inf.id, "types": inf.types, "code":inf.code})
            return jsonify(result)
        else:
            return jsonify({"error": f"infraction non trouvé"}), 404
    except Exception as e:
        print(f"erreur '{e}'")
        return {"une erreur est survenue"}, 500

if __name__ == '__main__':
    app.run(debug=True, host="0.0.0.0", port=8001)


"""Checks the validity of shortest subway path."""
import pickle
from subway_analysis import transfer_time

subway_data = {}

for filename in ('subway.txt', 'merge.txt', 'oneway.txt'):
    with open(f"./subway_data/{filename}.connections", 'rb') as f:
        connections = pickle.load(f)
    with open(f"./subway_data/{filename}.name_to_ids", 'rb') as f:
        name_to_ids = pickle.load(f)
    with open(f"./subway_data/{filename}.analysed", 'rb') as f:
        answer_map = pickle.load(f)

    subway_data[filename] = [connections, name_to_ids, answer_map]

def check(arg, inp, out):
    arg = arg.split('/')[-1]
    connections, name_to_ids, answer_map = subway_data[arg]
    inp = inp.split('\n')
    out = out.split('\n')
    while inp[-1] != 'QUIT':
        inp.pop()
    inp.pop()

    try:
        for i, line in enumerate(inp):
            start, end = line.split()
            shortest_weight = answer_map[start][end]
            path = out[i * 2]
            weight = int(out[i * 2 + 1])

            if weight != shortest_weight or path_weight(path, name_to_ids, connections) != weight:
                return False
        return True
    except Exception as e:
        return False

def path_weight(path, name_to_ids, connections):
    path = path.split()
    total_weight = 0
    for i, name in enumerate(path):
        if name[0] == '[' and name[-1] == ']':
            total_weight += transfer_time
            path[i] = name[1:-1]

    for i in range(1, len(path)):
        total_weight += edge_weight(path[i - 1], path[i], name_to_ids, connections)

    return total_weight

def edge_weight(name1, name2, name_to_ids, connections):
    ids1, ids2 = name_to_ids[name1], name_to_ids[name2]
    best_weight = float('inf')
    for id1 in ids1:
        for id2 in ids2:
            for connection in connections[id1]:
                if connection[0] == id2:
                    best_weight = min(best_weight, connection[1])
    return best_weight


import risar
from random import *
from math import *

def zoga():
    barva = risar.nakljucna_barva()
    x, y = risar.nakljucne_koordinate()
    krog = risar.krog(x, y, 10, barva)

    vx = randint(-5, 5)
    vy = sqrt(25 - vx ** 2)
    return vx, vy, krog

'''for j in range(30):'''
vx, vy, krog = zoga()

for i in range(800):
    krog.setPos(krog.x() + vx, krog.y() + vy)
    if not (10 < krog.x() < risar.maxX - 10):
        vx = -vx
    if not (10 < krog.y() < risar.maxY - 10):
        vy = -vy
    risar.cakaj(0.02)


from __future__ import absolute_import

import logging
import gzip

from ngsvtools.cache import Cache
from ngsvtools.sam.data.chromosome import Chromosome
from ngsvtools.sam.data.cytoband import CytoBand
from ngsvtools.exception import AlreadyLoadedError

def _update_database(fp, cytoband_data, chromosome_data):

    count = 0

    # load cytoband data
    for line in fp:
        cyto_chr, cyto_start, cyto_end, cyto_name, cyto_gie = line[:-1].split('\t')

        c_name = cyto_chr
        c_name = c_name.replace('Chr', '')
        c_name = c_name.replace('chr', '')
        c_name = c_name.replace('.', '')

        c = chromosome_data.get_by_name(c_name)

        if c is None:
            chromosome_data.append(c_name)
            c = chromosome_data.get_by_name(c_name)

        cytoband_data.append(c['id'],
                             long(cyto_start),
                             long(cyto_end),
                             cyto_name,
                             cyto_gie)

        count += 1

        logging.debug("Loaded %d bands\r" % count)

    logging.debug("Loaded %d bands" % count)

def load(db):
    cytoband_data = CytoBand(db)
    chromosome_data = Chromosome(db)

    if cytoband_data.count() > 0:
        raise AlreadyLoadedError('CytoBand is already loaded')

    url = "http://hgdownload.cse.ucsc.edu/goldenPath/hg19/database/cytoBand.txt.gz"

    logging.info("Begin to download from %s" % url)

    c = Cache(url)
    c.load()

    logging.info("updating database")

    f = gzip.open(c.name)

    _update_database(f, cytoband_data, chromosome_data)

    f.close()


from __future__ import print_function
from __future__ import absolute_import
#author: Bryan Bishop <kanzure@gmail.com>
#date: 2012-01-10
#show me an image
import Image
from math import floor
from . import extract_maps
from . import analyze_incbins

print("loading rom..")
extract_maps.load_rom()
#extract_maps.load_map_pointers()
#extract_maps.read_all_map_headers()

print("analyzing incbins..")
analyze_incbins.load_asm()
analyze_incbins.isolate_incbins()
analyze_incbins.process_incbins()

width = 1024
height = 1024

im = Image.new("P", (width, height), 0)

im.putpalette([
    0, 0, 0,
    126, 30, 156,
])

print("drawing incbins...")
for incbin_key in analyze_incbins.processed_incbins:
    incbin = analyze_incbins.processed_incbins[incbin_key]
    start = incbin["start"]
    end = incbin["end"]

    for pos in range(start, end+1):
        widthx = int(pos % width)
        heighty = int(floor(pos / height))
        im.putpixel((widthx, heighty), 1)

im.save("test.png")


from PIL import Image
from pylab import *
import scipy.stats as st
import numpy as np

file_path = 'resources/'

# Transform RGB image to gray_scale image
def img_transform(img_name):
    image = Image.open(file_path + img_name).convert('L')  # Открываем изображение и конвертируем в полутоновое
    image.save(file_path + "gray_scale_" + img_name)
    image.show()
    return image

# Image histogram and statistic
def img_histogram(image):
    img_array = array(image)
    hist_data = img_array.flatten()

    figure()
    hist(hist_data, 32)
    show()

    stat = [np.mean(hist_data), np.std(hist_data, ddof=1), st.mode(hist_data)[0][0], np.median(hist_data)]
    return hist_data, stat

# Hypothesis check
def hyp_check(data_set):
    chi2, p = st.chisquare(data_set)
    message = "Test Statistic: {}\np-value: {}"
    print(message.format(chi2, p))

def main():
    first_image = img_transform("first_image.jpg")
    second_image = img_transform("second_image.jpg")

    first_hist, first_hist_stat = img_histogram(first_image)
    second_hist, second_hist_stat = img_histogram(second_image)
    print('|Average|   |rms|   |mode|   |median|')
    print("First image: ", first_hist_stat)
    print("Second image: ", second_hist_stat)

    hist_correlation_coefficient = np.corrcoef(np.asarray(first_hist).flatten(),
                                               np.asarray(second_hist).flatten())[1, 0]
    print("Histogram correlation: ", hist_correlation_coefficient)

    image_correlation = np.corrcoef(np.asarray(first_image).flatten(), np.asarray(second_image).flatten())[1, 0]
    print('Images correlation', image_correlation)

    print('\nHypothesis:\n')
    first = [0.92, 1.4, 13.3, 16.9, 0.98, 1.1]
    second = [2.5, 6.5, 9.6, 7.9, 5, 3]
    hyp_check(first)
    hyp_check(second)

if __name__ == "__main__":
    main()


# D2.py - All That Talking
#
# The Problem:
#   Develop and test a Python program that determines how much time it would
#   take to download all instances of every word ever spoken. Assume the size
#   of this information as given in Figure 2-1. The download speed is to be
#   entered by the user in million of bits per second (Mbps). To find your
#   actual connection speed, go to the following website (from Intel Corporat-
#   ion) or similar site,
#       <www.intel.com/content/www/us/en/gamers/broadband-speed-test.html>.
#   Because connection speeds can vary, run this connection speed three times.
#   Take the average of three results, and use that as the connection speed to
#   enter into your program. Finally, determine what is an appropriate unit of
#   time to express your program results in minutes? hours? days? other?
#
# Analysis:
#   Acording to Figure 2-1 that all the words ever spoken is about 5 exabytes.
#   We can convert the exabytes to megabits since the connection speed is
#   usually measured in Mbps (megabits per second). In order to do that, we can
#   take 2^60 divide by 1024^2 since there are 2^60 bytes in an exabyte and by
#   dividing it by 1024^2, we will get the amount of megabytes there are. How-
#   ever, we would have to times that number by 8 because it is measued in
#   "megabit" not "megabyte" -- the amount of megabits we have is 1099511627776.
#   Finally, we can take that number and divide it by the connection speed (in
#   Mbps) to get the amount of time it would take in seconds.
#
# Implementation:
#   Ask the user if they want to test their current download speed. If yes, run
#   the speed testing function. Otherwise use the default connection speed.
#
# Date:    09/05/2016
# Author:  Chiayo Lin
# License: GPL 3.0

import urllib.request, time

# return the current average "download" Mbps
def speed_test():
    print("Testing the current connection speed...")
    print("Using the server from Softlayer, Singapore...")
    file_size = 100 * (2**20) # size of the test file in bytes
    start_time = time.time()
    urllib.request.urlretrieve(
        "http://speedtest.sea01.softlayer.com/downloads/test100.zip", \
            "/dev/null")

    # devide by 1024^2 and multiply by 8 to convert bytes to megabits
    speed = (int(file_size / (time.time() - start_time)) / (2**20)) * 8
    print("The current connection speed is about {:.2f} Mbps.".
        format(speed))

    return speed

# main
def main():
    print("How long it would take you to download 5EB of data?")
    # ask the user if he wants to test his current connection speed
    connection_mbps = speed_test() if \
        input("Do you want to test your current speed? (y/N) ") \
            .strip().lower()[0] is 'y' else \
                int(input("Enter your speed (Mbps): "))

    target_size_b = 5 * (2**60) * 8
    seconds_in_a_year = 60 * 60 * 24 * 365
    total_time = (target_size_b / (connection_mbps * (2**20))) / seconds_in_a_year

    print("At the speed of", format(connection_mbps, '.2f'),
        "Mbps, it will take about", format(total_time, '.2f'),
            "years to download.")

if __name__ == "__main__":
    main()


# MLS Player Data Flask Site by Derrick Priebe
# Leveraging Player URL Microservice by Hyunjae Kim
# Oregon State University, CS361 Software Engineering I
# Date: 8/7/2022

from flask import Flask, render_template, redirect, request, url_for
import pandas as pd
import webbrowser
import zmq

app = Flask(__name__)

# Data setup
table = pd.read_csv("salaries.csv")
table.fillna('', inplace=True)
headings = list(table.columns)
data = list(table.values)

# Zeromq setup and socket connection
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")

# Route for homepage
@app.route("/")
def home():
    return render_template("home.html", headings=headings, data=data)

# Route to get additional player info via "info" link
@app.route('/GetPlayerInfo/<int:rowindex>')
def get_player_info(rowindex):
    # Send and receive message request
    socket.send_string(str(rowindex))
    message = socket.recv_string()
    # Access url from message
    webbrowser.open(message)
    return redirect(url_for("home"))

# Function to get inputs from form
def getInputs(form):
    search = form.get("search")
    first_name = form.get("first_name")
    last_name = form.get("last_name")
    team = form.get("team")
    return search, first_name, last_name, team

# Function to get search criteria for an element
def getSearch(search_inputs, element, filterdata):
    full_search = search_inputs[0] == "" or search_inputs[0].lower() in element[0].lower() or search_inputs[0].lower() in element[1].lower() or search_inputs[0].lower() in element[2].lower()
    first_name_search = search_inputs[1] == "" or search_inputs[1].lower() in element[0].lower()
    last_name_search = search_inputs[2] == "" or search_inputs[2].lower() in element[1].lower()
    team_search = search_inputs[3] == "" or search_inputs[3].lower() in element[2].lower()
    if (full_search and first_name_search and last_name_search and team_search):
            filterdata.append(element)
    return filterdata

# Route to post search results
@app.route("/", methods=["POST"])
def search():
    search_inputs = getInputs(request.form)
    filterdata = []
    for element in data:
        filterdata = getSearch(search_inputs, element, filterdata)
    if filterdata == []:
        no_data = ["No  "]
        return render_template("home.html", headings=no_data, search= search_inputs[0],first_name=search_inputs[1], last_name=search_inputs[2], team=search_inputs[3])
    else:
        return render_template("home.html", headings=headings, data=filterdata, search=search_inputs[0], first_name=search_inputs[1], last_name=search_inputs[2], team=search_inputs[3])

# Listener
if __name__ == "__main__":
    app.run(port=2509, debug=True)


import os
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, Flatten, Dense
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
import numpy as np
import matplotlib.pyplot as plt

imdb_dir = 'D:\\aclImdb'
train_dir = os.path.join(imdb_dir, 'train')

labels = []
texts = []

for label_type in ['neg', 'pos']:
    dir_name = os.path.join(train_dir, label_type)
    for fname in os.listdir(dir_name):
        if fname[-4:] == '.txt':
            f = open(os.path.join(dir_name, fname), encoding='utf-8')
            texts.append(f.read())
            f.close()
            if label_type == 'neg':
                labels.append(0)
            else:
                labels.append(1)

maxlen = 100
training_samples = 200
validation_samples = 10000
max_words = 10000

tokenizer = Tokenizer(num_words=max_words)
tokenizer.fit_on_texts(texts)
sequences = tokenizer.texts_to_sequences(texts)

word_index = tokenizer.word_index

print('Found %s unique tokens.' % len(word_index))

data = pad_sequences(sequences, maxlen=maxlen)

labels = np.asarray(labels)

print('Shape of data tensor:', data.shape)
print('Shape of label tensor:', labels.shape)

indices = np.arange(data.shape[0])
np.random.shuffle(indices)
data = data[indices]
labels = labels[indices]

x_train = data[:training_samples]
y_train = labels[:training_samples]
x_val = data[training_samples: training_samples + validation_samples]
y_val = labels[training_samples: training_samples + validation_samples]

# 模型定义
embedding_dim = 100

model = Sequential()
model.add(Embedding(max_words, embedding_dim, input_length=maxlen))
model.add(Flatten())
model.add(Dense(32, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.summary()

# 训练与评估模型

model.compile(
    optimizer='rmsprop',
    loss='binary_crossentropy',
    metrics=['accuracy']
)
history = model.fit(
    x_train,
    y_train,
    epochs=10,
    batch_size=32,
    validation_data=(x_val, y_val)
)

model.save_weights('pre_trained_glove_model.h5')

# 绘制结果

accuracy = history.history['accuracy']
val_accuracy = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(1, len(accuracy) + 1)

plt.plot(epochs, accuracy, 'bo', label='Training accuracy')
plt.plot(epochs, val_accuracy, 'b', label='Validation accurcy')
plt.title('Training and validation accuracy')
plt.legend()

plt.figure()

plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()

plt.show()


#!/usr/bin/env python3

import json
import os
import sys

from kafka import KafkaProducer

def copy_to_topic(data):
    # place the data into the topic provided in environment variable
    try:
        kafka_topic = os.getenv('KAFKA_TOPIC', 'dl_stream_topic')
        kafka_url = os.getenv('KAFKA_URL', '127.0.0.1:9092')
        producer = KafkaProducer(bootstrap_servers=[kafka_url],
                         value_serializer=str.encode)
        producer.send(kafka_topic, value=data)
        producer.flush()

    except Exception as e:
        print("Exception: ", str(e))

def main():
    # extract paramters from argv[1]
    if len(sys.argv) > 1:
        args = sys.argv[1]
    else:
        args = '{}'
    copy_to_topic(args)
    # output the original input to next item in the pipeline
    print(args)

if __name__ == '__main__':
    main()


# Based on: https://www.linkedin.com/learning/programming-foundations-algorithms

def sequential_search(item, unordered_array):
    # Linear time complexity: O(n)
    for i in range(0, len(unordered_array)):
        if item == unordered_array[i]:
            return i
    return None

def binary_search(item, sorted_array):
    # Logarithmic time complexity: O(log n)
    lower_idx = 0
    upper_idx = len(sorted_array) - 1
    while lower_idx <= upper_idx:
        midpoint = (upper_idx + lower_idx) // 2
        if sorted_array[midpoint] == item:
            return midpoint
        elif item > sorted_array[midpoint]:
            lower_idx = midpoint + 1
        else:
            upper_idx = midpoint - 1
    if lower_idx > upper_idx:
        return None

def is_sorted(array):
    # Linear time complexity: O(n)
    # General solution:
    # for i in range(0, len(array)-1):
    #     if array[i] > array[i+1]:
    #         return False
    # return True
    return all([array[i] <= array[i+1] for i in range(0, len(array)-1)])  # Pythonic solution using list comprehension

def main():
    unordered_array = (6, 20, 8, 19, 56, 23, 87, 41, 49, 53)
    sorted_array = (6, 8, 19, 20, 23, 41, 49, 53, 56, 87)

    print(sequential_search(19, unordered_array))
    print(sequential_search(87, unordered_array))
    print(sequential_search(0, unordered_array))
    print()
    print(binary_search(19, sorted_array))
    print(binary_search(87, sorted_array))
    print(binary_search(0, sorted_array))
    print()
    print(is_sorted(unordered_array))
    print(is_sorted(sorted_array))

if __name__ == '__main__':
    main()


from operator import add, mul
functions = {1:add, 2:mul}

# Part 1

program = []

with open('02-input', 'r') as raw:
    intcode = [int(x) for x in raw.readline().strip().split(',')]

def part1():
    intcode[1] = 12
    intcode[2] = 2
    return compute(intcode)

def compute(program):
    """Executes the intcode program
    >>> compute([1,0,0,0,99])
    [2,0,0,0,99]
    >>> compute([2,3,0,3,99])
    [2,3,0,6,99]
    >>> compute([2,4,4,5,99,0])
    [2,4,4,5,99,9801]
    >>> compute([1,1,1,4,99,5,6,0,99])
    [30,1,1,4,2,5,6,0,99]
    """
    while True:
        for i in range(0, len(program), 4):
            if program[i] == 99:
                return program[0]
            elif program[i] in functions.keys():
                function = functions[program[i]]
                o1, o2 = program[program[i+1]], program[program[i+2]]
                output = program[i+3]
                program[output] = function(o1, o2)
            else:
                raise ValueError("Unknown operator provided")

# Part 2

def bruteforce():
    for x in range(100):
        for y in range(100):
            program = intcode.copy()
            program[1] = x
            program[2] = y
            result = compute(program)
            if result == 19690720:
                return 100 * x + y


import os
from csv import reader
#import experiment

NUMBER_OF_SAMPLES = 164

def calculate_correctness(path):
    # Read the csv file
    matrix = []
    with open(path + "/results/results.csv", "r") as f:
        csv_reader = reader(f)
        for row in csv_reader:
            matrix.append(row)
    # Calculate the correctess
    correctness = 0
    count = 0
    for i in range(1, NUMBER_OF_SAMPLES + 1):
        if  "N/A"  not in  matrix[i][1]:
            count += 1
            correctness += float(matrix[i][1])
    return float(correctness / NUMBER_OF_SAMPLES)

def calculate_different_correctness_cases(path):
    # Read the csv file
    matrix = []
    with open(path + "/results/results.csv", "r") as f:
        csv_reader = reader(f)
        for row in csv_reader:
            matrix.append(row)

    count_correct = 0
    count_incorrect = 0
    count_partially_correct = 0

    for i in range(1, NUMBER_OF_SAMPLES + 1):
        if 'N/A' in matrix[i][1]:
            count_incorrect += 1
        else:
            if float(matrix[i][1]) == 1.0:
                count_correct += 1
            elif float(matrix[i][1]) == 0.0:
                count_incorrect += 1
            else:
                count_partially_correct += 1

    results = [count_correct, count_incorrect, count_partially_correct]

    return results

def calculate_correctness_by_percentage(path):
    # Read the csv file
    matrix = []
    with open(path + "/results/results.csv", "r") as f:
        csv_reader = reader(f)
        for row in csv_reader:
            matrix.append(row)

    # Calculate the correctness
    count_75_100 = 0
    count_50_75 = 0
    count_25_50 = 0
    count_0_25 = 0
    for i in range(1, NUMBER_OF_SAMPLES + 1):
        if not 'N/A' in matrix[i][1]:
            if 1 > float(matrix[i][1]) > 0.75:
                count_75_100 += 1
            elif 0.75 >= float(matrix[i][1]) > 0.5:
                count_50_75 += 1
            elif 0.5 >= float(matrix[i][1]) > 0.25:
                count_25_50 += 1
            elif 0.25 >= float(matrix[i][1]) > 0:
                count_0_25 += 1
    return [count_75_100, count_50_75, count_25_50, count_0_25]

def return_percentages_correctness(path):
    scores = calculate_correctness_by_percentage(path)
    scale_ratings = calculate_correctness_with_test_cases(path)

    percentages = []
    percentages_scale_ratings = []

    for i in range(len(scores)):
        percentages.append(float(scores[i] / sum(scores)))

    for i in range(len(scale_ratings)):
        percentages_scale_ratings.append(
            float(scale_ratings[i] / sum(scale_ratings)))

    return percentages, percentages_scale_ratings

def read_all_exec_results(path):
    results = []
    for i in range(0, NUMBER_OF_SAMPLES):
        with open(path + "/" + str(i) + "/output_correctness_validity.txt", "r") as f:
            content = f.read().splitlines()
            results.append(content[0])
    return results

def count_invalid(path):
    # Read the csv file
    matrix = []
    with open(path + "/results/results.csv", "r") as f:
        csv_reader = reader(f)
        for row in csv_reader:
            matrix.append(row)
    count = 0
    for i in range(1, NUMBER_OF_SAMPLES + 1):
        if '0' in matrix[i][2]:
            count += 1
    return count

def calculate_validity(path):
    count = count_invalid(path)
    return float((NUMBER_OF_SAMPLES - count) / NUMBER_OF_SAMPLES)

print()
print("Average Correctness with Invalid Generations:", calculate_correctness(os.getcwd()))
print()
different_correctness_cases = calculate_different_correctness_cases(os.getcwd())
print("Correct:", str(different_correctness_cases[0]), "Incorrect:", str(different_correctness_cases[1]), "Partially Correct:", str(different_correctness_cases[2]))
print()
correctness_by_percentage = calculate_correctness_by_percentage(os.getcwd())
print("100 > N > 75:", correctness_by_percentage[0], "\n75 >= N > 50:", correctness_by_percentage[1], "\n50 >= N > 25:", correctness_by_percentage[2], "\n25 >= N > 0:", correctness_by_percentage[3])
print()
print("Validity percentage:", calculate_validity(os.getcwd()))
print("Number of Invalid Generations:", count_invalid(os.getcwd()))


# def eur_to_czk(pocet_eur, kurz=25):
def eur_to_czk(pocet_eur: int, kurz: int=25) -> int:
    """
    Funkce na prevod eur do korun.
    Bere dva parametry - pocet eur a kurz.
    Vrati kolik stoji eura v korunach.
    """
    pocet_czk = pocet_eur * kurz
    return pocet_czk

eura_uzivatel = input("Kolik si prejete smenit euro?")
print(eur_to_czk(eura_uzivatel))

"""to typovani nic neudela, je to napoveda... ale existuji tooly, ktere se daji pouzit a ktere podle tech napoved umi najít bugy"""


# dependencies
import tkinter as tk
# dictionary operations
def validateDict(config={}, defaults={}):# dict
    """Validate a dictionary by given defaults. Params must be dict."""
    validated = {}

    for each in defaults:
        try:
            validated[each] = config[each]
        except KeyError:
            validated[each] = defaults[each]

    return validated
def convertColor(value):
    """Convert the value into tk's color value and return it."""
    if value is None:
        color = None
    elif type(value) is tuple:
        color = "#%02x%02x%02x" % value
    else:
        color = value

    return color


import torch.nn as nn
import torch.nn.functional as F
import torch

from torch.nn import Linear
from torch_geometric.nn import GCN2Conv, AGNNConv, FiLMConv
from torch_geometric.nn import SuperGATConv, APPNP
from torch_geometric.nn.conv.gcn_conv import gcn_norm
from torch.nn import Sequential, Linear, BatchNorm1d, ReLU
from torch_geometric.nn import GINConv, global_add_pool
from tqdm import tqdm

def convert(adj_t):
    row, col, edge_attr = adj_t.t().coo()
    edge_index = torch.stack([row, col], dim=0)
    return edge_index, edge_attr

class GCN2Net(torch.nn.Module):
    def __init__(self, in_channels, hidden_channels, out_channels, num_layers, alpha=0.1, theta=0.5, shared_weights=True, dropout=0.0, batchnorm=False):
        super(GCN2Net, self).__init__()

        self.lins = torch.nn.ModuleList()
        self.lins.append(Linear(in_channels, hidden_channels))
        self.lins.append(Linear(hidden_channels, out_channels))
        self.num_layers = num_layers

        self.convs = torch.nn.ModuleList()
        for layer in range(num_layers):
            self.convs.append(GCN2Conv(hidden_channels, alpha, theta, layer + 1, shared_weights, normalize=False))

        self.dropout = dropout

    def forward(self, x, adj_t):
        # print(x.shape)
        # x = x.float()
        x = F.dropout(x, p=0.5, training=self.training)
        x = x_0 = self.lins[0](x).relu()
        # print(x.dtype, x_0.dtype)
        # x = F.dropout(x, self.dropout, training=self.training)
        for i, (adj, _, size) in enumerate(adj_t):
            edge_index, edge_attr = convert(adj)
            x = F.dropout(x, p=0.5, training=self.training)
            # x = x_0 = self.lins[0](x).relu()
            x = self.convs[i](x, x_0, edge_index)
            x = F.relu(x)
            # print(x, x_0)

        x = self.lins[1](x)
        return x.log_softmax(dim=-1)

    def reset_parameters(self):
        for lin in self.lins:
            lin.reset_parameters()
        for conv in self.convs:
            conv.reset_parameters()

    def inference_all(self, data):
        x, adj_t = data.x, data.adj_t
        x = x_0 = self.lins[0](x).relu()
        for i, conv in enumerate(self.convs):
            x = conv(x, x_0, adj_t)
            x = F.relu(x)
            x = F.dropout(x, p=self.dropout, training=self.training)
        # x = self.convs[-1](x, adj_t)
        x = self.lins[1](x)
        return x.log_softmax(dim=-1)

    def inference(self, x_all, layer_loader, device):
        pbar = tqdm(total=x_all.size(0) * self.num_layers, ncols=80)
        pbar.set_description('Evaluating')

        # Compute representations of nodes layer by layer, using *all*
        # available edges. This leads to faster computation in contrast to
        # immediately computing the final representations of each batch.
        xs_ori = []
        for i in range(self.num_layers):
            xs = []
            for idx, (batch_size, n_id, adj) in enumerate(layer_loader):
                # edge_index, _, size = adj.to(device)
                adj_t, _, size = adj.to(device)
                edge_index, edge_attr = convert(adj_t)
                x = x_all[n_id].to(device)
                # x_target = x[:size[1]]
                if i == 0:
                    x = x_0 = self.lins[0](x).relu()
                    xs_ori.append(x_0.detach().cpu())

                x = self.convs[i](x, xs_ori[idx].to(device), edge_index.float())
                x = F.relu(x)
                if i == (self.num_layers - 1):
                    x = self.lins[1](x)
                xs.append(x.detach().cpu())
                pbar.update(batch_size)

            x_all = torch.cat(xs, dim=0)
        # print(x_all.shape)
        pbar.close()
        del xs, xs_ori
        torch.cuda.empty_cache()
        return x_all.log_softmax(dim=-1).to(device)


import subprocess, os, time, sys, shutil

FTP_DIRECTORY = "/home/ASO/Builds"
ABSOL_PATH = "/home/podar/ASO"

def pullAndBuild(creds):
	ABS_PATH = "/home/podar/ASO"
	REPO_PATH = "~/ASO"
	BUILD_PATH = "~/ASO/Builds"

	print("Git tracking build scripts")

	os.chdir(os.path.expanduser(os.path.join(REPO_PATH, creds[0])))
	# print(os.path.join(REPO_PATH, creds[0]))

	p = subprocess.check_output(["git", "pull", "origin", "master"])
	x = p.decode(sys.stdout.encoding)
	# print(x)

	if (x == 'Already up to date.\n'):
		print("The repository " + creds[0] + " is up to date");
	else:
		print("Build...")
		# o = subprocess.call("runuser " + "-l " + creds[0] + " -c" + " '" + creds[1] + "'", shell = True)
		# o = subprocess.call("sudo " + "-u " + creds[0] + " '" + creds[1] + "'", shell = True)
		o = subprocess.call("su " + "-l " + creds[0] + " -c " + "'make -f MakeFile'", shell = True)
		if (o == 0):
			d = subprocess.check_output("ls", shell = True)
			dec = d.decode(sys.stdout.encoding)
			lis = dec.split("\n")
			if(creds[2] in lis):
				print("The build was succesfull!\n")
				os.chdir(os.path.expanduser(BUILD_PATH))
				d = subprocess.check_output("ls", shell = True)
				dec = d.decode(sys.stdout.encoding)
				lis = dec.split("\n")
				if(creds[0] not in lis):
					 subprocess.call(["mkdir", creds[0]])
				os.chdir(os.path.expanduser(os.path.join(os.path.join(BUILD_PATH, creds[0]))))
				n = subprocess.check_output("ls | wc -l", shell =True)
				m = n.decode(sys.stdout.encoding)
				i = int(m)
				print(i)
				i = i + 1
				z = "mkdir" + " build_" + str(i)
				z1 = "build_" + str(i)
				u = subprocess.call(z, shell = True)
				y = subprocess.call(["mv", os.path.join(ABS_PATH, creds[0], creds[3]), os.path.join(ABS_PATH, "Builds", creds[0], z1)])
		else:
			print("Build failed")

def check(creds):
	while True:
		for p in creds:
			pullAndBuild(p)
		time.sleep(10)

def createUsers(creds):
	for cred in creds:
		q = subprocess.call(["id", "-u", cred[0]])
		print(q)
		if (q == 1):
			subprocess.call(["sudo", "useradd", cred[0]])
			p = subprocess.call("sudo chown -R " + cred[0] + ":" + cred[0] + " " + ABSOL_PATH + "/" + cred[0] + "/", shell = True)
			o = subprocess.call("sudo chmod 704 " + ABSOL_PATH + "/" + cred[0] + "/", shell = True)

if (len(sys.argv) != 2):
	print("Scriptul trebuie sa contina doar un fisier de configurare ca parametru")
	sys.exit()

script_conf = sys.argv[1]
projects_builds = []

f = open(script_conf, "r")
projects_builds = f.read().splitlines()

print(projects_builds)

# every build's info from their configuration files
creds = []

for i in projects_builds:
	# info for build i
	g = open(i, "r")
	lines = g.read().splitlines()
	creds.append(lines)

print(creds)

createUsers(creds)

check(creds)