import datasets
from unidecode import unidecode

banned_word = 'wikipedia' # word to exclude from URL
out_dir = '' # output directory
dataset_dir = '' # dataset directory

dataset = datasets.load_from_disk(dataset_dir)
train_data, test_data = dataset['train'], dataset['test']

import requests
from bs4 import BeautifulSoup
import re
import urllib.request as urllib2
import nltk

def get_urls(query, num_results):

    search = query.replace(' ', '+')
    url = f"https://www.google.com/search?q={search}&num={num_results}"

    requests_results = requests.get(url)
    soup_link = BeautifulSoup(requests_results.content, "html.parser")
    links = soup_link.find_all("a")

    ret = []

    for link in links:
        link_href = link.get('href')
        if "url?q=" in link_href and not "webcache" in link_href:
            title = link.find_all('h3')
            if len(title) > 0:
                ret.append(link.get('href').split("?q=")[1].split("&sa=U")[0])

    ret = [link for link in ret if banned_word not in link]
    return ret

def get_sentences_web(num_results, ret):

    sentences = []
    num_seen = 0
    for url in ret:

        if num_seen == num_results:
            break

        try:

            hdr = {'User-Agent': 'Mozilla/5.0'}
            req = urllib2.Request(url, headers = hdr)
            page = urllib2.urlopen(req, timeout = 3)

            if 'pdf' in page.headers['Content-Type'].lower():
                continue

            soup = BeautifulSoup(page, "html.parser", from_encoding="iso-8859-1")

            paragraphs_web = soup.findAll("p")
            paragraphs_web = [p.text for p in paragraphs_web]

            if len(paragraphs_web) > 200 or len(paragraphs_web) == 0:
                continue

            old_sentence_length = len(sentences)
            for p in paragraphs_web:
                curr_sentences = nltk.sent_tokenize(p)
                for sentence in curr_sentences:
                    if len(sentence) < 30 or "©" in sentence or "license" in sentence.lower() or "cookies" in sentence.lower() or "http" in sentence.lower():
                        continue
                    sentences.append(sentence)

            if len(sentences) != old_sentence_length:
                num_seen += 1

        except Exception as e:
                _ = 1

    return sentences

def clean_text(text):

    if "displaystyle" in text:
        return ""

    text = re.sub(r'[^A-Za-z0-9.,?!:;\- ]+', ' ', text)
    text = text.replace('displaystyle', ' ').replace('\n', '')
    return re.sub(' +', ' ', text)

def scrape_page(page):

    section_text = []
    ground_truth_section = []

    soup = BeautifulSoup(page.summary, "html.parser")
    cleaned_ps = [clean_text(p.text) for p in soup.findAll("p")]
    cleaned_ps = [p for p in cleaned_ps if len(p) > 0]

    for p in cleaned_ps:
        section_text.extend(nltk.sent_tokenize(p))

    for section in page.sections:

        if section.title.lower() in {'see also', 'references', 'further reading', 'external links'}:
            break

        soup = BeautifulSoup(section.text, "html.parser")
        cleaned_ps = [clean_text(p.text) for p in soup.findAll("p")]
        cleaned_ps = [p for p in cleaned_ps if len(p) > 0]

        if len(cleaned_ps) == 0:
            continue

        for p in cleaned_ps:
            section_text.extend(nltk.sent_tokenize(p))

    return section_text

import pandas as pd
import numpy as np
import re
from bs4 import BeautifulSoup
import tqdm
import pickle
import time
import random

def clean_text(text):

    if "displaystyle" in text:
        return ""

    text = re.sub(r'[^A-Za-z0-9.,?!:; ]+', ' ', text)
    text = text.replace('displaystyle', ' ').replace('\n', '')
    return re.sub(' +', ' ', text)

def shuffle_sentences(information):
    all_sentences = information
    random.shuffle(all_sentences)
    return all_sentences

columns = ['aspect', 'title', 'web_sentences_with_desc', 'web_sentences_no_desc', 'output', 'output_aug']
new_dataset_train = {c: [] for c in columns}
new_dataset_test = {c: [] for c in columns}

num_train, num_test = len(train_data['output']), len(test_data['output'])

def get_data(data, idx):
    return data['title'][idx], data['output'][idx], data['output_aug'][idx]

for idx in tqdm.tqdm(range(num_train)):

    title, output, output_aug = get_data(train_data, idx)
    output, output_aug = output.split('\n\n')[0], output_aug.split('\n\n')[0]

    all_urls = []
    seen_urls = set()
    for sentence in nltk.sent_tokenize(output):
        curr_urls = get_urls(f"{title} {sentence}", 10)
        curr_urls_ = []
        for url in curr_urls:
            if url in seen_urls:
                continue
            seen_urls.add(url)
            curr_urls_.append(url)
        all_urls.append(curr_urls_)

    web_sentences = []
    for urls in all_urls:
       web_sentences.extend(get_sentences_web(3, urls))
    web_sentences = [unidecode(clean_text(sent)) for sent in web_sentences]

    general_sentences = shuffle_sentences(web_sentences)
    general_sentences_leak = shuffle_sentences(web_sentences + nltk.sent_tokenize(output))

    new_dataset_train['title'].append(title)
    new_dataset_train['output'].append(output)
    new_dataset_train['output_aug'].append(output_aug)
    new_dataset_train['web_sentences_with_desc'].append(general_sentences_leak)
    new_dataset_train['web_sentences_no_desc'].append(general_sentences)

    time.sleep(10)

for idx in tqdm.tqdm(range(num_test)):

    title, output, output_aug = get_data(test_data, idx)
    output, output_aug = output.split('\n\n')[0], output_aug.split('\n\n')[0]

    all_urls = []
    seen_urls = set()
    for sentence in nltk.sent_tokenize(output):
        curr_urls = get_urls(f"{title} {sentence}", 10)
        curr_urls_ = []
        for url in curr_urls:
            if url in seen_urls:
                continue
            seen_urls.add(url)
            curr_urls_.append(url)
        all_urls.append(curr_urls_)

    web_sentences = []
    for urls in all_urls:
       web_sentences.extend(get_sentences_web(3, urls))

    web_sentences = [unidecode(clean_text(sent)) for sent in web_sentences]

    general_sentences = shuffle_sentences(web_sentences)
    general_sentences_leak = shuffle_sentences(web_sentences + nltk.sent_tokenize(output_aug))

    new_dataset_test['title'].append(title)
    new_dataset_test['output'].append(output)
    new_dataset_test['output_aug'].append(output_aug)
    new_dataset_test['web_sentences_with_desc'].append(general_sentences_leak)
    new_dataset_test['web_sentences_no_desc'].append(general_sentences)

    time.sleep(10)

new_train, new_test = datasets.Dataset.from_dict(new_dataset_train), datasets.Dataset.from_dict(new_dataset_test)
new_ds = datasets.DatasetDict({'train': new_train, 'test': new_test})
new_ds.save_to_disk(out_dir)


from random import choice
from string import letters

from django.test import TestCase

from image_converter.utils.filename import get_filename_without_extension

__author__ = 'Dominic Dumrauf'

class FilenameTestCase(TestCase):
    """
    Tests the functions in the 'utils.filename' module.
    """
    def test_empty_filename(self):
        """
        Tests an empty file name.
        """
        # Given
        expected_name = 'file'
        # When
        name = get_filename_without_extension('')
        # Then
        self.assertEqual(expected_name,
                         name)

    def test_filename_without_extension(self):
        """
        Tests random file names that lack an extension.
        """
        for _ in xrange(100):
            # Given
            expected_filename = ''.join([choice(letters) for _ in xrange(8)])
            # When
            name = get_filename_without_extension(expected_filename)
            # Then
            self.assertEqual(expected_filename,
                             name)

    def test_multiple_extensions(self):
        """
        Tests random file names with multiple extensions.
        """
        for _ in xrange(100):
            # Given
            filename = ''.join([choice(letters) for _ in xrange(8)])
            for _ in xrange(10):
                filename += '.'.join([choice(letters) for _ in xrange(3)])
            # When
            name = get_filename_without_extension(filename)
            # Then
            expected_name, extension = filename.rsplit('.', 1)
            self.assertEqual(expected_name,
                             name)

    def test_standard_filename(self):
        """
        Tests random file names which are made up of a name without any dots and a single extension; name and
        extension are separated by a dot.
        """
        for _ in xrange(100):
            # Given
            expected_name = ''.join([choice(letters) for _ in xrange(8)])
            extension = ''.join([choice(letters) for _ in xrange(3)])
            filename = '{0}.{1}'.format(expected_name,
                                        extension)
            # When
            name = get_filename_without_extension(filename)
            # Then
            self.assertEqual(expected_name,
                             name)


from functools import partial
from collections import defaultdict
from prestring.go import (
    GoModule,
    goname,
    titleize,
)
from .langhelpers import tostring

class FileWriter:
    def __init__(self, writer):
        self.writer = writer

    def write(self, file, m):
        m.stmt(str(file.package))
        m.sep()

        def import_(fullname, as_=None, im=None, file=None):
            im(fullname, as_=as_)
            return file.import_(fullname, as_=as_)

        with m.import_group() as im:
            m.import_ = partial(import_, im=im, file=file)
            for ipackage in file.imported.values():
                im(ipackage.fullname, as_=ipackage.as_)

        for newtype in file.newtypes.values():
            self.writer.write_newtype(newtype, file, m=m)
            m.sep()
        for enum in file.enums.values():
            self.writer.write_enum(enum, file, m=m)
            m.sep()
        for interface in file.interfaces.values():
            self.writer.write_interface(interface, file, m=m)
            m.sep()
        for struct in file.structs.values():
            self.writer.write_struct(struct, file, m=m)
            m.sep()
        for fn in file.globals:
            fn(m)
            m.sep()
        for f in file.functions.values():
            self.writer.write_function(f, file, m=m)
            m.sep()
        # im.clear_ifempty()
        return m

class FuncWriter:
    def __init__(self, writer):
        self.writer = writer

    def write(self, f, file, m):
        if f.comment is not None or (f.name and f.name[0].isupper()):
            m.stmt('// {} : {}'.format(f.name, f.comment or ""))
        m.append(str(f))
        m.stmt(" {")
        with m.scope():
            f.body(m)
        m.stmt("}")
        return m

def _writecomment(m, prefix, comment):
    m.append(prefix)
    if not comment:
        m.stmt("")
        return
    lines = comment.strip().split("\n")
    m.stmt(lines[0])
    for line in lines[1:]:
        m.stmt("// {}", line)

class StructWriter:
    def __init__(self, writer):
        self.writer = writer

    def write(self, struct, file, m):
        if struct.comment is not None or (struct.name and struct.name[0].isupper()):
            _writecomment(m, '// {} : '.format(struct.name), struct.comment or "")
        with m.type_(struct.name, "struct"):
            for name, typ, tag, comment, embeded in struct.fields.values():
                if comment is not None and "\n" in comment:
                    _writecomment(m, '// ', comment)
                if embeded:
                    m.append(typ.typename(file))
                else:
                    m.append("{} {}".format(name, typ.typename(file)))
                if tag is not None:
                    m.append(" {}".format(tag))
                if comment is not None and "\n" not in comment:
                    m.append("  // {}".format(comment))
                m.stmt("")
        return m

