import os
import json
import time
from S3utility.s3_notification_info import parse_activity_data
from provider import digest_provider, download_helper, email_provider, utils
from activity.objects import Activity

class activity_ValidateDigestInput(Activity):
    "ValidateDigestInput activity"

    def __init__(self, settings, logger, client=None, token=None, activity_task=None):
        super(activity_ValidateDigestInput, self).__init__(
            settings, logger, client, token, activity_task
        )

        self.name = "ValidateDigestInput"
        self.version = "1"
        self.default_task_heartbeat_timeout = 30
        self.default_task_schedule_to_close_timeout = 60 * 30
        self.default_task_schedule_to_start_timeout = 30
        self.default_task_start_to_close_timeout = 60 * 5
        self.description = (
            "Download digest file input from the bucket, parse it, check for "
            + "valid data, and raise an error if it is invalid."
        )

        # Track some values
        self.input_file = None
        self.digest = None

        # Local directory settings
        self.directories = {
            "TEMP_DIR": os.path.join(self.get_tmp_dir(), "tmp_dir"),
            "INPUT_DIR": os.path.join(self.get_tmp_dir(), "input_dir"),
        }

        # Track the success of some steps
        self.statuses = {"build": None, "valid": None, "email": None}

        # Load the config
        self.digest_config = digest_provider.digest_config(
            self.settings.digest_config_section, self.settings.digest_config_file
        )

    def do_activity(self, data=None):
        """
        Activity, do the work
        """
        if self.logger:
            self.logger.info("data: %s" % json.dumps(data, sort_keys=True, indent=4))

        self.make_activity_directories()

        # parse the data with the digest_provider
        real_filename, bucket_name, bucket_folder = parse_activity_data(data)

        # Download from S3
        self.input_file = download_helper.download_file_from_s3(
            self.settings,
            real_filename,
            bucket_name,
            bucket_folder,
            self.directories.get("INPUT_DIR"),
        )

        # Parse input and build digest
        self.statuses["build"], self.digest = digest_provider.build_digest(
            self.input_file,
            self.directories.get("TEMP_DIR"),
            self.logger,
            self.digest_config,
        )

        # Approve files for emailing
        self.statuses["valid"], error_messages = digest_provider.validate_digest(
            self.digest
        )

        if not self.statuses.get("build") or not self.statuses.get("valid"):
            # Send error email
            self.statuses["email"] = self.email_error_report(
                real_filename, error_messages
            )
            self.log_statuses(self.input_file)
            return self.ACTIVITY_PERMANENT_FAILURE

        self.log_statuses(self.input_file)
        return True

    def log_statuses(self, input_file):
        "log the statuses value"
        self.logger.info(
            "%s for input_file %s statuses: %s"
            % (self.name, str(input_file), self.statuses)
        )

    def email_error_report(self, filename, error_messages):
        "send an email on error"
        datetime_string = time.strftime(utils.DATE_TIME_FORMAT, time.gmtime())
        body = email_provider.simple_email_body(datetime_string, error_messages)
        subject = error_email_subject(filename)
        sender_email = self.settings.digest_sender_email

        recipient_email_list = email_provider.list_email_recipients(
            self.settings.digest_validate_error_recipient_email
        )

        connection = email_provider.smtp_connect(self.settings, self.logger)
        # send the emails
        for recipient in recipient_email_list:
            # create the email
            email_message = email_provider.message(subject, sender_email, recipient)
            email_provider.add_text(email_message, body)
            # send the email
            email_provider.smtp_send(
                connection, sender_email, recipient, email_message, self.logger
            )
        return True

def error_email_subject(filename):
    "email subject for an error email"
    return "Error processing digest file: {filename}".format(filename=filename)


import store
import os
import FTP_Cryptography
import bson
from bson.binary import Binary
import mongodb
from cryptography.fernet import Fernet

files = mongodb.db.files

FILE_DATA = store.FILE_DATA
INFO_FILE = store.INFO_FILE

def check_file_name(file_name):
    return os.path.exists(file_name)

def send_file(socket, file_name):
    file = open(file_name, "rb")
    size = int(os.stat(file_name).st_size)
    socket.send(str(size).encode())

    LISTEN_MSG = socket.recv(1024)

    data = file.read(1024)
    while data:
        socket.send(data)
        data = file.read(1024)

def recv_file(socket, file_name):
    MSG = socket.recv(1024)
    size = int(MSG.decode())

    socket.send("continue".encode())

    time_loop = int(size / 1024)

    if os.path.exists(file_name):
        os.remove(file_name)
    data = b''
    i = 1
    file = open(file_name, "wb")
    while i <= time_loop:
        data = socket.recv(1024)
        file.write(data)
        i = i + 1
    last = size - time_loop*1024
    data = socket.recv(last)
    file.write(data)
    return

# UPLOAD -------------------------------------------------
def send_file_upload(socket, file_name, cryptography):
    file = open(file_name, "rb")
    size = int(os.stat(file_name).st_size)

    INFO_FILE["size"] = size
    INFO_FILE["cryptography"] = cryptography
    INFO_FILE_MSG = store.send_repr(INFO_FILE)
    socket.send(INFO_FILE_MSG)

    LISTEN_MSG = socket.recv(1024)

    data = file.read(1024)
    while data:
        socket.send(data)
        data = file.read(1024)

def recv_file_upload(socket, file_name):
    MSG = socket.recv(1024)
    MSG = store.recv_repr(MSG)
    size = MSG["size"]
    cryptography = MSG["cryptography"]

    socket.send("start".encode())

    time_loop = int(size / 1024)

    file = b''
    data = b''
    i = 1
    while i <= time_loop:
        data = Binary(socket.recv(1024))
        file = file + data
        i = i + 1
    last = size - time_loop*1024
    data = socket.recv(last)
    file = file + data
    insert_file(file_name, file, size, cryptography)
    return

def insert_file(file_name, file, size, cryptography):
    global FILE_DATA
    result = check_file_name_db(file_name)

    if(result != False):
        myquery = {"filename": file_name}
        files.delete_one(myquery)

    FILE_DATA = {
        "_id": file_name,
        "filename": file_name,
        "file": file,
        "size": size,
        "cryptography": cryptography,
    }

    files.insert_one(FILE_DATA)

# DOWNLOAD -------------------------------------------------
def check_file_name_db(filename):
    check_files = {}
    list_files = files.find({"filename": filename})
    for file in list_files:
        check_files = file
    if (check_files != {}):
        return check_files
    else:
        return False

def send_file_download(socket, file_name, cryptography):
    file = check_file_name_db(file_name)
    if(file != False):
        INFO_FILE["size"] = file["size"]
        INFO_FILE["cryptography"] = file["cryptography"]
        INFO_FILE_MSG = store.send_repr(INFO_FILE)
        socket.send(INFO_FILE_MSG)

        LISTEN_MSG = socket.recv(1024)

        socket.send(file['file'])

def recv_file_download(socket, file_name):
    MSG = socket.recv(1024)
    MSG = store.recv_repr(MSG)
    size = MSG["size"]
    cryptography = MSG["cryptography"]

    socket.send("start".encode())

    time_loop = int(size / 1024)

    if os.path.exists(file_name):
        os.remove(file_name)
    save_file = open(file_name, "wb")

    file = b''
    data = b''
    i = 1

    file = Binary(socket.recv(size))

    if(cryptography == True):
        key = FTP_Cryptography.load_key()
        f = Fernet(key)
        data = f.decrypt(file)
    else:
        data = file

    save_file.write(data)


import os
import maya.cmds as cmds
from functools import partial
import Core
reload(Core)

class UIContainer():
    frozen = False

def build(parent,
          imagesPath,
          iconSize=25,
          height=20,
          marginSize=5):
    """
    build widget
    @param parent : parent layout in maya
    @imagesPath : str path
    """
    # - Freeze Viewport---------------------------------------------------------------
    cmds.rowLayout(numberOfColumns=1, parent=parent)
    cmds.iconTextButton(style='iconOnly',
                        image1=os.path.join(imagesPath, 'freeze.png'),
                        hi=os.path.join(imagesPath, 'freeze_hi.png'),
                        width=iconSize, mw=marginSize, height=iconSize, mh=marginSize,
                        label='freeze',
                        annotation='Freeze / unFreeze viewport',
                        c=freeze)
    cmds.setParent("..")

def freeze(*args):
    """
    """
    if UIContainer.frozen == True:
        cmds.refresh(su=False)
        print('Viewport is unfrozen')
        UIContainer.frozen = False
    else:
        cmds.refresh(su=True)
        print('Viewport is frozen')
        UIContainer.frozen = True


import time
from bs4 import BeautifulSoup
import requests
from requests import Session

from Extractor.extractor import EbayKleinanzeigenExtractor
import json
from print_dict import pd

