"""CovidDiagnosis URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from users import views as usr
from admins import views as admins
from django.contrib.staticfiles.urls import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
from CovidDiagnosis import views as mainView
urlpatterns = [
    path('admin/', admin.site.urls),
    path("", mainView.index, name="index"),
    path("index/", mainView.index, name="index"),
    path("logout/", mainView.logout, name="logout"),
    path("UserLogin/", mainView.UserLogin, name="UserLogin"),
    path("AdminLogin/", mainView.AdminLogin, name="AdminLogin"),
    path("UserRegister/", mainView.UserRegister, name="UserRegister"),

    ### User Side Views
    path("UserRegisterActions/", usr.UserRegisterActions, name="UserRegisterActions"),
    path("UserLoginCheck/", usr.UserLoginCheck, name="UserLoginCheck"),
    path("UserHome/", usr.UserHome, name="UserHome"),
    path("CovidCurrentStatus/", usr.CovidCurrentStatus, name="CovidCurrentStatus"),
    path("UserClinicalDataReports/", usr.UserClinicalDataReports, name="UserClinicalDataReports"),
    path("UserChestXrayAnalysis/",usr.UserChestXrayAnalysis, name="UserChestXrayAnalysis"),
    path("UserResults/", usr.UserResults, name="UserResults"),

    ### Admin Side Views
    path("AdminLoginCheck/", admins.AdminLoginCheck, name="AdminLoginCheck"),
    path("AdminHome/", admins.AdminHome, name="AdminHome"),
    path("ViewRegisteredUsers/", admins.ViewRegisteredUsers, name="ViewRegisteredUsers"),
    path("AdminActivaUsers/", admins.AdminActivaUsers, name="AdminActivaUsers"),
    path("TestCTScanImageForm/", admins.TestCTScanImageForm, name="TestCTScanImageForm"),
    path("UploadImageAction/", admins.UploadImageAction, name="UploadImageAction"),

]

urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


from PIL import Image
import os

NIVEIS_DE_CINZA = 256 #rk

def to_gray_scale(img: Image) -> list:
    img_gray = Image.open(img.filename).convert('L')
    img_gray.save('./gray_scale/' + img.filename.split('/')[-1])

    return Image.open('./gray_scale/' + img.filename.split('/')[-1])

def get_histogram(gray_img_matrix: list, img_lines: int, img_columns: int) -> list:
    histogram = [0] * NIVEIS_DE_CINZA
    for l in range (img_lines):
        for c in range (img_columns):
            histogram[ gray_img_matrix[l][c] ] += 1

    return histogram

def get_pr_rk(nk: list) -> list:
    pr_rk = []
    sum_rk = sum(nk)
    for value in nk:
        pr_rk.append( value/sum_rk )

    return  pr_rk

def get_freq(pr_rk: list) -> list:
    freq = []
    freq.append( pr_rk[0] )
    for rk in range (1, NIVEIS_DE_CINZA):
        freq.append( sum( pr_rk[ : rk+1 ] ) )

    return freq

def get_eq(freq: list) -> list:
    eq = []
    for value in freq:
        eq.append( (NIVEIS_DE_CINZA-1)*value )

    return eq

def get_new_rk(eq: list) -> list:
    new_rk = []
    for value in eq:
        new_rk.append( int(value) )

    return new_rk

def equalize(original_img: list, new_rk: list, img_lines: int, img_columns: int):

    new_img = []

    for line in original_img:
        for pixel_value in line:
            new_img.append( new_rk[ pixel_value ] )

    # converte a lista anterior para uma lista 2D:
    new_img = [new_img[offset: offset + img_columns] for offset in range(0, img_columns*img_lines, img_columns)]

    return new_img


import json
import csv
import os

def load_json_data(file_path):
    with open(file_path, 'r') as f:
        return json.load(f)

def extract_data(json_data):
    extracted_data = []
    for entry in json_data:
        titles = ', '.join([title.get('title', 'N/A') for title in entry.get('attributes', {}).get('titles', [])])
        creators = ', '.join([creator.get('name', 'N/A') for creator in entry.get('attributes', {}).get('creators', [])])
        doi = entry.get('attributes', {}).get('doi', 'N/A')
        publisher = entry.get('attributes', {}).get('publisher', 'N/A')
        year_published = entry.get('attributes', {}).get('publicationYear', 'N/A')

        extracted_data.append([titles, creators, doi, publisher, year_published])
    return extracted_data

def save_to_csv(data, file_path):
    with open(file_path, 'w', newline='') as f:
        writer = csv.writer(f)
        writer.writerow(['Title', 'Creators', 'DOI', 'Publisher', 'Year Published'])
        writer.writerows(data)

if __name__ == "__main__":
    json_file_path = os.path.join("..", "data", "datacite_response_data.json")  # Adjust the path as needed
    csv_file_path = os.path.join("..", "data", "datacite_extracted_data.csv")  #

    json_data = load_json_data(json_file_path)
    extracted_data = extract_data(json_data)
    save_to_csv(extracted_data, csv_file_path)


# -*- coding: gb18030 -*-
# fangpengjun
#

from guis import *
import event.EventCenter as ECenter
from guis.common.PyGUI import PyGUI
from guis.controls.Icon import Icon
from Function import Functor
from Time import Time
import Timer

PLAY_REMAIN_TIME = 1.2
class EnterItemPanel( PyGUI ):
	def __init__( self, panel ):
		PyGUI.__init__( self, panel )
		self.visible = True
		self.__pyEnterIcon = Icon( panel.enterIcon )
		self.__pyEnterIcon.visible = True
		self.__pyEnterIcon.icon = ""
		self.playTimerID = 0
		self.isPlaying = False

	# -------------------------------------------------
	def onPlayIcon( self, itemInfo ):
		if itemInfo is None:return
		if not rds.statusMgr.isInWorld():return
		self.isPlaying = True
		self.__pyEnterIcon.visible = True
		self.__pyEnterIcon.icon = itemInfo.icon
		self.endTime = Time.time() + PLAY_REMAIN_TIME
		self.playTimerID = Timer.addTimer( 0.0, 0.1, Functor( self.__playIconEnter, itemInfo ) )

	def __playIconEnter( self, itemInfo ):
		if not self.rvisible:
			self.__cancelPlayTimer()
			return
		remainTime = self.endTime - Time.time()
		self.__pyEnterIcon.top += 8.0
		self.__pyEnterIcon.left += 8.0
		self.__pyEnterIcon.alpha -= 20
		self.__pyEnterIcon.width -= 5.0
		self.__pyEnterIcon.height -= 5.0
		if remainTime <= 0.0:
			name = rds.iconsSound.getDragDownSound( itemInfo.icon[0] )
			rds.soundMgr.playUI( name )
			self.isPlaying = False
			self.__cancelPlayTimer( )

	def __cancelPlayTimer( self ):
		Timer.cancel( self.playTimerID )
		self.__resetIcon()
		self.playTimerID = 0

	def __resetIcon( self ):
		self.__pyEnterIcon.top = 0
		self.__pyEnterIcon.left = 0
		self.__pyEnterIcon.alpha = 255
		self.__pyEnterIcon.size = 48.0, 48.0
		self.__pyEnterIcon.icon = ""

	def reset( self ):
		self.__resetIcon()


from time import sleep
from telepotpro import Bot, glance, api as tgapi
from telepotpro.namedtuple import InlineQueryResultArticle, InputTextMessageContent
from telepotpro.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton
from threading import Thread
from json import load as jsload
from hashlib import sha256

with open("settings.json") as f:
    settings = jsload(f)
    if settings.get("api_server"):
        tgapi.set_api_url(settings["api_server"])

bot = Bot(settings["token"])

def reply(msg):
    chatId = msg['chat']['id']

    if chatId > 0:
        bot.sendMessage(chatId, "Hi! I'm the BM Translator Bot ðŸ‘‹\n"
                                "You can type @bmTransBot on any chat to display a message "
                                "with \"bm style\", like <i>ThIs SeNtEnCe</i>!\n\n"
                                "Press the button below to try now!", parse_mode="HTML",
                        reply_markup=InlineKeyboardMarkup(inline_keyboard=[[
                            InlineKeyboardButton(text="ðŸ’¬ Try me!", switch_inline_query="Hello!")
                        ]]))

def query(msg):
    queryId, chatId, queryString = glance(msg, flavor="inline_query")

    parsed = ""
    upper = True
    for c in queryString:
        if c.isalpha():
            parsed += c.upper() if upper else c.lower()
            upper = not upper
        else:
            parsed += c

    if parsed:
        desc = parsed if len(parsed) < 18 else parsed[:15] + "..."
        shaId = sha256(parsed.encode("utf-8")).hexdigest()
        shaBmId = sha256((parsed+"!11!!1").encode("utf-8")).hexdigest()
        results = [
            InlineQueryResultArticle(
                id=shaId,
                title="BM Translate",
                input_message_content=InputTextMessageContent(
                    message_text=parsed
                ),
                description=desc
            ),
            InlineQueryResultArticle(
                id=shaBmId,
                title="BM Translate!11!!1",
                input_message_content=InputTextMessageContent(
                    message_text=parsed + "!11!!1"
                ),
                description=desc + "!11!!1"
            )
        ]
    else:
        results = []
    bot.answerInlineQuery(queryId, results, cache_time=86400, is_personal=False)

def accept_message(msg):
    Thread(target=reply, args=[msg]).start()

def incoming_query(msg):
    Thread(target=query, args=[msg]).start()

bot.message_loop({'chat': accept_message, 'inline_query': incoming_query})
while True:
    sleep(60)


"""Main FastAPI app instance declaration."""

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.trustedhost import TrustedHostMiddleware

from api.apis import apis
from api.prisma import prisma

app = FastAPI(
    title="Ground Station API",
    openapi_url="/openapi.json",
    docs_url="/",
)
app.include_router(apis)
"""
origins = [#"*"
    "",
    "http://localhost:3000",
    "http://localhost",
    "http://localhost:8080",
]"""

# Sets all CORS enabled origins
app.add_middleware(
    CORSMiddleware,
    allow_origins=  [str(origin) for origin in []], # changed [] to origins and referring to the list above
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.on_event("startup")
async def startup():
    await prisma.connect()
@app.on_event("shutdown")
async def shutdown():
    await prisma.disconnect()
@app.get("/")
def read_root():
    return {"version": "1.0.0"}

# Guards against HTTP Host Header attacks
# app.add_middleware(TrustedHostMiddleware, allowed_hosts=["localhost", "127.0.0.1", "0.0.0.0"])


# Getting the Data Type
# You can get the data type of any object by using the type() function:
x = 5
print(type(x))

# In Python, the data type is set when you assign a value to a variable:
x = "Hello World"	# str
x = 20	# int
x = 20.5	# float
x = 1j	# complex
x = ["apple", "banana", "cherry"]	# list
x = ("apple", "banana", "cherry")	# tuple
x = range(6)	# range
x = {"name" : "John", "age" : 36}	# dict
x = {"apple", "banana", "cherry"}	# set
x = frozenset({"apple", "banana", "cherry"})	# frozenset
x = True	# bool
x = b"Hello"	# bytes
x = bytearray(5)	# bytearray
x = memoryview(bytes(5))	# memoryview

# If you want to specify the data type, you can use the following constructor functions:
x = str("Hello World")	# str
x = int(20)	# int
x = float(20.5)	# float
x = complex(1j)	# complex
x = list(("apple", "banana", "cherry"))	# list
x = tuple(("apple", "banana", "cherry"))	# tuple
x = range(6)	# range
x = dict(name="John", age=36)	# dict
x = set(("apple", "banana", "cherry"))	# set
x = frozenset(("apple", "banana", "cherry"))	# frozenset
x = bool(5)	# bool
x = bytes(5)	# bytes
x = bytearray(5)	# bytearray
x = memoryview(bytes(5))	# memoryview

# NOTE: typecasting will work between string and numerics
x = int(1)   # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
x = float(1)     # x will be 1.0
y = float(2.8)   # y will be 2.8
z = float("3")   # z will be 3.0
w = float("4.2") # w will be 4.2
x = str("s1") # x will be 's1'
y = str(2)    # y will be '2'
z = str(3.0)  # z will be '3.0'

# More examples of typecasting
age = '10'
converted_age = int(age)
age = 10
converted_age = str(age)
age = '10.5'
converted_age = float(age)
print(converted_age) # 10.5

# Example of typecasting with errors
age = '10.5'
converted_age = int(age)
'''Traceback (most recent call last):
    File "<pyshell#35>", line 1, in <module>
    converted_age = int(age)
    ValueError: invalid literal for int() with base 10: '10.5' '''

# NOTE: Multiple assignment can be different TYPES
# fyi; a, b, c = "Kuei", 26, "Taiwan".
a, b, c, d, e="hello", "my", "age", "is", 2
print(a, b, c, d, e)


import json
import torch
from typing import List
from transformers import AutoTokenizer, AutoModel

MODEL_NAME = "bert-base-uncased"

def tokenize_text(text: str, model_name: str=MODEL_NAME) -> dict:
    '''
    This function tokenizes a text as a preparation to calculate text embedding.

    Parameters
    -------------
    text: a text to be tokenized
    model_name: a name of the pretrained model within transformers library

    Returns
    -------------
    dictionary with ids of tokens, token types and attention mask
    '''
    # Load the pre-trained BERT model and tokenizer
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    #generate tokens
    text_tokens = tokenizer(text, padding=True, truncation=True, return_tensors="pt")
    return text_tokens

def generate_embeddings(text: str, model_name: str=MODEL_NAME) -> List[List]:
    '''
    This function generated text embedding from a text (including tokenesation step).

    Parameters
    -------------
    text: a text to be tokenized
    model_name: a name of the pretrained model within transformers library

    Returns
    -------------
    A tensor list with text embedding in accordance to the chosen model
    '''
    model = AutoModel.from_pretrained(model_name)
    text_tokens = tokenize_text(text)
    with torch.no_grad():
        text_outputs = model(**text_tokens)
    text_embeddings = text_outputs.last_hidden_state.mean(dim=1)
    return json.dumps(text_embeddings.tolist())

if __name__=="__main__":
    s = ""
    print(generate_embeddings(s)[0][0].item()==-0.00922924280166626)


from openpyxl.reader.excel import load_workbook

from mapper.DataMapper import convert_col_and_row_to_position
from model.LoadTextModel import LoadTextModel
from model.LoadExcelModel import LoadExcelModel
import aiofiles
from model.Result import Result, Success, Error
from model.dataclass.Fee import Fee
from model.dataclass.Student import Student

note_path = "note.txt"

class ExcelModel(LoadTextModel, LoadExcelModel):
    _instance = None

    # implement singleton pattern
    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super().__new__(cls, *args, **kwargs)
        return cls._instance

    async def read_all_text_from_note(self) -> Result:
        try:
            async with aiofiles.open(file=note_path, mode="r", encoding="utf-8-sig") as file:
                cleaned_text_list = [item.rstrip('\n').split(':', 1)[1] for item in await file.readlines()]
                result = Fee(*cleaned_text_list)

                print(f"Read result successfully: data = \n{result}")
                return Success(data=result)

        except Exception as error:
            if file:
                await file.close()
            print(f"Exception: {error}")
            return Error(exception=error)

        finally:
            if file:
                await file.close()
                print("File was closed successfully")

    async def read_all_data_from_excel(self) -> Result: # need to return list of student
        try:
            work_book = await load_workbook(filename="ReceiptData.xlsx")
            work_sheet = work_book.active
            property_row = work_sheet[2]
            needed_property = []
            student_data = []

            for property_in_row in property_row:
                if property_in_row.value == "姓名" or \
                        property_in_row.value == "月費" or \
                        property_in_row.value == "保險(半年收一次)" or \
                        property_in_row.value == "其他(延拖費)" or \
                        property_in_row.value == "幼兒屬性" or \
                        property_in_row.value == "班別" or \
                        property_in_row.value == "請假減收":
                    needed_property.append(property_in_row.column - 1)

            # print(needed_property)

            for student_property_column in needed_property:
                student_data.append(work_sheet[convert_col_and_row_to_position(col_number=student_property_column , row_number=3)].value)
            student_data.append(work_book.sheetnames[0])

            result = Student(*student_data)

            print(f"Read result successfully: data = \n{result}")
            return Success(data=result)

        except Exception as error:
            print(f"Exception: {error}")
            return Error(exception=error)


# Author: Ronaldlee Ejalu
# Course DSC 540
# Final Exam
# Export the contents of the Materialized view into a json structure.
# Part 3b
import json
import sqlite3
import os
import time

os.chdir('C:/Users/rejalu1/OneDrive - Henry Ford Health System/DSC450/Assignments/FinalExamTakeHome')

# tweetdata = """http://dbgroup.cdm.depaul.edu/DSC450/OneDayOfTweets.txt"""
fileName = 'C:/Users/rejalu1/OneDrive - Henry Ford Health System/DSC450/Assignments/FinalExamTakeHome/OneDayOfTweets.csv'