class InterfaceWriter:
    def __init__(self, writer):
        self.writer = writer

    def write(self, interface, file, m):
        if interface.comment is not None or (interface.name and interface.name[0].isupper()):
            _writecomment(m, '// {} : '.format(interface.name), interface.comment or "")
        with m.type_(interface.name, "interface"):
            for name, f, tag, comment, embeded in interface.methods.values():
                if comment is not None and "\n" in comment:
                    _writecomment(m, '// ', comment)
                if embeded:
                    m.append(f.typename(file))
                else:
                    m.append(f.withtype(file, prefix=""))
                if tag is not None:
                    m.append(" {}".format(tag))
                if comment is not None and "\n" not in comment:
                    m.append("  // {}".format(comment))
                m.stmt("")
        return m

class NewtypeWriter:
    def __init__(self, writer):
        self.writer = writer

    def write(self, newtype, file, m):
        if newtype.comment is not None or (newtype.name and newtype.name[0].isupper()):
            _writecomment(m, '// {} : '.format(newtype.name), newtype.comment or "")
        m.type_alias(newtype.name, newtype.type.typename(file))
        return m

class EnumWriter:
    def __init__(self, writer):
        self.writer = writer

    @property
    def repository(self):
        return self.writer.repository

    def write(self, enum, file, m):
        self.write_definition(enum, file, m)
        self.write_string_method(enum, file, m)
        self.write_parse_method(enum, file, m)
        return m

    def write_definition(self, enum, file, m):
        if enum.comment is not None or (enum.name and enum.name[0].isupper()):
            m.stmt('// {} : {}'.format(enum.name, enum.comment or ""))
        m.stmt("type {} {}".format(enum.name, enum.type.typename(file)))
        m.sep()

        with m.const_group() as cm:
            for name, _, value, comment in enum.members.values():
                name = enum.varname(name)
                cm('// {} : {}'.format(name, comment or ""))
                cm('{} = {}({})'.format(name, enum.name, tostring(value)))

        return m

    def write_string_method(self, enum, file, m):
        r = self.repository
        fmt = m.import_("fmt")

        @file.method(
            "String", enum, returns=r.string, comment="stringer implementation", nostore=True
        )
        def string(m):
            s = enum.shortname
            with m.switch(s) as sw:
                for name, _, value, _ in enum.members.values():
                    with sw.case(enum.varname(name)):
                        sw.return_(tostring(name))
                with sw.default():
                    sw.stmt(
                        'panic({})'.format(
                            fmt.Sprintf(
                                "unexpected {} %s, in string()".format(enum.name),
                                _noencoded("string({})".format(s)),
                            )
                        )
                    )
            return m

        self.writer.write_function(string, file, m)

    def write_parse_method(self, enum, file, m):
        fmt = m.import_("fmt")

        @file.func(
            goname("Parse" + titleize(enum.name)),
            args=enum.type(enum.shortname),
            returns=enum,
            comment="parse",
            nostore=True
        )
        def parse(m):
            s = enum.shortname
            with m.switch(s) as sw:
                for name, _, value, _ in enum.members.values():
                    with sw.case(tostring(value)):
                        sw.return_(enum.varname(name))
                with sw.default():
                    sw.stmt(
                        'panic({})'.format(
                            fmt.Sprintf(
                                "unexpected {} %v, in parse()".format(enum.name), _noencoded(s)
                            )
                        )
                    )
            return m

        self.writer.write_function(parse, file, m)

class Writer:
    file_writer_factory = FileWriter
    enum_writer_factory = EnumWriter
    newtype_writer_factory = NewtypeWriter
    struct_writer_factory = StructWriter
    interface_writer_factory = InterfaceWriter
    func_writer_factory = FuncWriter

    def __init__(self, repository, module_factory=GoModule):
        self.repository = repository
        self.modules = defaultdict(module_factory)  # module is prestring's module
        self.file_writer = self.file_writer_factory(self)
        self.enum_writer = self.enum_writer_factory(self)
        self.newtype_writer = self.newtype_writer_factory(self)
        self.struct_writer = self.struct_writer_factory(self)
        self.interface_writer = self.interface_writer_factory(self)
        self.func_writer = self.func_writer_factory(self)

    def write(self, file, m=None):
        m = m or self.modules[file.fullname]
        return self.write_file(file, m=m)

    def write_file(self, file, m):
        return self.file_writer.write(file, m)

    def write_enum(self, enum, file, m):
        return self.enum_writer.write(enum, file, m)

    def write_newtype(self, newtype, file, m):
        return self.newtype_writer.write(newtype, file, m)

    def write_struct(self, struct, file, m):
        return self.struct_writer.write(struct, file, m)

    def write_interface(self, interface, file, m):
        return self.interface_writer.write(interface, file, m)

    def write_function(self, f, file, m):
        return self.func_writer.write(f, file, m)

class _noencoded(object):
    def __init__(self, v):
        self.v = v

    def __str__(self):
        return self.v


import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
from scipy.optimize import curve_fit

#Define a function that will create line to distinguish SFGs from QGs
def seperationline(x):
    m=(-2.6- -0.3)/(9.1-11.7)
    y=m*(x-11.7)-0.3
    return y

#Importing Data from SQL search
data = np.genfromtxt('MassVSFR.csv', delimiter=',')
data1= np.genfromtxt('ha_flux.csv', delimiter=',')

mangaid=data[:,0]
mangaid=np.delete(mangaid,0) #Deletes title of column in spreadsheet to avoid Nan value

np.savetxt('mangaid', mangaid)

mass=data[:,2]
mass=np.delete(mass,0)
logmass=np.log10(mass)

SFR=data[:,1]
SFR=np.delete(SFR,0)
logSFR=np.log10(SFR)

ha_flux=data1[:,1]
ha_flux=np.delete(ha_flux,0)

#Creating x points to allow the plotting of the distinguishing line
x1=np.linspace(7,13,9718)
y1=seperationline(x1)

plt.title('SFR vs Mass of Galaxies in MaNGA')
plt.xlabel(r'$log(M/M_{\odot})$')
plt.ylabel(r'$log(SFR/M_{\odot})$')
plt.scatter(logmass,logSFR, c=np.log10(ha_flux), vmin=-2, vmax=-0.8, cmap='viridis', alpha=0.1)
plt.hist2d(logmass,logSFR, cmap='viridis', bins=(np.linspace(7,13,51),np.linspace(-5.5,1,51)))
plt.plot(x1,y1,label='Separation Line')
plt.legend(loc='bottom left')
plt.colorbar().set_label('Ha Flux')
plt.savefig('Distinction Line.png')
plt.show()

#Putting Manga ID and logSFR/logmass in one array so galaxies can be tracked
logSFR_ID=np.column_stack((np.transpose(mangaid),np.transpose(logSFR),np.transpose(logmass)))

# Removing all rows with a NaN and infinity elments in logSFR and logmass to avoid problems with np.where

logSFR_ID=logSFR_ID[~np.isnan(logSFR_ID).any(axis=1)]
logSFR_ID=logSFR_ID[~np.isinf(logSFR_ID).any(axis=1)]

np.savetxt('Quiescent Galaxies From Distinction Line',logSFR_ID)

# Using np.where to find galaxy information around the distinction line
QG=logSFR_ID[~((logSFR_ID[:,1]>y1))]

SFG=logSFR_ID[~((logSFR_ID[:,1]<y1))]

np.savetxt('Quiescent Galaxies From Distinction Line',QG)
np.savetxt('Star-Forming Galaxies From Distinction Line',SFG)

#Checking if Classification above and below line actually worked
plt.scatter(SFG[:,2],SFG[:,1],marker='.')
plt.scatter(QG[:,2],QG[:,1],marker='s')
plt.xlim(6,14)
plt.plot(x1,y1)
plt.show()


"""In this challenge I have to get the 20 highest rated directors from a data set based on their
average movie IMDB ratings. """

import copy
import csv
import collections

YEAR = 2023
MINIMUM_YEAR = 1960
path = "movie_metadata.csv"
file = open(path, newline='', encoding='utf-8')
reader = csv.reader(file)
header = next(reader)
his_movie = collections.namedtuple('his_movie', ['Title', 'Year', 'Score'])

def get_important_data(the_file):
    """This function reads the file line by line and give us a dictionary which contains the directors with
    their works"""
    directors = collections.defaultdict(list)
    for line in the_file:
        try:
            director_name = line[1]  # Director name
            movie_title = line[11].replace('\xa0', '')  # Movie title
            title_year = int(line[23])  # Title year
            imdb_score = float(line[25])  # Imdb score
        except ValueError:
            continue
        director_movies = his_movie(movie_title, title_year, imdb_score)
        directors[director_name].append(director_movies)
    return directors

directors_dict = get_important_data(reader)

def minimum_year():
    """This function takes the dictionary as an input and give us all of the movies that have been made
    since the MINIMUM YEAR global variable"""
    lst_by_year = []
    for director, movie in directors_dict.items():
        if directors_dict[director][0].Year > MINIMUM_YEAR:
            lst_by_year.append(directors_dict[director][0])
    return lst_by_year

def most_famous_directors(top_i):
    """This function take the dictionary as an input and return the most common i directors
    with how many movies he/she did"""
    count = collections.Counter()
    for director, his_movies in directors_dict.items():
        count[director] += len(his_movies)
    return count.most_common(top_i)

def sort_movies_by_year(lst):  # Counting Sort Algorithm
    """Counting Sort Algorithm for the years, This Function takes the list that we made
    in the minimum_year() function and returns a sorted list by years, with the movie title and year
    and score"""
    # lst =elements: his_movie(Title='The Mongol King', Year=2005, Score=7.8) from  minimum_year()
    size = len(lst)
    output_lst = [0]*size  # movies lst size
    place = [0]*(YEAR - MINIMUM_YEAR)   # lst that has YEAR of elements
    for i in range(0, size):
        place[lst[i][1]-MINIMUM_YEAR] += 1
    count = copy.copy(place)

    for j in range(len(place)):
        place[j] += place[j-1]
    for h in range(len(place)):
        place[h] -= 1

    for b in range(len(place)-1, -1, -1):
        if b > 0:
            while place[b]-place[b-1] > 0:
                output_lst[place[b]] = (lst[place[b]][0], b + MINIMUM_YEAR, lst[place[b]][2])
                place[b] -= 1
        else:
            while place[b]-0 >= 0:
                output_lst[place[b]] = (lst[place[b]][0], b + MINIMUM_YEAR, lst[place[b]][2])
                place[b] -= 1
    return output_lst

def lst_all_movies_with_the_score(j):
    """This function takes a float between 0.0 and 10.0 and returns the movies with the same
    score in a list with the director name and the movie's title and year"""
    lst = []
    for director, s_movie in directors_dict.items():
        if s_movie[0].Score == j:
            lst.append((director, directors_dict[director][0].Title, directors_dict[director][0].Year))
    return lst

def top_movies(num):
    """this function takes integer as an N input and returns the top N movies from the dictionary"""
    sorted_lst = []
    for i in range(100, -1, -1):
        wanted_score = i/10
        the_movie_lst = lst_all_movies_with_the_score(wanted_score)
        if len(the_movie_lst) > 0:
            for j in range(len(the_movie_lst)):
                sorted_lst.append((the_movie_lst[j][1], the_movie_lst[j][2]))  # Give just the title and year
    return sorted_lst[0:num]

print(top_movies(10))

Day 004-006 collections/Exercises/Highest Rated Movie Directors.py


import unittest

from .utils import EmptyEntity
from gacha.logging import EmptyLog
from gacha.providers import InMemoryEntityProvider

