#!/usr/bin/env python

import json
import easypg
import operator
import re
import sys,traceback
from functools import cmp_to_key
from shutil import copyfile
import locale
import os
import argparse

# parser = argparse.ArgumentParser(description='Process subset of openlibrary.org "sample" data.')
# parser.add_argument('easypg config file', metavar='F', type=str, nargs='?', default='bookserver_import',
#                    help='file containing database connection information')
# parser.add_argument('limit amount imported', metavar='F', type=str, nargs='?', default='bookserver_import',
#                    help='file containing database connection information')
# parser.add_argument('--sum', dest='accumulate', action='store_const',
#                    const=sum, default=max,
#                    help='sum the integers (default: find the max)')

easypg.config_name = 'bookserver_import'

# Count the occurrences of any given key for a dataset
author_keys = {'name_too_long':0}
work_keys = {'title_too_long':0}
book_keys = {'title_too_long':0}

# Parse smaller chunks of data during some troubleshooting
# IS_LOOP_LIMIT = True
IS_LOOP_LIMIT = False
LOOP_LIMIT = 50
loop_counter = 0

LOGGING = True

log_file = open('data/sample-data/logs/load-template.log', 'w')
error_log = open('data/sample-data/logs/load-template-errors.log', 'w')

author_ids = {}
work_ids = {}
subject_ids = {}

# covers mapped to book_core.core_id for potential later use
work_covers = {}

def print_log_entry(log_file, message):
  if LOGGING:
    try:
        print >> log_file, message.encode('ascii', 'xmlcharrefreplace')
    except:
        e = sys.exc_info()[0]
        traceback.print_exc(file=error_log)
        print >> log_file, "Big time error! %s" % e

def print_key_occurrences(heading, output, sorted_keys):
    max_occurrences = sorted_keys[0][1]
    display_cutoff = max_occurrences / 2
    print >> output, "####%s Occurrences (>%s)" % (heading,display_cutoff)
    print >> output, "| Key | # | Mapping |"
    print >> output, "| ---- | ---- | ---- |"
    for x in sorted_keys:
        if x[1] > display_cutoff:
            print >> output, "| %s | %s |" % (x[0], x[1])
    print >> output, "\n"

    print >> output, "####%s Occurrences (all)" % (heading)
    print >> output, "| Key | # | Mapping |"
    print >> output, "| ---- | ---- | ---- |"
    for x in sorted_keys:
        print >> output, "| %s | %s |" % (x[0], x[1])
    print >> output, "\n"

# there are a lost of cover archives, check which one we need with out subset
def retrieve_cover_dump_names():
    # try:
    with open('data/sample-data/logs/cover_ids.json') as covers_file:
        download_list = open('data/sample-data/covers/archives/download_list.tmp', 'w')
        covers = json.load(covers_file)

        needed_files = []
        for book_id, cover in covers.iteritems():
            cover = str(cover[0])
            if len(cover) > 6:
                first_subset = "000%s" % cover[:1]
            else:
                first_subset = "0000"
            if len(cover) > 4:
                second_subset = "%s" % cover[1:3]
            else:
                second_subset = "00"
            # print "cover:%s -- first:%s -- second:%s" % (cover, first_subset, second_subset)
            needed_files.append( "_covers_%s_%s" % (first_subset,second_subset))
        needed_files_sorted = sorted(needed_files, key=cmp_to_key(locale.strcoll))
        for unique_file in set(needed_files_sorted):
            # print unique_file
            print >> download_list, "https://archive.org/download/s%s/s%s.tar" % (unique_file[:-3], unique_file)
        download_list.close()
    # except:
    #     e = sys.exc_info()[0]
    #     traceback.print_exc(file=error_log)
    #     print  "Big time error! %s" % e

def import_cover_dump(cover_size='s'):
    folder_names = {'s': 'small', 'm': 'medium'}
    if not os.path.exists("static/images/covers/"+folder_names[cover_size]):
        os.makedirs("static/images/covers/"+folder_names[cover_size])
    with open('data/sample-data/logs/cover_ids.json') as covers_file:
        covers = json.load(covers_file)
        loop_counter = 0
        download_list = open('data/sample-data/covers/'+folder_names[cover_size]+'_import_download_list.tmp', 'w')
        missing_files = open('data/sample-data/covers/'+folder_names[cover_size]+'_import_missing_file_list.tmp', 'w')
        log_file = open('data/sample-data/covers/'+folder_names[cover_size]+'_import_cover_dump.log', 'w')
        needed_files = []
        for book_id, cover in covers.iteritems():

            if IS_LOOP_LIMIT and (loop_counter > LOOP_LIMIT):
              break
            loop_counter += 1

            cover = str(cover[0])
            filename = cover.zfill(10)+"-"+cover_size.upper()+".jpg"
            # print cover
            if len(cover) > 5:
                first_subset = "000%s" % cover[:1]
            else:
                first_subset = "0000"
            if len(cover) > 4:
                second_subset = "%s" % cover[1:3]
            else:
                second_subset = "00"
            directory = cover_size+"_covers_%s_%s" % (first_subset,second_subset)
            # directory = "cover:%s -- first:%s -- second:%s" % (cover, first_subset, second_subset)
            with easypg.cursor() as cur:

                try:
                    # print "data/sample-data/covers/archives/extracted_small_covers/"+directory+"/"+filename
                    # print os.path.isfile("data/sample-data/covers/archives/extracted_small_covers/"+directory+"/"+filename)
                    # print "-------------"
                    if os.path.isfile("data/sample-data/covers/archives/extracted_"+folder_names[cover_size]+"_covers/"+directory+"/"+filename):
                        cur.execute('''
                          UPDATE books SET cover_name = %s
                          WHERE core_id = %s
                        ''', ("_covers_%s_%s/%s" % (first_subset,second_subset,cover.zfill(10)), book_id))
                        print >> log_file, "Inserting cover %s into book with core_id %s..." % (cover, book_id)
                        print >> log_file, "Moving %s to %s..." % ("data/sample-data/covers/archives/extracted_"+folder_names[cover_size]+"_covers/"+directory+"/"+filename, "static/images/covers/"+filename)
                        if not os.path.exists("static/images/covers/"+folder_names[cover_size]+"/"+directory):
                            os.makedirs("static/images/covers/"+folder_names[cover_size]+"/"+directory)
                        if not os.path.isfile("static/images/covers/"+folder_names[cover_size]+"/"+directory+"/"+filename):
                            copyfile("data/sample-data/covers/archives/extracted_"+folder_names[cover_size]+"_covers/"+directory+"/"+filename, "static/images/covers/"+folder_names[cover_size]+"/"+directory+"/"+filename)
                    else:
                        print >> missing_files, "Unable to find: %s..." % cover
                        needed_files.append( "_covers_%s_%s" % (first_subset,second_subset))
                except IOError:
                    print >> missing_files, "IOError: Unable to find: %s..." % cover
                    needed_files.append( "_covers_%s_%s" % (first_subset,second_subset))
                except:
                    e = sys.exc_info()[0]
                    traceback.print_exc(file=sys.stdout)
                    print >> log_file, "Big time error! %s" % e
        needed_files_sorted = sorted(needed_files, key=cmp_to_key(locale.strcoll))
        for unique_file in set(needed_files_sorted):
            # print unique_file
            print >> download_list, "https://archive.org/download/%s%s/%s%s.tar" % (cover_size, unique_file[:-3], cover_size, unique_file)
        download_list.close()
        missing_files.close()
        log_file.close()
        # if cur.rowcount == 1:

def generate_requests():
    with easypg.cursor() as cur:
        cur.execute('''
            SELECT book_id, is_active FROM book;
        ''')
        for book_id, is_active in cur:
            with easypg.cursor() as loop_cur:
                loop_cur.execute('''
                    INSERT INTO request (user_id, type, date_requested, priority, status, date_of_status)
                    VALUES(%s, %s, NOW(), %s, %s, NOW())
                    RETURNING request_id
                ''', (1, "Add Book", '1', "Approved"))
                request_id = loop_cur.fetchone()[0]

                loop_cur.execute('''
                    INSERT INTO request_on_book (request_id, book_id, request_type, request_text)
                    VALUES(%s, %s, %s, %s)
                    RETURNING request_on_book_id
                ''', (request_id, book_id, "Add Book", "load-template.py insertion"))
                request_on_book_id = loop_cur.fetchone()[0]

