import sys

import click
import textacy

from . import Content
from . import Network
from . import Qrmine
from . import ReadData
from . import Sentiment
from . import MLQRMine
from . import __version__

@click.command()
@click.option('--verbose', '-v', is_flag=True, help="Will print verbose messages.")
@click.option('--inp', '-i', multiple=True,
              help='Input file in the text format with <break>Topic</break>')
@click.option('--out', '-o', multiple=False, default='',
              help='Output file name')
@click.option('--csv', multiple=False, default='',
              help='csv file name')
@click.option('--num', '-n', multiple=False, default=3,
              help='N (clusters/epochs etc depending on context)')
@click.option('--rec', '-r', multiple=False, default=3,
              help='Record (based on context)')
@click.option('--titles', '-t', multiple=True,
              help='Document(s) or csv title(s) to analyze/compare')
@click.option('--filters', '-f', multiple=True,
              help='Filters to apply')
@click.option('--codedict', is_flag=True,
              help='Generate coding dictionary')
@click.option('--topics', is_flag=True,
              help='Generate topic model')
@click.option('--assign', is_flag=True,
              help='Assign documents to topics')
@click.option('--cat', is_flag=True,
              help='List categories of entire corpus or individual docs')
@click.option('--summary', is_flag=True,
              help='Generate summary for entire corpus or individual docs')
@click.option('--sentiment', is_flag=True,
              help='Generate sentiment score for entire corpus or individual docs')
@click.option('--sentence', is_flag=True, default=False,
              help='Generate sentence level scores when applicable')
@click.option('--nlp', is_flag=True,
              help='Generate all NLP reports')
@click.option('--nnet', is_flag=True,
              help='Display accuracy of a neural network model')
@click.option('--svm', is_flag=True,
              help='Display confusion matrix from an svm classifier')
@click.option('--knn', is_flag=True,
              help='Display nearest neighbours')
@click.option('--kmeans', is_flag=True,
              help='Display KMeans clusters')
@click.option('--cart', is_flag=True,
              help='Display Association Rules')
@click.option('--pca', is_flag=True,
              help='Display PCA')
def cli(verbose, inp, out, csv, num, rec, titles, filters, codedict, topics, assign, cat, summary, sentiment, sentence,
        nlp, nnet,
        svm,
        knn, kmeans, cart, pca):
    data = ReadData()
    if inp:
        data.read_file(inp)
    if len(filters) > 0:
        data = filter_data(inp, filters, sentence, num)
    if verbose:
        click.echo("We are in the verbose mode.")
    if out:
        sys.stdout = open(out, 'w')
    if inp and codedict:
        generate_dict(data, num)
    if inp and topics:
        generate_topics(data, assign, num)
    # if inp and assign:
    #     assign_topics(data)
    if inp and cat:
        generate_categories(data, titles, num)
    if inp and summary:
        generate_summary(data, titles)
    if inp and sentiment:
        get_sentiment(data, titles, sentence, verbose)
    if inp and cart: #python qrminer.py --cart -i src/qrmine/resources/interview.txt -n 10
        get_categories_association(data, num)
    if inp and nlp:
        main(inp)
    if csv:
        ml = MLQRMine()
        ml.csvfile = csv
        if len(titles) > 0:
            ml.titles = titles
    if csv and nnet:
        get_nnet(ml, num)
    if csv and svm:
        get_svm(ml)
    if csv and knn:
        get_knn(ml, num, rec)
    if csv and kmeans:
        get_kmeans(ml, num)
    if csv and cart:
        get_association(ml)
    if csv and pca:
        get_pca(ml, num, verbose)

"""
The following functions work on all the text sections.
"""

"""
This filters data according to search criteria

If search is empty, return entire data

If search is pos, neg or neu apply a sentiment filter

Here search is the filters applied

filters variable refers to the titles
"""

def filter_data(inp, search, sentence, num):
    data = ReadData()
    to_return = ReadData()
    data.read_file(inp)

    filters = []
    for s in search:
        if s == 'pos':
            for title in data.titles:
                t = [title]
                if get_sentiment(data, t, sentence, False) == 'pos':
                    filters.append(title)
        if s == 'neg':
            for title in data.titles:
                t = [title]
                if get_sentiment(data, t, sentence, False) == 'neg':
                    filters.append(title)
        if s == 'neu':
            for title in data.titles:
                t = [title]
                if get_sentiment(data, t, sentence, False) == 'neu':
                    filters.append(title)
        # If search itself is a title
        if any(s in l for l in data.titles):
            filters.append(s)
        # If the given category is present in the document
        for title in data.titles:
            t = [title]
            if any(s in l for l in generate_categories(data, t, num)):
                filters.append(title)

    click.echo("Selected Titles")
    for filter in filters:
        click.echo(filter)

    ct = 0
    for title in data.titles:
        if any(title in l for l in filters):
            to_return.append(title, data.documents[ct])
        ct += 1

    if len(search) > 0 and len(to_return.documents) > 0:
        click.echo("Filters applied. \n")
        return to_return
    else:
        return data

# test: test_generate_dict in test_nlp.py
def generate_dict(data, num):
    if not num:
        num = 10
    q = Qrmine()
    all_interviews = Content(data.content)
    q.print_dict(all_interviews, num)

def generate_topics(data, assign, num):
    q = Qrmine()
    q.content = data
    q.process_content()
    q.print_topics()
    if assign:
        q.print_documents(num)

# def assign_topics(data):
#     q = Qrmine()
#     q.content = data
#     q.process_content()
#     q.print_documents()

def get_categories_association(data, num):
    q = Qrmine()
    q.content = data
    click.echo(q.category_association(num))
    click.echo("Frequent Itemsets")
    click.echo("---------------------------")

"""
Function working at both levels
"""

def generate_categories(data, tags, num):
    q = Qrmine()

    if len(tags) > 0:
        ct = 0
        for title in data.titles:
            for tag in tags:
                if title == tag:
                    click.echo(tag)
                    content = data.documents[ct]
            ct += 1
        interview = Content(content)
        doc = textacy.make_spacy_doc(interview.doc)
        return q.print_categories(doc, num)

    else:
        all_interviews = Content(data.content)
        doc = textacy.make_spacy_doc(all_interviews.doc)
        return q.print_categories(doc, num)

def generate_summary(data, tags):
    if len(tags) > 0:
        ct = 0
        for title in data.titles:
            for tag in tags:
                if title == tag:
                    click.echo(tag)
                    content = data.documents[ct]
            ct += 1
        interview = Content(content)
        click.echo(" ".join(interview.generate_summary(2)))
        click.echo("_________________________________________")

    else:
        all_interviews = Content(data.content)
        click.echo(" ".join(all_interviews.generate_summary(2)))
        click.echo("_________________________________________")

"""
"""

def get_sentiment(data, tags, sentence, verbose):
    if len(tags) > 0:
        ct = 0
        for title in data.titles:
            for tag in tags:
                if title == tag:
                    click.echo(tag)
                    content = data.documents[ct]
            ct += 1
        interview = Content(content)
        doc = textacy.make_spacy_doc(interview.doc)

        ## Sentiment
        s = Sentiment()

        if sentence is True:
            for sentence in doc.sents:
                if len(sentence) > 3:
                    sent = s.sentiment_analyzer_scores(sentence.text)
                    if verbose:
                        click.echo("{:-<40} {}\n".format(sent["sentence"], str(sent["score"])))
                    click.echo(s.sentiment())

        else:
            sent = s.sentiment_analyzer_scores(doc.text)
            if verbose:
                click.echo("{:-<40} {}\n".format(sent["sentence"], str(sent["score"])))
            click.echo(s.sentiment())
        return s.sentiment()
    else:
        all_interviews = Content(data.content)
        doc = textacy.make_spacy_doc(all_interviews.doc)

        ## Sentiment
        s = Sentiment()
        if sentence is True:
            for sentence in doc.sents:
                if len(sentence) > 3:
                    sent = s.sentiment_analyzer_scores(sentence.text)
                    if verbose:
                        click.echo("{:-<40} {}\n".format(sent["sentence"], str(sent["score"])))
                    click.echo(s.sentiment())

        else:
            sent = s.sentiment_analyzer_scores(doc.text)
            if verbose:
                click.echo("{:-<40} {}\n".format(sent["sentence"], str(sent["score"])))
            click.echo(s.sentiment())
        return s.sentiment()

"""
ML
"""

def get_nnet(ml, n=3):
    ml.epochs = n
    ml.prepare_data(True)  # Oversample
    ml.get_nnet_predictions()
    click.echo("\n%s: %.2f%%" % (ml.model.metrics_names[1], ml.get_nnet_scores()[1] * 100))

def get_svm(ml):
    ml.prepare_data(True)  # Oversample
    click.echo(ml.svm_confusion_matrix())

# https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KDTree.html#sklearn.neighbors.KDTree
def get_knn(ml, n=3, r=3):
    ml.prepare_data()
    knn = ml.knn_search(n, r)
    for n in knn:
        print("Records: ", n + 1)

def get_kmeans(ml, n=3):
    ml.prepare_data()
    click.echo("K-Means Clusters:")
    click.echo(ml.get_kmeans(n))

def get_association(ml):
    ml.prepare_data()
    click.echo(ml.get_apriori())

def get_pca(ml, n=3, verbose=None):
    ml.prepare_data()
    if verbose:
        click.echo(ml.head)
    click.echo(ml.get_pca(n))

def main(input_file):
    # content property returns the entire text and the documents returns the array of documents
    data = ReadData()
    data.read_file(input_file)

    q = Qrmine()
    all_interviews = Content(data.content)

    q.content = data

    ## Summary
    click.echo(" ".join(all_interviews.generate_summary(2)))
    click.echo("_________________________________________")

    doc = textacy.make_spacy_doc(all_interviews.doc)

    ## Sentiment
    s = Sentiment()
    x = []
    for sentence in doc.sents:
        if len(sentence) > 3:
            x.append(sentence.text)
            sent = s.sentiment_analyzer_scores(sentence.text)
            click.echo("{:-<40} {}\n".format(sent["sentence"], str(sent["score"])))
            click.echo("{:-<40} {}\n".format(sentence.text, str(s.similarity(sentence.text, "Dummy sentence"))))

    ## Network
    n = Network()
    click.echo(n.sents_to_network(x))
    # n.draw_graph(True)
    click.echo(n.draw_graph(False))

    q.process_content()

    q.print_categories(doc)
    q.print_topics()
    q.print_documents()
    q.print_dict(all_interviews)

def main_routine():
    click.echo("_________________________________________")
    click.echo("QRMine(TM) Qualitative Research Miner. v" + __version__)
    cli()  # run the main function

if __name__ == '__main__':
    main_routine()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 17 14:21:50 2020