class TestInMemoryEntityProvider(unittest.TestCase):
    def test_get_entity_should_return_default_value_when_collection_does_not_exist(self):
        default_value = 123
        subject = InMemoryEntityProvider([], EmptyLog())
        result = subject.get_entity("collection", 1, default_value)
        self.assertEqual(result, default_value)

    def test_get_entity_should_return_default_value_when_entity_does_not_exist(self):
        entity = EmptyEntity(1)
        default_value = 123
        subject = InMemoryEntityProvider([("collection", entity)], EmptyLog())
        result = subject.get_entity("collection", 2, default_value)
        self.assertEqual(result, default_value)

    def test_get_entity_should_return_entity_when_exists(self):
        entity = EmptyEntity(1)
        subject = InMemoryEntityProvider([("collection", entity)], EmptyLog())
        result = subject.get_entity("collection", 1, None)
        self.assertEqual(result, entity)

    def test_get_collection_should_return_empty_collection_when_collection_does_not_exist(self):
        subject = InMemoryEntityProvider([], EmptyLog())
        result = subject.get_collection("collection")
        self.assertIsNotNone(result)
        result_list = list(result)
        self.assertListEqual(result_list, [])

    def test_get_collection_should_return_collection_when_exists(self):
        entity1 = EmptyEntity(1)
        entity2 = EmptyEntity(2)
        entity3 = EmptyEntity(3)
        subject = InMemoryEntityProvider([
            ("c1", entity1),
            ("c2", entity2),
            ("c1", entity3)
        ], EmptyLog())
        result = subject.get_collection("c1")
        self.assertIsNotNone(result)
        result_dict = dict([(item.id, item) for item in result])
        self.assertDictEqual(result_dict, {
            entity1.id: entity1,
            entity3.id: entity3
        })

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


import pygame
from pieces.peca import Peca
from inteligencia import get_ai_move

whiteGato = pygame.image.load('pieces_assets/wR-v2.png')
#whiteGato = pygame.image.load('pieces_assets/wR.png')
whiteGato = pygame.transform.scale(whiteGato, (75, 75))
#blackRato = pygame.image.load('pieces_assets/bp.png')
blackRato = pygame.image.load('pieces_assets/bp-v2.png')
blackRato = pygame.transform.scale(blackRato, (75, 75))
bloco_escuro = pygame.image.load('pieces_assets/marrom_escuro.png')
bloco_claro = pygame.image.load('pieces_assets/marrom_claro.png')
bloco_escuro = pygame.transform.scale(bloco_escuro, (75, 75))
bloco_claro = pygame.transform.scale(bloco_claro, (75, 75))

previsao = pygame.image.load('pieces_assets/previsao.png')
previsao = pygame.transform.scale(previsao, (75, 75))

screen = None
pygame.font.init()
font = pygame.font.SysFont('Arial', 22)

def initialize():
    """
    Função que inicializa o Pygame e define a janela do jogo.

    Inicializa o módulo Pygame e define o título da janela do jogo. Em seguida,
    cria uma janela com as dimensões de 600x650 pixels e preenche com a cor preta.

    Parâmetros:
    Nenhum.

    Retorno:
    Nenhum.
    """
    global screen
    pygame.init()
    pygame.display.set_caption('Jogo diferenciado de Igor e Luisa')
    screen = pygame.display.set_mode((600, 650))
    screen.fill((0, 0, 0))

def draw_background(board):
    """
    Desenha o fundo do tabuleiro e as peças nele contidas.

    Parâmetros:
    board (list): uma lista que representa o estado atual do tabuleiro.

    Retorno:
    None
    """
    block_x = 0
    for i in range(4):
        block_y = 0
        for j in range(4):
            screen.blit(bloco_claro, (block_x, block_y))
            screen.blit(bloco_escuro, (block_x + 75, block_y))
            screen.blit(bloco_claro, (block_x + 75, block_y + 75))
            screen.blit(bloco_escuro, (block_x, block_y + 75))
            block_y += 150
        block_x += 150
    step_x = 0
    step_y = pygame.display.get_surface().get_size()[0] - 75
    for i in range(8):
        for j in range(8):
            if isinstance(board[i][j], Peca):
                obj = globals()[f'{board[i][j].color}{board[i][j].type}']
                screen.blit(obj, (step_x, step_y))
            step_x += 75
        step_x = 0
        step_y -= 75
    pygame.display.update()

def draw_text(text):
    """
    Desenha um texto na parte inferior da tela.

    Parâmetros:
    text (str): o texto a ser exibido na tela.

    Retorno:
    None
    """
    s = pygame.Surface((400, 50))
    s.fill((0, 0, 0))
    screen.blit(s, (100, 600))
    text_surface = font.render(text, True, (100, 237, 237))
    x = 230
    screen.blit(text_surface, (x, 600))
    pygame.display.update()

def start(board):
    """
    Inicia o jogo com um tabuleiro e executa o loop principal do jogo.

    Parâmetros:
    board (Tabuleiro): o tabuleiro do jogo.

    Retorno:
    None
    """
    global screen
    possible_piece_moves = []
    running = True
    visible_moves = False
    dimensions = pygame.display.get_surface().get_size()
    game_over = False
    piece = None
    if board.game_mode == 1 and board.ai:
        get_ai_move(board)
        draw_background(board)
    while running:
        if game_over:
            draw_text(game_over_txt)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.MOUSEBUTTONDOWN and not game_over:
                x = 7 - pygame.mouse.get_pos()[1] // 75
                y = pygame.mouse.get_pos()[0] // 75
                if isinstance(board[x][y], Peca) and (board.get_player_color() == board[x][y].color or not board.ai) and (x, y) not in possible_piece_moves:
                    piece = board[x][y]
                    moves = piece.filter_moves(piece.get_moves(board), board)
                    move_positions = []
                    possible_piece_moves = []
                    for move in moves:
                        move_positions.append(
                            (dimensions[0] - (8 - move[1]) * 75, dimensions[1] - move[0] * 75 - 125))
                        move_x = 7 - move_positions[-1][1] // 75
                        move_y = move_positions[-1][0] // 75
                        possible_piece_moves.append((move_x, move_y))
                    if visible_moves:
                        draw_background(board)
                        visible_moves = False
                    for move in move_positions:
                        visible_moves = True
                        screen.blit(previsao, (move[0], move[1]))
                        pygame.display.update()
                else:
                    clicked_move = (x, y)
                    try:
                        if clicked_move in possible_piece_moves:
                            board.make_move(piece, x, y)
                            possible_piece_moves.clear()
                            draw_background(board)
                            if board.ai:
                                get_ai_move(board)
                                draw_background(board)

                        if board.white_won():
                            game_over = True
                            game_over_txt = 'Gato ganhou!'
                        elif board.black_won():
                            game_over = True
                            game_over_txt = 'Rato ganhou!'
                    except UnboundLocalError:
                        pass


######################## BEGIN LICENSE BLOCK ########################
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301  USA
######################### END LICENSE BLOCK #########################

from .universaldetector import UniversalDetector
from .enums import InputState
from .version import __version__, VERSION

__all__ = ['UniversalDetector', 'detect', 'detect_all', '__version__', 'VERSION']

def detect(byte_str):
    """
    Detect the encoding of the given byte string.

    :param byte_str:     The byte sequence to examine.
    :type byte_str:      ``bytes`` or ``bytearray``
    """
    if not isinstance(byte_str, bytearray):
        if not isinstance(byte_str, bytes):
            raise TypeError('Expected object of type bytes or bytearray, got: '
                            '{}'.format(type(byte_str)))
        else:
            byte_str = bytearray(byte_str)
    detector = UniversalDetector()
    detector.feed(byte_str)
    return detector.close()

def detect_all(byte_str):
    """
    Detect all the possible encodings of the given byte string.

    :param byte_str:     The byte sequence to examine.
    :type byte_str:      ``bytes`` or ``bytearray``
    """
    if not isinstance(byte_str, bytearray):
        if not isinstance(byte_str, bytes):
            raise TypeError('Expected object of type bytes or bytearray, got: '
                            '{}'.format(type(byte_str)))
        else:
            byte_str = bytearray(byte_str)

    detector = UniversalDetector()
    detector.feed(byte_str)
    detector.close()

    if detector._input_state == InputState.HIGH_BYTE:
        results = []
        for prober in detector._charset_probers:
            if prober.get_confidence() > detector.MINIMUM_THRESHOLD:
                charset_name = prober.charset_name
                lower_charset_name = prober.charset_name.lower()
                # Use Windows encoding name instead of ISO-8859 if we saw any
                # extra Windows-specific bytes
                if lower_charset_name.startswith('iso-8859'):
                    if detector._has_win_bytes:
                        charset_name = detector.ISO_WIN_MAP.get(lower_charset_name,
                                                            charset_name)
                results.append({
                    'encoding': charset_name,
                    'confidence': prober.get_confidence(),
                    'language': prober.language,
                })
        if len(results) > 0:
            return sorted(results, key=lambda result: -result['confidence'])

    return [detector.result]


import json
import os
import sys
from urllib import parse

from minerals.models import Mineral

def seed(json_filename):
    if not os.path.isfile(json_filename):
        print('{} not found! Please check your file path.'
              .format(json_filename))
    attributes = [
        'name',
        'image filename',
        'image caption',
        'category',
        'formula',
        'strunz classification',
        'crystal system',
        'unit cell',
        'color',
        'crystal symmetry',
        'cleavage',
        'mohs scale hardness',
        'luster',
        'streak',
        'diaphaneity',
        'optical properties',
        'refractive index',
        'crystal habit',
        'specific gravity',
    ]
    with open(json_filename) as json_file:
        mineral_data = json.load(json_file)
        for mineral in mineral_data:
            new_mineral = {}
            for item in attributes:
                if item in mineral.keys():
                    model_item = item.replace(' ', '_')
                    if model_item == 'image_filename':
                        mineral[item] = parse.unquote(mineral[item])
                    new_mineral[model_item] = mineral[item]
            Mineral.objects.create(**new_mineral)

if __name__ == '__main__':
    seed(sys.argv[1])


import openai
import numpy as np
import torch
import json
from icecream import ic
import pandas as pd
import os
# from dotenv import load_dotenv
import openai
from evaluators.automatic_eval import Automatic_Evaluator
from tqdm import tqdm
import glob

MAX_STEPS = 22
LMTYPE_TO_LMID = {
    "gpt3": "gpt3",
    "gpt-j-6B": "EleutherAI/gpt-j-6B",
    "gpt2-1.5B": "gpt2",
    "gpt2": "gpt2-large",
    "gpt2-xl": "gpt2-xl",
    "gpt_neo": "EleutherAI/gpt-neo-1.3B",
    "t5": "t5-3b",
    "bert": "bert-large-uncased",
    "roberta": "roberta-large",
    "bart": "facebook/bart-large"
}

t2i_template_dict = {
    "t2i-0": "What do I need to draw in the picture to describe the above text?",
    "t2i-1": "What do you see in the figure?",
    "t2i-2": "Let's think about what we need to visualize to present the above idea.",
    "t2i-3": "Describe what the picture corresponding to the text should have.",
    "t2i-4": "What do you usually draw?",
    "t2i-5": "Describe something irrelevant to the above text.",
}