def import_all():
    try:
        with open('data/sample-data/authors.json') as af:
            loop_counter = 0
            for line in af:
                # break
                if IS_LOOP_LIMIT and (loop_counter > LOOP_LIMIT):
                  break
                loop_counter += 1
                author = json.loads(line.strip())
                author_key = author['key']
                for key in author:
                    try:
                        author_keys[key.strip()] += 1
                        # if isinstance(val, dict):
                        #     for inner_key in val:
                        #         try:
                        #             author_keys[key.strip()+"_"+inner_key] += 1
                        #         except KeyError:
                        #             author_keys[key.strip()+"_"+inner_key] = 1

                    except KeyError:
                        author_keys[key.strip()] = 1
                # print 'found author', author_key
                # put the author in your database
                with easypg.cursor() as cur:
                    # first clean up book title
                    try:
                        author['name'] = author['name'].strip()
                    except KeyError:
                        print_log_entry(error_log,"No name for this author entry (%s) ! Continuing..." % loop_counter)
                        continue

                    if len(author['name']) > 255:
                        print_log_entry(error_log,"This author's name (%s) is too long, not importing it for the moment! Continuing..." % author['name'])
                        author_keys['name_too_long'] += 1
                        continue
                    try:
                        author['personal_name'] = author['personal_name'].strip()
                        if len(author['personal_name']) > 255:
                            print_log_entry(error_log,"This author's personal_name (%s) is too long, not importing it for the moment! Continuing..." % author['key'])
                            continue
                    except KeyError:
                        author['personal_name'] = ''
                    # now check to see if we have an existing "author" entry

                    # print "Inserting author name  %s along with key %s." % (author['name'], author['key'])
                    cur.execute('''
                      INSERT INTO author (author_name, author_alias)
                      VALUES(%s, %s)
                      RETURNING author_id
                    ''', (author['name'], author['personal_name']))
                    if cur.rowcount == 1:
                        author_ids[author['key']] = cur.fetchone()[0]
                        print_log_entry(log_file,"Inserted author name  %s along with alias %s, represented by author_id: %s." % (author['name'], author['personal_name'], author_ids[author['key']]))

        book_covers = open('data/sample-data/logs/cover_ids.json', 'w')
        with open('data/sample-data/works.json') as af:

            loop_counter = 0
            for line in af:
                if IS_LOOP_LIMIT and (loop_counter > LOOP_LIMIT):
                    break
                loop_counter += 1
                work = json.loads(line.strip())
                work_key = work['key']
                #print 'found work', work_key
                # put the work in your database
                for key in work:
                    try:
                        work_keys[key.strip()] += 1
                        # if isinstance(val, dict):
                        #     for inner_key in val:
                        #         try:
                        #             work_keys[key.strip()+"_"+inner_key] += 1
                        #         except KeyError:
                        #             work_keys[key.strip()+"_"+inner_key] = 1

                    except KeyError:
                        work_keys[key.strip()] = 1

                with easypg.cursor() as cur:
                    try:
                        work['title'] = work['title'].strip()
                        if len(work['title']) > 250:
                            work_keys['title_too_long'] += 1
                            print_log_entry(error_log,"Work import: This work's title (%s) is too long, not importing it for the moment! Continuing..." % work['title'])
                            continue
                    except KeyError:
                        print_log_entry(error_log,"Work import: No title for this work (%s)! Continuing..." % loop_counter)
                        continue

                    try:
                        work['description'] = work['description'].strip()
                    except KeyError:
                        work['description'] = ""
                    except AttributeError:
                        # print to error log later.
                        work['description'] = ""
                        print_log_entry(error_log,"Work import: Can't use description, AttributeError (%s)." % loop_counter)
                        pass

                    # now check to see if we have an existing "book_core" entry
                    #print "Checking to see if a book core entry exists for %s..." % works['title']
                    cur.execute('''
                        SELECT core_id
                        FROM book_core
                        WHERE book_title = %s
                    ''', (work['title'],))

                    if cur.rowcount != 1:
                        print_log_entry(log_file,"Work import: No book core entry found, adding one now...")
                        #too many, or no, matching book_core entries found -> make a new one
                        print_log_entry(log_file,"Work import: Inserting work / book core: %s :: %s." % (work['title'], loop_counter))
                        cur.execute('''
                          INSERT INTO book_core (book_title, book_description, edition)
                          VALUES(%s, %s, %s)
                          RETURNING core_id
                        ''', (work['title'], work['description'], 1))

                    # Retrieve book_core_id for book insertion
                    book_core_id = cur.fetchone()[0]
                    work_ids[work_key] = book_core_id
                    # map to book_tag
                    # e.g., "subjects": ["Environmental aspects", "Environmental aspects of Forest fires", "Forest Hydrology",
                    # "Forest fires", "Hydrology, Forest", "Streamflow", "Forest hydrology"]
                    # work['subjects']

                    # locations related to work/book?
                    # e.g.,  "subject_places": ["Black Hills (S.D. and Wyo.)", "Custer State Park",
                    # "Custer State Park (S.D.)", "Custer State Park Region", "South Dakota"]
                    # work['subject_places']
                    # print "Work['covers']: %s" % work['covers']
                    try:
                      for cover in work['covers']:
                        try:
                          work_covers[book_core_id].append(cover)
                        except:
                          work_covers[book_core_id] = []
                          work_covers[book_core_id].append(cover)
                    except KeyError:
                        # No covers here
                        print_log_entry(error_log,"Work import: No covers found for %s" % loop_counter)
                        pass

                    # add author relationships if found
                    try:
                        position = 1
                        for author in work['authors']:
                            author = author[key]
                            # if re.match("\('(.*)'[,]*\)", author):
                            #     m = re.match("\('(.*)'[,]*\)", author)
                            #     author = m.groups()[0]
                            try:
                                print_log_entry(log_file,author)
                                author_id = author_ids[author]
                                cur.execute('''
                                INSERT INTO authorship (core_id, author_id, position)
                                VALUES(%s, %s, %s)
                                RETURNING author_id
                                ''', (book_core_id, author_id, position))
                                position += 1
                            except KeyError:
                                print_log_entry(error_log,"Work import: Couldn't find an author_id entry for author key: %s..." % author)
                    except KeyError:
                        print_log_entry(error_log,"Work import: No authors found (%s)!" % loop_counter)

        json.dump(work_covers, book_covers)
        book_covers.close()

        with open('data/sample-data/books.json') as af:
            loop_counter = 0
            for line in af:
                if IS_LOOP_LIMIT and (loop_counter > LOOP_LIMIT):
                    break
                loop_counter += 1
                book = json.loads(line.strip())
                book_key = book['key']
                for key in book:
                    try:
                        book_keys[key.strip()] += 1
                        # if isinstance(val, dict):
                        #     for inner_key in val:
                        #         try:
                        #             book_keys[key.strip()+"_"+inner_key] += 1
                        #         except KeyError:
                        #             book_keys[key.strip()+"_"+inner_key] = 1

                    except KeyError:
                        book_keys[key.strip()] = 1
                # print 'found book', book_key
                # print book
                # put the book in your database
                with easypg.cursor() as cur:
                    # first clean up book title
                    try:
                        book['title'] = book['title'].strip()
                    except KeyError:
                        # should be output to error log
                        print_log_entry(error_log,"Book import: No title for this book entry (%s) ! Continuing..." % loop_counter)
                        continue

                    if len(book['title']) > 250:
                        # log this latter
                        book_keys['title_too_long'] += 1
                        print_log_entry(error_log,"Book import: This book's title (%s) is too long, not importing it for the moment! Continuing..." % book['title'])
                        continue

                    # now check to see if we have an existing "book_core" entry
                    print_log_entry(log_file,"Book import: Checking to see if a book core entry exists for %s..." % book['title'])
                    try:
                        book_core_id = work_ids[book['work']]
                        print_log_entry(log_file,"Book import: Book core ID obtained: %s!" % book_core_id)
                    except KeyError:

                        # cur.execute('''
                        #     SELECT core_id
                        #     FROM book_core
                        #     WHERE book_title = %s
                        # ''', (book['title'],))

                        # if (cur.rowcount != 1):
                        print_log_entry(error_log,"Book import: No book core entry found (work_ids[%s]), adding one now..." % book['work'])
                        #too many, or no, matching book_core entries found -> make a new one
                        cur.execute('''
                          INSERT INTO book_core (book_title, book_description, edition)
                          VALUES(%s, %s, %s)
                          RETURNING core_id
                        ''', (book['title'], '', book['revision']))

                        # Retrieve book_core_id for book insertion
                        book_core_id = cur.fetchone()[0]

                    # check to see what type of ISBN we have to work with, go with ISBN if possible
                    # explanation of this type of structure at: http://stackoverflow.com/a/1592578/1431509
                    try:
                        book['isbn_13']
                    except KeyError:
                        try:
                            book['isbn_10']
                        except KeyError:
                            print_log_entry(error_log,"Book import: %s has no ISBN!" % book['title'])
                            book_isbn = []
                        else:
                            book_isbn = book['isbn_10']
                    else:
                        book_isbn = book['isbn_13']

                    try:
                        book['number_of_pages']
                    except KeyError:
                        page_count = None
                        print_log_entry(error_log,"Book import: %s has no page count!" % book['title'])
                    else:
                        page_count = book['number_of_pages']

                    try:
                        book['physical_format']
                    except KeyError:
                        book_type = None
                    else:
                        book_type = book['physical_format']

                    try:
                        print_log_entry(log_file,"Book import: Publish date is: %s." % book['publish_date'])
                        original_date = book['publish_date']
                    except KeyError:
                        print_log_entry(error_log,"Book import: %s has no publication date!" % book['title'])
                        publication_date = None
                        original_date = None
                    except UnicodeEncodeError:
                        print_log_entry(error_log,"Book import: UnicodeEncodeError for publish_date (%s)?" % loop_counter)
                        pass
                    else:
                        if re.match('\w* \d{1,2}, \d{4}', book['publish_date']):
                            m = re.match('(\w* \d{1,2}, \d{4})', book['publish_date'])
                            publication_date = m.groups()[0]
                        elif re.match('\w* \d{4}', book['publish_date']):
                            m = re.match('(\w*) (\d{4})', book['publish_date'])
                            publication_date = "%s 01, %s" % (m.groups()[0], m.groups()[1])
                        elif re.match('\d{4}', book['publish_date']):
                            m = re.match('(\d{4})', book['publish_date'])
                            publication_date = "January 01, %s" % m.groups()[0]
                        else :
                            publication_date = None
                    print_log_entry(log_file,"Book import: Parsed publication_date is: %s (Original date: %s)." % (publication_date, original_date))
                    # was getting output in this formatting "[u'9780110827667']" slicing off the excess
                    # until I find whats going on
                    # book_isbn = book_isbn[2:-2]
                    for isbn in book_isbn:
                        try:
                            isbn = ''.join(x for x in isbn if x.isdigit())
                            print_log_entry(log_file,"Book import: Inserting book title %s along with core_id %s (ISBN: %s - Date: %s)." % (book['title'], book_core_id, isbn, publication_date))
                            cur.execute('''
                                INSERT INTO books (core_id, publication_date, isbn, book_type, page_count)
                                VALUES(%s, %s, %s, %s, %s)
                                RETURNING book_id
                            ''', (book_core_id, publication_date, isbn, book_type, page_count))
                            book_id = cur.fetchone()[0]
                        except:
                            e = sys.exc_info()[0]
                            traceback.print_exc(file=error_log)
                            print_log_entry(error_log,"Big time error! %s" % e)
                        # add publisher relationships if found

                        try:
                            position = 1
                            for publisher in book['publishers']:
                                if len(publisher) > 250:
                                    # log this latter
                                    print_log_entry(error_log,"Book import: This book's publisher (%s) is too long, not importing it for the moment! Continuing..." % publisher)
                                    continue
                                print_log_entry(log_file,"Book import: Checking to see if a publisher entry exists for %s..." % publisher)
                                cur.execute('''
                                    SELECT publicist_id
                                    FROM publicist
                                    WHERE publicist_name = %s
                                ''', (publisher,))

                                if cur.rowcount != 1:
                                    print_log_entry(log_file,"Book import: No publisher entry found, adding one now(%s)..." % loop_counter)
                                    print_log_entry(log_file,"Book import: Inserting publisher: %s :: %s." % (publisher, loop_counter))
                                    cur.execute('''
                                      INSERT INTO publicist (publicist_name)
                                      VALUES(%s)
                                      RETURNING publicist_id
                                    ''', (publisher,))

                                publicist_id = cur.fetchone()[0]
                                cur.execute('''
                                  INSERT INTO book_publication (book_id, publicist_id, position)
                                  VALUES(%s, %s,%s)
                                ''', (book_id,publicist_id,position))
                                position += 1
                        except KeyError:
                            print_log_entry(error_log,"Book import: KeyError importing publisher(s) for (%s)!" % loop_counter)

                    try:
                        for subject in book['subjects']:
                            subject = subject.strip()
                            # if re.match("\('(.*)'[,]*\)", subject):
                            #     m = re.match("\('(.*)'[,]*\)", subject)
                            #     subject = m.groups()[0]

                            #print "Checking to see if a subject entry exists for %s..." % tag
                            cur.execute('''
                                SELECT subject_id
                                FROM subject_genre
                                WHERE subject_name = %s
                            ''', (subject,))

                            if cur.rowcount != 1:
                                print_log_entry(log_file,"Book import: No subject entry found, adding one now(%s)..." % loop_counter)
                                #too many, or no, matching book_core entries found -> make a new one
                                print_log_entry(log_file,"Book import: Inserting subject: %s :: %s." % (subject, loop_counter))
                                cur.execute('''
                                  INSERT INTO subject_genre (subject_name)
                                  VALUES(%s)
                                  RETURNING subject_id
                                ''', (subject,))

                            # Retrieve book_core_id for book insertion
                            subject_id = cur.fetchone()[0]

                            cur.execute('''
                              INSERT INTO book_categorization (core_id, subject_id)
                              VALUES(%s, %s)
                            ''', (book_core_id,subject_id))
                    except KeyError:
                        print_log_entry(error_log,"Book import: Unable to access book['subjects'] for tag creation (%s)..." % loop_counter)

                    # add author relationships if found
                    position = 1
                    try:
                        for author in book['authors']:
                            # if re.match("\('(.*)'[,]*\)", author):
                            #     m = re.match("\('(.*)'[,]*\)", author)
                            #     author = m.groups()[0]

                            try:
                                author_id = author_ids[author]
                                cur.execute('''
                                INSERT INTO authorship (core_id, author_id, position)
                                VALUES(%s, %s, %s)
                                RETURNING author_id
                                ''', (book_core_id, author_id, position))
                                position += 1
                            except KeyError:
                                print_log_entry(error_log,"Book import: KeyError: Couldn't find an author_id entry for author key: %s..." % author)
                            except TypeError:
                                print_log_entry(error_log,"Book import: TypeError: Couldn't find an author_id entry for author key: %s..." % author)
                    except KeyError:
                        print_log_entry(error_log,"Book import: No authors found!")
    except:
        e = sys.exc_info()[0]
        print_log_entry(error_log,"Big time error! %s" % e)
        traceback.print_exc(file=error_log)

    sorted_author_keys = sorted(author_keys.items(), key=operator.itemgetter(1), reverse=True)
    sorted_work_keys = sorted(work_keys.items(), key=operator.itemgetter(1), reverse=True)
    sorted_book_keys = sorted(book_keys.items(), key=operator.itemgetter(1), reverse=True)

    markdown_file = open('data/sample-data/logs/load-template-results.md', 'w')
    print_key_occurrences("Author Keys", markdown_file, sorted_author_keys)
    print_key_occurrences("Work Keys", markdown_file, sorted_work_keys)
    print_key_occurrences("Book Keys", markdown_file, sorted_book_keys)
    markdown_file.close()