class Cookies:
    def __init__(self, filename: str = "default.json", log: bool = True,
                 cookies: dict = None, save=False, mode: str = "client", keep_old=False):
        # Test for custom configs
        self.log = log
        self.mode = mode
        self.save = save
        self.filename = filename
        self.googleChromeCookie = []
        self.request_cookies = dict()
        deploy_mode = "server"
        if deploy_mode == "online":
            self.cookie_domain = ".ebay-zakir-1996.onrender.com"
        elif deploy_mode == "offline":
            self.cookie_domain = ".ebay-kleinanzeigen-zakir.de"
        elif deploy_mode == "mobile":
            self.cookie_domain = ".192.168.151.149"
        elif deploy_mode == "server":
            self.cookie_domain = ".kleinanzeigen.de"
        # self.ebay_url = "https://www.ebay-kleinanzeigen.de/"
        # user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:108.0) Gecko/20100101 Firefox/108.0"
        # self.headers = {'User-Agent': user_agent, 'Connection': 'keep-alive',
        #                 'Accept-Encoding': 'gzip, deflate',
        #                 'Accept': '*/*'}

        if mode == "client":
            if cookies:
                self.request_cookies = cookies.copy()
                self.load_google_chrome_cookies_from_request(log=log)
        elif mode == "server":
            self.load_google_chrome_cookies_from_file(log=log)
            if not keep_old:
                print("refreshing cookies")
                self.refresh_google_chrome_cookies()
            for cook in self.googleChromeCookie:
                # print(cook['name'])
                self.request_cookies[cook['name']] = cook['value']

    ###############################################
    #   load google chrome cookies
    ###############################################
    def load_google_chrome_cookies_from_file(self, log=True):
        with open(self.filename, 'r') as f:
            self.googleChromeCookie = json.load(f)
            # for cook in self.googleChromeCookie:
            #     if "kleinanzeigen.de" not in cook.get("domain"):
            #         self.googleChromeCookie.remove(cook)
            # if log:
            #     print("Loading default config")

    ###############################################
    #   load google chrome cookies
    ###############################################
    def load_google_chrome_cookies_from_request(self, log=True):

        for name, value in self.request_cookies.items():
            cook = dict(name=name, value=value, path="/",
                        domain=self.cookie_domain,
                        expirationDate="")
            self.googleChromeCookie.append(cook)

        self.save_cookies()

    ###############################################
    #   saving cookies to a file
    ###############################################

    def save_cookies(self):
        f = open(self.filename, "w")
        f.write(json.dumps(self.googleChromeCookie))
        f.close()

    ################################################################
    #   removing expired cookies bevor sending request (mode == server
    ################################################################
    def refresh_google_chrome_cookies(self, log=True):
        for cook in self.googleChromeCookie:
            # cook['domain'] = self.cookie_domain
            index = 0
            if cook.get('expirationDate') is not None:
                # print("with expiration ",cook.get("name"))
                rem = int(cook.get('expirationDate')) - time.time()
                if rem <= 0:
                    self.googleChromeCookie.remove(cook)
                    if log:
                        print(cook['name'] + " was removed from cookies")

            else:
                pass
                # self.googleChromeCookie.remove(cook)
                # if log:
                #     print(cook['name'] + " was removed from cookies")

            pass
        pass

    ###############################################
    #   setting
    ###############################################
    def set_cookies(self, session: Session):
        # for co in session.cookies:
        #     for key , value in co.items()
        # c.
        self.cookies_temp = [
            {'name': c.name, 'secure': c.secure, 'hostOnly':  True ,
             'httpOnly': False, 'value': c.value, "sameSite": "unspecified",
             'expirationDate': c.expires, "session": c.expires is None, "storeId": 0,
             'domain': c.domain, 'path': c.path}
            for c in session.cookies]
        for cook in self.cookies_temp:
            self.request_cookies[cook['name']] = cook['value']
        found = False
        for cook in self.cookies_temp:
            for cook2 in self.googleChromeCookie:
                if cook['name'] == cook2['name']:
                    found = True
                    self.googleChromeCookie.remove(cook2)
                    self.googleChromeCookie.append(cook)

            if not found:
                # if cook['expirationDate']:
                self.googleChromeCookie.append(cook)
            found = False
        self.refresh_google_chrome_cookies(self.log)
        for cook in self.googleChromeCookie:
            self.request_cookies[cook['name']] = cook['value']
        self.save_cookies()

    ###############################################
    #   resetting cookies
    ###############################################
    def reset_cookies(self):
        return
        self.request_cookies = {}
        self.googleChromeCookie = []
        self.save_cookies()
    def print_request_cookies(self):
        # for key,value in self.request_cookies.items():
        #     print(key,value)
        for cook in self.googleChromeCookie:
            print(cook['name'],cook['domain'])
    def remove_specific_cookies(self):
        un_wanted_cookies = ["ak_bmsc","_gat","bm_sv"]
        for unw_cook in un_wanted_cookies:
            if self.request_cookies.get(unw_cook):
                print("removing",unw_cook)
                self.request_cookies.pop(unw_cook)
        for cook in self.googleChromeCookie:
            for unw_cook in un_wanted_cookies:
                if cook['name'] == unw_cook:
                    print("removing 2", unw_cook)
                    self.googleChromeCookie.remove(cook)


from flask import Blueprint, request, jsonify, json
from db import db, app, ma
from flask import Flask,  redirect, request, jsonify, json, session, render_template
from model.turnos  import turnos, turnosSchema

routes_turnos = Blueprint("routes_turnos", __name__)

turno_schema = turnosSchema()
turnos_schema = turnosSchema(many=True)

@routes_turnos.route('/turnos', methods=['GET'])
def obtenerturnos():
    returnall = turnos.query.all()

    resultado_turnos = turnos_schema.dump(returnall)
    return jsonify(resultado_turnos)

@routes_turnos.route('/saveturnos', methods=['POST'])
def guardar_turnos():
    newturnos = request.json['id_vehiculo','id_rol','Hora_inicio','Hora_Fin']
    new_tur = turnos(newturnos)
    db.session.add(new_tur)
    db.session.commit()
    return redirect('/turnos')

@routes_turnos.route('/eliminarturnos/<id>', methods=['GET'] )
def eliminartur(id):
    tur = turnos.query.get(id)
    db.session.delete(tur)
    db.session.commit()
    return jsonify(turnos_schema.dump(tur))

@routes_turnos.route('/actualizarturnos', methods=['POST'] )
def actualizartur():
    id = request.json['id']
    tur = request.json['id_vehiculo','id_rol','Hora_inicio','Hora_Fin']
    pusuario = turnos.query.get(id)
    pusuario.Nombre_turnos = tur
    db.session.commit()
    return redirect('/turnos')


from random import randint
import math
from PIL import Image, ImageDraw, ImageFont

# Did you know that calculating the correct font size
# is NP-Hard? This function is for when they will solve
# P = NP?
def calculate_font_size(string, image_width):
    global FONT_SIZE
    return FONT_SIZE

def pad_string(text, how_many):
    length = len(text)
    for _ in range(how_many):
        position = randint(1, length-1)
        text = text[:position] + ' ' + text[position:]
    return text

def generate_strings(text, how_many):
    already = [0] # because we don't like it in the first char
    strings = []
    for i in range(how_many):
        # This is to generate always different random numbers
        position = randint(0, len(text))
        if len(already) == len(text):
            already = [0]
        while position in already:
            position = randint(0, len(text))
        already.append(position)

        character = ['/', '_'][randint(0, 1)]
        strings.append("{}{}{}".format(
            text[:position],
            character,
            text[position:]
        ))

    return strings

def create_image(strings, colors, size, background):
    font_size = calculate_font_size(strings[0], size)
    line_height = 5
    image = Image.new('RGB', size)
    image.paste(background)
    draw = ImageDraw.Draw(image)
    font = ImageFont.truetype('arial.ttf', font_size)
    for i in range(len(strings)):
        draw.text(
            (0, (font_size+line_height) * i),
            strings[i],
            fill=colors[i % len(colors)],
            font=font
        )

    return image

# Size of the output image
IMAGE_SIZE = (256, 256)

# Use 34 if you want to write 'IN RAINBOWS'
FONT_SIZE = 34

# These are the colors of the original album art
COLORS = [
    (245, 230, 70),     # yellow
    (70, 138, 200),     # blue
    (237, 104, 42),     # orange
    (64, 185, 74),      # green
    (234, 173, 30),     # darker yellow
    (227, 34, 46),      # red
    (158, 222, 232)     # white/very light blue
]

main_text = "IN RAINBOWS"
lower_text = "RADIOHEAD"

if len(main_text) > len(lower_text):
    lower_text = pad_string(lower_text, len(main_text) - len(lower_text))
else:
    main_text = pad_string(main_text, len(lower_text) - len(main_text))

image = create_image(
    generate_strings(main_text, 4) + generate_strings(lower_text, 2),
    COLORS,
    IMAGE_SIZE,
    Image.open('background.png', mode='r')
)
image.save("output.png")


from datetime import datetime

import cv2 as cv
import matplotlib.pyplot as plt

def perform_sift_performant(query, train):
    start = datetime.now()
    img1 = cv.imread(query, cv.IMREAD_GRAYSCALE)  # queryImage
    img2 = cv.imread(train, cv.IMREAD_GRAYSCALE)  # trainImage

    # Initiate SIFT detector
    sift = cv.SIFT_create()

    # find the keypoints and descriptors with SIFT
    kp1, des1 = sift.detectAndCompute(img1, None)
    kp2, des2 = sift.detectAndCompute(img2, None)

    # FLANN parameters
    FLANN_INDEX_KDTREE = 1
    index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
    search_params = dict(checks=50)  # or pass empty dictionary
    flann = cv.FlannBasedMatcher(index_params, search_params)
    matches = flann.knnMatch(des1, des2, k=2)

    # Need to draw only good matches, so create a mask
    matchesMask = [[0, 0] for _ in range(len(matches))]

    # ratio test as per Lowe's paper
    for i, (m, n) in enumerate(matches):
        if m.distance < 0.6 * n.distance:
            matchesMask[i] = [1, 0]
    print(datetime.now() - start)
    draw_params = dict(matchColor=(0, 255, 0),
                       singlePointColor=(255, 0, 0),
                       matchesMask=matchesMask,
                       flags=cv.DrawMatchesFlags_DEFAULT)
    img3 = cv.drawMatchesKnn(img1, kp1, img2, kp2, matches, None, **draw_params)
    plt.imshow(img3, ), plt.show()


import os
import json
import requests

class app_info_mode():
    def __init__(self):
        self.path = os.path.abspath(os.path.dirname(__file__))
        self.mode = "translate"
        self.print_format = "t_mode"
        self.version = "v2.0"
        self.fromlang = 'en'
        self.tolang = 'zh'
        #get the appid&seckey;if none set before,then input
        #and store in info.json,if stored in info.json,then
        #read it
    def translate_init(self):
        fd = open(self.path+"/info.json","r")
        info_dict = json.load(fd)
        info_private_appid = info_dict["private"]["appid"]
        info_private_seckey = info_dict["private"]["seckey"]
        if info_private_appid == info_private_seckey:
            self.appid = input("[translate mode]set the appid:")
            self.seckey = input("[translate mode]set the seckey:")
            fd.close()

            info_dict["private"]["appid"]=self.appid
            info_dict["private"]["seckey"]=self.seckey

            #make the str that will write into the info.json
            #file a easy format to read
            info_str = str()
            for i in json.dumps(info_dict):
                info_str = info_str + i
                if i == ',':
                    info_str = info_str+'\n'

            #wirte the str into the info.json
            fd = open(self.path+"/info.json","w")
            fd.write(info_str)
            fd.close()
        else:
            self.appid = info_private_appid
            self.seckey = info_private_seckey
            fd.close()

    def robot_token_update(self):
        api_id = input("[robot mode]set api_id:")
        seckey = input("[robot mode]set seckey:")
        url = "https://aip.baidubce.com/oauth/2.0/token"
        req_url = url+f"?grant_type=client_credentials&client_id={api_id}&client_secret={seckey}"
        payload = ""
        headers = {'Content-Type': 'application/json','Accept': 'application/json'}
        response = requests.request("POST", req_url, headers=headers, data=payload)
        response = json.loads(response.text)

        if response["access_token"]:
           self.token = response["access_token"]
           fd = open(self.path+"/info.json",'r')
           info_dict = json.load(fd)
           fd.close()
           info_dict["token"]=response["access_token"]
           info_dict = json.dumps(info_dict)
           info_str = str()

           for i in info_dict:
               info_str = info_str+i
               if i == ',':
                   info_str=info_str+'\n'

           fd = open(self.path+"/info.json",'w')
           fd.write(info_str)
           fd.close()
    def token_get(self):
        fd = open(self.path+"/info.json",'r')
        info_dict = json.load(fd)
        #print(info_dict["token"])
        if info_dict["token"]=="none":
            #print(info_dict["token"])
            self.robot_token_update()
        else:
            #print('111')
            self.token = info_dict["token"]

