# -*- coding: utf-8 -*-
"""
Created on Sun May 31 12:27:01 2020

@author: jhr
"""
import json
import hashlib
import pickle
import random
import string
import shutil
import subprocess
import sys
from zipfile import ZipFile
from pathlib import Path
try:
    from flashtext import KeywordProcessor
except ModuleNotFoundError:
    print("installing flashtext...")
    subprocess.check_call([sys.executable, "-m", "pip", "install", "flashtext"])
    print("done!")
    from flashtext import KeywordProcessor

def flatten(l):
    return [item for sublist in l for item in sublist]

def read_json(file_path):
    with open(file_path, encoding="utf-8") as file:
        return json.load(file)

def pickle_list(lst, file_name):
    with open(file_name, "wb") as f:
        pickle.dump(lst, f)

def read_pickle(file_path):
    with open(file_path, "rb") as f:
        return pickle.load(f)

def find_all_messages(main_dir):
    """ returns iterator of all message paths """
    return main_dir.rglob("message_*.json")

def hash_name(name):
    """ simplified version (no salt) """
    return hashlib.sha1(name.encode()).hexdigest()

def check_name(hashed_name, new_name):
    return hashed_name == hash_name(new_name)

def is_a_cogsci(cogsci_hashes, new_name):
    return any(check_name(hsh, new_name) for hsh in cogsci_hashes)

def extract_participants(convo):
    return [participant["name"] for participant in convo["participants"]]

def is_cog_convo(participants, cog_hashes):
    """ Checks whether all participants in convo are cogsci19 """
    return any(check_name(hsh, name) for hsh in cog_hashes for name in participants)

def get_other_participant(lst, part):
    return next(filter(lambda x: x != part,  lst))

def format_subscribe(sub_msg):
    keep_keys = ["type", "timestamp_ms", "users"]
    return {k: v for k, v in sub_msg.items() if k in keep_keys}

def format_message(msg, participants):
    keep_keys = ["reactions", "sender_name", "timestamp_ms"]
    new_msg = {k: v for k, v in msg.items() if k in keep_keys}
    try:
        new_msg["reactions"] = format_reaction(new_msg["reactions"])
    except KeyError:
        pass
    if len(participants) == 2:
        new_msg["receiver_name"] = get_other_participant(participants, msg["sender_name"])
    return new_msg

def format_call(call, participants):
    assert call["type"] == "Call"
    new_call = call.copy()
    new_call.pop("content", None)
    if len(participants) == 2:
        new_call["receiver_name"] = get_other_participant(participants, call["sender_name"])
    return new_call

def format_reaction(reactions):
    """ simply removes the reaction leaving only the sender """
    return [reaction["actor"] for reaction in reactions]

def format_share(msg, participants):
    new_msg = format_message(msg, participants)
    try:
        new_msg.pop("share")
    except KeyError:
        pass
    return new_msg

def process_message(msg, participants):
    msg_type = msg["type"]
    if msg_type == "Generic":
        return format_message(msg, participants)
    elif msg_type == "Call":
        return format_call(msg, participants)
    elif msg_type == "Share":
        return format_share(msg, participants)
    elif msg_type == "Subscribe":
        pass
    elif msg_type == "Unsubscribe":
        pass
    else:
        print(msg)
        raise AssertionError

def create_hash_dict(participants):
    return {hash_name(name): [name] for name in participants}

def init_hashifier(participants):
    hash_dict = create_hash_dict(participants)
    hashifier = KeywordProcessor()
    hashifier.add_keywords_from_dict(hash_dict)
    return hashifier

def stringify_dict(dct):
    return json.dumps(dct, ensure_ascii=False)

def anonymize_stuff(processed_msgs, participants):
    hashifier = init_hashifier(participants)
    convo_string = stringify_dict(processed_msgs)
    anon_string = hashifier.replace_keywords(convo_string)
    return anon_string

def get_sub_users(sub_list):
    nested_list = [[user["name"] for user in subs["users"]] for subs in sub_list]
    return flatten(nested_list)

def get_all_participants(participants, subscriptions):
    """ get all participants including people who have left (host host Anne-Line!) """
    sub_participants = get_sub_users(subscriptions)
    return set(participants + sub_participants)

def process_convo(convo):
    participants = extract_participants(convo)
    if not is_cog_convo(participants, COG_HASHES):
        return None
    processed_msgs = []
    processed_subscriptions = []
    for msg in convo["messages"]:
        if msg["type"] == "Subscribe" or msg["type"] == "Unsubscribe":
            processed_subscriptions.append(format_subscribe(msg))
        else:
            processed_msgs.append(process_message(msg, participants))
    convo["messages"] = processed_msgs
    convo["participants"] = participants
    convo["subscriptions"] = processed_subscriptions
    convo.pop("title", None)
    convo.pop("is_still_participant", None)
    convo.pop("thread_path", None)
    # Use all participants for this stuff
    all_participants = get_all_participants(participants, processed_subscriptions)
    anon_convo = anonymize_stuff(convo, all_participants)
    return anon_convo

def random_long_id(N=32):
    return ''.join(random.choices(string.ascii_uppercase + string.digits, k=N))

def write_json_data(data):
    file_name = random_long_id() + ".json"
    file_path = Path("./data") / file_name
    with open(file_path, "w") as f:
        json.dump(data, f)

def anon_pipeline(convo_file):
    convo = read_json(convo_file)
    anon_convo = process_convo(convo)
    if anon_convo is not None:
        write_json_data(anon_convo)

def create_dir(path):
    TEMP_DATA = Path(path)
    try:
        TEMP_DATA.mkdir()
    except FileExistsError:
        pass
    return TEMP_DATA

def unzip_msg_files(zip_path, target_dir):
    with ZipFile(zip_path, 'r') as zipObj:
        # Get a list of all archived file names from the zip
        all_files = zipObj.namelist()
        for file in all_files:
            if file.endswith(".json"):
                zipObj.extract(file, target_dir)

is_dropout = input("""
Hello there! Thanks for helping us gather this awesome data :))
We'll do everything we can to ensure that your privacy is conserved.
All the processing is done automagically (you can check out the .zip-file afterwards)
We only have one question: Are you still attending the Cognitive Science Program? (If yes enter "1", if no enter "0")
""")

print("that's all! Now the program will do some magic :))")
print("setting up directories...")
create_dir("./data")
hash_path = Path("cogsci19.pkl")
COG_HASHES = read_pickle(hash_path)
zip_paths = list(Path(".").glob("facebook*.zip"))
temp_data = create_dir("./temp")

print("writing activity-status...")
with open(Path("./data/dropout.json"), "w") as f:
	json.dump({"is_dropout": is_dropout}, f)

print("finding messages in zip-file...")
if len(zip_paths) > 0:
    for zip_path in zip_paths:
        unzip_msg_files(zip_path, temp_data)
    print("done!")
    print("anonymizing the data....")
    for convo_file in find_all_messages(temp_data):
        anon_pipeline(convo_file)
    print("done!")

else:
    print("Mac has done the job for us!")
    print("anonymizing the data....")
    for convo_file in find_all_messages(Path("./messages")):
        anon_pipeline(convo_file)
    print("done!")

# Writing data to zip
print("write data to zip!")
shutil.make_archive(f"all_the_data_{random_long_id(N=8)}", 'zip', "data")
print("cleaning up directories...")
shutil.rmtree("data")
shutil.rmtree(temp_data)
print("all done!")


import argparse
import os

from fabric.api import local

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--src-s3')

    args = parser.parse_args()

    if args.src_s3:
        fname = os.path.split(args.src_s3)[1]
        local("aws s3 cp {} /tmp/{}".format(args.src_s3, fname))
        local("sudo pip-3.6 install /tmp/{}".format(fname))

if __name__ == "__main__":
    main()


import tweepy

consumer_key = 'xxxx'
consumer_secret = 'xxxx'
access_token = 'xxxx-xxxx'
access_token_secret = 'xxxx'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

DT_tweets = api.user_timeline('realDonaldTrump')

tweets = []
for tweet in DT_tweets:
	tweets.append({'time':tweet.created_at, 'text':tweet.text})

print(tweets)


from flask import Flask, make_response, send_from_directory, request
import os
import package
import json
app = Flask(__name__)

app.config["REPO_URL"] = "http://localhost:5000"

def setup():
    if(not os.path.isdir("./assets")):
        os.mkdir("./assets");
    if(not os.path.isfile("./assets/pkg_index.json")):
        f = open("./assets/pkg_index.json", "w")
        f.write('{"next_pkg_id": 0, "packages":[]}')
        f.close()

@app.route("/")
def hello():
    return "super secret don't look"

# v1 is used for the normal communication method where the server returns a list of all available packages
# v2 is the "direct" mode, where the client asks for once specific package

@app.route("/v1/download_release/<pkg_id>/<release_id>")
def get_file(pkg_id, release_id):
    resp = make_response(send_from_directory(f"./assets/{pkg_id}/{release_id}","file.jar"))
    f = open("./assets/pkg_index.json")
    j = json.loads(f.read())
    j['packages'][int(pkg_id)]['releases'][int(release_id)]['downloads'] += 1
    f = open("./assets/pkg_index.json", "w")
    f.write(json.dumps(j))
    f.close()

    return resp

@app.route("/v1/get_available_packages")
def get_available_packages():
    f = open("./assets/pkg_index.json")
    r = make_response(json.dumps(json.loads(f.read())['packages']))
    r.headers.set("Content-Type", "application/json")
    return r

@app.post("/v1/create_package")
def create_package_post():
    name = request.form.get("name", None)
    description = request.form.get("description", None);
    if name == None or description == None:
        return "Bad Request", 400

    package.Package.create_new("Very Cool Mod", "This is a very cool mod")
    return "OK", 200

@app.post("/v1/create_release")
def create_release_post():
    version = request.form.get("version", None)
    game_version = request.form.get("game_version", None)
    deps = request.form.get("deps", None)
    parent_package_id = int(request.form.get("parent_package_id", None))

    if version == None or game_version == None or deps == None or parent_package_id == None:
        return "Bad Request", 400
    deps = json.loads(deps)
    package.Release.create_new(version, game_version, deps, parent_package_id)
    return "OK", 200

@app.post("/v1/upload_release_file/<pkg_id>/<release_id>")
def upload_release_file_post(pkg_id, release_id):

    file = request.files["file"]
    file.save(f"./assets/{pkg_id}/{release_id}/file.jar")

    return "OK", 200

if __name__ == "__main__":
    setup()
    # of2 = package.Package.create_new("OptiFine2", "A performance enchancing mod but better")
    # package.Release.create_new("HD_U_G9", "1.16.5", [], of2.id)
    app.run()


from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from datetime import datetime, timedelta

chrome_driver_path = "C:\\Development\\chromedriver.exe"
chrome_service = Service(executable_path=chrome_driver_path)
driver = webdriver.Chrome(service=chrome_service)

driver.get('http://orteil.dashnet.org/experiments/cookie/')

cookie = driver.find_element(By.ID, "cookie")

