import json
import sys
import multiprocessing as mp

import project
from src.helper.files import read_lines, write_file
from src.spelling.evaluation import TokenErrorType, longest_common_subsequence
from src.spelling.space_preference import get_token_edit_labels

def get_token_errors(sequence_id, ground_truth, corrupt_sequence):
    print(sequence_id)
    print(corrupt_sequence)
    return get_token_edit_labels(ground_truth, corrupt_sequence)

if __name__ == "__main__":
    benchmark = sys.argv[1]  # "arXiv.OCR.no_spaces"  #"ACL"
    set = "test" if "-test" in sys.argv else "development"
    n_sequences = 1000

    benchmark_dir = "/home/hertel/tokenization-repair-dumps/data/spelling/" + benchmark + "/" + set + "/"
    out_file = "/home/hertel/tr-adgit/spelling-evaluation-webapp/results/" + benchmark + "." + set + ".json"

    corrupt_paragraphs = read_lines(benchmark_dir + "corrupt.txt")[:n_sequences]
    spelling_paragraphs = read_lines(benchmark_dir + "spelling.txt")[:n_sequences]
    n_sequences = len(corrupt_paragraphs)

    if benchmark in ("ACL", "arXiv.OCR", "Wiki.typos", "Wiki.typos.no_spaces"):
        approaches = [
            "google",
            "ours+google",
            "oracle+google"
        ]
    else:
        raise Exception("unknown benchmark '%s'" % benchmark)
    if benchmark == "ACL":
        approaches.append("nastase")
    if benchmark in ("Wiki.typos", "ACL", "arXiv.OCR", "Wiki.typos.no_spaces"):
        approaches.append("google_new")
        approaches.append("ours+google_new")
        approaches.append("oracle+google_new")
    #if benchmark == "arXiv.OCR":
    #    approaches.append("BID-the-one-from-paper")

    predicted_sequences = {
        approach: read_lines(benchmark_dir + approach + ".txt") for approach in approaches
    }

    error_counts = {error: 0 for error in TokenErrorType}
    correct_prediction_counts = {approach: {error: 0 for error in TokenErrorType} for approach in approaches}
    sequences_data = []

    n_processes = max(mp.cpu_count() - 1, 1)
    with mp.Pool(n_processes) as pool:
        sequence_errors = pool.starmap(get_token_errors,
                                       list(zip(range(len(spelling_paragraphs)),
                                                spelling_paragraphs,
                                                corrupt_paragraphs)))

    for i, (corrupt, spelling) in \
            enumerate(zip(corrupt_paragraphs, spelling_paragraphs)):
        print("sequence", i)
        print(spelling)
        print(corrupt)
        #errors = get_token_edit_labels(spelling, corrupt)
        errors = sequence_errors[i]
        for error in errors:
            error_counts[error] += 1
        correct_tokens = spelling.split()
        corrupt_tokens = corrupt.split()
        matched = longest_common_subsequence(correct_tokens, corrupt_tokens)
        matched_corrupt_tokens = {n for m, n in matched}
        corrupt_labels = ["NONE" if j in matched_corrupt_tokens else "WRONG" for j in range(len(corrupt_tokens))]
        sequence_data = {
            "corrupt": {
                "tokens": corrupt_tokens,
                "labels": corrupt_labels,
            },
            "correct": {
                "tokens": correct_tokens,
                "labels": [error.name for error in errors]
            },
            "predicted": {}
        }
        for approach in approaches:
            predicted = predicted_sequences[approach][i]
            predicted_tokens = predicted.split()
            matched = longest_common_subsequence(correct_tokens, predicted_tokens)
            labels = ["WRONG" for _ in predicted_tokens]
            for m, n in matched:
                correct_prediction_counts[approach][errors[m]] += 1
                labels[n] = errors[m]
            sequence_data["predicted"][approach] = {
                "tokens": predicted_tokens,
                "labels": [label.name if isinstance(label, TokenErrorType) else label for label in labels]
            }
        sequences_data.append(sequence_data)

    total_errors = sum(error_counts[error] for error in TokenErrorType if error != TokenErrorType.NONE)
    print(error_counts)
    for approach in approaches:
        print(approach, correct_prediction_counts[approach])

    for approach in approaches:
        print("\n" + approach)
        counts = correct_prediction_counts[approach]
        for error_type in TokenErrorType:
            print(f"{error_type.name}: {counts[error_type]}/{error_counts[error_type]}")
        total_correct = sum(counts[error] for error in TokenErrorType if error != TokenErrorType.NONE)
        print(f"all: {total_correct}/{total_errors} ({total_correct / total_errors * 100:.2f}%)")

    data = {
        "total": {error.name: error_counts[error] for error in TokenErrorType},
        "correct": {approach: {error.name: correct_prediction_counts[approach][error] for error in TokenErrorType}
                    for approach in approaches},
        "sequences": sequences_data
    }
    write_file(out_file, json.dumps(data))


# sequenceReads.py
# sequenceReads.py stores and processes individual DNA sequence reads.

import sys
from genAmplicons import misc

try:
    from genAmplicons import editdist
    editdist_loaded = True
except ImportError:
    sys.stderr.write("Warning: editdist library not loaded, Insertion/Deletion detection in barcodes and primers will not be performed\n")
    editdist_loaded = False

try:
    from genAmplicons import trim
    trim_loaded = True
except ImportError:
    sys.stderr.write("Warning: trim library not loaded, trimming using python\n")
    trim_loaded = False

def barcodeDist(b_1, b_2):
    # ------------------- calculate distance between two barcode sequences ------------------------------
    """
    gets edit distnace between two equal-length barcode strings
    """
    if editdist_loaded:
        return editdist.hamming_distance(b_1, b_2)
    elif len(b_1) == len(b_2) and len(b_1) > 0:
        return sum(map(lambda x: x[0] != x[1], zip(b_1, b_2)))
    else:
        sys.stderr.write("ERROR:[barcodeDist] lengths of barcodes and index read do not match!\n")
        sys.stderr.write("Target: %s" % b_1)
        sys.stderr.write("Index read: %s" % b_2)
        raise Exception

def primerDist(primer_l, read, dedup_float, max_diff, end_match):
    # ------------------- calculate distance between primer sequence and first part of read ------------------------------
    """
    gets edit distance between primer and read sequence
    """
    pr = None
    prMismatch = max_diff+1
    prStartPosition = 0
    prEndPosition = 0

    if editdist_loaded:
        pr_i, prMismatch, prStartPosition, prEndPosition = editdist.bounded_distance_list(primer_l, read, dedup_float, max_diff, end_match)

    else:
        for i, key in enumerate(primer_l):
            read = read[0:len(key)]
            if end_match == 0:
                dist = sum(map(lambda x: x[0] != x[1], zip(read, key)))
            else:
                dist = sum(map(lambda x: x[0] != x[1], zip(read[:-end_match], key[:-end_match])))
                for i in range(len(key)-end_match, len(key)):
                    if read[i] != key[i]:
                        dist += 100
            if dist < prMismatch:
                pr_i = i
                prMismatch = dist
                prEndPosition = len(key)

    if prMismatch > max_diff:
        pr = None
        prStartPosition = 0
        prEndPosition = 0
    else:
        pr = primer_l[pr_i]

    return (pr, prMismatch, prStartPosition, prEndPosition)

class FourSequenceReadSet:
    # ---------------- Class for 4 read sequence data with double barcode set ----------------
    """
    Class to hold one Illumina four read (double barocodes) set, only one read names is used for all sequence reads
    Class processes a read by defining the barcode pair and primer pair [optional] and project [optional]. Finally
    class returns a paired read set for output
    """

    def __init__(self, name, read_1, qual_1, read_2, qual_2, bc_1, bc_2):
        """
        Initialize a FourSequenceReadSet with a name, two read sequences
        and cooresponding quality sequence and two barcode sequences. A read
        is initially defined as 'not' a good read and requires processing before
        being labeled as a good read.
        """
        self.barcode = [None, 0, 0]  # when filled, a vector of length 3 [barcodePairID, editdist1, editdist2]
        self.primer = [None, None, 0, 0, None, 0, 0]  # when filled, a vector of length 7 [primerPairID,primerID1,editdist1,readendpos1, primerID2,editdist2,readendpos2]
        self.sample = None
        self.project = None
        self.name = name
        self.read_1 = read_1
        self.qual_1 = qual_1
        self.read_2 = read_2
        self.qual_2 = qual_2
        self.bc_1 = bc_1
        self.bc_2 = bc_2
        self.trim_left = len(read_1)
        self.trim_right = len(read_2)
        self.goodRead = False

    def assignBarcode(self, bcTable, max_diff):
        """
        Given a barcodeTable object and the maximum number of allowed difference (mismatch, insertion, deletions)
        assign a barcode pair ID from the reads barcodes.
        """
        # Barcode One Matching
        bc1 = None
        bc1Mismatch = max_diff+1
        for key in bcTable.getP7():
            bcdist = barcodeDist(key, self.bc_1)
            if bcdist < bc1Mismatch:
                bc1 = key
                bc1Mismatch = bcdist
        # Barcode Two Matching
        bc2 = None
        bc2Mismatch = max_diff+1
        for key in bcTable.getP5():
            bcdist = barcodeDist(key, self.bc_2)
            if bcdist < bc2Mismatch:
                bc2 = key
                bc2Mismatch = bcdist
        # Barcode Pair Matching
        self.barcode = [bcTable.getMatch(bc1, bc2), bc1Mismatch, bc2Mismatch]
        self.goodRead = self.barcode[0] is not None
        self.sample = self.barcode[0]
        return 0

    def assignPrimer(self, prTable, max_diff, endmatch):
        """
        Given a primerTable object, the maximum number of allowed difference (mismatch, insertion, deletions) and the
        required number of end match bases (final endmatch bases must match) assign a primer pair ID from the read
        sequences.
        """
        # Barcode One Matching
        pr1 = None
        pr1Mismatch = max_diff+1
        pr1Position = 0
        for key in prTable.getP5sequences():
            prdist = primerDist(key, self.read_1, max_diff, endmatch)
            if prdist[0] < pr1Mismatch:
                pr1 = key
                pr1Mismatch = prdist[0]
                pr1Position = prdist[1]
        # Barcode Two Matching
        pr2 = None
        pr2Mismatch = max_diff+1
        pr2Position = 0
        for key in prTable.getP7sequences():
            prdist = primerDist(key, self.read_2, max_diff, endmatch)
            if prdist[0] < pr2Mismatch:
                pr2 = key
                pr2Mismatch = prdist[0]
                pr2Position = prdist[1]
        # Barcode Pair Matching
        combined_pr = prTable.getMatch(pr1, pr2)
        self.goodRead = self.goodRead and combined_pr[0] is not None and pr1Mismatch <= max_diff and pr2Mismatch <= max_diff
        self.primer = [combined_pr[0], combined_pr[1], pr2Mismatch, pr1Position, combined_pr[2], pr2Mismatch, pr2Position]
        return 0

    def assignRead(self, sTable):
        """
        Given a samplesTable object, assign a sample ID and project ID using the reads barcode and primer designation
        """
        self.project = sTable.getProjectID(self.barcode[0], self.primer[0])
        self.sample = sTable.getSampleID(self.barcode[0], self.primer[0])
        self.goodRead = self.project is not None
        return 0

    def trimRead(self, minQ, minL):
        """
        Trim the read by a minQ score
        """
        if (trim_loaded):
            trim_points = trim.trim(self.qual_1, self.qual_2, minQ)
            self.trim_left = trim_points["left_trim"]
            self.trim_right = trim_points["right_trim"]
            if ((self.trim_left-self.primer[3]) < minL or (self.trim_right-self.primer[6]) < minL):
                self.goodRead = False

    def getBarcode(self):
        """
        Return the reads barcode pair ID
        """
        return self.barcode[0]

    def getPrimer(self):
        """
        Return the reads primer pair ID
        """
        return self.primer[0]

    def getSampleID(self):
        """
        Return the reads sample ID
        """
        return self.sample

    def getProject(self):
        """
        Return the reads project ID
        """
        return self.project

    def getFastq(self, kprimer):
        """
        Create four line string ('\n' separator included) for the read pair, returning a length 2 vector (one for each read)
        """
        if self.primer[0] is not None and kprimer is False:
            read1_name = "%s 1:N:0:%s:%s %s|%s|%s|%s %s|%s|%s" % (self.name, self.sample, self.primer[0], self.bc_1, self.barcode[1], self.bc_2, self.barcode[2], self.primer[1], self.primer[2], self.primer[3])
            read2_name = "%s 2:N:0:%s:%s %s|%s|%s|%s %s|%s|%s" % (self.name, self.sample, self.primer[0], self.bc_1, self.barcode[1], self.bc_2, self.barcode[2], self.primer[4], self.primer[5], self.primer[6])
            r1 = '\n'.join([read1_name, self.read_1[self.primer[3]:self.trim_left], '+', self.qual_1[self.primer[3]:self.trim_left]])
            r2 = '\n'.join([read2_name, self.read_2[self.primer[6]:self.trim_right], '+', self.qual_2[self.primer[6]:self.trim_right]])
        else:
            read1_name = "%s 1:N:0:%s %s|%s|%s|%s" % (self.name, self.sample, self.bc_1, self.barcode[1], self.bc_2, self.barcode[2])
            read2_name = "%s 2:N:0:%s %s|%s|%s|%s" % (self.name, self.sample, self.bc_1, self.barcode[1], self.bc_2, self.barcode[2])
            r1 = '\n'.join([read1_name, self.read_1[0:self.trim_left], '+', self.qual_1[0:self.trim_left]])
            r2 = '\n'.join([read2_name, self.read_2[0:self.trim_right], '+', self.qual_2[0:self.trim_right]])
        return [r1, r2]