if __name__ == "__main__":
    app_info = app_info_mode()
    print(app_info.appid,app_info.seckey)


'''
47. 이진 트리 직렬화 & 역직렬화
이진트리를 배열로 직렬화하고, 반대로 역직렬화하는 기능을 구현하라.
https://leetcode.com/problems/serialize-and-deserialize-binary-tree/
'''
from collections import deque

# Definition for a binary tree node.
class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Codec:
    def serialize(self, root):
        queue = deque()
        queue.append(root)
        answer = [-1]
        while queue:
            node = queue.popleft()
            if not node:
                answer.append(None)
            else:
                answer.append(node.val)
                queue.append(node.left)
                queue.append(node.right)

        while answer[-1] is None:
            answer.pop()
        return answer

    def deserialize(self, data):
        if len(data) == 1:
            return None

        data = deque(data[1:])
        root = TreeNode(data.popleft())
        queue = deque([root])
        while queue:
            node = queue.popleft()
            if node and data:
                left = data.popleft()
                node.left = TreeNode(left) if left is not None else None
                queue.append(node.left)
            if node and data:
                right = data.popleft()
                node.right = TreeNode(right) if right is not None else None
                queue.append(node.right)
        return root

if __name__ == '__main__':
    root = TreeNode(1)
    root.left = TreeNode(2)
    root.right = TreeNode(3)
    root.right.left = TreeNode(4)
    root.right.right = TreeNode(5)
    root.right.left.left = TreeNode(6)
    root.right.left.right = TreeNode(7)

    ser = Codec()
    deser = Codec()

    print(ser.serialize(root))
    print(deser.deserialize(ser.serialize(root)))


# Polinomial Linear Regression

# 1) Importing Libraries

import numpy as np                 # Mathematics fucntopns
import matplotlib.pyplot as plt    # Plot and Charts
import pandas as pd                #Data Set management and import them
import seaborn as sns              # Covariance Matrix

# 2) importing Dataset
poli_dataset = pd.read_csv('Position_Salaries.csv')
X = poli_dataset.iloc[:,1:2].values   # dependend variable matrix means that will helo in prediction [rows , columns]
Y = poli_dataset.iloc[:,2].values      # independent variable vector means that need to be predicted [rows , columns]
#poli_dataset.iloc[:,1:2].values  we specify 1:2 in it to make it matrix ,upper bound will not be included so relax

# 3)Taking Care of Missing Data (We take mean of that column and put in missing area thats simple)
from sklearn.preprocessing import Imputer  # importing class for data missing values
imputer = Imputer(missing_values = 'NaN' , strategy = 'mean' , axis = 0)   # making object of that class
imp_1 = imputer.fit(X[:, 0:1])  # fit above imputer object to our matrix X (we can do this way as well to select column by index no by  imp_1 = imputer.fit(X[:, [1,2]])   )
X[:,  0:1] = imp_1.transform(X[:, 0:1])   # LHS X dataset and RHs imp_object to implement missing values  (   X[:, 1:3] = imp_1.transform(X[:, 1:3])   )

"""
# 4) Categorical Data and Encoding it - in our case STATE is categorical variable
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
labelencoder_X = LabelEncoder()
startup_X[:, 3]  = labelencoder_X.fit_transform(startup_X[:, 3])
#X = np.array(X,dtype= 'int64')
#Here we are splitting categorial variable states in column as no of catogories - no of columns
onehotencoder = OneHotEncoder(categorical_features = [3])
startup_X = onehotencoder.fit_transform(startup_X).toarray() # Dummy variable  is created against state

#labelencoder_Y = LabelEncoder()
#Y  = labelencoder_Y.fit_transform(Y)

# 5) Avoiding Dummy Variable Trap
    # We always remove 1 column from dummy variable like in our case we generated three columns and dummy_var = dn-1
startup_X = startup_X[:, 1:]
"""

#NOTE We have very small data , to predict very accurate result we are not splitting our dataset instead we are going with full -Skipping this step
# 6) Split Data into Train and Test
from sklearn.cross_validation import train_test_split
X_train , X_test , Y_train, Y_test = train_test_split(X,Y , test_size = 0.2 , random_state=0) #test_size can be 0.3 or 1/3

# Plotting data into chart to see curve
plt.scatter(X , Y, color = 'red') #regressor.predict(X_train) tp predict the value of salary
plt.title('Polinomial Regression')
plt.xlabel('Position')
plt.ylabel('Salary')
plt.show()

# 6) Fitting Linear Regression to Dataset    (It is just made to compare)
from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg .fit(X,Y)

# 7) Fitting Polynomial Regression to Dataset
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 4)
X_poly = poly_reg.fit_transform(X)
lin_reg2 = LinearRegression()
lin_reg2 .fit(X_poly,Y)

# 8 ) Visualization

# Linear Regression
plt.scatter(X, Y, color = 'red')
plt.plot(X,lin_reg.predict(X) ,color = 'blue') #regressor.predict(X) tp predict the value of salary
plt.title('Linear Regression')
plt.xlabel('Exp position')
plt.ylabel('Salary')
plt.show() # If we see result it is not goog model to show

# Polynomial Linear Regression
plt.scatter(X, Y, color = 'red')
plt.plot(X,lin_reg2.predict(X_poly) ,color = 'blue') #Ploynomial regressor.predict(X) tp predict the value of salary
plt.title('Polynimial Regression')
plt.xlabel('Exp position')
plt.ylabel('Salary')
plt.show()

# Polynomial Linear Regression (better visulaization)
x_grid = np.arange(min(X),max(X),0.1)
x_grid = x_grid.reshape(len(x_grid),1)
plt.scatter(X, Y, color = 'red')
plt.plot(x_grid,lin_reg2.predict(poly_reg.fit_transform(x_grid)) ,color = 'blue') #Ploynomial regressor.predict(X) tp predict the value of salary
plt.title('Polynimial Regression')
plt.xlabel('Exp position')
plt.ylabel('Salary')
plt.show()

# 9) Predictions Result
# Linear Regression Prediction
pos_pred = lin_reg.predict(X)
pos_pred1 = lin_reg.predict(6.5)

# Polunomial Regression Prediction
pos_pred2  = lin_reg2.predict(X_poly)
pos_pred3  = lin_reg2.predict(poly_reg.fit_transform(6.5))

Machine Learning A-Z/Part 2 - Regression/Section 6 - Polynomial Regression/POLINOMIAL_LINEAR_REGRESSION.py


import inspect
import logging
import ssl
import signal
from pathlib import Path
from queue import Queue
from threading import Event, Lock, Thread, current_thread
from time import sleep
from typing import (
    Any,
    Callable,
    List,
    Optional,
    Tuple,
    Union,
    no_type_check,
    Generic,
    TypeVar,
    TYPE_CHECKING,
)

from telegram.error import InvalidToken, RetryAfter, TimedOut, Forbidden, TelegramError
from telegram._utils.warnings import warn
from telegram.ext import Dispatcher
from telegram.ext._utils.webhookhandler import WebhookAppClass, WebhookServer
from telegram.ext._utils.stack import was_called_by
from telegram.ext._utils.types import BT

if TYPE_CHECKING:
    from telegram.ext._builders import InitUpdaterBuilder

DT = TypeVar('DT', bound=Union[None, Dispatcher])