game_end_time = datetime.now() + timedelta(minutes=5)
chronometer_end_time = datetime.now() + timedelta(seconds=1)
while datetime.now() < game_end_time:
    cookie.click()

    if datetime.now() >= chronometer_end_time:
        chronometer_end_time = datetime.now() + timedelta(seconds=3)  # Update 5 second timer.

        # Get items fom store again with updated prices.
        store = driver.find_elements(By.CSS_SELECTOR, "#store b")[:-1]
        store_prices = [int(item.text.split("- ")[-1].replace(",", "")) for item in store][::-1]

        store_clickable_items = driver.find_elements(By.CSS_SELECTOR, "#store div")[:-1]
        store_clickable_items = store_clickable_items[::-1]

        for idx, item_price in enumerate(store_prices):
            money = int(driver.find_element(By.ID, "money").text.replace(",", ""))
            if item_price <= money:
                store_clickable_items[idx].click()
                break

cookies_per_second = driver.find_element(By.ID, "cps").text
print(cookies_per_second)
driver.quit()


from collections import deque
from math import ceil

def solution(progresses, speeds):
    answer = []
    queue = deque()
    for idx, progress in enumerate(progresses):
        tmp = 0
        remain_work = 100 - progress
        required_day = ceil(remain_work / speeds[idx])

        if not queue:
            queue.append(required_day)
        else:
            while queue:
                if queue[0] < required_day:
                    tmp += 1
                    queue.popleft()
                else:
                    break
            queue.append(required_day)
        if tmp != 0:
            answer.append(tmp)
    if queue:
        answer.append(len(queue))

    return answer

if __name__ == "__main__":
    print(solution( [5, 5, 5], speeds=[21, 25, 20]))


from itertools import combinations
n = int(input())

f = [list(map(int, input().split())) for _ in range(n)]
p = [list(map(int, input().split())) for _ in range(n)]

li = [i for i in range(10)]
pro_count = -float('inf')

for i in range(1,11):
    iteration = list(combinations(li, i))
    for j in iteration:
        profit = 0
        for l in range(n):
            count = 0
            for k in j:
                if f[l][k]==1:
                    count += 1
            profit += p[l][count]
        pro_count = max(pro_count, profit)

print(pro_count)


import urllib.parse

import bs4
import copy
import hashlib
import os.path

import traceback
import WebRequest

from WebMirror.processor.ProcessorBase import PageProcessor
# import TextScrape.SiteArchiver

import common.util.urlFuncs as urlFuncs
import WebMirror.processor.ProcessorUtils.gDocParse as gdp

# import TextScrape.RelinkLookup
# import TextScrape.RELINKABLE as RELINKABLE

########################################################################################################################
#
#	##     ##    ###    #### ##    ##     ######  ##          ###     ######   ######
#	###   ###   ## ##    ##  ###   ##    ##    ## ##         ## ##   ##    ## ##    ##
#	#### ####  ##   ##   ##  ####  ##    ##       ##        ##   ##  ##       ##
#	## ### ## ##     ##  ##  ## ## ##    ##       ##       ##     ##  ######   ######
#	##     ## #########  ##  ##  ####    ##       ##       #########       ##       ##
#	##     ## ##     ##  ##  ##   ###    ##    ## ##       ##     ## ##    ## ##    ##
#	##     ## ##     ## #### ##    ##     ######  ######## ##     ##  ######   ######
#
########################################################################################################################

class GdocPageProcessor(PageProcessor):

	wanted_mimetypes = ['text/html']
	want_priority    = 90

	@staticmethod
	def wantsUrl(url):
		return urlFuncs.isGdocUrl(url)[0]

	loggerPath = "Main.Text.GdocPageProcessor"

	def __init__(self, pageUrl, pgContent, loggerPath, relinkable, scannedDomains=None, tlds=None, **kwargs):
		self.loggerPath = loggerPath+".GDocExtract"
		self.pageUrl    = pageUrl

		self._relinkDomains = set()
		for url in relinkable:
			self._relinkDomains.add(url)

		self._tld            = set()
		self._scannedDomains = set()

		# Tell the path filtering mechanism that we can fetch google doc files
		# Not switchable, since not fetching google docs content from a google docs page
		# wouldn't work too well.
		self._scannedDomains.add('https://docs.google.com/document/')
		self._scannedDomains.add('https://docs.google.com/spreadsheets/')
		self._scannedDomains.add('https://drive.google.com/folderview')
		self._scannedDomains.add('https://drive.google.com/open')

		if not scannedDomains:
			scannedDomains = []
		if not tlds:
			tlds = []

		# Build the filtering structures for checking outgoing links.
		for tld in tlds:
			self._tld.add(tld)

		if isinstance(scannedDomains, (set, list)):
			for url in scannedDomains:
				self.installBaseUrl(url)
		else:
			self.installBaseUrl(scannedDomains)

		# File mapping LUT
		self.fMap = {}

	def installBaseUrl(self, url):
		# print("Inserting ", url)
		netloc = urllib.parse.urlsplit(url.lower()).netloc
		if not netloc:
			raise ValueError("One of the scanned domains collapsed down to an empty string: '%s'!" % url)

		# Generate the possible wordpress netloc values.
		if 'wordpress.com' in netloc:
			subdomain, mainDomain, tld = netloc.rsplit(".")[-3:]

			self._scannedDomains.add("www.{sub}.{main}.{tld}".format(sub=subdomain, main=mainDomain, tld=tld))
			self._scannedDomains.add("{sub}.{main}.{tld}".format(sub=subdomain, main=mainDomain, tld=tld))
			self._scannedDomains.add("www.{sub}.files.{main}.{tld}".format(sub=subdomain, main=mainDomain, tld=tld))
			self._scannedDomains.add("{sub}.files.{main}.{tld}".format(sub=subdomain, main=mainDomain, tld=tld))

		# Blogspot is annoying and sometimes a single site is spread over several tlds. *.com, *.sg, etc...
		if 'blogspot.' in netloc:
			subdomain, mainDomain, tld = netloc.rsplit(".")[-3:]
			self._tld.add(tld)
			for tld in self._tld:
				self._scannedDomains.add("www.{sub}.{main}.{tld}".format(sub=subdomain, main=mainDomain, tld=tld))
				self._scannedDomains.add("{sub}.{main}.{tld}".format(sub=subdomain, main=mainDomain, tld=tld))

		if 'sites.google.com/site/' in url:
			self._scannedDomains.add(url)

		elif 'google.' in netloc:
			self.log.info("Skipping URL: '%s'", url)

		else:

			base, tld = netloc.rsplit(".", 1)
			self._tld.add(tld)
			for tld in self._tld:
				self._scannedDomains.add("{main}.{tld}".format(main=base, tld=tld))
				# print(self._scannedDomains)

	########################################################################################################################
	#
	#	 ######    #######   #######   ######   ##       ########    ########   #######   ######   ######
	#	##    ##  ##     ## ##     ## ##    ##  ##       ##          ##     ## ##     ## ##    ## ##    ##
	#	##        ##     ## ##     ## ##        ##       ##          ##     ## ##     ## ##       ##
	#	##   #### ##     ## ##     ## ##   #### ##       ######      ##     ## ##     ## ##        ######
	#	##    ##  ##     ## ##     ## ##    ##  ##       ##          ##     ## ##     ## ##             ##
	#	##    ##  ##     ## ##     ## ##    ##  ##       ##          ##     ## ##     ## ##    ## ##    ##
	#	 ######    #######   #######   ######   ######## ########    ########   #######   ######   ######
	#
	########################################################################################################################

	def processGdocResources(self, resources):

		# Expected format of tuples in ret:
		# fName, mimeType, content, fHash
		ret = []

		for fName, mimeType, content in resources:
			m = hashlib.md5()
			m.update(content)
			fHash = m.hexdigest()

			pseudoUrl = "gdoc-"+fHash

			self.fMap[fName] = fHash

			fName = os.path.split(fName)[-1]

			self.log.info("Resource = '%s', '%s', '%s'", fName, mimeType, pseudoUrl)
			if mimeType in ["image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/svg+xml", "image/vnd.djvu"]:
				self.log.info("Processing resource '%s' as an image file. (mimetype: %s)", fName, mimeType)
				ret.append((fName, mimeType, content, pseudoUrl))
			elif mimeType in ["application/octet-stream"]:
				self.log.info("Processing '%s' as an binary file.", fName)
				ret.append((fName, mimeType, content, pseudoUrl))
			else:
				self.log.warn("Unknown MIME Type? '%s', FileName: '%s'", mimeType, fName)

		if len(resources) == 0:
			self.log.info("File had no resource content!")

		return ret

	def cleanGdocPage(self, soup, url):
		# doc = readability.readability.Document(str(soup))
		title = self.extractTitle(soup, url)

		for span in soup.find_all("span"):
			span.unwrap()

		for style in soup.find_all('style'):
			style.decompose()

		for tag in soup.find_all(attrs = {'class' : True}):
			del tag['class']

		return title, soup

	# Hook so plugins can modify the internal URLs as part of the relinking process
	def preprocessGdocReaderUrl(self, inUrl):
		if inUrl.lower().endswith("/preview"):
			inUrl = inUrl[:-len("/preview")]

		return inUrl

	def convertToGdocReaderImage(self, srcUrl):

		itemHash = None
		for rscEnd in self.fMap:
			if srcUrl.endswith(rscEnd):
				itemHash = self.fMap[rscEnd]

		# if srcUrl in self.fMap:
		# 	url = self.fMap[srcUrl]
		# elif any([fUrl in url for fUrl in self.fMap]):
		# 	print('wat')
		# 	raise ValueError("Unknown image URL! = '%s'" % url)
		if not itemHash:
			raise ValueError("Unknown image URL! = '%s' (hash '%s')" % (srcUrl, itemHash))

		url = '/books/render?mdsum=%s' % urllib.parse.quote(itemHash)

		return url

	def processGdocPage(self, url, content):
		dummy_fName, content = content
		soup = WebRequest.as_soup(content)
		urlFuncs.canonizeUrls(soup, url)

		pgTitle, soup = self.cleanGdocPage(soup, url)

		plainLinks = self.extractLinks(soup, url)
		self.log.info("Page title = '%s'", pgTitle)
		soup = self.relink(soup, imRelink=self.convertToGdocReaderImage)

		url = self.preprocessGdocReaderUrl(url)
		url = urlFuncs.trimGDocUrl(url)
		# Since the content we're extracting will be embedded into another page, we want to
		# strip out the <body> and <html> tags. `unwrap()`  replaces the soup with the contents of the
		# tag it's called on. We end up with just the contents of the <body> tag.
		soup.body.unwrap()
		pgBody = soup.prettify()

		# No image links, since they're served as resource files in a google doc
		imageLinks = []
		return plainLinks, imageLinks, pgTitle, pgBody
		# self.updateDbEntry(url=url, title=pgTitle, contents=pgBody, mimetype='text/html', dlstate=2)

	def retreiveGoogleDoc(self, url):

		self.log.info("Should fetch google doc at '%s'", url)
		doc = gdp.GDocExtractor(url)

		attempts = 0

		mainPage = None
		while 1:
			attempts += 1
			try:
				mainPage, resources = doc.extract()
			except TypeError:
				self.log.critical('Extracting item failed!')
				for line in traceback.format_exc().strip().split("\n"):
					self.log.critical(line.strip())

					raise urlFuncs.CannotAccessGDocException("Cannot access google doc! Is it protected?")

			if mainPage:
				break
			if attempts > 3:
				raise TextScrape.SiteArchiver.DownloadException

		resources = self.processGdocResources(resources)

		return self.processGdocPage(url, mainPage) + (resources, )

	# Process a Google-Doc resource page.
	# This call does a set of operations to permute and clean a google doc page.
	def extractContent(self):

		plainLinks, imageLinks, pgTitle, pgBody, resources = self.retreiveGoogleDoc(self.pageUrl)

		ret = {}
		ret['plainLinks'] = plainLinks
		ret['rsrcLinks']  = imageLinks
		ret['title']      = pgTitle
		ret['contents']   = pgBody

		ret['resources'] = resources

		return ret