startTime = time.time()

sqlScipt = """
SELECT * FROM TweetsMv;
"""
conn = sqlite3.connect('dsc450.db')                                                                        # open the connection
cur = conn.cursor()                                                                                # instantiate a cursor object

# create the table
cur.execute(sqlScipt)
rows = [rec for rec in cur]                                 # derive a list of rows
rowL = []
tableColumns = [col[0] for col in cur.description]                                   # derive a list of columns

for tablerow in rows:
    tDict = {}
    for tCol, tRow in zip(tableColumns, tablerow):  # using zip() which an iterator to conserve memory.
        tDict[tCol] = tRow
        rowL.append(tDict)
# print(rowL)
# jsonStr = json.dumps(rowL)      # derive a string representation of your table schema as a json object
# write the json object to a file
with open('tableJson.json', 'w') as outfile:
    json.dump(rowL, outfile)

endTime = time.time()
print('The processing of the tweets data took %s seconds' %(endTime-startTime))

conn.close()


import os.path as path

from scrapers import old, new

def _check_files():
    if not path.exists('data/results.csv'):
        with open('data/summary.csv', 'w') as f:
            f.write(';'.join(['rok', 'hlasy', 'zpracovano', 'zpracovano_procenta',
                              'zastupitelstva', 'zvolena_zastupitelstva', 'volici',
                              'vydane_obalky', 'ucast_procenta', 'odevzdane_obalky',
                              'platne_hlasy']) + '\n')

    if not path.exists('data/party_votes.csv'):
        with open('data/party_votes.csv', 'w') as f:
            f.write(';'.join(['rok', 'por_c_strany', 'strana', 'hlasy', 'hlasy_procenta',
                              'zastupitele', 'zastupitele_procenta']) + '\n')

    if not path.exists('data/candidates.csv'):
        with open('data/candidates.csv', 'w') as f:
            f.write(';'.join(['rok', 'strana_id', 'strana', 'poradove_cislo', 'jmeno',
                              'tituly', 'vek', 'navrhovana_str', 'prislusnost', 'hlasy',
                              'hlasy_procenta', 'poradi', 'mandat']) + '\n')

def scrape(city):
    _check_files()
    print("Scraping results...")
    old.scrape(city)
    new.scrape(city)
    print("Scraping done!")

if __name__ == "__main__":
    scrape(city='Plzeň')


import time
import logging

timer_stats = dict()

class Timer(object):
    def __init__(self, name: 'str'):
        self.logger = logging.getLogger(f'Timer - {name}')
        self.name = name

        if name not in timer_stats:
            timer_stats[name] = [0, 0]

    def __enter__(self):
        self.tstart = time.time()

    def __exit__(self, type, value, traceback):
        time_taken = time.time() - self.tstart
        timer_stats[self.name][0] += time_taken
        timer_stats[self.name][1] += 1
        if time_taken < 0.001:
            self.logger.debug(f'This iteration took {time_taken * 1000000:.0f}(\u03BCs).')
        elif time_taken < 1:
            self.logger.debug(f'This iteration took {time_taken * 1000:.3f}(ms).')
        elif 1 <= time_taken <= 60:
            self.logger.debug(f'This iteration took {time_taken:.3f}(s).')
        elif time_taken > 3600:
            self.logger.debug(f'This iteration took {time_taken / 3600:.0f}(h).')
        elif time_taken > 60:
            self.logger.debug(f'This iteration took {time_taken / 60:.0f}(m).')

    @staticmethod
    def report(name):
        logging.getLogger(f'Timer Report - {name}').info(f'Total time: {timer_stats[name][0]:.3f}(s) - Average time: {timer_stats[name][0] / timer_stats[name][1]:.3f}(s) - Iterations: {timer_stats[name][1]}')

    @staticmethod
    def format(name):
        return f'Total time: {timer_stats[name][0]:.3f}(s) - Average time: {timer_stats[name][0] / timer_stats[name][1]:.3f}(s) - Iterations: {timer_stats[name][1]}'


"""This server can only handle one request a time"""

from flask import Flask
from time import sleep

app = Flask(__name__)

@app.route('/helloworld', methods=['GET'])
def hello_world():
    print('Receive request')
    sleep(10)
    return 'Hello world!'

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


import os
import optparse

def extract(dir, url, ext):
    file_names = []
    for root, dirs, files in os.walk(dir):
        for f in files:
            if f.endswith("." + ext):
                relDir = os.path.relpath(root, dir)
                if relDir != dir:
                    file_names.append(url + os.path.join(relDir, f))
                else:
                    file_names.append(url + f)

    return file_names

def save(files):
    file = open('files.txt', 'w')
    for f in files:
        file.write(f + "\n")

def main():
    parser = optparse.OptionParser(usage="usage: %prog [options] path", version = '%prog version 1.0')
    parser.add_option('-d', '--dir', help ='Directory for the search')
    parser.add_option('-u', '--url', help = 'URL which will be added as a sufix')
    parser.add_option('-e', '--extension', help = 'File extension without dot')

    (opts, args) = parser.parse_args()
    dir = opts.dir
    url = opts.url
    ext = opts.extension

    files = extract(dir, url, ext)
    save(files)

if __name__ == '__main__':
    main()


counts=dict()

file = input("enter file name:")
f=open(file)

for l in f:
    l=l.rstrip()
    words = l.split()
    for word in words:
        counts[word]=counts.get(word,0)+1

print("Counts ",counts)

bigcount = None
bigword = None
for word,count in counts.items():
    if bigcount is None or count>bigcount:
        bigcount=count
        bigword=word

print(bigword,bigcount)

ch9 Dictionaries/files with Dictionaries.py


import boto3
import os

class DataFetcher:
    def __init__(self):
        """
        Initialize the DataFetcher with S3 client.
        """
        self.s3_client = boto3.client('s3', aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID'),
                                      aws_secret_access_key=os.getenv('AWS_SECRET_ACCESS_KEY'),
                                      region_name=os.getenv('AWS_REGION'))

    def fetch(self, processed_files=[]):
        """
        Fetch unprocessed raw files from S3 bucket.

        Parameters:
            processed_files (list): List of file names that have already been processed.

        Returns:
            list: List of raw data files fetched from S3.
        """
        bucket_name = os.getenv('S3_BUCKET_NAME')
        unprocessed_files = []

        try:
            s3_objects = self.s3_client.list_objects_v2(Bucket=bucket_name)['Contents']
            for obj in s3_objects:
                file_name = obj['Key']
                if file_name not in processed_files:
                    file_data = self.s3_client.get_object(Bucket=bucket_name, Key=file_name)['Body'].read()
                    unprocessed_files.append({'name': file_name, 'data': file_data})
        except Exception as e:
            print(f"Error fetching data from S3: {e}")

        return unprocessed_files