# ---------------- Class for 2 read sequence data processed with dbcAmplicons preprocess ----------------
class TwoSequenceReadSet:
    """
    Class to hold one Illumina two read set, assumed to have already been preprocessed with Barcodes and Primers already
    identified. Class processes a read by defining sample and project ids. Finally class returns a paired read set for output.
    """
    def __init__(self, name_1, read_1, qual_1, name_2, read_2, qual_2):
        """
        Initialize a TwoSequenceReadSet with names, two read sequences and cooresponding quality sequence.
        Barcode and primer sequence is inferred by their placement in the read names.
        A read is initially defined as 'not' a good read and requires processing before being labeled as a good read.
        """
        try:
            split_name = name_1.split(" ")
            self.name = split_name[0]
            self.barcode = split_name[1].split(":")[3]
            self.sample = self.barcode
            # dbcAmplicons processed, Primer and Barcode Identified
            if (len(split_name) == 4):
                self.primer_string1 = split_name[3]
                self.primer_string2 = name_2.split(" ")[3]
                self.primer = split_name[1].split(":")[4]
                self.barcode_string = split_name[2]
            # dbcAmplicons processed, Barcode only Identified
            elif (len(split_name) == 3):
                self.primer_string1 = None
                self.primer_string2 = None
                self.primer = None
                self.barcode_string = split_name[2]
            # Casava processed, check for barcode in the second string
            else:
                self.primer_string1 = None
                self.primer_string2 = None
                self.primer = None
                self.barcode_string = None
            self.read_1 = read_1
            self.qual_1 = qual_1
            self.read_2 = read_2
            self.qual_2 = qual_2
            self.trim_left = len(read_1)
            self.trim_right = len(read_2)
            self.goodRead = False
        except IndexError:
            sys.stderr.write('ERROR:[TwoSequenceReadSet] Read names are not formatted in the expected manner\n')
            raise
        except:
            sys.stderr.write('ERROR:[TwoSequenceReadSet] Unknown error occured initiating read\n')
            raise

    def assignBarcode(self, bcTable, max_diff):
        """
        Given a barcodeTable object and the maximum number of allowed difference (mismatch, insertion, deletions)
        assign a barcode pair ID from the reads barcodes.
        """

        # HARD CODED
        bc_1 = self.read_2[0:8]

        # Barcode One Matching
        bc1 = None
        bc1Mismatch = max_diff+1
        for key in bcTable.getP7():
            bcdist = barcodeDist(key, bc_1)
            if bcdist < bc1Mismatch:
                bc1 = key
                bc1Mismatch = bcdist
        # Barcode Two Matching
        bc2 = None
        bc2Mismatch = max_diff+1
        # for key in bcTable.getP5():
        #     bcdist = barcodeDist(key, self.bc_2)
        #     if bcdist < bc2Mismatch:
        #         bc2 = key
        #         bc2Mismatch = bcdist
        # Barcode Pair Matching
        self.barcode = [bcTable.getMatch(bc1, bc2), bc_1, bc1Mismatch, bc2Mismatch]
        self.goodRead = self.barcode[0] is not None
        self.sample = self.barcode[0]
        if self.goodRead:
            self.read_2 = self.read_2[8:]
            self.qual_2 = self.qual_2[8:]

        return 0

    def checkLinker(self, sequence, dedup_float, max_diff, endmatch):

        # Primer One Matching
        linker, linkerMismatch, linkerStartPosition, linkerEndPosition = primerDist([sequence], self.read_2, dedup_float, max_diff, endmatch)

        self.MI1 = self.read_2[0:linkerStartPosition]

        self.linker = [linker, linkerMismatch, linkerStartPosition, linkerEndPosition]
        self.goodRead = self.goodRead and linker is not None
        if self.goodRead:
            self.read_2 = self.read_2[linkerEndPosition:]
            self.qual_2 = self.qual_2[linkerEndPosition:]
            return 1
        else:
            return 0

    def assignPrimer(self, prTable, dedup_float, max_diff, endmatch):
        """
        Given a primerTable object, the maximum number of allowed difference (mismatch, insertion, deletions) and the
        required number of end match bases (final endmatch bases must match) assign a primer pair ID from the read
        sequences.
        """
        # Primer One Matching
        pr1, pr1Mismatch, pr1StartPosition, pr1EndPosition = primerDist(prTable.getP5sequences(), self.read_1, 0, max_diff, endmatch)

        # Primer One Matching
        pr2, pr2Mismatch, pr2StartPosition, pr2EndPosition = primerDist(prTable.getP7sequences(), self.read_2, 4, max_diff, endmatch)

        self.MI2 = self.read_2[0:pr2StartPosition]
        # Barcode Pair Matching
        combined_pr = prTable.getMatch(pr1, pr2)
        self.primer = [combined_pr[0], combined_pr[1], pr1Mismatch, pr1StartPosition, pr1EndPosition, combined_pr[2], pr2Mismatch, pr2StartPosition, pr2EndPosition]
        self.goodRead = self.goodRead and combined_pr[0] is not None
        if self.goodRead:
            return 1
        else:
            return 0

    def assignRead(self, sTable):
        """
        Given a samplesTable object, assign a sample ID and project ID using the reads barcode and primer designation
        """
        self.project = sTable.getProjectID(self.barcode[0], self.primer[0])
        self.sample = sTable.getSampleID(self.barcode[0], self.primer[0])
        self.goodRead = self.project is not None
        return 0

    def getBarcode(self):
        """
        Return the reads barcode pair ID
        """
        return self.barcode[0]

    def getLinker(self):
        """
        Return the reads barcode pair ID
        """
        return self.linker[0]

    def getPrimer(self):
        """
        Return the reads primer pair ID
        """
        return self.primer[0]

    def getSampleID(self):
        """
        Return the reads sample ID
        """
        return self.sample

    def getProject(self):
        """
        Return the reads project ID
        """
        return self.project

    def trimRead(self, minQ, minL):
        """
        Trim the read by a minQ score
        """
        if (trim_loaded):
            trim_points = trim.trim(self.qual_1, self.qual_2, minQ)
            self.trim_left = trim_points["left_trim"]
            self.trim_right = trim_points["right_trim"]
            if (self.trim_left < minL or self.trim_right < minL):
                self.goodRead = False

    def getFastqSRA(self):
        """
        Create four line string ('\n' separator included) for the read pair, returning a length 2 vector (one for each read)
        """
        read1_name = "%s 1:N:0:" % (self.name)
        read2_name = "%s 2:N:0:" % (self.name)
        r1 = '\n'.join([read1_name, self.read_1[0:self.trim_left], '+', self.qual_1[0:self.trim_left]])
        r2 = '\n'.join([read2_name, self.read_2[0:self.trim_right], '+', self.qual_2[0:self.trim_right]])
        return [r1, r2]

    def getFastq(self):
        """
        Create four line string ('\n' separator included) for the read pair, returning a length 2 vector (one for each read)
        """
        if self.primer is not None:
            read1_name = "%s 1:N:0:%s:%s %s %s" % (self.name, self.sample, self.primer, self.barcode_string, self.primer_string1)
            read2_name = "%s 2:N:0:%s:%s %s %s" % (self.name, self.sample, self.primer, self.barcode_string, self.primer_string2)
        else:
            read1_name = "%s 1:N:0:%s %s" % (self.name, self.sample, self.barcode_string)
            read2_name = "%s 2:N:0:%s %s" % (self.name, self.sample, self.barcode_string)
        r1 = '\n'.join([read1_name, self.read_1[0:self.trim_left], '+', self.qual_1[0:self.trim_left]])
        r2 = '\n'.join([read2_name, self.read_2[0:self.trim_right], '+', self.qual_2[0:self.trim_right]])
        return [r1, r2]

    def getFastq2(self, kprimer):
        """
        Create four line string ('\n' separator included) for the read pair, returning a length 2 vector (one for each read)
        """
        if self.primer[0] is not None and kprimer is False:
            read1_name = "%s*%s|%s*%s|%s|%s*%s|%s|%s|%s*%s|%s 1:N:0:None" % (self.name, self.sample, self.primer[0], self.barcode[0], self.barcode[1], self.barcode[2], self.primer[1], self.primer[2], self.primer[5], self.primer[6], self.MI1, self.MI2)
            read2_name = "%s*%s|%s*%s|%s|%s*%s|%s|%s|%s*%s|%s 2:N:0:None" % (self.name, self.sample, self.primer[0], self.barcode[0], self.barcode[1], self.barcode[2], self.primer[1], self.primer[2], self.primer[5], self.primer[6], self.MI1, self.MI2)
            r1 = '\n'.join([read1_name, self.read_1[self.primer[4]:self.trim_left], '+', self.qual_1[self.primer[4]:self.trim_left]])
            r2 = '\n'.join([read2_name, self.read_2[self.primer[8]:self.trim_right], '+', self.qual_2[self.primer[8]:self.trim_right]])
        else:
            read1_name = "%s*%s*%s|%s|%s|%s*%s|%s 1:N:0:None" % (self.name, self.sample, self.barcode[0], self.barcode[1], self.barcode[2], self.MI1, self.MI2)
            read2_name = "%s*%s*%s|%s|%s|%s*%s|%s 2:N:0:None" % (self.name, self.sample, self.barcode[0], self.barcode[1], self.barcode[2], self.MI1, self.MI2)
            r1 = '\n'.join([read1_name, self.read_1[0:self.trim_left], '+', self.qual_1[0:self.trim_left]])
            r2 = '\n'.join([read2_name, self.read_2[0:self.trim_right], '+', self.qual_2[0:self.trim_right]])
        return [r1, r2]

    def getFourReads(self, bc1_length=8, bc2_length=8):
        """
        Create four line string ('\n' separator included) for the read set, returning a length 4 vector (one for each read)
        """
        try:
            if self.barcode_string is not None:
                bc1 = self.barcode_string.split('|')[0]
                bc2 = self.barcode_string.split('|')[2]
            elif len(self.barcode) is (bc1_length+bc2_length):
                bc1 = self.barcode[:bc1_length]
                bc2 = self.barcode[bc1_length:]
            else:
                raise Exception("string in the barcode is not %s characters" % str(bc1_length+bc2_length))
            r1 = '\n'.join([self.name + ' 1:Y:0:', self.read_1[0:self.trim_left], '+', self.qual_1[0:self.trim_left]])
            r2 = '\n'.join([self.name + ' 2:Y:0:', bc1, '+', 'C' * len(bc1)])  # Give barcodes and arbitary quality of Cs
            r3 = '\n'.join([self.name + ' 3:Y:0:', bc2, '+', 'C' * len(bc2)])
            r4 = '\n'.join([self.name + ' 4:Y:0:', self.read_2[0:self.trim_right], '+', self.qual_2[0:self.trim_right]])
            return [r1, r2, r3, r4]
        except IndexError:
            sys.stderr.write('ERROR:[TwoSequenceReadSet] unable to exract barocode sequence from the read names\n')
            raise
        except:
            sys.stderr.write('ERROR:[TwoSequenceReadSet] Unknown error occured generating four read set\n')
            raise

    def getFasta(self):
        """
        Create two line string ('\n' separator included) for the read pair, returning a length 1 vector (one read)
        """
        name = '>' + self.name[1:]
        if self.primer is not None:
            read1_name = "%s 1:N:0:%s:%s:%i" % (name, self.sample, self.primer, len(self.read_1))
            read2_name = "%s 2:N:0:%s:%s:%i" % (name, self.sample, self.primer, len(self.read_2))
        else:
            read1_name = "%s 1:N:0:%s:%i" % (name, self.sample, len(self.read_1))
            read2_name = "%s 2:N:0:%s:%i" % (name, self.sample, len(self.read_2))
        r1 = '\n'.join([read1_name, self.read_1[0:self.trim_left]])
        r2 = '\n'.join([read2_name, self.read_2[0:self.trim_right]])
        return [r1, r2]

    def getJoinedFasta(self):
        """
        Create two line string ('\n' separator included) for the read pair, concatenating the two reads into a single returning length 1 vector (one read)
        """
        name = '>' + self.name[1:]
        joined_seq = self.read_1[0:self.trim_left] + misc.reverseComplement(self.read_2[0:self.trim_right])
        if self.primer is not None:
            read1_name = "%s|%s:%s:PAIR" % (name, self.sample, self.primer)
        else:
            read1_name = "%s|%s:PAIR" % (name, self.sample)
        r1 = '\n'.join([read1_name, joined_seq])
        return [r1]