def test():
	print("Test mode!")
	import WebRequest
	import logSetup
	logSetup.initLogging()

	wg = WebRequest.WebGetRobust()
	# content = wg.getpage('http://www.arstechnica.com')
	scraper = GdocPageProcessor('https://docs.google.com/document/d/1atXMtCutHRpcHwSRS5UyMAC58_gQjMPR2dDVn1LCD3E', 'Main.Test', 'testinating')
	print(scraper)
	extr, rsc = scraper.extractContent()
	print('Plain Links:')
	for link in extr['plainLinks']:
		print(link)
	print()
	print()
	print('Resource files:')
	# for link in extr['rsrcLinks']:
	# 	print(link)

	for fName, mimeType, content, pseudoUrl in rsc:
		print(fName, mimeType, pseudoUrl)
	# print(extr['contents'])

if __name__ == "__main__":
	test()


import os
import socket
import boto3
import pytest
from moto import mock_elbv2
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import src.index as index
from unittest.mock import patch

@mock_elbv2
@patch('socket.gethostbyname_ex')
def test_handler(mock_gethostbyname_ex):
    # Define your environment variables
    os.environ['RDS_ENDPOINT'] = "rds-endpoint"
    os.environ['RDS_PORT'] = "1433"
    os.environ['AWS_DEFAULT_REGION'] = 'us-east-1'

    # Mock the RDS endpoint
    mock_gethostbyname_ex.return_value = ('', [], ['10.0.2.46', '10.0.2.47'])

    # Create an elbv2 client and create a target group
    client = boto3.client('elbv2')
    response = client.create_target_group(
        Name='my-targets',
        Protocol='TCP',
        Port=1433,
        VpcId='vpc-3ac0fb5f',
        HealthCheckProtocol='TCP',
        HealthCheckPort='1433'
    )
    assert response['ResponseMetadata']['HTTPStatusCode'] == 200

    os.environ['TARGET_GROUP_ARN'] = response['TargetGroups'][0]['TargetGroupArn']

    # Register targets
    response = client.register_targets(
        TargetGroupArn=os.environ['TARGET_GROUP_ARN'],
        Targets=[
            {
                'Id': '10.0.2.44',
                'Port': 1433
            },
            {
                'Id': '10.0.2.45',
                'Port': 1433
            },
        ]
    )
    assert response['ResponseMetadata']['HTTPStatusCode'] == 200

    # Mock an event and context, call the handler
    event = {}  # Mock event
    context = {}  # Mock context
    index.handler(event, context)

    # Check that the old targets were deregistered and new targets registered
    response = client.describe_target_health(TargetGroupArn=os.environ['TARGET_GROUP_ARN'])
    target_ips = [target['Target']['Id'] for target in response['TargetHealthDescriptions']]
    assert '10.0.2.44' not in target_ips
    assert '10.0.2.45' not in target_ips
    assert '10.0.2.46' in target_ips
    assert '10.0.2.47' in target_ips


from turtle import position
from django.db import models
from django.contrib.auth.models import User

class job(models.Model):
    """
    - Position name
    - Text Description
    - Age Criteria
    - Salary
    - No. of openings
    """
    position_name =models.CharField(max_length=100)
    text_description = models.TextField()
    min_age =models.IntegerField()
    max_age =models.IntegerField()
    salary =models.IntegerField()
    no_openongs =models.IntegerField()
    create= models.ForeignKey(User, on_delete= models.CASCADE)

    def __str__(self):
        return self.position_name


import json
import logging

from . import spinnaker as sk

class BaseGateStatus(sk.SpinnakerStatus):
    """Common base class for gate status objects."""

    @classmethod
    def new(cls, operation, original_response):
        """Factory method.

        Args:
          operation: [AgentOperation] The operation this status is for.
          original_response: [string] The original JSON with the status identifier.

        Returns:
          sk.SpinnakerStatus for handling status from a Gate request.
        """
        return cls(operation, original_response)

    @property
    def finished(self):
        """True if status indicates the request has finished."""
        return self.current_state not in ["NOT_STARTED", "RUNNING", None]

    @property
    def finished_ok(self):
        """True if status indicates the request has finished successfully."""
        return self.current_state == "SUCCEEDED"

    def export_summary_to_json_snapshot(self, snapshot, entity):
        """Implements JsonSnapshotableEntity"""
        super().export_summary_to_json_snapshot(snapshot, entity)
        detail = self.detail_doc
        if not detail:
            return

        builder = snapshot.edge_builder

        if not isinstance(detail, list):
            base_time = detail.get("startTime", 0)
            self._export_status_entry_summary_to_json_snapshot(
                detail, base_time, builder, snapshot, entity
            )
            return

        base_time = detail[0].get("startTime", 0)
        self._export_status(detail[0], builder, entity)
        self._export_time_info(detail[0], base_time, builder, entity)

        step_list_entity = snapshot.new_entity(summary=f"{len(detail)} parts")

        for index, step in enumerate(detail):
            step_entity = snapshot.new_entity()
            self._export_status_entry_summary_to_json_snapshot(
                step, base_time, builder, snapshot, step_entity
            )
            builder.make(
                step_list_entity,
                "[{}] {}".format(index, step.get("name")),
                step_entity,
            )
        builder.make(entity, "Steps", step_list_entity)

    def _export_status_entry_summary_to_json_snapshot(
        self, status_entry, base_time, builder, snapshot, entity
    ):
        self._export_status(status_entry, builder, entity)
        self._export_time_info(status_entry, base_time, builder, entity)
        self._export_error_info(status_entry, builder, entity)
        if "execution" in status_entry:
            execution = status_entry["execution"]
            execution_entity = snapshot.new_entity(summary="Execution Summary")
            relation = self._STATUS_TO_RELATION.get(execution.get("status"))
            builder.make(entity, "Execution Info", execution_entity, relation=relation)
            self._export_status_entry_summary_to_json_snapshot(
                execution, base_time, builder, snapshot, execution_entity
            )
            return

        stages = status_entry.get("stages")
        if not stages:
            return
        stage_list_entity = snapshot.new_entity(
            summary=f"{len(stages)} stages"
        )
        worst_relation_score = self._RELATION_SCORE.get(None)
        for index, stage in enumerate(stages):
            stage_entity = snapshot.new_entity()
            relation = self._export_stage_summary_to_json_snapshot(
                stage, base_time, builder, snapshot, stage_entity
            )
            score = self._RELATION_SCORE.get(relation)
            if score is not None and (
                worst_relation_score is None or worst_relation_score < score
            ):
                worst_relation_score = score
            decorator = "(*) " if stage.get("status") == "RUNNING" else ""
            builder.make(
                stage_list_entity,
                "[{}] {}{}".format(index, decorator, stage.get("name")),
                stage_entity,
                relation=relation,
            )
        builder.make(
            entity,
            "Stages",
            stage_list_entity,
            relation=self._SCORE_TO_RELATION.get(worst_relation_score),
        )

    def _maybe_export_stage_context(self, stage, builder, snapshot, entity):
        context = stage.get("context")
        if not context:
            return
        ex = context.get("exception")
        if not ex:
            return
        builder.make(
            entity, "Exception Type", ex.get("exceptionType"), relation="ERROR"
        )
        errors = (ex.get("defaults") or ex.get("details", {})).get("errors")
        if errors:
            message = "\n".join([" * " + err for err in errors])
        else:
            message = ex.get("error")
        if message:
            builder.make(entity, "Errors", message, relation="ERROR", format="pre")

    def _export_stage_summary_to_json_snapshot(
        self, stage, base_time, builder, snapshot, entity
    ):
        stage_relation = self._export_status(stage, builder, entity)
        self._export_time_info(stage, base_time, builder, entity)
        self._maybe_export_stage_context(stage, builder, snapshot, entity)
        tasks = stage.get("tasks")
        if not tasks:
            return stage_relation

        task_list_entity = snapshot.new_entity(summary=f"{len(tasks)} tasks")
        worst_relation_score = self._RELATION_SCORE.get(None)
        for index, task in enumerate(tasks):
            task_name = task.get("name")
            task_entity = snapshot.new_entity()
            relation = self._export_status(task, builder, task_entity)
            score = self._RELATION_SCORE.get(relation)
            if score is not None and (
                worst_relation_score is None or worst_relation_score < score
            ):
                worst_relation_score = score
            self._export_time_info(task, base_time, builder, task_entity)
            builder.make(task_entity, "Task Name", task_name)
            decorator = "(*) " if task.get("status") == "RUNNING" else ""
            builder.make(
                task_list_entity,
                f"[{index}] {decorator}{task_name}",
                task_entity,
                relation=relation,
            )
        builder.make(
            entity,
            "Tasks",
            task_list_entity,
            relation=self._SCORE_TO_RELATION.get(worst_relation_score),
        )
        return stage_relation

class GateTaskStatus(BaseGateStatus):
    """Specialization of BaseGateStatus for accessing Gate 'tasks' status."""

    @property
    def timed_out(self):
        """True if status indicates the request timed out."""
        return (
            self.current_state == "TERMINAL"
            and str(self.detail).find(" timed out.") > 0
        )

    def __init__(self, operation, original_response=None):
        """Construct a new Gate request status.

        Args:
          operation: [AgentOperation] The operation this status is for.
          original_response: [string] The original JSON with the status identifier.
        """
        super().__init__(operation, original_response)

        if not original_response.ok():
            self._bind_error(original_response.error_message)
            self.current_state = "HTTP_ERROR"
            return

        doc = None
        try:
            doc = json.JSONDecoder().decode(original_response.output)
        except (ValueError, TypeError):
            pass

        if isinstance(doc, dict):
            self._bind_detail_path(doc["ref"])
            self._bind_id(self.detail_path)
        else:
            self._bind_error(f"Invalid response='{original_response}'")
            self.current_state = "CITEST_INTERNAL_ERROR"

    def _update_response_from_json(self, doc):
        """Updates abstract BaseGateStatus attributes from a Gate response.

        This is called by the base class.

        Args:
           doc: [dict] JSON Document object read from response payload.
        """
        self.current_state = doc["status"]
        self._bind_exception_details(None)

        exception_details = None
        gate_exception = None
        variables = doc.get("variables", None)
        if variables:
            for elem in variables:
                if elem["key"] == "exception":
                    value = elem["value"]
                    exception_details = value["details"]
                elif elem["key"] == "kato.tasks":
                    value = elem["value"]
                    for task in value:
                        if "exception" in task:
                            gate_exception = task["exception"]["message"]
                            break

        self._bind_exception_details(exception_details or gate_exception)