# retrieve_cover_dump_names()
# import_all()
# retrieve_cover_dump_names()
# import_cover_dump('m')
generate_requests()

log_file.close()
error_log.close()


from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User

from .models import Ruta, Etapa, Comentario, Categoria

# Forms

class UserForm(UserCreationForm):

    class Meta:
        model = User
        fields = [
            'username',
            'first_name',
            'last_name',
            'email',
        ]
        labels = {
            'username': 'Username',
            'first_name': 'Name',
            'last_name': 'Surnames',
            'email': 'Email',
        }

class RutaCreate(forms.ModelForm):

    class Meta:
        model = Ruta
        fields = [
            'foto',
            'titulo',
            'descripcion',
            'distancia',
            'elevacion',
            'tiempo_estimado',
            'nivel_dificultad',
            'numero_de_etapas',
            'id_pais',
            'id_usuario'
        ]
        labels = {
            'foto': 'Foto',
            'titulo': 'Title',
            'descripcion': 'Description',
            'distancia': 'Distance (KM)',
            'elevacion': 'Elevation (M)',
            'tiempo_estimado': 'Estimated Time (00:00:00)',
            'nivel_dificultad': 'Difficulty',
            'numero_de_etapas': 'Number of Stages',
            'id_pais': 'Pais',
            'id_usuario': 'User',
        }

class EtapaForm(forms.ModelForm):

    class Meta:
        model = Etapa
        fields = [
            'numero_etapa',
            'titulo_etapa',
            'descripcion_etapa',
            'id_ruta'
        ]
        labels = {
            'numero_etapa': 'Stage Number',
            'titulo_etapa': 'Stage Title',
            'descripcion_etapa': 'Stage Description',
            'id_ruta': 'Stage ID',
        }

class ComentarioForm(forms.ModelForm):
    class Meta:
        model = Comentario
        fields = [
            'titulo',
            'contenido',
            'categoria',
            'id_usuario',
            'id_ruta',
        ]
        labels = {
            'titulo': 'Title Blog',
            'contenido': 'Blog Content',
            'categoria': 'Category',
            'id_usuario': 'Author ID',
            'id_ruta': 'Route ID',
        }


## 백준 2455번 : 지능형 기차

import sys
input = sys.stdin.readline

person = [0] * 5

for i in range(4):
    out, come = map(int, input().split())
    person[i+1] = person[i] - out + come

print(max(person))


from PyQt5 import  uic, QtGui, QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog, QPushButton, QDialogButtonBox
import  sys
import requests
import urllib3
import json
import hashlib
import uuid
import time
from MWindow import MWindow
import sqlite3

#global variables
urll = 'http://narodmon.ru/api'
app_id = str(uuid.getnode())
md5_app_id = hashlib.md5(app_id.encode()).hexdigest()
payload = {
        'cmd': 'sensorsOnDevice',
        'devices': [],
        'uuid': md5_app_id,
        'api_key': 'TTyEvZaZ92G7b',
        'lang': 'ru'
}

#on/off params
params_new = {'id':True, 'name':True, 'my':True, 'owner':True, 'mac':True, 'cmd':True,
'location':True, 'distance':True, 'time':True, 'liked':True, 'uptime':True,'site':True, 'photo':True, 'info':True, 'sensors':True}

"""Creating database module"""
try:
    #  Connecting to the database
    sqlite_connection = sqlite3.connect('iot.db')
    cursor = sqlite_connection.cursor()

    sqlite_create_table_requests = '''CREATE TABLE requests (
                                        id INT NOT NULL PRIMARY KEY,
                                        date TIMESTAMP NOT NULL);'''

    sqlite_create_table_values = '''CREATE TABLE vals (
                                        id INT NOT NULL PRIMARY KEY,
                                        sensors_id INT NOT NULL,
                                        requests_id INT NOT NULL,
                                        val VARCHAR(150) NOT NULL,
                                        parameter VARCHAR(150) NOT NULL,
                                        FOREIGN KEY (requests_id) REFERENCES requests (id),
                                        FOREIGN KEY (sensors_id) REFERENCES sensors (id));'''

    sqlite_create_table_sensors = '''CREATE TABLE sensors (
                                        id INT NOT NULL PRIMARY KEY,
                                        id_owner INT NOT NULL);'''

    cursor.execute(sqlite_create_table_requests)
    cursor.execute(sqlite_create_table_values)
    cursor.execute(sqlite_create_table_sensors)
    sqlite_connection.commit()
    cursor.close()

except sqlite3.Error as error:
    pass

finally:
    if sqlite_connection:
        sqlite_connection.close()

#инициализация списка значениями из БД
sqlite_connection = sqlite3.connect('iot.db')
cursor = sqlite_connection.cursor()
cursor.execute('select * from sensors')
res = cursor.fetchall()
for i in res:
    payload['devices'].append(i[0])
cursor.close()
sqlite_connection.close()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    UIWindow = MWindow(payload, params_new, urll)
    app.exec_()


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

from biocluster.workflow import Workflow
import os
import shutil

class KeggcWorkflow(Workflow):
    """
    化合物注释
    """

    def __init__(self, wsheet_object):
        self._sheet = wsheet_object
        super(KeggcWorkflow, self).__init__(wsheet_object)
        options = [
            {"name": "table", "type": "infile", "format": "tool_lab.simple"},
            {"name": "update_info", "type": "string"},
            #{"name": "name", "type": "string"},
            {"name": "main_id", "type": "string"},
            {"name": "search_type", "type": "string", "default":"entry"}  #entry （对应compound id）； cas_id； formula；name

        ]
        self.add_option(options)
        self.revise_infiles()
        self.set_options(self._sheet.options())
        self.database_list = ["CBR","BP","EDC",'P','PC','L']

        self.database_desc ={
            "CBR" : "Compounds_Biological",
            "BP" : "Bioactive_Peptides",
            "EDC": "Endocrine_Disrupting",
            "P" : "Pesticides" ,
            "PC" : "Phytochemical",
            "L" : "Lipids"
        }

    def run(self):
        self.tool_map = {}
        for database in self.database_list:
            anno = self.add_tool("tool_lab.anno_keggc")
            options = {
                "metab_table": self.option('table'),
                "database_name" : database,
                "search_type" : self.option("search_type")
            }

            anno.set_options(options)
            self.tool_map[database] = anno

        self.on_rely(self.tool_map.values(), self.set_db)
        for tool in self.tool_map.values():
            tool.run()

        super(KeggcWorkflow, self).run()

    def set_db(self):
        """
        导表程序
        """
        anno_api = self.api.api('tool_lab.anno_keggc')

        true_databases = []
        for database in self.database_list:
            anno_tool = self.tool_map[database]
            ret = anno_api.add_stat_detail(self.option('main_id'), anno_tool.option('stat_out').path, database)
            if ret != 'no data':
                true_databases.append(database)
            else:
                continue
            database_desc = self.database_desc[database]
            if os.path.exists(self.output_dir+'/'+database_desc):
                shutil.rmtree(self.output_dir+'/'+database_desc)
            shutil.copytree(anno_tool.output_dir, self.output_dir+'/'+database_desc)

        anno_api.update_main(self.option('main_id'),true_databases)

        self.end()

    def end(self):
        result_dir = self.add_upload_dir(self.output_dir)
        result_dir.add_relpath_rules([
            [".", "", "KEGG化合物分类注释结果", 0]

        ])

        result_dir.add_regexp_rules([
            [".*/level.xls", "xls", "化合物分类层级表", 0],
            [".*/stat.xls", "xls", "化合物分类统计表", 0]
        ])

        super(KeggcWorkflow, self).end()

if __name__ == "__main__":
    from biocluster.wsheet import Sheet
    data = {
        'name': 'keggc',
        'id': 'tsg_36964',
        'type': 'workflow',
        'options': {
            "table" : "/mnt/ilustre/users/sanger-dev/workspace/20200228/Metabolome_tsg_36991/Preprocess/output/pos/c.list",
            "main_id" : "5e4ce5e817b2bf4b326f4b20",

        }
    }

    wsheet = Sheet(data=data)

    wf = KeggcWorkflow(wsheet)
    wf.run()


# A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
# a^2 + b^2 = c^2
#
# For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
#
# There exists exactly one Pythagorean triplet for which a + b + c = 1000.
# Find the product abc.

a = 1
# comme a<b<c et a+b+c=1000 max(a)=332
while a < 333:
    b = 2
    while b < 1000 - a - b:
        # a+b = 500 mini sinon l'aire est negative
        if a + b > 500:
            if a ** 2 + b ** 2 - (1000 - a - b) ** 2 == 0:
                print(a * b * (1000 - a - b))
        b += 1
    a += 1

projecteuler09 Special Pythagorean triplet.py


