from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from django.core.paginator import Paginator
from django.views.decorators.http import require_GET, require_POST, require_http_methods
from django.views.generic import *
from django.db.models import Q
from django.urls import reverse_lazy
from django.utils.translation import get_language
from .models import Corpora, Genres
from .apps import toGenres, toResult, toModal, toNLPjson, optimizeAbstract, generateID
from CorporaD.execute import *
from CorporaD.CorporaDB import corporaDB
from CorporaD.Global.summa import summarizer
from CorporaD.Global import bigram, sozgebolu
import time, os, re, json
from opencorpora.settings import BASE_DIR

temp_folder = BASE_DIR/'static/main/tmp'
remjs = os.path.join(temp_folder, "remove.js")
nlpjson = os.path.join(temp_folder, "nlp.json")
start = float(0)
end   = float(0)

genres_lang = {'kk':'kz', 'ru':'rus', 'en':'en'}

@require_GET
def index(request):
    til = get_language()
    current_genres_column = genres_lang.get(til, 'kz')
    start = time.monotonic()
    if os.path.exists(remjs):
        os.remove(remjs)
    if os.path.exists(nlpjson):
        os.remove(nlpjson)
    context = {}
    CPI = 5
    genres = Genres.objects.all().order_by(current_genres_column)
    toGenres(genres, lang=til)
    context['genres'] = genres
    context['check_kk'] = True if til == 'kk' else False
    context['check_ru'] = True if til == 'ru' else False
    context['check_en'] = True if til == 'en' else False
    if request.is_ajax or request.method == 'GET':
        if request.GET.get('source', None):
            source = request.GET.get('source', None)
            context['source'] = source
            if request.GET.get('filters', None):
                filters = request.GET.get('filters', None)
                if filters != 'all' or filters != '':
                    context['filters'] = filters
                    filterSid = {}
                    if(filters.find(',')):
                        filters = str(filters).split(',')
                        for genre in filters:
                            p = Genres.objects.filter(Q(kz=genre) | Q(rus=genre) | Q(en=genre))
                            if p:
                                filterSid[str(p[0].pk)] = genre
                    else:
                        p = Genres.objects.filter(Q(kz=filters) | Q(rus=filters) | Q(en=filters))
                        if p:
                            filterSid[str(p[0].pk)] = filters
                    context['filterSid'] = filterSid
                    start = time.monotonic()
                    corp = Corpora.objects.raw("SELECT * FROM corpora WHERE MATCH(title,text,annotation,keywords) AGAINST ('%s' IN IN BOOLEAN MODE) and genre_id in(%s)"%(source,",".join(filterSid.keys())))
                    end = time.monotonic()
                else:
                    start = time.monotonic()
                    corp = Corpora.objects.raw("SELECT * FROM corpora WHERE MATCH(title,text,annotation,keywords) AGAINST ('%s' IN IN BOOLEAN MODE)"%source)
                    end = time.monotonic()

            corp = Corpora.objects.raw("SELECT * FROM corpora WHERE MATCH(title,text,annotation,keywords) AGAINST ('%s' IN BOOLEAN MODE)"%source)
            if len(corp) == 0:
                context['results'] = None
                return render(request, 'main/index.html', context)
            elif len(corp) < CPI:
                CPI = len(corp)
            else:
                context['countitem'] = len(corp)
                paginator = Paginator(corp, CPI)
                if 'page' in request.GET:
                    page_num = request.GET['page']
                else:
                    page_num = 1
                page = paginator.get_page(page_num)
                context['page_obj'] = page
                if page.object_list:
                    cid         = generateID(CPI)
                    title       = [obj.title for obj in page.object_list]
                    text        = [obj.text for obj in page.object_list]
                    lemmas      = [obj.lemmas for obj in page.object_list]
                    morphanaliz = [obj.morphanaliz for obj in page.object_list]
                    keywords    = [obj.keywords for obj in page.object_list]
                    annotation  = [obj.annotation for obj in page.object_list]
                    url         = [obj.url for obj in page.object_list]
                    context['results']   = toResult(CPI= CPI, cid= cid, text=text, title= title, keywords= keywords, annotation= annotation, url= url, lang= til)
                    toNLPjson(CPI= CPI, cid= cid, title= title, text= text, lemmas= lemmas, morphanaliz= morphanaliz, annotation= annotation)
            end = time.monotonic()
            context['dttime'] = round(end-start, 5)
            context['null'] = False
            return render(request, 'main/index.html', context)
        context['null'] = True
        return render(request, 'main/index.html', context)
    return render(request, 'main/index.html', context)

@require_POST
def ressumma(request):
    if request.is_ajax and request.method == 'POST':
        if request.POST.get('id', None) and request.POST.get('type', None):
            changeid = request.POST['id']
            types = request.POST['type']
            data = {}
            with open(nlpjson) as f:
                myjson = json.load(f)
            try:
                res = myjson[changeid]
            except KeyError:
                render(request, 'main/index.html')
            data['id'] = changeid
            response = ""
            annotation = res['annotation']
            mydict = {
                'kk': ' Жабу.',
                'en': ' Close.',
                'ru': ' Закрыть.',
                'kk1': 'ары қарай оқу',
                'en1': 'read more',
                'ru1': 'читать далее'
            }
            if types == 'show':
                a = str(annotation) if str(annotation).endswith('.') else str(annotation) + ".\t"
                response = a + ''+mydict[get_language()]+''
            else:
                if ('' or '\n' or ' ' or None) != annotation :
                    obj = optimizeAbstract(annotation)
                else:
                    obj = optimizeAbstract(res['text'])
                response = obj.shortAbstract +''+mydict[get_language()+'1']+''
            data['summa'] = response
            return JsonResponse(data=data)
    return render(request, 'main/index.html')

@require_POST
def handlerpost(request):
    if request.method == 'POST':
        if request.POST.get('id', None):
            changeid = request.POST.get('id')
            data = {}
            with open(nlpjson) as f:
                myjson = json.load(f)
            res = myjson[changeid]
            data['token'] = toModal(res['token'])
            data['lemmas'] = toModal(res['lemmas'])
            data['morphanaliz'] = toModal(res['morphanaliz'])
            data['id'] = changeid
            return JsonResponse(data=data)
    return render(request, 'main/index.html')

@require_POST
def download(request):
    if request.is_ajax and request.method == 'POST':
        if request.POST.get('id', None) and request.POST.get('type', None):
            changeid = request.POST.get('id')
            types = request.POST.get('type', None)
            data = {}
            with open(nlpjson) as f:
                myjson = json.load(f)
            res = myjson[changeid]
            data['title'] = res['title']
            data['fileval'] = res[types]
            return JsonResponse(data=data)
    return render(request, 'main/index.html')

@require_POST
def execpost(request):
    context = {}
    if request.POST.get('text', None) and request.POST.get('text') != '' and request.POST.get('text') != '\n':
        input_val = request.POST['text']
        keywords = ""
        annotation = ""
        lemmatization = ""
        morphanaliz = ""
        #connect db
        ob = corporaDB()
        lencorp = int(ob.Count_corpora()[0]['sany'])

        with open(os.path.join(BASE_DIR,'CorporaD/tmp/text.tmp'),'w',encoding="utf-8") as f:
            f.write(input_val)
        os.system('''cd $HOME/sources/apertium-kaz-rus\ncat "{0}" | apertium -n -d. kaz-rus-tagger > "{1}"'''.format(os.path.join(BASE_DIR,'CorporaD/tmp/text.tmp'), os.path.join(BASE_DIR,'CorporaD/tmp/app.tmp')))
        text = open(os.path.join(papka_korpus,'tmp/text.tmp'),'r',encoding="utf8").read()
        apertium = open(os.path.join(papka_korpus,'tmp/app.tmp'),'r',encoding="utf-8").read()
        morphanaliz = str(EditedApertium_DB(text = text, apertium = apertium))
        outtexts = str(outtexts_DB(aptext = str(morphanaliz)))
        lemmatization = str(train_DB(outtexts=str(outtexts)))
        annotation = summarizer.summarize(text, language="kazakh")
        soz = sozgebolu(outtexts)

        #TF_IDF klasssyndagy konstruktordy qoldanyluy
        TfIdf = tf_idf(text = soz, len_corp = lencorp, objectCorporaDB = ob, length_keywords=5)
        #esepteuler
        tf = TfIdf.tf_esepteu()
        idf = TfIdf.idf_esepteu()
        tfidf = TfIdf.tf_idf_esepteu()

        #bigram klasssyndagy konstruktordy qoldanyluy
        bi = bigram(text = str(outtexts))
        bi_text = [bi.newlemm, bi.lastlemm]

        #bigram TF_IDF klasssyndagy konstruktordy qoldanyluy
        BiTfIdf = bi_tf_idf(text = bi_text, len_corp = lencorp, objectCorporaDB = ob, length_keywords=5)
        bi_tf = BiTfIdf.bi_tf_esepteu()
        bi_idf = BiTfIdf.bi_idf_esepteu()
        bi_tfidf = BiTfIdf.bi_tf_idf_esepteu()
        spis = []
        for x in tfidf:
            spis.append(str(x))
        for x in bi_tfidf:
            spis.append(str(x))

        keywords = ", ".join(spis)

        context['keywords'] = toModal(keywords)
        context['annotation'] = toModal(annotation)
        context['lemma'] = toModal(lemmatization)
        context['morph'] = toModal(morphanaliz)
        context['text'] = input_val
    return JsonResponse(context)

@require_GET
def execute(request):
    return render(request, 'main/execute.html')

@require_GET
def about(request):
    return render(request, 'main/about.html')

@require_GET
def privacy(request):
    return render(request, 'main/privacy.html')


import pygame
import field
"""Обрабатывает события в игре"""

class EventHandler:
    def __init__(self):
        self.evolution_tick = 6

    def event_handler(self, events_, mode_, is_done_, gamefield_, menu_):
        """Обработчик событий игры"""

        done_ = is_done_

        # Должен вернуть пару из mode, done

        for event in events_:
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()

                if (mode_ == 'draw' or mode_ == 'pause') \
                        and mouse_pos[1] < gamefield_.field_size[1] - field.Field.menu_height:
                    gamefield_.click(mouse_pos)

                if mouse_pos[1] > gamefield_.field_size[1] - field.Field.menu_height:
                    mode_ = menu_.click(mouse_pos, mode_)
            if event.type == pygame.QUIT or mode_ == 'quit':
                done_ = True
            elif event.type == pygame.KEYDOWN and mode_ == 'draw':
                if event.key == pygame.K_RETURN:
                    mode_ = 'evolution'  # начало эволюции
                    print('evolution mode on')

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    done_ = True
                if event.key == pygame.K_SPACE:
                    if mode_ == 'evolution':
                        mode_ = 'pause'
                    elif mode_ == 'pause':
                        mode_ = 'evolution'

                # Управление скорростью игры клавишами
                if event.key == pygame.K_UP:
                    self.evolution_tick = min(30, self.evolution_tick + 1)
                if event.key == pygame.K_DOWN:
                    self.evolution_tick = max(1, self.evolution_tick - 1)

        return mode_, done_


fire_data = input().split('#')
water_amount = int(input())
effort = 0
put_out_fires = []
for fire in fire_data:
    fire_type, value = fire.split(' = ')
    value = int(value)
    if fire_type == "High" and value not in range(81, 126):
        continue
    elif fire_type == "Medium" and value not in range(51, 81):
        continue
    elif fire_type == "Low" and value not in range(1, 51):
        continue
    if water_amount >=value:
        water_amount -= value
        effort += value * 0.25
        put_out_fires.append(value)
print("Cells:")
for fire in put_out_fires:
    print(f' - {fire}')
print(f'Effort:{effort: .2f}')
print(f'Total Fire: {sum(put_out_fires)}')