# ---------------- Class for 2 read sequence data processed with dbcAmplicons preprocess ----------------
class OneSequenceReadSet:
    """
    Class to hold a one Illumina read set, assumes the paired reads produced by dbcAmplicons preprocess have been merged
    """
    def __init__(self, name_1, read_1, qual_1):
        """
        Initialize a OneSequenceReadSet with name, one read sequences and cooresponding quality sequence.
        A read is initially defined as 'not' a good read and requires processing before being labeled as a good read.
        """
        self.goodRead = False
        self.primer = None
        # parse the read name for the sample and primer ids
        try:
            split_name = name_1.split(" ")
            self.name = split_name[0]
            self.sample = split_name[1].split(":")[3]
            if (len(split_name) == 4):
                self.primer = split_name[1].split(":")[4]
        except IndexError:
            sys.stderr.write('ERROR:[OneSequenceReadSet] Read names are not formatted in the expected manner\n')
            raise
        except:
            sys.stderr.write('ERROR:[OneSequenceReadSet] Unknown error occured initiating read\n')
            raise
        self.read_1 = read_1
        self.qual_1 = qual_1

    def getFastqSRA(self):
        """
        Create four line string ('\n' separator included) for the read pair, returning a length 2 vector (one for each read)
        """
        read1_name = "%s 1:N:0:" % (self.name)
        r1 = '\n'.join([read1_name, self.read_1, '+', self.qual_1])
        return [r1]

    def getFastq(self):
        """
        Create four line string ('\n' separator included) for the read, returning a length 1 vector (one read)
        """
        if self.primer is not None:
            read1_name = "%s 1:N:0:%s:%s" % (self.name, self.sample, self.primer)
        else:
            read1_name = "%s 1:N:0:%s" % (self.name, self.sample)
        r1 = '\n'.join([read1_name, self.read_1, '+', self.qual_1])
        return [r1]

    def getFasta(self):
        """
        Create two line string ('\n' separator included) for the read, returning a length 1 vector (one read)
        """
        name = '>' + self.name[1:]
        if self.primer is not None:
            read1_name = "%s|%s:%s:%i" % (name, self.sample, self.primer, len(self.read_1))
        else:
            read1_name = "%s|%s:%i" % (name, self.sample, len(self.read_1))
        r1 = '\n'.join([read1_name, self.read_1])
        return [r1]


"""
Bot 6 - A full-featured deep Q-learning agent.
"""

import argparse
import pickle
from pathlib import Path
import cv2
import gym
import numpy as np
import random
import tensorflow as tf
import time
# import tensorflow.compat.v1 as tf
# tf.disable_v2_behavior()
from a3c import a3c_model

from util.time_utils import format_time

total_begin_time = time.time()

num_episodes = 100

def downsample(state):
    """将输入的状态中的帧缩放到 84x84 的大小。
    """
    return cv2.resize(state, (84, 84), interpolation=cv2.INTER_LINEAR)[None]

def main():
    # 解析命令行参数
    parser = argparse.ArgumentParser(description='Run DQN agent.')
    parser.add_argument('--model', type=str, help='Path to model', default='models/SpaceInvaders-v0.tfmodel')
    parser.add_argument('--visual', action='store_true')
    args = parser.parse_args()

    # 创建环境
    env = gym.make('SpaceInvaders-v0')

    # 回报列表
    episode_reward_array = np.zeros(num_episodes, dtype=float)

    # 构建用于训练的模型，此处是一个 A3C 模型
    model = a3c_model(load=args.model)

    # 训练循环，每一次迭代就是一个 episode
    for episode_index in range(num_episodes):
        # 每个 episode 刚开始的时候，重置环境和相关数据
        episode_reward = 0
        state_list = [downsample(env.reset())]

        # 循环直至一个 episode 结束
        # 每一轮循环 agent 会执行一次 action，即一个 step
        while True:
            if len(state_list) < 4:
                action = env.action_space.sample()
            else:
                # 沿着第三维（RGB）进行合并，相当于是将过去最新的四帧沿着 RGB 的那一维合并。
                frames = np.concatenate(state_list[-4:], axis=3)
                # print("frames.shape:", frames.shape) # (1, 84, 84, 12)
                action = np.argmax(model([frames]))

            # 如果需要 GUI 可视化
            if args.visual:
                env.render()

            # 让 agent 在环境中执行动作
            # state: 执行之后 agent 所处的状态。对于该环境 state 的类型是 numpy.ndarray，形状为 (210, 160, 3)，第三维为 RGB。
            # reward: agent 获得的立即回报
            # done: 表示当前 episode 是否结束的指示变量（bool）
            state, reward, done, _ = env.step(action)
            # print("type(state):", type(state)) # numpy.ndarray
            # print(state.shape) # (210, 160, 3)

            # 记录数据
            state_list.append(downsample(state))
            # print("type(state_list[-1]):", type(state_list[-1])) # numpy.ndarray
            # print(state_list[-1].shape) # (1, 84, 84, 3)
            episode_reward += reward

            # 如果当前 episode 已经结束
            if done:
                print('Episode reward: %d' % episode_reward)
                # reward_list.append(episode_reward)
                episode_reward_array[episode_index] = episode_reward

                break
    print('Average episode reward: %.2f' % (sum(episode_reward_array) / episode_reward_array.shape[0]))

    # Persist the results
    results_path = Path("./results/dqn")
    results_path.mkdir(parents=True, exist_ok=True)
    results_file = results_path.joinpath("results.pkl")
    results = { "episode_reward_array": episode_reward_array }
    with open(results_file, "wb") as f:
        pickle.dump(results, f)
        print("The results have been persisted into the file \"{:s}\".".format(str(results_file)))

    total_end_time = time.time()
    print("Total time usage: {:s}.".format(format_time(total_end_time - total_begin_time)))

if __name__ == '__main__':
    main()


import re

from domain.src.exceptions.email_register_exception import EmailRegisterException
from domain.src.object_values.user_email_interface import UserEmailInterface

class UserUserEmail(UserEmailInterface):
    def __init__(self, email):
        self.regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
        if not self.__validate(email):
            raise EmailRegisterException()
        self.email: str = email

    def __validate(self, email: str) -> bool():
        if not re.search(self.regex, email):
            return False
        return True

    def get_email(self) -> str:
        return self.email


def threeSum(nums):
    nums.sort()

    nums_len = len(nums)

    triplets_lst = []
    for i in range(nums_len-2):
        l = i+1
        r = nums_len-1
        currSum = 0
        while l < r:
            currSum = nums[i] + nums[l] + nums[r]
            if currSum == 0:
                triplets_lst.append([nums[i], nums[l], nums[r]])
                l += 1
                r -= 1
            elif currSum < 0:
                l += 1
            else:
                r -= 1
    return set([tuple(x) for x in triplets_lst])

nums = [-1, 0, 1, 2, -1, -4]
print(threeSum(nums))


import math

def karatsuba_multiplication(x: int, y: int) -> int:
    """Takes two integers, multiplies them and returns the result as an integer.
    Multiplication is computed using the karatsuba algorithm.
    See https://en.wikipedia.org/wiki/Karatsuba_algorithm for more details on the algorithm

    Args:
        x : First integer to multiply
        y : Second integer to multiply

    Returns:
        The result of the multiplication
    """
    n_x = len(str(x))
    n_y = len(str(y))

    if n_x == 1 or n_y == 1:
        return x * y

    n = max(n_x, n_y)

    # Splitting both x and y into two n/2-digit numbers so that multiplications
    # can be recursively computed.
    n2 = n // 2

    first_half_x = x // (10 ** (n2))
    second_half_x = x % (10 ** (n2))

    first_half_y = y // 10 ** (n2)
    second_half_y = y % (10 ** (n2))

    first_halves_multiplication = karatsuba_multiplication(first_half_x, first_half_y)
    second_halves_multiplication = karatsuba_multiplication(
        second_half_x, second_half_y
    )
    sum_multiplication = karatsuba_multiplication(
        first_half_x + second_half_x, first_half_y + second_half_y
    )

    intermediate_numbers = (
        sum_multiplication - first_halves_multiplication - second_halves_multiplication
    )

    return (
        (10 ** (2 * n2)) * round(first_halves_multiplication)
        + (10 ** (n2)) * round(intermediate_numbers)
        + round(second_halves_multiplication)
    )


# Practice : check outlier
# label -> 3,4 :0, 5,6,7 :1, 8,9 :2 change
# 라벨 수정 권한 존재할 시에만 가능함

from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
import warnings
warnings.filterwarnings("ignore", category=np.VisibleDeprecationWarning)

# 1. data
datasets = pd.read_csv('../_data/winequality-white.csv', sep=';',
                       index_col=None, header=0 ) # (4898, 12)

datasets = datasets.values

x = datasets[:,0:11] # (4898, 11)
y = datasets[:,[11]] # (4898, 10)

# 1-1. y data label change
newlist = []
for i in list(y):
    if i <= 4:
        newlist += [0]
    elif i <= 7:
        newlist += [1]
    else:
        newlist += [2]

y = np.array(newlist) # (4898,)

# print(y.shape)

x_train, x_test, y_train, y_test = train_test_split(x, y,
      test_size=0.2, shuffle=True, random_state=66)

from sklearn.preprocessing import MinMaxScaler, StandardScaler, RobustScaler, MaxAbsScaler, QuantileTransformer, PowerTransformer
scaler = RobustScaler()
scaler.fit(x_train)
x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)

# 2. model
from xgboost import XGBClassifier, XGBRegressor

model = XGBClassifier(n_jobs=-1)

# 3. train
model.fit(x_train, y_train)

# 4. eval pred
score = model.score(x_test, y_test)
print("acc : ", score)

'''
acc :  0.9469387755102041
'''


# advent of code 2015, day 1
import re
from typing import Dict