@author: s1782911
"""
import parse_to_graph
import test1
import matplotlib.pyplot as pl
import os
import pickle

def test(lambda_list, path, positives, negatives):
    return_list=[]
    for l in lambda_list:
        tp=0
        fp=0
        tn=0
        fn=0
        print(l)
        G=parse_to_graph.constructGraphFromFile(path,l)
        #print("pos")
        for tup in  positives:
            try:
                res=test1.find_entailment(G, tup)
                #print(res)
                if res[1]=='y':
                    #print(tup,res)
                    tp+=1
                else:
                    fn+=1
            except AttributeError:
                print("no ", l)
                break
        #print("neg")
        for tup in negatives:
            try:
                res=test1.find_entailment(G, tup)
                #print(res)
                if res[1]=='n':
                    tn+=1
                else:
                    fp+=1
            except  AttributeError:
                print("no ", l)
                break
        print("tp: ",tp," tn: ",tn," fp: ",fp," fn: ",fn)
        if tp!=0:
            prec=tp/(tp+fp)
            print("precision :", tp/(tp+fp))
            rec=tp/(tp+fn)
            print("recall :", tp/(tp+fn))
        else:
            prec=1
            rec=0
        return_list.append((prec,rec))
    return return_list

def parse(input_path):
    positives=[]
    negatives=[]
    f=open(input_path,"r")
    one=False
    two=False
    for line in f:
        print(line)
        if "positive" in line:
            one=True
            continue
        if one and line!="\n":
            line=line.split("),(")
            a=line[1].replace("(","").replace(")","")
            b=line[0].replace("(","").replace(")","")
            positives.append((a,b))
        if line=="\n":
            one=False
        if "negative" in line:
            two=True
            continue
        if two:
            line=line.split("),(")
            a=line[1].replace("(","").replace(")","")
            b=line[0].replace("(","").replace(")","")
            negatives.append((a,b))
    return positives, negatives

def test_multi(lambda_list,file_path, type_pair, positives, negatives):
    return_list=[]
    for l in lambda_list:
        print("----------------------"+str(l)+"------------------------------")
        tp=0
        fp=0
        tn=0
        fn=0
        for filename in os.listdir(file_path):
            if type_pair in filename and str(l)+".pickle" in filename:
                #print(file_path+filename)
                G=pickle.load(open(file_path+filename,"rb"))
                #print(G[0])
                print("number of edges", G.number_of_edges())
                for tup in  positives:
                    try:
                        res=test1.find_entailment(G, tup)
                        #print(tup,res)
                        if res[1]=='y':
                            print(tup,res)
                            tp+=1
                        else:
                            fn+=1
                    except AttributeError:
                        print("no ", l)
                        break
                for tup in negatives:
                    try:
                        res=test1.find_entailment(G, tup)
                        #print(tup,res)
                        if res[1]=='n':
                            tn+=1
                        else:
                            fp+=1
                    except  AttributeError:
                        #print("no ", l)
                        break
                print("tp: ",tp," tn: ",tn," fp: ",fp," fn: ",fn)
                if tp!=0:
                    prec=tp/(tp+fp)
                    print("precision :", tp/(tp+fp))
                    rec=tp/(tp+fn)
                    print("recall :", tp/(tp+fn))
                else:
                    prec=1
                    rec=0
                return_list.append((prec,rec))
    return return_list

if __name__ == '__main__':
    """
    lambda_list=[ 0.004, 0.008, 0.012, 0.016, 0.020, 0.024, 0.028, 0.032, 0.036,
             0.040, 0.044, 0.048, 0.052, 0.056, 0.060, 0.064, 0.068, 0.072,
             0.076, 0.080, 0.084, 0.088, 0.092]
    for l in lambda_list:
        G=parse_to_graph.constructGraphFromFile_multi("person#person_gsim_HTLFRG",l,0)
        pickle.dump(G[0], open("/disk/scratch_big/sweber/GraphAlignment2/mergedGlobalGraphPickles/person#person"+str(l)+".pickle", "wb" ) )
        print("done ",l)

    G=pickle.load(open("mergedGraphPickles/person#person/merged_graphPerson#Person0.0099.pickle","rb"))
    for n in G.nodes():
        print(n)
        print(G.nodes[n]["verb"])

    #parse the test set
    positives,negatives=parse("/disk/scratch_big/sweber/manual_german_graph_test/person#person")
    # for all the different graphs of that type
    lambda_list=[0.0149, 0.025, 0.035, 0.045, 0.054, 0.064, 0.075, 0.085, 0.0125, 0.0225,
                 0.0324, 0.0425, 0.0524, 0.0625, 0.0724, 0.0825, 0.0175, 0.0274, 0.0375,
                 0.0474, 0.0575, 0.0675, 0.0775, 0.0874]

    return_list_low=test(lambda_list,"justGraphsLowL/PERSON#PERSON", positives, negatives)

    lambda_list=[0.15,  0.25,  0.34, 0.44, 0.55, 0.64, 0.75,  0.85, 0.125,  0.224,
                 0.324, 0.425, 0.524, 0.625,  0.725, 0.824, 0.174, 0.275, 0.375,
                 0.474, 0.574, 0.675, 0.774, 0.875
                 ]

    return_list_high=test(lambda_list,"justGraphsHighL/PERSON#PERSON", positives, negatives)
    return_list_low.extend(return_list_high)
    rec_list=[]
    prec_list=[]
    for t in return_list_low:
        prec_list.append(t[0])
        rec_list.append(t[1])
    pl.plot(rec_list, prec_list, "ro", label='German')

    lambda_list=[0.0125,  0.0375,  0.0675,  0.12,  0.27,  0.44,  0.62,  0.0175,  0.0425,
                 0.075,   0.15,  0.32,  0.47,  0.64,  0.85, 0.0225,  0.045,   0.0775,  0.17,
                 0.34,  0.52,  0.67,  0.87, 0.025,   0.0575,  0.0825,  0.22,  0.37,  0.55,
                 0.75, 0.035,   0.0625,  0.085,   0.25,  0.42,  0.57,  0.77]

    return_list1=test_multi(lambda_list,"newAligned/", "PERSON#PERSON", positives, negatives)

    rec_list1=[]
    prec_list1=[]
    for t in return_list1:
        prec_list1.append(t[0])
        rec_list1.append(t[1])
    pl.plot(rec_list1, prec_list1, "bv", label='MultiAll')
    """
    positives, negatives=parse("person#person_EN")
    lambda_list=[0.012,  0.052,  0.072,   0.032,  0.064  ]

    return_list2=test_multi(lambda_list,"newAlignedHighest/", "PERSON#PERSON", positives, negatives)

    rec_list2=[]
    prec_list2=[]
    for t in return_list2:
        prec_list2.append(t[0])
        rec_list2.append(t[1])
    pl.plot(rec_list2, prec_list2, "ro", label='MultiHigh')

    print(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,")
    lambda_list=[ 0.004, 0.008, 0.012, 0.016, 0.020, 0.024, 0.028, 0.032, 0.036,
                 0.040, 0.044, 0.048, 0.052, 0.056, 0.060, 0.064, 0.068, 0.072,
                 0.076, 0.080, 0.084, 0.088, 0.092]

    return_list=test_multi(lambda_list,"mergedGlobalGraphPickles/", "person#person", positives, negatives)

    rec_list3=[]
    prec_list3=[]
    for t in return_list:
        prec_list3.append(t[0])
        rec_list3.append(t[1])
    pl.plot(rec_list3, prec_list3, "g*", label='English')

    pl.legend(loc='upper right')
    pl.xlabel('recall')
    pl.ylabel("precision")
    pl.savefig('prec_recMinimal.pdf')
    pl.show()
    #positives_en, negatives_en=parse("/disk/scratch_big/sweber/manual_german_graph_test/person#person_en")


import pygame
import os
from pygame.locals import *
import shelve
import time
from Patoman import Matriz
from Patoman import menu
from Patoman import Ghost

main_dir = os.path.split(os.path.abspath(__file__))[0]
shelve_path=os.path.join(main_dir,"score.txt")

#Directions
UP    = 0
LEFT  = 1
DOWN  = 2
RIGHT = 3

#Defining Ghosts
Inky = 0
Blinky = 1
Clyde = 2
Pinky = 3

#Defining Ghosts' modes
SCATTER = 0
CHASE = 1
EATEN = 2
FRIGHTENED = 3
LEAVE = 4
WAITING = 5

#Adjust sprite position
Ajuste = 6

def load_img(name):
   path=os.path.join(main_dir,"imgs",name)
   return pygame.image.load(path)

def load_sound(name,volume):
   path=os.path.join(main_dir,"sounds",name)
   this_sound = pygame.mixer.Sound(path)
   this_sound.set_volume(volume)
   return this_sound

def play_music(name,volume):
   path=os.path.join(main_dir,"sounds",name)
   pygame.mixer.music.load(path)
   pygame.mixer.music.set_volume(volume)
   pygame.mixer.music.play(loops=-1)

def load_font(name,size):
   path=os.path.join(main_dir,"fonts",name)
   return pygame.font.Font(path,size)

def go_to_menu():
   running=False
   pygame.mixer.music.unload()
   pygame.mixer.pause()
   menu.run()

def setup(volume,is_gamer):
   #Initializing modules
   pygame.init()
   if not pygame.mixer.get_init():
     pygame.mixer.init()

   #Loads images
   background = load_img('background.png')
   background_white = load_img('background_white.png')
   background_open = load_img('background_open.png')
   power_img = load_img('corn.png')
   coin_img = load_img('bit.png')
   patoFC = load_img('patoFC.png')
   patoFD = load_img('patoFD.png')
   patoFB = load_img('patoFB.png')
   patoFE = load_img('patoFE.png')
   patoAC = load_img('patoAC.png')
   patoAD = load_img('patoAD.png')
   patoAB = load_img('patoAB.png')
   patoAE = load_img('patoAE.png')
   arrow = load_img('arrow.png')

   #Loads the sounds
   eat1 = load_sound('pac_chomp_one.wav',volume)
   eat2 = load_sound('pac_chomp_two.wav',volume)
   death = load_sound('death.wav',volume)
   eat_ghost = load_sound('eat.wav',volume)
   oneup = load_sound('1up.wav',volume)

   #loads the fonts
   font=load_font("emulogic.ttf",16)
   big_font=load_font("emulogic.ttf",32)

   #Screen
   screen = pygame.display.set_mode((448, 576))
   pygame.display.set_caption("Pato-man")
   #Clock Speed
   clock = pygame.time.Clock()

   def game(current_lives,current_score,player_normal_speed,player_fast_speed,ghost_normal_speed,ghost_slow_speed,is_gamer):
      def reset():
         #Inital screen appearence
         screen.fill((0, 0, 0))
         score.display()
         lives.display()
         screen.blit(background,(0,48))
         consumables.draw(screen) #displays the consumables
         screen.blit(font.render("ready!",False,pygame.Color("yellow")),(176,320))

         for i in range(4):
            if i==Inky:
               phantom[i].pos=(184,280)
               phantom[i].mode = WAITING
               phantom[i].waiting_movements = -30
               phantom[i].up = True
            elif i==Blinky:
               phantom[i].pos=(216,224)
               phantom[i].mode=SCATTER
            elif i==Clyde:
               phantom[i].pos=(248,280)
               phantom[i].mode = WAITING
               phantom[i].waiting_movements = 0
               phantom[i].up = True
            elif i==Pinky:
               phantom[i].pos=(216,280)
               phantom[i].mode=LEAVE
            phantom[i].frightened = False
            phantom[i].box = False
            phantom[i].moviments = 0
            phantom[i].direct = DOWN
            phantom[i].change = 0
            phantom[i].rect = phantom[i].img.get_rect()
            phantom[i].rect.height = 16
            phantom[i].rect.width = 16
            phantom[i].rect.x = phantom[i].pos[0]-Ajuste
            phantom[i].rect.y = phantom[i].pos[1]-Ajuste
            phantom[i].set_image()
            screen.blit(phantom[i].img, (phantom[i].pos[0]-Ajuste, phantom[i].pos[1]-Ajuste))
         player.pos=(216,416)
         player.direct=LEFT
         player.display()
         pygame.display.update()
         pygame.time.wait(2150)
         pygame.mixer.music.unpause()

      def pause_game():
         running=False
         pygame.mixer.music.pause()
         pygame.mixer.pause()

         pause_screen=pygame.Surface((448, 576))
         pause_screen.set_alpha(100)
         cursor_index=0
         positions=[(72,228),(8,276)]
         while not running:

            clock.tick(40)
            for event in pygame.event.get():
               #Quit game
               if event.type == pygame.QUIT:
                  pygame.quit()

               #Directions keys
               if event.type == KEYDOWN:
                  if event.key == K_ESCAPE:
                     running=True
                     pygame.mixer.music.unpause()
                     pygame.mixer.unpause()

                  if event.key == K_RETURN:

                     if cursor_index==0:
                        running=True
                        pygame.mixer.music.unpause()
                        pygame.mixer.unpause()
                     else:
                        go_to_menu()

                  if event.key == K_UP:
                     cursor_index+=1
                     if cursor_index==2:
                        cursor_index=0

                  if event.key == K_DOWN:
                     cursor_index-=1
                     if cursor_index==-1:
                        cursor_index=1

            screen.fill((0, 0, 0))
            score.display()
            lives.display()
            screen.blit(background,(0, 48))
            consumables.draw(screen)
            player.display()

            screen.blit(pause_screen,(0,0))
            screen.blit(big_font.render("PAUSED",False,pygame.Color("White")),(128,128))
            screen.blit(big_font.render("continue", False, pygame.Color('White')),(96,224))
            screen.blit(big_font.render("back to menu", False, pygame.Color('White')),(32,272))
            screen.blit(arrow,positions[cursor_index])

            pygame.display.update()

      def win_game():
         running=False
         pygame.mixer.music.pause()
         pygame.mixer.pause()

         path=os.path.join(main_dir,"sounds",'win_sound.wav')
         pygame.mixer.music.load(path)
         pygame.mixer.music.set_volume(volume)
         pygame.mixer.music.play(loops=1)

         pause_screen=pygame.Surface((448, 576))
         pause_screen.set_alpha(100)
         cursor_index=0
         positions=[(8,276)]
         while not running:

            clock.tick(60)
            for event in pygame.event.get():
               #Quit game
               if event.type == pygame.QUIT:
                  pygame.quit()

               #Directions keys
               if event.type == KEYDOWN:
                  if event.key == K_RETURN:
                     go_to_menu()

            screen.fill((0, 0, 0))
            score.display()
            lives.display()
            screen.blit(background,(0, 48))
            consumables.draw(screen)
            player.display()

            screen.blit(pause_screen,(0,0))
            screen.blit(big_font.render("YOU WIN", False, pygame.Color('Green')),(96,224))
            screen.blit(big_font.render("back to menu", False, pygame.Color('White')),(32,272))
            screen.blit(arrow,positions[cursor_index])

            pygame.display.update()

      class Score:

         def __init__(self,current_score,gamer):
            self.score=current_score #starting score
            self.is_gamer=gamer
            self.limit=int(self.score)//10000 + 1

            d = shelve.open(shelve_path)#open high score memory
            self.high_score='00'
            if not self.is_gamer and 'score' in d:
               self.high_score=d['score'] #Starts as last high value if played before (modo normal)
            elif self.is_gamer and 'score_gamer' in d:
               self.high_score=d['score_gamer'] #Starts as last high value if played before (modo gamer)
            d.close() #close high score memory

            self.high_score_text=font.render(self.high_score,False,pygame.Color("white")) #makes starter high score text
            self.score_text=font.render(self.score,False,pygame.Color("white")) #makes starter score text

         def display(self):
            screen.blit(font.render("high score",False,pygame.Color("white")),(144,0)) #display "high score" on screen
            screen.blit(self.score_text,(112-16*len(self.score),16)) #display current score on screen
            screen.blit(self.high_score_text,(272-16*len(self.high_score),16)) #display current high score on screen

         def add(self,num):
            new_score=int(self.score)+num
            if new_score>=10000*self.limit:
               lives.add()
               self.limit+=1
               oneup.play() #som de 1up
            self.score=str(new_score) #updates current score
            if int(self.score)>int(self.high_score): #checks if we need new high score
               d = shelve.open(shelve_path) #open high score memory
               if not self.is_gamer:
                  d['score'] = self.score #update high score memory (modo normal)
               if self.is_gamer:
                  d['score_gamer'] = self.score #update high score memory (modo gamer)

               d.close() #close high score memory
               self.high_score=self.score #gets new high score value
               self.high_score_text=font.render(self.high_score,False,pygame.Color("white")) #makes new high score text

            self.score_text=font.render(self.score,False,pygame.Color("white")) #makes new score text

      class Lives:

         def __init__(self,current_lives,gamer):
            self.lives=current_lives
            self.is_gamer=gamer

         def add(self):
            if self.lives<5 and not is_gamer:
               self.lives+=1

         def remove(self):
            self.lives-=1
            if self.lives==0:
               #Game over screen
               screen.fill((0, 0, 0))
               score.display()
               lives.display()
               screen.blit(background,(0,48))
               consumables.draw(screen)
               screen.blit(font.render("Game over",False,pygame.Color("red")),(152,320))
               pygame.display.update()
               pygame.time.wait(3500)
               go_to_menu()

         def display(self):
            for i in range(self.lives):
               screen.blit(patoAD,(8+i*32,548))

      class Coins(pygame.sprite.Sprite):

         def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image = coin_img
            self.rect = self.image.get_rect()

      class Power(pygame.sprite.Sprite):

         def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image = power_img
            self.rect = self.image.get_rect()

      class Player(pygame.sprite.Sprite):

         def __init__(self):
            self.pos = (216,416)

            self.direct = LEFT
            self.last_direct = LEFT
            self.memory_direct = LEFT
            self.change_direct = 0

            self.img_index=0
            self.imgs = [patoFC,patoFD,patoFB,patoFE,patoAC,patoAD,patoAB,patoAE]
            self.image = patoFC
            self.aberto=False

            self.rect = self.image.get_rect()

            self.rect.x = self.pos[0]
            self.rect.y = self.pos[1]
            self.rect.height = 16
            self.rect.width = 16

            self.eaten_phantom = 0
            self.set_image()
            self.count = True

            self.time=0
            self.normal_speed=player_normal_speed
            self.fast_speed=player_fast_speed
            self.speed=self.normal_speed
         def set_xy(self):
            self.rect.x = self.pos[0]
            self.rect.y = self.pos[1]

         def change_sound(self):
            #change chomp sound
            if self.count:
               eat1.play()
            else:
               eat2.play()
            self.count = not self.count

         def move(self):

            #Try (only one time) to do the memory direction
            if self.change_direct == 1:
               self.direct = self.memory_direct

            if self.direct == UP:
               self.pos = (self.pos[0],self.pos[1]-1)
            if self.direct == RIGHT:
               self.pos = (self.pos[0]+1,self.pos[1])
            if self.direct == DOWN:
               self.pos = (self.pos[0],self.pos[1]+1)
            if self.direct == LEFT:
               self.pos=(self.pos[0]-1,self.pos[1])

            if self.pos[1] == 272:
               if self.pos[0] < 0:
                  self.move_absolute(screen.get_width()-1,self.pos[1])
               elif self.pos[0] >= screen.get_width():
                  self.move_absolute(0,self.pos[1])

            if matriz[self.pos[1]][self.pos[0]] == 1:
               if self.direct == UP:
                  self.pos = (self.pos[0], self.pos[1] + 1)
               if self.direct == RIGHT:
                  self.pos = (self.pos[0] - 1, self.pos[1])
               if self.direct == DOWN:
                  self.pos = (self.pos[0], self.pos[1] - 1)
               if self.direct == LEFT:
                  self.pos = (self.pos[0] + 1, self.pos[1])

               if self.last_direct != self.direct:

                  #If direction is invalid, memory_direction save it
                  self.memory_direct = self.direct
                  self.change_direct += 1

                  self.direct = self.last_direct
                  self.move()

            self.last_direct = self.direct
            self.set_xy()
            self.set_image()

         def move_absolute(self,x,y):
            self.pos=(x,y)
            self.set_xy()

         def death(self):
            rotate = pygame.transform.rotate
            for frame in range(-1,7):
               screen.fill((0, 0, 0))
               score.display()
               lives.display()
               screen.blit(background,(0,48))
               consumables.draw(screen)
               screen.blit(rotate(patoAD,-((frame+1)*90)),(self.pos[0]-4,self.pos[1]-4))
               pygame.display.update()
               pygame.time.wait(150)
            pygame.time.wait(800)

         def set_image(self):
            self.img_index=0
            if self.aberto:
               self.img_index+=4
            if self.direct==RIGHT:
               self.img_index+=1
            elif self.direct==DOWN:
               self.img_index+=2
            elif self.direct==LEFT:
               self.img_index+=3
            self.set_xy()

         def display(self):
            screen.blit(self.imgs[self.img_index],(self.pos[0]-4,self.pos[1]-4))

         def collision(self):

            phantom_player_collision = pygame.sprite.spritecollide(player, phantom_list, False)

            for ghost in phantom_player_collision:

               if (ghost.mode == EATEN):
                  continue
               if (ghost.mode == FRIGHTENED):

                  phantom[ghost.ghost].mode = EATEN
                  eat_ghost.play()

                  #display everything but pacman and eaten ghost
                  screen.fill((0, 0, 0))
                  score.display()
                  lives.display()
                  screen.blit(background,(0, 48))
                  consumables.draw(screen)
                  for i in range(4):
                     if i!=ghost.ghost:
                        screen.blit(phantom[i].img, (phantom[i].pos[0]-Ajuste, phantom[i].pos[1]-Ajuste))

                  #display score got
                  screen.blit(load_img('score'+str(self.eaten_phantom)+'.png'),(ghost.pos[0]-7,ghost.pos[1]))
                  pygame.display.update()

                  pygame.time.wait(1000)

                  #displays everything normally
                  screen.fill((0, 0, 0))
                  score.display()
                  lives.display()
                  screen.blit(background,(0, 48))
                  consumables.draw(screen)
                  for i in range(4):
                     screen.blit(phantom[i].img, (phantom[i].pos[0]-Ajuste, phantom[i].pos[1]-Ajuste))
                  self.display()
                  pygame.display.update()

                  phantom[ghost.ghost].box = False
                  score.add(200 * (2 ** self.eaten_phantom))
                  self.eaten_phantom += 1
               else:
                  pygame.mixer.music.pause()
                  pygame.time.wait(750)
                  death.play()
                  self.death() #death animation
                  lives.remove()
                  reset()

         def collision_CP(self):
            #pato-man eats a normal pellet
            if matriz[self.pos[1]][self.pos[0]] == 2:
               for coins in consumables: #Search the atual coins in coins_list
                  if coins.rect.x == player.pos[0] and coins.rect.y == player.pos[1]:
                     coins.kill() #Remove from all groups
                     break

               matriz[self.pos[1]][self.pos[0]] = 0
               score.add(10)

               player.change_sound()

			#Pato-man eats a power pellet
            elif matriz[self.pos[1]][self.pos[0]] == 3:
               player.eaten_phantom=0#keeps track of currently eaten ghosts
               for i in range(4):
                  if(phantom[i].mode == WAITING or phantom[i].mode == LEAVE):
                     phantom[i].frightened = True
                  else:
                     if(phantom[i].mode != FRIGHTENED):
                        phantom[i].change_direct()
                        phantom[i].mode = FRIGHTENED
                  phantom[i].reset_time()

               for power in consumables: #Search the atual power in power_list
                  if power.rect.x == player.pos[0] and power.rect.y == player.pos[1]:
                     power.kill() #Remove from all groups
                     break

               matriz[self.pos[1]][self.pos[0]] = 0
               score.add(50)

               player.change_sound()

      #Stage 1 matrix. Holds information on active game state
      #State 0 --> Empty
      #State 1 --> Wall
      #State 2 --> Coin
      #State 3 --> Power
      matriz = Matriz.mat()

      #Create the groups of sprites
      consumables = pygame.sprite.Group()
      phantom = [Ghost.Ghost(0), Ghost.Ghost(1), Ghost.Ghost(2), Ghost.Ghost(3)]
      phantom_list = pygame.sprite.Group()
      phantom_list.add(phantom[Inky  ])
      phantom_list.add(phantom[Blinky])
      phantom_list.add(phantom[Clyde ])
      phantom_list.add(phantom[Pinky ])

      for i in range(4):
         phantom[i].normal_speed=ghost_normal_speed
         phantom[i].slow_speed=ghost_slow_speed
         phantom[i].speed=ghost_normal_speed

      player = Player()
      score = Score(current_score,is_gamer)
      lives = Lives(current_lives,is_gamer)

      #Create the sprites of coins and powers and put them in the respective groups
      for i in range(len(matriz)):
         for j in range(len(matriz[0])):
            coins = Coins()
            power = Power()

            if (matriz[i][j] == 2):
               coins.rect.x = j
               coins.rect.y = i
               consumables.add(coins)

            if (matriz[i][j] == 3):
               power.rect.x = j
               power.rect.y = i
               consumables.add(power)

      #Inital screen appearence
      screen.fill((0, 0, 0))
      score.display()
      lives.display()
      screen.blit(background,(0,48))
      consumables.draw(screen) #displays the consumables
      screen.blit(font.render("ready!",False,pygame.Color("yellow")),(176,320))
      screen.blit(font.render("player one",False,pygame.Color("cyan")),(144,224))
      pygame.display.update()

      #Plays the beginning sound
      play_music('begin.wav',volume)
      pygame.time.wait(2150)

      #initial screen appearence 2
      screen.blit(background,(0,48))
      consumables.draw(screen) #displays the consumables
      for i in range(4):#displays ghosts
         screen.blit(phantom[i].img, (phantom[i].pos[0] - Ajuste, phantom[i].pos[1] - Ajuste))
      player.display()
      pygame.display.update()

      #Waits until sound is over so game can start
      pygame.time.wait(2150)
      play_music('music1.wav',volume)
      level_music = 'music1.wav'
      now_music = 'music1.wav'

      running = True
      player_time=0

      while running:

         clock.tick(40)

         for event in pygame.event.get():

            #Quit game
            if event.type == pygame.QUIT:
               running = False
               pygame.quit()

            #Directions keys
            if event.type == KEYDOWN:
               player.change_direct = 0 #If key is used, memory_direction has to update
               if event.key == K_UP:
                  player.direct = UP
               if event.key == K_DOWN:
                  player.direct = DOWN
               if event.key == K_LEFT:
                  player.direct = LEFT
               if event.key == K_RIGHT:
                   player.direct = RIGHT
               if event.key == K_ESCAPE:
                  pause_game()

         #Handles player movement
         player.time+=player.speed+15
         for qtd in range((player.time // 100) * 2):
            player.move()
            player.collision_CP()

         player.time=player.time%100
         #changes pato-man's mouth animation
         player.aberto=not player.aberto

         for i in range(4):
            phantom[i].time+=phantom[i].speed+15
            for j in range((phantom[i].time//100) * 2):
               phantom[i].move(player.pos[1], player.pos[0], player.direct, 0, 0, 0)
            phantom[i].time=phantom[i].time%100

         #If after .move() memory_direction failed or not
         if player.change_direct == 2:
            player.change_direct = 1

         player.collision_CP()

         #music handler
         #------------------------------------------
         if (phantom[Blinky].mode == FRIGHTENED or phantom[Inky  ].mode == FRIGHTENED or phantom[Clyde ].mode == FRIGHTENED or phantom[Pinky ].mode == FRIGHTENED or phantom[Blinky].frightened or phantom[Inky  ].frightened or phantom[Clyde ].frightened or phantom[Pinky ].frightened):
            player.speed=player.fast_speed
            if now_music!='power.wav' and now_music!='eyes.wav':
               now_music='power.wav'
               play_music('power.wav',volume)

         if (phantom[Blinky].mode == EATEN or phantom[Inky  ].mode == EATEN or phantom[Clyde ].mode == EATEN or phantom[Pinky ].mode == EATEN):
            if now_music!='eyes.wav':
               now_music='eyes.wav'
               play_music('eyes.wav',volume)
         elif now_music=='eyes.wav' and not (phantom[Blinky].mode == EATEN or phantom[Inky  ].mode == EATEN or phantom[Clyde ].mode == EATEN or phantom[Pinky ].mode == EATEN):
            now_music=''

         if not (phantom[Blinky].mode == FRIGHTENED or phantom[Inky  ].mode == FRIGHTENED or phantom[Clyde ].mode == FRIGHTENED or phantom[Pinky ].mode == FRIGHTENED) and not (phantom[Blinky].mode == EATEN or phantom[Inky  ].mode == EATEN or phantom[Clyde ].mode == EATEN or phantom[Pinky ].mode == EATEN or phantom[Blinky].frightened or phantom[Inky  ].frightened or phantom[Clyde ].frightened or phantom[Pinky ].frightened):
            player.speed=player.normal_speed
            if now_music!=level_music:
               now_music=level_music
               play_music(level_music,volume)

         if len(consumables)==144:
            level_music = 'music2.wav'

         if len(consumables)==83:
            level_music = 'music3.wav'

         if len(consumables)==27:
            level_music = 'music4.wav'
         #------------------------------------------

         #goes back to menu when level is finished
         if len(consumables)==0:
            play_music('win_sound.wav',volume)

            for frame in range(10):
               if frame==5:
                  play_music('win_sound.wav',volume)

               screen.fill((0,0,0))
               score.display()
               lives.display()
               player.display()
               screen.blit(background_white, (0, 48))
               pygame.display.update()
               pygame.time.wait(100)

               screen.fill((0,0,0))
               score.display()
               lives.display()
               player.display()
               screen.blit(background_open, (0, 48))
               pygame.display.update()
               pygame.time.wait(100)
            nonlocal index
            index+=1
            if index==len(levels):
               win_game()
               pygame.time.wait(400)
               break

            game(current_lives = lives.lives,current_score=score.score,player_normal_speed=levels[index][0],player_fast_speed=levels[index][1],ghost_normal_speed=levels[index][2],ghost_slow_speed=levels[index][3],is_gamer=levels[index][4])
            break
         #Display objects on screen
         screen.fill((0, 0, 0))
         score.display()
         lives.display()
         screen.blit(background,(0, 48))
         #Display coins and powers on screen
         consumables.draw(screen)
         for i in range(4):
            screen.blit(phantom[i].img, (phantom[i].pos[0]-Ajuste, phantom[i].pos[1]-Ajuste))
         player.display()
         player.collision()
         pygame.display.update()

   index=0
   if is_gamer:
      levels=[[100,90,130,90,True],[100,90,130,90,True],[100,90,130,90,True],[100,90,130,100,True],[100,90,130,90,True],[100,90,130,90,True],[100,90,130,90,True],[100,90,130,90,True],[100,90,130,90,True]]
      game(current_lives=1,current_score='00',player_normal_speed=levels[index][0],player_fast_speed=levels[index][1],ghost_normal_speed=levels[index][2],ghost_slow_speed=levels[index][3],is_gamer=levels[index][4])
   else:
      levels=[[60,65,62,50,False],[60,65,62,50,False],[60,65,55,50,False],[60,65,55,50,False],[65,75,65,60,False],[65,75,65,60,False],[65,75,65,60,False],[65,75,65,60,False],[65,75,65,60,False],[65,75,65,60,False]]
      game(current_lives=3,current_score='00',player_normal_speed=levels[index][0],player_fast_speed=levels[index][1],ghost_normal_speed=levels[index][2],ghost_slow_speed=levels[index][3],is_gamer=levels[index][4])

   #victory screen
   go_to_menu()

if __name__=='__main__':
   setup(0.10,True)


"""

    run an algorithm in training mode