i2t_template_dict = {
    "i2t-0": "Rewrite the textual instruction using the knowledge from visualized instruction pair-wisely. Please keep the same number of steps as the provided procedure.",
    "i2t-1": "Revise each step according to the visual imagination. Please keep the same number of steps as the provided procedure.",
    "i2t-2": "Let's revise the procedure using the captions. Please keep the same number of steps as the provided procedure.",
    "i2t-3": "Based on the visual caption, can you revise the step-by-step procedure according to the paired captions? Please keep the same number of steps as the provided procedure.",
    "i2t-4": "Provide an interesting procedure to be irrelevant with the captions. Please keep the same number of steps as the provided procedure.",
    "i2t-5": "Give the textual instruction that disobey the visual captions. Please keep the same number of steps as the provided procedure."
}

class LM_Engine(object):
    def __init__(self, model, planning_lm_id, device):
        self.model = model
        self.planning_lm_id = planning_lm_id

    def generate(self, prompt, sampling_params):
        response = openai.Completion.create(engine=self.planning_lm_id, prompt=prompt, **sampling_params)
        generated_samples = [response['choices'][i]['text'] for i in range(sampling_params['n'])]
        # calculate mean log prob across tokens
        mean_log_probs = [np.mean(response['choices'][i]['logprobs']['token_logprobs']) for i in range(sampling_params['n'])]
        return generated_samples, mean_log_probs

class LLM_Reasoning(object):
    def __init__(self, opt):
        super().__init__()
        self.opt = opt
        self.completion = openai.Completion()
        self.language_model_type = opt.language_model_type
        self.open_loop = opt.open_loop
        self.model_type = opt.model_type
        self.model = None
        self.planning_lm_id = LMTYPE_TO_LMID[opt.language_model_type] #'gpt2-large'  # see comments above for all options
        self.translation_lm_id = 'stsb-roberta-large'  # see comments above for all options
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.generator = LM_Engine(self.model, self.planning_lm_id, self.device)
        self.data_type = opt.data_type
        self.max_tokens = opt.max_tokens

        # m-plan
        self.task_prediced_steps = []
        self.curr_prompt = ""
        self.result_list = []
        self.step_sequence = []
        self.task_eval_predict = ""

        openai.api_key = opt.api_key
        self.sampling_params = \
                {
                    "max_tokens": self.max_tokens,
                    "temperature": 0.7,
                    "top_p": 0.9,
                    "n": 10,
                    "logprobs": 1,
                    "presence_penalty": 1,
                    "frequency_penalty": 1,
                    "stop": '\n'
                }
        if opt.do_eval_each:
            self.total_score_cal = {"sentence-bleu": 0, "wmd": 0, "rouge-1-f": 0, "rouge-1-p": 0, "rouge-1-r": 0, "bert-score-f": 0, "bert-score-p": 0, "bert-score-r": 0, "meteor": 0, "sentence-bert-score": 0, "caption-t-bleu": 0, "LCS": 0, "caption-t-bleu": 0, "caption-vcap-lcs": 0, "gpt3-plan-accuracy": 0, "caption-gpt3-plan-accuracy": 0, "vplan-t-clip-score": 0, "tplan-v-clip-score": 0, "vplan-v-clip-score": 0, "tplan-t-clip-score": 0}

    def get_revision_plan(self, sample_path, current_revision_sample, template_dict, i2t_bridge):
        template = i2t_template_dict[i2t_bridge]
        task = current_revision_sample["tasks"]
        ori_steps = '\n'.join(current_revision_sample["steps"])
        captions = '\n'.join(current_revision_sample["captions"])
        if i2t_bridge == "i2t-0":
            prompt = f"Textual Instruction:\n{task}\n{ori_steps}\nVisualized Instruction:\n{captions}\n{template}"
        elif i2t_bridge == "i2t-1":
            prompt = f"Plan:\n{task}\n{ori_steps}\nVisual Imagination:\n{captions}\n{template}"
        elif i2t_bridge == "i2t-2":
            prompt = f"{task}\n{ori_steps}\n{captions}\n{template}"
        elif i2t_bridge in ["i2t-3", "i2t-4", "i2t-5"]:
            prompt = f"Step-by-step Procedure:\n{task}\n{ori_steps}\nPaired Captions:\n{captions}\n{template}"
        else:
            prompt = f"{task}\n{ori_steps}\n{captions}\n{template}"

        response = self.completion.create(
            prompt=prompt, engine="text-davinci-003", temperature=0.7,
            top_p=1, frequency_penalty=1, presence_penalty=1, best_of=1,
            max_tokens=self.max_tokens)
        answer = response.choices[0].text.strip().strip('-').strip('_').split('\n')
        # ic(i2t_bridge, answer)
        if not "Step" in answer[0]: # i2t_bridge in ["i2t-0", "i2t-1", "i2t-2", "i2t-3"] and
            answer = answer[1:]
        step_num = len(glob.glob1(sample_path,f"step_[0-9]*_bridget2i-0_caption.txt")) or len(glob.glob1(sample_path,"step_[0-9]*_bridge_caption.txt")) or len(glob.glob1(sample_path,"step_[0-9]*_caption.txt")) or len(glob.glob1(sample_path,"step_[0-9]*.txt"))
        step_num = min(step_num, len(answer))
        for step_idx in range(1, step_num+1):
            try:
                with open(os.path.join(sample_path, "step_{}_bridge{}_tplan.txt".format(str(step_idx), str(i2t_bridge) if (self.opt.t2i_template_check or self.opt.i2t_template_check) else "")), 'w') as f:
                    f.write(answer[step_idx-1])
            except:
                break

    def visual_plan_conditioned_textual_plan_revision(self, outpath, before_revision_example_list):
        for task_idx in tqdm(range(self.opt.task_num)):
            sample_path = os.path.join(outpath, f"task_{task_idx}")
            current_revision_sample = before_revision_example_list[task_idx]
            if self.opt.i2t_template_check:
                for i2t_bridge in i2t_template_dict.keys():
                    self.get_revision_plan(sample_path, current_revision_sample, i2t_template_dict, i2t_bridge)
            else:
                self.get_revision_plan(sample_path, current_revision_sample, i2t_template_dict, self.opt.i2t_bridge)

    def ask_visual_prompt(self, input_text, t2i_bridge):
        """
        - Physical Action
        Option 1:
        The task is how to surf. Learn to stand on the board properly

        What do I need to draw in the picture to describe the above text to show the action?

        In the picture, you would draw a person standing on a surfboard with their feet spread apart and arms outstretched. You could also draw waves in the background to show the person surfing.

        Option 2:
        pick up the wine glass

        What do I need to draw in the picture to describe the above text to show the action?

        You would need to draw a person with their hand reaching out to pick up a wine glass.

        - Visual Description
        The task is how to surf. Practice regularly. The more you practice, the better and more confident you will become.

        What do I need to draw in the picture to describe the above text?

        In the picture, you could draw a person standing on a surfboard in the ocean, with waves in the background. You could also draw a sun in the sky to indicate the time of day. Finally, you could add a speech bubble with the words "Practice regularly" to emphasize the importance of regular practice.

        - Plan Consistency with incorporation of Task
        use_task_hint

        - Negative Prompt
        Prompt: a person stands and a dog sits
        Negative Prompt:a person sits

        """
        prompt = ''.join(input_text) + "\n{}".format(t2i_template_dict[t2i_bridge])
        try:
            response = self.completion.create(
                prompt=prompt, engine="text-davinci-003", temperature=0.7,
                top_p=1, frequency_penalty=0, presence_penalty=0, best_of=1,
                max_tokens=50) # less max tokens for SD prompt generation
        except:
            import time
            time.sleep(60)
            response = self.completion.create(
                prompt=prompt, engine="text-davinci-003", temperature=0.7,
                top_p=1, frequency_penalty=0, presence_penalty=0, best_of=1,
                max_tokens=50) # less max tokens for SD prompt generation
        answer = response.choices[0].text.strip().strip('-').strip('_').strip('\n')
        if t2i_bridge == "t2i-0" and "draw " in answer:
            answer = answer[answer.index("draw")+4:]
        if t2i_bridge == "t2i-1" and "figure" in answer:
            answer = answer[answer.index("figure")+7:]
        return answer

    def ask_openloop(self, task_eval_predict, curr_prompt):
        if self.model_type == "task_only_base":
            prompt = "what is the step-by-step procedure of " + task_eval_predict + " without explanation "
        else:
            prompt = "A possible procedural plan is: " + curr_prompt + ", think of implementing " + task_eval_predict + " within 5 steps "
        response = self.completion.create(
            prompt=prompt, engine="text-davinci-003", temperature=0.7,
            top_p=1, frequency_penalty=1, presence_penalty=1, best_of=1,
            max_tokens=self.max_tokens)
        answer = response.choices[0].text.strip().strip('-').strip('_').split('\n')
        return [f"Step {item}" for item in answer if len(item) > 3]

    def ask(self, prompt, step_idx):
        if step_idx == 1:
            prompt = prompt + "\n what is the first step of above task in one sentence?"
        else:
            prompt = prompt + "\n what is the next step of the above procedure in one sentence? Reply \"END\" if the procedure is complete and already reached the final step."
            ic(prompt)

        import time
        time.sleep(3)
        try:
            response = self.completion.create(
                prompt=prompt, engine="text-davinci-003", temperature=0.7,
                top_p=1, frequency_penalty=1, presence_penalty=1, best_of=1,
                max_tokens=30)
        except:
            time.sleep(10)
            try:
                response = self.completion.create(
                    prompt=prompt, engine="text-davinci-003", temperature=0.7,
                    top_p=1, frequency_penalty=1, presence_penalty=1, best_of=1,
                    max_tokens=30)
            except:
                time.sleep(20)
                response = self.completion.create(
                    prompt=prompt, engine="text-davinci-003", temperature=0.7,
                    top_p=1, frequency_penalty=1, presence_penalty=1, best_of=1,
                    max_tokens=30)
        answer = response.choices[0].text.strip().strip('-').strip('_').strip('\n').strip('\t')
        ic(answer)
        return answer

    def language_planning(self, total_score_cal, data_example, sample_result_dir="", write_step_result=False):
        task = data_example["tasks"]
        if write_step_result:
            with open(os.path.join(sample_result_dir, f"task.txt"), 'w') as f:
                f.write(f"{task}")
        if self.model_type == "task_only_base":
            curr_prompt = task+'.'

        self.result_list.append('\n' + '-'*10 + ' GIVEN EXAMPLE ' + '-'*10+'\n')
        task_eval_groundtruth = task + '. ' + str(data_example["steps"])
        self.result_list.append(task_eval_groundtruth)

        task_eval_predict = task + ". "

        self.result_list.append(f'{task}.')
        step_sequence = []
        if True:
            generated_list = self.ask_openloop(task_eval_predict, curr_prompt)
            ic(generated_list)
            translated_list = []
            for step_idx, each_step in enumerate(generated_list):

                best_action = each_step # translated_action
                formatted_action = best_action # (best_action[0].upper() + best_action[1:]).replace('_', ' ')
                step_sequence.append(formatted_action)
                translated_list.append(f' Step {step_idx+1}: {formatted_action}.')
                if write_step_result:
                    with open(os.path.join(sample_result_dir, "step_{}.txt".format(str(step_idx+1))), 'w') as f:
                        f.write(f"{formatted_action}")
            self.result_list.append(" ".join(translated_list.copy()))
            task_eval_predict += " ".join(translated_list.copy())
        return self.result_list

    def generate_language_plan(self, opt, task_result_dir, summarize_example_data_list, sample=None, step_idx=-1):
        if opt.do_eval_each:
            if not os.path.isdir(task_result_dir): os.makedirs(task_result_dir)
            with open(os.path.join(task_result_dir, "{}_task_result.txt".format(opt.language_model_type)), 'w') as resultfile:
                for data_example in summarize_example_data_list[1:]:

                    self.total_score_cal, result_list = self.language_planning(self.total_score_cal, data_example)

                for score_key in self.total_score_cal[opt.model_type].keys():
                    self.total_score_cal[opt.model_type][score_key] /= (len(summarize_example_data_list))
                resultfile.writelines(result_list)
                json.dump(self.total_score_cal,resultfile)
                ic(self.total_score_cal[opt.model_type])
        else:
            if opt.task in ["c-plan"]:
                if step_idx == 1:
                    self.curr_prompt = sample["tasks"]
                    os.makedirs(task_result_dir, exist_ok=True)
                    with open(os.path.join(task_result_dir, "task.txt"), 'w') as f:
                        f.write(f"{self.curr_prompt}")
                best_action = self.ask(self.curr_prompt, step_idx)
                plan_termination = (step_idx > MAX_STEPS) or ("END" in best_action) or ("End" in best_action and len(best_action) < 5)
                if plan_termination: return plan_termination, None
                self.task_prediced_steps.append(best_action)
                formatted_action = f'Step {step_idx}: {best_action}' if not "Step" in best_action else best_action
                with open(os.path.join(task_result_dir, "step_{}.txt".format(str(step_idx))), 'w') as f:
                    f.write(f"{formatted_action}")
                self.curr_prompt += f'\n{formatted_action}'
                self.result_list.append(f'{formatted_action}')
                self.step_sequence.append(best_action)
                self.task_eval_predict += f' {formatted_action}'
                return plan_termination, best_action
            else:
                for task_idx, data_example in enumerate(summarize_example_data_list):
                    exist_task_num = len(os.listdir(task_result_dir))
                    start_task_idx = (task_idx + exist_task_num - 1) if self.opt.resume else task_idx
                    sample_result_dir = os.path.join(task_result_dir, "task_{}".format(str(start_task_idx)))
                    if not os.path.isdir(sample_result_dir): os.makedirs(sample_result_dir)
                    result_list = self.language_planning(None, data_example, sample_result_dir=sample_result_dir, write_step_result=True)