OUTSIDE_QUOTE = 0
IN_QUOTE = 1
AFTER_FIRST_BACKSLASH = 2
IN_HEX_ESCAPE = 3

def gen_encoded_string(line: str) -> int:
    for c in line:
        if c == '"':
            yield '\\"'
        elif c == '\\':
            yield '\\\\'
        else:
            yield c

def encoded_length_minus_string_length(line: str) -> int:
    return sum(len(x) for x in gen_encoded_string(line)) + 2 - len(line)

def main():
    with open("input/day8.txt", "r") as f:
        print(sum(encoded_length_minus_string_length(line.rstrip()) for line in f))

if __name__ == "__main__":
    main()


"""
this script profiles the ocrd.Workspace.remove_file_group() function
by inspecting several images.
To be executed from within the 'medium_workspace' dir.
"""

from ocrd import Workspace, Resolver
from ocrd_models import OcrdMets, OcrdFile
from lxml import etree as ET
import cProfile, pstats
import shutil, os

NS = {"mets": "http://www.loc.gov/METS/"}

resolver = Resolver()
dir = "."
ws = Workspace(resolver, dir)

tree = ET.parse('mets.xml')
root = tree.getroot()

USES = []

for element in root.findall('.//mets:fileGrp[@USE]', NS):
    attrib_dict = element.attrib
    USES.append(attrib_dict.get("USE"))

profiler = cProfile.Profile()
profiler.enable()

for USE in USES:
    ws.remove_file_group(USE, recursive=True)

profiler.disable()
stats = pstats.Stats(profiler).sort_stats('tottime')
stats.dump_stats('../results/remove_file_group.prof')


from django.shortcuts import render
from django.shortcuts import HttpResponse
from django.http import JsonResponse
from django.http import FileResponse
from django.utils.encoding import escape_uri_path

import os
import json

HonCloud = 'static/cloudfiles'
FileTypeMap = {
            'md': {'typen': 'Markdown文档', 'ctype': 'text/plain',},
            'zip': {'typen': '压缩文件', 'ctype': 'application/x-zip-compressed',},
            'rar': {'typen': '压缩文件', 'ctype': 'application/octet-stream'},
            '7z': {'typen': '压缩文件', 'ctype': 'text/plain',},
            'exe': {'typen': '应用程序', 'ctype': 'application/x-msdownload',},
            'ino': {'typen': 'Arduino源码', 'ctype': 'text/plain',},
            'tif': {'typen': '图片', 'ctype': 'image/tiff',},
            'png': {'typen': '图片', 'ctype': 'image/png',},
            'txt': {'typen': '文本文档', 'ctype': 'text/plain',},
            'pptx': {'typen': 'PowerPoint演示文档', 'ctype': 'application/octet-stream',},
            'docx': {'typen': 'Word文档', 'ctype': 'application/msword',},
            'py': {'typen': 'Python源码', 'ctype': 'application/octet-stream',},
            'asm': {'typen': '汇编源码', 'ctype': 'application/octet-stream',},
        }

def index(request):
    return render(request, '404.html')

def honcloud(request):
    return render(request, 'honcloud.html')

def upload(request):
    obj = request.FILES.get('wj')

    try:
        on = obj.name
        # print(on)
    except AttributeError:
        return render(request, 'back.html', context={'ps': '主人您还没有选择文件哦～'})

    fn = f'{HonCloud}/{on}'
    with open(fn, 'wb') as f:
        for line in obj.chunks():
            f.write(line)

    return render(request, 'back.html', context={'ps': '上传成功'})

def fsuffix(fn, mode):
    suffix = fn[fn.rfind('.')+1:]
    if mode == 0:
        return FileTypeMap[suffix]['typen'] if suffix in FileTypeMap else suffix
    if mode == 1:
        return FileTypeMap[suffix]['ctype'] if suffix in FileTypeMap else 'application/octet-stream'

def honcloudd(request):
    files = sorted(os.listdir(HonCloud))
    try:
        key = request.GET['keyWord']
        files = [{'name': x, 'type': fsuffix(x, 0)} for x in files if key in x]
    except:
        files = [{'name': x, 'type': fsuffix(x, 0)} for x in files]
    ct = {
        'listName': 'GHOST Cloud',
        'filePath': HonCloud,
        'files': files,
    }
    # return render(request, 'filelist.html', context=ct)
    return render(request, 'honcloudd.html', context=ct)

def download(request):
    fn = request.GET.get('fn')
    file = open(f'{HonCloud}/{fn}','rb')
    response = FileResponse(file)
    # print(f'fn = {fn}')
    response['Content-Type']=fsuffix(fn, 1)
    response['Content-Disposition'] = f'attachment;filename={escape_uri_path(fn)}'
    return response

# 配置异常页面
def page_400(request, e):
    return render(request, '404.html', status=400)

def page_403(request, e):
    return render(request, '404.html', status=403)

def page_404(request, e):
    return render(request, '404.html', status=404)

def page_500(request):
    return render(request, '404.html', status=500)


import requests
import re
camera_ip = "ENTER YOUR CAMERA IP HERE"
camera_cgi = camera_ip +"/cgi-bin/ldc.cgi"

# Sends packet to Furbo camera with you code injected into the x_value parameter
def send_request(x_value):
    url = camera_cgi

    headers = {
        'Authorization': 'Digest username="admin", realm="ycam.com", nonce="305c6990bf6f729492d06a297dd65dbc", uri="/cgi-bin/ldc.cgi?STRENGTH=0&X=1000&Y=540&zoom_num=1000&zoom_denum=1980&mode=1&pano_h_fov=0", response="f0759f665363934e981bd4e3ac5a1bef", qop=auth, nc=00000005, cnonce="1c297d4064f6f2b7"',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.5563.65 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest',
        'Referer': camera_ip + '/',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'en-US,en;q=0.9'
    }

    params = {
        'STRENGTH': '0',
        'X': f';export PATH=/usr/local/bin/:/bin:/sbin:$PATH;{x_value};',
        'Y': '540',
        'zoom_num': '1000',
        'zoom_denum': '1980',
        'mode': '1',
        'pano_h_fov': '0'
    }

    # Get resposne
    response = requests.get(url, headers=headers, params=params)
    # print(response.text)

    ### Filter packet response to make it look like terminal
    # remove "unknown option found: 63" and everything after it
    text = re.sub(r'unknown option found: 63.*', '', response.text)
    # remove everything after "/usr/local/bin/test_ldc"
    text = re.sub(r'/usr/local/bin/test_ldc.*', '', text)
    print(text)

while(True):
    request = input(": ")
    send_request(request)

# fb_led.sh blue
# fb_night.sh
# fb_toss_loop.sh 1


# -*- coding: utf-8 -*-
"""
Cross Entropy function
"""
import numpy as np

# We write a function that takes as input two lists Y, P,
# and returns the float corresponding to their cross-entropy.
def cross_entropy(Y, P):
    ce = 0
    for i in xrange(len(Y)):
        if Y[i] == 1:
            ce1 = - np.log(P[i])
        else:
            ce1 = - np.log(1-P[i])
        ce += ce1
    return ce

Y=[1,0,1,1]
P=[0.4,0.6,0.1,0.5]
cross_entropy(Y, P)

#The correct answer is 4.8283137373
#And your code returned 4.8283137373

# Another SOLUTION
import numpy as np

def cross_entropy(Y, P):
    Y = np.float_(Y)
    P = np.float_(P)
    return -np.sum(Y * np.log(P) + (1 - Y) * np.log(1 - P))


import json
from tqdm import tqdm
from transformers import AutoTokenizer
import numpy as np
import random
answer_prefix_suffix = [' ', '"', ',', '(', ')', '.', "'", ';', '-', "”", "/", "«", "»", "—", "]", "[",
                        "–", "#", '“', "‘", " ", "„", ">", "*", ":", "$", "’", "?", "，", "<", "!", "−", "s", "n"]