class GatePipelineStatus(BaseGateStatus):
    """Specialization of BaseGateStatus for accessing Gate 'pipelines' status."""

    @classmethod
    def new(cls, operation, original_response):
        """Factory method.

        Args:
          operation: [AgentOperation] The operation this status is for.
          original_response: [string] The original JSON with the status identifier.

        Returns:
          sk.SpinnakerStatus for handling status from a Gate request.
        """
        return GatePipelineStatus(operation, original_response)

    @property
    def timed_out(self):
        """True if status indicates the request timed out."""
        return (
            self.current_state == "TERMINAL"
            and str(self.detail).find(" timed out.") > 0
        )

    def __init__(self, operation, original_response=None):
        """Construct a new Gate request status.

        Args:
          operation: [AgentOperation] The operation this status is for.
          original_response: [string] The original JSON with the status identifier.
        """
        super().__init__(operation, original_response)
        self.__saw_pipeline = False

    def _update_response_from_json(self, doc):
        """Updates abstract sk.SpinnakerStatus attributes from a Gate response.

        This is called by the base class.

        Args:
           doc: [dict] JSON Document object read from response payload.
        """
        self._bind_exception_details(None)

        if not isinstance(doc, list):
            self._bind_error(f"Invalid response='{doc}'")
            self.current_state = "CITEST_INTERNAL_ERROR"

        # It can take a while for the running pipelines to show up.
        if len(doc) == 0 or not isinstance(doc, list):
            return

        if not self.__saw_pipeline:
            logger = logging.getLogger(__name__)
            logger.info("Pipeline is now visible.")
            self.__saw_pipeline = True

        # TODO(lwander): We need a way to ensure we are monitoring the correct
        #                pipeline.
        doc = doc[0]
        if self.current_state != doc["status"]:
            self.current_state = doc["status"]
            logger = logging.getLogger(__name__)
            logger.info("Pipeline state is now %s", self.current_state)

        # TODO(ewiseblatt): 20160308
        # It looks like sometimes when we get an exception, the exception is
        # burried in one of the interior stages and not propagated back up to
        # the top level status. It's context is empty. So this might not
        # be working at all, or at least is incomplete.
        context = doc.get("context", {})
        exception_details = context.get("exception", None)
        self._bind_exception_details(exception_details)

class GateAgent(sk.SpinnakerAgent):
    """Specialization of SpinnakerAgent for Gate subsystem.

    This class just adds convienence methods specific to Gate.
    """

    def make_create_app_operation(
        self,
        bindings,
        application,
        account_name,
        cloud_providers=None,
        description=None,
    ):
        """Create a Gate operation that will create a new application.

        Args:
          bindings: [dict] key/value pairs including optional TEST_EMAIL.
          application: [string] Name of application to create.
          account_name: [string] Account name owning the application.
          cloud_providers: [string] Comma-separated list of cloud providers.
          description: [string] Text description field for the operation payload.

        Returns:
          AgentOperation.
        """
        email = bindings.get("TEST_EMAIL", "testuser@testhost.org")
        payload = self.make_json_payload_from_kwargs(
            job=[
                {
                    "type": "createApplication",
                    "account": account_name,
                    "application": {
                        "name": application,
                        "description": description or "Gate Testing Application",
                        "email": email,
                        "platformHealthOnly": True,
                        "cloudProviders": cloud_providers or "",
                    },
                    "user": "[anonymous]",
                }
            ],
            description="Create Application: " + application,
            application=application,
        )

        return self.new_post_operation(title="create_app", data=payload, path="tasks")

    def make_delete_app_operation(self, application, account_name):
        """Create a Gate operation that will delete an existing application.

        Args:
          application: [string] Name of application to create.
          account_name: [string] Spinnaker account deleting the application.

        Returns:
          AgentOperation.
        """
        payload = self.make_json_payload_from_kwargs(
            job=[
                {
                    "type": "deleteApplication",
                    "account": account_name,
                    "application": {"name": application},
                    "user": "[anonymous]",
                }
            ],
            description="Delete Application: " + application,
            application=application,
        )

        return self.new_post_operation(title="delete_app", data=payload, path="tasks")

def new_agent(bindings, port=8084):
    """Create an agent to interact with a Spinnaker Gate server.

    Args:
      bindings: [dict] Bindings that specify how to connect to the server.
         The actual parameters used depend on the hosting platform.
         The hosting platform is specified with 'host_platform'.
      port: [int] The port the server is listening on.

    Returns:
      sk.SpinnakerAgent connected to the specified gate server, or None.
      The agent will have an additional attributes:
         managed_project: The name of the project Spinnaker is managing.
         account_name: The account name that Spinnaker is configured to use.
    """
    spinnaker = GateAgent.new_instance_from_bindings(
        "gate", GateTaskStatus.new, bindings, port
    )
    return spinnaker


import json
import os
import tempfile
from abc import ABC
from typing import Union

import numpy as np
import pytest
from elasticsearch import NotFoundError

try:
    import torch  # noqa: F401
    from torch import Tensor, nn  # noqa: F401
    from transformers import PretrainedConfig, elasticsearch_model_id  # noqa: F401

    from eland.ml.pytorch import (  # noqa: F401
        NlpBertTokenizationConfig,
        NlpTrainedModelConfig,
        PyTorchModel,
        TraceableModel,
        task_type_from_model_config,
    )
    from eland.ml.pytorch.nlp_ml_model import (
        NerInferenceOptions,
        TextClassificationInferenceOptions,
        TextEmbeddingInferenceOptions,
    )

    TracedModelTypes = Union[  # noqa: F401
        torch.nn.Module,
        torch.ScriptModule,
        torch.jit.ScriptModule,
        torch.jit.TopLevelTracedModule,
    ]

    HAS_PYTORCH = True
except ImportError:
    HAS_PYTORCH = False

from tests import ES_TEST_CLIENT, ES_VERSION

pytestmark = [
    pytest.mark.skipif(
        ES_VERSION < (8, 0, 0),
        reason="This test requires at least Elasticsearch version 8.0.0",
    ),
    pytest.mark.skipif(
        not HAS_PYTORCH, reason="This test requires 'pytorch' package to run"
    ),
]

TEST_BERT_VOCAB = [
    "Elastic",
    "##search",
    "is",
    "fun",
    "my",
    "little",
    "red",
    "car",
    "God",
    "##zilla",
    ".",
    ",",
    "[CLS]",
    "[SEP]",
    "[MASK]",
    "[PAD]",
    "[UNK]",
    "day",
    "Pancake",
    "with",
    "?",
]

NER_LABELS = [
    "O",
    "B_MISC",
    "I_MISC",
    "B_PER",
    "I_PER",
    "B_ORG",
    "I_ORG",
    "B_LOC",
    "I_LOC",
]

TEXT_CLASSIFICATION_LABELS = ["foo", "bar", "baz"]

if not HAS_PYTORCH:
    pytest.skip("This test requires 'pytorch' package to run", allow_module_level=True)

class TestTraceableModel(TraceableModel, ABC):
    def __init__(self, model: nn.Module):
        super().__init__(model)

    def _trace(self) -> TracedModelTypes:
        input_ids = torch.tensor(np.array(range(0, len(TEST_BERT_VOCAB))))
        attention_mask = torch.tensor([1] * len(TEST_BERT_VOCAB))
        token_type_ids = torch.tensor([0] * len(TEST_BERT_VOCAB))
        position_ids = torch.arange(len(TEST_BERT_VOCAB), dtype=torch.long)
        return torch.jit.trace(
            self._model,
            (
                input_ids,
                attention_mask,
                token_type_ids,
                position_ids,
            ),
        )

    def sample_output(self) -> torch.Tensor:
        input_ids = torch.tensor(np.array(range(0, len(TEST_BERT_VOCAB))))
        attention_mask = torch.tensor([1] * len(TEST_BERT_VOCAB))
        token_type_ids = torch.tensor([0] * len(TEST_BERT_VOCAB))
        position_ids = torch.arange(len(TEST_BERT_VOCAB), dtype=torch.long)
        return self._model(input_ids, attention_mask, token_type_ids, position_ids)

class NerModule(nn.Module):
    def forward(
        self,
        input_ids: Tensor,
        _attention_mask: Tensor,
        _token_type_ids: Tensor,
        _position_ids: Tensor,
    ) -> Tensor:
        """Wrap the input and output to conform to the native process interface."""
        outside = [0] * len(NER_LABELS)
        outside[0] = 1
        person = [0] * len(NER_LABELS)
        person[3] = 1
        person[4] = 1
        result = [outside for _t in np.array(input_ids.data)]
        result[1] = person
        result[2] = person
        return torch.tensor([result], dtype=torch.float)

class EmbeddingModule(nn.Module):
    def forward(
        self,
        _input_ids: Tensor,
        _attention_mask: Tensor,
        _token_type_ids: Tensor,
        _position_ids: Tensor,
    ) -> Tensor:
        """Wrap the input and output to conform to the native process interface."""
        result = [0] * 512
        return torch.tensor([result], dtype=torch.float)

class TextClassificationModule(nn.Module):
    def forward(
        self,
        _input_ids: Tensor,
        _attention_mask: Tensor,
        _token_type_ids: Tensor,
        _position_ids: Tensor,
    ) -> Tensor:
        # foo, bar, baz are the classification labels
        result = [0, 1.0, 0]
        return torch.tensor([result], dtype=torch.float)

MODELS_TO_TEST = [
    (
        "ner",
        TestTraceableModel(model=NerModule()),
        NlpTrainedModelConfig(
            description="test ner model",
            inference_config=NerInferenceOptions(
                tokenization=NlpBertTokenizationConfig(),
                classification_labels=NER_LABELS,
            ),
        ),
        "Godzilla Pancake Elasticsearch is fun.",
        "[Godzilla](PER&Godzilla) Pancake Elasticsearch is fun.",
    ),
    (
        "embedding",
        TestTraceableModel(model=EmbeddingModule()),
        NlpTrainedModelConfig(
            description="test text_embedding model",
            inference_config=TextEmbeddingInferenceOptions(
                tokenization=NlpBertTokenizationConfig()
            ),
        ),
        "Godzilla Pancake Elasticsearch is fun.",
        [0] * 512,
    ),
    (
        "text_classification",
        TestTraceableModel(model=TextClassificationModule()),
        NlpTrainedModelConfig(
            description="test text_classification model",
            inference_config=TextClassificationInferenceOptions(
                tokenization=NlpBertTokenizationConfig(),
                classification_labels=TEXT_CLASSIFICATION_LABELS,
            ),
        ),
        "Godzilla Pancake Elasticsearch is fun.",
        "bar",
    ),
]

AUTO_TASK_RESULTS = [
    ("any_bert", "BERTMaskedLM", None, "fill_mask"),
    ("any_roberta", "RoBERTaMaskedLM", None, "fill_mask"),
    ("sentence-transformers/any_bert", "BERTMaskedLM", None, "text_embedding"),
    ("sentence-transformers/any_roberta", "RoBERTaMaskedLM", None, "text_embedding"),
    ("sentence-transformers/mpnet", "MPNetMaskedLM", None, "text_embedding"),
    ("sentence-transformers/any_bert", "BertModel", None, "text_embedding"),
    ("anynermodel", "BERTForTokenClassification", None, "ner"),
    ("anynermodel", "MPNetForTokenClassification", None, "ner"),
    ("anynermodel", "RoBERTaForTokenClassification", None, "ner"),
    ("anynermodel", "BERTForQuestionAnswering", None, "question_answering"),
    ("anynermodel", "MPNetForQuestionAnswering", None, "question_answering"),
    ("anynermodel", "RoBERTaForQuestionAnswering", None, "question_answering"),
    ("aqaModel", "DPRQuestionEncoder", None, "text_embedding"),
    ("aqaModel", "DPRContextEncoder", None, "text_embedding"),
    (
        "any_bert",
        "BERTForSequenceClassification",
        ["foo", "bar", "baz"],
        "text_classification",
    ),
    (
        "any_bert",
        "BERTForSequenceClassification",
        ["contradiction", "neutral", "entailment"],
        "zero_shot_classification",
    ),
    (
        "any_bert",
        "BERTForSequenceClassification",
        ["CONTRADICTION", "NEUTRAL", "ENTAILMENT"],
        "zero_shot_classification",
    ),
    ("any_bert", "SomeUnknownType", None, None),
]