import os
import cv2
from Person_reID_pytorch import re_identification_realsense

#画像格納
frame_list = [0 for j in range(30)]

def save_frame_camera_key(color_image, dir_path, basename, skeletons_2d, ext='png'):

    os.makedirs(dir_path, exist_ok=True)
    base_path = os.path.join(dir_path, basename)

    global frame_list

    for skeleton_index in range(len(skeletons_2d)):
        skeleton_2D = skeletons_2d[skeleton_index]
        joints_2D = skeleton_2D.joints

        y1 = int(joints_2D[1].y)
        y2 = int(joints_2D[10].y)
        x1 = int(joints_2D[2].x)
        x2 = int(joints_2D[5].x)

        if(x1 > x2):
            temp = x1
            x1 = x2
            x2 = temp
        if(y1 > y2):
           temp = y1
           y1 = y2
           y2 = y1

        gap = 30
        if(y1-gap >= 0):
            y1 = y1 - gap
        if(y2+gap <= 720):
            y2 = y2 + gap
        if(x1-gap >= 0):
            x1 = x1 - gap
        if(x2+gap <= 1280):
            x2 = x2 + gap

        if color_image is None:
                return
#        if color_image.all():
        else:
            save_image = color_image[y1:y2, x1:x2]
            try:
                person_id = skeleton_2D.id

                h, w = save_image.shape[:2]
                height = round(h * (50 / w))
                resize_image = cv2.resize(save_image, dsize=(50, height))
                resize_image = re_identification_realsense.gamma_processing(resize_image)
                print(type(resize_image))
                frame_list[skeleton_index] = resize_image
                print("---------------- save picture ------------------")
                #print(frame_list)

            except Exception as ex:
#                print('Exception occured: "{}"'.format(ex))
                print("---------------- error ------------------")

    return frame_list


#coding: UTF-8
#Functions called in gui_conjuntos.py
import sys

def ordenaVetor(vetor):
	#Ordena em ordem crescente
	for i in range(len(vetor) - 1):
		for j in range(len(vetor) - 1):
			if vetor[j] > vetor[j + 1]:
				aux = vetor[j]
				vetor[j] = vetor[j + 1]
				vetor[j + 1] = aux
	vetor = list(set(vetor)) #Remove itens duplicados
	return vetor

def uniao(a, b):
	#Elementos de A e elementos de B
    uniao = []
    for i in range(0, len(a)):
        uniao.append(a[i])
    for j in range(0, len(b)):
        uniao.append(b[j])
    uniao = ordenaVetor(uniao)
    return uniao

def intersecao(a, b):
	#Elementos comuns ao conjunto A e ao conjunto B
    inter = []
    for j in range(0, len(b)):
        for i in range(0, len(a)):
            if (a[i] == b[j]):
                inter.append(a[i])
    inter = ordenaVetor(inter)
    return inter

def diferenca(a, b):
	#Elementos exclusivos de A
    for j in range(0, len(b)):
        for i in range(0, len(a)):
            if (a[i] == b[j]):
                a[i] = 'null'
    #Substitui interseções por 0 e as remove
    while a.count('null') > 0:
        a.remove('null')
    a = ordenaVetor(a)
    return a

def difSimetrica(a, b):
	#Elementos de A ou elementos de B mas não de ambos
	difS = uniao(a, b)
	inter = intersecao(a, b)

	for i in range(0, len(difS)):
		for j in range(0, len(inter)):
			if(difS[i] == inter[j]):
				difS[i] = 'null'
	#União - Interseção
	while difS.count('null') > 0:
		difS.remove('null')
	return difS


"""empty message

Revision ID: 9ef32a11645a
Revises: e13d5277b7d8
Create Date: 2018-01-26 15:47:03.131152

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '9ef32a11645a'
down_revision = 'e13d5277b7d8'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint('user_bill_id_key', 'user_bill', type_='unique')
    op.drop_column('user_bill', 'id')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('user_bill', sa.Column('id', sa.INTEGER(), autoincrement=False, nullable=False))
    op.create_unique_constraint('user_bill_id_key', 'user_bill', ['id'])
    # ### end Alembic commands ###


"""
Standard tuple uses numerical indexes to access its members which can lead to errors,
especially if the tuple has a lot of fields. namedtuple assigns names, as well as the
numerical index, to each member.