# prepare data of stepwise QA for 2WikiMultiHopQA
def preprocess_2wiki_step_data(is_train=0):
    if is_train==0:
        source_file = "../Data/2WikiMultiHopQA/train.json"
        selected_paras_file = "../Data/2WikiMultiHopQA/processed/relevant_paras_train.json"
        write_file = "../Data/2WikiMultiHopQA/processed/step_selected_processed_train_notitle.json"
    elif is_train==1:
        source_file = "../Data/2WikiMultiHopQA/dev.json"
        selected_paras_file = "../Data/2WikiMultiHopQA/processed/relevant_paras_dev.json"
        write_file = "../Data/2WikiMultiHopQA/processed/step_selected_processed_dev_notitle.json"
    else:
        source_file = "../Data/2WikiMultiHopQA/test.json"
        selected_paras_file = "../Data/2WikiMultiHopQA/processed/relevant_paras_test.json"
        write_file = "../Data/2WikiMultiHopQA/processed/step_selected_processed_test_notitle.json"

    no_match_num = 0
    more_than_one_num = 0

    with open(selected_paras_file, "r") as r_f:
        selected_paras_data = json.load(r_f)

    with open(source_file, "r") as source_f:
        original_data = json.load(source_f)

        print(len(original_data))

        for n in tqdm(range(len(original_data))):
            context_dict = {each[0]: each[1] for each in original_data[n]["context"]}
            cur_selected_paras_data = selected_paras_data[original_data[n]["_id"]]

            gold_paras = dict()
            gold_paras_titles_order = []
            for each in original_data[n]["supporting_facts"]:
                if each[0] not in gold_paras:
                    gold_paras[each[0]] = [each[1]]
                else:
                    gold_paras[each[0]].append(each[1])

                if each[0] not in gold_paras_titles_order:
                    gold_paras_titles_order.append(each[0])

            sp_sent_first_labels = []
            sp_sent_second_labels = []
            sp_sent_third_labels = []
            sp_sent_forth_labels = []
            sent_id_to_paras = []
            selected_context = "yes no "
            true_range = []
            false_range = []

            if len(gold_paras_titles_order) == 2:
                first_end = 0
                second_end = 1
                third_end = -1
                forth_end = -1
            else:
                # assert len(gold_paras_titles_order) == 4
                first_end = 0
                second_end = 0
                third_end = 0
                forth_end = 1

            original_data[n]['end_label'] = [first_end, second_end, third_end, forth_end]

            for each in cur_selected_paras_data:
                if is_train==0 and each not in gold_paras:
                    false_range_start = len(selected_context)

                selected_context += "[SEP] "

                for sent_id, each_sent in enumerate(context_dict[each]):
                    true_range_start, true_range_end = len(selected_context), len(selected_context)
                    selected_context += "[unused1] "+ each_sent.strip() + " "

                    if each in gold_paras:
                        if sent_id in gold_paras[each]:
                            true_range_end = len(selected_context)

                    if is_train < 2:
                        if each == gold_paras_titles_order[0]:
                            sp_sent_second_labels.append(0)
                            sp_sent_third_labels.append(0)
                            sp_sent_forth_labels.append(0)

                            if sent_id in gold_paras[each]:
                                sp_sent_first_labels.append(1)
                            else:
                                sp_sent_first_labels.append(0)
                        elif each == gold_paras_titles_order[1]:
                            sp_sent_first_labels.append(0)
                            sp_sent_third_labels.append(0)
                            sp_sent_forth_labels.append(0)

                            if sent_id in gold_paras[each]:
                                sp_sent_second_labels.append(1)
                            else:
                                sp_sent_second_labels.append(0)
                        else:
                            if len(gold_paras_titles_order) == 2:
                                sp_sent_first_labels.append(0)
                                sp_sent_second_labels.append(0)
                                sp_sent_third_labels.append(0)
                                sp_sent_forth_labels.append(0)
                            else:
                                # assert len(gold_paras_titles_order) == 4
                                if each == gold_paras_titles_order[2]:
                                    sp_sent_first_labels.append(0)
                                    sp_sent_second_labels.append(0)
                                    sp_sent_forth_labels.append(0)

                                    if sent_id in gold_paras[each]:
                                        sp_sent_third_labels.append(1)
                                    else:
                                        sp_sent_third_labels.append(0)
                                elif each == gold_paras_titles_order[3]:
                                    sp_sent_first_labels.append(0)
                                    sp_sent_second_labels.append(0)
                                    sp_sent_third_labels.append(0)

                                    if sent_id in gold_paras[each]:
                                        sp_sent_forth_labels.append(1)
                                    else:
                                        sp_sent_forth_labels.append(0)
                                else:
                                    sp_sent_first_labels.append(0)
                                    sp_sent_second_labels.append(0)
                                    sp_sent_third_labels.append(0)
                                    sp_sent_forth_labels.append(0)

                    sent_id_to_paras.append([each, sent_id])
                    true_range += list(range(true_range_start, true_range_end))

                if is_train==0 and each not in gold_paras:
                    false_range_end = len(selected_context)
                    false_range += list(range(false_range_start, false_range_end))

            original_data[n]["selected_context"] = selected_context

            original_data[n]["sp_sent_first_labels"] = sp_sent_first_labels
            original_data[n]["sp_sent_second_labels"] = sp_sent_second_labels
            original_data[n]["sp_sent_third_labels"] = sp_sent_third_labels
            original_data[n]["sp_sent_forth_labels"] = sp_sent_forth_labels
            original_data[n]["sent_id_to_paras"] = sent_id_to_paras

            first_supporting_facts = list()
            if is_train < 2:
                for each_first_id in gold_paras[gold_paras_titles_order[0]]:
                    first_supporting_facts.append([str(gold_paras_titles_order[0]), each_first_id])

            second_supporting_facts = list()
            if is_train < 2:
                for each_second_id in gold_paras[gold_paras_titles_order[1]]:
                    second_supporting_facts.append([str(gold_paras_titles_order[1]), each_second_id])

            third_supporting_facts = list()
            forth_supporting_facts = list()
            if len(gold_paras_titles_order) > 2:
                for each_third_id in gold_paras[gold_paras_titles_order[2]]:
                    third_supporting_facts.append([str(gold_paras_titles_order[2]), each_third_id])

                for each_forth_id in gold_paras[gold_paras_titles_order[3]]:
                    forth_supporting_facts.append([str(gold_paras_titles_order[3]), each_forth_id])

            original_data[n]["first_supporting_facts"] = first_supporting_facts
            original_data[n]["second_supporting_facts"] = second_supporting_facts
            original_data[n]["third_supporting_facts"] = third_supporting_facts
            original_data[n]["forth_supporting_facts"] = forth_supporting_facts

            if is_train==0:
                if original_data[n]["answer"] != "yes" and original_data[n]["answer"] != "no":
                    answer = original_data[n]["answer"]

                    if answer not in selected_context:
                        no_match_num += 1
                        original_data[n]["answer_start"] = -1
                    else:
                        answer_start = selected_context.find(answer)
                        find_true_answer_pos = False
                        while answer_start >= 0:
                            if answer_start in false_range:
                                answer_start = selected_context.find(answer, answer_start+1)
                            else:
                                if answer_start != 0 and answer_start + len(answer) < len(selected_context) and \
                                        selected_context[answer_start-1] not in answer_prefix_suffix and selected_context[answer_start+len(answer)] not in answer_prefix_suffix:
                                    answer_start = selected_context.find(answer, answer_start + 1)
                                elif answer_start != 0 and selected_context[answer_start-1] not in answer_prefix_suffix:
                                    answer_start = selected_context.find(answer, answer_start + 1)
                                elif answer_start + len(answer) < len(selected_context) and selected_context[answer_start+len(answer)] not in answer_prefix_suffix:
                                    print(answer)
                                    print(selected_context[answer_start: answer_start + len(answer) + 1],
                                          str(selected_context[answer_start+len(answer)] in answer_prefix_suffix) +
                                          selected_context[answer_start+len(answer)])
                                    print("$" * 80)
                                    answer_start = selected_context.find(answer, answer_start + 1)
                                else:
                                    find_true_answer_pos = True
                                    break

                        if find_true_answer_pos:
                            original_data[n]["answer_start"] = answer_start
                        else:
                            no_match_num += 1
                            original_data[n]["answer_start"] = -1

                else:
                    if original_data[n]["answer"] == "yes":
                        original_data[n]["answer_start"] = 0
                    elif original_data[n]["answer"] == "no":
                        original_data[n]["answer_start"] = 4

        print(no_match_num, more_than_one_num)

    with open(write_file, "w") as w_f:
        json.dump(original_data, w_f)

if __name__ == "__main__":
    preprocess_2wiki_step_data(is_train=0)
    preprocess_2wiki_step_data(is_train=1)
    preprocess_2wiki_step_data(is_train=2)


from workers.data_scraper.scraper_dormitory.parser_tools.tools import *

def post_list_parsing_process(**params):
    target_key_info = {
        'multiple_type' : ['uploaded_time', 'post_url', 'post_title']
    }
    var, soup, key_list, _ = html_type_default_setting(params, target_key_info)
    table = extract_children_tag(soup, 'table', child_tag_attrs={'class':'bg_bl'})
    tr_list = extract_children_tag(table, 'tr', is_child_multiple=True)
    if tr_list :
        tr_list = tr_list[3:]
    else :
        return
    for tr in tr_list :
        td_list = extract_children_tag(tr, 'td', is_child_multiple=True)
        for td_idx, td in enumerate(td_list) :
            if td_idx == 1:
                a_tag = extract_children_tag(td, 'a')
                href = extract_attrs(a_tag, 'href')
                var['post_title'].append(
                    extract_text(a_tag)
                )
                var['post_url'].append(
                    var['channel_main_url'] + href
                )
            elif td_idx == 2:
                var['uploaded_time'].append(
                    convert_datetime_string_to_isoformat_datetime(extract_text(td))
                )
    result = merge_var_to_dict(var=var, key_list=key_list)
    return result

def post_content_parsing_process(**params):
    target_key_info = {
        'single_type' : ['post_text', 'contact', 'view_count'],
        'multiple_type' : ['post_image_url']
    }
    var, soup, key_list, _ = html_type_default_setting(params, target_key_info)
    li_list = extract_children_tag(soup, 'li', is_child_multiple=True)
    for li in li_list:
        li_text = extract_text(li)
        if '조회수' in li_text :
            var['view_count'] = extract_numbers_in_text(li_text)
            break
    new_contents_common = extract_children_tag(soup, 'div', child_tag_attrs={'class':'new_contents_common'})
    tmp_contents = extract_children_tag(new_contents_common, 'td', child_tag_attrs={'class':'lnb2'})
    var['post_text'] = extract_text(tmp_contents)
    var['contact'] = extract_contact_numbers_from_text(extract_text(tmp_contents))
    var['post_image_url'] = search_img_list_in_contents(tmp_contents, var['channel_main_url'])
    result = convert_merged_list_to_dict(key_list, var)
    return result


from datetime import datetime
import backtrader as bt# import backtrader.feeds as btfeeds
import json,os,sys
import requests
import yfinance as yf
import backtrader.feeds as btfeed
from idc import *
# os.environ["http_proxy"] = "http://127.0.0.1:10809"
# os.environ["https_proxy"] = "http://127.0.0.1:10809"#7890

# Create a subclass of Strategy to define the indicators and logic

class TestStrategy(bt.Strategy):
    params = dict(
        pfast=8,
        pslow=20
    )

    def __init__(self):
        self.bbands = bt.ind.BollingerBands()
        sma0 = bt.ind.SMA(period=1)

        # self.crossover = bt.ind.CrossOver(sma0, self.bbands.lines.top)
        # self.crossover_down = bt.ind.CrossOver(sma0, self.bbands.lines.bot)

    def next(self):
        if not self.position:
        # if True:
            if self.data.high < self.bbands.lines.bot :
                self.buy()
        if self.position:
            if self.data.low > self.bbands.lines.top :
                self.close()

cerebro = bt.Cerebro()  # create a "Cerebro" engine instance
start_date = '2021-10-01'
symbol = 'TSLA'
# df = yf.download(symbol, start = start_date)
if len(sys.argv)>2:
	df = get_bar_min_tdd(sys.argv[1], int(sys.argv[2]))
else:
	df = get_bar_min_tdd(symbol, 3)

today=datetime.today().strftime('%Y-%m-%d')
print(today, '\t', symbol)

if df is None:
	print('df is None')
	sys.exit()
feed = bt.feeds.PandasData(dataname=df)
cerebro.adddata(feed)
cerebro.addstrategy(TestStrategy)  # Add the trading strategy
cerebro.broker.setcommission(commission = 0.005)
cerebro.addsizer(bt.sizers.PercentSizer, percents=50)
cerebro.addanalyzer(bt.analyzers.AnnualReturn,_name = 'areturn')
teststrat = cerebro.run()  # run it all
print(teststrat[0].analyzers.areturn.get_analysis())
print(df.head(1),'',df.tail(1))
cerebro.plot()  # and plot it with a single command


import numpy as np

class GaussianBandit(object):
    def __init__(self,  mean=0, stdev=1):
        self.mean = mean
        self.stdev = stdev

    def pull_lever(self):
        reward = np.random.normal(self.mean, self.stdev)

        return np.round(reward, 1)

class GaussianBanditGame(object):
    def __init__(self, bandits):
        self.bandits = bandits
        np.random.shuffle(self.bandits)
        self.reset_game()

    def play(self, choice):
        reward = self.bandits[choice - 1].pull_lever()
        self.rewards.append(reward)
        self.total_reward += reward
        self.n_played += 1
        return reward

    def user_play(self):
        self.reset_game()

        print("Game started. Enter 0 as input to end the game.")
        while True:
            print(f"\n -- Round {self.n_played}")
            choice = int(input(f"Choose a machine from 1 to {len(self.bandits)}: "))
            if choice in range(1, len(self.bandits) + 1):
                reward = self.play(choice)
                print(f"Machine {choice} gave a reward of {reward}.")
                avg_rew = self.total_reward / self.n_played
                print(f"Your average reward so far is {avg_rew}.")
            else:
                break

        print("Game has ended.")
        if self.n_played > 0:
            print(f"Total reward is {self.total_reward}"
              +
              f" after {self.n_played} round(s).")
            avg_rew = self.total_reward / self.n_played
            print(f"Average reward is {avg_rew}.")

    def reset_game(self):
        self.rewards = []
        self.total_reward = 0
        self.n_played = 0

if __name__ == '__main__':
    slotA = GaussianBandit(mean=5, stdev=3)
    slotB = GaussianBandit(mean=6, stdev=1)
    slotC = GaussianBandit(mean=1, stdev=5)

    game = GaussianBanditGame([slotA, slotB, slotC])
    game.user_play()


def exibe_candidatos(deque, pos, ordem):
    if pos < 0 or pos >= deque.size():
        return

    temp_deque = Deque()
    for _ in range(pos):
        temp_deque.add_rear(deque.remove_front())

    if ordem == 'direta':
        while not deque.is_empty():
            print(deque.remove_front())
    elif ordem == 'inversa':
        while not deque.is_empty():
            temp_deque.add_rear(deque.remove_front())
        while not temp_deque.is_empty():
            print(temp_deque.remove_rear())

    while not temp_deque.is_empty():
        deque.add_front(temp_deque.remove_rear())

[Questionário] Deque e Lista/Real Life LightSaber Combat Academy.py


from api.device_account.schemas import DeviceAccountCreateSchema
from db.db import db_session
from db.models.device_account import DeviceAccount
from fastapi import Depends, HTTPException
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.exc import IntegrityError

class DeviceAccountService:
    def __init__(self, session: AsyncSession = Depends(db_session)):
        self.session = session

    async def get_all_device_accounts(self) -> list[DeviceAccount]:
        examples = await self.session.execute(select(DeviceAccount))

        return examples.scalars().fetchall()

    async def add_device_account(self, data: DeviceAccountCreateSchema) -> DeviceAccount:
        device_account_data = DeviceAccount(**data.dict())

        try:
            self.session.add(device_account_data)
            await self.session.commit()
            await self.session.refresh(device_account_data)
        except IntegrityError as e:
            # If the exception is a UniqueConstraint violation, return a 409 Conflict response
            if "duplicate key value violates" in str(e):
                if "DeviceAccount_username_key" in str(e):
                    raise HTTPException(
                        status_code=409, detail="User with this username already exists"
                    )
                if "DeviceAccount_email_key" in str(e):
                    raise HTTPException(
                        status_code=409, detail="User with this email already exists"
                    )
            # Otherwise, re-raise the exception
            raise e

        return device_account_data