from textblob import TextBlob;
from textblob import Word;
s1= " The Game Of Thrones";
s2= " Winter is comming in thrones";
s3= " White walker are comming to kill all";
d1= TextBlob(s1);
d2= TextBlob(s2);
d3= TextBlob(s3);

word=Word("thrones");
doclist=[d1,d2,d3];
for doc in doclist:
 wordlist=doc.words;
 #noofwords=len(wordlist);

 for word in wordlist:

  tf=doc.words.count(word);

  print(tf)
  print(word+"occur"+str(tf)+"times")

#ntf=tf/noofwords;
#print(ntf)


import sys, os
import numpy as np
import pycbc.types

# ------------------
# MAIN

#
# Load data
#
strain_data = np.loadtxt(sys.argv[1])
asd_data = np.loadtxt(sys.argv[2])
sample_rate = 1024.0

#
# Insert into pycbc objects and do the whitening
#
strain_time = pycbc.types.TimeSeries(strain_data, delta_t=1./sample_rate)
strain_freq = strain_time.to_frequencyseries()

asd = np.interp(strain_freq.sample_frequencies, asd_data[:,0], asd_data[:,1])
strain_freq_white = pycbc.types.FrequencySeries(strain_freq.data[:]/asd,
        delta_f=strain_freq.delta_f)

strain_time_white = strain_freq_white.to_timeseries()

#
# Dump back out
#
np.savetxt(sys.argv[1].replace('.txt','_WHITENED.txt'),
        strain_time_white.data[:])


#!/usr/bin/python3
"""module for the app.py"""
from flask import Flask, render_template, jsonify, make_response
from models import storage
from api.v1.views import app_views
import os
from flask_cors import CORS

app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "0.0.0.0"}})
host = os.getenv('HBNB_API_HOST', '0.0.0.0')
port = os.getenv('HBNB_API_PORT', '5000')

app.register_blueprint(app_views)

@app.teardown_appcontext
def teardown_close(error):
    """method that closes the database"""
    storage.close()

@app.errorhandler(404)
def error_four04(error):
    """method that raies a 404 error"""
    status_code = 404
    message = {"error": "Not found"}
    return make_response(jsonify(message), status_code)

if __name__ == "__main__":
    app.run(host=host, port=port, threaded=True)


from flask_wtf import FlaskForm
from wtforms import StringField, FloatField, BooleanField, SelectField
from wtforms.validators import InputRequired, Optional, URL, NumberRange

class AddPetForm(FlaskForm):
    """Form for adding snacks."""

    name = StringField("Pet Name", validators=[InputRequired()])
    species = SelectField("Species", choices=[('dg','dog'), ('ct','cat'), ('pc', 'porcupine')])
    photo_url = StringField("Image", validators=[Optional(), URL()])
    age = FloatField("Age", validators=[Optional(), NumberRange(min=0, max=30)])
    notes = StringField("Notes", validators=[Optional()])
    # available = BooleanField("Available", validators=[InputRequired()])

class EditPetForm(FlaskForm):
    """Form for adding snacks."""

    photo_url = StringField("Image", validators=[Optional(), URL()])
    notes = StringField("Notes", validators=[Optional()])
    available = BooleanField("Available")


import re
import nav
from math import ceil

from crcnetd._utils.ccsd_common import *
from crcnetd._utils.ccsd_log import *
from crcnetd._utils.ccsd_events import *
from crcnetd._utils.ccsd_config import config_getint
from crcnetd._utils.ccsd_session import getSession, getSessionE
from crcnetd._utils.ccsd_server import exportViaXMLRPC

import ccs_asset
import ccs_interface

ccs_mod_type = CCSD_SERVER

DEFAULT_LINK_LENGTH = 20000
MIN_DEFAULT_LINK_LENGTH = 3000
# XXX: This is a bit of a hack
WIRELESS_LINK_TYPES = [ "Managed", "Ad-Hoc" ]

class ccs_link_error(ccsd_error):
    pass