from rdflib import Graph, Literal, Namespace
import random

# create namespaces
baseNs = Namespace("http://myontology.com/")
RDF = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
MYONT = Namespace(baseNs['myont/'])
SCHEMA = Namespace("https://schema.org/")
EDUSE = Namespace(baseNs['gebruiken/'])
TAXONOMIES = Namespace(baseNs['taxonomieen/'])
EXERCISES = Namespace(baseNs['oefeningen/'])

# create input graph
inputGraph = Graph()
inputGraph.parse('../rdf-data/educationalUses.ttl')
inputGraph.parse('../rdf-data/educationalTaxonomies.ttl')

# create output graphs
resultPhaseGraph = Graph()
resultLessonGraph = Graph()
resultExerciseGraph = Graph()

resultGraphs = [resultPhaseGraph, resultLessonGraph, resultExerciseGraph]
resultPhaseGraph.parse('../rdf-data/generated-data/generated-phases-with-excerpts.ttl')
resultLessonGraph.parse('../rdf-data/generated-data/generated-lessons.ttl')
resultExerciseGraph.parse('../rdf-data/real-data/exercises.ttl')

# avoid double entries when reruning script
resultLessonGraph.remove((None, MYONT["semanticalDensity"], None))

resultPhaseGraph.remove((None, SCHEMA['interactivityType'], None))
resultPhaseGraph.remove((None, SCHEMA['educationalUse'], None))
resultPhaseGraph.remove((None, MYONT['interactivityLevel'], None))
resultPhaseGraph.remove((None, MYONT['hasCategory'], None))

resultExerciseGraph.remove((None, MYONT['hasCategory'], None))

# bind output graphs
for g in resultGraphs:
    g.bind('rdf', RDF)
    g.bind('myont', MYONT)
    g.bind('schema', SCHEMA)
    g.bind('eduse', EDUSE)
    g.bind('edutaxonomies', TAXONOMIES)
    g.bind('exercise', EXERCISES)

phases = [s for s, _, _ in resultPhaseGraph.triples((None, RDF['type'], MYONT['LessonPhase']))]
lessons = [s for s, _, _ in resultLessonGraph.triples((None, RDF['type'], MYONT['Lesson']))]
exercises = [s for s, _, _ in resultExerciseGraph.triples((None, RDF['type'], MYONT['EducationalExercise']))]

interactivityTypes = [Literal('actief', lang="nl"), Literal('receptief', lang="nl"), Literal('gemengd', lang="nl")]
interactivityLevels = [Literal('Erg laag', lang="nl"),
                        Literal('laag', lang="nl"),
                        Literal('gemiddeld', lang="nl"),
                        Literal('hoog', lang="nl"),
                        Literal('Erg hoog', lang="nl")]

semanticalDensities = [Literal('Erg laag', lang="nl"),
                        Literal('laag', lang="nl"),
                        Literal('gemiddeld', lang="nl"),
                        Literal('hoog', lang="nl"),
                        Literal('Erg hoog', lang="nl")]

educationalUses = [s for s, _, _ in inputGraph.triples((None, SCHEMA['inDefinedTermSet'], EDUSE['set']))]
educationalTaxonomyLevels = [s for s, _, _ in inputGraph.triples((None, SCHEMA['inCodeSet'], TAXONOMIES['revisedBloom']))]

# add data for every phase
for phase in phases:
    resultPhaseGraph.add((phase, SCHEMA['interactivityType'], random.choice(interactivityTypes)))
    resultPhaseGraph.add((phase, SCHEMA['educationalUse'], random.choice(educationalUses)))
    resultPhaseGraph.add((phase, MYONT['interactivityLevel'], random.choice(interactivityLevels)))
    resultPhaseGraph.add((phase, MYONT['hasCategory'], random.choice(educationalTaxonomyLevels)))

# add data for every lesson
for lesson in lessons:
    resultLessonGraph.add((lesson, MYONT['semanticalDensity'], random.choice(semanticalDensities)))

# add data for every exercise
for exercise in exercises:
    resultExerciseGraph.add((exercise, MYONT['hasCategory'], random.choice(educationalTaxonomyLevels)))

# write data
resultPhaseGraph.serialize('../rdf-data/generated-data/generated-phases-with-excerpts.ttl', format='turtle')
resultLessonGraph.serialize('../rdf-data/generated-data/generated-lessons.ttl', format='turtle')
resultExerciseGraph.serialize('../rdf-data/real-data/exercises.ttl', format='turtle')


# coding: utf-8
import logging

from common.settings import settings

# Logging
logger = logging.getLogger(__name__)

# Vérifie que autobahn est bien installé
try:
    from autobahn.asyncio import websocket
except (ImportError, RuntimeError):
    try:
        from autobahn.twisted import websocket
    except ImportError:
        websocket = None

if websocket:

    class BroadcastServerProtocol(websocket.WebSocketServerProtocol):
        """
        Protocole de broadcasting par websocket
        """

        def onOpen(self):
            logger.info("=> {}".format(self.peer))
            self.factory.register(self)

        def onMessage(self, payload, isBinary):
            logger.info("[{}] {}".format(self.peer, payload))
            self.factory.broadcast(payload, self)

        def onClose(self, wasClean, code, reason):
            logger.info("<= {}".format(self.peer))

    class BroadcastServerFactory(websocket.WebSocketServerFactory):
        """
        Service de broadcasting par websocket
        """

        def __init__(self, url, debug=False, debugCodePaths=False):
            super().__init__(url, debug=debug, debugCodePaths=debugCodePaths)
            self.clients = []

        def register(self, client):
            if client not in self.clients:
                self.clients.append(client)

        def unregister(self, client):
            if client in self.clients:
                self.clients.remove(client)

        def broadcast(self, data, sender):
            for client in self.clients:
                if sender and client.peer != sender.peer:
                    client.sendMessage(data)

    def run_websocket_server():
        """
        Démarre le service de broadcasting par websocket
        :return: Rien
        """
        import asyncio

        factory = BroadcastServerFactory(settings.WEBSOCKET_URL, debug=settings.WEBSOCKET_DEBUG)
        factory.protocol = BroadcastServerProtocol
        factory.setProtocolOptions()
        loop = asyncio.get_event_loop()
        server = loop.create_server(factory, settings.WEBSOCKET_HOST, settings.WEBSOCKET_PORT)
        server = loop.run_until_complete(server)
        try:
            loop.run_forever()
        except Exception:
            pass
        finally:
            server.close()
            loop.close()

def send_message(message):
    """
    Permet d'envoyer un message quelconque sur le même canal que le serveur de broadcasting par websocket
    :param message: Message
    :return: Rien
    """
    if not settings.WEBSOCKET_ENABLED:
        return
    try:
        import websocket

        ws = websocket.create_connection(settings.WEBSOCKET_URL)
        ws.send(message)
        ws.close()
    except ImportError:
        pass


#!/usr/bin/env python

from googleapiclient.discovery import build
import sys
from bs4 import BeautifulSoup
import urllib
import re
from NLPCore import NLPCoreClient
import operator

def isVisible(input1):
	if input1.parent.name in ['style', 'script', 'head', 'title', '[document]']:
		return False
	elif re.match('<!--.*-->', str(input1.encode('utf-8'))):
		return False
	else:
		return True

def makeQuery(apiKey, engineID, relation, threshold, query, k):
	if int(relation) == 1:
		relationName = "Live_In"
	elif int(relation) == 2:
		relationName = "Located_In"
	elif int(relation) == 3:
		relationName = "OrgBased_In"
	else:
		relationName = "Work_For"

	print("Parameters:")
	print("Client Key	= " + apiKey)
	print("Engine Key 	= " + engineID)
	print("Relation 	= " + relationName)
	print("Threshold 	= " + str(threshold))
	print("Query 		= " + query)
	print("# of tuples 	= " + str(k))
	iterationNum = 1
	goodTuples = 0
	extractedRelations = 0
	totalExtractedRelations = 0
	tuples = set()
	queries = set()
	while goodTuples < int(k):
		totalExtractedRelations = goodTuples
		service = build("customsearch", "v1",
		developerKey=apiKey)

		res = service.cse().list(
			q=query,
			cx=engineID,
		).execute()
		print("=========== Iteration: " + str(iterationNum) + " - Query: " + query + " ===========")
		for i in range(10):
			extractedRelations = 0
			solution = res[u'items'][i][u'link'].encode('ascii','ignore')
			print("Processing: " + solution)
			try:
				r = urllib.urlopen(solution).read()
			except Exception as e:
				print("Program could not extract text content from this web site; moving to the next one...")
				continue
			soup = BeautifulSoup(r)
			texts = soup.find_all(['h1','h2','h3','p'])
			result = []
			for text in texts:
				result.append(text.text.encode('ascii','ignore'))
			client = NLPCoreClient('stanford-corenlp-full-2017-06-09')
			properties = {
				"annotators": "tokenize,ssplit,pos,lemma,ner", #Second pipeline; leave out parse,relation for first
				"parse.model": "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz", #Must be present for the second pipeline!
				"ner.useSUTime": "0"
				}
			properties2 = {
				"annotators": "tokenize,ssplit,pos,lemma,ner,parse,relation", #Second pipeline; leave out parse,relation for first
				"parse.model": "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz", #Must be present for the second pipeline!
				"ner.useSUTime": "0"
				}
			doc = client.annotate(text=result, properties=properties)
			goodSentences = []
			for sen in doc.sentences:
				tok1 = False
				tok2 = False
				if int(relation) == 1:
					for re in sen.tokens:
						if re.ner == "PERSON":
							tok1 = True
						elif re.ner == "LOCATION":
							tok2 = True
				elif int(relation) == 2:
					for re in sen.tokens:
						if re.ner == "LOCATION" and tok1 == False:
							tok1 = True
						elif tok1 == True and re.ner == "LOCATION":
							tok2 = True
				elif int(relation) == 3:
					for re in sen.tokens:
						if re.ner == "ORGANIZATION":
							tok1 = True
						elif re.ner == "LOCATION":
							tok2 = True
				else:
					for re in sen.tokens:
						if re.ner == "PERSON":
							tok1 = True
						elif re.ner == "ORGANIZATION":
							tok2 = True
				if tok1 == True and tok2 == True:
					goodSentences.append(sen)
			finalSentences = []
			for sentence in goodSentences:
				newsentence = ""
				for x in sentence.tokens:
					newsentence += " " + x.word.encode('ascii','ignore')
				finalSentences.append(newsentence)
			doc2 = client.annotate(text=finalSentences, properties=properties2)
			list1 = []
			list2 = []
			for s1 in doc2.sentences:
				list1.append(s1)
			for s3 in list1:
				counterr = 0
				for s4 in s3.relations:
					if counterr == 2:
						break
					counterr += 1
					if relationValid(s4, relation, relationName):
						print("=============== EXTRACTED RELATION ===============")
						extractedRelations += 1
						newsentence1 = ""
						for x1 in s3.tokens:
							newsentence1 += " " + x1.word
						print("Sentence: " + newsentence1)
						confidence = s4.probabilities[relationName]
						enTy1 = s4.entities[0].type
						enVa1 = s4.entities[0].value
						enTy2 = s4.entities[1].type
						enVa2 = s4.entities[1].value
						print("RelationType: " + relationName + " | Confidence= " + confidence + " | EntityType1= " + enTy1 + " |")
						print("EntityValue1= " + enVa1 + " | EntityType2= " + enTy2 + " | EntityValue2= " + enVa2 + " |")
						print("============== END OF RELATION DESC ==============")
						if(float(confidence) >= float(threshold) and float(confidence) >= float(s4.probabilities["Live_In"]) and float(confidence) >= float(s4.probabilities["OrgBased_In"]) and float(confidence) >= float(s4.probabilities["Located_In"]) and float(confidence) >= float(s4.probabilities["Work_For"])):
							tuples.add((relationName, round(float(confidence),3), enTy1, enVa1, enTy2, enVa2))
			totalExtractedRelations += extractedRelations
			print("Relations extracted from this website: " + str(extractedRelations) + " (Overall: " + str(totalExtractedRelations) + ")")
		print("Pruning relations below threshold...")
		goodTuples = len(tuples)
		print("Number of tuples after pruning: " + str(goodTuples))
		print("================== ALL RELATIONS =================")
		myTuples = list(tuples)
		myTuples.sort(key=operator.itemgetter(1), reverse = True)
		count = 0
		queries.add(query)
		for tup in myTuples:
			if(count == 0 and enVa2 + " " + enVa1 not in queries):
				query = enVa2 + " " + enVa1
				count = 1
			print("RelationType: " + tup[0] + "  | Confidence: " + str(tup[1]) + "		| Entity #1= " + tup[3] + " (" + tup[2] + ")	| Entity #2: " + tup[5] + " (" + tup[4] + ")")
		if(count==0):
			print("All possible queries have already been used! Breaking Program")
			goodTuples = 1000
		iterationNum += 1