import numpy as np
import pickle
import math

def clean(data):
    min_t = float(data[0][0])
    index = 0
    for i in range(1, len(data)):
        tmp = float(data[i][0])
        if tmp < min_t:
            min_t = tmp
            index = i

    inc = [data[index]]
    prev = min_t
    for i in range(index+1, len(data)):
        if float(data[i][0]) < prev:
            continue
        inc.append(data[i])
        prev = float(data[i][0])
    return inc

def map(data):
    flag = False
    ret = []
    for line in data:
        qx = line[1]
        qy = line[2]
        qz = line[3]
        qw = line[4]
        x = 2*qx*qz + 2*qy*qw
        y = 2*qy*qz - 2*qx*qw
        z = 1 - 2*qx*qx - 2*qy*qy
        if not flag:
            base_j = math.acos(x/math.sqrt(x*x+z*z))*180/math.pi
            flag = True
        jing = math.acos(x/math.sqrt(x*x+z*z))*180/math.pi - base_j
        wei = math.asin(y/math.sqrt(x*x+y*y+z*z))*180/math.pi
        ret.append([line[0], jing, wei])
    return ret

def clean2(raw):
    data = []
    begin = 0
    for i in range(0, len(raw)):
        if raw[i][0] != raw[begin][0]:
            tmp = [raw[begin][0], 0, 0]
            for j in range(begin, i):
                for k in range(0, 2):
                    tmp[k+1] += float(raw[j][1+k])
            for k in range(0, 2):
                tmp[k+1] = tmp[k+1]*1.0/(i-begin)
            data.append(tmp)
            begin = i

    tmp = [raw[begin][0], 0, 0]
    for j in range(begin, len(raw)):
        for k in range(0, 2):
            tmp[k+1] += float(raw[j][1+k])
    for k in range(0, 2):
        tmp[k+1] = tmp[k+1]*1.0/(len(raw)-begin)
    data.append(tmp)
    return data

def read_file(filename, experiment, client, video):
    raw = np.loadtxt(filename, dtype='string', delimiter=',')
    data = []
    begin = 1
    for i in range(1, len(raw)):
        if raw[i][1] != raw[begin][1]:
            tmp = [raw[begin][1], 0, 0, 0, 0]
            for j in range(begin, i):
                for k in range(0, 4):
                    tmp[k+1] += float(raw[j][2+k])
            for k in range(0, 4):
                tmp[k+1] = tmp[k+1]*1.0/(i-begin)
            data.append(tmp)
            begin = i

    tmp = [raw[begin][1], 0, 0, 0, 0]
    for j in range(begin, len(raw)):
        for k in range(0, 4):
            tmp[k+1] += float(raw[j][2+k])
    for k in range(0, 4):
        tmp[k+1] = tmp[k+1]*1.0/(len(raw)-begin)
    data.append(tmp)

    data = clean(data)
    data = map(data)
    data = clean2(data)
    filename = str(experiment) + '_' + str(client) + '_' + str(video) + '.pkl'
    with open('/home/gys/fineVR/formated/'+filename, 'wb') as outfile:
        pickle.dump(data, outfile)

    return

if __name__ == '__main__':
    tmp_str = '/home/netlab-ml/workspace/IQIYI/Formated_Data/Experiment_'
    for experiment in range(1,3):
        for client in range(1, 49):
            for video in range(0, 9):
                filename = tmp_str + str(experiment) + '/' + str(client) + '/video_' + str(video) + '.csv'
                print('%d %d %d' % (experiment, client, video))
                read_file(filename, experiment, client, video)


from ..helpers import confirm
import json
import copy

class NbHitsUpdater(object):
    new_nb_hit = None
    previous_nb_hits = None
    config_file = None
    config_content = None

    def __init__(self, config_file, config_content, previous_nb_hits, new_nb_hit):
        self.config_file = config_file
        self.config_content = copy.deepcopy(config_content)
        self.new_nb_hit = new_nb_hit
        self.previous_nb_hits = previous_nb_hits

    def update(self):
        if self._update_needed():
            print("previous nb_hits: " + str(self.previous_nb_hits) + "\n")

            if confirm('Do you want to update the nb_hits in ' + self.config_file + ' ?'):
                try:
                    self._update_config()
                    print("\n[OK] " + self.config_file + " has been updated")
                except Exception:
                    print("\n[KO] " + "Was not able to update " + self.config_file)

    def _update_needed(self):
        return self.previous_nb_hits is None or self.previous_nb_hits != self.new_nb_hit

    def _update_config(self):
        self.config_content['nb_hits'] = self.new_nb_hit
        with open(self.config_file, 'w') as f:
            f.write(json.dumps(self.config_content, indent=2, separators=(',', ': ')))


BASE_LR_RANGE = {'mnist': [0.1, 0.01, 0.005],
                 'cifar10': [0.01, 0.005, 0.001],
                 'gtsrb': [0.01, 0.005, 0.001],
                 'svhn': [0.01, 0.005, 0.001]}

# Range of alpha values used in the definition of robust error with rejection
ALPHA_LIST = [0.0, 0.01, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.5, 1.0]

GAP_FACTOR = {
    'mnist': 0.02,
    'cifar10': 0.5/255,
    'gtsrb': 0.5/255,
    'svhn': 0.5/255
}

# Number of classes
N_CLASSES = {
    'mnist': 10,
    'cifar10': 10,
    'gtsrb': 43,
    'svhn': 10
}

# Target true positive rate for selecting the rejection threshold
TPR_THRESHOLD = {
    'mnist': 0.99,
    'cifar10': 0.95,
    'gtsrb': 0.95,
    'svhn': 0.95
}

# Fraction of samples used for validation
VAL_RATIO = 0.1

# Sample size used for calculating the robustness with rejection
N_SAMPLES = 1000

# Setting for the adaptive attacks
CONFIG_PGDBT_ATTACK_OUTER = {
    'max_iterations': 1000,
    'base_lr': 0.001,
    'momentum': 0.9,
    'lr_factor': 1.1,
    'backtrack': True,
    'rand_init_name': 'random',
    'num_rand_init': 10,
    'clip_min': 0.0,
    'clip_max': 1.0
}

CONFIG_PGD_ATTACK_INNER = {
    'max_iterations': 200,
    'base_lr': 0.1,
    'momentum': 0.9,
    'lr_factor': 1.25,
    'backtrack': False,
    'rand_init_name': 'random',
    'num_rand_init': 5,
    'clip_min': 0.0,
    'clip_max': 1.0
}

CONFIG_MULTITARGET_ATTACK_OUTER = {
    'max_iterations': 200,
    'base_lr': 0.1,
    'momentum': 0.9,
    'lr_factor': 1.1,
    'backtrack': False,
    'rand_init_name': 'random',
    'num_rand_init': 5,
    'clip_min': 0.0,
    'clip_max': 1.0
}

CONFIG_MULTITARGET_ATTACK_INNER = {
     'max_iterations': 200,
     'base_lr': 0.1,
     'momentum': 0.9,
     'lr_factor': 1.25,
     'backtrack': False,
     'rand_init_name': 'random',
     'num_rand_init': 1,
     'clip_min': 0.0,
     'clip_max': 1.0
}

# Plot colors and markers
# https://matplotlib.org/2.0.2/examples/color/named_colors.html
COLORS = ['r', 'b', 'c', 'orange', 'g', 'm', 'lawngreen', 'grey', 'hotpink', 'y', 'steelblue', 'tan',
          'lightsalmon', 'navy', 'gold']
MARKERS = ['o', '^', 'v', 's', '*', 'x', 'd', '>', '<', '1', 'h', 'P', '_', '2', '|', '3', '4']

def lr_schedule(t, lr_max):
    # Learning rate schedule
    if t < 100:
        return lr_max
    elif t < 105:
        return lr_max / 10.
    else:
        return lr_max / 100.


# for x in range(1, 6) :
#     print(str())

# angka = ''
# size = 5

# for i in range(size) :
#     for j in range(i + 1) :
#         angka += '1 '
#     angka += '\n'

# print(angka)

# for x in range(1, 6) :
#     print((6 - x) * (str(x) + ' '))

# 1
# 1 2
# 1 2 3
# 1 2 3 4
# 1 2 3 4 5

# try :
#     txt = str(input('Masukkan Kalimat : '))
#         print('Reverse per kata : {}'.format(txt[::-1]))
# except :
#     print('Kalimat yang anda masukkan salah')

hari = {
    "senin" : "monday",
    "selasa" : "tuesday",
    "rabu" : "wednesday",
    "kamis" : "thursday",
    "jumat" : "friday",
    "sabtu" : "saturday",
    "minggu" : "sunday"
}

inputHari = str.lower(input('Masukkan Nama Hari : '))
if inputHari == 'monday':
    print('Hari yang anda pilih {} dalam bahasa Indonesia adalah senin'.format(inputHari))
elif inputHari == 'senin' :
    print('Hari yang anda pilih {} dalam bahasa Inggris adalah {}'.format(inputHari, hari["senin"]))
elif inputHari == 'tuesday' :
    print('Hari yang anda pilih {} dalam bahasa Indonesia adalah selasa'.format(inputHari))
elif inputHari == 'selasa' :
    print('Hari yang anda pilih {} dalam bahasa Inggris adalah {}'.format(inputHari, hari["selasa"]))
elif inputHari == 'wednesday' :
    print('Hari yang anda pilih {} dalam bahasa Indonesia adalah rabu'.format(inputHari))
elif inputHari == 'rabu' :
    print('Hari yang anda pilih {} dalam bahasa Inggris adalah {}'.format(inputHari, hari["rabu"]))
elif inputHari == 'thursday' :
    print('Hari yang anda pilih {} dalam bahasa Indonesia adalah kamis'.format(inputHari))
elif inputHari == 'kamis' :
    print('Hari yang anda pilih {} dalam bahasa Inggris adalah {}'.format(inputHari, hari["kamis"]))
elif inputHari == 'friday' :
    print('Hari yang anda pilih {} dalam bahasa Indonesia adalah jumat'.format(inputHari))
elif inputHari == 'jumat' :
    print('Hari yang anda pilih {} dalam bahasa Inggris adalah {}'.format(inputHari, hari["jumat"]))
elif inputHari == 'saturday' :
    print('Hari yang anda pilih {} dalam bahasa Indonesia adalah sabtu'.format(inputHari))
elif inputHari == 'sabtu' :
    print('Hari yang anda pilih {} dalam bahasa Inggris adalah {}'.format(inputHari, hari["sabtu"]))
elif inputHari == 'sunday' :
    print('Hari yang anda pilih {} dalam bahasa Indonesia adalah minggu'.format(inputHari))
elif inputHari == 'minggu' :
    print('Hari yang anda pilih {} dalam bahasa Inggris adalah {}'.format(inputHari, hari["minggu"]))
elif inputHari == " "  or inputHari == "" :
    print('nama hari yang anda masukan tidak ada')
else :
    print('Nama hari yang anda masukkan salah')


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

from django.core.files.images import ImageFile
from django.core.files.temp   import NamedTemporaryFile
from django.shortcuts         import render
from django.http              import HttpResponse, HttpResponseRedirect
from django.db.models         import Sum
from urllib.request           import urlopen
from random                   import choice
from sys                      import exc_info
from re                       import search
from dynimg.models            import *
from dynimg.forms             import *
from dynimg.processing        import *