class ccs_link(ccs_class):
    """Provides an abstraction of a link in the Configuration System.

    The members of this class loosely map to the fields of the link table
    in the database.
    """

    def __init__(self, session_id, link_id):
        """Initialises a new class for a specified link.

        Parent must be a reference to the core crcnetd class.

        The specified session must be valid and have appropriate access to
        the database for the tasks you intend to perform with the class. All
        database access / configuration manipulation triggered by this
        instance will pass through the specified session.
        """

        self._errMsg = ""
        self._commit = 0
        self._csInit = ""

        session = getSession(session_id)
        if session is None:
            raise ccs_interface_error("Invalid session id")
        self._session_id = session_id

        # See if the specified link id makes sense
        sql = "SELECT * FROM link WHERE link_id=%s"
        res = session.query(sql, (link_id))
        if len(res) != 1:
            # DB Query failed
            raise ccs_link_error("Invalid link. Unable to retrieve "
                "details")

        # Store details
        self._properties = res[0]
        self.link_id = link_id

    @registerEvent("linkModified")
    @exportViaXMLRPC(SESSION_RW, AUTH_ADMINISTRATOR, True, "updateLinkDetails")
    def updateDetails(self, newDetails):
        """Updates the details of the link.

        newDetails should be a dictionary containing only those link parameters
        that have changed and need to be updated in the database.

        Returns ccs_site.SUCCESS
        """

        session = getSessionE(self._session_id)

        # Check the data
        if "link_id" not in newDetails:
            newDetails["link_id"] = self.link_id
        validateLink(self._session_id, newDetails)

        # If a changeset is not already active, start one for this batch
        self._forceChangeset("Updated details of link #%s" % self.link_id, \
            "link")

        # Build SQL Update string
        props = ["description", "type", "mode", "class_id", "essid", \
                "channel", "key", "network_address", "shared_network" ]

        if "network_address" in newDetails.keys() and len(newDetails["network_address"]) == 0:
            newDetails["network_address"] = -1

        if "type" in newDetails.keys():
            if newDetails["type"] not in WIRELESS_LINK_TYPES:
                newDetails["essid"] = -1
                newDetails["channel"] = -1
                newDetails["key"] = -1
        (sql, values) = buildUpdateFromDict("link", props, newDetails, \
                "link_id", self.link_id, True)

        # Run the query
        res = session.execute(sql, values)

        # Check that AP isn't set for an ad-hoc network
        if "type" in newDetails.keys() and newDetails["type"] == "Ad-Hoc":
            sql = "SELECT * FROM interface WHERE link_id=%s"
            res = session.query(sql, (self.link_id))
            for iface in res:
                session.execute("UPDATE interface SET master_channel = NULL, \
                master_interface = 'f' WHERE interface_id = %s", (iface["interface_id"]))

        # Trigger an Event
        triggerEvent(self._session_id, "linkModified", link_id=self.link_id)

        return self.returnSuccess()

    @exportViaXMLRPC(SESSION_RO, AUTH_USER, True, "getLink")
    def getDetails(self):
        """Returns a detailed object describing the link"""

        session = getSessionE(self._session_id)

        sql = "SELECT l.*, lm.description AS mode_desc FROM link l, " \
                "link_mode lm WHERE l.mode=lm.mode AND l.link_id=%s"
        res = session.query(sql, (self.link_id))
        return res[0]

    @registerEvent("linkRemoved")
    @exportViaXMLRPC(SESSION_RW, AUTH_ADMINISTRATOR, True)
    def removeLink(self):
        """Removes the link from the database."""
        session = getSessionE(self._session_id)

        # Trigger an Event
        triggerEvent(self._session_id, "linkRemoved", link_id=self.link_id)

        # Delete the link, will cause cascaded deletes
        sql = "DELETE FROM link WHERE link_id=%s"
        res = session.execute(sql, (self.link_id))
        return self.returnSuccess()

    @exportViaXMLRPC(SESSION_RO, AUTH_USER, True, "linkIsAdHoc")
    def isAdHoc(self):

        if self["type"] == "AdHoc":
            return True

        return False

    @exportViaXMLRPC(SESSION_RO, AUTH_USER, True, "linkIsManaged")
    def isManaged(self):

        if self["type"] == "Managed":
            return True

        return False

    @exportViaXMLRPC(SESSION_RO, AUTH_USER, True, "linkIsShared")
    def isShared(self):

        if self["shared_network"] == "t":
            return True

        return False

    @exportViaXMLRPC(SESSION_RO, AUTH_USER, True, "linkLength")
    def getLength(self, from_interface):
        """Returns the length in metres of the link from the named interface

        If there are multiple peer interfaces on the link, the length returned
        is the length to the most distance interface.

        The length is calculated using the great circle distance based on the
        GPS co-ordinates of the two sites where the host containing the
        interface is located. If GPS information is not available for either
        end the link is set to a default value of 20000m. The default value
        can be modified via the default_link_length configuration file
        parameter
        """
        session = getSessionE(self._session_id)
        orig = None
        orig_lat = 0
        orig_long = 0
        peers = []
        length = 0

        # Get the default link length
        default_link_length = config_getint("network", \
                "default_link_length", DEFAULT_LINK_LENGTH)
        if default_link_length < MIN_DEFAULT_LINK_LENGTH:
            log_error("Invalid default link length! Must be > %sm" % \
                    MIN_DEFAULT_LINK_LENGTH)
            default_link_length = DEFAULT_LINK_LENGTH

        # Select the interface co-ordinates for all interfaces on this link
        res = session.query("SELECT * FROM interface_gpslocation WHERE " \
                "link_id=%s", (self.link_id))
        if len(res) < 2:
            return default_link_length

        for iface in res:
            # No GPS co-ords available
            if iface["gps_lat"] <= 0 or iface["gps_long"] <= 0:
                return default_link_length
            this_lat = iface["gps_lat"]
            this_long = iface["gps_long"]
            if iface["interface_id"] == from_interface:
                try:
                    orig = nav.GeoPoint()
                    orig.set_latitude_dec(this_lat)
                    orig.set_longitude_dec(this_long)
                except:
                    log_warn("Invalid co-ordinates for interface %s" % \
                            from_interface, sys.exc_info())
                    return default_link_length
            else:
                peers.append((this_lat, this_long))
            if orig is not None:
                # Calculate distance
                while peers != []:
                    lat, long = peers.pop()
                    try:
                        peer = nav.GeoPoint()
                        peer.set_latitude_dec(lat)
                        peer.set_longitude_dec(long)
                    except:
                        log_warn("Invalid co-ordinates for interface", \
                            sys.exc_info())
                        return default_link_length
                    # Round the length up to the nearest integer and convert
                    # to metres
                    plength = int(ceil(orig.get_distance_to(peer) * 1000))
                    if plength > length: length = plength

        # Check that at least one distance was calculated
        if length == 0: length = default_link_length
        return length

    @exportViaXMLRPC(SESSION_RO, AUTH_USER, True, "getLinkDefaultGateway")
    def getDefaultGateway(self, host_id):
        """Returns the default gateway for the link for traffic from host_id

        Checks each host with an interface on the link to see if that host is
        marked as being an internet gateway. If it is it's IP address is
        returned.

        If there are no explicit gateways the link is checked for an interface
        acting in master mode. If one is found it is used as the gateway.

        If the previous two steps fail the lowest numbered IP address on the
        link that is not the specified host is returned.

        As a last resort a default route of 0.0.0.0 is returned.
        """
        session = getSessionE(self._session_id)

        # Look for an explicit gateway interface
        sql = "SELECT i.interface_id, h.host_id, i.ip_address FROM " \
                "interface i, host h WHERE i.host_id=h.host_id AND " \
                "i.use_gateway='f' AND h.is_gateway='t' AND i.link_id=%s"
        res = session.query(sql, (self.link_id))
        if len(res)>1:
            log_warn("Multiple gateways detected on link %s" % self.link_id)
        if len(res)>0:
            return res[0]["ip_address"]

        # Look for an interface acting as an access point
        sql = "SELECT i.interface_id, h.host_id, i.ip_address FROM " \
                "interface i, host h WHERE i.host_id=h.host_id AND " \
                "i.use_gateway='f' AND i.master_interface='t' AND " \
                "i.link_id=%s"
        res = session.query(sql, (self.link_id))
        if len(res)>1:
            log_warn("Multiple gateways detected on link %s" % self.link_id)
        if len(res)>0:
            return res[0]["ip_address"]

        # Use lowest IP on another host which isn't explicit set to use a
        # gateway itself
        sql = "SELECT i.interface_id, i.ip_address FROM interface i " \
                "WHERE i.link_id=%s AND i.use_gateway='f' AND i.host_id!=%s " \
                "ORDER BY ip_address"
        res = session.query(sql, (self.link_id, host_id))
        if len(res)>0:
            return res[0]["ip_address"]

        # Last Resort
        return "0.0.0.0"

    @exportViaXMLRPC(SESSION_RO, AUTH_USER, True, "linkGetAvailableHardware")
    def getAvailableHardware(self, interface_type):
        """Returns a list of subassets that could be attached to this link

        This list does not include physical ethernet interfaces
        """
        session = getSessionE(self._session_id)

        sql =  "SELECT * FROM link_wireless_interfaces_avail"
        if interface_type == ccs_interface.INTERFACE_TYPE_VAP:
            sql = "SELECT * FROM link_vap_interfaces_avail"
        res = session.query(sql, ())
        return res

    def isValidIP(self, ip_address, checkUsage=False):
        """Returns true if the specified IP is valid on this link.

        If checkUsage is True then the IP is only valid if it is not currently
        assigned to an interface.

        Returns True if the IP address is valid
        """

        lnetwork = cidrToNetwork(self["network_address"])
        lnetmask = cidrToNetmask(self["network_address"])
        ip = ipnum(ip_address)

        # Determine the network of the specified ip
        network = ipnetwork(ip, lnetmask)

        if lnetwork != network:
            # link network and IP network differ
            return False

        # Check IP usage if required
        if checkUsage:
            if self.ipInUse(ip):
                return False

        return True

    def connectsHost(self, host_id, interface_id=-1):
        """Returns true if the specified host is connected to this link by
        an interface other than the specified one."""
        session = getSessionE(self._session_id)

        if interface_id != -1:
            return session.getCountOf("SELECT count(*) FROM interface " \
                    "WHERE host_id=%s AND link_id=%s AND interface_id!=%s " \
                    "AND bridge_interface IS NULL AND monitor_interface " \
                    "IS NULL", (host_id, self.link_id, interface_id))
        else:
            return session.getCountOf("SELECT count(*) FROM interface " \
                    "WHERE host_id=%s AND link_id=%s AND bridge_interface " \
                    "IS NULL AND monitor_interface IS NULL", \
                    (host_id, self.link_id))

    def allocateIP(self, host_id, master_if=False):
        """Allocates an IP address for use on the link

        Uses a heuristic to try and determine the appropriate IP for the
        interface given the IP allocation scheme for the given link class.

        The two parameters host_id and master_if are not used at the present
        time but must be specified to allow a more sophisticated allocation
        algorithm to be easily implemented in future.
        """

        #First ensure that there is supposed to be an ip address
        if checkNoIp(self._session_id, self["class_id"]):
            return ""

        # Here we assume that networks that allocate upwards use "internal"
        # style addressing
        if getLinkClassAllocateUp(self._session_id, self["class_id"]):
            return self._allocateInternalIP(host_id, master_if)
        else:
            return self._allocateRoutingIP(host_id, master_if)

    def _allocateRoutingIP(self, host_id, master_if):
        """Allocates an IP address for use on a routing link

        We assume that if no other interfaces are yet allocated on this link
        then this interface is closest to the center of the network and should
        be the lowest IP in the network, otherwise assign downwards from the
        highest IP in the network for each successive interface.
        """

        nIfs = self._getNoInterfaces()

        if nIfs == 0:
            return self._minIP()

        return self._maxIP()

    def _allocateInternalIP(self, host_id, master_if):
        """Allocates an IP address for use on an internal link

        The gateway interface (which we assume is the first interface to be
        added to the link) gets the highest IP. Following interfaces get
        assigned from the second to bottom IP up to the middle of the range.

        The lowest IP is reserved for a server.
        The range from the middle IP to the top is reserved for DHCP
        """

        nIfs = self._getNoInterfaces()

        if nIfs == 0:
            return self._maxIP()

        # Reserve the lowest IP for a server
        return self._minIP(True, 1)

    def _minIP(self, checkFree=True, skipN=0):
        """Returns the lowest IP on the link, optionally finding a free IP.

        If checkFree is True (the default) this function returns the lowest
        IP on the link that is not:
        a) The network address
        b) Already assigned to another interface.

        If checkFree is false, the second check from the above list is dropped
        and the return value from this function will be the lowest IP on the
        link that is not the network address regardless of whether it is
        already assigned to an interface or not.

        skipN allows the lowest N addresses in the link to be skipped from the
        check, so minIP + N will be the optimal return value, or minIP + N +1
        if minIP + N is not free, etc.
        """

        # Absolute Min IP == network_address + 1 + however many we are skipping
        min = cidrToNetwork(self["network_address"])+1+skipN
        if not checkFree:
            return formatIP(min)

        # Find the lowest free IP
        max = self._maxIP(False)
        while self._ipInUse(min):
            if min==max:
                raise ccs_link_error("No IPs available on link!")
            min += 1

        return formatIP(min)

    def _maxIP(self, checkFree=True, skipN=0):
        """Returns the highest IP on the link, optionally finding a free IP

        If checkFree is True (the default) this function returns the highest
        IP on the link that is not:
        a) The broadcast address
        b) Already assigned to another interface.

        If checkFree is false, the second check from the above list is dropped
        and the return value from this function will be the highest IP on the
        link that is not the broadcast address regardless of whether it is
        already assigned to an interface or not.

        skipN allows the highest N addresses in the link to be skipped from the
        check, so maxIP - N will be the optimal return value, or maxIP - N - 1
        if maxIP - N is not free, etc.
        """

        # Absolute Max IP == broadcast_address -1 - however many we skip
        max = ipbroadcast(cidrToNetwork(self["network_address"]),
                cidrToNetmask(self["network_address"]))-1-skipN
        if not checkFree:
            return formatIP(max)

        # Find the highest free IP
        min = self._minIP(False)
        while self._ipInUse(max):
            if max==min:
                raise ccs_link_error("No IPs available on link!")
            max -= 1

        return formatIP(max)

    def _ipInUse(self, ip):
        """Checks if the specified IP address is in use anywhere"""
        session = getSessionE(self._session_id)
        ips = formatIP(ip)

        return session.getCountOf("SELECT count(*) FROM "
                "interface WHERE ip_address=%s", (ips))

    def _getNoInterfaces(self):
        """Returns the number of interfaces configured on this link"""
        session = getSessionE(self._session_id)

        return session.getCountOf("SELECT count(*) FROM interface WHERE "
                "link_id=%s", (self["link_id"]))

@registerEvent("linkAdded")
@exportViaXMLRPC(SESSION_RW, AUTH_ADMINISTRATOR)
def addLink(session_id, link):
    """Adds the specified link to the database

    If the link was sucessfully added to the database the new link_id is
    returned.
    """
    session = getSessionE(session_id)

    # verify link details
    validateLink(session_id, link, True)

    # insert the site record
    props = [ "description", "type", "mode", "class_id", "network_address", \
            "shared_network" ]
    if link["type"] in WIRELESS_LINK_TYPES:
        props.extend(["essid", "channel", "key"])

    if "network_address" in link.keys() and len(link["network_address"]) == 0:
        link["network_address"] = -1

    (sql, values) = buildInsertFromDict("link", props, link, True)
    session.execute(sql, values)

    # Get the link_id that was created for the link
    sql = "SELECT currval('link_link_id_seq') as link_id"
    res = session.query(sql, ())
    link_id = res[0]["link_id"]

    # Raise the event
    triggerEvent(session_id, "linkAdded", link_id=link_id);

    return link_id

@exportViaXMLRPC(SESSION_RO, AUTH_USER)
def getLinkList(session_id, subasset_id=-1):
    """Returns a list of links in the system"""
    session = getSessionE(session_id)

    typeq = ""
    if subasset_id != -1:
        subasset = ccs_asset.ccs_subasset(session_id, subasset_id)
        if subasset.supportsFunction("wireless_interface"):
            typeq = " AND l.type!='Ethernet'"
        else:
            typeq = " AND l.type='Ethernet'"

    sql = "SELECT l.link_id, l.description, l.type, l.mode, lc.description " \
            "AS class_desc, l.essid, l.channel, l.network_address, " \
            "l.shared_network, COALESCE(lic.no_interfaces, 0) AS " \
            "no_interfaces FROM link_class lc, link l LEFT JOIN " \
            "link_interface_count lic ON lic.link_id=l.link_id " \
            "WHERE lc.link_class_id=l.class_id%s ORDER BY l.network_address" \
            % typeq
    res = session.query(sql, ())
    res2 = []
    for row in res:
        nrow = row
        str = row["type"]
        str = "%s %s" % (str, \
                getModeDesc(session_id, row["type"], row["mode"]))
        if row["shared_network"] == "t":
            str = "%s (shared)" % str
        nrow["mode_desc"] = str
        res2.append(nrow)

    return res2

def getLinkTemplateVariables(session_id):
    links = {}
    for link in getLinkList(session_id):
        links[link["link_id"]] = link
    return links

def getModeDesc(session_id, type, mode):
    """Returns a description string describing the link's mode"""
    #XXX: This is a bit of a hack
    if type == "Ethernet":
        return "%sMbps" % mode
    elif type == "Managed" or type == "Ad-Hoc":
        return "802.11%s" % mode

    return ""

@exportViaXMLRPC(SESSION_RO, AUTH_USER)
def getLinkTypes(session_id):
    """Returns a detailed object describing the available link types"""
    session = getSessionE(session_id)

    sql = "SELECT * FROM link_type"
    res = session.query(sql, ())
    return res