class Updater(Generic[BT, DT]):
    """
    This class, which employs the :class:`telegram.ext.Dispatcher`, provides a frontend to
    :class:`telegram.Bot` to the programmer, so they can focus on coding the bot. Its purpose is to
    receive the updates from Telegram and to deliver them to said dispatcher. It also runs in a
    separate thread, so the user can interact with the bot, for example on the command line. The
    dispatcher supports handlers for different kinds of data: Updates from Telegram, basic text
    commands and even arbitrary types. The updater can be started as a polling service or, for
    production, use a webhook to receive updates. This is achieved using the WebhookServer and
    WebhookHandler classes.

    Note:
         This class may not be initialized directly. Use :class:`telegram.ext.UpdaterBuilder` or
         :meth:`builder` (for convenience).

    .. versionchanged:: 14.0

        * Initialization is now done through the :class:`telegram.ext.UpdaterBuilder`.
        * Renamed ``user_sig_handler`` to :attr:`user_signal_handler`.
        * Removed the attributes ``job_queue``, and ``persistence`` - use the corresponding
          attributes of :attr:`dispatcher` instead.

    Attributes:
        bot (:class:`telegram.Bot`): The bot used with this Updater.
        user_signal_handler (:obj:`function`): Optional. Function to be called when a signal is
            received.

            .. versionchanged:: 14.0
                Renamed ``user_sig_handler`` to ``user_signal_handler``.
        update_queue (:obj:`Queue`): Queue for the updates.
        dispatcher (:class:`telegram.ext.Dispatcher`): Optional. Dispatcher that handles the
            updates and dispatches them to the handlers.
        running (:obj:`bool`): Indicates if the updater is running.
        exception_event (:class:`threading.Event`): When an unhandled exception happens while
            fetching updates, this event will be set. If :attr:`dispatcher` is not :obj:`None`, it
            is the same object as :attr:`telegram.ext.Dispatcher.exception_event`.

            .. versionadded:: 14.0

    """

    __slots__ = (
        'dispatcher',
        'user_signal_handler',
        'bot',
        'logger',
        'update_queue',
        'exception_event',
        'last_update_id',
        'running',
        'is_idle',
        'httpd',
        '__lock',
        '__threads',
    )

    def __init__(
        self: 'Updater[BT, DT]',
        *,
        user_signal_handler: Callable[[int, object], Any] = None,
        dispatcher: DT = None,
        bot: BT = None,
        update_queue: Queue = None,
        exception_event: Event = None,
    ):
        if not was_called_by(
            inspect.currentframe(), Path(__file__).parent.resolve() / '_builders.py'
        ):
            warn(
                '`Updater` instances should be built via the `UpdaterBuilder`.',
                stacklevel=2,
            )

        self.user_signal_handler = user_signal_handler
        self.dispatcher = dispatcher
        if self.dispatcher:
            self.bot = self.dispatcher.bot
            self.update_queue = self.dispatcher.update_queue
            self.exception_event = self.dispatcher.exception_event
        else:
            self.bot = bot
            self.update_queue = update_queue
            self.exception_event = exception_event

        self.last_update_id = 0
        self.running = False
        self.is_idle = False
        self.httpd = None
        self.__lock = Lock()
        self.__threads: List[Thread] = []
        self.logger = logging.getLogger(__name__)

    @staticmethod
    def builder() -> 'InitUpdaterBuilder':
        """Convenience method. Returns a new :class:`telegram.ext.UpdaterBuilder`.

        .. versionadded:: 14.0
        """
        # Unfortunately this needs to be here due to cyclical imports
        from telegram.ext import UpdaterBuilder  # pylint: disable=import-outside-toplevel

        return UpdaterBuilder()

    def _init_thread(self, target: Callable, name: str, *args: object, **kwargs: object) -> None:
        thr = Thread(
            target=self._thread_wrapper,
            name=f"Bot:{self.bot.id}:{name}",
            args=(target,) + args,
            kwargs=kwargs,
        )
        thr.start()
        self.__threads.append(thr)

    def _thread_wrapper(self, target: Callable, *args: object, **kwargs: object) -> None:
        thr_name = current_thread().name
        self.logger.debug('%s - started', thr_name)
        try:
            target(*args, **kwargs)
        except Exception:
            self.exception_event.set()
            self.logger.exception('unhandled exception in %s', thr_name)
            raise
        self.logger.debug('%s - ended', thr_name)

    def start_polling(
        self,
        poll_interval: float = 0.0,
        timeout: float = 10,
        bootstrap_retries: int = -1,
        read_timeout: float = 2.0,
        allowed_updates: List[str] = None,
        drop_pending_updates: bool = None,
    ) -> Optional[Queue]:
        """Starts polling updates from Telegram.

        .. versionchanged:: 14.0
            Removed the ``clean`` argument in favor of ``drop_pending_updates``.

        Args:
            poll_interval (:obj:`float`, optional): Time to wait between polling updates from
                Telegram in seconds. Default is ``0.0``.
            timeout (:obj:`float`, optional): Passed to :meth:`telegram.Bot.get_updates`.
            drop_pending_updates (:obj:`bool`, optional): Whether to clean any pending updates on
                Telegram servers before actually starting to poll. Default is :obj:`False`.

                .. versionadded :: 13.4
            bootstrap_retries (:obj:`int`, optional): Whether the bootstrapping phase of the
                :class:`telegram.ext.Updater` will retry on failures on the Telegram server.

                * < 0 - retry indefinitely (default)
                *   0 - no retries
                * > 0 - retry up to X times

            allowed_updates (List[:obj:`str`], optional): Passed to
                :meth:`telegram.Bot.get_updates`.
            read_timeout (:obj:`float` | :obj:`int`, optional): Grace time in seconds for receiving
                the reply from server. Will be added to the ``timeout`` value and used as the read
                timeout from server (Default: ``2``).

        Returns:
            :obj:`Queue`: The update queue that can be filled from the main thread.

        """
        with self.__lock:
            if not self.running:
                self.running = True

                # Create & start threads
                dispatcher_ready = Event()
                polling_ready = Event()

                if self.dispatcher:
                    self._init_thread(self.dispatcher.start, "dispatcher", ready=dispatcher_ready)
                self._init_thread(
                    self._start_polling,
                    "updater",
                    poll_interval,
                    timeout,
                    read_timeout,
                    bootstrap_retries,
                    drop_pending_updates,
                    allowed_updates,
                    ready=polling_ready,
                )

                self.logger.debug('Waiting for polling to start')
                polling_ready.wait()
                if self.dispatcher:
                    self.logger.debug('Waiting for Dispatcher to start')
                    dispatcher_ready.wait()

                # Return the update queue so the main thread can insert updates
                return self.update_queue
            return None

    def start_webhook(
        self,
        listen: str = '127.0.0.1',
        port: int = 80,
        url_path: str = '',
        cert: str = None,
        key: str = None,
        bootstrap_retries: int = 0,
        webhook_url: str = None,
        allowed_updates: List[str] = None,
        drop_pending_updates: bool = None,
        ip_address: str = None,
        max_connections: int = 40,
    ) -> Optional[Queue]:
        """
        Starts a small http server to listen for updates via webhook. If :attr:`cert`
        and :attr:`key` are not provided, the webhook will be started directly on
        http://listen:port/url_path, so SSL can be handled by another
        application. Else, the webhook will be started on
        https://listen:port/url_path. Also calls :meth:`telegram.Bot.set_webhook` as required.

        .. versionchanged:: 13.4
            :meth:`start_webhook` now *always* calls :meth:`telegram.Bot.set_webhook`, so pass
            ``webhook_url`` instead of calling ``updater.bot.set_webhook(webhook_url)`` manually.

        .. versionchanged:: 14.0
            Removed the ``clean`` argument in favor of ``drop_pending_updates`` and removed the
            deprecated argument ``force_event_loop``.

        Args:
            listen (:obj:`str`, optional): IP-Address to listen on. Default ``127.0.0.1``.
            port (:obj:`int`, optional): Port the bot should be listening on. Must be one of
                :attr:`telegram.constants.SUPPORTED_WEBHOOK_PORTS`. Defaults to ``80``.
            url_path (:obj:`str`, optional): Path inside url.
            cert (:obj:`str`, optional): Path to the SSL certificate file.
            key (:obj:`str`, optional): Path to the SSL key file.
            drop_pending_updates (:obj:`bool`, optional): Whether to clean any pending updates on
                Telegram servers before actually starting to poll. Default is :obj:`False`.

                .. versionadded :: 13.4
            bootstrap_retries (:obj:`int`, optional): Whether the bootstrapping phase of the
                :class:`telegram.ext.Updater` will retry on failures on the Telegram server.

                * < 0 - retry indefinitely (default)
                *   0 - no retries
                * > 0 - retry up to X times

            webhook_url (:obj:`str`, optional): Explicitly specify the webhook url. Useful behind
                NAT, reverse proxy, etc. Default is derived from ``listen``, ``port`` &
                ``url_path``.
            ip_address (:obj:`str`, optional): Passed to :meth:`telegram.Bot.set_webhook`.

                .. versionadded :: 13.4
            allowed_updates (List[:obj:`str`], optional): Passed to
                :meth:`telegram.Bot.set_webhook`.
            max_connections (:obj:`int`, optional): Passed to
                :meth:`telegram.Bot.set_webhook`.

                .. versionadded:: 13.6

        Returns:
            :obj:`Queue`: The update queue that can be filled from the main thread.

        """
        with self.__lock:
            if not self.running:
                self.running = True

                # Create & start threads
                webhook_ready = Event()
                dispatcher_ready = Event()

                if self.dispatcher:
                    self._init_thread(self.dispatcher.start, "dispatcher", dispatcher_ready)
                self._init_thread(
                    self._start_webhook,
                    "updater",
                    listen,
                    port,
                    url_path,
                    cert,
                    key,
                    bootstrap_retries,
                    drop_pending_updates,
                    webhook_url,
                    allowed_updates,
                    ready=webhook_ready,
                    ip_address=ip_address,
                    max_connections=max_connections,
                )

                self.logger.debug('Waiting for webhook to start')
                webhook_ready.wait()
                if self.dispatcher:
                    self.logger.debug('Waiting for Dispatcher to start')
                    dispatcher_ready.wait()

                # Return the update queue so the main thread can insert updates
                return self.update_queue
            return None

    @no_type_check
    def _start_polling(
        self,
        poll_interval,
        timeout,
        read_timeout,
        bootstrap_retries,
        drop_pending_updates,
        allowed_updates,
        ready=None,
    ):  # pragma: no cover
        # Thread target of thread 'updater'. Runs in background, pulls
        # updates from Telegram and inserts them in the update queue of the
        # Dispatcher.

        self.logger.debug('Updater thread started (polling)')

        self._bootstrap(
            bootstrap_retries,
            drop_pending_updates=drop_pending_updates,
            webhook_url='',
            allowed_updates=None,
        )

        self.logger.debug('Bootstrap done')

        def polling_action_cb():
            updates = self.bot.get_updates(
                self.last_update_id,
                timeout=timeout,
                read_timeout=read_timeout,
                allowed_updates=allowed_updates,
            )

            if updates:
                if not self.running:
                    self.logger.debug('Updates ignored and will be pulled again on restart')
                else:
                    for update in updates:
                        self.update_queue.put(update)
                    self.last_update_id = updates[-1].update_id + 1

            return True

        def polling_onerr_cb(exc):
            # Put the error into the update queue and let the Dispatcher
            # broadcast it
            self.update_queue.put(exc)

        if ready is not None:
            ready.set()

        self._network_loop_retry(
            polling_action_cb, polling_onerr_cb, 'getting Updates', poll_interval
        )

    @no_type_check
    def _network_loop_retry(self, action_cb, onerr_cb, description, interval):
        """Perform a loop calling `action_cb`, retrying after network errors.

        Stop condition for loop: `self.running` evaluates :obj:`False` or return value of
        `action_cb` evaluates :obj:`False`.

        Args:
            action_cb (:obj:`callable`): Network oriented callback function to call.
            onerr_cb (:obj:`callable`): Callback to call when TelegramError is caught. Receives the
                exception object as a parameter.
            description (:obj:`str`): Description text to use for logs and exception raised.
            interval (:obj:`float` | :obj:`int`): Interval to sleep between each call to
                `action_cb`.

        """
        self.logger.debug('Start network loop retry %s', description)
        cur_interval = interval
        while self.running:
            try:
                if not action_cb():
                    break
            except RetryAfter as exc:
                self.logger.info('%s', exc)
                cur_interval = 0.5 + exc.retry_after
            except TimedOut as toe:
                self.logger.debug('Timed out %s: %s', description, toe)
                # If failure is due to timeout, we should retry asap.
                cur_interval = 0
            except InvalidToken as pex:
                self.logger.error('Invalid token; aborting')
                raise pex
            except TelegramError as telegram_exc:
                self.logger.error('Error while %s: %s', description, telegram_exc)
                onerr_cb(telegram_exc)
                cur_interval = self._increase_poll_interval(cur_interval)
            else:
                cur_interval = interval

            if cur_interval:
                sleep(cur_interval)

    @staticmethod
    def _increase_poll_interval(current_interval: float) -> float:
        # increase waiting times on subsequent errors up to 30secs
        if current_interval == 0:
            current_interval = 1
        elif current_interval < 30:
            current_interval *= 1.5
        else:
            current_interval = min(30.0, current_interval)
        return current_interval

    @no_type_check
    def _start_webhook(
        self,
        listen,
        port,
        url_path,
        cert,
        key,
        bootstrap_retries,
        drop_pending_updates,
        webhook_url,
        allowed_updates,
        ready=None,
        ip_address=None,
        max_connections: int = 40,
    ):
        self.logger.debug('Updater thread started (webhook)')

        # Note that we only use the SSL certificate for the WebhookServer, if the key is also
        # present. This is because the WebhookServer may not actually be in charge of performing
        # the SSL handshake, e.g. in case a reverse proxy is used
        use_ssl = cert is not None and key is not None

        if not url_path.startswith('/'):
            url_path = f'/{url_path}'

        # Create Tornado app instance
        app = WebhookAppClass(url_path, self.bot, self.update_queue)

        # Form SSL Context
        # An SSLError is raised if the private key does not match with the certificate
        if use_ssl:
            try:
                ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
                ssl_ctx.load_cert_chain(cert, key)
            except ssl.SSLError as exc:
                raise TelegramError('Invalid SSL Certificate') from exc
        else:
            ssl_ctx = None

        # Create and start server
        self.httpd = WebhookServer(listen, port, app, ssl_ctx)

        if not webhook_url:
            webhook_url = self._gen_webhook_url(listen, port, url_path)

        # We pass along the cert to the webhook if present.
        if cert is not None:
            with open(cert, 'rb') as cert_file:
                self._bootstrap(
                    cert=cert_file,
                    max_retries=bootstrap_retries,
                    drop_pending_updates=drop_pending_updates,
                    webhook_url=webhook_url,
                    allowed_updates=allowed_updates,
                    ip_address=ip_address,
                    max_connections=max_connections,
                )
        else:
            self._bootstrap(
                max_retries=bootstrap_retries,
                drop_pending_updates=drop_pending_updates,
                webhook_url=webhook_url,
                allowed_updates=allowed_updates,
                ip_address=ip_address,
                max_connections=max_connections,
            )

        self.httpd.serve_forever(ready=ready)

    @staticmethod
    def _gen_webhook_url(listen: str, port: int, url_path: str) -> str:
        return f'https://{listen}:{port}{url_path}'

    @no_type_check
    def _bootstrap(
        self,
        max_retries,
        drop_pending_updates,
        webhook_url,
        allowed_updates,
        cert=None,
        bootstrap_interval=5,
        ip_address=None,
        max_connections: int = 40,
    ):
        retries = [0]

        def bootstrap_del_webhook():
            self.logger.debug('Deleting webhook')
            if drop_pending_updates:
                self.logger.debug('Dropping pending updates from Telegram server')
            self.bot.delete_webhook(drop_pending_updates=drop_pending_updates)
            return False

        def bootstrap_set_webhook():
            self.logger.debug('Setting webhook')
            if drop_pending_updates:
                self.logger.debug('Dropping pending updates from Telegram server')
            self.bot.set_webhook(
                url=webhook_url,
                certificate=cert,
                allowed_updates=allowed_updates,
                ip_address=ip_address,
                drop_pending_updates=drop_pending_updates,
                max_connections=max_connections,
            )
            return False

        def bootstrap_onerr_cb(exc):
            if not isinstance(exc, Forbidden) and (max_retries < 0 or retries[0] < max_retries):
                retries[0] += 1
                self.logger.warning(
                    'Failed bootstrap phase; try=%s max_retries=%s', retries[0], max_retries
                )
            else:
                self.logger.error('Failed bootstrap phase after %s retries (%s)', retries[0], exc)
                raise exc

        # Dropping pending updates from TG can be efficiently done with the drop_pending_updates
        # parameter of delete/start_webhook, even in the case of polling. Also we want to make
        # sure that no webhook is configured in case of polling, so we just always call
        # delete_webhook for polling
        if drop_pending_updates or not webhook_url:
            self._network_loop_retry(
                bootstrap_del_webhook,
                bootstrap_onerr_cb,
                'bootstrap del webhook',
                bootstrap_interval,
            )
            retries[0] = 0

        # Restore/set webhook settings, if needed. Again, we don't know ahead if a webhook is set,
        # so we set it anyhow.
        if webhook_url:
            self._network_loop_retry(
                bootstrap_set_webhook,
                bootstrap_onerr_cb,
                'bootstrap set webhook',
                bootstrap_interval,
            )

    async def stop(self) -> None:
        """Stops the polling/webhook thread, the dispatcher and the job queue."""
        with self.__lock:
            if self.running or (self.dispatcher and self.dispatcher.has_running_threads):
                self.logger.debug(
                    'Stopping Updater %s...', 'and Dispatcher ' if self.dispatcher else ''
                )

                self.running = False

                self._stop_httpd()
                self._stop_dispatcher()
                self._join_threads()

                # Clear the connection pool only if the bot is managed by the Updater
                # Otherwise `dispatcher.stop()` already does that
                if not self.dispatcher:
                    await self.bot.shutdown()

    @no_type_check
    def _stop_httpd(self) -> None:
        if self.httpd:
            self.logger.debug(
                'Waiting for current webhook connection to be '
                'closed... Send a Telegram message to the bot to exit '
                'immediately.'
            )
            self.httpd.shutdown()
            self.httpd = None

    @no_type_check
    def _stop_dispatcher(self) -> None:
        if self.dispatcher:
            self.logger.debug('Requesting Dispatcher to stop...')
            self.dispatcher.stop()

    @no_type_check
    def _join_threads(self) -> None:
        for thr in self.__threads:
            self.logger.debug('Waiting for %s thread to end', thr.name)
            thr.join()
            self.logger.debug('%s thread has ended', thr.name)
        self.__threads = []

    @no_type_check
    def _signal_handler(self, signum, frame) -> None:
        self.is_idle = False
        if self.running:
            self.logger.info(
                'Received signal %s (%s), stopping...',
                signum,
                # signal.Signals is undocumented for some reason see
                # https://github.com/python/typeshed/pull/555#issuecomment-247874222
                # https://bugs.python.org/issue28206
                signal.Signals(signum),  # pylint: disable=no-member
            )
            self.stop()
            if self.user_signal_handler:
                self.user_signal_handler(signum, frame)
        else:
            self.logger.warning('Exiting immediately!')
            # pylint: disable=import-outside-toplevel, protected-access
            import os

            os._exit(1)

    def idle(
        self, stop_signals: Union[List, Tuple] = (signal.SIGINT, signal.SIGTERM, signal.SIGABRT)
    ) -> None:
        """Blocks until one of the signals are received and stops the updater.

        Args:
            stop_signals (:obj:`list` | :obj:`tuple`): List containing signals from the signal
                module that should be subscribed to. :meth:`Updater.stop()` will be called on
                receiving one of those signals. Defaults to (``SIGINT``, ``SIGTERM``, ``SIGABRT``).

        """
        for sig in stop_signals:
            signal.signal(sig, self._signal_handler)

        self.is_idle = True

        while self.is_idle:
            sleep(1)