def main(request):
    if request.method == "POST":
        form = NewImageForm(request.POST)

        if form.is_valid():
            name = form.cleaned_data['name']
            urls = form.cleaned_data['urls'].splitlines()

            image = DynamicImg(name=name)
            image.save()
            for url in urls:
                url_obj = ImageUrl.objects.get_or_create(url=url)[0]
                image.urls.add(url_obj)
            image.save()

    else:
        form = NewImageForm()

    return render(request, "dynimg/main.html", locals())

def getimg(request, id_img):
    try:
        image = DynamicImg.objects.get(id=id_img)
    except:
        exception = search("'(.*)'", str(exc_info()[0])).group(1) # Ugly code to get exception name
        return imgHttpResponse(genErrImg(exception))

    image.times_used += 1
    image.save()
    imageUrl = choice(image.urls.all()) # Choosing an URL randomly from provided URLs list
    imageUrl.times_used += 1
    imageUrl.save()

    if image.shadowMode:
        try:
            imgtmp = NamedTemporaryFile(delete=True)
            imgtmp.write(urlopen(url).read())
            imgtmp.flush()
            img = ImageFile(imgtmp)
            return HttpResponse(img, content_type="image/jpeg") # Works with PNG
        except:
            exception = search("'(.*)'", str(exc_info()[0])).group(1) # Ugly code to get exception name
            return imgHttpResponse(genErrImg(exception))

    else:
        return HttpResponseRedirect(imageUrl.url)

def about(request):
    registeredImgs = DynamicImg.objects.count()
    registeredUrls = ImageUrl.objects.count()
    displayedImgs = DynamicImg.objects.aggregate(Sum('times_used'))['times_used__sum']

    return render(request, "dynimg/about.html", locals())


#coding: utf-8

import re
from django import forms
from django.utils.translation import ugettext_lazy as _
from main.utils import parse_csv_file as read_file

def _validation_word(word):
	if len(word) > 46:
		raise forms.ValidationError(_('Maximum length allowed 46 words.'))
	if ' ' in word:
		raise forms.ValidationError(_('Forbidden use words with whitespaces.'))
	if not re.match(r'^\w+$', word):
		raise forms.ValidationError(_('Forbidden use words with special character.'))

class FilesAdminForm(forms.ModelForm):

	def clean_file(self):
		file = self.cleaned_data['file']
		parse_file = read_file(file)

		if not 'word' in parse_file.fieldnames:
			raise forms.ValidationError(_('Structure file incorrect, check example in documentation.'))

		for row in parse_file:
			_validation_word(row['word'])

		return file

	class Meta:
		fields = '__all__'

class WordsAdminForm(forms.ModelForm):

	def clean_word(self):
		word = self.cleaned_data['word']
		_validation_word(word)
		return word

	class Meta:
		fields = '__all__'


# Solved on 2022. 1. 15.
# 1939 중량제한

from collections import deque
import sys
input = sys.stdin.readline

def bfs(weight):
    queue = deque()
    visit = [False] * (N+1)
    visit[S] = True
    queue.append(S)

    while queue:
        node = queue.popleft()
        if node == E:
            return True

        for nxt, w in graph[node]:
            if not visit[nxt] and w > weight:
                queue.append(nxt)
                visit[nxt] = True
    return False

def binarysearch():
    start, end = 1, int(10e9)
    while start < end:
        mid = (start+end)//2
        if bfs(mid):
            start = mid + 1
        else:
            end = mid
    return end

N, M = map(int, input().split())
graph = [[] for _ in range(N+1)]
for _ in range(M):
    A, B, C = map(int, input().split())
    graph[A].append((B, C))
    graph[B].append((A, C))
S, E = map(int, input().split())
print(binarysearch())


#!/usr/bin/python
import os
import sys
import shutil
import warnings
import yaml

from gslab_scons import _exception_classes, misc
from gslab_scons import check_prereq

def configuration(ARGUMENTS, paper = False, config_user_yaml = 'config_user.yaml',
                  config_global_yaml = 'config_global.yaml'):
    # Determines whether to print traceback messages
    debug = ARGUMENTS.get('debug', False)
    if not debug:
        # Hide traceback for configuration test only
        # http://stackoverflow.com/questions/27674602/hide-traceback-unless-a-debug-flag-is-set
        sys.tracebacklimit = 100

    # Copies config_user template if no such file exists
    if not os.path.isfile(config_user_yaml):
        shutil.copy('../config/config_user_template.yaml', config_user_yaml)

    # Loads arguments and configurations
    mode = ARGUMENTS.get('mode', 'develop') # Gets mode; defaults to 'develop'

    # Checks mode
    if not (mode in ['develop', 'cache']):
        message = 'Error: %s is not a defined mode' % mode
        raise _exception_classes.PrerequisiteError(message)

    # Checks for cache directory
    if mode == 'cache':
        cache_dir = misc.load_yaml_value(config_user_yaml, 'cache_directory')
        cache_dir = misc.check_and_expand_path(cache_dir)
    else:
        cache_dir = None

    # Loads config yaml files
    CONFIG = {'user': config_user_yaml, 'global': config_global_yaml}
    for key, val in CONFIG.items():
        CONFIG[key] = yaml.load(open(val, 'rU'), Loader = yaml.SafeLoader)
        if not CONFIG[key]:
            del CONFIG[key]

    # Stores executable names and prerequisite checks. Prefer user to global.
    executable_names = misc.add_two_dict_keys(d = CONFIG, common_key = 'executable_names')
    prereq_checks = misc.add_two_dict_keys(d = CONFIG, common_key = 'prereq_checks')

    # Checks prerequisite applications and record known prereqs
    prereqs = []
    for prereq, should_check_prereq in prereq_checks.items():
        if str(should_check_prereq).lower().strip() in ['y', 'yes', 't', 'true']:
            check_prereq(prereq, manual_execs = executable_names,
                         gslab_vers = CONFIG['global']['gslab_version'])
            prereqs.append(prereq)

    # Stores PYTHONPATH
    try:
        pythonpath = os.environ['PYTHONPATH']
    except KeyError:
        pythonpath = ''

    # Gets return list
    return_list = [mode, cache_dir, CONFIG, executable_names, prereqs, pythonpath]

    # Restores default tracebacklimit and return values
    sys.tracebacklimit = 1000

    return return_list


# coding=utf-8

import tensorflow as tf

learning_rate = 0.001
dropout = 0.9

# 定义对Variable变量的数据汇总函数
"""
计算出var的mean,stddev,max和min，
对这些标量数据使用tf.summary.scalar进行记录和汇总。
同时，使用tf.summary.histogram直接记录变量var的直方图。
"""
def variable_summaries(var,debug=False):
    if debug:
        with tf.name_scope('summaries'):
            mean = tf.reduce_mean(var)
            tf.summary.scalar('mean', mean)
            with tf.name_scope('stddev'):
                stddev = tf.sqrt(tf.reduce_mean(tf.square(var-mean)))

            tf.summary.scalar('stddev', stddev)
            tf.summary.scalar('max', tf.reduce_max(var))
            tf.summary.scalar('min', tf.reduce_min(var))
            tf.summary.histogram('histogram', var)

# 定义神经网络模型参数的初始化方法，
# 权重依然使用常用的truncated_normal进行初始化，偏置则赋值为0.1
def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)
# 设计一个MLP多层神经网络来训练数据，在每一层中都会对模型参数进行数据汇总。
"""
定一个创建一层神经网络并进行数据汇总的函数nn_layer。
这个函数的输入参数有输入数据input_tensor,输入的维度input_dim,
输出的维度output_dim和层名称layer_name，激活函数act则默认使用Relu。
在函数内，显示初始化这层神经网络的权重和偏置，并使用前面定义的
variable_summaries对variable进行数据汇总。
然后对输入做矩阵乘法并加上偏置，再将未进行激活的结果使用tf.summary.histogram统计直方图。
同时，在使用激活函数后，再使用tf.summary.histogram统计一次。
"""
def nn_layer(input_tensor, input_dim,
             output_dim, layer_name,act=tf.nn.relu,
             debug=False):
    with tf.name_scope(layer_name):
        with tf.name_scope('weight'):
            weights = weight_variable([input_dim, output_dim])
            variable_summaries(weights,debug)

        with tf.name_scope('biases'):
            biases = bias_variable([output_dim])
            variable_summaries(biases,debug)

        with tf.name_scope('Wx_plus_b'):
            preactivate = tf.matmul(input_tensor, weights) + biases
            if debug:
                tf.summary.histogram('pre_activations', preactivate)

        activations = act(preactivate, name='actvations')
        if debug:
            tf.summary.histogram('activations', activations)

        return activations


#!/usr/bin/python3
# encoding: utf-8
'''
@author: matthew hsw
@contact: murdockhou@gmail.com
@software: pycharm
@file: spm_model_test.py
@time: 2019/9/11 下午8:16
@desc:
'''
import tensorflow as tf
import os
import cv2
import argparse
import time

import numpy as np
from nets.spm_model import SpmModel
from decoder.decode_spm import SpmDecoder

parser = argparse.ArgumentParser()
parser.add_argument('--video', default=None, type=str)
parser.add_argument('--imgs',
                    default='/media/hsw/E/datasets/ai-challenger/ai_test/ai_test_a/test_a',
                    type=str)
parser.add_argument('--score', default=0.1, type=float)
parser.add_argument('--dist', default=20., type=float)
parser.add_argument('--netH', default=512, type=int)
parser.add_argument('--netW', default=512, type=int)
parser.add_argument('--ckpt', default=None, type=str)
args = parser.parse_args()

colors = [[0,0,255],[255,0,0],[0,255,0],[0,255,255],[255,0,255],[255,255,0]]
netH = args.netH
netW = args.netW
score = args.score
dist = args.dist

@tf.function
def infer(model, inputs):

    center_map, kps_reg_map = model(inputs)

    return center_map, kps_reg_map