@exportViaXMLRPC(SESSION_RO, AUTH_USER)
def getLinkModes(session_id, type_name=""):
    """Returns a dictionary describing the available link modes"""
    session = getSessionE(session_id)
    sql = "SELECT * FROM link_mode"
    params = []
    if type_name != "":
        sql = "%s WHERE type_name=%%s" % sql
        params.append(type_name)
    res = session.query(sql, params)
    return res

@exportViaXMLRPC(SESSION_RO, AUTH_USER)
def getLinkClasses(session_id, includeHostAllocs=False):
    """Returns a dictionary describing the available link classes

    Link classes that can only support a single host (ie, allocsize 32) are
    excluded from this list.
    """
    session = getSessionE(session_id)

    sql = "SELECT * FROM link_class"
    if not includeHostAllocs:
        sql += " WHERE allocsize<32"
    res = session.query(sql, ())
    return res

def getInterfaceLink(session_id, interface_id):
    """Returns a link object for the link specified for the interface"""
    from crcnetd.modules.ccs_interface import ccs_interface

    interface = ccs_interface(session_id, interface_id)

    return ccs_link(session_id, interface["link_id"])

@exportViaXMLRPC(SESSION_RO, AUTH_USER)
def getChannelList(session_id, mode=""):
    """Returns a dictionary describing the available channels for a mode"""
    session = getSessionE(session_id)
    sql = "SELECT * FROM mode_channels"
    params = []
    if mode != "":
        sql = "%s WHERE mode=%%s" % sql
        params.append(mode)
    res = session.query(sql, params)
    return res

def getLinkClassTemplateVariables(session_id):
    """Returns a list of link classes for use in templates"""
    session = getSessionE(session_id)

    classes = {}
    for lc in getLinkClasses(session_id, True):
        lc["mangled_description"] = re.sub("[ \.\-]", "_", lc["description"])
        classes[lc["link_class_id"]] = lc
    return classes

def checkNoIp(session_id, link_class_id):
    """Returns true if the specified link class does not allocate IP"""
    session = getSessionE(session_id)

    # Get link class details
    lcd = session.query("SELECT * FROM link_class WHERE link_class_id=%s", \
            (link_class_id))
    if len(lcd) != 1:
        raise ccs_link_error("Invalid link class ID!")

    if len(lcd[0]["netblock"]) > 0:
        return False
    else:
        return True

def getLinkClassAllocateUp(session_id, link_class_id):
    """Returns true if the specified link class allocates upwards"""
    session = getSessionE(session_id)

    # Get link class details
    lcd = session.query("SELECT * FROM link_class WHERE link_class_id=%s", \
            (link_class_id))
    if len(lcd) != 1:
        raise ccs_link_error("Invalid link class ID!")

    return lcd[0]["allocup"]

@exportViaXMLRPC(SESSION_RO, AUTH_USER)
def allocateNetwork(session_id, link_class_id):
    """Allocates a network address for use on a link"""
    session = getSessionE(session_id)

    # Get link class details
    lcd = session.query("SELECT * FROM link_class WHERE link_class_id=%s", \
            (link_class_id))
    if len(lcd) != 1:
        raise ccs_link_error("Invalid link class ID!")

    base = cidrToNetwork(lcd[0]["netblock"])
    baselen = cidrToLength(lcd[0]["netblock"])
    alloclen = lcd[0]["allocsize"]

    if lcd[0]["allocup"]:
        net = _getLowestAvailableNetwork(session_id, base, baselen, alloclen)
    else:
        net = _getHighestAvailableNetwork(session_id, base, baselen, alloclen)

    return net

def _getLowestAvailableNetwork(session_id, base, basesize, allocsize):
    """Returns the lowest network not in use in the specified base netblock.

    Uses basesize and allocsize to determine the bounds of each network.
    """
    allocmask = bitsToNetmask(allocsize)

    # Determine the top network
    basemask = bitsToNetmask(basesize)
    topnet = ipbroadcast(base, basemask)

    # Loop upwards until we find a free network
    while base <= topnet:
        net = ipnetwork(base, allocmask)
        netstr = "%s/%s" % (formatIP(net), allocsize)
        if not _networkInUse(session_id, netstr, basesize):
            # Found one, return it
            return netstr
        # In use, move up a network
        base += (2**(32-allocsize))

    # None available!
    raise ccs_link_error("No available networks!")

def _getHighestAvailableNetwork(session_id, base, basesize, allocsize):
    """Returns the lowest network not in use in the specified base netblock.

    Uses basesize and allocsize to determine the bounds of each network.
    """
    allocmask = bitsToNetmask(allocsize)

    # Determine the top network
    basemask = bitsToNetmask(basesize)
    topnet = ipbroadcast(base, basemask)

    # Determine the bottom network
    botnet = ipnetwork(base, allocmask)

    # Start the the top
    base = topnet

    # Loop downwards until we find a free network
    while base >= botnet:
        net = ipnetwork(base, allocmask)
        netstr = "%s/%s" % (formatIP(net), allocsize)
        if not _networkInUse(session_id, netstr, basesize):
            # Found one, return it
            return netstr
        # In use, move up a network
        base -= (2**(32-allocsize))

    # None available
    raise ccs_link_error("No available networks!")

def _networkInUse(session_id, cidr_network, baselen):
    """Checks if the specified network is in use anywhere in the system"""

    session = getSessionE(session_id)

    # Check for this exact network
    n = session.getCountOf("SELECT count(*) FROM link WHERE " \
            "network_address=%s", (cidr_network))
    if n > 0:
        return True

    # Check for larger networks (smaller prefix lengths) that include this
    net = cidrToNetwork(cidr_network)
    len = cidrToLength(cidr_network)
    while len > baselen:
        len -= 1
        netmask = bitsToNetmask(len)
        newnw = ipnetwork(net, netmask)
        nwstr = "%s/%s" % (formatIP(newnw), len)
        n = session.getCountOf("SELECT count(*) FROM link WHERE " \
            "network_address=%s", (cidr_network))
        if n > 0:
            return True

    # Not in use
    return False

def validateLinkClassId(session_id, link_class_id):
    """Checks that the specified class_ID is valid (ie in the DB)"""
    session = getSessionE(session_id)

    rv = session.getCountOf("SELECT count(*) FROM link_class WHERE " \
            "link_class_id=%s", (link_class_id))
    if rv != 1:
        raise ccs_link_error("Invalid Link Class ID! Link class not found!")

    return

def validateLinkMode(session_id, type_name, mode):
    """Checks that the specified link type is valid"""
    session = getSessionE(session_id)

    rv = session.getCountOf("SELECT count(*) FROM link_type WHERE " \
            "type_name=%s", (type_name))
    if rv != 1:
        raise ccs_link_error("Invalid link type! Type not found!")

    rv = session.getCountOf("SELECT count(*) FROM link_mode WHERE " \
            "type_name=%s AND mode=%s", (type_name, mode))
    if rv != 1:
        raise ccs_link_error("Invalid link mode! Mode not found!")

    return

def validateLinkId(session_id, link_id):
    """Checks that the specified linkID is valid (ie in the DB)"""
    session = getSessionE(session_id)

    rv = session.getCountOf("SELECT count(*) FROM link WHERE link_id=%s", \
                (link_id))
    if rv != 1:
        raise ccs_link_error("Invalid Link ID! Link not found!")

    return

def validateLink(session_id, details, isAdd=False):
    """Validates that the specified link details are correct"""

    # Link ID
    if "link_id" in details.keys():
        validateLinkId(session_id, details["link_id"])
    elif not isAdd:
        raise ccs_link_error("Link ID must be specified!")

    # Required props
    if isAdd:
        rprops = [ "description", "type", "mode", "class_id", \
                "network_address", "shared_network" ]
        for prop in rprops:
            if prop not in details.keys():
                raise ccs_link_error("%s is a required link property!" % prop)

    # Check values
    if "description" in details.keys():
        if len(details["description"])<=0:
            raise ccs_link_error("Description must be specified!")
        if len(details["description"])>250:
            raise ccs_link_error("Description must be less than 250 chars!")

    if "mode" in details.keys() and "type" in details.keys():
        validateLinkMode(session_id, details["type"], details["mode"])
    if "class_id" in details.keys():
        validateLinkClassId(session_id, details["class_id"])
    if "type" in details.keys() and details["type"] in WIRELESS_LINK_TYPES:
        if "essid" in details.keys():
            if len(details["essid"])<=0:
                raise ccs_link_error("ESSID must be specified!")
            if len(details["essid"])>64:
                raise ccs_link_error("ESSID must be less than 64 chars!")
            essid_valid = re.compile("[a-zA-z0-9\-]*")
            if essid_valid.match(details["essid"]) == None:
                raise ccs_link_error("Invalid character in ESSID!")
        if "channel" in details.keys():
            try:
                c = int(details["channel"])
                if c == 0:
                    raise ccs_link_error("Channel must not be 0!")
            except:
                raise ccs_link_error("Invalid channel specified!")
        if "key" in details.keys() and details["key"] != "":
            if len(details["key"]) != 32:
                raise ccs_link_error("Key must be 32 characters!")
            key_valid = re.compile("[a-zA-z0-9]*")
            if key_valid.match(details["key"]) == None:
                raise ccs_link_error("Invalid character in key!")
    if "network_address" in details.keys() and len(details["network_address"]) > 0:
        validateCIDR(details["network_address"])

        session = getSession(session_id)
        if session is None:
            raise ccs_interface_error("Invalid session id")

        # Must ensure that no two links can have same ip range
        sql = "SELECT link_id FROM link WHERE network_address=%s"
        p = [details["network_address"]]
        if "link_id" in details.keys():
            sql += " AND link_id != %s"
            p.append(details["link_id"])
        res = session.query(sql, p)
        if len(res) != 0:
            raise ccs_link_error("Network address already in used!")

    if "shared_network" in details.keys():
        if details["shared_network"] not in ["t","f"]:
            raise ccs_link_error("Shared Network flag must be 't' or 'f'!")

    # All OK
    return


import datetime as dt
import dateutil.parser
import random

GAME_STEPS = {
    "Starting": 0,
    "Draft": 1,
    "Captain": 2,
    "Running": 3,
    "Close": 4}

PRINT_DATE_FORMAT = r"%d-%m-%Y"