from sys import exit

from kojak import cli
from kojak.exceptions import KojakException
from kojak.models import Analyze
from kojak.reports import Report

from pbr.version import VersionInfo

def main():
    args = cli.argparser().parse_args()
    version = VersionInfo("kojak")
    if args.version:
        print("kojak v{version}".format(version=version))
        return 0
    filename = args.file
    if not filename:
        return 0

    try:
        analyze = Analyze(filename)
    except KojakException as err:
        print(str(err))
        return 1
    else:
        report = Report()
        report.rendering("cli.txt", analyze)
    footer = "Generated by kojak v{version}".format(version=version)
    print("-" * len(footer))
    print(footer)
    print("-" * len(footer))
    return 0

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


import os
from shutil import copyfile
from typing import List, TYPE_CHECKING

import jinja2
from ..checks.issues import Issues
from ..checks.file_size import FileSizeCheck
from .utils import create_parent_directory
from ..checks.base import Check

if TYPE_CHECKING:
    from combine.core import ContentDirectory

class File:
    def __init__(self, path: str, content_directory: "ContentDirectory") -> None:
        self.path = path
        self.references: List[str] = []
        self.content_directory = content_directory
        self.content_relative_path = os.path.relpath(
            self.path, self.content_directory.path
        )

        self.root, self.extension = os.path.splitext(self.content_relative_path)
        self.root_parts = os.path.split(self.root)
        self.name_without_extension = self.root_parts[-1]

        self.output_relative_path = self._get_output_relative_path()

    def __repr__(self) -> str:
        return f"<{self.__class__.__name__} {self.path}>"

    def _get_output_relative_path(self) -> str:
        return self.content_relative_path

    def load(self, jinja_environment: jinja2.Environment) -> None:
        """Load properties that can vary depending on content of the file"""
        self.references = []

    def render(self, output_path: str, jinja_environment: jinja2.Environment) -> None:
        self.output_path = self._render_to_output(output_path, jinja_environment)

    def _render_to_output(
        self, output_path: str, jinja_environment: jinja2.Environment
    ) -> str:
        target_path = os.path.join(output_path, self.output_relative_path)
        create_parent_directory(target_path)

        if os.path.exists(target_path):
            os.remove(target_path)

        copyfile(self.path, target_path)

        return target_path

    def check_output(self) -> Issues:
        issues = Issues()

        for check in self.get_checks():
            for issue in check.run():
                issues.append(issue)

        if issues:
            issues.print(f"Issues in {self.content_relative_path}")

        return issues

    def get_checks(self) -> List[Check]:
        if self.output_path:
            # Not all files have an output
            return [
                FileSizeCheck(path=self.output_path),
            ]

        return []


from selenium.webdriver.common.by import By
from lib import Lib
from time import sleep
import pyautogui