def run(model, img):

    img_show = img.copy()

    h, w, c = img.shape
    # 只在最右边或者最下边填充0, 这样不会影响box或者点的坐标值, 所以无需再对box或点的坐标做改变
    #if w > h:
    #    img = cv2.copyMakeBorder(img, 0, w - h, 0, 0, cv2.BORDER_CONSTANT, value=(0, 0, 0))
    #else:
    #    img = cv2.copyMakeBorder(img, 0, 0, 0, h - w, cv2.BORDER_CONSTANT, value=(0, 0, 0))

    img = img.astype(np.float32) / 255.0
    img = cv2.resize(img, (netH, netW), interpolation=cv2.INTER_CUBIC)
    img_input = np.expand_dims(img, axis=0)
    factor_x = img_show.shape[1] / (netW / 4)
    factor_y = img_show.shape[0] / (netH / 4)

    center_map, kps_reg_map = infer(model, img_input)
    # print (center_map.shape)
    # print (kps_reg_map.shape)
    # label = outputs[0]
    spm_decoder = SpmDecoder(factor_x, factor_y, netH // 4, netW // 4)
    results = spm_decoder([center_map[0].numpy(), kps_reg_map[0].numpy()], score_thres=score, dis_thres=dist)

    for j, result in enumerate(results):
        # print (result)
        center = result['center']
        single_person_joints = result['joints']
        cv2.circle(img_show, (int(center[0]), int(center[1])), 5, colors[j%3], thickness=-1)
        for i in range(12):

            x = int(single_person_joints[2*i])
            y = int(single_person_joints[2*i+1])
            cv2.circle(img_show, (x,y), 4, colors[j%3],thickness=-1)
            cv2.putText(img, str(i), (x,y), cv2.FONT_HERSHEY_COMPLEX, 1,(0, 0, 250), 1)

    return img_show

if __name__ == '__main__':

    use_gpu = False
    use_nms = True

    if use_gpu:
        os.environ['CUDA_VISIBLE_DEVICES'] = '0'
    else:
        os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

    inputs = tf.keras.Input(shape=(netH, netW, 3), name='modelInput')
    outputs = SpmModel(inputs, 12, is_training=False)
    model = tf.keras.Model(inputs, outputs)

    assert args.ckpt is not None

    model.load_weights(args.ckpt)

    if args.video is not None:
        cap = cv2.VideoCapture(args.video)
        ret, img = cap.read()
        while ret:
            s = time.time()
            result = run(model, img)
            e = time.time()
            print ('time: ', e-s)
            cv2.imshow('result', result)
            k = cv2.waitKey(1)
            if k == ord('q'):
                break
            ret, img = cap.read()
    elif os.path.isdir(args.imgs):
        for img_name in os.listdir(args.imgs):
            print('----------------------------------------------')
            img = cv2.imread(os.path.join(args.imgs, img_name))
            result = run(model, img)
            cv2.imshow('result', result)
            k = cv2.waitKey(0)
            if k == ord('q'):
                break
    elif os.path.isfile(args.imgs):
        img = cv2.imread(args.imgs)
        result = run(model, img)
        cv2.imshow('result', result)
        k = cv2.waitKey(0)

    else:
        print ('You Must Provide one video or imgs/img_path')


from django.db import models

class Review(models.Model):
	name = models.CharField(max_length = 120)
	text = models.TextField()
	date = models.DateTimeField(auto_now_add=True)

	class Meta:
		verbose_name = "Отзыв"
		verbose_name_plural = 'Отзывы'

	def __str__(self):
		return self.name


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

import jieba.posseg as pseg
import pandas as pd

import sklearn.feature_extraction

data = pd.read_csv()
'''
加载停用词
'''
def stopword():
    stop_words = set()
    with open('D:\mr\stopwords.txt', "r", encoding="utf-8") as f:
        for word in f.readlines():
            stop_words.add(word.strip())
    return stop_words

'''
中文分词
'''
def clean_text(text):
    words = pseg.cut(''.join(text.split('\t')))
    all_list = list()
    allowPOS = ['n','v','j']
    for word,flag in words:
        if flag in allowPOS and len(word) > 1:
            all_list.append(word)
    return ' '.join([word for word in all_list if word not in stopword()])

'''
把处理过的数据变成Wordvec的要求
'''
list(data.clearn_word.apply(lambda text :text.split(' ')))


from __future__ import print_function, division, absolute_import

def parse_input(data):

    acres = {}
    for i, line in enumerate(data):
        for j in range(len(line)):
            acres[i, j] = line[j]
    return acres, len(data), len(data[0])

def count_adjacent(row, col, acres, char='|'):
    count = 0
    for d_row, d_col in [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]:
        if d_row == d_col == 0:
            continue
        try:
            if acres[row + d_row, col + d_col] == char:
                count += 1
        except KeyError:
            pass
    return count

def mutate(acres, num_rows, num_cols):
    acres_new = acres.copy()
    for i in range(num_rows):
        for j in range(num_cols):

            if acres[i, j] == '.':
                if count_adjacent(i, j, acres, '|') >= 3:
                    acres_new[i, j] = '|'

            elif acres[i, j] == '|':
                if count_adjacent(i, j, acres, '#') >= 3:
                    acres_new[i, j] = '#'

            elif acres[i, j] == '#':
                if count_adjacent(i, j, acres, '#') > 0 and count_adjacent(i, j, acres, '|') > 0:
                    acres_new[i, j] = '#'
                else:
                    acres_new[i, j] = '.'

    return acres_new

def print_land(acres, num_rows, num_cols):
    for i in range(num_rows):
        for j in range(num_cols):
            print(acres[i, j], end='')
        print()
    print()

def solve(data, minutes=10):

    acres, num_rows, num_cols = parse_input(data)

    for time in range(minutes):
        acres = mutate(acres, num_rows, num_cols)
        # print()
        # print_land(acres, num_rows, num_cols)

    num_wooded = len([a for a in acres.values() if a == '|'])
    num_lumber_yards = len([a for a in acres.values() if a == '#'])
    num_resources = num_wooded * num_lumber_yards

    print('Wooded acres:', num_wooded)
    print('Lumber yards:', num_lumber_yards)
    print('Total Resources:', num_resources)

if __name__ == '__main__':

    # with open('test_input.txt', 'r') as f:
    #     lines = [s.rstrip() for s in f.readlines()]
    # solve(data=lines)

    # part 1
    with open('input.txt', 'r') as f:
        lines = [s.rstrip() for s in f.readlines()]
    solve(data=lines, minutes=10)

    # part 2
    with open('input.txt', 'r') as f:
        lines = [s.rstrip() for s in f.readlines()]

    # Find how long it takes the pattern to repeat
    for minutes in range(100, 1000, 100):
        print()
        print('After {0} minutes:'.format(minutes))
        solve(data=lines, minutes=minutes)


from google.cloud import bigquery
from google.cloud import pubsub_v1
import six
import os
import base64
import json
from google.cloud import documentai_v1beta3 as documentai
from google.cloud import storage

def parse_form(event, context):
    """Parse a form"""

    # Event listing
    print('Event ID: {}'.format(context.event_id))
    print('Event type: {}'.format(context.event_type))
    print('Bucket: {}'.format(event['bucket']))
    print('File: {}'.format(event['name']))
    print('Metageneration: {}'.format(event['metageneration']))
    print('Created: {}'.format(event['timeCreated']))
    print('Updated: {}'.format(event['updated']))
    processor_id = os.environ['processor_id']
    location = os.environ['location']
    project_id = os.environ['project_id']
    bq_topic_name= os.environ['bq_topic_name']
    file_path = event['name']
    form_input_bucket = event['bucket']
    publisher = pubsub_v1.PublisherClient()

    # Instantiates a client
    client = documentai.DocumentProcessorServiceClient()

    # The full resource name of the processor, e.g.:
    # projects/project-id/locations/location/processor/processor-id
    # You must create new processors in the Cloud Console first
    name = f"projects/{project_id}/locations/{location}/processors/{processor_id}"

    image_content = download_blob(form_input_bucket, file_path).download_as_bytes()

    # Read the file into memory
    document = {"content": image_content, "mime_type": "application/pdf"}

    # Configure the process request
    request = {"name": name, "document": document}

    # Recognizes text entities in the PDF document
    result = client.process_document(request=request)

    document = result.document

    print("Document processing complete.")

    # For a full list of Document object attributes, please reference this page: https://googleapis.dev/python/documentai/latest/_modules/google/cloud/documentai_v1beta3/types/document.html#Document

    document_pages = document.pages

    # Read the text recognition output from the processor
    print("The document contains the following paragraphs:")
    for page in document_pages:
        print('Page number: {}'.format(page.page_number))
        futures = []
        paragraphs = page.paragraphs
        for paragraph in paragraphs:
            paragraph_text = get_text(paragraph.layout, document)

        for form_field in page.form_fields:
            print('********************************')
            #print('Field Value confidence: {}'.format(form_field.field_value.confidence))
            text = get_text(form_field.field_name,document)
            text_confidence = form_field.field_name.confidence
            text_value = get_text(form_field.field_value,document)
            if(text_value == ""):
                text_value = "Empty Value (added by code)"
            text_value_confidence = form_field.field_value.confidence

            print("Pub/Sub insert page_no: {} form_key: {} form_key_confidence: {} form_value: {} form_value_confidence: {} " .format(page.page_number, text, text_confidence, text_value, text_value_confidence))
            message = {
                'page_no': page.page_number,
                'form_key': text,
                'form_key_confidence': text_confidence,
                'form_value': text_value,
                'form_value_confidence': text_value_confidence,
            }

            print('Topic Name: {}'.format(bq_topic_name))
            message_data = json.dumps(message).encode('utf-8')
            topic_path = publisher.topic_path(project_id, bq_topic_name)
            print('Topic Path: {}'.format(topic_path))
            future = publisher.publish(topic_path, data=message_data)
            futures.append(future)
            for future in futures:
                future.result()
            print ("Sent to PubSub")

def download_blob(bucket_name, source_blob_name):
    """Downloads a blob from the bucket."""

    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(source_blob_name)

    return blob
# Extract shards from the text field
def get_text(doc_element: dict, document: dict):
    """
    Document AI identifies form fields by their offsets
    in document text. This function converts offsets
    to text snippets.
    """
    response = ""
    # If a text segment spans several lines, it will
    # be stored in different text segments.
    for segment in doc_element.text_anchor.text_segments:
        start_index = (
            int(segment.start_index)
            if segment in doc_element.text_anchor.text_segments
            else 0
        )
        end_index = int(segment.end_index)
        response += document.text[start_index:end_index]
    return response


def count_names_starting_with_s(names_list):
    names_dict = {}

    for name in names_list:
        name = name.capitalize()
        if name[0] == 'S':
            if name in names_dict:
                names_dict[name] += 1
            else:
                names_dict[name] = 1

    return names_dict

names_list = ['Sarah', 'Sam', 'Sophia', 'Steven', 'John', 'Samantha', 'Sophia', 'Asa', 'Ashley', 'Daniel', 'Steven',
              'sophia', 'Samantha', 'Susu']
result = count_names_starting_with_s(names_list)
print(result)


# Вершины называются целыми числами
N, M = map(int, input().split())
# или
# N, M = [int(x) for x in input().split()]
# или
# N, M = input().split()
# N = int(N)
# M = int(M)

graph = {i: set() for i in range(N)}
for i in range(M):
    v1, v2 = map(int, input().split())
    graph[v1].add(v2)
    graph[v2].add(v1)

print(graph)
for key, value in graph.items():
    print(key, ':', value)

18 Графы/07 Считывание графа в списки смежности  2.py


from __future__ import unicode_literals
from builtins import object

import os
from sumatra import versioncontrol

def find_versions_from_versioncontrol(dependencies):
    """Determine whether a file is under version control, and if so,
       obtain version information from this."""
    for dependency in dependencies:
        if dependency.version == "unknown":
            try:
                wc = versioncontrol.get_working_copy(dependency.path)
            except versioncontrol.VersionControlError:
                pass  # dependency.version remains "unknown"
            else:
                if wc.has_changed():
                    dependency.diff = wc.diff()
                dependency.version = wc.current_version()
                dependency.source = wc.repository.url
    return dependencies

# add support for using packaging systems, e.g. apt, to find versions.

# add support for looking for Subversion $Id:$ tags, etc.

def find_versions(dependencies, heuristics):
    """
    Try to find version information by calling a series of functions in turn.

    *dependencies*:
        a list of Dependency objects.
    *heuristics*:
        a list of functions that accept a component as the single
        argument and return a version number or 'unknown'.

    Returns a possibly modified list of dependencies
    """
    for heuristic in heuristics:
        dependencies = heuristic(dependencies)
    return dependencies

def find_file(path, current_directory, search_dirs):
    """
    Look for path as an absolute path then relative to the current directory,
    then relative to *search_dirs*.
    Return the absolute path.
    """
    op = os.path
    if op.exists(path):
        return op.abspath(path)
    for dir in [current_directory] + search_dirs:
        search_path = op.join(dir, path)
        if op.exists(search_path):
            return search_path
    raise IOError("File %s does not exist" % path)

class BaseDependency(object):

    """
    Contains information about a program component, and tries to determine version information.

    *name*:
        an identifying name, e.g. the module name in Python
    *path*:
        the location of the dependency file in the local filesystem
    *version*:
        the version of the dependency, if that can be determined, otherwise
        'unknown'. Always a string, even if the version can also be represented
        as a number.
    *diff*:
        if the dependency is under version control and has been modified, the
        diff between the actual version and the last-committed version.
    *source*:
        an identifier for where the dependency came from, if known, e.g. the
        url of a version control repository or the name of a Linux package.

    """

    def __init__(self, name, path=None, version='unknown', diff='', source=None):
        self.name = name
        self.path = path
        self.diff = diff
        self.version = version
        self.source = source  # e.g. url of (upstream?) repository

    def __repr__(self):
        return "%s (%s) version=%s%s" % (self.name, self.path, self.version, self.diff and "*" or '')

    def __eq__(self, other):
        return self.name == other.name and self.path == other.path and \
            self.version == other.version and self.diff == other.diff

    def __ne__(self, other):
        return not self.__eq__(other)

    def __hash__(self):
        return hash(self.name) ^ hash(self.path) ^ hash(self.version) ^ hash(self.diff)