class Person:
    def __init__(self, name, dob, dod=None, WID=None, citizenships=[], genders=[], occupations=[]):
        self.name = name
        self.dob = self._parse_date(dob)
        self.dod = self._parse_date(dod)
        self.WID = WID
        self.citizenships = citizenships
        self.genders = genders
        self.occupations = occupations
        self.is_captain = False
        self.is_first_death = False
        self.score = None

    def __eq__(self, other):
        return self.WID == other.WID

    def __repr__(self):
        string = f"{self.name} ({dt.datetime.strftime(self.dob, PRINT_DATE_FORMAT)}"
        if self.dod:
            string += f" - {dt.datetime.strftime(self.dod, PRINT_DATE_FORMAT)}"
        string += ")"
        return f"Person({string})"

    def __str__(self):
        string = f"{self.name} ({dt.datetime.strftime(self.dob, PRINT_DATE_FORMAT)}"
        if self.dod:
            string += f" - {dt.datetime.strftime(self.dod, PRINT_DATE_FORMAT)}"
        string += ")"
        return string

    def _parse_date(self, date):
        if isinstance(date, dt.datetime):
            return date
        elif isinstance(date, str):
            return dateutil.parser.parse(date)
        return None

    @property
    def age(self):
        if self.dod is not None:
            recent_date = self.dod
        else:
            recent_date = dt.datetime.now()
        return self._calculate_age(self.dob, recent_date)

    def _calculate_age(self, date1, date2):
        years = date2.year - date1.year
        months = date2.month - date1.month
        days = date2.day - date1.day
        if months >= 0 and days >= 0:
            return years + 1
        else:
            return years

    def calculate_score(self):
        basic_score = 100 - self.age

        # Primo sangue
        first = 5 if self.is_first_death else 0

        # Speedy gonzales
        gonzales = 0
        if self.dod is not None and self.dod.month == 1:
            gonzales = 3

        # Zona cesarini
        cesarini = 0
        if self.dod is not None and self.dod.month == 12 and self.dod.day >= 25:
            cesarini = 5

        # 27 club
        club27 = 27 if self.age == 27 else 0

        # Globetrotter
        globetrotter = max(0, 2 * (len(self.citizenships) - 1))

        # Inclusivity
        inclusivity = max(0, 5 * (len(self.genders) - 1))

        # Happy birthday
        birthday = 0
        if self.dod is not None and self.dob is not None and self.dod.month == self.dob.month and self.dod.day == self.dob.day:
            birthday = 10

        # Jack of all trades
        jack = max(0, 2 * (len(self.occupations) - 1))

        total_score = basic_score + first + gonzales + cesarini + club27 + globetrotter + inclusivity + birthday + jack

        if self.is_captain:
            return total_score * 2
        else:
            return total_score

    def add_occupation(self, occupation):
        self.occupations.append(occupation)

    def add_citizienship(self, citizienship):
        self.citizenships.append(citizienship)

    def add_gender(self, gender):
        self.genders.append(gender)

class Team:
    def __init__(self, name="", owner=None, players=[], captain=None):
        self.name = name
        self.owner = owner
        self.players = players
        self.captain = self._set_captain(captain)
        self.score = self._calculate_score()

    def __eq__(self, other):
        return self.owner == other.owner and self.name == other.name

    def _set_captain(self, captain):
        self.captain = None
        for idx, player in enumerate(self.players):
            if captain == player:
                self.captain = captain
                self.players[idx].is_captain = True
            else:
                self.players[idx].is_captain = False

    def _calculate_score(self):
        scores = [p.score if p.dod else 0 for p in self.players]
        self.score = sum(scores)

    def calculate_score(self):
        self._calculate_score()
        return self.score

    def set_captain(self, captain):
        self._set_captain(captain)
        return self.captain

    def has_captain(self):
        if self.captain:
            return True
        else:
            return False

    def add_player(self, player):
        if player in self.players:
            raise ValueError("The player is already part of this team")
        self.players.append(player)

    def update_player(self, player):
        if player not in self.players:
            raise ValueError("The player is not part of this team")
        for p in self.players:
            p.dod = player.dod
            p.citizenships = player.citizenships
            p.genders = player.genders
            p.occupations = player.occupations
            p.is_first_death = player.is_first_death

class Game:
    def __init__(self, teams=[], ban_list={}, team_size=10):
        self.teams = teams
        self.all_players = {'alive': {}, 'dead': {}}
        self.ban_list = ban_list
        self.first_death = None
        self.status = GAME_STEPS["Starting"]
        self.draft_order = []
        self.current_drafter = 0
        self.team_size = team_size

    def update_structure(self):
        if not hasattr(self, "team_size"):
            self.team_size = 10

    def add_team(self, team):
        if team.owner in [t.owner for t in self.teams]:
            raise ValueError(f"There is already a team owned by {team.owner}")
        if team in self.teams:
            raise ValueError("The team is already part of the game!")
        self.teams.append(team)

    def start_draft(self):
        self.draft_order = [t for t in self.teams]
        random.shuffle(self.draft_order)
        self.current_drafter = 0
        self.status = GAME_STEPS["Draft"]

    def next_drafter(self):
        self.current_drafter += 1
        if self.current_drafter >= len(self.draft_order):
            self.current_drafter = 0
            self.draft_order.reverse()
        next_drafter = self.draft_order[self.current_drafter]
        return next_drafter

    def get_current_drafter(self):
        return self.draft_order[self.current_drafter]

    def cancel_draft(self):
        for team in self.teams:
            team.players = []
            team.captain = None
            team.score = 0
        self.status = GAME_STEPS["Starting"]

    def start_game(self):
        self.status = GAME_STEPS["Running"]

    def close_game(self):
        self.status = GAME_STEPS["Close"]

    def start_captain(self):
        self.status = GAME_STEPS["Captain"]

    def get_team_from_owner(self, owner):
        team = [t for t in self.teams if t.owner == owner]
        if team:
            return team[0]
        else:
            return None

    def set_captain(self, team, idx):
        if self.status not in [GAME_STEPS["Draft"], GAME_STEPS["Captain"]]:
            raise ValueError("You have to be in the draft or right after to set a captain")
        try:
            idx_team = self.teams.index(team)
        except ValueError:
            raise ValueError("The team is not part of the game!")

        self.teams[idx_team].set_captain(team.players[idx])

    def rename_team(self, team, name):
        try:
            idx_team = self.teams.index(team)
        except ValueError:
            raise ValueError("The team is not part of the game!")
        self.teams[idx_team].name = name

    def get_team_from_name(self, name):
        return [t for t in self.teams if t.name == name]

    def add_player(self, team, player):
        if self.status != GAME_STEPS["Draft"]:
            raise ValueError("You have to start the draft before adding players")
        try:
            idx = self.teams.index(team)
        except ValueError:
            raise ValueError("The team is not part of the game!")
        if player.dod:
            raise ValueError("The player is already dead :(")
        if player.WID in self.ban_list:
            raise ValueError("The player is in the ban list")
        if player.WID in self.all_players['alive']:
            raise ValueError("The player is already part of a team")
        self.all_players['alive'][player.WID] = player
        self.teams[idx].add_player(player)
        return player

    def update_alive_players(self, updated_players):
        if not self.first_death:
            update_first_death = True
            self.first_death = []
        else:
            update_first_death = False
        for id, player in updated_players.items():
            if id in self.all_players['alive']:
                del self.all_players['alive'][id]
                if update_first_death:
                    player.is_first_death = True
                    self.first_death.append(player)
                self.all_players['dead'][id] = player
                for t in self.teams:
                    if player in t.players:
                        t.update_player(player)

    def update_ban_list(self, ban_list):
        self.ban_list = ban_list

    @property
    def ranking(self):
        return ((t.name, t.score) for t in self.teams)


n = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))

def find_smaller_in_alist(index, b):
    for i in range(n):
        if a_list[i] <= b and a_list[index] <= b_list[i]:
            return i
    return -1
counter = 0

for i in range(n):
    if a_list[i] <= b_list[i]:
        pass
    elif counter == n-2:
        print("No")
        break
    else:
        counter += 1
        index = find_smaller_in_alist(i, b_list[i])
        if index == -1:
            print("No")
            break
        else:
            a_list[i], a_list[index] = a_list[index], a_list[i]
    if i == n-1:
        print("Yes")


import torch
import torch.nn as nn
from arguments import get_model_classes, get_args

class Model(torch.nn.Module):

    def __init__(self, args, tokenizer = None, prompt_label_idx = None):
        super().__init__()
        model_classes = get_model_classes()   # model_classes存储的是一个模型字典
        model_config = model_classes[args.model_type]  # 选择模型，加载模型配置 model_type=roberta ：{'config': <class 'transformers.models.roberta.configuration_roberta.RobertaConfig'>, 'tokenizer': <class 'transformers.models.roberta.tokenization_roberta.RobertaTokenizer'>, 'model': <class 'transformers.models.roberta.modeling_roberta.RobertaModel'>}

        self.prompt_label_idx = prompt_label_idx  # 每个位置对应的所有可能出现的label的编号

        self.model = model_config['model'].from_pretrained(
            args.model_name_or_path,
            return_dict=False,
            cache_dir=args.cache_dir if args.cache_dir else None)  # 加载预训练模型

        self.mlp = torch.nn.Sequential(
            torch.nn.Linear(self.model.config.hidden_size, self.model.config.hidden_size),
            torch.nn.ReLU(),
            # nn.Dropout(p=args.dropout_prob),
            torch.nn.Linear(self.model.config.hidden_size, self.model.config.hidden_size),
            )

        self.extra_token_embeddings = nn.Embedding(args.new_tokens, self.model.config.hidden_size) # new_tokens=5

    def forward(self, input_ids, attention_mask, token_type_ids, input_flags, mlm_labels, labels):  # input_ids = {Tensor: (8,512)} = attention_mask, token_type_ids, input_flags, mlm_labels
        raw_embeddings = self.model.embeddings.word_embeddings(input_ids)  # roberta原始的词嵌入 raw_embeddings = {Tensor: (8,512,1024)}
        new_token_embeddings = self.mlp(self.extra_token_embeddings.weight)  # 新的嵌入？？？ new_token_embeddings = {Tensor: (5,1024)}
        new_embeddings = new_token_embeddings[input_flags]  # 这里相当于按照新的嵌入吧input_flags扩大一维 new_embeddings = {Tensor: (8,512,1024)} 最有一个维度是一样的，应该是初始化操作
        inputs_embeds = torch.where(input_flags.unsqueeze(-1) > 0, new_embeddings,raw_embeddings)  # torch.where（）函数  input_flags.unsqueeze(-1)的张量中大于0的用 new_embeddings对应元素填充，其他位置用raw_embeddings中对应位置元素填充 ，一开始都是0，那么就是直接用原始嵌入在填充？？？
        hidden_states, _ = self.model(inputs_embeds=inputs_embeds,attention_mask=attention_mask, token_type_ids=token_type_ids)  # hidden_states = {Tensor:(8,512,1024))}
        hidden_states = hidden_states[mlm_labels >= 0].view(input_ids.size(0), len(self.prompt_label_idx),-1)  # 取hidden_states中[MASK]的位置的编码,并将维度变为.view(8，5，-1）--> hidden_states={Tensor: (8,5,1024)}
        logits = [
            torch.mm(hidden_states[:,index,:],  self.model.embeddings.word_embeddings.weight[i].transpose(1,0))
            for index, i in enumerate(self.prompt_label_idx)
        ]   # # torch.mm()矩阵乘法  prompt_label_idx存储的是[MASK]位置的label的编号 ①index=0,i=[621,1651,10014] ②index=1,i=[18,354,7325]...⑤   hidden_states[:,index,:]=(8,1024)    预训练模型的嵌入表：model.embeddings.word_embeddings.weight=(50265,1024)   model.embeddings.word_embeddings.weight[i]=(3,1024)     model.embeddings.word_embeddings.weight[i].transpose(1,0)=(1024,3)    (8,1024)*(1024,3)=(8,3)  所以最终得到一个（8，3）的向量
        return logits