class TestsPage(Lib):
    'Coloquem os localizadores dos elementos aqui, vai facilitar!'
    URL = 'https://undbclassroom.undb.edu.br'
    __LOGIN_DIRETO_URL = 'https://undbclassroom.undb.edu.br/login/index.php#'
    __URL_ICON = 'https://cdn-icons-png.flaticon.com/512/1521/1521260.png'
    __EMAIL_TEXTBOX =  By.ID, 'username'
    __SENHA_TEXTBOX = By.ID, 'password'
    __ACESSAR_BUTTON = By.CLASS_NAME, 'btn-primary'
    __ESQUECEU_SENHA_BUTTON = By.XPATH, '//a[contains(text(), "Esqueceu o seu usuário ou senha?")]'
    __ID_USUARIO_TEXTBOX = By.ID, 'id_username'
    __BUSCAR_BUTTON = By.NAME, 'submitbuttonusername'
    __ID_EMAIL_TEXTBOX = By.ID, 'id_email'
    __ACESSAR_TEIA = By.XPATH, '//*[@id="page"]/div[2]/div/div/div[1]/div/a'
    __ACESSAR_PORTAL = By.XPATH, '//*[@id="page"]/div[2]/div/div/div[2]/div/a'
    __ACESSAR_SITE = By.XPATH, '//*[@id="page"]/div[2]/div/div/div[3]/div/a'
    __LEMBRAR_USUARIO = By.NAME, "rememberusername"
    __ACESSAR_LINK_PLAYSTORE = By.XPATH, '//*[@id="top-footer"]/div/div/div[1]/h4/a[1]/img'
    __CALENDARIO = By.XPATH, '//*[@id="nav-drawer"]/ul/li[3]/a'
    __SETA_CALENDARIO = By.CLASS_NAME, 'arrow_text'
    __ACESSAR_LINK_APPSTORE = By.XPATH, '//*[@id="top-footer"]/div/div/div[1]/h4/a[2]/img'
    __ACESSAR_EMAIL_INSTITUCIONAL = By.XPATH, '//*[@id="boxForm"]/div/div/div/a'
    __ACESSAR_BARRA_OPCOES = By.XPATH, '//*[@id="action-menu-toggle-1"]'
    __ACESSSAR_SAIR = By.XPATH, '//*[@id="action-menu-1-menu"]/a[7]'
    __EMAIL_GOOGLE = By.ID, 'identifierId'
    __BOTAO_PROXIMO = By.XPATH, '//*[@id="identifierNext"]/div/button'
    __DISCIPLINA_TESTAGEM = By.XPATH, '//*[@id="frontpage-course-list"]/div/div[1]/div[2]/div[3]/div/a'
    __CLICK_SARAIVA = By.XPATH, '//*[@id="module-120666"]/div/div/div[2]/div/a'
    __CLICK_PEARSON = By.XPATH, '//*[@id="module-120667"]/div/div/div[2]/div'
    __EMAIL_NAV = By.CLASS_NAME, 'fa-envelope'
    __NEW_EMAIL = By.CLASS_NAME, 'mail-navbar-menu-compose-link'
    __SEND_EMAIL = By.NAME, 'send'
    __ERROR_DESTINATARIO = By.ID, 'id_error_recipients'
    __PESQUISA_SATISFACAO = By.XPATH, '//*[@id="module-120665"]/div/div/div[2]/div/a/span'
    __RESPONDA_PESQUISA = By.XPATH, '//*[@id="region-main"]/div[1]/div/div/div[2]/div/a'
    __SIM_BUTTTON = By.ID, 'id_multichoice_16594_1'
    __NAO_BUTTTON = By.ID, 'id_multichoice_16594_2'
    __NOTIFICACOES = By.CLASS_NAME, 'slicon-bell'
    __PREFERENCIAS_NOTIFICACOES = By.CLASS_NAME, 'slicon-settings'
    __NOTIFICAO_WEB = By.CLASS_NAME, 'preference-state-status-container'
    __AV_QUALIS = By.XPATH, '//span[contains(text(), "Av Qualis - Produto")]'
    __ADICIONAR_ENVIO = By.XPATH, '//button[contains(text(), "Adicionar envio")]'
    __SALVAR_ENVIO = By.NAME, 'submitbutton'
    __ALERTA_ENVIO = By.CLASS_NAME, 'alert-danger'
    __PERFIL_USER = By.XPATH, '//*[@id="action-menu-toggle-1"]/span/span[2]/span/img'
    __PREFERENCIAS_USER = By.XPATH, '//*[@id="action-menu-1-menu"]/a[6]'
    __MODIFICAR_PERFIL = By.XPATH, '//*[@id="region-main"]/div/div/div/div/div[1]/div/div/div/div[1]/a'
    __SOLTAR_IMAGEM = By.CLASS_NAME, 'dndupload-arrow'
    __UTILIZAR_URL = By.XPATH, '//span[contains(text(), "Utilizar uma URL")]'
    __DOWNLOAD_IMAGE = By.CLASS_NAME, 'fp-login-submit'
    __IMAGEM = By.CLASS_NAME, 'fp-reficons2'
    __FILL_URL = By.XPATH, '//input[@id="fileurl"]'
    __ATV__TESTAGEM = By.XPATH, '//*[@id="module-122898"]/div/div/div[2]/div/a/span'
    __TEST_SEND_AGAIN = By.XPATH, '//*[@id="yui_3_17_2_1_1632675935201_861"]/a[1]/div[1]/div[3]'
    __NOTIFICATION_CHOICE = By.XPATH, '//*[@id="popover-region-container-6150a8ed2ee9f6150a8ed05dd017"]/div[2]/div/div[1]/div[1]/a[1]/div[1]/div[2]'
    __SEARCH_COURSE = By.XPATH, '//*[@id="shortsearchbox"]'
    __GO_TO_COURSE = By.XPATH, '//*[@id="coursesearch"]/fieldset/button'
    __TOTURIAL_COURSE = By.XPATH, '//*[@id="page"]/div[2]/div/div/div[4]/div/a'
    __NIVELAMENTO = By.XPATH, '//*[@id="module-120647"]/div/div/div[2]/div/a/span'
    __NIVELAMENTO_CHECK = By.XPATH, '//*[@id="q10210:3_answer0"]'
    __UNIDADE_APRENDIZADO = By.XPATH, '//*[@id="section-4"]/div/div[1]/div[2]/button'
    __CONF_ACESS = By.XPATH, '//*[@id="themesettings-control"]/span'
    __XPATH_CX_DISLX = By.XPATH, '//*[@id="fonttype"]'
    __XPATH_DISLX_SOP = By.XPATH, '//*[@id="fonttype"]/option[2]'
    __CLASS_PERFIL = By.CLASS_NAME, 'userpicture defaultuserpic'
    __XPATH_PREFERENCIAS = By.XPATH, '//*[@id="actionmenuaction-6"]'
    __XPATH_IDIOMA_PREFERIDO = By.XPATH, '//*[@id="region-main"]/div/div/div/div/div[1]/div/div/div/div[3]/a'
    __XPATH_CX_IDIOMA = By.XPATH, '//*[@id="id_lang"]'
    __XPATH_INGLES = By.XPATH, '//*[@id="id_lang"]/option[1]'
    __NAME_SAVE_IDIOMA = By.NAME, 'Salvar mudanças'
    __XPATH_ICON_MSG = By.XPATH, '//*[@id="message-drawer-toggle-614f5910d1996614f5910a546218"]'
    __XPATH_MSG_FAV = By.XPATH, '//*[@id="yui_3_17_2_1_1632590098721_72"]'
    __XPATH_MSG_SELF_USER = By.XPATH, '//*[@id="view-overview-favourites-target-614f5910d6a43614f5910a546229"]/div[2]/a/div[1]/p'
    __TESTE_SELF_TEXTBOX_ = By.XPATH, '//*[@id="message-drawer-614f5910d6a43614f5910a546229"]/div[3]/div[1]/div[1]/div[2]/textarea'
    __ICON_BARRA_LATERAL = By.CLASS_NAME, 'btn nav-link float-sm-left mr-1'
    __LINK_GEST_QUALD_SFT = By.LINK_TEXT, 'https://undbclassroom.undb.edu.br/course/view.php?id=3835'
    __ICON_PARTICIPANTES = By.LINK_TEXT, 'https://undbclassroom.undb.edu.br/user/index.php?id=3835'
    __LINK_MANUAL_DO_CASE = By.XPATH, '//*[@id="module-29817"]/div/div/div[2]/div/a/span'
    __XPATH_DOWNLOAD_CASE_PAPPER = By.XPATH, '//*[@id="icon"]/iron-icon'
    __LINK_MANUAL_PAPPER = By.XPATH, '//*[@id="module-29818"]/div/div/div[2]/div/a/span'
    __ICON_ID_NOTAS = By.ID, 'actionmenuaction-4'

    def __init__(self, driver):
        self.driver = driver

    def ct_0001(self, user, password):
        #Teste de login
        self.open_page(self.URL)
        self.fill(self.__EMAIL_TEXTBOX, user)
        self.fill(self.__SENHA_TEXTBOX, password)
        self.click(self.__ACESSAR_BUTTON)
        text = 'UNDB Classroom'
        assert text in self.driver.title

    def ct_0002(self, user):
        #Alteração de usuário ou senha pela identificação do usuário
        self.open_page(self.URL)
        self.click(self.__ESQUECEU_SENHA_BUTTON)
        self.fill(self.__ID_USUARIO_TEXTBOX, user)
        self.click(self.__BUSCAR_BUTTON)
        text = 'Se o usuário e o email estiverem corretos um email deve ter sido enviado a você.'
        assert text in self.driver.title

    def ct_0003(self, email):
        #Alteração de usuário ou senha pelo email institucional
        self.open_page(self.URL)
        self.click(self.__ESQUECEU_SENHA_BUTTON)
        self.fill(self.__ID_EMAIL_TEXTBOX, email)
        self.click(self.__BUSCAR_BUTTON)
        text = 'Se o usuário e o email estiverem corretos um email deve ter sido enviado a você.'
        assert text in self.driver.title

    def ct_0004(self):
        #Alteração de Gravatar
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__PERFIL_USER)
        self.click(self.__PREFERENCIAS_USER)
        self.click(self.__MODIFICAR_PERFIL)
        self.click(self.__SOLTAR_IMAGEM)
        self.click(self.__UTILIZAR_URL)
        self.click(self.__FILL_URL)
        self.fill(self.__FILL_URL, self.__URL_ICON)
        self.click(self.__DOWNLOAD_IMAGE)
        result = self.driver.find_element(*self.__IMAGEM).is_enabled()
        assert result is True

    def ct_0005(self):
        #Biblioteca Online Saraiva
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)
        self.click(self.__CLICK_SARAIVA)
        text = 'Saraiva Online'
        assert text in self.driver.title

    def ct_0006(self):
        #Biblioteca Online Pearson
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)
        self.click(self.__CLICK_PEARSON)
        text = 'Pearson'
        assert text in self.driver.title

    def ct_0007(self):
        #Envio de email sem destinatário
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)
        self.click(self.__EMAIL_NAV)
        self.click(self.__NEW_EMAIL)
        self.click(self.__SEND_EMAIL)
        result = self.driver.find_element(*self.__ERROR_DESTINATARIO).is_enabled()
        assert result is True

    def ct_0008(self):
        #Teste de múltipla escolha. marcando apenas 1 alternativa
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)
        self.click(self.__PESQUISA_SATISFACAO)
        self.click(self.__RESPONDA_PESQUISA)
        self.click(self.__SIM_BUTTTON)
        self.click(self.__NAO_BUTTTON)
        result = self.driver.find_element(*self.__SIM_BUTTTON).is_selected()
        assert result is False

    def ct_0009(self):
        #Desativar alguma notificação
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__NOTIFICACOES)
        self.click(self.__PREFERENCIAS_NOTIFICACOES)
        buttons = self.driver.find_elements(*self.__NOTIFICAO_WEB)
        buttons[0].click()
        result = buttons[0].is_enabled()
        assert result is True

    def ct_0010(self):
        #Verificação de sucesso no envio
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)
        self.click(self.__AV_QUALIS)
        self.click(self.__ADICIONAR_ENVIO)
        self.click(self.__SALVAR_ENVIO)
        result = self.driver.find_element(*self.__ALERTA_ENVIO).is_enabled()
        assert result is True

    def ct_0011(self):
        #Verificação de sucesso no envio
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)
        self.click(self.__AV_QUALIS)
        self.click(self.__ADICIONAR_ENVIO)
        self.click(self.__SALVAR_ENVIO)
        result = self.driver.find_element(*self.__ALERTA_ENVIO).is_enabled()
        assert result is False

    def ct_0012(self):
        #Verificação de sucesso no envio
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)

        self.click(self.__ATV__TESTAGEM)
        self.click(self.__ADICIONAR_ENVIO)
        self.click(self.__SALVAR_ENVIO)
        result = self.driver.find_element(*self.__ALERTA_ENVIO).is_enabled()

        assert result is True

    def ct_0013(self):
        #Verificação de sucesso no envio
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)

        self.click(self.__ATV__TESTAGEM)
        self.click(self.__ADICIONAR_ENVIO)
        self.click(self.__TEST_SEND_AGAIN)

        self.click(self.__ADICIONAR_ENVIO)
        self.click(self.__SALVAR_ENVIO)
        result = self.driver.find_element(*self.__ALERTA_ENVIO).is_enabled()

        assert result is True

    def ct_0014(self):
        #Verificação de sucesso no envio
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__ICON_NOTFICATION)

    def ct_0015(self):
        #Verificação de sucesso no envio
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__ICON_NOTFICATION)
        sleep(2)
        self.click(self.__NOTIFICATION_CHOICE)

    def ct_0016(self):
        #Verificação de sucesso no envio
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.fill(self.__SEARCH_COURSE, 'Testagem')
        self.click(self.__GO_TO_COURSE)

    def ct_0017(self):
        #Verificação de sucesso no envio
        self.open_page(self.URL)
        self.click(self.__TOTURIAL_COURSE )

    def ct_0018(self):
        #Verificação de sucesso no envio
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)
        self.click(self.__NIVELAMENTO)

    def ct_0019(self):
        #Verificação de sucesso no envio
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)
        self.click(self.__NIVELAMENTO)
        self.click(self.__NIVELAMENTO_CHECK)

    def ct_0020(self):
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)
        self.click(self.__UNIDADE_APRENDIZADO)

    def ct_0021(self):
        #Redirecionamento do link do site TEIA
        self.open_page(self.URL)
        sleep(2)
        self.click(self.__ACESSAR_TEIA)
        text = 'Espaço Teia'
        assert text in self.driver.title

    def ct_0022(self,):
        #Redirecionamento do link do site do portal academico
        self.open_page(self.URL)
        sleep(2)
        self.click(self.__ACESSAR_PORTAL)
        text = 'Portal do Aluno'
        assert text in self.driver.title

    def ct_0023(self,):
        #Redirecionamento do link do site da UNDB
        self.open_page(self.URL)
        sleep(2)
        self.click(self.__ACESSAR_SITE)
        text = 'Sou UNDB'
        assert text in self.driver.title

    def ct_0024(self,email):
        #Testagem de e-mail institucional
        self.ct_0029()
        self.fill(self.__EMAIL_GOOGLE, email)
        result = self.driver.find_element(*self.__BOTAO_PROXIMO).is_enabled()
        assert result is True

    def ct_0025(self,):
        #Checagem de identificação de usuário
        self.open_page(self.__LOGIN_DIRETO_URL)
        self.click(self.__LEMBRAR_USUARIO)
        result = self.driver.find_element(*self.__LEMBRAR_USUARIO).is_selected()
        assert result is True

    def ct_0026(self,):
        #Redirecionamento do site da google play store
        self.open_page(self.URL)
        self.click(self.__ACESSAR_LINK_PLAYSTORE)
        text = 'UNDB Classroom'
        assert text in self.driver.title

    def ct_0027(self,):
        #Chave de eventos e visualização mensal
        self.ct_0001('user', 'password')
        self.click(self.__CALENDARIO)
        result = self.driver.find_element(*self.__SETA_CALENDARIO).is_enabled()
        assert result is True

    def ct_0028(self,):
        #Redirecionamento do site da app store
        self.open_page(self.URL)
        self.click(self.__ACESSAR_LINK_APPSTORE)
        text = 'UNDB Classroom'
        assert text in self.driver.title

    def ct_0029(self,):
        #Testagem de login com e-mail institucional
        self.open_page(self.URL)
        self.click(self.__ACESSAR_EMAIL_INSTITUCIONAL)
        text = 'Fazer login nas Contas do Google'
        assert text in self.driver.title

    def ct_0030(self,):
        #Testagem caixa de comando "sair"
        self.ct_0001('user', 'password')
        self.click(self.__ACESSAR_BARRA_OPCOES)
        self.click(self.__ACESSSAR_SAIR )
        text = 'UNDB Classroom'
        assert text in self.driver.title

    def ctt_0031(self):
        #TESTAGEM DE FONTE DE DISLEXIA
        self.ct_0001('user', 'password')
        self.click(self.__CONF_ACESS)
        self.click(self.__XPATH_CX_DISLX)
        self.click(self.__XPATH_DISLX_SOP)
        self.click(self.__XPATH_CX_DISLX)

    def ctt_0032(self):
        #SALVAMENTO DA FONTE DE DISLEXIA
        self.ct_0001('user', 'password')
        self.click(self.__CONF_ACESSIBILIDADE)
        self.click(self.__XPATH_CX_DISLX)
        sleep(3)
        self.click(self.__XPATH_DISLX_SOP)
        sleep(3)
        self.click(self.__XPATH_CX_DISLX)
        sleep(2)
        pyautogui.moveTo(1280, 394)
        pyautogui.doubleClick()

    def ctt_0033(self):
        #TESTAGEM MUDAR IDIOMA DO SISTEMA
        self.ct_0001('user', 'password')
        self.click(self.__CLASS_PERFIL)
        self.click(self.__XPATH_PREFERENCIAS)
        self.click(self.__XPATH_IDIOMA_PREFERIDO)
        self.click(self.__XPATH_CX_IDIOMA)
        self.click(self.__XPATH_INGLES)
        self.click(self.__XPATH_CX_IDIOMA)
        self.click(self.__NAME_SAVE_IDIOMA)

    def ctt_0034(self, test_text):
        #TESTAR ENVIAR MENSAGEM PARA SI MESMO
        self.ct_0001('user', 'password')
        self.click(self.__XPATH_ICON_MSG)
        self.click(self.__XPATH_MSG_FAV)
        self.click(self.__XPATH_MSG_SELF_USER)
        self.fill(self.__TESTE_SELF_TEXTBOX_, test_text)

    def ctt_0035(self):
        #TESTAGEM DE MENU LATERAL
        self.ct_0001('user', 'password')
        self.click(self.__ICON_BARRA_LATERAL)

    def ctt_0036(self):
        #ACESSO AOS PARTICIPANTES DA DISCIPLINA
        self.ct_0001('user', 'password')
        self.click(self.__LINK_GEST_QUALD_SFT)
        self.click(self.__ICON_BARRA_LATERAL)
        self.click(self.__ICON_PARTICIPANTES)

    def ctt_0037(self):
        #PREFERÊNCIAS DE USUÁRIO
        self.ct_0001('user', 'password')
        self.click(self.__CLASS_PERFIL)
        self.click(self.__XPATH_PREFERENCIAS)

    def ctt_0038(self):
        #DOWNLOAD MANUAL DO CASE
        self.ct_0001('user', 'password')
        self.click(self.__LINK_MANUAL_DO_CASE)
        self.click(self.__XPATH_DOWNLOAD_CASE_PAPPER)
        pyautogui.press('Enter')

    def ctt_0039(self):
        #DOWNLOAD DO MANUAL DO PAPPER
        self.ct_0001('user', 'password')
        self.click(self.__LINK_MANUAL_PAPPER)
        self.click(self.__XPATH_DOWNLOAD_CASE_PAPPER)
        pyautogui.press('Enter')

    def ctt_0040(self):
        #VISUALIZAÇÃO DE NOTAS
        self.ct_0001('user', 'password')
        self.click(self.__CLASS_PERFIL)
        self.click(self.__ICON_ID_NOTAS)