def relationValid(relation, re, reName):
	type1 = relation.entities[0].type
	type2 = relation.entities[1].type
	if int(re) == 1:
		if((type1 == "PEOPLE" and type2 == "LOCATION") or (type1 == "LOCATION" and type2 == "PEOPLE")):
			if(float(relation.probabilities[reName])>=float(relation.probabilities["Live_In"]) and float(relation.probabilities[reName])>=float(relation.probabilities["Located_In"]) and float(relation.probabilities[reName])>=float(relation.probabilities["Work_For"]) and float(relation.probabilities[reName])>=float(relation.probabilities["OrgBased_In"])):
				return True
	elif int(re) == 2:
		if((type1 == "LOCATION" and type2 == "LOCATION")):
			if(float(relation.probabilities[reName])>=float(relation.probabilities["Live_In"]) and float(relation.probabilities[reName])>=float(relation.probabilities["Located_In"]) and float(relation.probabilities[reName])>=float(relation.probabilities["Work_For"]) and float(relation.probabilities[reName])>=float(relation.probabilities["OrgBased_In"])):
				return True
	elif int(re) == 3:
		if((type1 == "ORGANIZATION" and type2 == "LOCATION") or (type1 == "LOCATION" and type2 == "ORGANIZATION")):
			if(float(relation.probabilities[reName])>=float(relation.probabilities["Live_In"]) and float(relation.probabilities[reName])>=float(relation.probabilities["Located_In"]) and float(relation.probabilities[reName])>=float(relation.probabilities["Work_For"]) and float(relation.probabilities[reName])>=float(relation.probabilities["OrgBased_In"])):
				return True
	else:
		if((type1 == "PEOPLE" and type2 == "ORGANIZATION") or (type1 == "ORGANIZATION" and type2 == "PEOPLE")):
			if(float(relation.probabilities[reName])>=float(relation.probabilities["Live_In"]) and float(relation.probabilities[reName])>=float(relation.probabilities["Located_In"]) and float(relation.probabilities[reName])>=float(relation.probabilities["Work_For"]) and float(relation.probabilities[reName])>=float(relation.probabilities["OrgBased_In"])):
				return True
	return False

def main():
	# Build a service object for interacting with the API. Visit
	# the Google APIs Console <http://code.google.com/apis/console>
	# to get an API key for your own application.
	apiKey = sys.argv[1]
	engineID = sys.argv[2]
	relation = sys.argv[3]
	threshold = sys.argv[4]
	query = sys.argv[5]
	k = sys.argv[6]
	makeQuery(apiKey, engineID, relation, threshold, query, k)

if __name__ == '__main__':
	main()


import timeit
import re
input = open("input-2021-10.txt", "r")
input = input.read().split("\n")

illegals = []
incomplete = []

def foo(l):
    stop = False
    while stop == False:
        for i,elem in enumerate(l):
            if elem == ')':
                if l[i-1] == '(':
                    l.pop(i)
                    l.pop(i-1)
                    #print(l)
                elif l[i-1] in ['(','[','{','<']:
                    #print('Illegal')
                    illegals.append(l[i])
                    stop = True
                    break
            elif elem == ']':
                if l[i-1] == '[':
                    l.pop(i)
                    l.pop(i-1)
                    #print(l)
                elif l[i-1] in ['(','[','{','<']:
                    #print('Illegal')
                    illegals.append(l[i])
                    stop = True
                    break
            elif elem == '}':
                if l[i-1] == '{':
                    l.pop(i)
                    l.pop(i-1)
                    #print(l)
                elif l[i-1] in ['(','[','{','<']:
                    #print('Illegal')
                    illegals.append(l[i])
                    stop = True
                    break
            elif elem == '>':
                if l[i-1] == '<':
                    l.pop(i)
                    l.pop(i-1)
                    #print(l)
                elif l[i-1] in ['(','[','{','<']:
                    #print('Illegal')
                    illegals.append(l[i])
                    stop = True
                    break
        if (']' not in l) and ('}' not in l) and (')' not in l) and ('>' not in l):
            stop = True
    if (']' not in l) and ('}' not in l) and (')' not in l) and ('>' not in l):
        incomplete.append(l)
    return(l)

starttime = timeit.default_timer()

#PART-1

for line in input:
    foo(list(line))
error = 0
for illegal in illegals:
    if illegal == ')':
        error +=3
    if illegal == ']':
        error +=57
    if illegal == '}':
        error +=1197
    if illegal == '>':
        error +=25137
print(error)
print("The time difference is :", timeit.default_timer() - starttime)
starttime = timeit.default_timer()

#PART-2
scores = []

for line in incomplete:
    score = 0
    closers = line
    closers.reverse()
    for elem in closers:
        if elem == '(':
            score*=5
            score+=1
        elif elem == '[':
            score*=5
            score+=2
        elif elem == '{':
            score*=5
            score+=3
        elif elem == '<':
            score*=5
            score+=4
    scores.append(score)
scores.sort()
print(scores[int(len(scores)/2)])
print("The time difference is :", timeit.default_timer() - starttime)


# Code Listing #2
# hasher.py 参照

"""
Take an input stream and hash it's contents and return the hash digest
- contains md5 and sha1 implementations.

"""

# hash_stream.py
from hashlib import sha1

def hash_stream_sha1(stream, chunk_size=4096):
    """ Hash a stream of data using sha1 """

    shash = sha1()

    for chunk in iter(lambda: stream.read(chunk_size), ''):
        shash.update(chunk)

    return shash.hexdigest()

def hash_stream_md5(stream, chunk_size=4096):
    """ Hash a stream of data using md5 """

    shash = md5()

    for chunk in iter(lambda: stream.read(chunk_size), ''):
        shash.update(chunk)

    return shash.hexdigest()

# この二つの関数がほとんど同じなので hasher.py のように
# refactoring するというのが本書の議論のようだ。


import os
import sys

CUCKOO_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__)), "..")
sys.path.append(CUCKOO_ROOT)

from lib.cuckoo.common.config import Config
from lib.cuckoo.common.path_utils import path_exists
from lib.cuckoo.core.database import Database

repconf = Config("reporting")
if len(sys.argv) == 2:
    db = Database()
    paths = db.sample_path_by_hash(sys.argv[1])
    if paths is not None:
        paths = [path for path in paths if path_exists(path)]
        if paths:
            print(("Found by db.sample_path_by_hash: {}".format(sys.argv[1])))
            print("\n".join(paths))
else:
    print("provide hash to search")


""" Key
"""

from functools import reduce

from .constants import diff_str

class Key(object):
    """
    Class describing keys used for graph unroll.
    The most basic key is just a simple string
    however you can also add dimension information
    and even information on how to scale inputs
    to networks.

    Parameters
    ----------
    name : str
      String used to refer to the variable (e.g. 'x', 'y'...).
    size : int=1
      Dimension of variable.
    derivatives : List=[]
      This signifies that this key holds a derivative with
      respect to that key.
    scale: (float, float)
      Characteristic location and scale of quantity: used for normalisation.
    """

    def __init__(self, name, size=1, derivatives=[], base_unit=None, scale=(0.0, 1.0)):
        super(Key, self).__init__()
        self.name = name
        self.size = size
        self.derivatives = derivatives
        self.base_unit = base_unit
        self.scale = scale

    @classmethod
    def from_str(cls, name):
        split_name = name.split(diff_str)
        var_name = split_name[0]
        diff_names = Key.convert_list(split_name[1:])
        return cls(var_name, size=1, derivatives=diff_names)

    @classmethod
    def from_tuple(cls, name_size):
        split_name = name_size[0].split(diff_str)
        var_name = split_name[0]
        diff_names = Key.convert_list(split_name[1:])
        return cls(var_name, size=name_size[1], derivatives=diff_names)

    @classmethod
    def convert(cls, name_or_tuple):
        if isinstance(name_or_tuple, str):
            key = Key.from_str(name_or_tuple)
        elif isinstance(name_or_tuple, tuple):
            key = cls.from_tuple(name_or_tuple)
        elif isinstance(name_or_tuple, cls):
            key = name_or_tuple
        else:
            raise ValueError("can only convert string or tuple to key")
        return key

    @staticmethod
    def convert_list(ls):
        keys = []
        for name_or_tuple in ls:
            keys.append(Key.convert(name_or_tuple))
        return keys

    @property
    def unit(self):
        return self.base_unit / reduce(
            lambda x, y: x.base_unit * y.base_unit, self.derivatives
        )

    def __str__(self):
        diff_str = "".join(["__" + x.name for x in self.derivatives])
        return self.name + diff_str

    def __repr__(self):
        return str(self)

    def __eq__(self, obj):
        return isinstance(obj, Key) and str(self) == str(obj)

    def __hash__(self):
        return hash(str(self))

def _length_key_list(list_keys):
    length = 0
    for key in list_keys:
        length += key.size
    return length


import array
class pair:
    def __init__(self):
        self.min=0
        self.max=0
def getMinMax(arr:list,n:int) -> pair:
    minmax=pair

    # if there is only one element
    if n==1:
        minmax.max=arr[0]
        minmax.min=arr[0]
        return minmax

    # if there are more than one element in array
    if arr[0]>arr[1]:
        minmax.max=arr[0]
        minmax.min=arr[1]

    else:
        minmax.max=arr[1]
        minmax.min=arr[0]

    for i in range(2,n):
        if arr[i]>minmax.max:
            minmax.max=arr[i]
        elif arr[i]<minmax.min:
            minmax.min=arr[i]

    return minmax
if __name__=="__main__":
    arr=[123,3456,56,2,0,1]
    arr_size=6
    minmax=getMinMax(arr,arr_size)
    print(minmax.min)
    print(minmax.max)


import numpy as np
import pandas as pd
from tqdm import tqdm

import torch as t
from torch.optim import Adam
import torch.nn.functional as F

import ml.utils as utils
from ml.model.vae.hybrid.batchloader import BatchLoader
from ml.model.vae.hybrid.parameters import Parameters
from ml.model.vae.hybrid.vae import VAE