@pytest.fixture(scope="function", autouse=True)
def setup_and_tear_down():
    ES_TEST_CLIENT.cluster.put_settings(
        body={"transient": {"logger.org.elasticsearch.xpack.ml": "DEBUG"}}
    )
    yield
    for (
        model_id,
        _,
        _,
        _,
        _,
    ) in MODELS_TO_TEST:
        model = PyTorchModel(ES_TEST_CLIENT, model_id.replace("/", "__").lower()[:64])
        try:
            model.stop()
            model.delete()
        except NotFoundError:
            pass

def upload_model_and_start_deployment(
    tmp_dir: str, model: TraceableModel, config: NlpTrainedModelConfig, model_id: str
):
    print("Loading HuggingFace transformer tokenizer and model")
    model_path = model.save(tmp_dir)
    vocab_path = os.path.join(tmp_dir, "vocabulary.json")
    with open(vocab_path, "w") as outfile:
        json.dump({"vocabulary": TEST_BERT_VOCAB}, outfile)
    ptm = PyTorchModel(ES_TEST_CLIENT, model_id)
    try:
        ptm.stop()
        ptm.delete()
    except NotFoundError:
        pass
    print(f"Importing model: {ptm.model_id}")
    ptm.import_model(
        model_path=model_path, config_path=None, vocab_path=vocab_path, config=config
    )
    ptm.start()
    return ptm

class TestPytorchModelUpload:
    @pytest.mark.parametrize("model_id,model,config,input,prediction", MODELS_TO_TEST)
    def test_model_upload(self, model_id, model, config, input, prediction):
        with tempfile.TemporaryDirectory() as tmp_dir:
            ptm = upload_model_and_start_deployment(tmp_dir, model, config, model_id)
            result = ptm.infer(docs=[{"text_field": input}])
            assert result.get("predicted_value") is not None
            assert result["predicted_value"] == prediction

    @pytest.mark.parametrize(
        "model_id,architecture,labels,expected_task", AUTO_TASK_RESULTS
    )
    def test_auto_task_type(self, model_id, architecture, labels, expected_task):
        config = (
            PretrainedConfig(
                name_or_path=model_id,
                architectures=[architecture],
                label2id=dict(zip(labels, range(len(labels)))),
                id2label=dict(zip(range(len(labels)), labels)),
            )
            if labels
            else PretrainedConfig(
                name_or_path=model_id,
                architectures=[architecture],
            )
        )
        assert task_type_from_model_config(model_config=config) == expected_task

    def test_elasticsearch_model_id(self):
        model_id_from_path = elasticsearch_model_id(r"/foo/bar")
        assert model_id_from_path == "foo_bar"

        model_id_from_path = elasticsearch_model_id(r"\BAZ\with space")
        assert model_id_from_path == "baz__with__space"

        model_id_from_path = elasticsearch_model_id(r"/foo/with tab\tback\slash")
        assert model_id_from_path == "foo__with__space__back__slash"

        long_id_left_truncated = elasticsearch_model_id(
            "/foo/bar/64charactersoftext64charactersoftext64charactersoftext64charofte"
        )
        assert (
            long_id_left_truncated
            == "64charactersoftext64charactersoftext64charactersoftext64charofte"
        )


from django.urls import path, include
from . import views

urlpatterns = [
    path('lista_autos/', views.lista_autos, name="lista_autos"),
    path('lista_motos/', views.lista_moto, name="lista_motos"),
    path('detalle_moto/<int:id>/', views.detalle_moto, name="detalle_moto"),
    path('detalle_auto/<int:id>/', views.detalle_auto, name="detalle_auto"),
    path('buscar_auto/', views.busquedaAuto, name="buscar_auto")
]


from bs4 import BeautifulSoup
from newsapi import NewsApiClient
from tkinter import *
import requests
# personal file "setting.py" that contains
# api key of newsapi
from settings import MY_API_KEY

newsapi = NewsApiClient(api_key=MY_API_KEY)

def search():
    # add a vertical scrollbar to the canvas
    scrollbar = Scrollbar(second_frame, orient=VERTICAL, command=canvas.yview)
    scrollbar.pack(side=RIGHT, fill=Y)

    # configure the canvas
    canvas.configure(yscrollcommand=scrollbar.set)
    canvas.bind('<Configure>', lambda e: canvas.configure(scrollregion=canvas.bbox("all")))

    # create a news frame inside canvas to place the widgets later
    news_frame = Frame(canvas)
    # create canvas window with news frame
    canvas.create_window((0,0), window=news_frame, anchor="nw")

    # get the query text entered by the user
    text = entry.get()
    # perform the query
    data = newsapi.get_everything(q=text, language="en", page_size=20, sources="the-verge")
    articles = data["articles"]
    # for each article, obtain the title and
    # summary content to link it with its url
    for article in articles:
        title = article["title"]
        url = article["url"]
        content = article["content"]
        # place clickable title lables to the news_frame
        title_lbl = Label(news_frame, text=title, font=("monospace", 14, "bold"), textvariable=url)
        title_lbl.pack(pady=5)
        title_lbl.bind("<Button-1>", callback)
        # place clickable content lables to the news_frame
        content_lbl = Label(news_frame, text=content, font=("monospace", 11), textvariable=url)
        content_lbl.pack()
        content_lbl.bind("<Button-1>", callback)

# when the title or the content of the news
# is clicked
def callback(event):
    # get title and url from the call
    title = event.widget.cget("text")
    url = event.widget.cget("textvariable")
    # make a request to the url
    source = requests.get(url)
    soup = BeautifulSoup(source.content, "html.parser")
    content = soup.find("div", class_="c-entry-content")
    # get the article from its website
    full_text = ""
    for paragraph in content.find_all("p"):
        full_text += paragraph.text
    # open a new window
    new_window = Toplevel(master)
    new_window.geometry("800x400")
    # place the label of the article
    Label(new_window, text=title, font=("monospace", 16, "bold")).pack()
    # place all of the content
    text = Text(new_window, font=("monospace", 12))
    text.insert(END, full_text)
    text.pack(fill="both")

# creates a Tk() object
master = Tk()
master.resizable(width=False, height=False)
master.configure(bg="white")
master.title("Breaking News")
master.geometry("900x660")

# upper frame that involves search bar
# and additional information labels
frame = Frame(master, bg="white")
frame.pack(fill=BOTH, expand=1)

# greeting label
label = Label(frame, bg="white", text="Welcome to the ... News.", anchor="center")
label.config(font=("monospace", 18, "bold"))
label.pack()

label_detail = Label(frame, bg="white", text="Find the most up-to-date news as if on the wings of the wind!", anchor="center")
label_detail.config(font=("monospace", 12))
label_detail.pack()

search_bar_exp = Label(frame, bg="white", text="Use the search bar below to find the news.", anchor="center")
search_bar_exp.config(font=("monospace", 12))
search_bar_exp.pack()

# get user search keys to perform the query
entry = Entry(frame, width=50, borderwidth=2)
entry.pack()

enter_btn = Button(frame, text="Search", command=search, height=2, width=20, bg="#20bebe", fg="white")
enter_btn.pack()

# --- bottom frame for news ---

# create the bottom frame to place canvas
second_frame = Frame(master, bg="blue")
second_frame.pack(fill=BOTH, expand=1)

# create canvas inside the second frame
# this canvas is used to place labels relevant to
# searched news and scrollbar
canvas = Canvas(second_frame, width=880, height=500)
canvas.pack(side=LEFT, fill=BOTH, expand=1)

# start main loop
mainloop()


from typing import Callable
from utils.solution import Solution

def part1(input: list[str]) -> int:
    rotated = list(zip(*input[::-1]))
    width = len(rotated[0])
    height = len(rotated)
    gamma = ['0'] * height
    epsilon = ['0'] * height

    for i in range(height):
        bits = rotated[i]
        if bits.count('1') > (width / 2):
            gamma[i] = '1'
            epsilon[i] = '0'
        else:
            gamma[i] = '0'
            epsilon[i] = '1'

    gamma = int(''.join(gamma), 2)
    epsilon = int(''.join(epsilon), 2)
    return gamma * epsilon

def part2(input: list[str]) -> int:
    rotated = list(zip(*input[::-1]))
    width = len(rotated[0])
    height = len(rotated)
    oxygen_indices = range(width)
    co2_indices = range(width)

    def filter_rating(bits: tuple, indices: list[int], get_match_bit: Callable) -> list[int]:
        match_bit = get_match_bit([c for j, c in enumerate(bits) if j in indices])
        return [j for j in indices if input[j][i] == match_bit]

    for i in range(height):
        if len(oxygen_indices) > 1:
            oxygen_indices = filter_rating(reversed(rotated[i]), oxygen_indices,
                lambda filtered_bits: '1' if filtered_bits.count('1') >= (len(filtered_bits) / 2) else '0')
        if len(co2_indices) > 1:
            co2_indices = filter_rating(reversed(rotated[i]), co2_indices,
                lambda filtered_bits: '0' if filtered_bits.count('0') <= (len(filtered_bits) / 2) else '1')

    oxygen_generator_rating = int(input[oxygen_indices[0]], 2)
    co2_scrubber_rating = int(input[co2_indices[0]], 2)

    return oxygen_generator_rating * co2_scrubber_rating

solution = Solution(3, part1, part2, (198, 230))
solution.run()


from locale import normalize
from typing import Union
import torchaudio
import torch
import librosa
from omegaconf import DictConfig, OmegaConf
import logging
import os
import numpy as np

logger = logging.getLogger(__name__)