def get_model(tokenizer, prompt_label_idx,device):
    args = get_args()
    model = Model(args, tokenizer, prompt_label_idx)
    if torch.cuda.device_count() > 1:
        model = torch.nn.DataParallel(model)
    if(device=="cpu"):
        model.cpu()
    else:
        model.cuda()
    return model

def get_tokenizer(special=[]):
    """选择预训练模型"""
    args = get_args()
    model_classes = get_model_classes()
    # 选择模型
    model_config = model_classes[args.model_type]
    # 选择token
    tokenizer = model_config['tokenizer'].from_pretrained(
        args.model_name_or_path,
        cache_dir=args.cache_dir if args.cache_dir else None)
    tokenizer.add_tokens(special)
    return tokenizer


#!/usr/bin/python

import math

def spectrumKLD(means1, vars1, means2, vars2):
    '''
    Calculates KLD for spectral/cepstral distributions
    '''
    lp = min(len(means1), len(means2))
    flTmp = 0.0

    for i in range(lp):
        flTmp += ((1 / vars1[i] + 1 / vars2[i]) * (means1[i] - means2[i]) * (means1[i] - means2[i]) +
                   vars1[i] / vars2[i] + vars2[i] / vars1[i]);

    return flTmp / 2 - lp

def pitchKLD( weight1, means1, vars1, weight2, means2, vars2 ):
    return ( math.log ( weight1 / weight2 * (1-weight2) / (1-weight1) )
                 * (weight1 - weight2) + 0.5 * (weight1 - weight2)
                 * math.log( vars2[0] / vars1[0] ) + 0.5 * (means1[0] - means2[0]) * (means1[0] - means2[0])
                 * (weight1 / vars2[0] + weight2 / vars1[0]) - 0.5 * (weight1 + weight2)
                 + 0.5 * ( weight1 * vars1[0] / vars2[0] + weight2 * vars2[0] / vars1[0] ) )

def bndapKLD( means1, vars1, means2, vars2 ):
    flTmp = 0.0
    for i in range(len(means1)):
        flTmp += ( (1.0 / vars1[i] + 1.0 / vars2[i]) * (means1[i] - means2[i]) * (means1[i] - means2[i])
                 + vars1[i] / vars2[i] + vars2[i] / vars1[i] )

    return flTmp / 2.0 - len(means1);

def durationKLD(mean1, var1, mean2, var2):
    return (math.log(var2 / var1) +
           (math.pow(var1, 2) + math.pow(mean1 - mean2, 2)) / (2*math.pow(var2,2))
              - 0.5
           )


"""
Return the root node of a binary search tree that matches the given preorder traversal.

(Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val.  Also recall that a preorder traversal displays the value of the node first, then traverses node.left, then traverses node.right.)

It's guaranteed that for the given test cases there is always possible to find a binary search tree with the given requirements.

Example 1:

Input: [8,5,1,7,10,12]
Output: [8,5,10,1,7,null,12]

https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/
TC:O(N)
SC:O(N)

"""

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def __init__(self):
        self.pre_index=0
        self.preorder=list()

    def helper(self, left, right):
        if self.pre_index == len(self.preorder):
            return None
        value = self.preorder[self.pre_index]

        if value<left or value>right:
            return None

        root = TreeNode(value)
        self.pre_index+=1

        root.left = self.helper(left, value)
        root.right = self.helper(value, right)

        return root

    def bstFromPreorder(self, preorder: List[int]) -> TreeNode:
        if not preorder or len(preorder)==0:
            return None

        self.pre_index=0
        self.preorder=preorder
        return self.helper(float('-inf'), float('inf'))


"""migrations

Revision ID: 045fadf58bb4
Revises: 279d71ca1ab5
Create Date: 2023-08-06 21:47:35.556911

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '045fadf58bb4'
down_revision = '279d71ca1ab5'
branch_labels = None
depends_on = None

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('surveys', sa.Column('user_id', sa.Integer(), nullable=True))
    op.create_foreign_key(None, 'surveys', 'users', ['user_id'], ['id'], ondelete='NO ACTION')
    # ### end Alembic commands ###

def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'surveys', type_='foreignkey')
    op.drop_column('surveys', 'user_id')
    # ### end Alembic commands ###


import pickle
from io import BytesIO

class SecureUnpicklerFor1337Haxx0rsOnly(pickle.Unpickler):
    def find_class(self, module: str, name: str) -> object:
        print(module, name)
        if module != '__main__':
            return "1m4o n0p3"

        if any(x in name for x in ['ex', 'ev', 'system', 'break', 'help', 'load', 'npickle']):
            return "t00 d4ng3r0u5"

        return super().find_class(module, name)

if __name__ == '__main__':
    import sys
    import pickletools
    print(dir(sys.modules['__main__'].SecureUnpicklerFor1337Haxx0rsOnly.find_class))
    payload = b'\x80\x04\x95\x1f\x00\x00\x00\x00\x00\x00\x00\x8c\x08__main__\x94\x8c\x05print\x94\x93\x94\x8c\x02ls\x94\x85\x94R\x94.'
    # data = pickle.loads(payload)
    # print(data)
    # data = bytes.fromhex(input('$ '))
    SecureUnpicklerFor1337Haxx0rsOnly(BytesIO(payload)).load()


from boggle import Boggle
from flask import Flask, request, render_template, jsonify, session

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret_key'

boggle_game = Boggle()

@app.route('/')
def display_board():
    """Shows Boggle board"""
    board = boggle_game.make_board()
    # Store the board in session
    session['board'] = board
    return render_template('board.html', board=board)

@app.route('/check-words')
def check_word():
    """Takes the form value and checks if it's a valid word in the dictionary"""
    board = session['board']
    word = request.args.get('guess')
    response = boggle_game.check_valid_word(board, word)

    return jsonify(result=response)

@app.route('/update-score', methods=['POST'])
def update_score():
    """Updates the score and times played on the server"""
    score = request.json['score']
    highscore = session.get('highscore', 0)
    times_played = session.get('times_played', 0)

    session['times_played'] = times_played +1
    session['highscore'] = max(score, highscore)

    return jsonify(highestScore =score > highscore)


import os, sys
import subprocess, random
import unicodedata

"""
20 Newsgroups (Lang, 1995): the 20
Newsgroups dataset is a benchmark topic
classification dataset, we use the publicly
available copy at http://qwone.com/
jason/20Newsgroups. There are 20 topics
in this dataset. We derived four topic
classification tasks from this dataset. The
first task is to classify documents across all
20 topics. The second task is to classify
related science documents into four science
topics (sci.crypt, sci.electronics,
sci.med, sci.med). 3 The third and
fourth tasks are talk.religion.misc
vs. alt.atheism and comp.graphics
vs. comp.windows.x. To consider a more
realistic setting, we removed header information
from each article since they often contain label
information.
"""

base = "/cab1/corpora/bayes_opt/20_newsgroups/"
original_train = base + 'original/20news-bydate-train/'
original_test = base + 'original/20news-bydate-test/'

groups = {'all_topics':['talk.religion.misc', 'comp.windows.x', 'rec.sport.baseball', 'talk.politics.mideast', 'comp.sys.mac.hardware', 'sci.space', 'talk.politics.guns', 'comp.graphics', 'comp.os.ms-windows.misc', 'soc.religion.christian', 'talk.politics.misc', 'rec.motorcycles', 'comp.sys.ibm.pc.hardware', 'rec.sport.hockey', 'misc.forsale', 'sci.crypt', 'rec.autos', 'sci.med', 'sci.electronics', 'alt.atheism'],
          'science':['sci.space', 'sci.crypt', 'sci.med', 'sci.electronics'],
          'religion':['talk.religion.misc', 'alt.atheism'],
          'comp':['comp.windows.x', 'comp.graphics']}

#sys.exit(0)

def read_one_file(f_in):
    contents = ''
    f = open(f_in, 'r')
    lines = f.readlines()

    first_header_removed = False
    second_header_removed = False
    for line in lines:
        if line == '\n':
            if not first_header_removed:
                first_header_removed = True
            elif not second_header_removed:
                second_header_removed = True
        if second_header_removed:

            line = unicode(line, errors='ignore')
            unicodedata.normalize('NFKD', line).encode('ascii', 'ignore')
            line = ''.join(ch for ch in line if unicodedata.category(ch)[0]!="C")
            contents = contents + line.replace('\n', ' ').replace('\r', ' ').replace('\t', ' ').replace('\0',' ').replace('*', '.')#.replace('\\\*','*').replace('\\*','*').replace('\*','*')

    return contents.strip().replace('"', "''").replace("\\","\\\\")

random.seed(999)

def do_stuff(original_loc, make_dev):
    for group in groups:
        #make directory
        os.system('mkdir -p ' + base + group)
        examples = {}
        for topic in groups[group]:
            for f in os.listdir(original_loc + topic):
                lines = read_one_file(original_loc + topic + '/' + f)

                examples[len(examples)] = (lines, topic)

        dev_items = sorted(random.sample(xrange(len(examples)), int(.2*len(examples))))
        if not make_dev:
            dev_items = []
        print(len(dev_items))

        if make_dev:
            train_json_out = open(base + group + '/train.json', 'w')
            train_csv_out = open(base + group + '/train.csv', 'w')
            dev_json_out = open(base + group + '/dev.json', 'w')
            dev_csv_out = open(base + group + '/dev.csv', 'w')
        else:
            train_json_out = open(base + group + '/test.json', 'w')
            train_csv_out = open(base + group + '/test.csv', 'w')

        train_json_out.write('{\n')
        train_csv_out.write('idontknow,whattoputhere\n')
        if make_dev:
            dev_csv_out.write('idontknow,whattoputhere\n')
            dev_json_out.write('{\n')

        dev_counter = 0
        train_counter = 0
        for index in examples:
            if index in dev_items:
                dev_counter = dev_counter + 1
                if index == dev_items[len(dev_items)-1]:
                    dev_json_out.write('  "' + str(dev_counter) + '": "' + examples[index][0].replace('\*','*') + '"\n')
                else:
                    dev_json_out.write('  "' + str(dev_counter) + '": "' + examples[index][0].replace('\*','*') + '",\n')
                dev_csv_out.write(str(dev_counter) + ',' + examples[index][1] + '\n')
            else:
                train_counter = train_counter + 1
                train_json_out.write('  "' + str(train_counter) + '": "' + examples[index][0].replace('\*','*') + '",\n')
                train_csv_out.write(str(train_counter) + ',' + examples[index][1] + '\n')

        train_json_out.write('}')
        if make_dev:
            dev_json_out.write('}')
        print("DON'T FORGET TO REMOVE THE EXTRA COMMA AT THE END OF TRAIN.JSON AND TEST.JSON")

        train_json_out.close()
        train_csv_out.close()
        if make_dev:
            dev_json_out.close()
            dev_csv_out.close()

do_stuff(original_train, True)
do_stuff(original_test, False)