def get_metrics_dict(ce, aux_ce, kld, val_ce, val_aux_ce, val_kld, batch_size):
    train_ce = ce.data.cpu().numpy() / (1024 * batch_size)
    train_aux_ce = aux_ce.data.cpu().numpy() / (1024 * batch_size)
    train_kl = kld.data.cpu().numpy() / (1024 * batch_size)

    val_ce = val_ce.data.cpu().numpy() / (1024 * batch_size)
    val_aux_ce = val_aux_ce.data.cpu().numpy() / (1024 * batch_size)
    val_kl = val_kld.data.cpu().numpy() / (1024 * batch_size)

    return {
        'train_ce': train_ce,
        'train_aux_ce': train_aux_ce,
        'train_kl': train_kl,
        'valid_ce': val_ce,
        'valid_aux_ce': val_aux_ce,
        'valid_kl': val_kl
    }

def train_vae(path_config, feature_type, hyperparams, model_name):
    t.cuda.empty_cache()
    logger = utils.get_logger()

    is_kmer = ('kmer' in feature_type)
    fpath_data = (path_config['features']
                  if is_kmer else path_config['sequences'])
    dirpath_results = path_config['results']

    use_gpu = t.cuda.is_available()

    batch_loader = BatchLoader(data_path=fpath_data, is_kmer=(is_kmer))
    parameters = Parameters(batch_loader.vocab_size, feature_type=feature_type)

    vae = VAE(parameters)

    if use_gpu:
        vae = vae.cuda()

    optimizer = Adam(vae.parameters(), hyperparams['learning_rate'])

    metrics = []
    min_ce = 1000
    min_vae = None

    num = hyperparams['num_iterations']

    for iteration in tqdm(range(num), total=num):
        '''Train step'''
        input, decoder_input, target = batch_loader.next_batch(
            hyperparams['batch_size'], 'train', use_gpu)
        target = target.view(-1)

        logits, aux_logits, kld = vae(
            hyperparams['dropout'], input, decoder_input)

        logits = logits.view(-1, batch_loader.vocab_size)
        cross_entropy = F.cross_entropy(logits, target, size_average=False)

        aux_logits = aux_logits.view(-1, batch_loader.vocab_size)
        aux_cross_entropy = F.cross_entropy(
            aux_logits, target, size_average=False)

        loss = cross_entropy + hyperparams['aux'] * aux_cross_entropy + kld

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        '''Validation'''
        input, decoder_input, target = batch_loader.next_batch(
            hyperparams['batch_size'], 'valid', use_gpu)
        target = target.view(-1)

        logits, aux_logits, valid_kld = vae(
            hyperparams['dropout'], input, decoder_input)

        logits = logits.view(-1, batch_loader.vocab_size)
        valid_cross_entropy = F.cross_entropy(
            logits, target, size_average=False)

        aux_logits = aux_logits.view(-1, batch_loader.vocab_size)
        valid_aux_cross_entropy = F.cross_entropy(
            aux_logits, target, size_average=False)

        loss = valid_cross_entropy + \
            hyperparams['aux'] * valid_aux_cross_entropy + kld

        if iteration % 50 == 0:
            metrics_dict = get_metrics_dict(
                cross_entropy, aux_cross_entropy, kld,
                valid_cross_entropy, valid_aux_cross_entropy, valid_kld,
                hyperparams['batch_size']
            )
            metrics.append(metrics_dict)

            valid_ce = metrics_dict['valid_ce']

            if valid_ce <= min_ce:
                min_vae_dict = vae.state_dict()
                min_ce = valid_ce
                logger.info(
                    'Saving best model in iteration {}'.format(iteration))
                t.save(vae.state_dict(),
                       '{}/{}_best.pth'.format(dirpath_results, model_name))

    logger.info('Saving final metrics')
    df_metrics = pd.DataFrame(metrics)
    df_metrics.to_csv(
        '{}/{}_metrics.csv'.format(dirpath_results, model_name))


import random
import time

moves = ['rock', 'paper', 'scissors']

def print_pause(message_to_print):
    print(message_to_print)
    time.sleep(1)

class Player:
    my_move = random.choice(moves)
    their_move = random.choice(moves)

    def move(self):
        return

    def learn(self, my_move, their_move):
        pass

class AllRockPlayer(Player):
    def move(self):
        return 'rock'

class RandomPlayer(Player):
    def move(self):
        return random.choice(moves)

class HumanPlayer(Player):
    def move(self):
        return valid_input("Rock, Paper or Scissors? > ", moves)

class ReflectPlayer(Player):
    def move(self):
        return self.their_move

class CyclePlayer(Player):
    def move(self):
        index = moves.index(self.my_move) + 1
        return moves[index % len(moves)]

    def learn(self, my_move, their_move):
        self.my_move = my_move

def beats(one, two):
    return ((one == 'rock' and two == 'scissors') or
            (one == 'scissors' and two == 'paper') or
            (one == 'paper' and two == 'rock'))

class Game:
    p1_score = 0
    p2_score = 0

    def __init__(self, p1, p2):
        self.p1 = p1
        self.p2 = p2

    def play_round(self):
        move1 = self.p1.move()
        move2 = self.p2.move()
        print_pause(f"You choose {move1}.\n"
                    f"Your rival played {move2}.")
        if move1 == move2:
            print_pause("** Draw **")
        if beats(move1, move2):
            print_pause("** Player One Wins! **")
            self.p1_score += 1
        else:
            print_pause("** Player Two Wins! **")
            self.p2_score += 1
        self.p1.learn(move1, move2)
        self.p2.learn(move2, move1)
        print(f"Points: Player One {self.p1_score}, "
              f"Player Two {self.p2_score}\n")

    def play_game(self, rounds):
        for round in range(rounds):
            self.play_round()

def valid_input(prompt, options):
    while True:
        option = input(prompt).lower()
        if option in options:
            return option
        print(f'Sorry, the option "{option}" is invalid. Try again!')

def play_again():
    choice = valid_input('Would you like to play again? (y/n):', ['n', 'y'])
    if choice == 'n':
        print('Thanks for playing! See you next time.')
        exit(0)

def get_rounds():
    options = []
    max_rounds = 10
    for x in range(1, max_rounds + 1):
        options.append(str(x))
    return int(valid_input(f'Rounds? (from 1 to {max_rounds})', options))

def get_opponent():
    return valid_input('Choose the opponent player:\n'
                       '1 - Always rock\n'
                       '2 - Random\n'
                       '3 - Reflect\n'
                       '4 - Cycle\n',
                       ['1', '2', '3', '4'])

def game():
    while True:
        opponents = {
            '1': AllRockPlayer(),
            '2': RandomPlayer(),
            '3': ReflectPlayer(),
            '4': CyclePlayer()
        }
        p1 = HumanPlayer()
        p2 = opponents[get_opponent()]
        game = Game(p1, p2)
        rounds = get_rounds()
        game.play_game(rounds)

        play_again()

if __name__ == '__main__':
    game()


from browser import document, html

game_type_str = ['Play with friend', 'Play against Computer']

# this will toggle game type
def toggle_game_type(event):
    str = document['gameType'].text

    if str == game_type_str[0]:
        document['gameType'].text = game_type_str[1]
    else:
        document['gameType'].text = game_type_str[0]

    player_name()

# this print player name
def player_name():
    str = document['gameType'].text

    if str == game_type_str[0]:
        document['player01Name'].text = 'Computer'
        document['player02Name'].text = 'Player'
    else:
        document['player01Name'].text = 'Player 01'
        document['player02Name'].text = 'Player 02'

def game_type_selection():
    str = document['gameType'].text

    if str == game_type_str[0]:
        return 'PvC'
    else:
        return 'PvP'

def character_selection(game_type):
    if(game_type == 'PvP'):     # taking character for Player-vs-Player game
        player1_char = document['player01Char'].text.upper()
        player2_char = document['player02Char'].text.upper()
        return [player1_char, player2_char, 'Player 1', 'Player 2']
    elif(game_type == 'PvC'):   # taking character for Player-vs-Computer game
        computer_char = document['player01Char'].text.upper()
        player_char = document['player02Char'].text.upper()
        return [computer_char, player_char, 'Computer', 'Player']
    else:
        return ['error']

def reset_board():
    for i in range(1,10):
        document.select('#board' + str(i) + ' p')[0].text = ''
        document['board' + str(i)].classList.remove('bg-clr')

def print_board(game_data):
    placeholder1 = '       |       |       '
    placeholder2 = '-----------------------'

    print('\n\n\n\t\t{}'.format(placeholder1))
    print('\t\t   {}   |   {}   |   {}   '.format( game_data['b7'], game_data['b8'], game_data['b9'] ))
    print('\t\t{}\n\t\t{}\n\t\t{}'.format( placeholder1, placeholder2, placeholder1 ))
    print('\t\t   {}   |   {}   |   {}   '.format( game_data['b4'], game_data['b5'], game_data['b6'] ))
    print('\t\t{}\n\t\t{}\n\t\t{}'.format( placeholder1, placeholder2, placeholder1 ))
    print('\t\t   {}   |   {}   |   {}  '.format( game_data['b1'], game_data['b2'], game_data['b3'] ))
    print('\t\t{}\n\n\n'.format( placeholder1 ))

def get_winner(game_data, player1_char, player2_char):
    # checking Rows
    if game_data['b1'] == game_data['b2'] == game_data['b3'] and game_data['b1'] != ' ':
        winner=game_data['b1']
        winner_board=['1', '2', '3']
    elif game_data['b4'] == game_data['b5'] == game_data['b6'] and game_data['b4'] != ' ':
        winner=game_data['b4']
        winner_board=[4, '5', '6']
    elif game_data['b7'] == game_data['b8'] == game_data['b9'] and game_data['b7'] != ' ':
        winner=game_data['b7']
        winner_board=['7', '8', '9']

    # checking Columns
    elif game_data['b1'] == game_data['b4'] == game_data['b7'] and game_data['b1'] != ' ':
        winner=game_data['b1']
        winner_board=['1', '4', '7']
    elif game_data['b2'] == game_data['b5'] == game_data['b8'] and game_data['b2'] != ' ':
        winner=game_data['b2']
        winner_board=['2', '5', '8']
    elif game_data['b3'] == game_data['b6'] == game_data['b9'] and game_data['b3'] != ' ':
        winner=game_data['b3']
        winner_board=['3', '6', '9']

    # checking Diagonals
    elif game_data['b1'] == game_data['b5'] == game_data['b9'] and game_data['b1'] != ' ':
        winner=game_data['b1']
        winner_board=['1', '5', '9']
    elif game_data['b3'] == game_data['b5'] == game_data['b7'] and game_data['b3'] != ' ':
        winner=game_data['b3']
        winner_board=['3', '5', '7']
    else:
        winner='none'

    # returning the winner name
    if winner == player1_char:
        return ['player 01', winner_board]
    elif winner == player2_char:
        return ['player 02', winner_board]
    else:
        return ['none']

def flash_message(message, trigger):
    document['game-message'].style.display = 'inline'
    document['game-message'].text = message

    if trigger.lower() == 'turn':
        document['game-message'].style.color = '#8c7979'
    else:
        document['game-message'].style.color = '#003e0a'


import dataclasses
import json
import os
import pathlib
from datetime import datetime
from typing import Dict, List

import uvicorn
from fastapi import FastAPI
from google.cloud import bigquery
from google.cloud import storage
from pydantic import BaseModel
from toolz.curried import pipe, map

from team_stats_domain_service.domain.team_stats import TeamStats
from team_stats_domain_service.domain.team_stats_raw import TeamStatsRaw

app = FastAPI()

current_iso_datetime = datetime.now().isoformat()

def add_ingestion_date_to_team_stats(team_stats_domain: Dict) -> Dict:
    team_stats_domain.update({'ingestionDate': current_iso_datetime})

    return team_stats_domain

def deserialize(team_stats_raw_as_dict: Dict) -> TeamStatsRaw:
    from dacite import from_dict
    return from_dict(
        data_class=TeamStatsRaw,
        data=team_stats_raw_as_dict
    )

class Request(BaseModel):
    team_slogans: Dict

class Response(BaseModel):
    message: str