class AudioProcessor:
    def __init__(self,
                 config : DictConfig,
                 custom_feature_extractor : callable = None):
        self.config = config
        logger.info("Init AudioProcessor with parameters: " +
                    str(OmegaConf.to_container(self.config)))

        if custom_feature_extractor is not None:
            assert callable(custom_feature_extractor)
            logger.info("Overriding configuration with custom feature extractor")
            self.feature_extractor = custom_feature_extractor

        valid_features = ['spectrogram', 'melspectrogram', 'cqt', 'chroma_stft']
        if self.config.feature not in valid_features:
            raise ValueError("Invalid Feature: "+ str(self.config.feature))

        if self.config.feature == 'spectrogram':
            if self.config.loader == "torchaudio":
                self.feature_extractor = torchaudio.transforms.Spectrogram(
                    n_fft=self.config.spectrogram_params.n_fft,
                    pad_mode="reflect", # TODO: add config
                    center=True,  # TODO: add config
                    win_length=self.config.spectrogram_params.win_length,
                    hop_length=self.config.spectrogram_params.hop_length
                )
        elif self.config.feature == 'melspectrogram':
            if self.config.loader == "torchaudio":
                self.feature_extractor = torchaudio.transforms.MelSpectrogram(
                    sample_rate=self.config.sr,
                    center=True, # TODO: add config
                    n_fft=self.config.melspectrogram_params.n_fft,
                    win_length=self.config.melspectrogram_params.win_length,
                    hop_length=self.config.melspectrogram_params.hop_length,
                    n_mels=self.config.melspectrogram_params.n_mels,
                    f_min=self.config.melspectrogram_params.f_min,
                    f_max=self.config.melspectrogram_params.f_max
                )
        elif self.config.feature == 'cqt':
            def _cqt(x):
                return np.abs(
                    librosa.cqt(
                        np.array(x),
                        sr=self.config.cqt_params.sr,
                        hop_length=self.config.cqt_params.hop_length,
                        n_bins=self.config.cqt_params.n_bins,
                        bins_per_octave=self.config.cqt_params.bins_per_octave
                    )
                )
            self.feature_extractor = _cqt
        elif self.config.feature == 'chroma_stft':
            def _chroma_stft(x):
                return np.abs(
                    librosa.feature.chroma_stft(
                        np.array(x),
                        sr=self.config.chroma_stft_params.sr,
                        n_fft=self.config.chroma_stft_params.n_fft,
                        n_chroma=self.config.chroma_stft_params.n_chroma,
                        hop_length=self.config.chroma_stft_params.hop_length
                    )
                )
            self.feature_extractor = _chroma_stft
        else:
            raise NotImplementedError

    def wav2feature(self, x) -> Union[np.ndarray, torch.Tensor]:
        feature = self.feature_extractor(x)
        return feature

    def get_feature_from_audio_path(
        self, path: Union[os.PathLike, str]
    ) -> Union[np.ndarray, torch.Tensor]:
        return self.wav2feature(self.load_wav(path))

    def get_feature_from_audio(
        self, wav: Union[np.ndarray, torch.Tensor]
    ) -> Union[np.ndarray, torch.Tensor]:
        return self.wav2feature(wav)

    def load_wav(
        self,
        path: Union[os.PathLike, str],
        start_sec: float = None,
        duration_sec: float = None
    ) -> torch.Tensor:
        if self.config.loader == "torchaudio":
            if self.config.normalize:
                raise NotImplementedError
            if start_sec is not None or duration_sec is not None:
                sr = torchaudio.info(path).sample_rate
                wav, sr = torchaudio.load(
                    path,
                    normalize=True,  # TODO: add config
                    frame_offset=int(start_sec*sr),
                    num_frames=int(duration_sec*sr)
                )
                wav = torch.mean(wav, dim=0) # TODO: add config and move to transforms
            else:
                wav, sr = torchaudio.load(
                    path,
                    normalize=True,  # TODO: add config
                )
            if self.config.resample and sr != self.config.sr:
                resample = torchaudio.transforms.Resample(sr, self.config.sr) # TODO: add config and move to transforms
                wav = resample(wav)
        elif self.config.loader == "librosa":
            wav, sr = librosa.load(
                path,
                mono=True,
                sr=self.config.sr if self.config.resample else None,
                offset=start_sec,
                duration=duration_sec
            )
            if self.config.normalize:
                wav = librosa.util.normalize(wav)
            wav = torch.from_numpy(wav)
        else:
            raise NotImplementedError
        return wav


from ase.build import fcc111, add_adsorbate
from ase.visualize import view
from ase import Atoms
from ase.constraints import FixAtoms
from ase.constraints import FixBondLength
from ase.calculators.emt import EMT
from ase.io import read
from ase.optimize import *
from catlearn.optimize.mlmin import MLMin

"""
    Structure relaxation using constraints. CO on an Al(111) surface.
    Fixed bond length constraint.
    Benchmark using MLMin, GPMin, LBFGS and FIRE.
"""

# 1. Build Atoms Object.
###############################################################################

# Setup calculator:
calc = EMT()

# Create CO molecule:
d = 1.1
co = Atoms('CO', positions=[(0, 0, 0), (0, 0, d)])

# Create slab:
slab = fcc111('Al', size=(2,2,3), vacuum=10.0)
slab = fcc111('Al', size=(2,2,3))

# Add CO on the slab:
add_adsorbate(slab, co, 2., 'bridge')
slab.center(vacuum=10.0, axis=2)

# Set constraints:
c1 = FixAtoms(indices=[atom.index for atom in slab if atom.symbol == 'Al'])
c2 = FixBondLength(12, 13)
slab.set_constraint([c1, c2])

atoms = slab.copy()

# 2. Benchmark.
###############################################################################

# 2.A. Optimize structure using MLMin (CatLearn).
initial_mlmin = atoms.copy()
initial_mlmin.set_calculator(calc)
mlmin_opt = MLMin(initial_mlmin, trajectory='results_catlearn.traj')
mlmin_opt.run(fmax=0.01, kernel='SQE')

final_atoms = read('results_catlearn.traj', ':')

# 2.B Optimize using GPMin.
initial_gpmin = atoms.copy()
initial_gpmin.set_calculator(calc)
gpmin_opt = GPMin(initial_gpmin, trajectory='results_gpmin.traj',
                  update_hyperparams=True)
gpmin_opt.run(fmax=0.01)

# 2.C Optimize using LBFGS.
initial_lbfgs = atoms.copy()
initial_lbfgs.set_calculator(calc)
lbfgs_opt = BFGS(initial_lbfgs, trajectory='results_lbfgs.traj')
lbfgs_opt.run(fmax=0.01)

# 2.D Optimize using FIRE.
initial_fire = atoms.copy()
initial_fire.set_calculator(calc)
fire_opt = FIRE(initial_fire, trajectory='results_fire.traj')
fire_opt.run(fmax=0.01)

# 3. Summary of the results:
###############################################################################

print('\n Summary of the results:\n ------------------------------------')

fire_results = read('results_fire.traj', ':')
print('Number of function evaluations using FIRE:',
      len(fire_results))

lbfgs_results = read('results_lbfgs.traj', ':')
print('Number of function evaluations using LBFGS:',
      len(lbfgs_results))

gpmin_results = read('results_gpmin.traj', ':')
print('Number of function evaluations using GPMin:', gpmin_opt.function_calls)

catlearn_results = read('results_catlearn.traj', ':')
print('Number of function evaluations using MLMin (CatLearn):',
      len(catlearn_results))


import requests
from youtube_dl import YoutubeDL

# By Feh's

YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist': 'True'}

def search(arg):
    with YoutubeDL(YDL_OPTIONS) as ydl:
        try:
            requests.get(arg)
        except:
            video = ydl.extract_info(f"ytsearch:{arg}", download=False)['entries'][0]
        else:
            video = ydl.extract_info(arg, download=False)

    return video['webpage_url']

info = search(input('Nome da música: ').replace(' ', '+'))

print(info)

#By Ota's

import youtube_dl

ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download([info])


import matplotlib.pyplot as plt

testAccuracy = []
trainAccuracy = []
iterations = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]

trainSize = [39, 79, 119, 159, 199, 239, 279, 319, 359]

varied = 'variedIteration'
algorithms = ['RHC', 'SA', 'GA']
dataset = ['train', 'test']
figure = 0

fig1 = plt.figure(1)
fig1.suptitle("Train Accuracy vs. Iterations (Overall)")
fig2 = plt.figure(2)
fig2.suptitle("Test Accuracy vs. Iterations (Overall)")

for algo in algorithms:
    sourceFileTrain = '../Assignment2/ABAGAIL/src/opt/test/myTests/outputData/' + str(varied) + '/' + str(
        algo) + '/' + str(algo) + '_' + '' + 'trainAccuracy_70.txt'
    sourceFileTest = '../Assignment2/ABAGAIL/src/opt/test/myTests/outputData/' + str(varied) + '/' + str(
        algo) + '/' + str(algo) + '_' + '' + 'testAccuracy_70.txt'
    with open(sourceFileTrain) as f:
        data = f.read()
    accuracy = [float(value) for value in data.split("\n") if value]

    plt.figure(1)
    plt.plot(iterations, accuracy, label=algo, lw=2.0)
   # plt.axis([np.min(iterations), np.max(iterations), 0, 100])

    with open(sourceFileTest) as f:
        data = f.read()
    accuracy = [float(value) for value in data.split("\n") if value]

    plt.figure(2)
    plt.plot(iterations, accuracy, label=algo, lw=2.0)
    #plt.axis([np.min(iterations), np.max(iterations), 0, 100])

sourceFileTrain = '../Assignment2/ABAGAIL/src/opt/test/myTests/outputData/' + 'backpropTrain.txt'
sourceFileTest = '../Assignment2/ABAGAIL/src/opt/test/myTests/outputData/' + 'backpropTest.txt'
with open(sourceFileTrain) as f:
    data = f.read()
trainAccuracy = [float(value)*100 for value in data.split("\n") if value]
plt.figure(1)
plt.plot(iterations, trainAccuracy, label="backpropagation", lw=2.0)

plt.xlabel("iteration")
plt.ylabel("Classification Accuracy")
plt.legend(loc='lower right')

with open(sourceFileTest) as f:
    data = f.read()
testAccuracy = [float(value)*100 for value in data.split("\n") if value]

plt.figure(2)
plt.plot(iterations, testAccuracy, label="backpropagation", lw=2.0)

plt.xlabel("iteration")
plt.ylabel("Classification Accuracy")
plt.legend(loc='lower right')

fig1.savefig(
    '../Assignment2/ABAGAIL/src/opt/test/myTests/outputGraphs/' + "trainAccuracy" + '_' + 'overall' + '_' + "_plot" + '.png',
    dpi=300)
fig2.savefig(
    '../Assignment2/ABAGAIL/src/opt/test/myTests/outputGraphs/' + 'testAccuracy' + '_' + 'overall' + '_' + "_plot" + '.png',
    dpi=300)
# plt.show()


# ---------- IMPORT ------------
import logging
import json
import datetime
import matplotlib
import matplotlib.pyplot as plt
# ------------------------------

# ---------- INPUT -------------
path_source = "C:/Users/Lukas/Desktop/goalAchiever.json"
# ------------------------------

# ---------- OUTPUT ------------

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

# ---------- CONFIG ------------
t = 50
# ------------------------------

# ---------- INITIAL -----------
logging.basicConfig(format='%(asctime)s [%(levelname)s] - %(message)s', datefmt='%d-%m-%y %H:%M:%S', level=logging.INFO)
datetimeFormat = "%Y-%m-%d"
list_of_datetimes = []
values = []
# ------------------------------

logging.info("Accessing plot data ...")
with open(path_source, "r") as fp:
    plotdata = json.load(fp)

for entry in plotdata[str(t)]:
    list_of_datetimes.append(datetime.datetime.strptime(entry, datetimeFormat).date())
    values.append(plotdata[str(t)][entry])

logging.info("Done. (1/2)")

logging.info("Creating plot ...")

dates = matplotlib.dates.date2num(list_of_datetimes)
matplotlib.pyplot.plot_date(dates, values, '-')
plt.axvline(x=datetime.datetime.strptime("2016-05-19", datetimeFormat).date(), color='#D3685D', label="Design change")
plt.xlabel("Time")
plt.ylabel("Share of users having a streak > " + str(t))
plt.legend()
plt.show()

logging.info("Done. (2/2)")


import os, json, boto3, base64
from jsonschema import validate
from boto3.dynamodb.conditions import Key
from datetime import datetime, timezone

chats_table = boto3.resource('dynamodb').Table(os.environ.get('DDB_CHATS'))
advertisements_table = boto3.resource('dynamodb').Table(os.environ.get('DDB_AD'))

with open('schemas/message_post.json','r') as file:
    schema_post = json.load(file)