import os
import sys
import math
import argparse

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

import matplotlib
matplotlib.use('Agg')
import numpy as np

from models import *
import datasets as dsets
from datasets import PairwiseDataset

models = {
    'cyclegan': CycleGAN,
    'unit': UNIT
}

def load_data(data_type):
    if data_type == 'mnist':
        return dsets.mnist.load_data()
    elif data_type == 'svhn':
        return dsets.svhn.load_data()
    else:
        return dsets.load_data(data_type)

def main():
    # Parsing arguments
    parser = argparse.ArgumentParser(description='Training GANs or VAEs')
    parser.add_argument('--model', type=str, required=True)
    parser.add_argument('--first-data', type=str, required=True)
    parser.add_argument('--second-data', type=str, required=True)
    parser.add_argument('--epoch', type=int, default=500)
    parser.add_argument('--batchsize', type=int, default=50)
    parser.add_argument('--output', default='output')
    parser.add_argument('--zdims', type=int, default=128)
    parser.add_argument('--gpu', type=int, default=0)
    parser.add_argument('--resume', type=str, default=None)
    parser.add_argument('--testmode', action='store_true')

    args = parser.parse_args()

    # select gpu
    os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu)

    # Make output direcotiry if not exists
    if not os.path.isdir(args.output):
        os.mkdir(args.output)

    # Load datasets
    x_data = load_data(args.first_data)
    x_data = x_data.images * 2.0 - 1.0
    y_data = load_data(args.second_data)
    y_data = y_data.images * 2.0 - 1.0

    datasets = PairwiseDataset(x_data, y_data)
    num_data = len(datasets)

    # Construct model
    if args.model not in models:
        raise Exception('Unknown model:', args.model)

    model = models[args.model](
        input_shape=datasets.shape[1:],
        z_dims=args.zdims,
        output=args.output
    )

    if args.resume is not None:
        model.load_model(args.resume)

    # Make samples
    x_samples = datasets.x_data[num_data:num_data+25]
    y_samples = datasets.y_data[num_data:num_data+25]
    samples = (x_samples, y_samples)

    # Training loop
    model.main_loop(datasets, samples,
        epochs=args.epoch,
        batchsize=args.batchsize,
        reporter=['loss', 'g_loss', 'd_loss', 'g_acc', 'd_acc'])