"""

import argparse

from core.AlgorithmWrapper import AlgorithmWrapper

def run(algo):
    """
        run the given algorithm in training mode
    """

    algo = AlgorithmWrapper(algo)
    algo.runAlgorithm()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Wrapper to run a given algorithm')
    parser.add_argument(
        'algo',
        help='Path to the folder of the algorithm you want to run'
    )

    args = parser.parse_args()
    run(args.algo)


from django.shortcuts import render, get_object_or_404
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import Music, Artist, Comment
from .serializers import MusicSerializer, ArtistSerializer, CommentSerializer, ArtistDetailSerializer

@api_view(['GET'])
def music_list(request):
    # 만약에 artist_pk 가 Query Params 로 넘어온다면 artist_pk 로 필터링한 값만 응답
    params = {}
    artist_pk = request.GET.get('artist_pk')
    if artist_pk is not None:
        params['artist_id'] = artist_pk
    musics = Music.objects.filter(**params)
    serializer = MusicSerializer(musics, many=True)
    return Response(serializer.data)

@api_view(['GET', 'PUT', 'DELETE'])
def music_detail(request, music_pk):
    music = get_object_or_404(Music, pk=music_pk)
    if request.method == 'GET':
        serializer = MusicSerializer(music)
        return Response(serializer.data)
    elif request.method == 'PUT':
        serializer = MusicSerializer(data=request.data, instance=music)
        if serializer.is_valid(raise_exception=True):
            serializer.save()
        return Response(serializer.data)
    else:
        music.delete()
        return Response({'message': 'music has been deleted'})

@api_view(['GET'])
def artists_list(request):
    artists = Artist.objects.all()
    serializer = ArtistSerializer(artists, many=True)
    return Response(serializer.data)

@api_view(['GET'])
def artist_detail(request, artist_pk):
    artist = get_object_or_404(Artist, pk=artist_pk)
    serializer = ArtistDetailSerializer(artist)
    return Response(serializer.data)

@api_view(['GET'])
def comments_list(request):
    comments = Comment.objects.all()
    serializer = CommentSerializer(comments, many=True)
    return Response(serializer.data)

@api_view(['GET', 'PUT', 'DELETE'])
def comment_detail(request, comment_pk):
    comment = get_object_or_404(Comment, pk=comment_pk)
    if request.method == 'GET':
        serializer = CommentSerializer(comment)
        return Response(serializer.data)
    elif request.method == 'PUT':
        serializer = CommentSerializer(data=request.data, instance=comment)
        if serializer.is_valid(raise_exception=True):
            serializer.save()
        return Response(serializer.data)
    else:
        comment.delete()
        return Response({'message': 'Comment has been deleted'})

@api_view(['POST'])
def comments_create(request, music_pk):
    print(request.data)
    serializer = CommentSerializer(data=request.data)
    # 검증에 실패하면 400 Bad Request Error 발생시킴
    if serializer.is_valid(raise_exception=True):
        serializer.save(music_id=music_pk)
    return Response(serializer.data)


import shutil
import tempfile

from django.conf import settings
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import Client, TestCase
from django.urls import reverse

from posts.forms import PostForm
from posts.models import Comment, Group, Post, User

TEMP_MEDIA_ROOT = tempfile.mkdtemp(dir=settings.BASE_DIR)

class PostBaseTestCase(TestCase):

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.group = Group.objects.create(
            title='test_group',
            slug='test_slug',
            description='test_description',
        )

        cls.author = User.objects.create_user(username='auth')
        cls.post = Post.objects.create(
            author=cls.author,
            text='test_post',
            group=cls.group,
            pub_date='test_date',
        )
        cls.url_index = '/'
        cls.url_group = f'/group/{cls.group.slug}/'
        cls.url_profile = f'/profile/{cls.author.username}/'
        cls.url_detail = f'/posts/{cls.post.pk}/'
        cls.form = PostForm()
        cls.user = User.objects.create_user(username='noname')

    @classmethod
    def tearDownClass(cls):
        super().tearDownClass()
        shutil.rmtree(TEMP_MEDIA_ROOT, ignore_errors=True)

    def setUp(self) -> None:
        self.guest_client = Client()

        self.author_client = Client()
        self.author_client.force_login(self.author)

        self.authorized_client = Client()
        self.authorized_client.force_login(self.user)

class PostFormTests(PostBaseTestCase):

    def test_labels_and_help_text(self):
        """Проверка labels и help_text."""
        fields = [
            'text',
            'group',
        ]

        for field in fields:
            with self.subTest(field=field):
                self.assertEqual(
                    self.form.fields[field].label,
                    Post._meta.get_field(field).verbose_name.capitalize()
                )
                self.assertEqual(
                    self.form.fields[field].help_text,
                    Post._meta.get_field(field).help_text
                )

    def test_create_post(self):
        """Валидная форма создает запись в Post."""
        posts_count = Post.objects.count()
        small_gif = (
            b'\x47\x49\x46\x38\x39\x61\x02\x00'
            b'\x01\x00\x80\x00\x00\x00\x00\x00'
            b'\xFF\xFF\xFF\x21\xF9\x04\x00\x00'
            b'\x00\x00\x00\x2C\x00\x00\x00\x00'
            b'\x02\x00\x01\x00\x00\x02\x02\x0C'
            b'\x0A\x00\x3B'
        )
        uploaded = SimpleUploadedFile(
            name='small.gif',
            content=small_gif,
            content_type='image/gif'
        )
        form_data = {
            'text': 'Тестовая запись 2',
            'group': self.group.pk,
            'image': uploaded,
        }
        response = self.authorized_client.post(
            reverse('posts:post_create'),
            data=form_data,
            follow=True)
        self.assertRedirects(response, reverse('posts:profile', kwargs={
            'username': f'{self.user.username}'}))
        self.assertEqual(Post.objects.count(), posts_count + 1)

        post = Post.objects.latest('id')
        self.assertTrue(post.text, form_data['text'])
        self.assertTrue(post.group, form_data['group'])
        self.assertTrue(post.image, 'posts/small.gif')
        self.assertTrue(post.author, self.post.author)

    def test_edit_post_author(self):
        """Валидная форма редактирует запись от имени автора."""
        posts_count = Post.objects.count()
        new_group = Group.objects.create(
            title='test_new_group',
            slug='test_new_group_slug',
            description='test_new_group_description',
        )
        small_gif = (
            b'\x47\x49\x46\x38\x39\x61\x02\x00'
            b'\x01\x00\x80\x00\x00\x00\x00\x00'
            b'\xFF\xFF\xFF\x21\xF9\x04\x00\x00'
            b'\x00\x00\x00\x2C\x00\x00\x00\x00'
            b'\x02\x00\x01\x00\x00\x02\x02\x0C'
            b'\x0A\x00\x3B'
        )
        uploaded = SimpleUploadedFile(
            name='small.gif',
            content=small_gif,
            content_type='image/gif'
        )
        form_data = {
            'text': 'Новый текст',
            'group': new_group.pk,
            'image': uploaded,
        }
        response = self.author_client.post(
            reverse('posts:post_edit', kwargs={
                'post_id': f'{self.post.pk}'}),
            data=form_data,
            follow=True)
        self.assertRedirects(response, reverse('posts:post_detail', kwargs={
            'post_id': f'{self.post.pk}'}))
        self.assertEqual(Post.objects.count(), posts_count)

        post = Post.objects.latest('id')
        self.assertEqual(post.text, form_data['text'])
        self.assertEqual(post.group.pk, form_data['group'])
        self.assertTrue(post.image, 'posts/small.gif')
        self.assertTrue(post.author, self.post.author)

    def test_edit_post_not_author(self):
        """Валидная форма не редактирует запись, если пользователь не автор."""
        posts_count = Post.objects.count()
        new_group = Group.objects.create(
            title='test_new_group',
            slug='test_new_group_slug',
            description='test_new_group_description',
        )
        form_data = {
            'text': 'Новый текст',
            'group': new_group.pk,
        }
        response = self.authorized_client.post(
            reverse('posts:post_edit', kwargs={
                'post_id': f'{self.post.pk}'}),
            data=form_data,
            follow=True)
        self.assertRedirects(response, reverse('posts:post_detail', kwargs={
            'post_id': f'{self.post.pk}'}))
        self.assertEqual(Post.objects.count(), posts_count)

        post = Post.objects.latest('id')
        self.assertEqual(self.post.text, 'test_post')
        self.assertEqual(self.post.group.slug, 'test_slug')
        self.assertTrue(post.author, self.post.author)

    def test_edit_post_not_authorized(self):
        """Пост не редактируется не авторизованным."""
        posts_count = Post.objects.count()
        new_group = Group.objects.create(
            title='test_new_group',
            slug='test_new_group_slug',
            description='test_new_group_description',
        )
        form_data = {
            'text': 'Новый текст',
            'group': new_group.pk,
        }
        response = self.guest_client.post(
            reverse('posts:post_edit', kwargs={
                'post_id': f'{self.post.pk}'}),
            data=form_data,
            follow=True)
        self.assertRedirects(response,
                             '/auth/login/?next=/posts/1/edit/')
        self.assertEqual(Post.objects.count(), posts_count)

        post = Post.objects.latest('id')
        self.assertTrue(post.author, self.post.author)

    def test_comment_post_authorized(self):
        """Пост комментируется авторизованным."""
        post = self.post
        form_data = {
            'text': 'Текстовый комментарий',
            post: post,
        }
        response = self.authorized_client.post(
            reverse('posts:add_comment', kwargs={
                'post_id': f'{self.post.pk}'}),
            data=form_data,
            follow=True)
        self.assertRedirects(response, reverse('posts:post_detail', kwargs={
            'post_id': f'{self.post.pk}'}))

        comment = Comment.objects.latest('id')
        self.assertEqual(comment.text, form_data['text'])
        self.assertEqual(Comment.objects.count(), 1)

    def test_comment_post_not_authorized(self):
        """Пост не комментируется не авторизованным."""
        post = self.post
        form_data = {
            'text': 'Текстовый комментарий',
            post: post,
        }
        response = self.guest_client.post(
            reverse('posts:add_comment', kwargs={
                'post_id': f'{self.post.pk}'}),
            data=form_data,
            follow=True)
        self.assertRedirects(response, '/auth/login/?next=/posts/1/comment/')
        self.assertEqual(Comment.objects.count(), 0)


# importing necessary libraries

import numpy as np # linear algebra
import pandas as pd # data processing and preparation
import matplotlib.pyplot as plt # Visualization
import seaborn as sns # Visualization on top of matplotlib
import matplotlib
import seaborn as sns # Advanced visualization

#import data
df = pd.read_csv("https://raw.githubusercontent.com/UCDPA-Moses/UCDPA_Moses/main/Car_sales.csv")
print(df.head())

#Data Exploratory
print("Information about the dataset")
print(df.info())

print("Description of the dataset")
print(df.describe())

print("Shape, i.e Rows by Columns")
print(df.shape)

print(df.duplicated) #checking for duplicates

# Replacing space in column names
df.columns = df.columns.str.replace(' ', '_') #using the replace function

total_missing=df.isna().sum().sum() #checking for missing values in the dataset
print("Total missing values are:", total_missing)

#Replacing missing data with the mean of the column
print("Replacing missing data with the mean of the column")
df.fillna(df.mean(),inplace=True)
total_missing=df.isna().sum().sum() #checking for missing values in the dataset
print("Total missing values are:", total_missing)

#checking null values
print('null values in each column is:')
print(df.isnull().sum())

# Sorting all the data in the dataset with respexct to Engine size
df_sorted = df.sort_values(by = 'Engine_size', ascending = False) #sorting the data by Engine size
print("sorting the data by Engine size")
print(df_sorted.head())

# Correlation with Price_in_thousands
df_corr = df.corr()['Price_in_thousands'][:-1]
print(df_corr)

# Counting all the cars in the dataset by its category
categories_avail = df.groupby('Manufacturer').size()
print("The different categories / manufacturers of cars in this dataset are:")
print(categories_avail)

print("Categories of cars based on the vehicle type")
categories_avail2= df.groupby('Vehicle_type').size()
print(categories_avail2)

#Formatting the default setting the charts and graphs
sns.set_style('whitegrid')
matplotlib.rcParams['font.size'] =8
matplotlib.rcParams['figure.figsize'] = (10,6)
matplotlib.rcParams['figure.facecolor'] = '#00000000'

#plotting a bar chat for the groups of cars
plt.pie(categories_avail, labels=categories_avail.index)
plt.title('Plot of Different Categories / Manufacturers of Cars',size = 25)
plt.show()

# Creating histogram for continuous numerical variable
plt.hist(df['Horsepower'])
plt.title('Distribution of different Horsepower',size = 25)
plt.xlabel('Horsepower value',size = 18)
plt.ylabel('Number of cars',size = 18)
plt.show()

plt.pie(categories_avail2, labels=categories_avail2.index, autopct='%1.1f%%')
plt.title('Plot of Different Vehicle type', size = 25)
plt.show()

#Machine Learning development to predict the selling price of car

#Removing columns that does not contribute to the selling price
df = df.drop(['Model','Latest_Launch'], axis = 1)

#Data encoding using dummy values
df=pd.get_dummies(df,drop_first=True)
print('Dataset with categorical values encoded')
print(df)

#Splitting dataset into features and target variables
target= df['Price_in_thousands']
features = df.drop(['Price_in_thousands'],axis = 1)

#Feature importance
from sklearn.ensemble import ExtraTreesRegressor
model = ExtraTreesRegressor()
model.fit(features,target)

#plotting the feature importance
feat_importances = pd.Series(model.feature_importances_, index=features.columns)
feat_importances.nlargest(10).plot(kind='barh')
plt.title('Feature importance for the highest 10 variables',size = 25)
plt.show()

#Performing data scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
sc.fit(features)
input_scaled = sc.transform(features)

#Splitting data into training and test set
from sklearn.model_selection import train_test_split

x_train,x_test,y_train,y_test = train_test_split(input_scaled,target,test_size = 0.2, random_state=100)

#Building linear regression model for the selling price

#fit the model for training set
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(x_train,y_train)

#predict the values for the training dataset
y_pred = model.predict(x_train)

#Plotting the line of best fit
plt.scatter(y_train,y_pred)
plt.title('Plotting actual target vs predicted target',size = 25)
plt.xlabel("Target (y_train)", size = 18)
plt.ylabel("Predicted (y_hat)", size = 18)
plt.show()

# Performance Metrics of the training set
print("Linear regression accuracy is {:.2f}%".format(model.score(x_train, y_train) *100))

#Testing the model

#predict the values for the training dataset
y_pred_test = model.predict(x_test)

plt.scatter(y_test,y_pred_test)
plt.title('Linear Regression: Predicting target values from the feature values',size = 25)
plt.xlabel("Targets (y_test)", size = 18)
plt.ylabel("Predictions (y_pred_test)", size = 18)
plt.show()

from sklearn.metrics import r2_score, mean_absolute_error,mean_squared_error
print("R_square value is: ", r2_score(y_test,y_pred_test))
print('MAE:',mean_absolute_error(y_test, y_pred_test))
print("MSE is: ", mean_squared_error(y_test,y_pred_test))
print("RMSE is: ", np.sqrt(mean_squared_error(y_test,y_pred_test)))

#Building a Random Forest model

from sklearn.ensemble import RandomForestRegressor as RFC

regressor=RFC() # creating a random forest regressor model
regressor.fit(x_train,y_train)

# Performance Metrics of the training set
print("Random forest regressor accuracy is {:.2f}%".format(regressor.score(x_train, y_train) *100))

predictions=regressor.predict(x_test)

plt.scatter(y_test,predictions)
plt.title('Random Forest: Predicting target values from the feature values',size = 25)
plt.xlabel("Targets (y_test)", size = 18)
plt.ylabel("Predictions (y_pred_test)", size = 18)
plt.show()

#  Performance Metrics
print("R_square value is: ", r2_score(y_test,predictions))
print('MAE:', mean_absolute_error(y_test, predictions))
print('MSE:', mean_squared_error(y_test, predictions))
print('RMSE:', np.sqrt(mean_squared_error(y_test, predictions)))


from Crypto.Cipher import AES, PKCS1_OAEP, PKCS1_v1_5
from Crypto.PublicKey import RSA
from Crypto.Util.Padding import pad, unpad
from Crypto.Util import Counter
from Crypto.Util.number import inverse, long_to_bytes, bytes_to_long, isPrime, getPrime, getRandomRange, sieve_base
from tqdm import tqdm
from pwn import *
from sage.all import *
import gmpy2, pickle, itertools, sys, json, hashlib, os, math, time, base64, binascii, string, re, struct, datetime, subprocess
import numpy as np
import random as rand
import multiprocessing as mp
from base64 import b64encode, b64decode
from sage.modules.free_module_integer import IntegerLattice
from Crypto.Hash import SHA3_256, HMAC, BLAKE2s
from Crypto.Cipher import AES, ARC4, DES
from mt19937predictor import MT19937Predictor
from Crypto.Hash import SHA256

conn = remote("139.162.61.222", 13373)

def bitcount(x):
    return bin(x).count("1")

d_val = [-1 for _ in range(128)]

THRESHOLD = 40
query_rem = 2022

c_results = []
for i in range(20):
    query_rem -= 1
    conn.sendline(b"c")
    c_fault, c_res = eval(conn.recvline())
    c_results.append([c_fault, c_res])

n = 0
while True:
    query_rem -= 1
    conn.sendline(b"-1")
    vec, val = eval(conn.recvline())
    if val != 1:
        n = val + 1
        break

COMP = [0] * 500
INV = [0] * 500
for i in range(500):
    COMP[i] = Zmod(n)(4) ** (1 << i)
    INV[i] = COMP[i] ** (-1)

conn.sendlines([b"2"] * query_rem)
lines = conn.recvlines(query_rem)

res = []
for i in range(query_rem):
    vec, val = eval(lines[i])
    res.append([vec, val])

def getbit(x, i):
    return (int(x) >> i) & 1

def work(mask1, val1, mask2, val2):
    # if too slow rewrite in sage

    # 2^(d + mask1 - 2 * (d & mask1)) == val1
    # 2^(d + mask2 - 2 * (d & mask2)) == val2
    # 2^(mask1 - mask2 - 2 (d & mask1) + 2 * (d & mask2)) == val1 / val2
    # remove common parts, still same
    # 4^((d & mask2) - (d & mask1)) == target

    val1 = Zmod(n)(val1)
    val2 = Zmod(n)(val2)

    target = (val1 * (val2 ** -1))
    target = (target * Zmod(n)(2) ** mask2)
    target = (target * Zmod(n)(2) ** (-mask1))

    common = mask1 & mask2
    mask1 -= common
    mask2 -= common

    for i in range(128):
        if d_val[i] == 0:
            if getbit(mask1, i) == 1:
                mask1 -= (1 << i)
            if getbit(mask2, i) == 1:
                mask2 -= (1 << i)
        if d_val[i] == 1:
            if getbit(mask1, i) == 1:
                target = (target * COMP[i])
                mask1 -= (1 << i)
            if getbit(mask2, i) == 1:
                target = (target * INV[i])
                mask2 -= (1 << i)

    # now MITM work begins
    # 4^((d & mask2) - (d & mask1)) == target

    d_search = []
    for i in range(128):
        if getbit(mask1, i) == 1 or getbit(mask2, i) == 1:
            d_search.append(i)
    lef = d_search[:len(d_search) // 2]
    rig = d_search[len(d_search) // 2:]

    dic = {}
    for i in tqdm(range(1 << len(lef))):
        val = Zmod(n)(1)
        for j in range(len(lef)):
            bit = getbit(i, j)
            if bit == 0:
                continue
            if getbit(mask1, lef[j]) == 1:
                val = (val * INV[lef[j]])
            if getbit(mask2, lef[j]) == 1:
                val = (val * COMP[lef[j]])
        dic[val] = i

    for i in tqdm(range(1 << len(rig))):
        val = Zmod(n)(1)
        for j in range(len(rig)):
            bit = getbit(i, j)
            if bit == 0:
                continue
            if getbit(mask1, rig[j]) == 1:
                val = (val * INV[rig[j]])
            if getbit(mask2, rig[j]) == 1:
                val = (val * COMP[rig[j]])
        desire = (target * (val ** -1))
        if desire in dic.keys():
            print("OK!")
            final_mask = dic[desire] + (1 << len(lef)) * i
            for j in range(len(d_search)):
                d_val[d_search[j]] = getbit(final_mask, j)
            return
    return

t = 0
for i in tqdm(range(query_rem)):
    for j in range(i+1, query_rem):
        if bitcount(res[i][0] ^ res[j][0]) < THRESHOLD:
            work(res[i][0], res[i][1], res[j][0], res[j][1])
            t |= (res[i][0] ^ res[j][0])

undetermined = []
for i in range(128):
    if d_val[i] == -1:
        undetermined.append(i)
l = len(undetermined)

d_final = 0
for i in range(1 << l):
    for j in range(l):
        d_val[undetermined[j]] = ((i >> j) & 1)
    d = 0
    for j in range(128):
        d += d_val[j] * (1 << j)
    isok = True
    for j in range(query_rem):
        if pow(2, d ^ res[j][0], n) != res[j][1]:
            isok = False
    if isok:
        d_final = d
        print("d found!")

for i in range(20):
    for j in range(i+1, 20):
        actual_exp1 = d_final ^ c_results[i][0]
        actual_exp2 = d_final ^ c_results[j][0]
        if GCD(actual_exp1, actual_exp2) == 1:
            print("OK, exists pair to do GCD stuff")
            u = inverse(actual_exp1, actual_exp2)
            v = (actual_exp1 * u - 1) // actual_exp2
            # c_res[i][1]^u * c_res[j][1]^-v = c
            ptxt = pow(c_results[i][1], u * d_final, n)
            ptxt = ptxt * inverse(pow(c_results[j][1], v * d, n), n)
            ptxt = ptxt % n
            print("FLAG", long_to_bytes(int(ptxt)))


from typing import List

class Solution1:
    def reverse(self, nums, left, right):
        while left < right:
            nums[left], nums[right] = nums[right], nums[left]
            left += 1
            right -= 1

    def rotate(self, nums: [int], k: int) -> None:
        """
        Time O(n)
        Space O(1)
        k 需要先除以长度，计算余数，因为k可能超过数组的长度，会出现重复循环的旋转
        """
        k %= len(nums)

        self.reverse(nums, 0, len(nums) - 1)
        self.reverse(nums, 0, k - 1)
        self.reverse(nums, k, len(nums) - 1)

        return nums

class Solution2:
    def rotate(self, nums: List[int], k: int) -> None:
        """
        Time O(n * k) 每一次insert都要移动整个数组
        Space O(1)
        弹出尾巴，然后insert进开始
        """
        for i in range(k):
            element = nums.pop()
            nums.insert(0, element)

        return nums

s = Solution2()
print(s.rotate(nums=[1, 2, 3, 4, 5, 6, 7], k=3))


import random
from unittest.mock import patch
import pytest
from base58 import b58decode
from ipld import marshal, multihash
from operator import attrgetter

from transactions.common import crypto
from transactions.common.transaction import Transaction, TransactionLink, Input
from transactions.types.assets.create import Create
from transactions.types.assets.transfer import Transfer
from planetmint.exceptions import CriticalDoubleSpend

pytestmark = pytest.mark.bdb

class TestBigchainApi(object):
    def test_get_spent_with_double_spend_detected(self, b, alice):
        from transactions.common.exceptions import DoubleSpend

        from planetmint.exceptions import CriticalDoubleSpend

        tx = Create.generate([alice.public_key], [([alice.public_key], 1)])
        tx = tx.sign([alice.private_key])

        b.models.store_bulk_transactions([tx])

        transfer_tx = Transfer.generate(tx.to_inputs(), [([alice.public_key], 1)], asset_ids=[tx.id])
        transfer_tx = transfer_tx.sign([alice.private_key])
        transfer_tx2 = Transfer.generate(tx.to_inputs(), [([alice.public_key], 2)], asset_ids=[tx.id])
        transfer_tx2 = transfer_tx2.sign([alice.private_key])

        with pytest.raises(DoubleSpend):
            b.validate_transaction(transfer_tx2, [transfer_tx])

        b.models.store_bulk_transactions([transfer_tx])

        with pytest.raises(DoubleSpend):
            b.validate_transaction(transfer_tx2)

        with pytest.raises(CriticalDoubleSpend):
            b.models.store_bulk_transactions([transfer_tx2])

    def test_double_inclusion(self, b, alice):
        from planetmint.backend.exceptions import OperationError
        from planetmint.backend.tarantool.sync_io.connection import TarantoolDBConnection

        tx = Create.generate([alice.public_key], [([alice.public_key], 1)])
        tx = tx.sign([alice.private_key])

        b.models.store_bulk_transactions([tx])
        if isinstance(b.models.connection, TarantoolDBConnection):
            with pytest.raises(CriticalDoubleSpend):
                b.models.store_bulk_transactions([tx])
        else:
            with pytest.raises(OperationError):
                b.models.store_bulk_transactions([tx])

    @pytest.mark.usefixtures("inputs")
    def test_non_create_input_not_found(self, b, user_pk):
        from planetmint_cryptoconditions import Ed25519Sha256
        from transactions.common.exceptions import InputDoesNotExist

        # Create an input for a non existing transaction
        input = Input(
            Ed25519Sha256(public_key=b58decode(user_pk)), [user_pk], TransactionLink("somethingsomething", 0)
        )
        tx = Transfer.generate([input], [([user_pk], 1)], asset_ids=["mock_asset_link"])
        with pytest.raises(InputDoesNotExist):
            b.validate_transaction(tx)

    def test_write_transaction(self, b, user_sk, user_pk, alice, create_tx):
        asset1 = {"data": "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4"}

        tx = Create.generate([alice.public_key], [([alice.public_key], 1)], assets=[asset1]).sign([alice.private_key])
        b.models.store_bulk_transactions([tx])

        tx_from_db = b.models.get_transaction(tx.id)

        before = tx.to_dict()
        after = tx_from_db.to_dict()

        assert before["assets"][0] == after["assets"][0]
        before.pop("assets", None)
        after.pop("assets", None)
        assert before == after

class TestTransactionValidation(object):
    def test_non_create_input_not_found(self, b, signed_transfer_tx):
        from transactions.common.exceptions import InputDoesNotExist
        from transactions.common.transaction import TransactionLink

        signed_transfer_tx.inputs[0].fulfills = TransactionLink("c", 0)
        with pytest.raises(InputDoesNotExist):
            b.validate_transaction(signed_transfer_tx)

    @pytest.mark.usefixtures("inputs")
    def test_non_create_valid_input_wrong_owner(self, b, user_pk):
        from transactions.common.crypto import generate_key_pair
        from transactions.common.exceptions import InvalidSignature
        from transactions.common.transaction_link import TransactionLink

        output = b.models.get_outputs_filtered(user_pk).pop()
        input_transaction = b.models.get_transaction(output.transaction_id)
        sk, pk = generate_key_pair()
        tx = Create.generate([pk], [([user_pk], 1)])
        tx.operation = "TRANSFER"
        tx.assets = [{"id": input_transaction.id}]
        tx.inputs[0].fulfills = TransactionLink(output.transaction_id, output.index)

        with pytest.raises(InvalidSignature):
            b.validate_transaction(tx)

    @pytest.mark.usefixtures("inputs")
    def test_non_create_double_spend(self, b, signed_create_tx, signed_transfer_tx, double_spend_tx):
        from transactions.common.exceptions import DoubleSpend

        b.models.store_bulk_transactions([signed_create_tx, signed_transfer_tx])

        with pytest.raises(DoubleSpend):
            b.validate_transaction(double_spend_tx)

class TestMultipleInputs(object):
    def test_transfer_single_owner_single_input(self, b, inputs, user_pk, user_sk):
        user2_sk, user2_pk = crypto.generate_key_pair()
        tx_output = b.models.get_outputs_filtered(user_pk).pop()
        input_tx = b.models.get_transaction(tx_output.transaction_id)
        tx_converted = Transaction.from_dict(input_tx.to_dict(), True)

        tx = Transfer.generate(tx_converted.to_inputs(), [([user2_pk], 1)], asset_ids=[input_tx.id])
        tx = tx.sign([user_sk])

        # validate transaction
        b.validate_transaction(tx)
        assert len(tx.inputs) == 1
        assert len(tx.outputs) == 1

    def test_single_owner_before_multiple_owners_after_single_input(self, b, user_sk, user_pk, inputs):
        user2_sk, user2_pk = crypto.generate_key_pair()
        user3_sk, user3_pk = crypto.generate_key_pair()
        tx_output = b.models.get_outputs_filtered(user_pk).pop()

        input_tx = b.models.get_transaction(tx_output.transaction_id)
        tx_converted = Transaction.from_dict(input_tx.to_dict(), True)

        tx = Transfer.generate(tx_converted.to_inputs(), [([user2_pk, user3_pk], 1)], asset_ids=[input_tx.id])
        tx = tx.sign([user_sk])

        b.validate_transaction(tx)
        assert len(tx.inputs) == 1
        assert len(tx.outputs) == 1

    @pytest.mark.usefixtures("inputs")
    def test_multiple_owners_before_single_owner_after_single_input(self, b, user_sk, user_pk, alice):
        user2_sk, user2_pk = crypto.generate_key_pair()
        user3_sk, user3_pk = crypto.generate_key_pair()

        tx = Create.generate([alice.public_key], [([user_pk, user2_pk], 1)])
        tx = tx.sign([alice.private_key])
        b.models.store_bulk_transactions([tx])

        tx_output = b.models.get_outputs_filtered(user_pk).pop()
        input_tx = b.models.get_transaction(tx_output.transaction_id)
        input_tx_converted = Transaction.from_dict(input_tx.to_dict(), True)

        transfer_tx = Transfer.generate(input_tx_converted.to_inputs(), [([user3_pk], 1)], asset_ids=[input_tx.id])
        transfer_tx = transfer_tx.sign([user_sk, user2_sk])

        # validate transaction
        b.validate_transaction(transfer_tx)
        assert len(transfer_tx.inputs) == 1
        assert len(transfer_tx.outputs) == 1

    @pytest.mark.usefixtures("inputs")
    def test_multiple_owners_before_multiple_owners_after_single_input(self, b, user_sk, user_pk, alice):
        user2_sk, user2_pk = crypto.generate_key_pair()
        user3_sk, user3_pk = crypto.generate_key_pair()
        user4_sk, user4_pk = crypto.generate_key_pair()

        tx = Create.generate([alice.public_key], [([user_pk, user2_pk], 1)])
        tx = tx.sign([alice.private_key])
        b.models.store_bulk_transactions([tx])

        # get input
        tx_output = b.models.get_outputs_filtered(user_pk).pop()
        tx_input = b.models.get_transaction(tx_output.transaction_id)
        input_tx_converted = Transaction.from_dict(tx_input.to_dict(), True)

        tx = Transfer.generate(input_tx_converted.to_inputs(), [([user3_pk, user4_pk], 1)], asset_ids=[tx_input.id])
        tx = tx.sign([user_sk, user2_sk])

        b.validate_transaction(tx)
        assert len(tx.inputs) == 1
        assert len(tx.outputs) == 1

    def test_get_owned_ids_single_tx_single_output(self, b, user_sk, user_pk, alice):
        user2_sk, user2_pk = crypto.generate_key_pair()

        tx = Create.generate([alice.public_key], [([user_pk], 1)])
        tx = tx.sign([alice.private_key])
        b.models.store_bulk_transactions([tx])

        stored_tx = b.models.get_transaction(tx.id)

        owned_inputs_user1 = b.models.get_outputs_filtered(user_pk)
        owned_inputs_user2 = b.models.get_outputs_filtered(user2_pk)
        assert owned_inputs_user1 == [stored_tx.outputs[0]]
        assert owned_inputs_user2 == []

        tx_transfer = Transfer.generate(tx.to_inputs(), [([user2_pk], 1)], asset_ids=[tx.id])
        tx_transfer = tx_transfer.sign([user_sk])
        b.models.store_bulk_transactions([tx_transfer])

        owned_inputs_user1 = b.models.get_outputs_filtered(user_pk)
        owned_inputs_user2 = b.models.get_outputs_filtered(user2_pk)

        stored_tx_transfer = b.models.get_transaction(tx_transfer.id)

        assert owned_inputs_user1 == [stored_tx.outputs[0]]
        assert owned_inputs_user2 == [stored_tx_transfer.outputs[0]]

    def test_get_owned_ids_single_tx_multiple_outputs(self, b, user_sk, user_pk, alice):
        user2_sk, user2_pk = crypto.generate_key_pair()

        # create divisible asset
        tx_create = Create.generate([alice.public_key], [([user_pk], 1), ([user_pk], 1)])
        tx_create_signed = tx_create.sign([alice.private_key])
        b.models.store_bulk_transactions([tx_create_signed])

        # get input
        owned_inputs_user1 = b.models.get_outputs_filtered(user_pk)
        owned_inputs_user2 = b.models.get_outputs_filtered(user2_pk)

        stored_tx = b.models.get_transaction(tx_create.id)

        expected_owned_inputs_user1 = [stored_tx.outputs[0], stored_tx.outputs[1]]
        assert sorted(owned_inputs_user1, key=attrgetter("index")) == sorted(
            expected_owned_inputs_user1, key=attrgetter("index")
        )
        assert owned_inputs_user2 == []

        # transfer divisible asset divided in two outputs
        tx_transfer = Transfer.generate(
            tx_create.to_inputs(), [([user2_pk], 1), ([user2_pk], 1)], asset_ids=[tx_create.id]
        )
        tx_transfer_signed = tx_transfer.sign([user_sk])
        b.models.store_bulk_transactions([tx_transfer_signed])
        stored_tx_transfer = b.models.get_transaction(tx_transfer.id)

        owned_inputs_user1 = b.models.get_outputs_filtered(user_pk)
        owned_inputs_user2 = b.models.get_outputs_filtered(user2_pk)
        assert sorted(owned_inputs_user1, key=attrgetter("index")) == sorted(
            expected_owned_inputs_user1, key=attrgetter("index")
        )
        assert sorted(owned_inputs_user2, key=attrgetter("index")) == sorted(
            [stored_tx_transfer.outputs[0], stored_tx_transfer.outputs[1]], key=attrgetter("index")
        )

    def test_get_owned_ids_multiple_owners(self, b, user_sk, user_pk, alice):
        user2_sk, user2_pk = crypto.generate_key_pair()
        user3_sk, user3_pk = crypto.generate_key_pair()

        tx = Create.generate([alice.public_key], [([user_pk, user2_pk], 1)])
        tx = tx.sign([alice.private_key])

        b.models.store_bulk_transactions([tx])
        stored_tx = b.models.get_transaction(tx.id)

        owned_inputs_user1 = b.models.get_outputs_filtered(user_pk)
        owned_inputs_user2 = b.models.get_outputs_filtered(user_pk)
        expected_owned_inputs_user1 = [stored_tx.outputs[0]]

        assert owned_inputs_user1 == owned_inputs_user2
        assert owned_inputs_user1 == expected_owned_inputs_user1

        tx = Transfer.generate(tx.to_inputs(), [([user3_pk], 1)], asset_ids=[tx.id])
        tx = tx.sign([user_sk, user2_sk])
        b.models.store_bulk_transactions([tx])

        owned_inputs_user1 = b.models.get_outputs_filtered(user_pk)
        owned_inputs_user2 = b.models.get_outputs_filtered(user2_pk)
        spent_user1 = b.models.get_spending_transaction(tx.id, 0)

        assert owned_inputs_user1 == owned_inputs_user2
        assert not spent_user1

    def test_get_spent_single_tx_single_output(self, b, user_sk, user_pk, alice):
        user2_sk, user2_pk = crypto.generate_key_pair()

        tx = Create.generate([alice.public_key], [([user_pk], 1)])
        tx = tx.sign([alice.private_key])
        b.models.store_bulk_transactions([tx])

        owned_inputs_user1 = b.models.get_outputs_filtered(user_pk).pop()

        # check spents
        input_txid = owned_inputs_user1.transaction_id
        spent_inputs_user1 = b.models.get_spending_transaction(input_txid, 0)
        assert spent_inputs_user1 is None

        # create a transaction and send it
        tx = Transfer.generate(tx.to_inputs(), [([user2_pk], 1)], asset_ids=[tx.id])
        tx = tx.sign([user_sk])
        b.models.store_bulk_transactions([tx])

        spent_inputs_user1 = b.models.get_spending_transaction(input_txid, 0)
        assert spent_inputs_user1 == tx.to_dict()

    def test_get_spent_single_tx_multiple_outputs(self, b, user_sk, user_pk, alice):
        # create a new users
        user2_sk, user2_pk = crypto.generate_key_pair()

        # create a divisible asset with 3 outputs
        tx_create = Create.generate([alice.public_key], [([user_pk], 1), ([user_pk], 1), ([user_pk], 1)])
        tx_create_signed = tx_create.sign([alice.private_key])
        b.models.store_bulk_transactions([tx_create_signed])

        owned_inputs_user1 = b.models.get_outputs_filtered(user_pk)

        # check spents
        for input_tx in owned_inputs_user1:
            assert b.models.get_spending_transaction(input_tx.transaction_id, input_tx.index) is None

        # transfer the first 2 inputs
        tx_transfer = Transfer.generate(
            tx_create.to_inputs()[:2], [([user2_pk], 1), ([user2_pk], 1)], asset_ids=[tx_create.id]
        )
        tx_transfer_signed = tx_transfer.sign([user_sk])
        b.models.store_bulk_transactions([tx_transfer_signed])

        # check that used inputs are marked as spent
        for ffill in tx_create.to_inputs()[:2]:
            spent_tx = b.models.get_spending_transaction(ffill.fulfills.txid, ffill.fulfills.output)
            assert spent_tx == tx_transfer_signed.to_dict()

        # check if remaining transaction that was unspent is also perceived
        # spendable by Planetmint
        assert b.models.get_spending_transaction(tx_create.to_inputs()[2].fulfills.txid, 2) is None

    def test_get_spent_multiple_owners(self, b, user_sk, user_pk, alice):
        user2_sk, user2_pk = crypto.generate_key_pair()
        user3_sk, user3_pk = crypto.generate_key_pair()

        transactions = []
        for i in range(3):
            payload = multihash(marshal({"msg": random.random()}))
            tx = Create.generate([alice.public_key], [([user_pk, user2_pk], 1)], payload)
            tx = tx.sign([alice.private_key])
            transactions.append(tx)

        b.models.store_bulk_transactions(transactions)

        owned_inputs_user1 = b.models.get_outputs_filtered(user_pk)
        # check spents
        for input_tx in owned_inputs_user1:
            assert b.models.get_spending_transaction(input_tx.transaction_id, input_tx.index) is None

        # create a transaction
        tx = Transfer.generate(transactions[0].to_inputs(), [([user3_pk], 1)], asset_ids=[transactions[0].id])
        tx = tx.sign([user_sk, user2_sk])
        b.models.store_bulk_transactions([tx])

        # check that used inputs are marked as spent
        assert b.models.get_spending_transaction(transactions[0].id, 0) == tx.to_dict()
        # check that the other remain marked as unspent
        for unspent in transactions[1:]:
            assert b.models.get_spending_transaction(unspent.id, 0) is None

def test_get_outputs_filtered_only_unspent(b, alice):
    tx = Create.generate([alice.public_key], [([alice.public_key], 1), ([alice.public_key], 1)])
    tx = tx.sign([alice.private_key])
    b.models.store_bulk_transactions([tx])

    tx_transfer = Transfer.generate(tx.to_inputs([0]), [([alice.public_key], 1)], asset_ids=[tx.id])
    tx_transfer = tx_transfer.sign([alice.private_key])
    b.models.store_bulk_transactions([tx_transfer])

    outputs = b.models.get_outputs_filtered(alice.public_key, spent=False)
    assert len(outputs) == 2

def test_get_outputs_filtered_only_spent(b, alice):
    tx = Create.generate([alice.public_key], [([alice.public_key], 1), ([alice.public_key], 1)])
    tx = tx.sign([alice.private_key])
    b.models.store_bulk_transactions([tx])

    tx_transfer = Transfer.generate(tx.to_inputs([0]), [([alice.public_key], 1)], asset_ids=[tx.id])
    tx_transfer = tx_transfer.sign([alice.private_key])
    b.models.store_bulk_transactions([tx_transfer])

    outputs = b.models.get_outputs_filtered(alice.public_key, spent=True)
    assert len(outputs) == 1

def test_get_outputs_filtered(b, alice):
    tx = Create.generate([alice.public_key], [([alice.public_key], 1), ([alice.public_key], 1)])
    tx = tx.sign([alice.private_key])
    b.models.store_bulk_transactions([tx])

    tx_transfer = Transfer.generate(tx.to_inputs([0]), [([alice.public_key], 1)], asset_ids=[tx.id])
    tx_transfer = tx_transfer.sign([alice.private_key])
    b.models.store_bulk_transactions([tx_transfer])

    outputs = b.models.get_outputs_filtered(alice.public_key)
    assert len(outputs) == 3

def test_cant_spend_same_input_twice_in_tx(b, alice):
    """Recreate duplicated fulfillments bug
    https://github.com/planetmint/planetmint/issues/1099
    """
    from transactions.common.exceptions import DoubleSpend

    # create a divisible asset
    tx_create = Create.generate([alice.public_key], [([alice.public_key], 100)])
    tx_create_signed = tx_create.sign([alice.private_key])
    assert b.validate_transaction(tx_create_signed) == tx_create_signed
    b.models.store_bulk_transactions([tx_create_signed])

    # Create a transfer transaction with duplicated fulfillments
    dup_inputs = tx_create.to_inputs() + tx_create.to_inputs()
    tx_transfer = Transfer.generate(dup_inputs, [([alice.public_key], 200)], asset_ids=[tx_create.id])
    tx_transfer_signed = tx_transfer.sign([alice.private_key])
    with pytest.raises(DoubleSpend):
        b.validate_transaction(tx_transfer_signed)

def test_transaction_unicode(b, alice):
    import copy

    from transactions.common.utils import serialize

    # http://www.fileformat.info/info/unicode/char/1f37a/index.htm

    beer_python = [{"data": multihash(marshal({"beer": "\N{BEER MUG}"}))}]
    beer_json = {"data": multihash(marshal({"beer": "\N{BEER MUG}"}))}

    tx = (Create.generate([alice.public_key], [([alice.public_key], 100)], assets=beer_python)).sign(
        [alice.private_key]
    )

    tx_1 = copy.deepcopy(tx)
    b.models.store_bulk_transactions([tx])

    assert beer_json["data"] in serialize(tx_1.to_dict())


from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StructField
from pyspark.sql.types import StringType, IntegerType

spark = SparkSession.builder.master("local").appName('Read Something').getOrCreate()

schema_data = StructType([
                          StructField("Character", StringType(), False),
                          StructField("Movie", IntegerType(), False)
                        ])

data = spark.read.csv('/home/benito.ramos/Desktop/GIT/PySpark/inputs/data_movies',inferSchema=True, header=False )

data = spark.read.format("csv").options(inferSchema=True, header=False).schema(schema_data).load('inputs/data_movies')

data.show()

data.printSchema()

spark.stop()


#!/usr/bin/env python3
import setuptools, os

with open("README.md", "r") as fh:
    long_description = fh.read()

with open("./staphb_toolkit/lib/VERSION",'r') as versionFile:
    version = versionFile.readline().strip()

setuptools.setup(
    name="staphb_toolkit",
    version=version,
    author="Kelsey Florek",
    author_email="kelsey.florek@slh.wisc.edu",
    description="A ToolKit of commonly used Public Health Bioinformatics Tools",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/StaPH-B/staphb_toolkit",
    packages=setuptools.find_packages(),
    package_data={
        'staphb_toolkit' : ['lib/VERSION'],
        '' : ['../requirements.txt','../workflows.json','../staphb_toolkit/config/docker.config','../staphb_toolkit/config/singularity.config','../staphb-tk']
    },
    entry_points={
        "console_scripts": [
            'staphb-tk = staphb_toolkit.toolkit_main:main'
        ]
    },
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
        "Operating System :: OS Independent",
    ],
    install_requires=[
        "spython>=0.0.73",
        "psutil>=5.6.3",
        "docker>=4.1.0",
        "pexpect>=4.8",
        "pyfiglet>=0.8.post1",
        "rich>=12.4.4"],
    python_requires='>=3.7',
)


from django.core.management.base import BaseCommand
from rooms.models import HouseRule

NAME = "house rules"

class Command(BaseCommand):

    help = f"This command creates some {NAME}"

    def handle(self, *args, **options):
        house_rules = [
            "No smoking.",
            "No parties or events.",
            "No pets/Pets allowed.",
            "No unregistered guests.",
            "No food or drink in bedrooms.",
            "No loud noise after 11 PM.",
        ]

        for house_rule in house_rules:
            HouseRule.objects.create(name=house_rule)

        self.stdout.write(self.style.SUCCESS(f"Create {NAME}"))


from multiprocessing.dummy import Pool
import binascii
import hashlib

BASE_OFFSET = 0xbc050000
DUMP_LENGTH = 0x1545b8 * 4

data = bytearray(DUMP_LENGTH)

def process_line(line):
    if(line.startswith("bc")):
        groups = line.split(" ")
        offset = int(groups[0].rstrip(":"), 16) - BASE_OFFSET
        line_bytes = b''
        for x in groups[1:5]:
            assert len(x) == 8
            b = binascii.unhexlify(x)
            #data was printed as little endian 32-bit numbers, so we need to reverse the byte order
            line_bytes += b[::-1]
        data[offset:offset + 16] = line_bytes

with open("putty.log","r") as fin:
    with Pool(12) as p:
        p.map(process_line, fin.readlines())

with open("firmware.bin","wb") as fout:
    #we read 8 extra bytes
    fout.write(data[:-8])


from utils import save_read_classifier as srcl
from sklearn import metrics
import time
from utils import matrix
import thundersvm
import os
import warnings

def main():
    warnings.filterwarnings('ignore')

    # gpu 1 without torch, gpu 0 with torch (using nvidia-smi in terminal the gpus are inverted)
    selectGpu = 0
    # selectGpu = 1
    if selectGpu == 0:
        os.environ["CUDA_VISIBLE_DEVICES"] = '0'
    else:
        os.environ["CUDA_VISIBLE_DEVICES"] = '1'

    isUpperGland = True
    # change kernel type if you want
    if isUpperGland:
        clf = srcl.loadSVM("classifier/classifier_All_10x10_coordinate_Up")
        test_cov, test_labels = matrix.read_matrix('Dataset/test_up')
    else:
        clf = srcl.loadSVM("classifier/classifier_All_10x10_coordinate_Low")
        test_cov, test_labels = matrix.read_matrix('Dataset/test_low')

    test_labels = test_labels[0]

    time_start_predict = time.time()
    print(time_start_predict)
    predict = clf.predict(test_cov)
    print('predict done')

    f1score = metrics.f1_score(test_labels, predict)
    accuracy = metrics.accuracy_score(test_labels, predict)
    print(metrics.classification_report(test_labels, predict))
    time_end_predict = time.time()
    print(time_end_predict - time_start_predict)

    count, count1, count2, count3, count4 = 0, 0, 0, 0, 0
    for i in range(len(predict)):
        if test_labels[i] == predict[i]:
            count += 1
        if predict[i] == 1 and test_labels[i] == 1:
            count1 += 1
        elif predict[i] == 1 and test_labels[i] == 0:
            count3 += 1
        if test_labels[i] == 1:
            count2 += 1
        elif test_labels[i] == 0:
            count4 += 1

    print("Blocchi matched dalla predict: ", count)
    print("Blocchi totali: ", len(test_labels))
    print('Accuracy: ', accuracy)
    print('F1_score: ', f1score)
    print("Meibomio selezionato % (% di 1): ", count1/count2 * 100)
    print("Meibomio perso %: ", 100 - (count1/count2 * 100))
    print("Parte non corrispondente al Meibomio selezionata %: ", count3/count4 * 100)

    # change log destination file if you use another kernel type
    if isUpperGland:
        f = open("log/all_10x10_coordinate_up.txt", "w")
        f.write("all_10x10_coordinate_up \n")
    else:
        f = open("log/all_10x10_coordinate_low.txt", "w")
        f.write("all_10x10_coordinate_low \n")
    f.write("Blocchi matched dalla predict: " + str(count) + '\n')
    f.write("Blocchi totali: " + str(int(len(test_labels))) + '\n')
    f.write('Accuracy: ' + str(accuracy) + '\n')
    f.write('F1_score: ' + str(f1score) + '\n')
    f.write("Meibomio selezionato % (% di 1): " + str(count1/count2 * 100) + '\n')
    f.write("Meibomio perso %: " + str(100 - (count1/count2 * 100)) + '\n')
    f.write("Parte non corrispondente al Meibomio selezionata %: " + str(count3/count4 * 100) + '\n')
    f.write(metrics.classification_report(test_labels, predict) + '\n')
    f.close()

if __name__ == "__main__":
    main()


import numpy as np
from collections import defaultdict
from gym.envs.toy_text.nchain import NChainEnv
from rl.policy.discrete_policy import RandomPolicy, GreedyPolicy
#
# env = BlackjackEnv()
env = NChainEnv()
#
print("Action Space : ", env.action_space)
print("Observation Space : ", env.observation_space)
#
def mc_control_importance_sampling(env, num_episodes, behavior_policy, discount_factor=1.0):
    Q = defaultdict(lambda : np.zeros(env.action_space.n))
    C = defaultdict(lambda : np.zeros(env.action_space.n))
    target_policy = GreedyPolicy(env, Q)

    for i_episodes in range(1, num_episodes+1):
        if i_episodes % 1000 == 0:
            print("\rEpisode {}/{}.".format(i_episodes, num_episodes), end="")
            for key, value in Q.items():
                print(" ", key, "    ", value)
            # sys.stdout.flush()
        episode = []
        state = env.reset()
        for t in range(100):
            action = behavior_policy.predict(state)
            next_state, reward, done, _ = env.step(action)
            episode.append((state, action, reward))
            if done:
                break
            state = next_state
        # Sum of discounted returns
        G = 0.0
        #
        W = 1.0
        # For each step in episode, backwards
        for t in reversed(np.arange(len(episode))):
            state, action, reward = episode[t]
            G = discount_factor * G + reward
            C[state][action] += W
            #
            alpha = W / C[state][action]
            Q[state][action] += alpha * (G - Q[state][action])
            target_policy.update(Q)
            if action != target_policy.predict(state):
                break
            W = W * 1. / behavior_policy.action_probs[action]

    return Q, target_policy

#
random_policy = RandomPolicy(env)
Q, policy = mc_control_importance_sampling(env, num_episodes=500000, behavior_policy=random_policy,discount_factor=1.0)