__author__ = 'nelson'
#Esta función es la que se encarga de dividir el array en mitades recursivamente, meter el numero mayor en una lista auxiliar, hasta reducir esa lista auxiliar a 1 o 2 elementos
def indexMayorAux2(lista,listaAux1,numeroMayor):
    if (len(lista) == 1) :#si el len de la lista es 1 mete ese numero en la listaAux que sera retornada
        listaAux1.append(lista[0])
    elif (len(lista) == 2):
        if ((lista[0]>lista[1] and lista[0] != numeroMayor)or(lista[0]<lista[1] and lista[1] == numeroMayor)):#si el len 2 revisa cual es el mayor de esos 2 y que no sea repetido con el mayor
            listaAux1.append(lista[0])
        elif ((lista[0]<lista[1] and lista[1] != numeroMayor)or((lista[0]>lista[1] and lista[0] == numeroMayor))) :
            listaAux1.append(lista[1])
    else:#si es más de 2 divide el problema
        indexMayorAux2(lista[0:len(lista)//2],listaAux1,numeroMayor)
        indexMayorAux2(lista[len(lista)//2:len(lista)],listaAux1,numeroMayor)
    return listaAux1
#Esta Función se encarga de revisar los casos base de 1 o 2 elementos para devolver el mayor, lista de retorno es el array donde se devolvera los 2 numero mayores, #numeromayor es (si ya fue encontrado) el numero mayor y no repetirlo
def indexMayorAux1(lista,listaRetorno,numeroMayor):
    print(lista)
    if (len(lista) == 1 or(len(lista)==2 and (lista[0]>lista[1] and lista[0] != numeroMayor ))) : #revisa el primer caso base donde solo hay un numero en el array
        print(lista[0])                                                                           #o el primer numero del array ya esta buscado como el mayor
        listaRetorno.append(lista[0])
    elif(len(lista)==2 and (lista[1]>lista[0] and lista[1] != numeroMayor)):                    #revisa el segundocaso base donde el segundo numero del array es mayor
        listaRetorno.append(lista[1])                                                           #es mayor que el primero
    else:
        listanueva = []
        lista2 = indexMayorAux2(lista, listanueva,numeroMayor) #llama al segundo auxiliar (donde se aplica el divide y conquista) con la lista, una listanueva para  #guardar el array reducido por las diviciones
        indexMayorAux1(lista2,listaRetorno,numeroMayor)#se llama a si misma para ver si el array se reducio lo suficiente como para ser evaluado en un caso base
    return listaRetorno

#Estuscar a Función se encarga de buscar el index de los numeros
def indexMayor(lista):
    listaRetorno = []#array donde se devolvera los 2 numeros mayores
    numeroMayor = 'a' #esta variable sirve como indicador para saber si ya se tiene el numero mayor y no repetirlo, si es 'a' significa que aun no hay numero mayor
    x =(indexMayorAux1(lista,listaRetorno,numeroMayor)) #llamamos a la primera función auxiliar para buscar el numero mayor
    mayor = lista.index(x[0]);
    numeroMayor = x[0]
    y = (indexMayorAux1(lista,listaRetorno,numeroMayor))
    segundo = lista.index(y[1])
    return "El Indice del numero mayo es: " + str(mayor) + ", el Segundo Indice mayor es: " + str(segundo)

listaPrueba=[1, 5, 2, 6, 7, 82, 0, 9, 8, 25, 4,
             1000,86]
numeroPrueba = 0
listap = indexMayor(listaPrueba)
print(listap)


# Input: m = 3, n = 2
# Output: 3
# Explanation:
# From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
# 1. Right -> Down -> Down
# 2. Down -> Down -> Right
# 3. Down -> Right -> Down

class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        if m == 1 or n == 1:
            return 1

        return self.uniquePaths(m - 1, n) + self.uniquePaths(m, n - 1)


import datetime

from flask_script import Command, Option
from sqlalchemy.orm import load_only

from app import db
from app.resources.users.models import User
from app.resources.user_settings.models import UserSettings
from app.resources.guest_user.schemas import GuestUserSchema
from app.resources.user_settings.helpers import create_default_user_settings
from app.resources.sectors.models import Sector
from app.resources.industries.models import Industry

class AddUserSettings(Command):
    """
    Command to add user settings for missing users

    :arg verbose:        print progress
    :arg dry:
        dry run
    """

    option_list = [
        Option('--verbose', '-v', dest='verbose', action='store_true',
               default=False),
        Option('--dry', '-dry', dest='dry', action='store_true',
               default=False),
    ]

    def run(self, verbose, dry):
        if verbose:
            print('---' + str(datetime.datetime.utcnow()) + '---')
            print('Adding user settings ...')
        try:
            user_all_data = User.query.join(UserSettings,
                UserSettings.user_id == User.row_id, isouter=True).filter(
                UserSettings.user_id == None
            ).all()

            remain_users = list(user_all_data)
            for user in remain_users:
                user = create_default_user_settings(user)
                db.session.add(user)
                db.session.commit()

        except Exception as e:
            raise e
            db.session.rollback()
            print(e)
            exit(1)

        print('---' + str(datetime.datetime.utcnow()) + '---')
        print('Done')


def cyclic_rotate_arr(arr, k):
    for _ in range(k):
        tmp = arr[len(arr) - 1]
        for i in range(len(arr) - 1 - _, 0, -1):
            arr[i], arr[i - 1] = arr[i - 1], arr[i]
        arr[0] = tmp
    return arr

print(cyclic_rotate_arr([1, 2, 3, 4, 5, 6, 7], 2))


import json
import os
import requests
from time import sleep
from bs4 import BeautifulSoup

# file_pass = os.path.abspath('rWebDesign.json')
# temporary_data = open(file_pass, 'r')
# json_dict = json.load(temporary_data)#ダブルをシングルに変換

# get_url_info = requests.get(json_dict['data'][1]['from'], headers=headers)
# print(get_url_info.text)

# print(requests.get(json_dict['data'][1]['from']).url)

def get_redirect():
	target_url = 'https://www.pinterest.com/r/pin/554505772865723124/4995915543595742901/4cf0b7ee262acd41d1b0176d1e3ef047f3209215e827b790dc718ca5c1ea0bf6'
	i = 0
	# with requests.Session() as s:
	# 	p = s.post(login_url, data=payload, headers=headers)
		# cookies = p.cookies
		# print(type(cookies))
		# print(json_dict['data'][1]['from'])
	try:
		while 'www.pinterest' in target_url:
			if i > 20 or 'www.pinterest' in target_url and '/r/' not in target_url:
				return target_url

			sleep(0.5)
			r = requests.get(target_url)
			# print(r.url)
			target_url = r.url
			# print(r.url)
			i += 1
		url = r.url
		done = 'これを保存' + url
	except requests.exceptions.ConnectionError as e:
		url = e.request.url
		done = 'これを保存リンク切れ' + url

	print(done)
	return url

def confirmation_url(got_url):
	check_url = got_url

	if 'www.pinterest' in check_url and '/pin/' in check_url and '/r/' not in check_url:
		r = requests.get(check_url)
		html = BeautifulSoup(r.text, 'html.parser')
		head = html.find('head')
		metatag = head.find('meta', attrs={'property':'og:see_also', 'content':True})
		# print(metatag)
		if metatag['content']:
			check_url = metatag['content']

	return check_url

got_url = get_redirect()
got_url = confirmation_url(got_url)
print(got_url)

# result = [login(login_url, payload) for _ in range(9)]; print(result.count(True))

# import requests
# link = 'https://www.pinterest.com/r/pin/554505772869102844/4995915543595742901/ab9290928e62760b540f32156fb9686925897abcbda51275fe2866dd3442d330'
# ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
# headers = {'User-Agent': ua}

# link = requests.get(link, headers=headers)
# print(link.url)


nama = []
nohp = []
def menu() :
    print ('Selamat datang !\n')
    print ('--Menu--')
    print ('1. Daftar Kontak')
    print ('2. Tambah Kontak')
    print ('3. Keluar')

def daftar_kontak() :
    if len(nama and nohp) <= 0:
        print ('belum ada kontak')
    else :
        for indeks in range(len(nama and nohp)):
            print('Nama: {}'.format(nama[indeks]))
            print('No Telepon: {}'.format(nohp[indeks]))

def tambah_kontak() :
    nama_baru = input('Nama: ')
    nama.append(nama_baru)
    nohp_baru = input('No Telepon: ')
    nohp.append(nohp_baru)

def pesan() :
    print ('Kontak berhasil ditambahkan')

#program utama
while True :
    menu()
    pilih =input("Pilih Menu : ")
    if pilih == ('1') : daftar_kontak()
    elif pilih == ('2') : tambah_kontak() , pesan()
    elif pilih == ('3') : print('Program selesai, sampai jumpa!')
    else : print('Menu tidak tersedia')


import pandas as pd
import time
import csv

def csv_writer(data, path):
    new_list = []
    for i in range(len(data)):
        listys = [data[i]]
        new_list.append(listys)
    with open(path, "w", encoding='utf-8', newline='') as csv_file:
        writer = csv.writer(csv_file)
        writer.writerows(new_list)
        csv_file.close()

def save_csv_header_w(path, data_frame, columns=True, w_or_a='w'):
    data_frame.to_csv(path, mode=w_or_a, encoding='utf-8', index=False, header=columns)

def save_excel(path, data_frame, columns=True):
    writer_on = pd.ExcelWriter(path, engine='xlsxwriter')
    data_frame.to_excel(writer_on, index=False, header=columns)
    writer_on.save()

start_time = time.time()
# Подготовка данных
df_spektr = pd.read_excel('СПЕКТРОНУС.xlsx')
df_spektr = df_spektr.replace('АГ.01.01.07 ', 'АГ.01.01.07')
df_spektr = df_spektr.replace('АГ.02.01.07 ', 'АГ.02.01.07')
df_spektr = df_spektr.replace('АГ.02.01.08M ', 'АГ.02.01.08M')
df_spektr = df_spektr.replace('РА1.040.002M', 'РА1.040.002М')
df_spektr = df_spektr.replace('РА1.040.009 M', 'РА1.040.009М')
df_spektr = df_spektr.replace('РА1.040.020 ', 'РА1.040.020')
df_spektr = df_spektr.replace('РА1.040.021 М2', 'РА1.040.021М2')
df_spektr = df_spektr.replace('РА1.040.021M2', 'РА1.040.021М2')
df_spektr = df_spektr.replace('РА7.200.011 - 01', 'РА7.200.011-01')
df_spektr = df_spektr.replace('РА7.200.012 - 03', 'РА7.200.012-03')
df_spektr = df_spektr.replace('СДТ1.040.301 ', 'СДТ1.040.304')
df_spektr = df_spektr.replace('ЭЛ 4.00.002 ', 'ЭЛ4.00.002')
df_spektr = df_spektr.fillna(0)
df_spektr.drop_duplicates().reset_index(drop=True)
df_spektr = df_spektr.assign(t_prepa=lambda x: x['cn_т_подг_мин'] + x['dart_т_подг_мин'] + x['tour_т_подг_мин'])
df_spektr = df_spektr.assign(t_cutting=lambda x: x['cn_т_маш_мин'] + x['dart_т_маш_мин'] + x['tour_т_маш_мин'])
# Анализ данных
detali_list = df_spektr['обозначение'].unique()
for i in range(len(detali_list)):
    one_detal = df_spektr[df_spektr['обозначение'] == detali_list[i]]
    one_detal = one_detal.sort_values(by='номер_заказа')
    one_detal = one_detal.loc[:, 'номер_заказа': 't_cutting']
    first_order = one_detal['номер_заказа'].min()
    extreme_order = one_detal['номер_заказа'].max()
    df_first_order = one_detal[one_detal['номер_заказа'] == first_order].reset_index(drop=True)
    df_extreme_order = one_detal[one_detal['номер_заказа'] == extreme_order].reset_index(drop=True)
    first_change_cutting = df_first_order.loc[0, 't_cutting']
    second_change_cutting = df_extreme_order.loc[0, 't_cutting']
    first_change_prepa = df_first_order.loc[0, 't_prepa']
    second_change_prepa = df_extreme_order.loc[0, 't_prepa']
    if first_change_cutting != 0:
        connection_cutting = second_change_cutting / first_change_cutting
    else:
        connection_cutting = 0
    if first_change_prepa != 0:
        connection_prepa = second_change_prepa / first_change_prepa
    else:
        connection_prepa = 0
    if connection_prepa < 1 or connection_cutting < 1:
        print('{:.1%}, {:.1%}'.format(connection_prepa, connection_cutting))
print("--- %s seconds ---" % (time.time() - start_time))

исследование СПЕКТРОН и ИБГ/alfa.py


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

urlpatterns = [
    path('', views.webtoon_list, name='webtoon-list'),
    # re_path(r'(?P<pk>\d+)/$', views.post_detail),
    # path('', views.webtoon_detail(), name='webtoon-detail'),

    path('<int:pk>/', views.episode_detail, name='episode-detail'),
    path('webtoon/', views.episode_list, name='episode-list'),
]


import matplotlib.pyplot as plt

def plot_loss(loss_file):
    with open(loss_file, 'r') as file:
        losses = []
        avg_losses = []
        avg = 0
        index = 1
        for line in file:
            loss = float(line.strip())
            losses.append(loss)
            avg += loss
            index += 1
            if index % 100 == 0:
                avg_losses.append(avg / 100)
                avg = 0

    plt.plot(list(range(len(losses))), losses, 'bp')
    plt.title('Train loss  (Binary-Cross-Entropy)')
    plt.xlabel('number of examples')
    plt.ylabel('loss')
    plt.show()

    plt.plot(list(range(len(avg_losses))), avg_losses)
    plt.title('Train average loss  (Binary-Cross-Entropy)')
    plt.xlabel('number of examples / 100')
    plt.ylabel('average loss')
    plt.show()

def plot_auc(train_auc, test_auc):
    with open(train_auc, 'r') as file:
        tr_auc = []
        for line in file:
            tr_auc.append(float(line.strip()))
    with open(test_auc, 'r') as file:
        te_auc = []
        for line in file:
            te_auc.append(float(line.strip()))

    plt.plot(list(range(len(tr_auc))), tr_auc, label='train')
    plt.plot(list(range(len(te_auc))), te_auc, label='test')

    plt.title('edited cancer data AUC score per number of epochs')
    plt.xlabel('epoch')
    plt.ylabel('auc score')
    plt.legend()
    plt.show()

def plot_mul_auc(auc_files, labels, title):
    for auc_file, label in zip(auc_files, labels):
        with open(auc_file, 'r') as file:
            aucs = []
            for line in file:
                aucs.append(float(line.strip()))
            plt.plot(list(range(len(aucs))), aucs, label=label)
    plt.title(title)
    plt.xlabel('epoch')
    plt.ylabel('auc score')
    plt.legend()
    plt.show()

# plot_loss('loss_file1')
# plot_loss('loss_file2')
# plot_loss('loss_file3')
# plot_loss('loss_file100_n')

# plot_auc('train_auc', 'test_auc')
# plot_auc('train_auc_b50', 'test_auc_b50')
# plot_auc('train_auc_b100', 'test_auc_b100')
'''
plot_mul_auc(['test_auc_lr0.001_wd0', 'test_auc_lr0.001_wd0.001', 'test_auc_lr0.001_wd0.0001', 'test_auc_lr0.001_wd1e-05',
              'test_auc_lr0.0001_wd0', 'test_auc_lr0.0001_wd0.001', 'test_auc_lr0.0001_wd0.0001', 'test_auc_lr0.0001_wd1e-05',
              'test_auc_lr1e-05_wd0', 'test_auc_lr1e-05_wd0.001', 'test_auc_lr1e-05_wd0.0001', 'test_auc_lr1e-05_wd1e-05'],
             ['lr=1e-3, wd=0', 'lr=1e-3, wd=1e-3', 'lr=1e-3, wd=1e-4', 'lr=1e-3, wd=1e-5',
              'lr=1e-4, wd=0', 'lr=1e-4, wd=1e-3', 'lr=1e-4, wd=1e-4', 'lr=1e-4, wd=1e-5',
              'lr=1e-5, wd=0', 'lr=1e-5, wd=1e-3', 'lr=1e-5, wd=1e-4', 'lr=1e-5, wd=1e-5'])

plot_mul_auc(['train_auc_lr0.01_wd0', 'train_auc_lr0.01_wd0.001', 'train_auc_lr0.01_wd0.0001', 'train_auc_lr0.01_wd1e-05',
              'train_auc_lr0.1_wd0', 'train_auc_lr0.1_wd0.001', 'train_auc_lr0.1_wd0.0001', 'train_auc_lr0.1_wd1e-05'],
             ['lr=1e-2, wd=0', 'lr=1e-2, wd=1e-3', 'lr=1e-2, wd=1e-4', 'lr=1e-2, wd=1e-5',
              'lr=1e-1, wd=0', 'lr=1e-1, wd=1e-3', 'lr=1e-1, wd=1e-4', 'lr=1e-1, wd=1e-5'])
'''
'''
plot_mul_auc(['train_auc_s', 'train_auc_d', 'test_auc_s', 'test_auc_d'],
             ['siamese model train', 'double model train', 'siamese model test', 'double model test'],
             'Train and test AUC for different models')
'''

'''
lrs = [1e-4, 1e-3, 1e-2, 1e-1]
wds = [1, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5]
models = ['siamese', 'double']
for model in models:
    for option in [3]:
        files = []
        labels = []
        for lr in lrs:
            for wd in wds:
                if model == 'siamese':
                    key = 's'
                elif model == 'double':
                    key = 'd'
                files.append('cancer_grid_o3/cancer_grid_o3_auc/test_auc2_' + key + str(option) + '_lr' + str(lr) + '_wd' + str(wd))
                labels.append('lr=' + str(lr) + ', wd=' + str(wd))
        title = 'Test auc score, ' + model + ' model, option ' + str(option)
        plot_mul_auc(files, labels, title)
'''

def plot_auc_option_2(train, test_w, test_c):
    with open(train, 'r') as file:
        tr_auc = []
        for line in file:
            tr_auc.append(float(line.strip()))
    with open(test_w, 'r') as file:
        te_auc_w = []
        for line in file:
            te_auc_w.append(float(line.strip()))
    with open(test_c, 'r') as file:
        te_auc_c = []
        for line in file:
            te_auc_c.append(float(line.strip()))

    plt.plot(list(range(len(tr_auc))), tr_auc, label='train')
    plt.plot(list(range(len(te_auc_w))), te_auc_w, label='weizmann test')
    plt.plot(list(range(len(te_auc_c))), te_auc_c, label='cancer test')

    plt.title('Option 2 AUC score per number of epochs')
    plt.xlabel('epoch')
    plt.ylabel('auc score')
    plt.legend()
    plt.show()

#plot_auc_option_2('train_auc_2', 'test_auc_2_w', 'test_auc_2_c')
#plot_auc('train_auc_3000', 'test_auc_3000')
#plot_auc('train_auc_3_10tcr1000', 'test_auc_3_10tcr1000')

'''
# weizmann, shugay, cancer, different regularization (dropout=0.1 hdim=30)
plot_mul_auc(['train_w_d01_hdim30', 'test_w_d01_hdim30', 'train_w_d01_hdim30_wd-5', 'test_w_d01_hdim30_wd-5'],
             ['train, wd=0', 'test, wd=0', 'train, wd=1e-5', 'test, wd=1e-5'],
             'LSTM based model with different regularization, Weizmann data')

plot_mul_auc(['train2_s_d01_hdim30', 'test2_s_d01_hdim30', 'train2_s_d01_hdim30_wd-5', 'test2_s_d01_hdim30_wd-5'],
             ['train, wd=0', 'test, wd=0', 'train, wd=1e-5', 'test, wd=1e-5'],
             'LSTM based model with different regularization, Shugay data')

plot_mul_auc(['train_c_d05_hdim30_wd-5', 'test_c_d05_hdim30_wd-5', 'train_c_d01_hdim30_wd-5', 'test_c_d01_hdim30_wd-5'],
             ['train, dropout=0.5', 'test, dropout=0.5', 'train, dropout=0.1', 'test, dropout=0.1'],
             'LSTM based model with different dropouts, cancer data')
'''


from kivy.uix.boxlayout import BoxLayout
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.relativelayout import RelativeLayout
from kivy.properties import ObjectProperty

class Basic_Menu_View_Node(ToggleButton):

	def __init__(self, command_node, group_name, **kwargs):
		kwargs['text'] = command_node.name
		kwargs['group'] = group_name
		super().__init__(**kwargs)
		self.name = command_node.name
		self.command_node = command_node

class MyVerticalMenu(RelativeLayout):

	command_node = ObjectProperty()

	def __init__(self, menu_graph, size_hint_dropdown, right=True, **kwargs):
		'''
		args:
			kwargs must contain both size_hint and pos_hint with both x, y sizes and positions

		# TODO
		# add a scroll bar that would allow me to scroll down the butttons
		# set a min size for both the main buttons and the dropdown options
		'''

		my_args = {'size_hint': (1, 1)}
		super().__init__(**my_args)

		action_layout = BoxLayout(orientation='vertical', **kwargs)
		size_hint_x = self.__size_hint_x(kwargs)
		size_hint_y = self.__size_hint_y(kwargs)
		size_hint = (size_hint_x, size_hint_y)
		x = self.__x_pos_hint(kwargs['pos_hint'], size_hint)
		y = self.__y_pos_hint(kwargs['pos_hint'], size_hint)
		size_hint_x_DD, size_hint_y_DD = size_hint_dropdown

		num_buttons = len(list(menu_graph.G.successors('Root')))
		self.main_btn_list = []
		self.btntxt2DD = {}
		self.dd_btn_list = []

		for i, main_graph_node in enumerate(menu_graph.G.successors('Root')):
			main_btn = menu_graph.G.node[main_graph_node]['view_node']
			action_layout.add_widget(main_btn)
			self.main_btn_list.append(main_btn)

			dd_x = self.__get_pos_x(x, size_hint_x, size_hint_x_DD, right)
			dd_y = self.__get_pos_y(i, num_buttons, size_hint_y, size_hint_y_DD)
			my_args = {'size_hint': (size_hint_x_DD, size_hint_y_DD),
			'pos_hint': {'x': dd_x, 'y': dd_y}}

			DD = MyDropDown(self, **my_args)
			self.btntxt2DD[main_btn.text] = DD
			main_btn.bind(on_press=self._update_main_menu)

			for dd_graph_node in menu_graph.G.successors(main_graph_node):
				dd_btn = menu_graph.G.node[dd_graph_node]['view_node']
				self.dd_btn_list.append(dd_btn)
				DD.add_widget(dd_btn)

		self.add_widget(action_layout)
		self.action_layout = action_layout
		self.active_DD = None

	def _update_main_menu(self, button=None):
		self.active_DD = None
		for btntxt, DD in self.btntxt2DD.items():
			if (button.text == btntxt) and (button.state == 'down'):
				DD.open(button)
				self.active_DD = DD
			else:
				DD.close(button)

		# clost down the dropdown buttons to give user chance to select
		for btn in self.dd_btn_list:
			btn.state = 'normal'

	def btn_select(self, view_node):

		if view_node.state == 'down':
			self.command_node = view_node.command_node

	def on_touch_down(self, touch):

		pass_on_touch = True

		if self.active_DD != None:

			if not self.active_DD.collide_point(*touch.pos):

				self.active_DD.close(None)
				self.active_DD = None
				for btn in self.main_btn_list:
					btn.state = 'normal'

			else:

				hit_btn = None
				for btn in self.dd_btn_list:
					if btn.collide_point(*touch.pos):
						btn.state = 'down'
						hit_btn = btn
					else:
						btn.state = 'normal'

				self.btn_select(hit_btn)

				return True

		super().on_touch_down(touch)

	@staticmethod
	def __x_pos_hint(pos_hint_dict, size_hint_tuple):
		if 'x' in pos_hint_dict:
			return pos_hint_dict['x']
		elif 'right' in pos_hint_dict:
			return pos_hint_dict['right'] - size_hint_tuple[0]
		elif 'center_x' in pos_hint_dict:
			return pos_hint_dict['center_x'] - size_hint_tuple[0] / 2
		elif 'center' in pos_hint_dict:
			return pos_hint_dict['center'][0] - size_hint_tuple[0] / 2
		else:
			raise Exception('Need to define x coordinate in pos_hint')

	@staticmethod
	def __y_pos_hint(pos_hint_dict, size_hint_tuple):
		if 'y' in pos_hint_dict:
			return pos_hint_dict['x']
		elif 'top' in pos_hint_dict:
			return pos_hint_dict['top'] - size_hint_tuple[1]
		elif 'center_y' in pos_hint_dict:
			return pos_hint_dict['center_y'] - size_hint_tuple[1] / 2
		elif 'center' in pos_hint_dict:
			return pos_hint_dict['center'][1] - size_hint_tuple[1] / 2
		else:
			raise Exception('Need to define x coordinate in pos_hint')

	@staticmethod
	def __size_hint_x(kwargs):
		if 'size_hint' in kwargs:
			return kwargs['size_hint'][0]
		elif 'size_hint_x' in kwargs:
			return kwargs['size_hint_x']
		else:
			raise Exception('Need to define x coordinate of size_hint')

	@staticmethod
	def __size_hint_y(kwargs):
		if 'size_hint' in kwargs:
			return kwargs['size_hint'][1]
		elif 'size_hint_y' in kwargs:
			return kwargs['size_hint_y']
		else:
			raise Exception('Need to define y coordinate of size_hint')

	@staticmethod
	def __get_pos_y(i_button, num_buttons, size_hint_y, size_hint_y_DD):
		return size_hint_y - (size_hint_y / num_buttons) * i_button - size_hint_y_DD
		# return (i_button + 1) * (size_hint_y / num_buttons) - size_hint_y_DD

	@staticmethod
	def __get_pos_x(x, size_hint_x, size_hint_x_DD, right=True):
		if right:
			return x + size_hint_x
		else:
			return x - size_hint_x_DD

class MyDropDown(BoxLayout):
	def __init__(self, parent_relative, **kwargs):
		super().__init__(**kwargs)
		self.orientation = 'vertical'
		self.parent_relative = parent_relative
		self.viewed = False

	def open(self, button):
		if self.viewed:
			return

		self.parent_relative.add_widget(self)
		self.viewed = True

	def close(self, button):
		if not self.viewed:
			return

		self.parent_relative.remove_widget(self)
		self.viewed = False


import requests

url = "http://122.51.248.121:8910/"
header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"
}
proxies = {
    "http": "http://47.113.180.76:81"
}
# while True:
resp = requests.get(url, proxies=proxies)
resp.encoding = "utf-8"
print(resp.text)


from google.cloud import bigquery

def load_to_bigquery(event, context):
    client = bigquery.Client()
    dataset_id = 'googleplaystore'
    bucket = event['bucket']
    file = event['name']
    url = 'gs://{0}/{1}'.format(bucket, file)
    dataset_ref = client.dataset(dataset_id)
    job_config = bigquery.LoadJobConfig()
    job_config.skip_leading_rows = 1
    job_config.max_bad_records = 1
    # The source format defaults to CSV, so the line below is optional.
    job_config.source_format = bigquery.SourceFormat.CSV
    load_job = client.load_table_from_uri(
        url,
        dataset_ref.table('play_store'),
        job_config=job_config)  # API request
    print('Starting job {}'.format(load_job.job_id))

    result = load_job.result()  # Waits for table load to complete.
    print(result)
    print('Job finished.')


from fractions import *
import numpy as np
from .utilities import *

# ======================= LinearProgram Class ==================================
class LinearProgram:
    """
    Inner representation of a linear program.
    Contains the following datas :
        - nbVar : the initial number of variables
        - nbConst : number of constraints
        - objectiveFunction : the vector c such that transpose(c)*x is the objective value
        - objectiveVector : the vector b such that constraint i is <= b
        - constraintMatrix
    """

    # _____ Parsing and initialisation _____
    def __init__(self,filename):
        # retrieving data from file
        f = open(filename, 'r')
        input_data = [ s.strip() for s in f.readlines()]
        f.close()

        # formatting to build a LinearProgram object
        self.nbVar = int(input_data[0])
        self.nbConst = int(input_data[1])
        self.objectiveFunction = np.array([convert(u) for u in input_data[2].strip().split()])
        self.constraintVector = np.array([convert(u) for u in input_data[3].strip().split()])
        self.constraintMatrix = np.array([Fraction(0,1)]*self.nbVar*self.nbConst).reshape((self.nbConst,self.nbVar))
        for i in range(self.nbConst):
            l = [convert(u) for u in input_data[4+i].strip().split()]
            for j in range(self.nbVar):
                self.constraintMatrix[i,j]=l[j]

        self.need_2_phases = False # Real value set at computation of the standard form of the LP
        f.close()

    # _____ Output fontions ______
    def __str__(self):
        output_string = "OUTPUT \nThe input linear program is: \n\n"

        obj_func_line = "Maximize  "
        first_coef = True
        for i in range(len(self.objectiveFunction)):
            if (self.objectiveFunction[i]!=0):
                if (not first_coef and self.objectiveFunction[i]>0):
                    obj_func_line += "+ {0}x_{1} ".format(self.objectiveFunction[i], i+1)
                elif (first_coef and self.objectiveFunction[i]>0):
                    first_coef = False
                    obj_func_line += "{0}x_{1} ".format(self.objectiveFunction[i], i+1)
                elif (self.objectiveFunction[i]<0):
                    first_coef = False
                    obj_func_line += "{0}x_{1} ".format(self.objectiveFunction[i], i+1)
        output_string+= obj_func_line+"\n"

        const_line = "Such that "
        for j in range(len(self.constraintMatrix)):
            first_coef = True
            for i in range(len(self.constraintMatrix[j,:])):
                if (not first_coef and self.constraintMatrix[j,i]>0):
                    const_line += "+ {0}x_{1} ".format(frac_print(self.constraintMatrix[j,i]), i+1)
                elif (first_coef and self.constraintMatrix[j,i]>0):
                    first_coef = False
                    const_line += "{0}x_{1} ".format(frac_print(self.constraintMatrix[j,i]), i+1)
                elif (self.constraintMatrix[j,i]<0):
                    first_coef = False
                    const_line += "{0}x_{1} ".format(frac_print(self.constraintMatrix[j,i]), i+1)

            const_line += " <= " + frac_print(self.constraintVector[j])
            const_line += "\n          "
        output_string+= const_line
        non_neg_line = ", ".join(["x_{}".format(i+1) for i in range(self.nbVar)])
        non_neg_line += " are non-negative\n"
        output_string += non_neg_line
        return output_string

    def __repr__(self):
        return self.__str__()


#Fernando Schroder RA:11201921885

#algoritmo de busca em profundidade
def dfs(graph, v, visited, stack):
    visited[v] = True
    for neighbor in graph[v]:
        if not visited[neighbor]:
            dfs(graph, neighbor, visited, stack)
    stack.insert(0, v)

#Função para Transpor o Grafo
def transpose_graph(n, edges):
    transposed = [[] for _ in range(n)]
    for u, v in edges:
        transposed[v].append(u)
    return transposed

#Executa a DFS no grafo transposto
def dfs_transposed(graph, v, visited, component):
    visited[v] = True
    component.append(f"({v}")
    for neighbor in graph[v]:
        if not visited[neighbor]:
            dfs_transposed(graph, neighbor, visited, component)
    component.append(f"{v})")

#Verifica componentes fortemente conexos
def find_scc(n, edges):
    stack = []
    visited = [False] * n
    graph = [[] for _ in range(n)]
    for u, v in edges:
        graph[u].append(v)

    for i in range(n):
        if not visited[i]:
            dfs(graph, i, visited, stack)

    transposed = transpose_graph(n, edges)
    visited = [False] * n

    scc = []
    for v in stack:
        if not visited[v]:
            component = []
            dfs_transposed(transposed, v, visited, component)
            scc.append(component)

    return scc
#Formata a lista de acordo com a saída pedida no exercicio
def format_scc(scc):
    formatted_scc = []
    for component in scc:
        formatted_scc.append(' '.join(component))
    return ' '.join(formatted_scc)

#funcao main
n, m = map(int, input("").split())
edges = []
print(f"")
for _ in range(m):
    u, v = map(int, input().split())
    edges.append((u, v))

components = find_scc(n, edges)
formatted_components = format_scc(components)
print(formatted_components)


import os

Import("projenv", "env")

print(os.environ.items())

def get_env_data_as_dict(dotenv_path):
    # thanks to https://stackoverflow.com/a/74856258/3631348
    # have to parse manually since dotenv package is not available
    ''' read file and parse key/values '''

    try:
        with open(dotenv_path, 'r', encoding='utf8') as file_obj:
            lines = file_obj.read().splitlines()  # Removes \n from lines
    except:
        return {}

    result = {}
    for line in lines:
        line = line.strip()
        if not line or line.startswith("#") or "=" not in line:
            continue

        if "#" in line:
            line = line.split("#")[0].strip()

        k, v = line.split("=", maxsplit=1)
        result[k] = v

    return result

dotenv = get_env_data_as_dict(os.getcwd()+'/.env')

def parse_github_envs():
    ''' read github envs, prefixed with "SMAHOO_" .. '''

    for key, val in os.environ.items():
        if not key.startswith('SMAHOO_'):
            continue

        dotenv[key.replace('SMAHOO_', '', 1)] = val

parse_github_envs()

def patch_into_cppdefs():
    ''' patch collected settings into CPPDEF's '''
    for key, val in dotenv.items():
        # patch every key and value into the projects CPP defs..
        projenv.Append(CPPDEFINES=[
            (key, env.StringifyMacro(val))
        ])


from tweepy import API
from tweepy import Cursor
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import joblib

import twitter_credentials

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import re

from sklearn.feature_extraction.text import TfidfVectorizer
#from sklearn.metrics import classification_report

from sklearn import svm
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier

trainData = pd.read_csv("train.csv")
testData = pd.read_csv("test.csv")

vectorizer = TfidfVectorizer(min_df = 5,
                             max_df = 0.8,
                             sublinear_tf = True,
                             use_idf = True)

train_vectors = vectorizer.fit_transform(trainData['Content'])
test_vectors = vectorizer.transform(testData['Content'])

# # # # Using SVM # # # #

#classifier_linear = svm.SVC(kernel='linear')
#classifier_linear.fit(train_vectors, trainData['Label'])
#joblib.dump(classifier_linear, 'svm.pkl')#this saves the trained model in a file.
classifier_linear = joblib.load("svm.pkl")

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

# # # # Using KNN # # # #

#Y = trainData[['Label']]
#Y = Y['Label'].values
#k = 8
#neigh = KNeighborsClassifier(n_neighbors = k).fit(train_vectors,Y)
#joblib.dump(neigh, 'knn.pkl')#this saves the trained model in a file.
neigh = joblib.load("knn.pkl")

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

# # # # Using logistic regression # # # #

#Y = np.asarray(trainData['Label'])
#Logistic = LogisticRegression(C=0.01, solver='liblinear').fit(train_vectors,Y)
#joblib.dump(Logistic, 'lr.pkl')#this saves the trained model in a file.
Logistic = joblib.load("lr.pkl")

# # # # # # # # # # # # #
# # # # Using Tree Classifier # # # #

#Y = trainData[['Label']]
#sentTree = DecisionTreeClassifier(criterion="entropy", max_depth = 4)
#sentTree.fit(train_vectors,Y)
#joblib.dump(sentTree, 'tc.pkl')#this saves the trained model in a file.
sentTree = joblib.load("tc.pkl")

# # # # # # # # # # # # #
# # # # TWITTER CLIENTS # # # #
class TwitterClient():
    def __init__(self, twitter_user=None):
        self.auth = TwitterAuthenticator().authenticate_twitter_app()
        self.twitter_client = API(self.auth)

        self.twitter_user = twitter_user

    def get_twitter_client_api(self):
        return self.twitter_client

    def get_tweets(self, query, count = 10):
        tweets1 = []
        try:
            # call twitter api to fetch tweets
            fetched_tweets = self.twitter_client.search(q = query, count = count)

            for tweet in fetched_tweets:

                # appending parsed tweet to tweets list
                if not hasattr(tweet,'retweeted_status'):
                    # if tweet has retweets, ensure that it is appended only once
                    tweets1.append(tweet)

            # return parsed tweets
            return tweets1

        except tweepy.TweepError as e:
            # print error (if any)
            print("Error : " + str(e))
    """
    def get_user_timeline_tweets(self, num_tweets):
        tweets = []
        for tweet in Cursor(self.twitter_client.user_timeline, id=self.twitter_user).items(num_tweets):
            tweets.append(tweet)
        return tweets

    def get_friend_list(self, num_friends):
        friend_list = []
        for friend in Cursor(self.twitter_client.friends, id=self.twitter_user).items(num_friends):
            friend_list.append(friend)
        return friend_list

    def get_home_timeline_tweets(self, num_tweets):
        home_timeline_tweets = []
        for tweet in Cursor(self.twitter_client.home_timeline, id=self.twitter_user).items(num_tweets):
            home_timeline_tweets.append(tweet)
        return home_timeline_tweets

    """
# # # # TWITTER AUTHENTICATER # # # #
class TwitterAuthenticator():

    def authenticate_twitter_app(self):
        auth = OAuthHandler(twitter_credentials.CONSUMER_KEY, twitter_credentials.CONSUMER_SECRET)
        auth.set_access_token(twitter_credentials.ACCESS_TOKEN, twitter_credentials.ACCESS_TOKEN_SECRET)
        return auth

# # # #  TWITTER ANALYZER# # # #
class TweetAnalyzer():
    """
    Functionality for analyzing and categorizing content from tweets.
    """

    def clean_tweet(self, tweet):
        return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", tweet).split())

    def analyze_sentiment_svm(self, tweet):
        review_vector = vectorizer.transform([tweet])
        return classifier_linear.predict(review_vector)

    def analyze_sentiment_knn(self, tweet):
        review_vector = vectorizer.transform([tweet])
        return neigh.predict(review_vector)

    def analyze_sentiment_logisticReg(self, tweet):
        review_vector = vectorizer.transform([tweet])
        return Logistic.predict(review_vector)

    def analyze_sentiment_TreeClassifier(self, tweet):
        review_vector = vectorizer.transform([tweet])
        return sentTree.predict(review_vector)

    def tweets_to_data_frame(self, tweets):
        df = pd.DataFrame(data=[tweet.text for tweet in tweets], columns=['tweets'])

        #df['id'] = np.array([tweet.id for tweet in tweets])
        df['len'] = np.array([len(tweet.text) for tweet in tweets])
        #df['date'] = np.array([tweet.created_at for tweet in tweets])
        #df['source'] = np.array([tweet.source for tweet in tweets])
        #df['likes'] = np.array([tweet.favorite_count for tweet in tweets])
        #df['retweets'] = np.array([tweet.retweet_count for tweet in tweets])

        return df

if __name__ == '__main__':

    twitter_client = TwitterClient()
    tweet_analyzer = TweetAnalyzer()

    api = twitter_client.get_twitter_client_api()

    #tweets = api.user_timeline(screen_name = "guardiannews", count=200)
    tweets1 = twitter_client.get_tweets(query = 'Dark Phoenix', count = 200)

    df = tweet_analyzer.tweets_to_data_frame(tweets1)
    df['sent_svm'] = np.array([tweet_analyzer.analyze_sentiment_svm(tweet) for tweet in df['tweets']])
    df['sent_knn'] = np.array([tweet_analyzer.analyze_sentiment_knn(tweet) for tweet in df['tweets']])
    df['sent_lr'] = np.array([tweet_analyzer.analyze_sentiment_logisticReg(tweet) for tweet in df['tweets']])
    df['sent_treeC'] = np.array([tweet_analyzer.analyze_sentiment_TreeClassifier(tweet) for tweet in df['tweets']])

    print(df.head(14))
    print('positive: ', report['pos'])
    print('negative: ', report['neg'])


def add(a, b):
    ret = a + b
    return ret

ans = add(5, 10)
print(ans)

class Hero:
    def __init__(self, hp, mp):
        self.hp = hp
        self.mp = mp
    def attack(self):
        print("こうげき")
h = Hero(400,30)
h.attack()

ls = [1,5,3,2,10,9]
ls.sort(reverse=True)
print(ls)


import os
import random
import quadra_config
import time
import glob

LOVE_DELAY = 3
EXP_DELAY = 3
LOVE_GRADE = [-100,-70,-20,30,90,210,270,300]
REQ_EXP = [10]

MAX_LOVE = LOVE_GRADE[len(LOVE_GRADE)-1]
MIN_LOVE = LOVE_GRADE[0]

love_1 = ["무슨 낯짝으로 내 앞에 서있는건지 모르겠는데?","저리 가. 별로 보고싶지 않으니까.","굳이 내 앞에 서 있는 이유가 뭐야?"]
love_2 = ["참을 인도 쓰다보면 팔이 아프단것만 알아둬.","어.. 너구나. 안녕.","글쎄.. 굳이 표정을 찡그려줘야해?"]
love_3 = ["글쎄.. 어떨까나?","서로 알아갈 시간이 더 필요할거같아.","굳이 말하자면, 좋지도 싫지도 않은 정도?"]
love_4 = ["조금은 더 알아가고 싶은 사람, 정도로 괜찮을까?","적어도 밉지는 않아."]
love_5 = ["친구 정도로 생각하고 있어.","더 친해져보고 싶은사람!","음.. 문자 정도는 주고받을 수 있는 사이?"]
love_6 = ["너 정도면 믿음직한 친구지.","친구, 오늘은 어떻게 나와 놀아줄거야?","쫑긋! 너 정도면 좋은 친구지!"]
love_7 = ["쫑긋! ..헤헤","..부끄러워! 너무 가까이 오진 마..","넌 최고의 친구 중 한명이야!"]
love_cap = ["정말 싫어","조금 별로","그저 그래","아는 지인","친구","친한 친구","정말 좋아"]

supply_text = ["... 더 주기 싫으니까 빨리 받고 가.","...굳이 많이 필요없잖아? 이정도만 가져가.","자. 오늘의 지원금이야!","돈이 필요해? 아껴쓰라구!","오늘도 지원금이야! 많이는 못줘서 미안해~","최대한 주고싶은데, 이정도가 한계야.. 헤헤...","최대한 주고싶은데, 이정도가 한계야.. 헤헤..."]

for i in range(1,40,1):
	REQ_EXP.append(int((i+2)**(2))+REQ_EXP[i-1])
for i in range(40,70,1):
	REQ_EXP.append(int(REQ_EXP[i-1]*1.1))
for i in range(70,80,1):
	REQ_EXP.append(int(REQ_EXP[i-1]*1.15))
for i in range(80,90,1):
	REQ_EXP.append(int(REQ_EXP[i-1]*1.2))
for i in range(90,100,1):
	REQ_EXP.append(int(REQ_EXP[i-1]*1.3))

CASH_SOFTCAP = 1000000
EXP_HARDCAP = REQ_EXP[99] * 100

fp = open("quadra_level_require.txt",'w')
for i in REQ_EXP:
	fp.write(str(i)+"\n")
fp.close()

for i in range(1,len(REQ_EXP),1):
	REQ_EXP[i] = REQ_EXP[i]+REQ_EXP[i-1]

fp = open("quadra_level_total.txt",'w')
for i in REQ_EXP:
	fp.write(str(i)+"\n")
fp.close()

MAX_LEVEL = len(REQ_EXP)

class quadra_user:
	user_id = ""
	cash = 0
	cash_time = 0
	love = 0
	love_time = 0
	exp = 0
	exp_time = 0
	level = 0
	lifetime_time = 0
	lifetime_check = 0
	def __init__(self,_user_id):
		self.user_id = str(_user_id)
		profile_name = "user_database/"+str(_user_id)+".txt"
		if os.path.exists(profile_name) == False:
			fp = open(profile_name, 'w')
			for i in range(0,9,1):
				fp.write("0\n")
			fp.close()
		fp = open(profile_name,'r')
		target = fp.readlines()
		fp.close()
		for i in range(0,len(target),1):
			target[i] = (target[i].split('\n'))[0]
			if i == 3 or i == 5: target[i] = float(target[i])
			if i == 7: target[i] = str(target[i])
			else: target[i] = int(float(target[i]))
		self.cash = target[0]
		self.cash_time = target[1]
		self.love = target[2]
		self.love_time = target[3]
		self.exp = target[4]
		self.exp_time = target[5]
		self.level = target[6]
		if len(target) < 8:
			self.lifetime_time = 0
			self.lifetime_check = 0
		else:
			self.lifetime_time = target[7]
			self.lifetime_check = target[8]

	def save(self):
		profile_name = "user_database/"+self.user_id+".txt"
		fp = open(profile_name, 'w')
		fp.write(str(self.cash)+"\n")
		fp.write(str(self.cash_time)+"\n")
		fp.write(str(self.love)+"\n")
		fp.write(str(self.love_time)+"\n")
		fp.write(str(self.exp)+"\n")
		fp.write(str(self.exp_time)+"\n")
		fp.write(str(self.level)+"\n")
		fp.write(str(self.lifetime_time)+"\n")
		fp.write(str(self.lifetime_check)+"\n")
		fp.close()

	def canSupply(self):
		if self.cash_time != int((time.localtime()).tm_mday): return True
		else: return False

	def mody(self, cash=0, cash_time=False, love=0, love_time=False,exp=0,exp_time=False):
		levelup = False
		if cash_time:
			if self.cash_time != int((time.localtime()).tm_mday):
				self.cash += cash
				self.cash_time = int((time.localtime()).tm_mday)
		else: self.cash += cash
		if love_time:
			if self.love_time + LOVE_DELAY < time.time():
				self.love += love
				self.love_time = time.time()
		else: self.love += love
		if self.love > MAX_LOVE: self.love = MAX_LOVE
		if self.love < MIN_LOVE: self.love = MIN_LOVE
		if exp_time:
			if self.exp_time + EXP_DELAY < time.time():
				self.exp += exp
				self.exp_time = time.time()
		else: self.exp += exp
		if self.exp > EXP_HARDCAP: self.exp = EXP_HARDCAP
		if REQ_EXP[self.level-1] > self.exp:
			for i in range(0,len(REQ_EXP),1):
				if REQ_EXP[i] > self.exp:
					self.level = i
					break
		elif self.level < 100 and REQ_EXP[self.level] <= self.exp:
			levelup = True
			for i in range(0,len(REQ_EXP),1):
				if REQ_EXP[i] > self.exp:
					self.level = i
					break
			if self.level == 100 :
				_now = time.localtime()
				fp = open("quadra_MAX_LEVEL_note","a")
				fp.write("[%04d-%02d-%02d %02d:%02d:%02d][%d] %s\n"% (_now.tm_year, _now.tm_mon, _now.tm_mday, _now.tm_hour, _now.tm_min, _now.tm_sec,self.level,self.user_id))
				fp.close()
		self.save()
		return levelup

	def change(self, cash=None, love=None, exp=None, level=None):
		if cash != None: self.cash = cash
		if love != None: self.love = love
		if exp != None: self.exp = exp
		if self.exp > EXP_HARDCAP: self.exp = EXP_HARDCAP
		if REQ_EXP[self.level-1] > self.exp:
			for i in range(0,len(REQ_EXP),1):
				if REQ_EXP[i] > self.exp:
					self.level = i
					break
		elif REQ_EXP[self.level] <= self.exp:
			for i in range(0,len(REQ_EXP),1):
				if REQ_EXP[i] > self.exp:
					self.level = i
					break
		if level != None:
			self.level = int(level)
			self.exp = REQ_EXP[level-1]
		self.save()

	def love_level(self):
		temp = 0
		for i in LOVE_GRADE:
			if self.love < i: return temp
			temp += 1
		return len(LOVE_GRADE)-1

	def love_short(self):
		lev = self.love_level()
		return love_cap[lev-1]

	def love_text(self):
		lev = self.love_level()
		if lev == 1:
			return random.choice(love_1)
		elif lev == 2:
			return random.choice(love_2)
		elif lev == 3:
			return random.choice(love_3)
		elif lev == 4:
			return random.choice(love_4)
		elif lev == 5:
			return random.choice(love_5)
		elif lev == 6:
			return random.choice(love_6)
		else:
			return random.choice(love_7)

	def supply_text(self):
		lev = self.love_level()
		return supply_text[lev-1]

	def user_rank(self):
		user_list = glob.glob("user_database/*.*")
		user_exp = []
		trg_point = self.exp
		for i in user_list:
			fp = open(i,"r")
			target = fp.readlines()
			user_exp.append(int((target[4].split('\n'))[0]))
		fp.close()
		user_exp.sort()
		user_exp.reverse()
		return (user_exp.index(trg_point)+1,len(user_exp))

	def general_exp_process(self, msg_len):
		exp_inc = 0
		l_lv = self.love_level()
		temp_lv = l_lv
		temp_len = msg_len
		ratio = 1
		check = 40
		while temp_len > 0:
			if temp_len > check:
				exp_inc += int(ratio * check)
				temp_len -= check
				temp_lv -= 1
				if temp_lv <= 0: break
				ratio /= 2
			else:
				exp_inc += int(ratio * temp_len)
				break
		exp_inc = int(exp_inc * (1 + (0.05*(l_lv-3))))
		if msg_len < 10: exp_inc = 0
		self.mody(exp = exp_inc)

	def lifetime_enable(self, time_var):
		_now = time_var
		priv_time = self.lifetime_time
		target_time = "%02d%02d"%(_now.tm_hour, _now.tm_min)
		if target_time == self.lifetime_time:
			self.lifetime_check += 1
		else:
			self.lifetime_check = 1
			self.lifetime_time = target_time
		self.save()
		return [self.lifetime_check, priv_time, target_time]


"""
"""
import os

from model import VadModel
from parameters import Parameters
from srt_features import SrtWriter
from wav_features import WavFeatures

params = Parameters()

source_wav = params.get_movie_path()
target_srt = os.path.splitext(source_wav)[0] + '.srt'

batch_size = 1
sequence_size = params.get_rnn_sequence_length()
wav_window_size = params.get_wav_window_size()
wav_window_step = params.get_wav_window_step()
wav_sample_rate = params.get_wav_sample_rate()

wav_window_size_second = float(wav_window_size) / float(wav_sample_rate)
wav_window_step_second = float(wav_window_step) / float(wav_sample_rate)

input_wav = WavFeatures()

input_wav.load(
    source_wav,
    window_size=wav_window_size_second,
    window_step=wav_window_step_second,
    numcep=params.get_wav_cepstrum_size())

wav_features = input_wav.features()

wav_batch = [wav_features]

model = VadModel(params)

features = model.detect(wav_batch)

SrtWriter.save(target_srt, features[0], wav_sample_rate, wav_window_step)


"""
Function to do speed tests easily.
"""
import numpy as np
import pandas as pd
from timeit import default_timer

def speedtest(speed_inputs, speed_input_labels, funcs):
    """
    Runs speed tests, and asserts outputs are all the same. Runs the first test before timing anything to make sure
        numba functions are initialized properly.

    :param speed_inputs: list of tuples of (args, kwargs) where args is the *args and kwargs is the **kwargs
    :param speed_input_labels: names to use for each speed_input test
    :param funcs: the functions to test
    """
    func_times, outputs = [], []

    # Initialize numba
    for func in funcs:
        func(*speed_inputs[0][0], **speed_inputs[0][1])

    # Run timing tests
    for func in funcs:
        times = []
        these_outputs = []

        for args, kwargs in speed_inputs:
            t = default_timer()
            these_outputs.append(func(*args, **kwargs))
            times.append(default_timer() - t)

        func_times.append(times)
        outputs.append(these_outputs)

    for i in range(1, len(outputs)):
        for j in range(len(outputs[0])):
            np.testing.assert_array_almost_equal(outputs[i][j], outputs[i - 1][j])

    print("Timings:\n")
    print(pd.DataFrame(func_times, columns=speed_input_labels, index=[f.__name__ for f in funcs]).to_markdown(floatfmt='0.4f'))


import sys
sys.stdin = open('input.txt','r')

def backtrack(a, k, n): # k는 깊이 inp는 자릿수
    c=[0]*2
    if sum(a)==10:
        for i in a:
            if i>0:
                print(i,end=" ")
        print()
    elif k==n:
        return
    else:
        k+=1
        ncandidates=construct_candidates(a,k,c) # 자리에 올 후보군을 추린다.
        for i in range(ncandidates):
            if c[i]:
                a[k]=inp[k-1]
                if sum(a)>10:
                    continue
            else:
                a[k]=0
            backtrack(a,k,n)

def construct_candidates(a,k,c):
    c[0]=True
    c[1]=False
    return 2

inp = list(map(int, input().split()))
NMAX=11
a=[0]*NMAX
backtrack(a,0,10)


"""
Author: Atahan Kucuk
Assignment: 08.5 - Number Reader
Date: 11/15/2021

Description:
this is an application that reads from a previously created file that has random numbers on it and finds its min, max, sum, average and count of numbers.

"""

def main():

    #declarimng an array to store the random numbers
    lines = []
    #iterating through the file
    with open('random_numbers.txt') as fo:
        for line in fo:
            lines.append(int(line.rstrip()))
    #finding the count, min, max, sum and average of the random numbers
    countL = len(lines)
    minL = min(lines)
    maxL = max(lines)
    sumL = sum(lines)
    avgL = float(sumL / countL)

    #displaying the outputs
    print(f'{countL:2,.0f} numbers were read from the file.')
    print(f'Min: {minL:1,.0f}')
    print(f'Max: {maxL:1,.0f}')
    print(f'Sum: {sumL:2,.0f}')
    print(f'Avg: {avgL:1,.1f}')
    fo.close

if __name__ == '__main__':
    main()


'''
date: 2020/2/7
author: 流氓兔233333
content: 注意力机制融合双向LSTM
model架构参考  NLP_融合主题模型和注意力机制的政策文本分类模型_胡吉明
'''

import numpy as np
import pandas as pd
from tqdm import tqdm
import time
import random
import os, warnings, pickle
warnings.filterwarnings('ignore')

import torch
import torch.utils.data as Data
# 导入nn及优化器
import torch.nn.functional as F
import torch.optim as optim
from torch import nn
import sklearn.metrics as ms

data_path = './data_raw/'
save_path = './temp_results/'

# load word2idx tar2idx
word2idx_dict = pickle.load(open(save_path+'word2idx_dict.pkl', 'rb'))
tar2idx_dict = pickle.load(open(save_path+'target2idx_dict.pkl', 'rb'))

# load tensor data
train_tensor_tuple = pickle.load(open(save_path+'train_tensor_tuple.pkl', 'rb'))
val_tensor_tuple = pickle.load(open(save_path+'val_tensor_tuple.pkl', 'rb'))
x_train_tensor, y_train_tensor = train_tensor_tuple[0], train_tensor_tuple[1]
x_val_tensor, y_val_tensor = val_tensor_tuple[0], val_tensor_tuple[1]

del train_tensor_tuple, val_tensor_tuple

batch_size = 128
dataset_trn = Data.TensorDataset(x_train_tensor, y_train_tensor)
loader_trn = Data.DataLoader(dataset_trn, batch_size, True)
dataset_val = Data.TensorDataset(x_val_tensor, y_val_tensor)
loader_val = Data.DataLoader(dataset_val, len(dataset_val), True)

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
device

def set_seed(seed):
    """ Set all seeds to make results reproducible """
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False
    np.random.seed(seed)
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)

class BiLSTM_Attention(nn.Module):
    def __init__(self, embed_dim, hid_dim, vocab_size, tar_size):
        super(BiLSTM_Attention, self).__init__()
        self.hid_dim = hid_dim
        self.word_embedding = nn.Embedding(vocab_size, embed_dim)
        self.lstm = nn.LSTM(embed_dim, hid_dim, bidirectional=True, batch_first=True)
        # attention, self.attn 的 out_dim 是任意的，这里直接设成 tar_dim
        self.attn = nn.Linear(self.hid_dim, tar_size)
        self.v = nn.Linear(tar_size, 1, bias=False)

        self.fc = nn.Linear(self.hid_dim, tar_size)

    def forward(self, sequence):
        batch_size = sequence.shape[0]

        # nn.Embedding 要求输入的数据为 [batch_size, seq_len]
        # 输出为 [batch_szie, seq_len, embed_dim]
        embeds = self.word_embedding(sequence)
        seq_len = embeds.shape[1]

        # nn.LSTM 要求输入数据为 [batch_size, seq_len, embed_dim]
        # hidden_0 未给出时会被默认设置为0
        # lstm_out  [batch_size, seq_len, hid_dim*num_directions]
        # h_n  [n_layers*num_directions, batch_size, embed_dim]
        # h_c 用于生成 h_n, 不用于输出
        lstm_out, (h_n, h_c) = self.lstm(embeds)
        # [bs, seq_len, hid_size]
        lstm_out = lstm_out[:, :, :self.hid_dim] + lstm_out[:, :, self.hid_dim:]

        lstm_out = F.tanh(lstm_out)

        attention = torch.zeros(seq_len, batch_size)   # [seq_len, batch_size]
        for t in range(seq_len):

            # attention_iuput [batch_size, hid_dim*2]
            # h_n[0, :, :] shape [batch_size, hid_dim]
            attention_iuput = lstm_out[:, t, :]
            # mt [batch_size, tar_size]
            mt = self.attn(attention_iuput)
            attention_t = self.v(mt)  # [batch_size, 1]
            attention[t] = attention_t.squeeze(1)

        attention = F.softmax(attention, dim=0)
        attention = attention.transpose(0,1).unsqueeze(1) # [batch_size, 1, seq_len]
        # lstm_out [batch_size, seq_len, hid_dim*2]
        c = torch.bmm(attention, lstm_out) # [batch_size, 1, hid_dim*2]
        c = c.squeeze(1) # [batch_size, hid_dim*2]

        output = self.fc(c) # [batch_size, tar_size]

        return output

def save(model, optimizer):
    # save
    torch.save({
        'model_state_dict': model.state_dict(),
        'optimizer_state_dict': optimizer.state_dict()
    }, './BiLSTM_Attention.pth')
    print('The best model has been saved')

def train_eval(model, criterion, optimizer, train_loader, val_loader, epochs=2):
    try:
        checkpoint = torch.load('./model_bert.pth', map_location='cpu')
        model.load_state_dict(checkpoint['model_state_dict'])
        optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
        print('-----Continue Training-----')
    except:
        print('No Pretrained model!')
        print('-----Training-----')

    model.to(device)
    for epoch in range(epochs):
        model.train()
        print('eopch: %d/%d'% (epoch, epochs))
        for i, batch in enumerate(tqdm(train_loader)):
            batch = tuple(t.to(device) for t in batch)
            output = model(batch[0])
            loss = criterion(output, batch[-1])
            if((i+1) % 8)==0:
                optimizer.step()        # 反向传播，更新网络参数
                optimizer.zero_grad()   # 清空梯度

        if epoch % 1 == 0:
            print(loss.item())
            eval(model, optimizer, val_loader)

best_score = 0
def eval(model, optimizer, val_loader):
    model.eval()
    _, batch_A = next(enumerate(val_loader))
    batch = tuple(t.to(device) for t in batch)
    with torch.no_grad():
        output = model(batch[0])
        label_ids = batch[-1].numpy()

        _, prediction = torch.max(F.softmax(output, dim=1), 1)
        pred_val = prediction.data.numpy().squeeze()
        acc_val = ms.accuracy_score(label_ids, pred_val)

    print("Validation Accuracy: {}".format(acc_val))
    global best_score
    if best_score < acc_val:
        best_score = acc_val
        save(model, optimizer)

tar_size = len(tar2idx_dict)
vocab_size = len(word2idx_dict)
embed_size = 2 * int(np.floor(np.power(vocab_size, 0.25)))

model = BiLSTM_Attention(embed_dim=embed_size, hid_dim=64, vocab_size=vocab_size, tar_size=tar_size)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=1e-3)

set_seed(1)
train_eval(model, criterion, optimizer, loader_trn, loader_val, epochs=2)

loss_his = []
step, (batch_x, batch_y) = next(enumerate(loader))
for epoch in range(20):
    model = model.train()
    for step, (batch_x, batch_y) in enumerate(tqdm(loader)):
        output = model(batch_x)

        loss = loss_fun(output, batch_y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    loss_his.append(loss.item())

    if epoch % 1 == 0:
        model = model.eval()
        output = model(x_val_tensor)

        _, prediction = torch.max(F.softmax(output, dim=1), 1)
        pred_val = prediction.data.numpy().squeeze()
        acc_test = ms.accuracy_score(y_val_tensor.numpy(), pred_val)
        print('EPOCH: ', epoch,   '         loss:', loss.item())
        print('  test_acc:', acc_test)

# loss 曲线图
import matplotlib.pyplot as plt
plt.plot(loss_his)
plt.ylabel('loss', fontsize=20)
plt.xlabel('eopch', fontsize=20)
plt.tick_params(labelsize=20)
plt.title('EOPCH_LOSS_BiLSTM_Attention', fontsize=20)
plt.grid()
plt.show()

# model save
torch.save(model.state_dict(), save_path+'BiLSTM_Attention.pkl')

x = [1, 2, 3, 5]

[1 if i >2 else 0 for i in x ]


import matplotlib.pyplot as plt
import numpy as np

"""
PROBLEM STATEMENT
flux = u^2/2
u_0(x) = sin(x)
Domain (0,2)
"""

#Declaring the variables
N = 1000                        # Number of points

xmin,xmax = 0,np.pi             # Domain limits
L = xmax - xmin                 # Domain Size
dx = L/N                        # Mesh width
x = np.linspace(xmin,xmax,N)    # Mesh Points

dt = 0.001                      # time step
T = 1                           # Total time
Nt = int(T/dt)                  # no of time iterations

# U_x and initial data
U = np.zeros((Nt+1,N))
U[0,:] = np.sin(x)

# A function to calculate numerical flux
# This is to avoid data overflow error
def Num_flux(i,j):
    return (U[n,i]**2 + U[n,j]**2)/4 - (dx/dt)*(U[n,j]-U[n,i])/2

# Numerical Analysis
for n in range(0,Nt):
    for j in range(1,N-1):
        # First calculating the numerical flux
        F_nj1 = Num_flux(j,j+1)
        F_nj2 = Num_flux(j-1,j)

        # Basic Formula to calculate U^n_j+1
        U[n+1,j] = U[n,j] - (dt/dx)*(F_nj1 - F_nj2)

    # Assigning U^n+1_0 as loop starts from 1
    U[n+1,0] = U[n+1,N-1]

    # Plotting
    if(n == 0): fig,ax = plt.subplots(figsize = (5.5,4))
    plt.clf()
    plt.plot(x,U[n+1,:])
    plt.axis([xmin,xmax,0,1.5])
    plt.title('t='+str(round(dt*(n+1),3)),fontsize=16)
    plt.xlabel('x')
    plt.ylabel('u')
    plt.grid(True)

    plt.draw()
    plt.pause(0.001)

plt.show()


import tkinter as tk
from tkinter import *
from tkinter import messagebox
from tkinter.ttk import *
import re
import openpyxl

# Hàm kiểm tra mã số sinh viên hợp lệ
def is_valid_student_id(student_id):
    return bool(re.match(r'^\d{7}$', student_id))

# Hàm kiểm tra email hợp lệ
def is_valid_email(email):
    return bool(re.match(r'^[\w\.-]+@[\w\.-]+$', email))

# Hàm kiểm tra số điện thoại hợp lệ
def is_valid_phone(phone):
    return bool(re.match(r'^\d{10}$', phone))

# Hàm kiểm tra học kỳ hợp lệ
def is_valid_semester(semester):
    return semester in ['1', '2', '3']

# Hàm kiểm tra định dạng ngày sinh hợp lệ
def is_valid_date_of_birth(date_of_birth):
    return bool(re.match(r'^\d{2}/\d{2}/\d{4}$', date_of_birth))

# Hàm lưu thông tin vào tệp Excel
def save_to_excel(student_id, full_name, email, phone, semester, date_of_birth, academic_year, selected_courses):
    wb = openpyxl.load_workbook('C:\\Python\\Lab04\\Cau6\\student_info.xlsx')
    sheet = wb.active
    row = [student_id, full_name, email, phone, semester, date_of_birth, academic_year] + selected_courses
    sheet.append(row)
    wb.save('C:\\Python\\Lab04\\Cau6\\student_info.xlsx')
    messagebox.showinfo("Thông báo", "Đăng ký thành công!")

# Hàm xử lý sự kiện khi nhấn nút Đăng ký
def register():
    student_id = student_id_entry.get()
    full_name = full_name_entry.get()
    email = email_entry.get()
    phone = phone_entry.get()
    semester = semester_var.get()
    date_of_birth = date_of_birth_entry.get()
    academic_year = academic_year_var.get()

    selected_courses = []
    for course_var in course_vars:
        if course_var.get():
            selected_courses.append(course_var.get())

    if not student_id or not full_name or not email or not phone or not semester or not date_of_birth or not academic_year:
        messagebox.showerror("Lỗi", "Vui lòng điền đầy đủ thông tin!")
        return

    if not is_valid_student_id(student_id):
        messagebox.showerror("Lỗi", "Mã số sinh viên không hợp lệ!")
        return

    if not is_valid_email(email):
        messagebox.showerror("Lỗi", "Email không hợp lệ!")
        return

    if not is_valid_phone(phone):
        messagebox.showerror("Lỗi", "Số điện thoại không hợp lệ!")
        return

    if not is_valid_semester(semester):
        messagebox.showerror("Lỗi", "Học kỳ không hợp lệ!")
        return

    if not is_valid_date_of_birth(date_of_birth):
        messagebox.showerror("Lỗi", "Ngày sinh không hợp lệ! (dd/mm/yyyy)")
        return

    if not selected_courses:
        messagebox.showerror("Lỗi", "Vui lòng chọn ít nhất một môn học!")
        return

    save_to_excel(student_id, full_name, email, phone, semester, date_of_birth, academic_year, selected_courses)

# Tạo giao diện
root = tk.Tk()
root.title("Đăng ký sinh viên")
root.geometry("600x450")

# Mã số sinh viên
student_id_label = tk.Label(root, text="THÔNG TIN SINH VIÊN")
student_id_label.grid(row=0, column=1, sticky= W, pady = 2)
student_id_label = tk.Label(root, text="Mã số sinh viên:")
student_id_label.grid(row=1, column=0, sticky = W, pady = 2)
student_id_entry = tk.Entry(root)
student_id_entry.grid(row=1, column=1, ipadx=100)

# Họ tên sinh viên
full_name_label = tk.Label(root, text="Họ tên sinh viên:")
full_name_label.grid(row=2, column=0, sticky = W, pady = 2)
full_name_entry = tk.Entry(root)
full_name_entry.grid(row=2, column=1, ipadx=100)

# Email
email_label = tk.Label(root, text="Email:")
email_label.grid(row=3, column=0, sticky = W, pady = 2)
email_entry = tk.Entry(root)
email_entry.grid(row=3, column=1, ipadx=100)

# Số điện thoại
phone_label = tk.Label(root, text="Số điện thoại:")
phone_label.grid(row=4, column=0, sticky = W, pady = 2)
phone_entry = tk.Entry(root)
phone_entry.grid(row=4, column=1, ipadx=100)

# Học kỳ
semester_label = tk.Label(root, text="Học kỳ:")
semester_label.grid(row=5, column=0, sticky = W, pady = 2)
semester_var = tk.StringVar()
semester_var.set("1")
semester_radio1 = tk.Radiobutton(root, text="1", variable=semester_var, value="1")
semester_radio2 = tk.Radiobutton(root, text="2", variable=semester_var, value="2")
semester_radio3 = tk.Radiobutton(root, text="3", variable=semester_var, value="3")
semester_radio1.grid(row=5, column=1, sticky = W, pady = 2)
semester_radio2.grid(row=6, column=1, sticky = W, pady = 2)
semester_radio3.grid(row=7, column=1, sticky = W, pady = 2)

# Ngày sinh
date_of_birth_label = tk.Label(root, text="Ngày sinh (dd/mm/yyyy):")
date_of_birth_label.grid(row=8, column=0, sticky = W, pady = 2)
date_of_birth_entry = tk.Entry(root)
date_of_birth_entry.grid(row=8, column=1, ipadx=100)

# Năm học
academic_year_label = tk.Label(root, text="Năm học:")
academic_year_label.grid(row=9, column=0, sticky = W, pady = 2)
academic_year_var = tk.StringVar()
academic_year_var.set("2022-2023")
academic_year_optionmenu = tk.OptionMenu(root, academic_year_var, "2022-2023", "2023-2024", "2024-2025")
academic_year_optionmenu.grid(row=9, column=1, sticky = W, pady = 2)

# Môn học
courses_label = tk.Label(root, text="Chọn môn học:")
courses_label.grid(row=10, column=0, sticky = W, pady = 2)
course_vars = []
course_checkboxes = []
course_names = ["Lập trình Java", "Lập trình Python", "Công nghệ phần mềm", "Phát triển ứng dụng web"]
for i, course_name in enumerate(course_names):
    course_var = tk.StringVar()
    course_var.set("")
    course_vars.append(course_var)
    course_checkbox = tk.Checkbutton(root, text=course_name, variable=course_var, onvalue=course_name, offvalue="")
    course_checkbox.grid(row=10 + i, column=1, sticky = W, pady = 2)
    course_checkboxes.append(course_checkbox)

# Nút Đăng ký
register_button = tk.Button(root, text="Đăng ký", command=register)
register_button.grid(row=14, column=0, columnspan=4)

root.mainloop()


from math import ceil, floor
import sys
input = sys.stdin.readline

def input_list_numbers():
    return(list(map(int, input().split())))

inp = input_list_numbers()
n = inp[0]
abc = inp[1:]
abc.sort()
answer = False

results = []

for n1 in range(ceil(n/abc[0]) + 1):
    if n1 * abc[0] > n:
        break
    for n2 in range(ceil(n/abc[1]) + 1):
        if n1 * abc[0] + n2 * abc[1] > n:
            break
        for n3 in range(ceil(n/abc[2]) + 1):
            if n1 * abc[0] + n2 * abc[1] + n3 * abc[2] > n:
                break
            if n1 * abc[0] + n2 * abc[1] + n3 * abc[2] == n:
                results.append(n1 + n2 + n3)

print(max(results))


#!/usr/bin/env python

""" Determine the version string from news.yml. """

import yaml

path_to_news = "../pg_backup_api/news.yml"

with open(path_to_news, "r") as fh:
    news = yaml.safe_load(fh)

latest_release = news["pg-backup-api release notes"][0]
version = latest_release["version"]

print(f"{version}")


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 24 18:53:12 2023

@author: umbertocappellazzo
"""

import os
from Speech_CLscenario.base_dataset import _ContinuumDataset
#from base_dataset import _ContinuumDataset

#from class_incremental import ClassIncremental
import numpy as np
from typing import Union
import string
try:
    import soundfile
except:
    soundfile = None

class Slurp(_ContinuumDataset):
    def __init__(self, data_path, max_len_text, max_len_audio, train: Union[bool, str] = True, download: bool = True):
        if not isinstance(train, bool) and train not in ("train", "valid", "test","train_real","train_synthetic"):
            raise ValueError(f"`train` arg ({train}) must be a bool or train/valid/test/train_synthetic/train_real.")
        if isinstance(train, bool):
            if train:
                train = "train"
            else:
                train = "test"

        data_path = os.path.expanduser(data_path)
        self.max_len_text = max_len_text
        self.max_len_audio = max_len_audio
        super().__init__(data_path, train, download)

    def get_data(self):
        path_to_wavs = "/data/cappellazzo/slurp"
        x, y, transcriptions = [], [], []  # For now ENTITIES are not taken into account.
        #digits = ['0','1','2','3','4','5','6','7','8','9']

        punctuations = list(string.punctuation)
        punctuations.append('’')

        with open(os.path.join(self.data_path, f"{self.train}_intents.csv")) as f:
            lines = f.readlines()[1:]
        for line in lines:
            items = line[:-1].split(';')

            transcription = items[3].lower()
            # Remove first 2 characters 's1' or 's2' containing numbers.
            #if transcription[:2] == 's1' or transcription[:2] == 's2':
            #    transcription = transcription[2:]

            # 47 transcriptions still have some numbers --> remove them.

            if 'synth' in items[2]:
                    #x.append(os.path.join(self.data_path,'slurp_synth', items[2]))
                #pathh = os.path.join(self.data_path,'slurp_synth', items[2])
                pathh = os.path.join(path_to_wavs,'slurp_synth', items[2])
                #x.append(os.path.join(path_to_wavs,'slurp_synth', items[2]))
            else:
                    #x.append(os.path.join(self.data_path,'slurp_real', items[2]))
                #pathh = os.path.join(self.data_path,'slurp_real', items[2])
                #x.append(os.path.join(path_to_wavs,'slurp_real', items[2]))
                pathh = os.path.join(path_to_wavs,'slurp_real', items[2])

            wav = soundfile.read(pathh)[0]
            #skip = False
            #if any((c in digits) for c in transcription):
            #    skip = True

            if len(transcription) > self.max_len_text or len(wav) > self.max_len_audio:

                pass

            else:

                transc = [char for char in transcription if char not in punctuations]
                transcription = ''
                for charr in transc:
                    transcription += charr

                scenario, action = items[4:6]

                entities = items[6]

                if entities == 'null':
                    aug_trans = scenario + '_' + action + ' _SEP ' + transcription
                else:
                    entities = entities.split(',')
                    aug_trans = scenario + '_' + action + ' _SEP '
                    for count in range(0,len(entities)//2,2):
                        aug_trans += entities[count] + ' _FILL ' + entities[count+1] + ' _SEP '
                        aug_trans += transcription
               #aug_trans = scenario + '_' + action + ' _SEP ' + transcription

                # For NON-AUGMENTED transcriptions.
                #transcriptions.append(transcription)

                # For AUGMENTED transcriptions.
                transcriptions.append(aug_trans)

                #transcriptions.append(scenario)
                if 'synth' in items[2]:
                    #x.append(os.path.join(self.data_path,'slurp_synth', items[2]))
                    x.append(os.path.join(path_to_wavs,'slurp_synth', items[2]))
                else:
                    #x.append(os.path.join(self.data_path,'slurp_real', items[2]))
                    x.append(os.path.join(path_to_wavs,'slurp_real', items[2]))
                #transcription = items[3].lower()

                #transcriptions.append(items[3].lower())
                y.append(self.scenarios[scenario])
                #y.append([
                #    self.scenarios[scenario],
                #    self.actions[action],
                #])

        return np.array(x), np.array(y), None, transcriptions

    @property
    def transformations(self):
        return None

    @property
    def scenarios(self):
        return {
            'alarm': 0,
            'audio': 1,
            'calendar': 2,
            'cooking': 3,
            'datetime': 4,
            'email': 5,
            'general': 6,
            'iot': 7,
            'lists': 8,
            'music': 9,
            'news': 10,
            'play': 11,
            'qa': 12,
            'recommendation': 13,
            'social': 14,
            'takeaway': 15,
            'transport': 16,
            'weather': 17,
            }

    @property
    def actions(self):
        return {
            'addcontact': 0,
            'affirm': 1,
            'audiobook': 2,
            'cleaning': 3,
            'coffee': 4,
            'commandstop': 5,
            'confirm': 6,
            'convert': 7,
            'createoradd': 8,
            'currency': 9,
            'definition': 10,
            'dislikeness': 11,
            'dontcare': 12,
            'events': 13,
            'explain': 14,
            'factoid': 15,
            'game': 16,
            'greet': 17,
            'hue_lightchange': 18,
            'hue_lightdim': 19,
            'hue_lightoff': 20,
            'hue_lighton': 21,
            'hue_lightup': 22,
            'joke': 23,
            'likeness': 24,
            'locations': 25,
            'maths': 26,
            'movies': 27,
            'music': 28,
            'negate': 29,
            'order': 30,
            'podcasts': 31,
            'post': 32,
            'praise': 33,
            'query': 34,
            'querycontact': 35,
            'quirky': 36,
            'radio': 37,
            'recipe': 38,
            'remove': 39,
            'repeat': 40,
            'sendemail': 41,
            'set': 42,
            'settings': 43,
            'stock': 44,
            'taxi': 45,
            'ticket': 46,
            'traffic': 47,
            'volume_down': 48,
            'volume_mute': 49,
            'volume_other': 50,
            'volume_up': 51,
            'wemo_off': 52,
            'wemo_on': 53,
            }

class TextTransform:
    """Maps characters to integers and vice versa

    28 --> SOS (#)
    29 --> EOS (*)
    30 --> PAD (@)
    """

    def __init__(self):

        self.char_map_str = ["@","-","_",".",",","0","1","2","3","4","5","6","7","8","9",">","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","C","B","A","O"]

        self.char_map = {}
        self.index_map = {}
        for index,ch in enumerate(self.char_map_str):

            self.char_map[ch] = int(index)
            self.index_map[int(index)] = ch

    def text_to_int(self, text):
        """ Use a character map and convert text to an integer sequence """
        int_sequence = []
        #text = [char for char in text if char not in self.punctuations]

        for c in text:

            ch = self.char_map[c]
            int_sequence.append(ch)
        return int_sequence

    def int_to_text(self, labels):
        """ Use a character map and convert integer labels to an text sequence """
        string_ = []
        for i in labels:
            string_.append(self.index_map[i])
        return ''.join(string_).replace('@', '')

if __name__ == "__main__":
    data_path ='/Users/umbertocappellazzo/Desktop/PHD/CL_SLU/slurp'
    a = Slurp(data_path,max_len_text=130,max_len_audio=112000,train="train",download=False)
    b,_,_,_ = a.get_data()
    t = TextTransform()

    # count = 0
    # for x in b:
    #     x = x.split("/")[-1]

    #     xx = t.text_to_int(x)
    #     xxx = t.int_to_text(xx)

    #     if xxx == x: count +=1

    # print(len(b))
    # print(count)

#     tot = len(b)
#     maxx = 0
#     print(tot)
#     text_transform = TextTransform()
#     count = 0
#     count_maxx = 0
#     check = ['0','1','2','3','4','5','6','7','8','9']
#     for x in b:
#         print(x)
#         c = text_transform.text_to_int(x)
#         #count += len(c)
#         #if len(c) > maxx:
#         #    maxx = len(c)
#         #    print("New max: ", maxx )

#         if len(c) > 129:
#             #print(x)
#             count_maxx += 1
#     #print(count/tot)
#     #print(maxx)
#     print(count_maxx)
#     #print(count)


import re
import sys
import copy

def defeat_obfuscation_loops(obfuscated_script):

    obfuscation_loops = [m.span() for m in re.finditer(r'\$\w+\s*=\s*\d+\n\$\w+\s*=\s*\d+\nDo\nSwitch\s*\$\w+.*?EndSwitch\nUntil\s*.*?\n', obfuscated_script, re.DOTALL)]

    first_loop = obfuscated_script[obfuscation_loops[0][0]:obfuscation_loops[0][1] + 1]
    case_number = re.search(r'\$\w+\s*=\s*(\d+)', first_loop).group(1)
    statements = re.search(f'Case\s+{case_number}(.*)ExitLoop', first_loop, re.DOTALL).group(1)
    deobfuscated_script = obfuscated_script[:obfuscation_loops[0][0]] + statements

    for loop_index in range(1, len(obfuscation_loops)):
        deobfuscated_script += obfuscated_script[obfuscation_loops[loop_index - 1][1]: obfuscation_loops[loop_index][0]]
        obfuscated_loop = obfuscated_script[obfuscation_loops[loop_index][0]: obfuscation_loops[loop_index][1] + 1]

        case_number = re.search(r'\$\w+\s*=\s*(\d+)', obfuscated_loop).group(1)
        statements = re.search(f'Case\s+{case_number}(.*)ExitLoop', obfuscated_loop, re.DOTALL).group(1)
        deobfuscated_script += statements

    last_loop = obfuscated_script[obfuscation_loops[-1][0]:obfuscation_loops[-1][1] + 1]
    case_number = re.search(r'\$\w+\s*=\s*(\d+)', last_loop).group(1)
    statements = re.search(f'Case\s+{case_number}(.*)ExitLoop', last_loop, re.DOTALL).group(1)
    deobfuscated_script += obfuscated_script[obfuscation_loops[-2][1]:obfuscation_loops[-1][0]] + statements + obfuscated_script[obfuscation_loops[-1][1] + 1:]

    return deobfuscated_script

def _deobfuscate_strings(numbers_string, substract_number):
    result = ''

    numbers_list = numbers_string.split('.')
    for number in numbers_list:
        result = result + chr(int(number) - int(substract_number))

    return '"{}"'.format(result)

def deobfuscate_strings(obfuscated_script):

    deobfuscation_function_calls = [m.span() for m in re.finditer(r'faBwnHc\(\"[\d.]*\",\d\)', obfuscated_script)]

    first_call = obfuscated_script[deobfuscation_function_calls[0][0]:deobfuscation_function_calls[0][1] + 1]
    numbers_string = re.search(r'\"([\d.]*)\"', first_call).group(1)
    substract_number = re.search(r',(\d)', first_call).group(1)
    deobfuscated_script = obfuscated_script[:deobfuscation_function_calls[0][0]] + _deobfuscate_strings(numbers_string, substract_number)

    for call_index in range(1, len(deobfuscation_function_calls)):
        deobfuscated_script += obfuscated_script[deobfuscation_function_calls[call_index - 1][1]: deobfuscation_function_calls[call_index][0]]
        call = obfuscated_script[deobfuscation_function_calls[call_index][0]: deobfuscation_function_calls[call_index][1] + 1]
        numbers_string = re.search(r'\"([\d.]*)\"', call).group(1)
        substract_number = re.search(r',(\d)', call).group(1)
        deobfuscated_script += _deobfuscate_strings(numbers_string, substract_number)

    last_call = obfuscated_script[deobfuscation_function_calls[-1][0]:deobfuscation_function_calls[-1][1] + 1]
    numbers_string = re.search(r'\"([\d.]*)\"', last_call).group(1)
    substract_number = re.search(r',(\d)', last_call).group(1)
    deobfuscated_script += obfuscated_script[deobfuscation_function_calls[-2][1]:deobfuscation_function_calls[-1][0]] + _deobfuscate_strings(numbers_string, substract_number) + obfuscated_script[deobfuscation_function_calls[-1][1] + 1:]

    return deobfuscated_script

def defeat_unused_variables(obfuscated_script):

    deobfuscated_script = copy.deepcopy(obfuscated_script)
    script_variables_tokens = re.findall(r'\$\w+', obfuscated_script)
    variable_declarations = re.findall(r'Local \$\w* = ', obfuscated_script)
    for declaration in variable_declarations:
        variable_name = re.search(r'(\$\w+)', declaration).group(1)
        if script_variables_tokens.count(variable_name) == 1:
            deobfuscated_script = re.sub(f'Local \{variable_name} = .*', '', deobfuscated_script)

    return deobfuscated_script

def defeat_unused_functions(obfuscated_script):

    deobfuscated_script = copy.deepcopy(obfuscated_script)
    function_tokens = re.findall(r'(\w+)\(', obfuscated_script)
    function_declarations = re.findall(r'Func \w+\(', obfuscated_script)
    for declaration in function_declarations:
        function_name = re.search(r'(\w+)\(', declaration).group(1)
        if function_tokens.count(function_name) == 1:
            deobfuscated_script = re.sub(f'Func {function_name}\(.*?EndFunc', '', deobfuscated_script, flags=re.DOTALL)

    return deobfuscated_script

def defeat_unused_variables_and_functions(obfuscated_script):

    return re.sub(r'\n{3,}', '\n\n', defeat_unused_functions(defeat_unused_variables(obfuscated_script)))

if __name__ == '__main__':
    with open(sys.argv[1], 'r') as obfuscated_script_file:
        obfuscated_script = obfuscated_script_file.read()
        with open ('deobfuscated_script.au3', 'w') as output_file:
            output_file.writelines(defeat_unused_variables_and_functions(deobfuscate_strings(defeat_obfuscation_loops(obfuscated_script))))


class Solution:
    def intersect(self, nums1, nums2):
        res = list()
        counts = dict()
        for num in nums1:
            counts[num] = counts.get(num, 0) + 1
        for num in nums2:
            if num in counts and counts[num] > 0:
                counts[num] -= 1
                res.append(num)
        return res

print(Solution.intersect(Solution, [1, 2, 2, 1], [2, 2]))


from game import Game

"""
Original Project Idea: https://www.youtube.com/watch?v=delJJ9zHXio

Features Added: GUI Interface, Main Menu, Website redirect, Program Screen
"""
g = Game()

"""
Used to start the game and display menu screen
"""

while g.running:
    g.curr_menu.display_menu()
    g.game_loop()


import unittest
from unittest.mock import patch

from tests.common_validate import context, context_patch, JsonResponse, TestConsumer

ACCESS_TOKEN = 'xyz-abc-123'

class TestImportFull(unittest.TestCase):

    @patch('connector.server_access.AssetServer.get_machine_list')
    @patch('car_framework.car_service.CarService.get_import_schema')
    def test_create_asset_host(self, schema, mock_machine_list):
        """
             Summary unit test for create asset host.
        """
        context_patch(incremental=False)
        mock_machine_list.return_value = JsonResponse(200, 'vm_details_log.json').json()
        schema.return_value = JsonResponse(200, 'schema.json').json()
        actual_response = context().data_collector.create_asset_host(incremental=False)
        assert actual_response is not None
        context().full_importer.handle_data([
            'asset',
            'hostname',
            'asset_hostname',
            'application',
            'asset_application',
        ], actual_response)

        collections = context().full_importer.data_handler.collections
        edges = context().full_importer.data_handler.edges
        validate = TestConsumer()

        validations = all([
            validate.asset_validate(collections.get('asset')),
            validate.host_validate(collections.get('hostname')),
            validate.asset_host_validate(edges.get('asset_hostname')),
            validate.application_validate(collections.get('application')),
            validate.asset_application_validate(edges.get('asset_application'))
        ])
        assert validations is True

    @patch('connector.server_access.AssetServer.get_machine_list')
    @patch('connector.server_access.AssetServer.mac_private_ip_information')
    @patch('car_framework.car_service.CarService.get_import_schema')
    def test_create_ip_mac(self, schema, mock_private_ip_response, mock_machine_list):
        """
             Summary unit test for create ipaddress macaddress.
        """
        context_patch(incremental=False)
        mock_machine_list.return_value = JsonResponse(200, 'vm_details_log.json').json()
        mock_private_ip_response.return_value = JsonResponse(200, 'private_ip_log.json').json()
        schema.return_value = JsonResponse(200, 'schema.json').json()
        actual_response, _, _, _ = context().data_collector.create_ipaddress_macaddress(incremental=False)
        assert actual_response is not None
        context().full_importer.handle_data([
            'ipaddress',
            'macaddress',
            'ipaddress_macaddress',
            'asset_ipaddress',
            'asset_macaddress',
            'asset',
            'hostname',
            'asset_hostname',
            'application',
            'asset_application',
        ], actual_response)

        collections = context().full_importer.data_handler.collections
        edges = context().full_importer.data_handler.edges
        validate = TestConsumer()

        validations = all([
            validate.ipaddr_validate(collections.get('ipaddress')),
            validate.mac_validate(collections.get('macaddress')),
            validate.ip_mac_address_validate(edges.get('ipaddress_macaddress')),
            validate.asset_ip_validate(edges.get('asset_ipaddress')),
            validate.asset_mac_validate(edges.get('asset_macaddress')),
            validate.asset_validate(collections.get('asset')),
            validate.host_validate(collections.get('hostname')),
            validate.asset_host_validate(edges.get('asset_hostname')),
            validate.application_validate(collections.get('application')),
            validate.asset_application_validate(edges.get('asset_application'))
        ])
        assert validations is True

    @patch('connector.server_access.AssetServer.vulnerability_information')
    @patch('connector.server_access.AssetServer.get_alerts_list')
    @patch('connector.server_access.AssetServer.get_machine_list')
    @patch('car_framework.car_service.CarService.get_import_schema')
    def test_create_vulnerability(self, schema, mock_machine_list, mock_alert_log, mock_vuln_vm_details):
        """
             Summary unit test for create vulnerability.
        """
        context_patch(incremental=False)
        # test_log_data = JsonResponse(200, 'vulnerability_log.json')
        mock_machine_list.return_value = JsonResponse(200, 'vm_details_log.json').json()
        mock_alert_log.return_value = JsonResponse(200, 'alerts_log.json').json()
        mock_vuln_vm_details.return_value = JsonResponse(200, 'machine_vuln_details.json').json()
        schema.return_value = JsonResponse(200, 'schema.json').json()
        actual_response, _, _, _, _ = context().data_collector.create_vulnerability(incremental=False)

        assert actual_response is not None
        context().full_importer.handle_data([
            'application',
            'asset_application',
            'vulnerability',
            'asset_vulnerability'
        ], actual_response)

        collections = context().full_importer.data_handler.collections
        edges = context().full_importer.data_handler.edges
        validate = TestConsumer()

        validations = all([
            validate.application_validate(collections.get('application')),
            validate.asset_application_validate(edges.get('asset_application')),
            validate.vuln_validate(collections.get('vulnerability')),
            validate.asset_vuln_validate(edges.get('asset_vulnerability'))
        ])
        assert validations is True

    @patch('connector.server_access.AssetServer.get_access_token')
    @patch('connector.server_access.RestApiClient.call_api')
    @patch('connector.server_access.AssetServer.get_machine_list')
    @patch('car_framework.car_service.CarService.get_import_schema')
    def test_create_user(self, schema, mock_machine_list, mock_user_details, mock_access_token):
        """
             Summary unit test for create user.
        """
        context_patch(incremental=False)
        mock_access_token.return_value = ACCESS_TOKEN
        mock_machine_list.return_value = JsonResponse(200, 'vm_details_log.json').json()
        schema.return_value = JsonResponse(200, 'schema.json').json()
        mock_user_details.return_value = JsonResponse(200, 'user_details_log.json')
        actual_response = context().data_collector.create_user(incremental=False)
        assert actual_response is not None
        context().full_importer.handle_data([
            'user',
            'asset_account',
            'account_hostname',
            'account',
            'user_account',
        ], actual_response)

        collections = context().full_importer.data_handler.collections
        edges = context().full_importer.data_handler.edges
        validate = TestConsumer()

        validations = all([
            validate.user_validate(collections.get('user')),
            validate.asset_account_validate(edges.get('asset_account')),
            validate.account_host_validate(edges.get('account_hostname')),
            validate.account_validate(collections.get('account')),
            validate.user_account_validate(edges.get('user_account')),
        ])
        assert validations is True