if __name__ == '__main__':
    main()


"""Tests for pydent.base.py."""
import copy

import pytest

from pydent import AqSession
from pydent import ModelBase
from pydent import ModelRegistry
from pydent.exceptions import NoSessionError
from pydent.marshaller import add_schema
from pydent.marshaller import exceptions
from pydent.marshaller import fields
from pydent.marshaller import SchemaRegistry

# def test_model_base():
#     """
#     Upon instantiation, .session should be None
#     """
#     m = ModelBase()
#     assert m.session is None

@pytest.fixture(scope="function")
def base():
    old_schemas = dict(SchemaRegistry.schemas)
    old_models = dict(ModelRegistry.models)

    yield ModelBase

    new_schemas = set(SchemaRegistry.schemas.keys()).difference(set(old_schemas))
    new_models = set(ModelRegistry.models.keys()).difference(set(old_models))

    for s in new_schemas:
        SchemaRegistry.schemas.pop(s)

    for m in new_models:
        ModelRegistry.models.pop(m)

@pytest.fixture(scope="function")
def mymodel(base):
    @add_schema
    class MyModel(base):
        pass

    yield MyModel

def test_record_id():
    """Creating a ModelBase should create a new record id 'rid.' For each
    instance, a new 'rid' should be created."""

    @add_schema
    class MyModel(ModelBase):
        pass

    @add_schema
    class MyOtherModel(ModelBase):
        pass

    m = MyModel()
    m2 = MyOtherModel()
    m3 = MyModel()
    assert m.rid != m2.rid
    assert m2.rid != m3.rid
    assert m.rid != m3.rid

def test_deepcopy():
    """Deepcopy should retain attributes exactly."""

    @add_schema
    class MyModel(ModelBase):
        pass

    m = MyModel()
    copied = copy.deepcopy(m)
    assert m.rid == copied.rid

@pytest.mark.parametrize(
    "copy_method",
    [pytest.param(lambda x: x.copy()), pytest.param(lambda x: copy.copy(x))],
)
def test_copy(copy_method):
    """Copy should anonymize models."""

    @add_schema
    class MyModel(ModelBase):
        def __init__(self, id):
            super().__init__(id=id)

    m = MyModel(5)
    copied = copy_method(m)
    assert m.rid != copied.rid
    assert copied.id is None

def test_copy_anonymizes_nested_relationships():
    """Copy should recursively anonymize all models."""

    @add_schema
    class MyModel(ModelBase):
        def __init__(self, id):
            super().__init__(id=id)

    @add_schema
    class MyOtherModel(ModelBase):
        def __init__(self, id):
            super().__init__(id=id)

    m = MyModel(1)
    m2 = MyOtherModel(2)
    m3 = MyModel(3)
    m3.other = m2
    m2.other = m

    rid1 = m.rid
    rid2 = m2.rid
    rid3 = m3.rid

    copied = m3.copy()
    assert copied.id is None
    assert copied.other.id is None
    assert copied.other.other.id is None

    assert copied.rid != rid1
    assert copied.other.rid != rid2
    assert copied.other.other.rid != rid3

    assert m3.rid == rid3
    assert m3.other.rid == rid2
    assert m3.other.other.rid == rid1

def test_basic_constructor(mymodel):
    """Model should absorb the kwargs."""
    m = mymodel(name="SomeName", id=2)
    assert m.name == "SomeName"
    assert m.id == 2
    data = m.dump()
    data.pop("rid")
    assert data == {"name": "SomeName", "id": 2}

def test_base_constructor_with_marshaller(mymodel):
    """MyModel initializes should absorb kwargs into attributes.

    With a schema, those attributes are also tracked and available for
    dumping.
    """
    m = mymodel(name="model", id=5)
    assert m.name == "model"
    assert m.id == 5
    mdump = m.dump()
    del mdump["rid"]
    assert mdump == {"name": "model", "id": 5}

def test_connect_to_session(mymodel, fake_session):
    """Connecting to other sessions afterward should not be allowed."""
    m = mymodel()
    assert m.session is None

    # connect to session
    m.connect_to_session(fake_session)
    assert m.session == fake_session

    # attempt to connect to another session
    fake_session2 = copy.copy(fake_session)

    with pytest.raises(Exception):
        m.connect_to_session(fake_session2)

def test_empty_relationships(mymodel):
    m = mymodel()
    assert m.get_relationships() == {}

def test_check_for_session(mymodel, fake_session):
    """If session is none, _check_for_session should raise an
    AttributeError."""
    m = mymodel()
    with pytest.raises(NoSessionError):
        m._check_for_session()

    m.connect_to_session(fake_session)
    m._check_for_session()

def test_model_registry(mymodel):
    """We expect get_model to return the value in the models dictionary."""

    assert "MyModel" in ModelRegistry.models
    assert ModelRegistry.models["MyModel"] == mymodel
    assert ModelRegistry.get_model("MyModel") == mymodel
    del ModelRegistry.models["MyModel"]

def test_no_model_in_registry():
    """ModelRegistry should raise error if model doesn't exist."""
    with pytest.raises(exceptions.ModelRegistryError):
        ModelRegistry.get_model("SomeModelThatDoesntExist")

def test_find_no_session(mymodel):
    """ModelBase should raise AttributeError if no session is attacheded."""
    m = mymodel()
    with pytest.raises(NoSessionError):
        m.find_callback(None, None)

def test_where_no_session(mymodel):
    """ModelBase should raise AttributeError if no session is attacheded."""
    m = mymodel()
    with pytest.raises(NoSessionError):
        m.where_callback(None, None)

def test_where_and_find(mymodel, monkeypatch, fake_session):
    """Calling the 'where' wrapper on a ModelBase should attempt to get a model
    interface and call 'where' or 'find' on the interface.

    In this case, a fake model interface is returned in which the
    methods return the parameter passed in
    """

    def fake_model_interface(self, model_name):
        """A fake model interface to test where."""

        class FakeInterface:
            def find(id):
                return id

            def where(params):
                return params

        return FakeInterface

    monkeypatch.setattr(
        AqSession, AqSession.model_interface.__name__, fake_model_interface
    )

    m = mymodel()
    ModelRegistry.models["FakeModel"] = ModelBase
    m.connect_to_session(fake_session)
    assert m.where_callback("FakeModel", 5) == 5
    assert m.find_callback("FakeModel", 6) == 6

def test_print(mymodel):
    m = mymodel()
    print(m)
    m.print()

def test_load_many(base, fake_session):
    @add_schema
    class Child(base):
        pass

    @add_schema
    class Parent(base):
        fields = dict(children=fields.Relationship("Child", "get_children", many=True))

        def get_children(self, model_name):
            return None

    parent = Parent.load_from(
        {"id": 10, "children": [{"id": 1, "name": "Child1"}, {"id": 2}]},
        fake_session.utils,
    )
    print(parent.children)
    assert len(parent.children) == 2
    assert isinstance(parent.children[0], Child)
    assert parent.children[0].name == "Child1"

def test_uri(base):
    """Expect with with the `include_uri` key includes the default URI."""

    @add_schema
    class MyModel(base):
        pass

    model = MyModel()
    model.id = 10
    assert model.uri == "http://aquarium.org/my_models/10"

def test_dump_uri(base):
    """Expect with with the `include_uri` key includes the default URI."""

    @add_schema
    class MyModel(base):
        pass

    model = MyModel()
    model.id = 10
    no_uri = model.dump()
    with_uri = model.dump(include_uri=True)
    assert "__uri__" not in no_uri
    assert "__uri__" in with_uri
    assert with_uri["__uri__"] == "http://aquarium.org/my_models/10"

def test_dump_model(base):
    """Expect with with the `include_uri` key includes the default URI."""

    @add_schema
    class MyModel(base):
        pass

    model = MyModel()
    model.id = 10
    no_mt = model.dump()
    with_mt = model.dump(include_model_type=True)
    assert "__model__" not in no_mt
    assert "__model__" in with_mt
    assert with_mt["__model__"] == "MyModel"


import time
import random
import datetime
import sys
import numpy as np
from utils.parameters import parse_command_line
from utils.bayesian_optimization import *
from utils.default_params import *

np.set_printoptions(precision = 4, suppress = True)
np.set_printoptions(threshold=sys.maxsize)

###### TRAIN PARAMETERS ##################

args = parse_command_line()

seed = args.seed
depth = args.p
nwarmup = args.nwarmup
nbayes = args.nbayes
quantum_noise =  args.quantum_noise
type_of_graph = args.type_of_graph
lattice_spacing = args.lattice_spacing
verbose_ = args.verbose
kernel_choice = args.kernel
shots = args.shots
discard_percentage = args.discard_percentage

np.random.seed(seed)
random.seed(seed)

####### CREATE BAYES OPT INSTANCE ########
bo = Bayesian_optimization(depth,
                           type_of_graph,
                           lattice_spacing,
                           quantum_noise,
                           nwarmup,
                           nbayes,
                           kernel_choice,
                           shots,
                           discard_percentage,
                           seed,
                           verbose_
                           )
bo.print_info()
bo.init_training()
bo.run_optimization()