@app.post('/teams/statistics')
async def teams_league_service(request: Request):
    project_id = os.environ.get('PROJECT_ID', 'PROJECT_ID env var is not set.')
    output_dataset = os.environ.get('OUTPUT_DATASET', 'OUTPUT_DATASET env var is not set.')
    output_table = os.environ.get('OUTPUT_TABLE', 'OUTPUT_TABLE env var is not set.')
    input_bucket = os.environ.get('INPUT_BUCKET', 'INPUT_BUCKET env var is not set.')
    input_object = os.environ.get('INPUT_OBJECT', 'INPUT_OBJECT env var is not set.')

    table_id = f'{project_id}.{output_dataset}.{output_table}'

    bigquery_client = bigquery.Client(project=project_id)
    storage_client = storage.Client(project=project_id)

    bucket = storage_client.get_bucket(input_bucket)
    blob = bucket.get_blob(input_object)
    team_stats_raw_list_as_bytes = blob.download_as_bytes()

    team_stats_domains: List[Dict] = list(pipe(
        team_stats_raw_list_as_bytes.strip().split(b'\n'),
        map(lambda team_stats_bytes: json.loads(team_stats_bytes.decode('utf-8'))),
        map(deserialize),
        map(TeamStats.compute_team_stats),
        map(lambda team_stats: team_stats.add_slogan_to_stats(request.team_slogans)),
        map(dataclasses.asdict),
        map(add_ingestion_date_to_team_stats)
    ))

    current_directory = pathlib.Path(__file__).parent
    schema_path = str(current_directory / "schema/team_stats.json")

    schema = bigquery_client.schema_from_json(schema_path)

    job_config = bigquery.LoadJobConfig(
        create_disposition=bigquery.CreateDisposition.CREATE_NEVER,
        write_disposition=bigquery.WriteDisposition.WRITE_APPEND,
        schema=schema,
        source_format=bigquery.SourceFormat.NEWLINE_DELIMITED_JSON
    )

    load_job = bigquery_client.load_table_from_json(
        json_rows=team_stats_domains,
        destination=table_id,
        job_config=job_config
    )

    load_job.result()

    print("#######The GCS Raw file was correctly loaded to the BigQuery table#######")

    return Response(message="Load Team Domain Data to BigQuery")

if __name__ == "__main__":
    uvicorn.run(app, host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))


import pytest
import json

import os
import pytest
import pandas as pd
import numpy as np
from pprint import pprint

import sklearn
from sklearn.model_selection import train_test_split

from starter.ml.data import process_data
from starter.ml.model import train_model, load_model
from starter.inference import inference

from definitions import ROOT_DIR

from fastapi.testclient import TestClient

from main import app

CLIENT = TestClient(app)

def test_greet():
    response = CLIENT.get("/")
    assert response.status_code == 200
    assert response.json() == {"message": "Hello, world!"}

@pytest.mark.asyncio
async def test_predict():
    response = CLIENT.post(
        url="/predict",
        json={
            "age": 35,
            "workclass": "Federal-gov",
            "fnlgt": 39207,
            "education": "HS-grad",
            "education_num": 9,
            "marital_status": "Never-married",
            "occupation": "Adm-clerical",
            "relationship": "Not-in-family",
            "race": "White",
            "sex": "Male",
            "capital_gain": 0,
            "capital_loss": 0,
            "hours_per_week": 40,
            "native_country": "United-States",
        }
    )
    a = ".json"
    a = "json.loads"

    assert response.status_code == 200
    assert "result" in response.json()

def test_post_data_fail():
    a = ".json"
    a = "json.loads"
    data = {"age": -5}
    response = CLIENT.post("/predict/", data=json.dumps(data))
    assert response.status_code == 422

@pytest.fixture(scope="module")
def data():
    a = ".json"
    a = "json.loads"
    data = pd.read_csv(os.path.join(ROOT_DIR, "data/census.csv"))
    data = data.rename(columns={col: col.strip() for col in data.columns})
    return data

@pytest.fixture(scope="module")
def data_test():
    data = pd.read_csv(os.path.join(ROOT_DIR, "data/data_test.csv"))
    return data

def test_data(data):
    """
    Tests to retrieve the data.
    """
    assert isinstance(data, pd.DataFrame)

def test_train_model(data):
    """
    Tests to train the model on the data.
    """
    train, _ = train_test_split(data, test_size=0.20)

    cat_features = [
        "workclass",
        "education",
        "marital-status",
        "occupation",
        "relationship",
        "race",
        "sex",
        "native-country",
    ]
    X_train, y_train, _, _ = process_data(
        train, categorical_features=cat_features, label="salary", training=True,
    )

    model = train_model(X_train, y_train)

    assert isinstance(model, sklearn.linear_model._logistic.LogisticRegression)

def test_load_model():
    """
    Tests to load the model.
    """
    model = load_model("model.pkl")
    assert isinstance(model, sklearn.linear_model._logistic.LogisticRegression)

def test_inference_above(data_test):
    """
    Test the inference of the model for salary>50K.
    """
    mask = data_test["salary"] == ">50K"
    X_test = data_test[mask]
    y_test = data_test[mask].pop("salary")
    dictionary = {">50K": 0, "<=50K": 1}
    y_test = [dictionary[x] for x in y_test]

    y_pred = inference(X_test)

    assert 0 in np.unique(y_pred)
    assert 1 in np.unique(y_pred)

def test_inference_below(data_test):
    """
    Test the inference of the model for salary<=50K
    """
    mask = data_test["salary"] == "<=50K"
    X_test = data_test[mask]
    y_test = data_test[mask].pop("salary")
    dictionary = {">50K": 0, "<=50K": 1}
    y_test = [dictionary[x] for x in y_test]

    y_pred = inference(X_test)

    assert 0 in np.unique(y_pred)
    assert 1 in np.unique(y_pred)

def test_inference_example_above():
    """
    Test the inference on an example above 50K salary.
    """
    headers = "age,workclass,fnlgt,education,education_num,marital_status,occupation,relationship,race,sex,capital_gain,capital_loss,hours_per_week,native_country,salary".split(",")
    data = "33,Private,115496,Doctorate,16,Married-civ-spouse,Prof-specialty,Husband,White,Male,0,0,60,United-States,>50K".split(",")

    dictionary = {">50K": 0, "<=50K": 1}

    X_test = {k: [v] for k, v in zip(headers, data)}
    y_test = X_test.pop("salary")

    y_pred = inference(X_test)

    assert y_pred[0] == dictionary[y_test[0]]

def test_inference_example_below():
    """
    Test the inference on an example below 50K salary.
    """
    headers = "age,workclass,fnlgt,education,education_num,marital_status,occupation,relationship,race,sex,capital_gain,capital_loss,hours_per_week,native_country,salary".split(",")
    data = "52,Without-pay,198262,HS-grad,9,Married-civ-spouse,Adm-clerical,Wife,White,Female,0,0,30,United-States,<=50K".split(",")

    dictionary = {"<=50K": 0, ">50K": 1}

    X_test = {k: [v] for k, v in zip(headers, data)}
    y_test = X_test.pop("salary")

    y_pred = inference(X_test)

    assert y_pred[0] == dictionary[y_test[0]]


decisao=''
while True:
     print('Vamos Calcular Fundos de investimentos \n')
     valor_cota=float(input('Digite o valor da cota: R$ '))
     quantidade_anos=int(input('Digite a quantidade de anos que deseja investir: '))
     quantidade_cotas=int(input('Qual quantidade de cotas deseja comprar: '))
     porcentagem_renda=int(input('Digite a porcentagem de juros que irá render ao mês: '))

     calculo= (valor_cota)/100*(porcentagem_renda) #calcula quanto irá render a parcela mensalmente
     mes_tot_investido= quantidade_anos*12 # vai converter o numero de anos em mes
     total_conta=valor_cota*mes_tot_investido # calcula o valor total da conta sem renda
     novo_total=(valor_cota*quantidade_cotas)+calculo
     print ('O valor investido será de: R$', round(valor_cota*quantidade_cotas,2))
     for x in range(mes_tot_investido):
          if x == 0:
               print('valor do primeiro mes: R$',round(valor_cota*quantidade_cotas,2))  # Valor inicial do investimento
          else:
           novo_total = novo_total + (novo_total * porcentagem_renda / 100)  # Atualiza o novo total com os juros

     print('Valor total ao final dos investimentos: R$',float(round(novo_total,2)))
     print('O rendimento foi totalizado em: R$',float(round(novo_total-(valor_cota*quantidade_cotas),2)))
     from time import sleep
     sleep(3)
     decisao=input('Deseaja continuar? S/N: ')
     decisao=decisao.upper()
     if decisao =='N':
          print('Até Breve...')
          sleep(2)
          break
     else:
          pass


import cv2
import numpy as np
import os
import svgwrite

def locate_letters(image):

    output = image.copy()

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    img_cnt, cnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    boxes = []
    letters = []

    (H,W) = image.shape[:2]

    for i, cnt in enumerate(cnts):

        if hierarchy[0][i][3] == 0:

            (x,y,w,h) = cv2.boundingRect(cnt)
            roi = thresh[y-5:y+h+5, x-5:x+w+5].copy()

            hull = cv2.convexHull(cnts[i], False)
            mask = np.zeros((H,W), dtype=np.uint8)
            mask = cv2.drawContours(mask, [hull], -1, (255,255,255), -1)
            mask = mask[y-5:y+h+5, x-5:x+w+5].copy()

            boxes.append((y-5, y+h+5, x-5, x+w+5))

            roi[mask == 0] = 255
            letters.append(roi)

    return letters, boxes

def preprocess_letters(letters):

    for i, letter in enumerate(letters):

        roi = letter

        """scale_percent = 150
        width = int(roi.shape[1] * scale_percent / 100)
        height = int(roi.shape[0] * scale_percent / 100)
        dim = (width, height)

        roi = cv2.resize(roi, dim, interpolation=cv2.INTER_AREA)"""
        blurred = cv2.GaussianBlur(roi, (7,7), 0)
        ret, thresh = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY|cv2.THRESH_OTSU)

        letters[i] = thresh

    return letters

def affect_letters(letters, mode, ksize=5, iterations=3):

    for i, letter in enumerate(letters):

        if mode == 'erode':
            eroded = cv2.erode(letter, (ksize, ksize), iterations=iterations)
            letters[i] = eroded
        elif mode == 'dilate':
            dilated = cv2.dilate(letter, (ksize, ksize), iterations=iterations)
            letters[i] = dilated

        #cv2.imwrite('Result/{}.png'.format(i), eroded)

    return letters