def get_chat(event, context):
    try:
        title = event['pathParameters']['title'].replace('%20', ' ')
    except KeyError:
        return {"statusCode": 400, "body": "No se ha indicado anuncio"}

    ann = advertisements_table.query(KeyConditionExpression=Key('title').eq(title)).get("Items")[0]
    if not ann:
        return {"statusCode": 404, "body": f"No se ha encontrado el anuncio {title}"}

    if 'user' in event['pathParameters']:
        owner = ann['owner']
        user = event['requestContext']['authorizer']['claims']['email']
        chat_id = title+'/'+event['pathParameters']['user']
        if not (user == owner or user == event['pathParameters']['user']):
            return {"statusCode": 403, "body": f"Usuario no autorizado en el chat"}
    else:
        chat_id = title

    chat = chats_table.query(KeyConditionExpression=Key('chat_id').eq(chat_id))
    messages = [{'fecha_hora': x['fecha_hora'], 'user_id': x['user_id'], 'message': x['message']} for x in chat.get("Items", [])]
    if not messages:
        return {"statusCode": 404, "body": "No messages found"}
    else:
        return {"statusCode": 200, "body": json.dumps(messages)}

def send_message(event, context):

    user = event['requestContext']['authorizer']['claims']['email']
    try:
        title = event['pathParameters']['title'].replace('%20', ' ')
    except KeyError:
        return {"statusCode": 400, "body": "No se ha indicado anuncio"}

    ann = advertisements_table.query(KeyConditionExpression=Key('title').eq(title)).get("Items")[0]
    if not ann:
        return {"statusCode": 404, "body": f"No se ha encontrado el anuncio {title}"}

    owner = ann['owner']

    if 'user' in event['pathParameters']:
        chat_id = title+'/'+event['pathParameters']['user']
        if not (user == owner or user == event['pathParameters']['user']):
            return {"statusCode": 403, "body": f"Usuario no autorizado en el chat"}
    else:
        chat_id = title

    message = json.loads(event.get('body', '{}'))
    if not message:
        return {"statusCode": 400, "body": "No se ha indicado body"}

    validate(instance=message, schema=schema_post)

    item = {
            'chat_id': chat_id,
            'fecha_hora': datetime.utcnow().replace(tzinfo=timezone.utc).isoformat(),
            'user_id': user,
            'message': message['message'],
        }
    chats_table.put_item(Item=item)
    return {"statusCode": 201, "body": f"Posted in {chat_id}" }


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

from keras_uncertainty.losses import regression_gaussian_nll_loss, regression_gaussian_beta_nll_loss
import numpy as np
import matplotlib.pyplot as plt

from keras.layers import Dense, Input, Flatten

import tensorflow as tf

from tensorflow import keras
from tensorflow.keras.models import Model

import keras_uncertainty.backend as K
from keras_uncertainty.models import TwoHeadStochasticRegressor, StochasticRegressor
from keras_uncertainty.layers import DropConnectDense, StochasticDropout

tf.compat.v1.disable_eager_execution()

# Losses commonly used in uncertainty quantification and probabilistic forecasting

def regression_gaussian_nll_loss(variance_tensor, epsilon=1e-8, variance_logits=False):
    """
        Gaussian negative log-likelihood for regression, with variance estimated by the model.
        This function returns a keras regression loss, given a symbolic tensor for the sigma square output of the model.
        The training model should return the mean, while the testing/prediction model should return the mean and variance.
    """
    def nll(y_true, y_pred):
        #if variance_logits:
        #    variance_tensor = K.exp(variance_tensor)

        return 0.5 * K.mean(K.log(variance_tensor + epsilon) + K.square(y_true - y_pred) / (variance_tensor + epsilon))

    return nll

def train_standard_model(x_train, y_train):
    """

    :param x_train:
    :param y_train:
    :return:
    """

    prob = 0.05
    inp = Input(shape=(1,))
    x = DropConnectDense(32, activation="relu", prob=prob)(inp)
    x = DropConnectDense(32, activation="relu", prob=prob)(x)
    mean = DropConnectDense(1, activation="linear")(x)
    var = DropConnectDense(1, activation="softplus")(x)

    train_model = Model(inp, mean)
    pred_model = Model(inp, [mean, var])

    opt = keras.optimizers.Adam(learning_rate=0.1)

    train_model.compile(loss=regression_gaussian_nll_loss(var), optimizer=opt)
    train_model.fit(x_train, y_train, verbose=2, epochs=100)

    return train_model, pred_model

def evaluate_model(domain, pred_model):
    # model taken from given repository: returns the mean and std (two heads)
    model = TwoHeadStochasticRegressor(pred_model)

    # predict on the same model 20 times and disentangle the uncertainties
    # where aleatoric uncertainty = mean of the variances from each predicted model (previously trained)
    # and epistemic uncertainty = variance of the means from each predicted model (previously trained)
    mean_pred, epi_std, ale_std = model.predict(domain, 20, disentangle_uncertainty=True)

    return mean_pred, epi_std, ale_std

# generating big and small datasets
X = np.clip(np.random.normal(0.0, 1.0, 100).reshape(-1,1), -3, 3)

# let us generate a grid to check how models fit the data
x_grid = np.linspace(-5, 5, 100).reshape(-1,1)

# defining the function - noisy
# function adapted from : https://www.kaggle.com/code/gdmarmerola/risk-and-uncertainty-in-deep-learning/notebook
noise = lambda x: (x**2)/10
target_toy = lambda x: (x + 0.3*np.sin(2*np.pi*(x + noise(x)) +
                        0.3*np.sin(4*np.pi*(x + noise(x)) +
                        noise(x) - 0.5)))

# defining the function - no noise
target_toy_noiseless = lambda x: (x + 0.3*np.sin(2*np.pi*(x)) + 0.3*np.sin(4*np.pi*(x)) - 0.5)

# runnning the target
y = np.array([target_toy(e) for e in X])
y_noiseless = np.array([target_toy_noiseless(e) for e in x_grid])

# check the toy data
plt.figure(figsize=[12,6], dpi=200)

# return trained model and predictive model
train_model, pred_model =train_standard_model(X, y)

# predict the model on data and return epistemic and aleatoric noise
predicted_mean, epi, ale = evaluate_model(x_grid, pred_model)

# reshape the vectors
y_pred_mean = predicted_mean.reshape((-1,))
y_epi = epi.reshape((-1,))
y_ale =ale.reshape((-1,))

# predictive uncertainty is the sum of epistemic and aleatoric
y_pred_std = y_epi + y_ale

#  calculate lower and upper bounds
y_pred_up_1 = y_pred_mean + y_pred_std
y_pred_down_1 = y_pred_mean - y_pred_std

plt.figure(figsize=[12,6], dpi=200)

# plot aleatoric uncertainty with the predicted mean
plt.plot(X, y, 'kx', label='Toy data', alpha=0.5, markersize=5)
plt.plot(x_grid, predicted_mean, label='neural net fit', color='tomato', alpha=0.8)
plt.fill_between (x_grid.reshape(1,-1)[0], y_pred_mean-y_ale, y_pred_mean+y_ale, alpha=0.2, label="Aleatoric Uncertainty", color='orange')
plt.title('Neural network fit for median expected value with aleatoric uncertainty')
plt.xlabel('$x$'); plt.ylabel('$y$')
plt.xlim(-3.5,3.5); plt.ylim(-5, 3)
plt.legend()
plt.show()

# plot epistemic uncertainty with the predicted mean
plt.plot(X, y, 'kx', label='Toy data', alpha=0.5, markersize=5)
plt.plot(x_grid, predicted_mean, label='neural net fit', color='tomato', alpha=0.8)
plt.fill_between (x_grid.reshape(1,-1)[0], y_pred_mean-y_epi, y_pred_mean+y_epi, alpha=0.2, label="Epistemic Uncertainty", color='orange')
plt.title('Neural network fit for median expected value with epistemic uncertainty')
plt.xlabel('$x$'); plt.ylabel('$y$')
plt.xlim(-3.5,3.5); plt.ylim(-5, 3)
plt.legend()
plt.show()

# plot predictive uncertainty with the predicted mean
plt.plot(X, y, 'kx', label='Toy data', alpha=0.5, markersize=5)
plt.plot(x_grid, predicted_mean, label='neural net fit', color='tomato', alpha=0.8)
plt.fill_between (x_grid.reshape(1,-1)[0], y_pred_mean-y_pred_std , y_pred_mean+y_pred_std , alpha=0.2, label="Predictive Uncertainty", color='orange')
plt.title('Neural network fit for median expected value with predictive uncertainty')
plt.xlabel('$x$'); plt.ylabel('$y$')
plt.xlim(-3.5,3.5); plt.ylim(-5, 3)
plt.legend()
plt.show()


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

from scipy.optimize import linear_sum_assignment
from utils.box_ops import generalized_iou, box_cxcywh_xyxy

class HungarianMatcher(nn.Module):
  def __init__(self, cost_class = 1, cost_bbox = 1, cost_giou = 1):
    super(HungarianMatcher, self).__init__()

    self.cost_class = cost_class
    self.cost_bbox = cost_bbox
    self.cost_giou = cost_giou

  def forward(self, preds, targets):
    """
    Preds is a dictonary that contains following keys:
      pred_class: [B, N, num_class]: Predicted class logits
      pred_bbox: [B, N, 4]: Predicted bouding boxes

    Target is a list of dictonary with following keys
      class: [B, N]: True labels
      bbox: [B, N, 4]: True bounding boxes
    """

    bs, num_queries = preds['pred_class'].shape[:2]

    # [B, N, num_class] -> [B*N, num_class]
    pred_class = preds['pred_class'].flatten(0, 1).softmax(-1)
    pred_bbox = preds['pred_bbox'].flatten(0, 1)

    tgt_ids = torch.cat([v['classes'] for v in targets])
    tgt_bbox = torch.cat([v['bboxes'] for v in targets])

    cost_class = -pred_class[:, tgt_ids]

    cost_bbox = torch.cdist(pred_bbox, tgt_bbox, p=1)

    cost_giou = -generalized_iou(box_cxcywh_xyxy(pred_bbox), box_cxcywh_xyxy(tgt_bbox))

    C = self.cost_class * cost_class + self.cost_bbox * cost_bbox + self.cost_giou * cost_giou

    C = C.view(bs, num_queries, -1).detach().cpu()

    sizes = [len(v['bboxes']) for v in targets]

    indices = []
    for i, c in enumerate(C.split(sizes, -1)):
      indices.append(linear_sum_assignment(c[i]))

    # indices = [linear_sum_assignment(c[i] for i, c in enumerate(C.split(sizes, -1)))]
    return [(torch.as_tensor(i, dtype=torch.int32), torch.as_tensor(j, dtype=torch.int32)) for i, j in indices]