amedtuple instances are just as memory efficient as regular tuples because they do not
have per-instance dictionaries. Each kind of namedtuple is represented by its own class,
created by using the namedtuple() factory function. The arguments are the name of the new
class and a string containing the names of the elements.
"""

import collections
import sys

PPPerson = collections.namedtuple('Participant', 'name age gender')
bob = PPPerson(name='Bob', age=35, gender='male')
print(bob)
print(bob.gender)

# Invalid Field Names : As the field names are parsed, invalid values cause ValueError exceptions.

try:
    collections.namedtuple('Person', 'name class age gender')
except ValueError:
    tb = sys.exc_info()[1]
    print(tb)
    # print(ValueError)

try:
    collections.namedtuple('Person', 'name age age gender')
except ValueError:
    tb = sys.exc_info()[1]
    print(tb)

# In situations where a namedtuple is being created based on values outside of the control
# of the program (such as to represent the rows returned by a database query, where the
# schema is not known in advance), set the rename option to True so the fields are renamed.
with_class = collections.namedtuple('Person', 'name class age gender', rename=True)
print(with_class._fields)

two_ages = collections.namedtuple('Person', 'name age gender age', rename=True)
print(two_ages._fields)


import torch
import fused_adam_cuda

class FusedAdam(torch.optim.Adam):

    """Implements Adam algorithm. Currently GPU-only.  Requires Apex to be installed via
    ``python setup.py install --cuda_ext --cpp_ext``.

    It has been proposed in `Adam: A Method for Stochastic Optimization`_.

    Arguments:
        params (iterable): iterable of parameters to optimize or dicts defining
            parameter groups.
        lr (float, optional): learning rate. (default: 1e-3)
        betas (Tuple[float, float], optional): coefficients used for computing
            running averages of gradient and its square. (default: (0.9, 0.999))
        eps (float, optional): term added to the denominator to improve
            numerical stability. (default: 1e-8)
        weight_decay (float, optional): weight decay (L2 penalty) (default: 0)
        amsgrad (boolean, optional): whether to use the AMSGrad variant of this
            algorithm from the paper `On the Convergence of Adam and Beyond`_
            (default: False) NOT SUPPORTED in FusedAdam!
        eps_inside_sqrt (boolean, optional): in the 'update parameters' step,
            adds eps to the bias-corrected second moment estimate before
            evaluating square root instead of adding it to the square root of
            second moment estimate as in the original paper. (default: False)

    .. _Adam\: A Method for Stochastic Optimization:
        https://arxiv.org/abs/1412.6980
    .. _On the Convergence of Adam and Beyond:
        https://openreview.net/forum?id=ryQu7f-RZ
    """

    def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8,
                 weight_decay=0, amsgrad=False, eps_inside_sqrt = False):
        if amsgrad:
            raise RuntimeError('FusedAdam does not support the AMSGrad variant.')
        super(FusedAdam, self).__init__(params, lr, betas, eps, weight_decay, amsgrad)
        self.eps_mode = 0 if  eps_inside_sqrt else 1

    def step(self, closure=None, grads=None, output_params=None, scale=1.):
        """Performs a single optimization step.

        Arguments:
            closure (callable, optional): A closure that reevaluates the model
                and returns the loss.
            grads (list of tensors, optional): weight gradient to use for the
                optimizer update. If gradients have type torch.half, parameters
                are expected to be in type torch.float. (default: None)
            output params (list of tensors, optional): A reduced precision copy
                of the updated weights written out in addition to the regular
                updated weights. Have to be of same type as gradients. (default: None)
            scale (float, optional): factor to divide gradient tensor values
                by before applying to weights. (default: 1)
        """
        loss = None
        if closure is not None:
            loss = closure()
        if grads is not None:
           assert len(self.param_groups)==1, "mixed precision optimizer works for a single group only"
        for group in self.param_groups:
            if grads is None:
               grads = [None]*len(group['params'])
            if output_params is None:
               output_params = [None]*len(group['params'])
            for p, grad, output_param in zip(group['params'],grads, output_params):
                #note: p.grad should not ever be set for correct operation of mixed precision optimizer that sometimes sends None gradients
                if p.grad is None and grad is None:
                    continue
                if grad is None:
                    grad = p.grad.data
                if grad.is_sparse:
                    raise RuntimeError('FusedAdam does not support sparse gradients, please consider SparseAdam instead')

                state = self.state[p]

                # State initialization
                if len(state) == 0:
                    state['step'] = 0
                    # Exponential moving average of gradient values
                    state['exp_avg'] = torch.zeros_like(p.data)
                    # Exponential moving average of squared gradient values
                    state['exp_avg_sq'] = torch.zeros_like(p.data)

                exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq']
                beta1, beta2 = group['betas']

                state['step'] += 1
                out_p = torch.tensor([], dtype = torch.float) if output_param is None else output_param
                fused_adam_cuda.adam(p.data,
                                     out_p,
                                     exp_avg,
                                     exp_avg_sq,
                                     grad,
                                     group['lr'],
                                     beta1,
                                     beta2,
                                     group['eps'],
                                     scale,
                                     state['step'],
                                     self.eps_mode)
        return loss


#extract and display all the header tags
try:
    import requests
    from bs4 import BeautifulSoup
except Exception as e:
    print(e)
url = input('Enter Url to Get Header tags:- ')
try:
    response = requests.get(url)
    response.raise_for_status()
except requests.exceptions.HTTPError as e:
    print(e)
except requests.exceptions.ConnectionError:
    print('Server Not Found')
else:
    doc_body = BeautifulSoup(response.text,'html.parser')
header_tag = doc_body.find_all(['h1','h2','h3','h4','h5','h6'])
for header in header_tag:
    print(f'Header Tag is  : {header.text}')


from pyspark.sql import SparkSession
from question import Question
from linking import Linking
from EndPoint import EndPoint
from linking_parallel import ParallelLinking
import json

max_Vs = 1
max_Es = 21
max_answers = 41
limit_VQuery = 600
limit_EQuery = 300

questions = [
    "What is the revenue of IBM?",
    "Who founded Intel?",
    "What is Angela Merkel’s birth name?",
    "Where did Abraham Lincoln die?",
    "What is the profession of Frank Herbert?",
    "When did princess Diana die?",
    "Which movies did Kurosawa direct?",
    "Who was the wife of President Lincoln?",
    "Who does the voice of Bart Simpson?",
    "How many moons does Mars have?"
]
knowledge_graph = 'dbpedia'
sparql_end_point = EndPoint('dbpedia', 'http://206.12.95.86:8890/sparql')
linking = Linking(sparql_end_point, n_limit_EQuery=limit_EQuery, n_max_Es=max_Es, n_max_Vs=max_Vs)

def perform(question, index):
    parsed_question = Question(question_text=question, question_id=index)
    linking.extract_possible_V_and_E(parsed_question)
#
#
spark = SparkSession \
    .builder \
    .appName("Python Spark SQL basic example") \
    .config("spark.some.config.option", "some-value") \
    .getOrCreate()
# questions_rdd = spark.sparkContext.parallelize(questions)
# questions_with_index = questions_rdd.zipWithIndex()
# result = questions_with_index.map(lambda x: perform(x[0], x[1]))
# result.collect()

# for i, q in enumerate(questions):
#     parsed_question = Question(question_text=q, question_id=i)
#     linking.extract_possible_V_and_E(parsed_question)

file_name = r"qald-9.json"
with open(file_name) as f:
    qald9_testset = json.load(f)

question_filtered = []
for i, question in enumerate(qald9_testset['questions']):
    for language_variant_question in question['question']:
        if language_variant_question['language'] == 'en':
            question_text = language_variant_question['string'].strip()
            break
    question_filtered.append((question_text, question['id']))

question_with_index = spark.sparkContext.parallelize(question_filtered)
result = question_with_index.map(lambda x: perform(x[0], x[1]))
result.collect()
    # parsed_question = Question(question_text=question_text, question_id=question['id'])
    # linking.extract_possible_V_and_E(parsed_question)


# define a new class
class titan:
	# create a class attribute
	# if not purposely changed, this will be the default
	source = 'Japanese anime'

	# initializer is called whenever a new object (aka variable, like eren shown below)
	# is created
	# in this particular class, there are 2 instance attributes, unique to each new object
	# the 2 are: name and power

	# self is a constant, represent current object itself when this class being instantiate
	# Instantiating a class is creating a copy of the class
	# which inherits all class variables and methods.

	# take the name and power input and assign to the newly created object, aka, self
	def __init__ (self, name, power):

		self.name = name
		self.power = power

	def output(self):
		print('{} is the {} titan'.format(self.name, self.power))

	def output_add_lname(self):
		last_name = input("Do you remember {}'s last name?".format(self.name))
		full_name = self.name+' '+last_name
		return full_name

# calling the class as if we are calling a function
# the titan objects have unique name and power value
eren = titan('Eren', 'founder')
eren.output()
print('...')
print(f'the full name of {eren.name} is about to show...',eren.output_add_lname())

# we can modify an instance attributes like this
eren.power = 'a mixture of many titans...'
print('something about {} is his power is basically {}'.format(eren.name, eren.power))
print('{} is a character from a {}, Attack on Titan'.format(eren.name,eren.source))


"""
Based on Dave Hylands' json-ipc/serial_port.py
(https://github.com/dhylands/json-ipc.git).
This module implements the SerialPort class, which allows the host to talk
to another device using a serial like interface over a UART.
"""

import serial
import select
from serial.tools import list_ports

class SerialPort(object):
	"""Implements a PySerial port."""

	def __init__(self):
		self.serial_port = None

	def connect_serial(self, port, baud=115200):
		"""
		Try to connect to serial port.

	  	This function tries to connect to a serial port
		named `port`.

		Parameters
		----------
		port : string
		    name of port (e.g. /dev/ttyACM0)

		Returns
		-------
		bool
		    True if connection could be established,
		    otherwise False.
		"""
		try:
			self.serial_port = serial.Serial(port=port,
                                             		 baudrate=baud,
                                             		 timeout=0.1,
                                             		 bytesize=serial.EIGHTBITS,
                                             		 parity=serial.PARITY_NONE,
                                             		 stopbits=serial.STOPBITS_ONE,
                                             		 xonxoff=False,
                                             		 rtscts=False,
                                             		 dsrdtr=False)
		except:
			return False
		return True

	def close_serial(self):
		"""Close serial connection if it is open."""
		if self.serial_port is not None:
			print('Closing serial...')
			self.serial_port.close()
		self.serial_port = None

	def is_byte_available(self):
		"""Check if byte can be read from serial port."""
		if self.serial_port.is_open:
			readable, _, _ = select.select([self.serial_port.fileno()], [], [], 0)
			return bool(readable)
		else:
			print('Cannot check if byte is available because port is not open.')

	def read_byte(self):
		"""Read a byte from the serial port.

		   Returns
		   -------
		   int
		       Value of byte.
		"""
		if self.is_byte_available():
			data = self.serial_port.read()
			if type(data[0]) == int:
				return data[0]
			elif type(data[0]) == str:
				return ord(data[0])
			else:
				raise TypeError('serial_port.read() returned unrecognised type {0}'.format(type(data[0])))

	def write(self, data):
		"""Write `data` to the serial port.

		   Parameters
		   ----------
		   data : string / bytearray
		       Data to write.
		"""
		if self.serial_port.is_open:
			self.serial_port.write(data)
		else:
			print('Cannot write because port is not open.')

def is_micropython_usb_device(port):				#function form rshell project of dhylands
	"""Check a USB device to see if it looks like a MicroPython device.

	   Parameters
	   ----------
	   port : serial.tools.list_ports object
	       Port to check.

	   Returns
	   -------
	   bool
	       True if device connected to 'port' looks like a MicroPython
	       device, False otherwise.
	"""
	if type(port).__name__ == 'Device':
		# Assume its a pyudev.device.Device
		if ('ID_BUS' not in port or port['ID_BUS'] != 'usb' or
		'SUBSYSTEM' not in port or port['SUBSYSTEM'] != 'tty'):
			return False
		usb_id = 'usb vid:pid={}:{}'.format(port['ID_VENDOR_ID'], port['ID_MODEL_ID'])
	else:
		# Assume its a port from serial.tools.list_ports.comports()
		usb_id = port[2].lower()
	# We don't check the last digit of the PID since there are 3 possible
	# values.
	if usb_id.startswith('usb vid:pid=f055:980'):
		return True
	# Check for Teensy VID:PID
	if usb_id.startswith('usb vid:pid=16c0:0483'):
		return True
	return False

def autoscan():						#function form rshell project of dhylands
	"""Check all serial ports to see if they are MicroPython devices.

	   Checks serial ports until it finds a port with matching VID:PID for
	   a MicroPython board.

	   Returns
	   -------
	   string
	       Full device name/path. None if no MicroPython device was found.
	"""
	for port in serial.tools.list_ports.comports():
		if is_micropython_usb_device(port):
			return port[0]
	return None


"""mydjangoDemo01 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""

from django.contrib import admin
from django.urls import path
import library.views
# 用于 代码测试 和 回忆语法
# import userAdmin.views
# import userStudent.views

urlpatterns = [
    path('admin/', admin.site.urls),
    #和views中的统一页面跳转函数相对应, page/<str:pageName>是restfulURL，<str:pageName>代表地址栏传递的参数
    #比如： http://127.0.0.1:8000/page/test.html
    #这个 pageName 要与 views中的函数参数名称相同

    # jump Page
    path('page/<str:pageName>',library.views.topage),
    path('',library.views.index),
    path('library/distinguishIdentity',library.views.distinguishIdentity),
    path('library/register',library.views.register),
    path("library/userRegister",library.views.userRegister),

    # Admin Index
    path("library/addseatpage",library.views.addSeatPage),
    path("library/addseat",library.views.addSeat),
    path("library/manageSeatPage",library.views.manageSeatPage),
    path("library/modifySeat",library.views.modifySeat),

    path("library/manageStudentPage",library.views.manageStudentPage),
    path("library/modifyStudent",library.views.modifyStudent),

    path("library/manageReservationPage",library.views.manageReservationPage),

    ##### Student Index #####
    path("library/reservationPage",library.views.reservationPage),
    path("library/reservation",library.views.reservation),
    path("library/sign",library.views.sign),

    # 用于 代码测试 与 回忆语法
    # path('admin/index',userAdmin.views.register),
    # path('student/index',userStudent.views.testRedirect),
    # path('user/testRender',userStudent.views.testRender),
    # path('user/testTemplate',userStudent.views.testTemplate),
]


from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect
from django.contrib.auth.views import LoginView
from django.views import View
import songs.views as lib
import users.models
from .models import Playlist
# Create your views here.

class example(View):
    def get(self,request):
        play = Playlist.objects.all()
        return render(request, 'index.html', {'play': play})

class login(LoginView):
    template_name = 'registration/login.html'
    success_url = '/'

    def get_success_url(self):
        return self.success_url

def logout_view(request):
    logout(request)
    return redirect('/')

def signUpView(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            profile = form.save(commit= False)
            profile.user = request.user
            profile.save()
            return redirect('/')  # Reindirizza a una pagina di successo o ad una pagina specifica
    else:
        form = RegistrationForm()

    return render(request, 'registration/signup.html', {'form': form})

class RegistrationForm(UserCreationForm):
    class Meta:
        model = users.models.Profile
        fields = ('username', 'email','age','password1', 'password2')

@login_required(redirect_field_name='/login')
def library(request):
    return lib.library(request)

def artists(request):
    return lib.artists(request)


import os
import re
import time
from urllib import request
from selenium import webdriver
from selenium.common.exceptions import WebDriverException

#  获得某一个关键词下的所有页面的链接的url
def get_sub_page_url(browser, url):
    i = 0
    browser.get(url)
    next = 1
    all_url = []
    while next:
        for page_num in range(7):  # 这里指的是所有页码-1
            i += 1
            time.sleep(1)
            page_element_list = browser.find_elements_by_xpath('//td/a[contains(@target, "_blank")]')
            for item in page_element_list:
                all_url.append(item.get_attribute("href"))

            # 换页操作
            next_page = browser.find_element_by_link_text('{}'.format(page_num+2))
            next_page.click()
            time.sleep(3)

        if i == 7:  # 这里的数值是指所有页码-1
            break

    return all_url

#  获得子页的所有文本内容
def get_text_from_page(browser, all_url):
    # try:
    i = 0
    for item in all_url:

        # 打不开的网页，跳过
        try:
            browser.get(item)
        except WebDriverException:
            pass

        title = browser.find_elements_by_xpath('//div/h1')
        i += 1
        print(i)
        text_page = browser.find_elements_by_xpath('//p')
        try:
            if title[0].text == '':
                with open(r'E:\爱国主义\解放军报\{}.txt'.format(all_url[i]), 'w', encoding="utf-8") as f:
                    for it in text_page:
                        f.write('    ')
                        f.write(it.text)
                        f.write('\n')
            else:
                with open(r'E:\爱国主义\解放军报\{}.txt'.format(title[0].text), 'w', encoding="utf-8") as f:
                    for it in text_page:
                        f.write('    ')
                        f.write(it.text)
                        f.write('\n')
        except:
            pass
    time.sleep(3)

if __name__ == "__main__":
    url = 'http://search.chinamil.com.cn/search/milsearch/stouch.jsp?keyword=%E7%88%B1%E5%9B%BD%E4%B8%BB%E4%B9%89&searchfield=TITLE&indexsearch=1#'
    browser = webdriver.Chrome()
    options = webdriver.ChromeOptions()
    options.binary_location = r"D:\Google Chrome\Chrome\Application\chrome.exe"
    all_url = get_sub_page_url(browser, url)
    all_url = set(all_url)
    print(all_url)
    print(len(all_url))
    get_text_from_page(browser, all_url)
    browser.close()


# cython:language_level=3

from cmath import sqrt
from math import isclose
from math import sqrt as msqrt
from math import tanh
from time import time
import os
import psutil

import matplotlib.pyplot as plt
from pauli_func import MultiPauliString, get_sigma, l_super, norm, SparseMultiPauliString

SAVE_PATH = r'data.csv'

N = 20
MAX_BASIS = 21
TOL = 1e-8
TOL_M = 1e-16
NORM_L = 10
TOL_CHECK = 1e-12

# JXX = 1
# JXX_2 = 0
# V1 = 1
# V2 = 0

J = 3
C1 = 0
C2 = 1
t_list = [2] * N#[-2.87801326957137, -2.63752109868905, -2.2072468580584044, -1.510002582013794, -0.5432159592101127, 0.5432159592101127, 1.510002582013794, 2.2072468580584044, 2.63752109868905, 2.87801326957137, 3.005040639375704,
         # 2.87801326957137, 2.63752109868905, 2.2072468580584044, 1.510002582013794, 0.5432159592101127, -0.5432159592101127, -1.510002582013794, -2.2072468580584044, -2.63752109868905]  # [tanh(0.35*(x-J+0.5))/4/0.35*4.39 for x in range(1, N)]

def get_init(m):
    return get_sigma(m, N, 2)  # get_a(1, SP) + get_a(1, SM)

def get_current_memory_gb():
    # 获取当前进程内存占用。
    pid = os.getpid()
    p = psutil.Process(pid)
    info = p.memory_full_info()
    return info.uss / 1024 / 1024 / 1024.

# def get_hamilton():
#     # 生成哈密顿量
#     hamilton = MultiPauliString()
#     for i in range(1, N+1):
#         x = get_sigma(i, N, 5) @ get_sigma(i+1, N, 6)
#         hamilton -= (x + x.dagger) * JXX

#     for i in range(1, N+1):
#         x = get_sigma(i, N, 5) @ get_sigma(i, N,
#                                            2) @ get_sigma(i+1, N, 2) @ get_sigma(i+2, N, 6)
#         hamilton -= (x + x.dagger) * JXX_2

#     for i in range(1, N+1):
#         hamilton += (get_sigma(i+1, N, 2) + get_sigma(1, N, 0)
#                      ) @ (get_sigma(i, N, 2) + get_sigma(1, N, 0)) * (0.25 * V1)

#     for i in range(1, N+1):
#         hamilton += (get_sigma(i+2, N, 2) + get_sigma(1, N, 0)
#                      ) @ (get_sigma(i, N, 2) + get_sigma(1, N, 0)) * (0.25 * V2)

#     return hamilton

def get_hamilton():
    # 生成哈密顿量
    hamilton = MultiPauliString(N)
    for i in range(1, N+1):
        x = get_sigma(i, N, 5) @ get_sigma(i+1, N, 6)
        hamilton -= (x + x.dagger) * t_list[i-1]

    IN = get_sigma(1, N, 0)

    for i in range(1, N+1):
        hamilton += (get_sigma(i+1, N, 2) +
                     IN) @ (get_sigma(i, N, 2) + IN) * (0.25 * C2)

    return hamilton

def lanc(init_operator, h):

    basis_0 = None
    basis_1 = None
    b = []
    epsilon = msqrt(TOL_M)
    # 计算b0
    b.append(sqrt(norm(init_operator)).real)
    if isclose(b[0], 0, abs_tol=epsilon):
        return b
    basis_0 = (init_operator / b[0])
    # 计算b1
    t = l_super(basis_0, h)
    b.append(sqrt(norm(t)).real)
    if isclose(b[1], 0, abs_tol=epsilon):
        return b
    basis_1 = t / b[1]
    del t
    # with open(SAVE_PATH, 'a') as f:
    #     f.write(f'{0},{b[0]}\n{1},{b[1]}\n')

    # 计算bn
    for i in range(2, MAX_BASIS):
        t = l_super(basis_1, h) - basis_0 * b[i-1]
        t_b = sqrt(norm(t)).real
        if isclose(t_b, 0, abs_tol=epsilon):
            return b
        # t = t - inner_dot(t, basis_1) * basis_1
        # t = t - inner_dot(t, basis_1) * basis_1
        # x = inner_dot(t, t)
        b.append(t_b)
        basis_0 = basis_1
        basis_1 = (t / b[-1])
        del t
        # with open(SAVE_PATH, 'a') as f:
        #     f.write(f'{i},{b[-1]}\n')
        print(f'b_{i} = {b[-1]}')

    print('Warning')
    return b

#generate_basis = pro

tt = time()
h = SparseMultiPauliString.from_data(get_hamilton())
print(f'计算Hamilton耗时：{time() - tt}')

for i in range(1, 2):
    tt = time()
    init = get_init(i)
    #init = np.dot(get_a(1, SP), get_a(1, SM))
    b = lanc(init, h)  # fo
    print(f'计算Basis耗时：{time() - tt}')
    print(b)
    print(f'长度：{len(b)}')
    print(f'memory used: {get_current_memory_gb()} GB')
    # with open(SAVE_PATH, 'a') as f:
    #     f.write(f'{i},{",".join(map(str, b))}\n')
    # f.write(f'memory used: {get_current_memory_gb()} GB\n')

#b_2, basis_2 = th_pro(init, h)

# print(basis)
# plt.subplot(2, 1, 1)
# plt.plot([abs(i[0]-i[1]) for i in zip(b, b_2)])
# plt.subplot(2, 1, 2)

plt.plot(b)
plt.show()


# Question
# 31.	Given  a  tuple  pairs  =  {(2,5),(4,2),(9,8),(12,10)}  ,  count  the  number  of  pairs  (a,b) such that both a and b are even.

# Code

tp = {(2,5),(4,2),(9,8),(12,10)}
nl = []

for tup in tp: nl.append(tup) if (tup[0]%2==0 and tup[1]%2==0) else None
print(len(nl), "tuples are even")

# Input

# Additional Comments


# -*- coding: utf-8 -*-

"""Ycm URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""

from django.conf.urls import include, url
from django.contrib import admin
from ycm.views import login, index,check,logout
from installos.views import *
from asset.views import *
from audits.views import *
from server.views import *
from monitor.views import *
urlpatterns = [
    url(r'^ycm/admin/', include(admin.site.urls)),
    #url(r'^/index.html$', 'ycm.views.index'),
    url(r'^$', index, name='index'),
    url(r'^login/$', login,name='login'),
    url(r'^logout/$', logout,name='logout'),
    url(r'^check/$',check,name='check'),
    url(r'^install/install_list/$', system_install_list, name='install_list'),
    url(r'^install/install_manage/(?P<id>\d+)/$', system_install_managed, name='install_manage'),
    url(r'^install/add_install/$', system_install_managed, name='add_install'),
    url(r'^install/system_install/$',system_install, name='system_install'),
    url(r'^install/install_record/$',system_install_record, name='install_record'),
    #资产管理
    url(r'^asset/host_list/$', host_list, name='host_list'),
    url(r'^asset/add_host/$', host_list_manage, name='add_host'),
    url(r'^asset/delete_host/', host_list_manage, name='host_delete'),
    url(r'^asset/host_manage/(?P<id>\d+)/$', host_list_manage, name='host_manage'),
    #url(r'^asset/server_asset/$', server_asset_list, name='server_asset_list'),
    url(r'^asset/server_get/$', get_server_asset, name='get_server_asset'),
    url(r'^asset/device_list/$', network_device_list, name='network_device_list'),
    url(r'^asset/device_add/$', network_device_discovery, name='add_device'),
    url(r'^asset/idc_list/$', idc_asset_list, name='idc_asset_list'),
    url(r'^asset/add_idc/$', idc_asset_manage, name='add_idc'),
    #命令操作
    url(r'^server/key_list/$', salt_key_list, name='key_list'),
    url(r'^server/key_delete/$', salt_delete_key, name='delete_key'),
    url(r'^server/key_accept/$', salt_accept_key, name='accept_key'),
    url(r'^server/module_deploy/$', module_deploy, name='module_deploy'),
    url(r'^server/remote_execution/$', remote_execution, name='remote_execution'),

    url(r'^audit/history/$', history_list, name='history_list'),
    #监控
    url(r'monitor/monitor_host_list/$',monitor_host_list,name="monitor_host_list"),
    url(r'monitor/chcek_host_monitort/$',chcek_host_monitor,name="chcek_host_monitor"),
]


from __future__ import annotations

import math

import symforce.internal.symbolic as sf
from symforce import geo
from symforce import typing as T
from symforce.cam.camera_util import compute_odd_polynomial_critical_point
from symforce.cam.linear_camera_cal import LinearCameraCal

from .camera_cal import CameraCal

class PolynomialCameraCal(CameraCal):
    """
    Polynomial camera model in the style of OpenCV

    Distortion is a multiplicative factor applied to the image plane coordinates in the camera
    frame. Mapping between distorted image plane coordinates and image coordinates is done using
    a standard linear model.

    The distortion function is a 6th order even polynomial that is a function of the radius of the
    image plane coordinates::

        r = (p_img[0] ** 2 + p_img[1] ** 2) ** 0.5
        distorted_weight = 1 + c0 * r^2 + c1 * r^4 + c2 * r^6
        uv = p_img * distorted_weight
    """

    NUM_DISTORTION_COEFFS = 3
    DEFAULT_MAX_FOV = math.radians(120)

    def __init__(
        self,
        focal_length: T.Sequence[T.Scalar],
        principal_point: T.Sequence[T.Scalar],
        distortion_coeffs: T.Sequence[T.Scalar] = (0.0, 0.0, 0.0),
        critical_undistorted_radius: T.Scalar = None,
        max_fov: T.Scalar = DEFAULT_MAX_FOV,
    ) -> None:
        """
        Args:
            critical_undistorted_radius: The maximum radius allowable for distortion. This should
                                         be as large as largest expected undistorted radius. If the
                                         distortion coeffs are all numerical, this will be computed
                                         automatically as either the first local maximum of
                                         r(radius) in the interval (0, max_radius), or as max_radius
                                         if there is no local maximum in the interval.
            max_fov: Only used to compute critical_undistorted_radius when all camera parameters
                     are numerical. The maximum FOV (field of view) determines the maximum
                     image plane coordinates which is used to compute maximum radius.
        """
        super().__init__(focal_length, principal_point, distortion_coeffs)

        if critical_undistorted_radius is not None:
            self.critical_undistorted_radius = critical_undistorted_radius
        else:
            if any(
                isinstance(c, sf.Expr) and not isinstance(c, sf.Number) for c in distortion_coeffs
            ):
                raise ValueError(
                    "critical_undistorted_radius must be provided if the distortion_coeffs are not all numerical"
                )
            else:
                self.critical_undistorted_radius = self._compute_critical_undistorted_radius(
                    max_fov
                )

    @classmethod
    def from_distortion_coeffs(
        cls,
        focal_length: T.Sequence[T.Scalar],
        principal_point: T.Sequence[T.Scalar],
        distortion_coeffs: T.Sequence[T.Scalar] = tuple(),
        **kwargs: T.Scalar,
    ) -> PolynomialCameraCal:
        """
        Construct a Camera Cal of type cls from the focal_length, principal_point, and distortion_coeffs.

        kwargs are additional arguments which will be passed to the constructor.

        Symbolic arguments may only be passed if the kwarg critical_undistorted_radius is passed.
        """
        return cls(
            focal_length=focal_length,
            principal_point=principal_point,
            distortion_coeffs=distortion_coeffs,
            **kwargs,
        )

    @classmethod
    def storage_order(cls) -> T.Tuple[T.Tuple[str, int], ...]:
        return (
            ("focal_length", 2),
            ("principal_point", 2),
            ("critical_undistorted_radius", 1),
            ("distortion_coeffs", cls.NUM_DISTORTION_COEFFS),
        )

    def _distortion_weight(self, undistorted_radius: T.Scalar) -> T.Scalar:
        """
        Compute the distortion weight for the given undistorted radius. This weight is applied to
        the image plane coordinates.
        """
        total = 1.0
        radius_term = 1.0
        for coef in self.distortion_coeffs.to_flat_list():
            radius_term *= undistorted_radius ** 2
            total += radius_term * coef
        return total

    def pixel_from_camera_point(
        self, point: geo.Matrix31, epsilon: T.Scalar = sf.epsilon()
    ) -> T.Tuple[geo.Matrix21, T.Scalar]:
        p_img, project_is_valid = LinearCameraCal.project(point, epsilon)

        undistorted_radius = p_img.norm(epsilon)
        distortion_is_valid = sf.is_positive(self.critical_undistorted_radius - undistorted_radius)
        distorted_p_img = p_img * self._distortion_weight(undistorted_radius)

        linear_camera_cal = LinearCameraCal(
            self.focal_length.to_flat_list(), self.principal_point.to_flat_list()
        )
        uv = linear_camera_cal.pixel_from_unit_depth(distorted_p_img)

        is_valid = sf.logical_and(project_is_valid, distortion_is_valid, unsafe=True)

        return uv, is_valid

    def camera_ray_from_pixel(
        self, pixel: geo.Matrix21, epsilon: float = 0
    ) -> T.Tuple[geo.Matrix31, T.Scalar]:
        raise NotImplementedError(
            "Back projection involves computing the inverse of a polynomial function"
        )

    def _compute_critical_undistorted_radius(self, max_fov: float) -> float:
        """
        Compute the critical_undistorted_radius for the given (numerical) distortion_coeffs and
        max_fov. The max_fov is used as a bound for the max critical radius.
        """
        # The maximum radius in the image plane given the maximum expected FOV of the camera.
        # FOV is symmetric.
        max_radius = math.tan(max_fov / 2)
        return compute_odd_polynomial_critical_point(
            self.distortion_coeffs.to_flat_list(), max_radius
        )

    # -------------------------------------------------------------------------
    # Storage concept - see symforce.ops.storage_ops
    # -------------------------------------------------------------------------

    @classmethod
    def storage_dim(cls) -> int:
        # Contains the standard intrinsics, plus the critical_undistorted_radius
        return 4 + 1 + cls.NUM_DISTORTION_COEFFS

    def to_storage(self) -> T.List[T.Scalar]:
        return (
            self.focal_length.to_storage()
            + self.principal_point.to_storage()
            + [self.critical_undistorted_radius]
            + self.distortion_coeffs.to_storage()
        )

    @classmethod
    def from_storage(cls, vec: T.Sequence[T.Scalar]) -> PolynomialCameraCal:
        assert len(vec) == cls.storage_dim()
        return cls(
            focal_length=vec[0:2],
            principal_point=vec[2:4],
            critical_undistorted_radius=vec[4],
            distortion_coeffs=vec[5:],
        )

    @classmethod
    def symbolic(cls, name: str, **kwargs: T.Any) -> PolynomialCameraCal:
        with sf.scope(name):
            return cls(
                focal_length=sf.symbols("f_x f_y"),
                principal_point=sf.symbols("c_x c_y"),
                critical_undistorted_radius=sf.Symbol("radius_crit"),
                distortion_coeffs=geo.Matrix(cls.NUM_DISTORTION_COEFFS, 1)
                .symbolic("C", **kwargs)
                .to_flat_list(),
            )

    def __repr__(self) -> str:
        return "<{}\n  focal_length={},\n  principal_point={},\n  critical_undistorted_radius={},\n  distortion_coeffs={}>".format(
            self.__class__.__name__,
            self.focal_length.to_storage(),
            self.principal_point.to_storage(),
            self.critical_undistorted_radius,
            self.distortion_coeffs.to_storage(),
        )


import unittest
import api_request as ar
import zendesk_ticket_viewer as ztv
from unittest.mock import patch
from io import StringIO

# The TestCases class contains all the related unit tests.
class TestCases(unittest.TestCase):
    # The helper method for getting all the tickets
    def getAllTicketsTestHelper(self, given_answer, expected_out, parameter):
        with patch('builtins.input', return_value=given_answer), patch('sys.stdout', new=StringIO()) as fake_out:
            ztv.getAllTickets(parameter)
            self.assertEqual(fake_out.getvalue().strip(), expected_out)

    # The helper method for getting a specific method
    def getSpecificTicketTestHelper(self, given_answer, expected_out, parameter):
        with patch('builtins.input', return_value=given_answer), patch('sys.stdout', new=StringIO()) as fake_out:
            ztv.getSpecificTicket(parameter, given_answer)
            self.assertEqual(fake_out.getvalue().strip(), expected_out)

    def authenticate(self):
        credentials = 'jfu57@wisc.edu', 'apJIARU1293403'
        data = ar.makeRequest(credentials = credentials)
        tickets_posts = data['tickets']
        return tickets_posts

    # The method tests the function of getAllTickets if all the tickets typed in exist
    def testGetAllTickets(self):
        tickets_posts = self.authenticate()
        result = ("Id: 1; Subject: Sample ticket: Meet the ticket; Created by 405960585111 on 2020-11-05T23:13:28Z\n"
         " Id: 2; Subject: I need help; Created by 405960585111 on 2020-11-05T23:13:45Z\n"
         " Id: 3; Subject: velit eiusmod reprehenderit officia cupidatat; Created by 405960585111 on 2020-11-06T02:55:45Z\n"
         " Id: 4; Subject: excepteur laborum ex occaecat Lorem; Created by 405960585111 on 2020-11-06T02:55:45Z\n"
         " Id: 5; Subject: ad sunt qui aute ullamco; Created by 405960585111 on 2020-11-06T02:55:45Z")

        self.getAllTicketsTestHelper('1 2 3 4 5', result, tickets_posts)
        self.maxDiff

    # The method tests the function of getAllTickets if some of the tickets typed in do not exist
    def testGetAllTicketsNotExist(self):
        tickets_posts = self.authenticate()
        result = ("Id: 1; Subject: Sample ticket: Meet the ticket; Created by 405960585111 on 2020-11-05T23:13:28Z\n"
         " Id: 2; Subject: I need help; Created by 405960585111 on 2020-11-05T23:13:45Z\n"
         " Id: 3; Subject: velit eiusmod reprehenderit officia cupidatat; Created by 405960585111 on 2020-11-06T02:55:45Z\n"
         " Id: 4; Subject: excepteur laborum ex occaecat Lorem; Created by 405960585111 on 2020-11-06T02:55:45Z\n"
         " The typed ticket number 1000 is not found.")

        self.getAllTicketsTestHelper('1 2 3 4 1000', result, tickets_posts)
        self.maxDiff

    # The method tests the function of getSpecificTicket if the ticket exists
    def testGetSpecificExistingTicket(self):
        tickets_posts = self.authenticate()
        result = 'Id: 2; Subject: I need help; Created by 405960585111 on 2020-11-05T23:13:45Z'
        self.getSpecificTicketTestHelper('2', result, tickets_posts)

    # The method tests the function of getSpecificTicket if the ticket does not exist
    def testGetSpecificNonexistingTicket(self):
        tickets_posts = self.authenticate()
        result = ''
        self.getSpecificTicketTestHelper('1000', result, tickets_posts)

    # The method tests the function of getSpecificTicket if the ticket typed in is not a valid number
    def testGetSpecificInvalidTicket(self):
        tickets_posts = self.authenticate()
        result = ''
        self.getSpecificTicketTestHelper('abc', result, tickets_posts)

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


#!/usr/bin/env python

# reward.py: utility functions to compute reward for reinforcement learning
# Author: Nishanth Koganti
# Date: 2017/03/02

# import modules
import cv2
import rospy
import numpy as np
import cPickle as pickle
from sensor_msgs.msg import Image
from sklearn.externals import joblib
from cv_bridge import CvBridge, CvBridgeError

def computeForceReward(fData, threshInd, fThresh, forceRate):
    # compute instant reward
    nSamples = fData['left'].shape[0]
    errLeft = np.maximum(fData['left']-fThresh['left'][:,0],0)
    errRight = np.maximum(fData['right']-fThresh['right'][:,0],0)
    fReward = np.exp(-forceRate*(errLeft+errRight))

    if threshInd < nSamples-1:
        fReward[threshInd+1:] = 0.0
    return fReward

def computeImgReward(imgData, termScale, modelName='rewardModel.p'):
    # estimate img reward
    imgModel = joblib.load(modelName)
    imgReward = termScale*imgModel.predict(imgData)
    return imgReward

def computeTermReward(offsetX, offsetY, termScale, modelName='rewardModel.p',
                      cutSize=250, imSize=100):
    # get img message
    msg = rospy.wait_for_message("/kinect2/sd/image_depth_rect", Image, timeout=2)
    msg2 = rospy.wait_for_message("/kinect2/qhd/image_color_rect", Image, timeout=2)

    # convert msg to mat
    bridge = CvBridge()
    img = bridge.imgmsg_to_cv2(msg, msg.encoding)
    img2 = bridge.imgmsg_to_cv2(msg2, msg2.encoding)

    rect = img[offsetX:offsetX+cutSize,offsetY:offsetY+cutSize]
    frame = cv2.resize(rect, (imSize,imSize))

    color = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)

    # process rect for model
    dat = np.atleast_2d(((frame*255.0)/4096.0).flatten()).astype(np.uint8)
    imgModel = joblib.load(modelName)
    reward = termScale*imgModel.predict(dat)
    return reward[0], color


# Max Clingroth
# Lab 06 Question 6
SCORES = [40,91,85,15]
i = 0
while i < len(SCORES):
    j = i + 1
    while j < len(SCORES):
        print(SCORES[i], SCORES[j])
        j += 1
    i += 1