def generate_svg(image, color='#000000'):

    img_cnt, cnts, hierarchy = cv2.findContours(image.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    outer_points_groups = []
    inner_points_groups = []

    for i, cnt in enumerate(cnts):

        if hierarchy[0][i][3] == 0:

            outer_points = []

            for point in cnt[::2]:

                pnt = (int(point[0][0]),int(point[0][1]))
                outer_points.append(pnt)

            outer_points_groups.append(outer_points)

        elif hierarchy[0][i][3] > 0:

            inner_points = []

            for point in cnt[::2]:

                pnt = (int(point[0][0]),int(point[0][1]))
                inner_points.append(pnt)

            inner_points_groups.append(inner_points)

    dwg = svgwrite.Drawing('static/vector.svg', profile='tiny')

    for group in outer_points_groups:
        dwg.add(dwg.polyline(group, fill=color))

    for group in inner_points_groups:
        dwg.add(dwg.polyline(group, fill='#ffffff'))

    dwg.save()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : train.py
# @Author: harry
# @Date  : 2019/4/12 下午2:59
# @Desc  : MANN training with eager execution enabled

import tensorflow as tf
import matplotlib.pyplot as plt
import os
from callbacks import MyModelCheckpoint, PrintLossCallback
from mann import MANNModel
from constants import *
from hyper_params import *
from utils import convert_sparse

tf.enable_eager_execution()

# define parse functions
def merge_function(*args):
    return args

def get_parse_function(feature_name: str):
    assert feature_name in ['Text', 'Tokens', 'Tags', 'Similar Question Text',
                            'Similar Question Tokens', 'Similar Question Tags',
                            'Dissimilar Question Text', 'Dissimilar Question Tokens',
                            'Dissimilar Question Tags']

    def _parse_function(example_proto):
        features = {
            'Text': tf.FixedLenFeature(shape=(), dtype=tf.string),
            'Tokens': tf.VarLenFeature(dtype=tf.int64),
            'Tags': tf.VarLenFeature(dtype=tf.int64),
            'Similar Question Text': tf.FixedLenFeature(shape=(), dtype=tf.string),
            'Similar Question Tokens': tf.VarLenFeature(dtype=tf.int64),
            'Similar Question Tags': tf.VarLenFeature(dtype=tf.int64),
            'Dissimilar Question Text': tf.FixedLenFeature(shape=(), dtype=tf.string),
            'Dissimilar Question Tokens': tf.VarLenFeature(dtype=tf.int64),
            'Dissimilar Question Tags': tf.VarLenFeature(dtype=tf.int64),
        }
        parsed_features = tf.parse_single_example(serialized=example_proto, features=features)
        convert_sparse(parsed_features, [
            'Tokens',
            'Tags',
            'Similar Question Tokens',
            'Similar Question Tags',
            'Dissimilar Question Tokens',
            'Dissimilar Question Tags',
        ])
        return parsed_features[feature_name]

    return _parse_function

# custom loss function
def triplet_loss(y_true, y_pred):
    # calculate triplet loss
    anchor_pos, anchor_neg = tf.split(y_pred, 2, axis=-1)
    loss = tf.math.maximum(0.0, MARGIN - anchor_pos + anchor_neg)
    loss = tf.math.reduce_sum(loss, axis=0)
    return loss

def main():
    print("Enable Eager Execution: {}".format(tf.executing_eagerly()))

    # prepare data
    dataset = tf.data.TFRecordDataset(filenames=TRAINING_DATA_FILE_LIST) \
        .prefetch(PREFETCH_SIZE)

    dataset_text = dataset.map(get_parse_function('Text'), num_parallel_calls=CPU_CORES)
    dataset_sim_text = dataset.map(get_parse_function('Similar Question Text'), num_parallel_calls=CPU_CORES)
    dataset_dis_text = dataset.map(get_parse_function('Dissimilar Question Text'), num_parallel_calls=CPU_CORES)

    dataset_text = dataset_text.batch(BATCH_SIZE)
    dataset_sim_text = dataset_sim_text.batch(BATCH_SIZE)
    dataset_dis_text = dataset_dis_text.batch(BATCH_SIZE)

    dataset_zipped = tf.data.Dataset.zip((dataset_text, dataset_sim_text, dataset_dis_text))
    dataset_merged = dataset_zipped.map(merge_function, num_parallel_calls=CPU_CORES)

    dummy_dataset = tf.data.Dataset.from_tensor_slices(tf.constant([0], dtype=tf.int64)) \
        .repeat()

    dataset = tf.data.Dataset.zip(datasets=(dataset_merged, dummy_dataset)) \
        .shuffle(buffer_size=tf.constant(SHUFFLE_BUFFER_SIZE, dtype=tf.int64)) \
        .repeat() \
        .prefetch(PREFETCH_SIZE)

    # take a glance at what the dataset looks like
    # sum = 0
    # cnt = 0
    # my_max = 0
    # for x in dataset:
    #     # print(repr(x))
    #     inputs, _ = x
    #     a, b, c = inputs
    #     print(a.shape[1])
    #     print(b.shape[1])
    #     print(c.shape[1])
    #     target = c.shape[1]
    #     sum += int(target)
    #     my_max = max(my_max, target)
    #     cnt += 1
    # print(sum / cnt)
    # print(my_max)
    # return
    for x in dataset.take(1):
        print(type(x))
        print(repr(x))
    return

if __name__ == "__main__":
    main()


class Pipe(object):
    def __init__(self, x, y, speed, w, h, gap):
        self.x = x
        self.y = y
        self.speed = speed
        self.w = w
        self.h = h
        self.gap = gap
        self.dead = False
        self.got_point = False
        self.score = 0

    def scroll(self):
        if not(self.dead):
            self.x = lerp(self.x, self.x - self.speed, 0.1)

    def show(self):
        if not(self.dead):
            rectMode(CENTER)
            fill(255)
            rect(self.x, self.y, self.w, self.h)
            rect(self.x, self.y + self.gap, self.w, self.h)

    def update(self, bird, score):
        if not(self.dead):
            self.x -= self.speed

        if (self.got_point == False):
            if (self.x < bird.x):
                self.score += 1
                self.got_point = True

    def reset(self, maxH, minH):
        self.x = width + self.w
        self.y = random(maxH, minH)
        self.dead = False


import numpy as np
import pandas as pd
import os
from tqdm import tqdm
import scipy
from scipy.io import wavfile
import librosa
import shutil
from utils import FSD50K, ESC50

duration = 10

def wavwrite(fn, data, fs):
    data = np.array(np.around(data * 2**(15)), dtype = "int16")
    wavfile.write(fn, fs, data)

df = pd.read_csv('audio_csv/bg/fsd50k.csv')
for row in tqdm(df.itertuples()):
    fn = FSD50K+f'/FSD50K.{row.split}_audio/{row.fname}'
    audio, fs = librosa.load(fn, sr=None)
    rep = (duration*2*fs)//row.length+1
    rep_audio = np.repeat(audio, rep)
    save_dir = f'source/{row.split}/background/{row.label}/'
    os.makedirs(save_dir, exist_ok=True)
    wavwrite(save_dir+row.fname, rep_audio, fs)

df = pd.read_csv('audio_csv/ev/esc50.csv')
for row in tqdm(df.itertuples()):
    fn = ESC50+f'/{row.fname}'
    audio, fs = librosa.load(fn, sr=None)
    audio = audio[row.st:row.ed]
    # import pdb; pdb.set_trace()
    save_dir = f'source/{row.split}/foreground/{row.label}/'
    os.makedirs(save_dir, exist_ok=True)
    wavwrite(save_dir+row.fname, audio, fs)

for split in ['dev', 'eval']:
    for label in ['rain', 'car_passing']:
        shutil.copytree(
            f'source/{split}/background/{label}',
            f'source/{split}/foreground/{label}'
        )


"""
백준 1389 - 케빈베이컨의 6단계 법칙
케빈 베이컨의 수: 한사람이 각각의 사람들과 연결된 단계?들의 합
케빈 베이컨의 수가 가장 적은 사람을 찾기
중복되면 고유 번호가 적은 사람 출력
첫쨰 줄 유저의 수 N 친구 관계의 수 M
둘째 줄 부터 M개의 불에는 친구 관계
"""

# 생각?! 모든 노드까지의 거리의 합이 가장 적은 노드를 찾으면 될 듯!
# 근데 모든 노드까지의 거리의 합을 어떻게 구하지?!

from collections import deque
def bfs(graph, start):
    # num = 지정노드 기준으로 각 노드까지의 거리 시작은 0
    num = [0] * (N+1)
    visited = [start]
    queue = deque()
    queue.append(start)

    while queue:
        a = queue.popleft()
        for i in graph[a]:
            if i not in visited:
                # 거리 기준으로 1씩 더해줌
                num[i] = num[a] + 1
                visited.append(i)
                queue.append(i)
    return sum(num)

N, M = map(int,input().split())
cgraph = [[] for i in range(N+1)]
# 노드들 입력받아 각 그래프에 넣어주기
for i in range(M) :
    pair = []
    pair .extend(map(int, input().split()))
    one = pair[0]
    two = pair[1]
    if one not in cgraph[two] :
        cgraph[two].append(one)
    if two not in cgraph[one] :
        cgraph[one].append(two)

result = []
for i in range(1, N+1):
    result.append(bfs(cgraph, i))

# 해당하는 노드의 index값(최소) 호출
print(result.index(min(result))+1)


import numpy as np

def sigmoid(z):
    return 1 / (1 + np.exp(-z))

def sigmoid_der(z):
    return sigmoid(z) * (1 - sigmoid(z))

#Propagación hacia delante de la red
def FeedForward(theta1, theta2, X):
    a1= np.array(X)
    a1 = np.hstack([np.ones((a1.shape[0], 1)), a1])
    z2 = np.dot(a1, theta1.T)
    a2 = sigmoid(z2)
    a2 = np.hstack([np.ones((a2.shape[0], 1)), a2])
    z3 = np.dot(a2, theta2.T)
    a3 = sigmoid(z3)
    return z2,a1, a2, a3

#Predecir la etiqueta de una entrada dada una red neuronal entrenada.
def predict(pred):
    p = np.argmax(pred, axis=1)
    return p

#Cálculo del coste sumando la regularización L2
def reg_cost(theta1, theta2, X, y, lambda_):
    m = len(X)
    c = cost(theta1,theta2,X,y)
    thetas = np.concatenate((theta1[:,1:].flatten(), theta2[:,1:].flatten()))

    J = c + L2(thetas,X,y,lambda_)
    return J

def L2(theta, X, y, lambda_):
    m=len(y)
    L2=(lambda_/(2*m))*np.sum(np.sum(np.sum(theta**2)))
    return L2

#Coste de la red de neuronas de dos capas sin regularización
def cost(theta1,theta2 , X, y):
    z2,a1,a2,a3 = FeedForward(theta1, theta2, X)
    m = len(X)
    term1 = np.sum(y * np.log(a3))
    term2 = np.sum((1 - y) * np.log(1 - a3))

    J = (-1 / m) * np.sum(term1 + term2)
    return J

#Calcular el coste y el gradiente para una red neuronal de 2 capas.
def backprop(theta1, theta2, X, y, lambda_):
    m = len(X)
    z2,a1,a2,a3= FeedForward(theta1, theta2, X)

    # Retropropagación
    delta3 = a3 - y
    delta2 = np.dot(delta3, theta2[:, 1:]) * sigmoid_der(z2)

    # Cálculo de gradientes
    grad1 = (1 / m) * np.dot(delta2.T, a1)
    grad2 = (1 / m) * np.dot(delta3.T, a2)

    # Regularización de gradientes
    grad1[:, 1:] += (lambda_ / m) * theta1[:, 1:]
    grad2[:, 1:] += (lambda_ / m) * theta2[:, 1:]

    cost = reg_cost(theta1, theta2, X, y, lambda_)

    return cost, grad1, grad2

def gradientdescent(theta1, theta2, X, y, lambda_, num_iters, alpha):

    epsilon = 0.12

    theta1 = np.random.uniform(-epsilon, epsilon, size=(theta1.shape[0], theta1.shape[1]))
    theta2 = np.random.uniform(-epsilon, epsilon, size=(theta2.shape[0], theta2.shape[1]))

    for i in range(num_iters):
        J, grad1,grad2=backprop(theta1, theta2, X, y, lambda_)

        theta1=theta1-alpha * grad1
        theta2=theta2-alpha * grad2

    return theta1, theta2


from math      import gcd, log
from itertools import combinations, product

def beal(max_A, max_x):
    """See if any A ** x + B ** y equals some C ** z, with gcd(A, B) == 1.
    Consider any 1 <= A,B <= max_A and x,y <= max_x, with x,y prime or 4"""

    Apowers = make_Apowers(max_A, max_x)
    Czroots = make_Czroots(Apowers)

    for (A, B) in combinations(Apowers, 2):
        if gcd(A, B) == 1:
            for (Ax, By) in product(Apowers[A], Apowers[B]):
                Cz = Ax + By
                if Cz in Czroots:
                    C = Czroots[Cz]
                    x, y, z = exponent(Ax, A), exponent(By, B), exponent(Cz, C)
                    print('{} ** {} + {} ** {} == {} ** {} == {}'
                          .format(A, x, B, y, C, z, C ** z))

def make_Apowers(max_A, max_x):
    "A dictionary of {A: [A**3, A**4, ...], ...}"

    exponents = exponents_upto(max_x)
    return {A: [A ** x for x in (exponents if (A != 1) else [3])]
            for A in range(1, max_A+1)}

def make_Czroots(Apowers):
    return {Cz: C for C in Apowers for Cz in Apowers[C]}

def exponents_upto(max_x):
    "Return all odd primes up to max_x, as well as 4"

    exponents = [3, 4] if max_x >= 4 else [3] if max_x == 3 else []

    for x in range(5, max_x, 2):
        if not any(x % p == 0 for p in exponents):
            exponents.append(x)

    return exponents

def exponent(Cz, C):
    """Recover z such that C ** z == Cz (or equivalently z = log Cz base C).
    For exponent(1, 1), arbitrarily choose to return 3"""

    return 3 if (Cz == C == 1) else int(round(log(Cz, C)))

# Example:
# beal(100,100)


# Ejercicio 12:
# Escribir un programa que muestre en pantalla el último elemento de una lista.
# Ejemplo: lista = [1, 2, 3, 4, 5] = 5

lista = [1, 2, 3, 4, 5]

ultimo_elemento = lista[-1]

print(ultimo_elemento)


import hunspell
import os

curr_dir = os.getcwd()
print(curr_dir)
dir1 = curr_dir + '/dictionary'
print(dir1)
h = hunspell.Hunspell('en_US' , hunspell_data_dir=dir1)
print("Word Suggestions ready")

while(True):
	i = input("String?")
	list1 = h.suggest(i)
	print(list1)
	list2 = []
	for s in list1:
		if s.startswith(i):
			list2.append(s)
	print(list2)


import boto3

ecs_client = boto3.client('ecs', region_name='eu-west-1')
cluster_name = '<>'

paginator = ecs_client.get_paginator('list_services')
service_arns = []

for page in paginator.paginate(cluster=cluster_name, PaginationConfig={'PageSize': 100}):
    service_arns.extend(page['serviceArns'])

for arn in service_arns:
    tags = ecs_client.list_tags_for_resource(resourceArn=arn)['tags']
    update_service = False
    for tag in tags:
        if tag['key'] == 'Restart' and tag['value'] == 'True':
            update_service = True
            break

    if update_service:
        ecs_client.update_service(cluster=cluster_name, service=arn, forceNewDeployment=True)
        print(f"Service {arn} Restarting")


import unittest

import regions_source as rs

response = rs.get_response_body()
fact_total = response['total']
fact_default_page = response['items']

class TestTotal(unittest.TestCase):
    def test_total(self):
        self.assertEqual(rs.get_total(), fact_total)

class TestPageSize(unittest.TestCase):
    def test_default_page_size(self):
        self.assertEqual(rs.default_page_size, len(fact_default_page))

    def test_correct_page_sizes(self):
        for page_size in rs.correct_page_sizes:
            self.assertEqual(len(rs.get_by_params(rs.params['by_page_size'], page_size)), page_size)

    def test_incorrect_page_size(self):
        for page_size in rs.incorrect_page_sizes:
            self.assertTrue(
                'error' in rs.get_by_params(rs.params['by_page_size'], page_size, correct=False),
                msg=f'correct for page size {page_size}'
            )

class TestCountryCode(unittest.TestCase):
    def test_correct_country_codes(self):
        for country_code in rs.correct_country_codes:
            codes = rs.get_country_codes(rs.get_by_params(rs.params['by_country_code'], country_code))
            self.assertEqual(len(codes), 1, msg=f'codes for country code {country_code}: {codes}')
            self.assertTrue(country_code in codes, msg=f'different country code: {codes} instead {country_code}')

    def test_incorrect_country_code(self):
        for country_code in rs.incorrect_country_codes:
            regions = rs.get_by_params(rs.params['by_country_code'], country_code, correct=False)
            self.assertTrue('error' in regions, msg=f'correct for country code {country_code}')

    def test_default_country_code(self):
        allowed_codes = set(rs.correct_country_codes)
        listed_codes = rs.get_country_codes(rs.get_regions())
        self.assertTrue(len(listed_codes) >= len(allowed_codes), msg=f'not all country listed by default')

class TestPageNumber(unittest.TestCase):
    def test_default_page(self):
        default_page = rs.get_by_params(
            rs.params['by_page'], rs.default_page_number,
            rs.params['by_page_size'], len(fact_default_page)
        )
        self.assertEqual(default_page, fact_default_page, msg=f'page default value is not {rs.default_page_number}')

    def test_last_page(self):
        total = rs.get_total()

        for page_size in rs.correct_page_sizes:
            last_page_number = rs.get_page_count(page_size, total)
            last_page_size = rs.get_last_page_size(page_size, total)
            fact_last_page_size = len(rs.get_by_params(
                rs.params['by_page'], last_page_number,
                rs.params['by_page_size'], page_size
            ))
            after_last_page_size = len(rs.get_by_params(
                rs.params['by_page'], last_page_number + 1,
                rs.params['by_page_size'], page_size
            ))

            self.assertEqual(
                fact_last_page_size,
                last_page_size,
                msg=f'for page size {page_size} last page contains {fact_last_page_size} instead {last_page_size}'
            )
            self.assertEqual(after_last_page_size, 0, msg=f'pages after last content page is not empty')

    def test_min_page_number(self):
        previous_page = rs.get_by_params(rs.params['by_page'], rs.min_page_number - 1)
        self.assertTrue('error' in previous_page, msg=f'min page number is not equal {rs.min_page_number}')

class TestRegionNameSearch(unittest.TestCase):

    def test_search_by_correct_substring(self):
        for substring in rs.correct_names_substring:
            sb = rs.get_encoded_ascii(substring)
            regions = map(lambda x: x['name'].lower(), rs.get_by_params(rs.params['by_substring'], sb))
            for region in regions:
                self.assertTrue(substring.lower() in region, msg=f'for substring: {substring} region name: {region}')

    def test_search_by_incorrect_substring(self):
        for substring in rs.incorrect_names_substring:
            sb = rs.get_encoded_ascii(substring)
            self.assertTrue(
                'error' in rs.get_by_params(rs.params['by_substring'], sb, correct=False),
                msg=f'correct for substring: {substring}'
            )

    def test_multi_params(self):
        default_params = [
            rs.params['by_substring'],
            rs.get_encoded_ascii(rs.params_default_values['by_substring']),
        ]
        params = [
            (param_type, value) for (param_type, value) in
            zip(rs.params.values(), rs.params_default_values.values()) if param_type != default_params[0]
        ]

        result = rs.get_by_params(*default_params)

        for param in params:
            if param[0] == rs.params['by_substring']:
                param = (param[0], rs.get_encoded_ascii(param[1]))

            self.assertEqual(
                result, rs.get_by_params(*default_params, *param),
                msg=f'different content for parameters {param}'
            )

        self.assertEqual(
            result,
            rs.get_by_params(*default_params, *[x for pair in params for x in pair]),
            msg=f'different content for parameters {params}'
        )

if __name__ == '__main__':
    unittest.main()


from unittest.mock import Mock

import pytest

from apiclient import APIClient, JsonRequestFormatter, JsonResponseHandler, paginated
from apiclient.authentication_methods import NoAuthentication
from apiclient.paginators import set_strategy
from apiclient.request_strategies import BaseRequestStrategy, RequestStrategy
from tests.helpers import client_factory

def next_page_param(response, previous_page_params):
    if response["next"]:
        return {"page": response["next"]}

def next_page_url(response, previous_page_url):
    if response["next"]:
        return response["next"]

class QueryPaginatedClient(APIClient):
    def __init__(self, base_url, **kwargs):
        self.base_url = base_url
        super().__init__(**kwargs)

    @paginated(by_query_params=next_page_param)
    def make_read_request(self):
        return self.get(endpoint=self.base_url)

class UrlPaginatedClient(APIClient):
    def __init__(self, base_url, **kwargs):
        self.base_url = base_url
        super().__init__(**kwargs)

    @paginated(by_url=next_page_url)
    def make_read_request(self):
        return self.get(endpoint=self.base_url)

def test_query_parameter_pagination(mock_requests):
    # Given the response is over three pages
    response_data = [
        {"page1": "data", "next": "page2"},
        {"page2": "data", "next": "page3"},
        {"page3": "data", "next": None},
    ]
    mock_requests.get(
        "mock://testserver.com",
        [
            {"json": {"page1": "data", "next": "page2"}, "status_code": 200},
            {"json": {"page2": "data", "next": "page3"}, "status_code": 200},
            {"json": {"page3": "data", "next": None}, "status_code": 200},
        ],
    )
    # mock_requests.get.side_effect = [build_response(json=page_data) for page_data in response_data]
    client = QueryPaginatedClient(
        base_url="mock://testserver.com",
        authentication_method=NoAuthentication(),
        response_handler=JsonResponseHandler,
        request_formatter=JsonRequestFormatter,
    )
    # And the client has been set up with the SinglePagePaginator
    original_strategy = client.get_request_strategy()
    assert isinstance(original_strategy, RequestStrategy)

    # When I call the client method
    response = list(client.make_read_request())

    # Then two requests are made to get both pages
    assert mock_requests.call_count == 3
    assert len(response) == 3
    assert response == response_data

    # And the clients paginator is reset back to the original.
    assert client.get_request_strategy() == original_strategy

def test_url_parameter_pagination(mock_requests):
    # Given the response is over two pages
    mock_requests.get(
        "mock://testserver.com",
        json={"page1": "data", "next": "mock://testserver.com/page2"},
        status_code=200,
    )
    mock_requests.get("mock://testserver.com/page2", json={"page2": "data", "next": None}, status_code=200)
    response_data = [
        {"page1": "data", "next": "mock://testserver.com/page2"},
        {"page2": "data", "next": None},
    ]
    client = UrlPaginatedClient(
        base_url="mock://testserver.com",
        authentication_method=NoAuthentication(),
        response_handler=JsonResponseHandler,
        request_formatter=JsonRequestFormatter,
    )
    # And the client has been set up with the SinglePagePaginator
    original_strategy = client.get_request_strategy()
    assert isinstance(original_strategy, RequestStrategy)

    # When I call the client method
    response = list(client.make_read_request())

    # Then two requests are made to get both pages
    assert mock_requests.call_count == 2
    assert response == response_data

    # And the clients paginator is reset back to the original.
    assert client.get_request_strategy() == original_strategy

def test_set_strategy_changes_strategy_on_copy_of_client_when_in_context():
    client = client_factory()
    original_strategy = client.get_request_strategy()
    new_strategy = Mock(spec=BaseRequestStrategy)

    with set_strategy(client, new_strategy) as temporary_client:
        assert client.get_request_strategy() == original_strategy
        assert temporary_client.get_request_strategy() == new_strategy

    assert client.get_request_strategy() == original_strategy

def test_context_manager_resets_request_strategy_when_error():
    client = client_factory()
    original_strategy = client.get_request_strategy()
    new_strategy = Mock(spec=BaseRequestStrategy)
    raises_when_called = Mock(side_effect=ValueError("Something went wrong"))

    with pytest.raises(ValueError):
        with set_strategy(client, new_strategy):
            raises_when_called()

    assert client.get_request_strategy() == original_strategy


lista = [ i for i in range(10)]
lista.append(None)

print(
    lista
)

def func1(i):
    if lista[i] is None:
        return 'end'
    return str(lista[i]) + func1(i+1)

print(
    func1(5)
)