class Criterion(nn.Module):
  def __init__(self, matcher, num_classes, eos_coef):
    super(Criterion, self).__init__()

    self.matcher = matcher
    self.num_classes = num_classes
    self.eos_coef = eos_coef
    empty_weight = torch.ones(self.num_classes + 1, device=device)
    empty_weight[-1] = self.eos_coef
    self.register_buffer('empty_weight', empty_weight)

  def _get_src_permutation_idx(self, indices):
    batch_idx = torch.cat([torch.full_like(src, i) for i, (src, _) in enumerate(indices)])
    src_idx = torch.cat([src for (src, _) in indices])
    return batch_idx, src_idx

  def _get_tgt_pernuration_idx(self, indices):
    batch_idx = torch.cat([torch.full_like(tgt, i) for i, (_, tgt) in enumerate(indices)])
    tgt_idx = torch.cat([tgt for (_, tgt) in indices])
    return batch_idx, tgt_idx

  def get_label_loss(self, outputs, targets, indices):
    pred_class = outputs['pred_class']
    idx = self._get_src_permutation_idx(indices)

    targets_class_o = torch.cat([t['classes'][J] for t, (_, J) in zip(targets, indices)])
    target_classes = torch.full(pred_class.shape[:2], self.num_classes, dtype=torch.int64, device=pred_class.device)

    target_classes[idx] = targets_class_o

    loss_ce = F.cross_entropy(pred_class.transpose(1, 2), target_classes, self.empty_weight)

    return loss_ce

  def get_bbox_loss(self, outputs, targets, indices, num_bboxes):

    idx = self._get_src_permutation_idx(indices)
    pred_bbox = pred_bbox = outputs['pred_bbox'][idx]
    tgt_bbox = torch.cat([t['bboxes'][i] for t, (_, i) in zip(targets, indices)], dim=0)

    loss_bbox = F.l1_loss(pred_bbox, tgt_bbox, reduction='none').sum() / num_bboxes
    giou_bbox = 1 - torch.diag(generalized_iou(
        box_cxcywh_xyxy(pred_bbox),
        box_cxcywh_xyxy(tgt_bbox)
    ))

    giou_loss = giou_bbox.sum() / num_bboxes
    return loss_bbox, giou_loss

  def forward(self, outputs, targets):
    """
    Arguments:
    Outputs is a dictonary containing:
      pred_class: [B, num_queries, num_classes] prediction for class
      pred_bbox: [B, num_queries, 4] prediction for bbox

    targets is a list of dictonary with elements having following keys:
      classes: [B, N] true labels
      bboxes: [B, N, 4] True bounding boxes
    """

    indices = self.matcher(outputs, targets)
    num_bboxes = sum(len(v['bboxes']) for v in targets)
    num_bboxes = torch.as_tensor([num_bboxes], dtype=torch.float, device=outputs['pred_class'].device)

    losses = {}
    l1_bbox_loss, giou_bbox = self.get_bbox_loss(outputs, targets, indices, num_bboxes)

    losses['label_loss'] = self.get_label_loss(outputs, targets, indices)
    losses['l1_loss'] = l1_bbox_loss
    losses['giou_loss'] = giou_bbox

    return losses


from pkg_resources import get_distribution
from pkg_resources import parse_version
from plone.restapi.bbb import IPloneSiteRoot
from plone.restapi.deserializer import json_body
from plone.restapi.exceptions import DeserializationError
from plone.restapi.interfaces import ISerializeToJson
from plone.restapi.services import Service
from urllib import parse
from zExceptions import BadRequest
from zope.component import getMultiAdapter

zcatalog_version = get_distribution("Products.ZCatalog").version
if parse_version(zcatalog_version) >= parse_version("5.1"):
    SUPPORT_NOT_UUID_QUERIES = True
else:
    SUPPORT_NOT_UUID_QUERIES = False

class QuerystringSearch:
    """Returns the querystring search results given a p.a.querystring data."""

    def __init__(self, context, request):
        self.context = context
        self.request = request

    def __call__(self):
        try:
            data = json_body(self.request)
        except DeserializationError as err:
            raise BadRequest(str(err))

        query = data.get("query", None)
        try:
            b_start = int(data.get("b_start", 0))
        except ValueError:
            raise BadRequest("Invalid b_start")
        try:
            b_size = int(data.get("b_size", 25))
        except ValueError:
            raise BadRequest("Invalid b_size")
        sort_on = data.get("sort_on", None)
        sort_order = data.get("sort_order", None)
        try:
            limit = int(data.get("limit", 1000))
        except ValueError:
            raise BadRequest("Invalid limit")
        fullobjects = bool(data.get("fullobjects", False))

        if not query:
            raise BadRequest("No query supplied")

        if sort_order:
            sort_order = "descending" if sort_order == "descending" else "ascending"

        querybuilder = getMultiAdapter(
            (self.context, self.request), name="querybuilderresults"
        )

        querybuilder_parameters = dict(
            query=query,
            brains=True,
            b_start=b_start,
            b_size=b_size,
            sort_on=sort_on,
            sort_order=sort_order,
            limit=limit,
        )

        # Exclude "self" content item from the results when ZCatalog supports NOT UUID
        # queries and it is called on a content object.
        if not IPloneSiteRoot.providedBy(self.context) and SUPPORT_NOT_UUID_QUERIES:
            querybuilder_parameters.update(
                dict(custom_query={"UID": {"not": self.context.UID()}})
            )

        try:
            results = querybuilder(**querybuilder_parameters)
        except KeyError:
            # This can happen if the query has an invalid operation,
            # but plone.app.querystring doesn't raise an exception
            # with specific info.
            raise BadRequest("Invalid query.")

        results = getMultiAdapter((results, self.request), ISerializeToJson)(
            fullobjects=fullobjects
        )
        return results

class QuerystringSearchPost(Service):
    """Returns the querystring search results given a p.a.querystring data."""

    def reply(self):
        querystring_search = QuerystringSearch(self.context, self.request)
        return querystring_search()

class QuerystringSearchGet(Service):
    """Returns the querystring search results given a p.a.querystring data."""

    def reply(self):
        # We need to copy the JSON query parameters from the querystring
        # into the request body, because that's where other code expects to find them
        self.request["BODY"] = parse.unquote(
            self.request.form.get("query", "{}")
        ).encode(self.request.charset)

        # unset the get parameters
        self.request.form = {}
        querystring_search = QuerystringSearch(self.context, self.request)
        return querystring_search()


import argparse
import os
from PIL import Image

# Giảm chiều của ảnh về kích thước cho trước
def resize_image(image, size):
    """Giảm kích thước MỘT ảnh về kích thước cho trước"""
    return image.resize(size, Image.ANTIALIAS)

def resize_images(image_dir, output_dir, size):
    """Giảm kích thước các ảnh trong thư mục 'image_dir' và lưu trữ ảnh đã giảm vào thư mục 'output_dir'."""
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    images = os.listdir(image_dir)
    num_images = len(images)
    for i, image in enumerate(images):
        with open(os.path.join(image_dir, image), 'r+b') as f:
            with Image.open(f) as img:
                img = resize_image(img, size)
                img.save(os.path.join(output_dir, image), img.format)
        if (i+1) % 100 == 0:
            print ("[{}/{}] Resized the images and saved into '{}'."
                   .format(i+1, num_images, output_dir))

def main(args):
    image_dir = args.image_dir
    output_dir = args.output_dir
    image_size = [args.image_size, args.image_size]
    resize_images(image_dir, output_dir, image_size)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--image_dir', type=str, default='./data/train2017/',
                        help='directory for train images')
    parser.add_argument('--output_dir', type=str, default='./data/resized2017/',
                        help='directory for saving resized images')
    parser.add_argument('--image_size', type=int, default=256,
                        help='size for image after processing')
    args = parser.parse_args()
    main(args)


'''
I AM TOTALLY STEALING THIS FROM PYTHON CRASH COURSE ALIEN Invasion AND THEN MODIFYING TO USE MEDIEVAL SPRITES AND INCLUDE HITPOINTS AND POWER UPS
Eric Matthes wrote Python Crash Course and it rules
'''
#week 1 got ship to show up on screen
#week 2 got ship to fire bullets
#week 3 got bullets to die when off screen
#this week i included removing bullet from memory and incorporating update function, all from python crash course
import pygame
import sys
import game_functions as gf

from pygame.sprite import Group
from settings import Settings
from ship import Ship
from turret import Turret
from enemy import Enemy

def run_game():
    #initialize pygame, settings, and screen object
    ai_settings = Settings()

    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
    #initialize
    pygame.init()
    pygame.display.set_caption("Alien Invasion")

    #set background color
    # bg_color = (230, 230, 230)

    #CREATE A SHIP
    ship = Ship(ai_settings, screen)
    turret = Turret(ai_settings, screen, ship)
    # make a group to store bullets
    bullets = Group()
    enemies = Group()
    # create fleet

    gf.create_fleet(ai_settings, screen, ship, enemies)

    # make an enemy
    enemy = Enemy(ai_settings, screen)

    global current_mouse
    current_mouse = pygame.mouse.get_pos()
    #Start the main loop for the run_game

    while True:
        gf.check_events(ai_settings, screen, ship,bullets)
        ship.update()
        turret.update(ship)
        gf.update_bullets(bullets)
        gf.update_screen(ai_settings, screen, ship, enemies, bullets)
        # print(pygame.mouse.get_pos())
        if pygame.mouse.get_pos() != current_mouse:
            current_mouse = pygame.mouse.get_pos()
            string_mouse = str(current_mouse)
            gf.message_display(ai_settings, string_mouse, screen)
        #Make the most recently drawn screen visible.
        pygame.display.flip()

run_game()


from django.urls import path
from . import views

urlpatterns = [
    path('', views.home),
    path('add/', views.add_reg),
    path('delete/<id>', views.delete_reg),
    path('edit/<id>', views.edit_reg),
    path('update/<id>', views.update_reg)
]


"""
The input to the DNN that's being used in vector based. We store experience tuple
in this Replay Buffer as the agent is interacting with the environment and then
sample a small batch of tuples from it in order to learn.

As, a result, the agent will be able to learn from individual tuple multiple times,
recall rare occurrences/interactions, and in general make better use of past experiences.

@author : Rohith Banka

Project for Udacity NanoDegree in Deep Reinforcement Learning (DRLND)

code expanded and adaptyed from code examples provided by Udacity DRL Team, 2021.

Other References: https://pytorch.org/tutorials/intermediate/reinforcement_q_learning.html

"""

# Import the required packages/modules

import torch
import numpy as np
import random
from collections import deque, namedtuple

def get_device():
    '''To detect the device for training/testing i.e., CPU or GPU'''
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    return device

class ReplayBuffer:
    '''Replay Buffer class to store the agent experiences in the environment.'''

    def __init__(self, action_size, buffer_size, batch_size, seed):
        '''This part of the code defines the action_size, state_size, memory of the Replay Buffer'''
        self.action_size = action_size
        self.memory = deque(maxlen=buffer_size)
        self.batch_size = batch_size
        self.experience = namedtuple("Experience", field_names=['state', 'action', 'reward', 'next_state', 'done'])
        self.seed = random.seed(seed)
        self.device = get_device()

    def add(self, state, action, reward, next_state, done):
        '''Adds state, action, reward, next_state and done status to the memory pool.'''
        exp = self.experience(state, action, reward, next_state, done)
        self.memory.append(exp)

    def get_batch(self):
        '''This method randomly samples a batch of experiences from memory'''
        experiences = random.sample(self.memory, k=self.batch_size)
        states = torch.from_numpy(np.vstack([e.state for e in experiences if e is not None])).float().to(self.device)
        actions = torch.from_numpy(np.vstack([e.action for e in experiences if e is not None])).long().to(self.device)
        rewards = torch.from_numpy(np.vstack([e.reward for e in experiences if e is not None])).float().to(self.device)
        next_states = torch.from_numpy(np.vstack([e.next_state for e in experiences if e is not None])).float().to(self.device)
        dones = torch.from_numpy(np.vstack([e.done for e in experiences if e is not None]).astype(np.uint8)).float().to(self.device)

        return (states, actions, rewards, next_states, dones)

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