#! /usr/bin/env python3
"""Cluster amplicon sequences using distribution patterns across samples to
inform OTU generation.
"""

from __future__ import print_function, division

__author__ = 'Christopher Thornton'
__license__ = 'GPLv3'
__date__ = '2019-10-08'  #project initiation: 2014-07-23
__status__ = "Beta"
__version__ = '0.3.1'

import argparse
from envclust import *
import os
import random
from scipy.stats import *
import sys

def verbosity(verbose, string):
    if verbose:
        print(string)

def file_check(files, mode):
    """Check whether a file exists and/or is accessible to the user."""
    cwd = os.getcwd()
    path = cwd + '/' + files
    exists = os.path.exists(path)
    read_access = os.access(path, os.R_OK)
    access = True
    reason = None
    if mode == 'rU':
        if exists and not read_access:
            reason = "Error: you do not have the proper permissions to \
            access " + files
            access = False
        elif not exists:
            reason = "Error: " + files + " does not exist in " + cwd
            access = False
    elif mode == 'w':
        if not exists:
            try:
                fh = open(files, mode)
                fh.close()
            except IOError as e:
                reason = str(e)
                access = False
        else:
            access = False
            reason = "Error: " + files + " already exists in " + cwd + ". \
            Please move the file before continuing so that it will not be \
            overwritten"
    return access, reason

def get_abundance(count_table):
    """Generate a list of two-tuples which contain the sequence identifier \
    and frequency that the sequence appears in the samples.
    """
    abunds = []
    abund_dict = {}
    with open(count_table, 'rU') as count:
        header = count.readline()
        for line in count:
            count_info = line.split()
            distribution = count_info[2:]
            abundance = count_info[1]
            identifier = count_info[0]
            abund_tuple = (identifier, abundance)
            abunds.append(abund_tuple)
            abund_dict[identifier] = distribution

    return abunds, abund_dict

def fasta_parse(fasta_file):
    """Return a dictionary of sequences with identifier as key and sequences \
    as values.
    """
    sequence_dict = {}
    with open(fasta_file, 'rU') as fasta:
        for line in fasta:
            if line.startswith('>'):
                identifier = line.strip('>\n')
                sequence_dict[identifier] = ''
            else:
                sequence_dict[identifier] += line.strip()

    return sequence_dict

def return_last(last):
    """Return the last item in an iterable object.
    """
    return last[-1]

def sort_by_last(tuple_list, direction):
    """Sort list of tuples by the value of the last item in tuple."""
    sorted_list = sorted(tuple_list, reverse=direction, key=return_last)
    return sorted_list

def compare_distributions(cand, rep, iters):
    """Compare distributions to determine if they are significantly different.
    """
    obs_table = (cand, rep)

    # Remove if all cells in index contain a zero value
    zero_positions = []
    position_offset = 0
    for index in range(len(obs_table[0])):
        count = 0
        for dist in range(len(obs_table)):
            if int(obs_table[dist][index]) == 0:
                count += 1
        if count == len(obs_table):
            zero_positions.append(index - position_offset)
            position_offset += 1

    for position in zero_positions:
        for row in obs_table:
            rm_zero = row.pop(position)

    R, C, N, row_sums, col_sums = table_counts(obs_table)
    chisq_obs, exp_obs = chisq_test(obs_table)

    # Perform monte-carlo simulation to obtain P value if required for
    # accuracy. Otherwise, calculate p-value using chi-squared distribution
    simulate = check_cells(exp_obs)
    if simulate:
        p = simulate_chisq(obs_table, chisq_obs, iters)
    else:
        dof = (R - 1) * (C - 1)  #degrees of freedom
        cdf = chi2.cdf(chisq_obs, dof)  #cumulative distribution function
        p = 1 - cdf

    return p

def table_counts(contingency_table):
    """Obtain summary information about contingency table created from \
    candidate and representative distributions.
    """
    num_rows = len(contingency_table)
    num_columns = len(contingency_table[0])

    row_sums = [sum(row) for row in contingency_table]

    column_sums = []
    for c in range(num_columns):
        col_total = 0
        for r in range(num_rows):
            col_total += contingency_table[r][c]
        column_sums.append(col_total)

    table_total = sum(row_sums)

    return num_rows, num_columns, table_total, row_sums, column_sums

def check_cells(exp_table):
    """Verify that 80% of cells in the expected table have values of five or \
    more and that the smallest expected value is at least one. If not, \
    recommend Monte carlo simulation.
    """
    simulate = False
    count = 0
    num_cells = 0
    values = []
    for row in exp_table:
        num_cells += len(row)
        values.extend(row)
        for cell in row:
            if int(cell) < 5:
                count += 1

    percent_min_values = count / num_cells
    smallest = sorted(values)[0]

    if percent_min_values > 0.20 or smallest < 1:
        simulate = True

    return simulate

def main():
    parser = argparse.ArgumentParser(description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('fasta_file',
        metavar='in.fasta',
        help="input sequences in aligned FASTA format")
    parser.add_argument('count_file',
        metavar='in.count',
        help="input data table file of sequence counts. Data should be in the "
            "form species x sites, where rows represent sequence IDs and "
            "columns represent distinct samples")
    parser.add_argument('-o', '--out',
        default = sys.stdout,
        metavar='out.tsv',
        help="output tab-delimited OTU table to file [default: output to "
            "stdout]")
    parser.add_argument('-v', '--verbose',
        action='count',
        default=0,
        help="increase output verbosity")
    parser.add_argument('-d', '--distance',
        dest='dist_method',
        metavar='METHOD',
        choices=['jc69', 'k80', 'hamming'],
        default='hamming',
        help="model for calculating distance between sequences [default: "
            "hamming]. Options are Jukes-Cantor '69 (jc69), Kimura '80 (k80), "
            "or Hamming (hamming)")
    parser.add_argument('-i', '--iter',
        type=int,
        dest='num_iters',
        metavar='INT',
        default=10000,
        help="number of iterations to use when performing a Monte-carlo "
            "simulation if required for the chi-square test [default: 10000]")
    parser.add_argument('-m', '--max',
        type=float,
        metavar='DISTANCE',
        dest='max_dist',
        default=0.10,
        help="maximum genetic variation allowed between sequences in a "
            "population/cluster [default: 0.10]")
    parser.add_argument('-p', '--p',
        type=float,
        dest='p_cutoff',
        metavar='THRESHOLD',
        default=0.05,
        help="p-value cutoff for determining whether to reject the null "
            "hypothesis that the distribution of two sequences are "
            "statistically similar [default: 0.05]")
    parser.add_argument('--version',
        action='version',
        version='%(prog)s ' + __version__)

    args = parser.parse_args()

    # Check program parameters
    in_files = [args.fasta_file, args.count_file]
    for in_file in in_files:
        in_access, in_reason = file_check(in_file, 'rU')
        if not in_access:
            print(in_reason)
            sys.exit(1)

    outfile = args.out
    out_access, out_reason = file_check(outfile, 'w')
    if not out_access:
        print(out_reason)
        sys.exit(1)

    max_dist = args.max_dist
    if max_dist <= 0 or max_dist > 1:
        print('--max-dist out of range. Value should be between 0 and 1', \
            file=sys.stderr)
        sys.exit(1)

    p_cutoff = args.p_cutoff
    if p_cutoff <= 0 or p_cutoff >= 1:
        print('--p-cutoff out of range. Value should be between 0 and 1', \
            file=sys.stderr)
        sys.exit(1)

    if args.dist_method == 'jc69':
        correction_method = distance_jc69
    elif args.dist_method == 'k80':
        correction_method = distance_k80
    else:
        correction_method = distance_hamming

    otu_dict = {}
    seq_dict = fasta_parse(args.fasta_file)
    abunds, abund_dict = get_abundance(args.count_file)
    sorted_abunds = sort_by_last(abunds, True)
    seqs_processed = 0
    num_seqs = len(sorted_abunds)
    num_otus = 0
    for candidate in sorted_abunds:
        otu_reps = otu_dict.keys()
        cand_id, cand_abund = candidate
        cand_seq = seq_dict[cand_id]

        message = 'Selecting {} as candidate sequence'.format(cand_id)
        verbosity(args.verbose, message)

        rep_distances = []
        for rep_id in otu_reps:
            rep_seq = seq_dict[rep_id]
            if len(cand_seq) != len(rep_seq):
                print('Error: sequence lengths are not the same', file=sys.stderr)
                sys.exit(1)

            distance = correction_method(cand_seq, rep_seq)
            if distance < max_dist:
                dist_tuple = (rep_id, distance)
                rep_distances.append(dist_tuple)

        # Compare distributions between candidate and OTU representative seqs
        sorted_reps = sort_by_last(rep_distances, False)
        added = False
        for rep in sorted_reps:
            rep_id = rep[0]
            cand_dist = [int(i) for i in abund_dict[cand_id]]
            rep_dist = [int(i) for i in abund_dict[rep_id]]
            obs_table = (cand_dist, rep_dist)
            p_value = compare_distributions(cand_dist, rep_dist, args.num_iters)

            # Add candidate to representative's OTU if distributions match
            if p_value >= p_cutoff:
                message = "Adding '{}' to cluster represented by '{}'"\
                    .format(cand_id, rep_id)
                verbosity(args.verbose, message)

                message = 'p-value: ' + str(p_value)
                verbosity(args.verbose, message)

                otu_dict[rep_id].append(cand_id)
                added = True
                break

        # Create OTU with candidate sequence as the representative sequence if
        # distribution is sufficiently unique
        if not added:
            num_otus += 1
            message = 'OTUs created: ' + str(num_otus)
            verbosity(args.verbose, message)

            otu_dict[cand_id] = [cand_id]
            message = "Creating cluster with '{}' as the representative"\
                .format(cand_id)
            verbosity(args.verbose, message)

        # Output current status
        seqs_processed += 1
        completion = int((seqs_processed / num_seqs) * 100)
        message = 'Percentage of sequence processed: {!s}%\n'.format(completion)
        verbosity(args.verbose, message)

    # Output information on run
    largest = max([len(otu_dict[i]) for i in otu_dict])
    smallest = min([len(otu_dict[j]) for j in otu_dict])
    print('Total OTUs: {!s}'.format(num_otus), file=sys.stderr)
    print('Sequences processed: {!s}'.format(seqs_processed), file=sys.stderr)
    print('Size of largest cluster: {!s}'.format(largest), file=sys.stderr)
    print('Size of smallest cluster: {!s}'.format(smallest), file=sys.stderr)

    # Write clusters to file
    message = "Finished processing sequences.\nWriting OTUs to '{}'."\
        .format(outfile)
    verbosity(args.verbose, message)

    fill = len(str(num_otus))
    otu_names = ['eOTU{}'.format(str(i).zfill(fill)) for i in \
        range(1, int(num_otus) + 1)]

    with open(outfile, 'w') as out_h:

        position = 0
        for cluster_rep in otu_dict:
            position += 1
            rep_name = otu_names[position]
            members = '\t'.join(otu_dict[cluster_rep])

            output = '{}\t{}\n'.format(rep_name, members)
            out.write(output)

if __name__ == '__main__':
    main()
    sys.exit(0)


#!/usr/bin/env python3
import sys
import time
from typing import List

import requests
import datetime
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap

from gui.py.setting_window import Ui_Dialog
from gui.py.window import Ui_MainWindow
from gui.py.task_box import Ui_frame
from gui.py.custome_window import Ui_Custom
from gui.py.custome_task import Ui_Frame as Ui_CustomTask

API_key = None
# Forcing setting file back in to the application folder.
# This will make sure that I don't leave any junk files on peoples computers. Since there is no uninstall button.
settings = QtCore.QSettings("setting.ini", QtCore.QSettings.IniFormat)

with open("icons/statistics.txt", "r") as file:
    task_options = file.readlines()
    task_options = [task.removesuffix("\n") for task in task_options]

# Setting window contains setting for the program
# Here you can choose what tasks will be shown and more.
class SettingWindow(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)
        self.buttonBox.accepted.connect(self.save)

        # Displayed Tasks setting

        # Sets all checkboxes, needs to be parsed to int because it returns string and bool parser always give true
        for child in self.findChildren(QtWidgets.QCheckBox):  # type: QtWidgets.QCheckBox
            # assert isinstance(child, QtWidgets.QCheckBox)
            child.setChecked(settings.value(child.objectName(), type=bool, defaultValue=1))

        # Advanced Options
        self.busts_number.setValue(settings.value("busts_number", type=int, defaultValue=3))
        self.addiction_amount.setValue(settings.value("addiction_amount", type=int, defaultValue=10))

    def save(self):  # Saves check box states in to a setting.ini

        for child in self.findChildren(QtWidgets.QCheckBox):  # type: QtWidgets.QCheckBox
            # assert isinstance(child, QtWidgets.QCheckBox)
            settings.setValue(child.objectName(), child.isChecked())

        # Advanced Options
        settings.setValue("busts_number", self.busts_number.value())
        settings.setValue("addiction_amount", self.addiction_amount.value())

        the_button_was_clicked()  # Force Updates the list so the new setting is applied

class TaskBox(QtWidgets.QFrame, Ui_frame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)

class CustomTask(QtWidgets.QFrame, Ui_CustomTask):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)

        self.comboBox_4.addItems(task_options)
        self.comboBox_4.setInsertPolicy(QtWidgets.QComboBox.NoInsert)
        self.comboBox_4.completer().setCompletionMode(QtWidgets.QCompleter.PopupCompletion)

        self.comboBox_3.addItems(["increased by"])

class CustomWindow(QtWidgets.QDialog, Ui_Custom):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)
        self.buttonBox.accepted.connect(self.save)
        self.item_list: List[CustomTask] = list()

        saved_list = settings.value("customs", defaultValue=[], type=list)

        for task in saved_list:
            loaded_task = CustomTask(self.frame)

            loaded_task.lineEdit.setText(task.get("description", ""))
            loaded_task.comboBox_4.setCurrentText(task.get("stat", ""))
            loaded_task.comboBox_3.setCurrentText(task.get("check", "increased by"))
            loaded_task.spinBox_2.setValue(task.get("value", 0))

            loaded_task.pushButton_2.clicked.connect(lambda: self.remove_task(loaded_task))

            self.verticalLayout_2.addWidget(loaded_task)
            self.item_list.append(loaded_task)

        self.verticalLayout.setAlignment(Qt.AlignTop)
        self.new_task = CustomTask(self.frame)

        self.new_task.pushButton_2.setText("➕")
        self.new_task.pushButton_2.clicked.connect(self.add_task)

        self.verticalLayout_2.addWidget(self.new_task)

    pass

    def add_task(self):
        self.new_task.pushButton_2.clicked.disconnect()

        temp = self.new_task

        self.item_list.append(temp)

        self.new_task.pushButton_2.clicked.connect(lambda: self.remove_task(temp))
        self.new_task.pushButton_2.setText("🗑")

        self.new_task = CustomTask(self.frame)
        self.new_task.pushButton_2.setText("➕")
        self.new_task.pushButton_2.clicked.connect(self.add_task)
        self.verticalLayout_2.addWidget(self.new_task)

    def remove_task(self, task):
        self.verticalLayout_2.removeWidget(task)
        self.item_list.remove(task)

    def save(self):

        customs = []

        for child in self.item_list:

            if child.lineEdit.text() == "":
                continue

            customs.append({
                "description": child.lineEdit.text(),
                "stat": child.comboBox_4.currentText(),
                "check": child.comboBox_3.currentText(),
                "value": child.spinBox_2.value()
            })

        settings.setValue("customs", customs)
        pass

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)

        self.pushButton.clicked.connect(the_button_was_clicked)
        self.actionSetting.triggered.connect(self.open_setting)
        self.actionCustome_Tasks.triggered.connect(self.open_custome)
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(update_tasks)
        self.error.hide()

        self.verticalLayout_2.setAlignment(Qt.AlignTop)

    # The timer needs to be stop so the UI doesn't freez on the user
    def open_setting(self):
        dialog = SettingWindow(self)
        self.timer.stop()
        dialog.open()
        the_button_was_clicked()

    def open_custome(self):
        dialog = CustomWindow(self)
        self.timer.stop()
        dialog.open()
        the_button_was_clicked()
        pass

    def generate_save_file(self):
        dialog = SettingWindow(self)
        dialog.save()

class Task:
    priority = 0
    ID = 0

    def __init__(self, task_name, priority=0, link="link", image="icons/missing_icon.png", ID=0):
        self.frame = TaskBox(window.scrollAreaWidgetContents)

        self.frame.task_name.setText(task_name)
        self.frame.icon.setAutoFillBackground(False)
        self.frame.icon.setPixmap(QPixmap(image).scaled(self.frame.icon.size()))

        self.frame.link.setText(link)
        self.frame.link.setOpenExternalLinks(True)

        self.frame.show()
        self.ID = ID
        self.priority = priority

    def __del__(self):
        self.frame.deleteLater()

    def move(self, position):
        window.verticalLayout_2.addWidget(self.frame)
        pass

def the_button_was_clicked():
    global API_key
    API_key = window.textEdit.toPlainText()

    code = get_request("https://api.torn.com/user/?selections=timestamp&key={}".format(API_key))

    if code == 2:  # Checks if the API_key is valid, shows error message if it isn't.
        window.error.show()
        return False

    if settings.value("API_key") != API_key:  # if the keys different and the new key is valid save key to file.
        settings.setValue("API_key", API_key)

    window.timer.start(30500)  # every 30-second update (new data is shown every 30 seconds so no need to update sooner)
    window.error.hide()
    update_tasks()

tasks = list()

# Sorts Task based on priority. Should be called every time new task list is updated.
def reorder_task():
    offset = 0
    for i in range(10):
        for task in tasks:
            if task.priority == i:
                task.move(offset)
                offset += 1

def get_request(url):  # Code that waits and retryes request when error occurs (most comonly running out off API calls)
    json_info = requests.get(url).json()

    while True:
        if json_info.get("error") is None:
            break
        elif json_info.get("error").get("code") != "5":
            return json_info.get("error").get("code")

        json_info = requests.get(url).json()
    return json_info

def link(url):
    return "Visit".format(url)

# gets either statistics from timestamp, or the current ones, if time is not provided
def get_stats(time_start=None):
    statistics = settings.value("customs", [], list)
    statistics = [statistics[i:i + 10] for i in range(0, len(statistics), 10)]

    data = dict()
    for batch in statistics:
        url = "https://api.torn.com/user/?selections=personalstats&stat="
        for stat in batch:
            url += stat.get("stat") + ","

        if time_start is not None:
            url += "&timestamp=" + str(time_start)

        url += "&key=" + settings.value("API_key")

        temp = dict(get_request(url).get("personalstats"))
        data.update(temp)
        # Even thou correctly cashing shouldn't get triggerd with different request, it seems it still happens if the
        # requests are close together.
        time.sleep(1)

    return data

old_data = None
def update_tasks():
    global old_data
    tasks.clear()
    print("Updating")

    # This is when asking for logs to only ask for the one that are from current day.
    t_time = datetime.datetime.utcnow().time()
    timestamp = datetime.datetime.now().timestamp()
    time_from_midnight = t_time.second + t_time.minute * 60 + t_time.hour * 3600
    time_day_start = int(timestamp - time_from_midnight)

    info = get_request(
        f'https://api.torn.com/user/?selections=cooldowns,profile,refills,networth,bars,icons&key={API_key}')
    count_logs = str(get_request(
        f'https://api.torn.com/user/?selections=log&log=5360,7815,7810,7805,8370,8371,6005&from={time_day_start}&key={API_key}'))

    if old_data is None:
        old_data = get_stats(time_day_start)

    new_data = get_stats()

    # -Used log IDs
    # 5360 - Bust success                       # Can be rewritten to use stats
    # 7815 - Missions complete                  # Can be rewritten to use stats
    # 7810 - Missions fail
    # 7805 - Missions decline
    # 8370 - Casino spin the wheel start
    # 8371 - Casino spin the wheel free spin
    # 6005 - Rehab

    # Notes
    # NPC_shop should be skipped automatically when is completed.

    # count_newsletter = str(get_request(f'https://api.torn.com/user/?selections=log&log=400,401&key={API_key}'))

    # Drug cooldown task
    drugs = int(info.get("cooldowns").get("drug"))
    if drugs < 60 and int(settings.value("drug")):
        tasks.append(Task("Take a drug!", 1, ID=1, image=("icons/drugs.png")))

        # Medical cooldown task
    medical = int(info.get("cooldowns").get("medical"))
    life = (float(info.get("life").get("current")) / float(info.get("life").get("maximum"))) * 100
    if medical < 14400 and life > 90 and int(
            settings.value("medical")):  # set up that you get task only if you can fill 3 blood packs
        tasks.append(Task("Fill a Blood Bag", 5, ID=1, image=str("icons/medical.png")))

    # Booster cooldown task
    booster = int(info.get("cooldowns").get("booster"))
    if booster < 28800 and int(settings.value("booster")):
        tasks.append(Task("Go Drink some Beer", 4, ID=1, image="icons/booster.png"))

    # Energy refill task
    refills = info.get("refills")
    if not bool(refills.get("energy_refill_used")) and int(settings.value("energy_refill")):
        tasks.append(Task("Use Your Energy Refill", 4, link=link("https://www.torn.com/points.php"), ID=2,
                          image="icons/e_refill.png"))

    # Nerve refill task
    if not bool(refills.get("nerve_refill_used")) and int(settings.value("nerve_refill")):
        tasks.append(Task("Use Your Nerve Refill", 5, link=link("https://www.torn.com/points.php"), ID=2))

    # Casino refill task
    if not bool(refills.get("token_refill_used")) and int(settings.value("casino_refill")):
        tasks.append(Task("Use Your Casino Tokens Refill", 5, link=link("https://www.torn.com/points.php"), ID=2,
                          image="icons/casino.png"))

    # Energy task
    energy = info.get("energy")
    if int(energy.get("current")) == int(energy.get("maximum")) and int(settings.value("energy")):
        tasks.append(Task("Spend your Energy on something", 2, ID=3, image="icons/energy.png"))

    # Crime task
    nerve = info.get("nerve")
    if int(nerve.get("current")) >= int(nerve.get("maximum")) and int(settings.value("nerve")):
        tasks.append(Task("Go Commit some Crimes", 2, link=link("https://www.torn.com/crimes.php"), ID=3,
                          image="icons/crimes.png"))

    # Unpaidfees task
    unpaidfees = int(info.get("networth").get("unpaidfees"))
    if unpaidfees != 0 and int(settings.value("bills")):
        tasks.append(
            Task("You have -${:,} unpaidfees".format(-1 * unpaidfees), 6, link("https://www.torn.com/loan.php"), ID=5,
                 image="icons/fees.png"))

    # Racing task
    race = info.get("icons").get("icon17")
    if race is None and int(settings.value("race")):
        tasks.append(Task("You should enter a race", 5, link=link("https://www.torn.com/loader.php?sid=racing"), ID=8,
                          image="icons/race.png"))

    # Rehab task
    addiction = info.get("icons").get("icon57")
    if addiction is not None and int(settings.value("rehab")):
        addiction = str(addiction)[(addiction.find("(") + 1):addiction.find(")")].strip("-%")
        if int(addiction) >= 3:
            tasks.append(Task("Go to rehab", 6, ID=11, image="icons/rehab.png"))

    # Employee effectiveness
    # If player already rehab that day, don't show
    # We have to do this since addiction only updates once a day,
    # So the task would stay there long after the player already rehabbed
    rehab = count_logs.count("g\': 6005")
    if not rehab and int(settings.value("rehab")):
        company = get_request(f'https://api.torn.com/company/?selections=employees&key={API_key}')
        addiction = company.get("company_employees").get(str(info.get("player_id"))).get("effectiveness").get(
            "addiction")
        if addiction <= int(settings.value("addiction_amount")) * -1 and not rehab:
            tasks.append(Task("Go to rehab your addiction is {}".format(addiction), 6, ID=11))

    # #Use newsletter bonus
    # newsletters_unused = count_newsletter.count("g\': 400") - count_newsletter.count("g\': 401")
    # if newsletters_unused > 0 :
    #     tasks.append(Task("You have {} unused newsletter bonuses".format(newsletters_unused),10,ID=15))

    # Daily mission task
    missions = count_logs.count("g\': 7815") + count_logs.count("g\': 7810") + count_logs.count("g\': 7805")
    if missions == 0 and int(settings.value("missions")):
        tasks.append(
            Task("Complete your daily mission", 9, link=link("https://www.torn.com/loader.php?sid=missions"), ID=6,
                 image="icons/mission.png"))

    # Bust people task
    if settings.value("busts_number") is not None:  # Just in case value doesn't exist in setting.
        busts = count_logs.count("g\': 5360")
        busts_number = int(settings.value("busts_number"))
        if busts < busts_number and int(settings.value("busts")):
            tasks.append(
                Task("Do {} more busts".format(busts_number - busts), 7, link=link("https://www.torn.com/jailview.php"),
                     ID=7, image="icons/busts.png"))
            pass

    # Wheel of Fortune
    spins = count_logs.count("g\': 8370") - count_logs.count("g\': 8371")
    if spins < 3 and int(settings.value("wheels")):
        tasks.append(Task("You have {} wheels to be spun".format(3 - spins), 8,
                          link=link("https://www.torn.com/loader.php?sid=spinTheWheel"), ID=10,
                          image="icons/wheel.png"))

    # Buy stuff at NPC shop task
    if int(settings.value("npc")):
        npc_shop = get_request(
            f'https://api.torn.com/user/?selections=log&log=4200&from={time_day_start}&key={API_key}')
        npc_shop = npc_shop.get("log")
        brought = 0
        if npc_shop is not None:
            for log in npc_shop:
                brought += int(npc_shop.get(log).get("data").get("quantity"))
        if brought < 100 and int(settings.value("npc")):
            tasks.append(Task("Buy {} items at NPC shop".format(100 - brought), 9,
                              link=link("https://www.torn.com/shops.php?step=bitsnbobs"), ID=8,
                              image="icons/bits_bobs.png"))

    if int(settings.value("virus")):
        virus = get_request(f'https://api.torn.com/user/?selections=log&log=5800,5801,5802&key={API_key}')
        virus = virus.get("log")
        if virus[next(iter(virus))].get("log") != 5800:
            tasks.append(Task("Start programming virus", 9, ID=10, image="icons/virus.png"))

    for stat in settings.value("customs", defaultValue=[]):
        key = stat.get("stat")

        value = stat.get("value", 0) - (new_data[key] - old_data[key])

        if value > 0:
            description = str(stat.get("description")).replace("${value}", str(value))
            tasks.append(Task(description, 9, image="icons/note.png", link=link("https://www.torn.com/index.php")))

    reorder_task()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)  # not sure what it does
    window = MainWindow()  # initializing window.py basically
    window.show()

    if len(settings.allKeys()) != 19:  # Checks if setting file exists if no forces window to generate new one.
        window.generate_save_file()

    if settings.value("API_key") is not None:  # inserst key in to field if it exists.
        window.textEdit.setText(settings.value("API_key"))

    """
    Below code can be used to get data for statistics.txt
    key = settings.value("API_key")
    data = get_request(f'https://api.torn.com/user/?selections=personalstats&key={key}').get("personalstats")

    print(data)

    for key in data:
        print(key)

    """

    app.exec()


from stack_diy import Stack

def iostep(in_cabin, out_cabin):
    # <將你的程式碼寫在這裡>
    # 記得要 return，回傳值型態為 list
    stack_step = []                                 # 欲回傳之list先定義為空的list
    stack_len = len(in_cabin)
    st = Stack(stack_len)

    for i in in_cabin:
        st.push(i)                                  # push進st裡面
        stack_step.append('push')                   # stack_step加入push
        for j in range(st.top, -1,-1):              # 從st往回檢查
            if st.data[j] == out_cabin[0]:
                st.pop()                            # pop進st裡面
                stack_step.append('pop')            # stack_step加入pop
                out_cabin.remove(out_cabin[0])
            else:
                break
    if st.top != -1:                                # 若st內還有值
        stack_step = []                             # 則回傳值為空的list

    return stack_step

if __name__ == "__main__":
    step = iostep(in_cabin = [1, 2, 3], out_cabin = [2, 1, 3])
    print(step)

#留言板


import datetime

from django.shortcuts import redirect

from core.tasks import send_email
from core.models import User, Product, ProductPackage, \
    ClientPackages, Transaction, Notification

def generate_buyer_token(buyer):
    """
    GET BUYER ID
    GET THE DATEANDTIME
    GENERATE TOKEN BY ADDING THE BUYER ID + DATEANDTIME
    """
    return f"{buyer.id}{datetime.datetime.now().isoformat().replace('-', '').replace(':', '').replace('.', '')}"

def complete_product_purchase(data):
    """
    COMPLETE PRODUCT PURCHASE:
    GET PRODUCT PACKAGE
    GET PRODUCT BUYER
    GET SELLER
    CREATE CLIENT PACKAGE[SO USER CAN VIEW PRODUCT IN DASHBOARD]
    IF API PRODUCT:
        GENERATE TOKEN
        APPEND THE NO OF REUQESTS
    TRANSACTION LOG TO SUBRACT AMOUNT FROM BUYER
    TRANSACTION LOG TO ADD AMOUNT TO SELLER
    """
    product_package = ProductPackage.objects.get(id=data['product_package_id'])
    buyer = User.objects.get(id=data['uid'])
    seller = product_package.service.owner

    if product_package.service.product_type == 'A':
        cp = ClientPackages.objects.create(
            package=product_package,
            user=buyer,
            token=generate_buyer_token(buyer),
            amount_paid=product_package.price
        )
        cp.normal_requests_left = cp.normal_requests_left + product_package.normal_requests
        cp.save()
    else:
        ClientPackages.objects.create(
            package=product_package,
            user=buyer,
            token="LIFETIME-TOKEN",
            amount_paid=product_package.price,
        )
    # SUBRACT AMOUNT FROM BUYER
    Transaction.objects.create(
        coins=product_package.price,
        user=buyer,
        content='Product Purchase',
        type=1
    )
    send_email.delay(
        buyer.email,
        'Product Purchase Successful',
        f'Product Purchased successfully.\n{product_package.price}$ is subracted from your wallet.'
    )
    # ADD AMOUNT TO SELLER
    Transaction.objects.create(
        coins=product_package.price,
        user=seller,
        content=f'{buyer.username} purchased your product.',
        type=0
    )
    # SERVICE FEE SUBRACTING
    service_fee = product_package.price // 10
    Transaction.objects.create(
        coins=service_fee,
        user=seller,
        content=f'[SERVICE FEE] {buyer.username} purchased your product.',
        type=1
    )
    # ADD SERVICE FEE TO ADMIN ACCOUNT
    Transaction.objects.create(
        coins=service_fee,
        user=User.objects.get(username='admin'),
        content=f'{buyer.username} purchased your product.',
        type=0
    )
    send_email.delay(
        seller.email,
        'Product Purchase',
        f'{buyer.username} purchased your product.\n{product_package.price}$ is added to your wallet.'
    )


import cv2
import numpy as np

def drawCNTs(cropped, area_cnts):

    cnt_cropped = np.copy(cropped) * 2  # creating a blank to draw cnts on
        # # obtáhnutí vybraných kontur
    for cnt in area_cnts:
        for pix in cnt:
            for px in pix:
                i = int(px[1])
                j = int(px[0])
                # print(i, "           ", j)
                # print(img[i][j])
                cnt_cropped[i][j] = 255
    return cnt_cropped


import numpy as np
from scipy import optimize
from scipy.spatial.distance import pdist, squareform
try:
    from sklearn.preprocessing import OrdinalEncoder
    UseOrdinalEncoder = True
except ImportError:
    UseOrdinalEncoder = False
from numpy.testing import assert_array_almost_equal
from numpy.testing import assert_array_equal
import pytest

from skopt.learning.gaussian_process import GaussianProcessRegressor
from skopt.learning.gaussian_process.kernels import ConstantKernel
from skopt.learning.gaussian_process.kernels import DotProduct
from skopt.learning.gaussian_process.kernels import ExpSineSquared
from skopt.learning.gaussian_process.kernels import HammingKernel
from skopt.learning.gaussian_process.kernels import Matern
from skopt.learning.gaussian_process.kernels import RationalQuadratic
from skopt.learning.gaussian_process.kernels import RBF
from skopt.learning.gaussian_process.kernels import WhiteKernel

KERNELS = []

for length_scale in [np.arange(1, 6), [0.2, 0.3, 0.5, 0.6, 0.1]]:
    KERNELS.extend([
        RBF(length_scale=length_scale),
        Matern(length_scale=length_scale, nu=0.5),
        Matern(length_scale=length_scale, nu=1.5),
        Matern(length_scale=length_scale, nu=2.5),
        RationalQuadratic(alpha=2.0, length_scale=2.0),
        ExpSineSquared(length_scale=2.0, periodicity=3.0),
        ConstantKernel(constant_value=1.0),
        WhiteKernel(noise_level=2.0),
        Matern(length_scale=length_scale, nu=2.5) ** 3.0,
        RBF(length_scale=length_scale) + Matern(length_scale=length_scale,
                                                nu=1.5),
        RBF(length_scale=length_scale) * Matern(length_scale=length_scale,
                                                nu=1.5),
        DotProduct(sigma_0=2.0)
    ])

# Copied (shamelessly) from sklearn.gaussian_process.kernels
def _approx_fprime(xk, f, epsilon, args=()):
    f0 = f(*((xk,) + args))
    grad = np.zeros((f0.shape[0], f0.shape[1], len(xk)), float)
    ei = np.zeros((len(xk), ), float)
    for k in range(len(xk)):
        ei[k] = 1.0
        d = epsilon * ei
        grad[:, :, k] = (f(*((xk + d,) + args)) - f0) / d[k]
        ei[k] = 0.0
    return grad

def kernel_X_Y(x, y, kernel):
    X = np.expand_dims(x, axis=0)
    Y = np.expand_dims(y, axis=0)
    return kernel(X, Y)[0][0]

def numerical_gradient(X, Y, kernel, step_size=1e-4):
    grad = []
    for y in Y:
        num_grad = optimize.approx_fprime(X, kernel_X_Y, step_size, y, kernel)
        grad.append(num_grad)
    return np.asarray(grad)

def check_gradient_correctness(kernel, X, Y, step_size=1e-4):
    X_grad = kernel.gradient_x(X, Y)
    num_grad = numerical_gradient(X, Y, kernel, step_size)
    assert_array_almost_equal(X_grad, num_grad, decimal=3)

@pytest.mark.fast_test
@pytest.mark.parametrize("kernel", KERNELS)
def test_gradient_correctness(kernel):
    rng = np.random.RandomState(0)
    X = rng.randn(5)
    Y = rng.randn(10, 5)
    check_gradient_correctness(kernel, X, Y)

@pytest.mark.fast_test
@pytest.mark.parametrize("random_state", [0, 1])
@pytest.mark.parametrize("kernel", KERNELS)
def test_gradient_finiteness(random_state, kernel):
    """
    When x is the same as X_train, gradients might become undefined because
    they are divided by d(x, X_train).
    Check they are equal to numerical gradients at such points.
    """
    rng = np.random.RandomState(random_state)
    X = rng.randn(5).tolist()
    Y = [X]
    check_gradient_correctness(kernel, X, Y, 1e-6)

@pytest.mark.fast_test
def test_distance_string():
    # Inspired by test_hamming_string_array in scipy.tests.test_distance
    a = np.array(['eggs', 'spam', 'spam', 'eggs', 'spam', 'spam', 'spam',
                  'spam', 'spam', 'spam', 'spam', 'eggs', 'eggs', 'spam',
                  'eggs', 'eggs', 'eggs', 'eggs', 'eggs', 'spam'],
                 dtype='|S4')
    b = np.array(['eggs', 'spam', 'spam', 'eggs', 'eggs', 'spam', 'spam',
                  'spam', 'spam', 'eggs', 'spam', 'eggs', 'spam', 'eggs',
                  'spam', 'spam', 'eggs', 'spam', 'spam', 'eggs'],
                 dtype='|S4')
    true_values = np.array([[0, 0.45], [0.45, 0]])
    X = np.vstack((a, b))
    hm = HammingKernel()
    assert_array_almost_equal(-np.log(hm(X)) / 20.0, true_values)

@pytest.mark.fast_test
def test_isotropic_kernel():
    rng = np.random.RandomState(0)
    X = rng.randint(0, 4, (5, 3))
    hm = HammingKernel()

    # Scipy calulates the mean. We need exp(-sum)
    hamming_distance = squareform(pdist(X, metric='hamming'))
    scipy_dist = np.exp(-hamming_distance * X.shape[1])
    assert_array_almost_equal(scipy_dist, hm(X))

@pytest.mark.fast_test
def test_anisotropic_kernel():
    rng = np.random.RandomState(0)
    X = rng.randint(0, 4, (5, 3))
    hm = HammingKernel()
    X_kernel = hm(X)
    hm_aniso = HammingKernel(length_scale=[1.0, 1.0, 1.0])
    X_kernel_aniso = hm_aniso(X)
    assert_array_almost_equal(X_kernel, X_kernel_aniso)

    hm = HammingKernel(length_scale=2.0)
    X_kernel = hm(X)
    hm_aniso = HammingKernel(length_scale=[2.0, 2.0, 2.0])
    X_kernel_aniso = hm_aniso(X)
    assert_array_almost_equal(X_kernel, X_kernel_aniso)

@pytest.mark.fast_test
def test_kernel_gradient():
    rng = np.random.RandomState(0)
    hm = HammingKernel(length_scale=2.0)
    X = rng.randint(0, 4, (5, 3))
    K, K_gradient = hm(X, eval_gradient=True)
    assert_array_equal(K_gradient.shape, (5, 5, 1))

    def eval_kernel_for_theta(theta, kernel):
        kernel_clone = kernel.clone_with_theta(theta)
        K = kernel_clone(X, eval_gradient=False)
        return K

    K_gradient_approx = _approx_fprime(
        hm.theta, eval_kernel_for_theta, 1e-10, (hm,))
    assert_array_almost_equal(K_gradient_approx, K_gradient, 4)

    hm = HammingKernel(length_scale=[1.0, 1.0, 1.0])
    K_gradient_approx = _approx_fprime(
        hm.theta, eval_kernel_for_theta, 1e-10, (hm,))
    K, K_gradient = hm(X, eval_gradient=True)
    assert_array_equal(K_gradient.shape, (5, 5, 3))
    assert_array_almost_equal(K_gradient_approx, K_gradient, 4)

    X = rng.randint(0, 4, (3, 2))
    hm = HammingKernel(length_scale=[0.1, 2.0])
    K_gradient_approx = _approx_fprime(
        hm.theta, eval_kernel_for_theta, 1e-10, (hm,))
    K, K_gradient = hm(X, eval_gradient=True)
    assert_array_equal(K_gradient.shape, (3, 3, 2))
    assert_array_almost_equal(K_gradient_approx, K_gradient, 4)

@pytest.mark.fast_test
def test_Y_is_not_None():
    rng = np.random.RandomState(0)
    hm = HammingKernel()
    X = rng.randint(0, 4, (5, 3))

    hm = HammingKernel(length_scale=[1.0, 1.0, 1.0])
    assert_array_equal(hm(X), hm(X, X))

@pytest.mark.fast_test
def test_gp_regressor():
    rng = np.random.RandomState(0)
    X = np.asarray([
        ["ham", "spam", "ted"],
        ["ham", "ted", "ted"],
        ["ham", "spam", "spam"]])
    y = rng.randn(3)
    hm = HammingKernel(length_scale=[1.0, 1.0, 1.0])
    if UseOrdinalEncoder:
        enc = OrdinalEncoder()
        enc.fit(X)

    gpr = GaussianProcessRegressor(hm)
    if UseOrdinalEncoder:
        gpr.fit(enc.transform(X), y)
        assert_array_almost_equal(gpr.predict(enc.transform(X)), y)
        assert_array_almost_equal(gpr.predict(enc.transform(X[:2])), y[:2])
    else:
        gpr.fit(X, y)
        assert_array_almost_equal(gpr.predict(X), y)
        assert_array_almost_equal(gpr.predict(X[:2]), y[:2])


import pygame
from cell import *

class Button:
    def __init__(self, WIN):
        self.FONT = pygame.font.Font('font/8-BIT WONDER.ttf', 20)
        self.WIN = WIN

    def draw_buttons(self):
        start_text = self.FONT.render("Start", 1, LIGHT_GRAY)
        reset_text = self.FONT.render("Reset",1, LIGHT_GRAY)
        pause_text = self.FONT.render("Pause", 1, LIGHT_GRAY)
        self.WIN.blit(start_text, (153, 105))
        self.WIN.blit(pause_text, (353, 105))
        self.WIN.blit(reset_text, (553, 105))

    def button_hover(self,mouse):
        # start button()
        if mouse[0] >= 150 and mouse[0] <= 250 and mouse[1] >= 100 and mouse[1]<= 140:
            pygame.draw.rect(self.WIN, DARK_GREEN, [150, 100, 100, 40])
        else:
            pygame.draw.rect(self.WIN, GREEN, [150, 100, 100, 40])
        # pause button
        if mouse[0] >= 350 and mouse[0] <= 450 and mouse[1] >= 100 and mouse[1]<= 140:
            pygame.draw.rect(self.WIN, DARK_BLUE, [350, 100, 100, 40])
        else:
            pygame.draw.rect(self.WIN, BLUE, [350, 100, 100, 40])
        # stop button
        if mouse[0] >= 550 and mouse[0] <= 650 and mouse[1] >= 100 and mouse[1]<= 140:
            pygame.draw.rect(self.WIN, DARK_RED, [550, 100, 100, 40])
        else:
            pygame.draw.rect(self.WIN, RED, [550, 100, 100, 40])


from elasticmagic import agg, types
from elasticmagic.result import SearchResult

def test_search_result_with_error_and_aggregations():
    raw_result = {'error': True}
    res = SearchResult(
        raw_result,
        aggregations={'types': agg.Terms(field='type', type=types.Integer)}
    )
    assert res.aggregations['types'].buckets == []


import discord
from discord import ApplicationContext
from discord.commands import slash_command
from discord.ext import commands

from ..archive import cfg

from ..archive import embed_builder

class Portal(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @slash_command(name="gtool")
    async def portal(self, ctx: ApplicationContext):
        """原神でお世話になりがちなリンク集を送り返します"""
        embed = embed_builder._portal()
        await ctx.respond(embed=embed, view=PortalView(), ephemeral=True)
        return

class PortalView(discord.ui.View):
    def __init__(self):
        super().__init__(timeout=None)
        self.add_item(discord.ui.Button(label="HoYo Lab", url=cfg.hoyo_lab, row=0))
        self.add_item(discord.ui.Button(label="コード受け取り", url=cfg.redeem_code, row=0))
        self.add_item(discord.ui.Button(label="ログインボーナス", url=cfg.login_bonus, row=0))
        self.add_item(discord.ui.Button(label="公式マップ", url=cfg.official_map, row=0))
        self.add_item(discord.ui.Button(label="非公式マップ", url=cfg.unofficial_map, row=1))
        self.add_item(
            discord.ui.Button(label="shinshin.moe", url=cfg.shinshin_moe, row=1)
        )
        self.add_item(discord.ui.Button(label="paimon.moe", url=cfg.paimon_moe, row=1))
        self.add_item(discord.ui.Button(label="ダメージ計算機", url=cfg.damage_calc, row=1))

def setup(bot):
    return bot.add_cog(Portal(bot))


import cv2
import numpy as np
import time

t0 = time.time()

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    if ret:
        t1 = time.time() - t0
        t1_str = str(round(t1, 2))
        frame = cv2.flip(frame, 1)
        text = f"Amir Madankan {t1_str}"
        cv2.putText(frame, text, (100, 100), cv2.FONT_HERSHEY_SIMPLEX,
                    1, (0, 0, 255), 1)
        frame_LD = 255 - frame
        frame_RD = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        n = frame_RD.shape[0]
        m = frame_RD.shape[1]
        frame_RD = frame_RD.reshape((n, m, 1))
        frame_RD = np.concatenate([frame_RD, frame_RD, frame_RD], 2)
        frame_RU = frame.copy()
        frame_RU[:, :, 2] = 255
        frame_new1 = np.concatenate((frame, frame_RU), axis=1)
        frame_new2 = np.concatenate((frame_LD, frame_RD), axis=1)
        frame_new = np.concatenate((frame_new1, frame_new2), axis=0)
        cv2.imshow("webcam", frame_new)
        q = cv2.waitKey(1)
        if q == ord("q"):
            break

cv2.destroyAllWindows()
cap.release()


from Header import Header
from SequenceData import SequenceData
from RecognizerFactory import RecognizerFactory
from estremo_utils import wc

import string

class Paragraph(object):
    def __init__(self,paragraph_lines,ignore_recognizer=False):
        self.paragraph_lines = paragraph_lines
        self.header = Header(paragraph_lines[0])
        #Paragraph can't end with empty line!
        seq_lines = [line for line in paragraph_lines[1:]
                     if line[0] in string.uppercase]
        recognizer_lines = [line for line in paragraph_lines[1:]
                                if not line[0] in string.uppercase]
        self.seq_data = map(SequenceData,seq_lines)
        if not ignore_recognizer:
            recognizer_data = [[float(val) for val in line.split(",")]
                               for line in recognizer_lines]
        else:
            recognizer_data = [[0 for i in range(len(self.seq_data[0].site))]
                               for j in range(4)]
        self.recognizer = RecognizerFactory(recognizer_data)

    def toFasta(self):
        return "\n".join([">Site %s\n%s" % (i,seq.site)
                          for i,seq in enumerate(self.seq_data)])
    def motif(self):
        return [seq.site for seq in self.seq_data]

    def adjusted_motif(self):
        def adjust(site):
            if self.recognizer.score(site) < self.recognizer.score(wc(site)):
                return site
            else:
                return wc(site)
        return [adjust(site) for site in self.motif()]


import datetime
import platform
import socket
import re
import uuid
import psutil
import logging

import matplotlib.pyplot as plt
from datetime import date

def base36encode(number):
    base36, sign, alphabet = str(), str(), '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    if number < 0:
        sign = '-'
        number = -number
    if 0 <= number < len(alphabet):
        return sign + alphabet[number]
    while number != 0:
        number, i = divmod(number, len(alphabet))
        base36 = alphabet[i] + base36
    return sign + base36

def create_code():
    actual_datetime = datetime.datetime.now()
    actual_datetime = actual_datetime.strftime("1%d%m%y%H%M%S%f")
    return str(base36encode(int(actual_datetime)))

def getSystemInfo():
    try:
        info={}
        info['platform']=platform.system()
        info['platform-release']=platform.release()
        info['platform-version']=platform.version()
        info['architecture']=platform.machine()
        info['hostname']=socket.gethostname()
        info['ip-address']=socket.gethostbyname(socket.gethostname())
        info['mac-address']=':'.join(re.findall('..', '%012x' % uuid.getnode()))
        info['processor']=platform.processor()
        info['ram']=str(round(psutil.virtual_memory().total / (1024.0 **3)))+" GB"
        return info
    except Exception as e:
        return logging.exception(e)

def create_graphics(x_data: list, y_data: list, second_data: list = None, title: str = 'grafico', legends: list = ['linha a', 'linha b']):

    eixo_x, eixo_y = x_data, y_data
    label_a, label_b = legends[0], legends[1]
    plt.title(title)
    plt.plot(eixo_x, eixo_y, color="red", label=label_a)

    if second_data is not None:
        eixo_second_x, eixo_second_y = second_data[0], second_data[1]
        plt.plot(eixo_second_x, eixo_second_y, color="blue", label=label_b)

    plt.xticks(rotation=45)
    plt.legend();
    plt.savefig('c:/tmp/grafico_line.png')
    plt.close()

def create_graphic_bar(x_data):
    plt.hist(x_data, 5, facecolor='blue', alpha=0.5)
    plt.title('Grafico Histograma')
    plt.xticks(rotation=45)
    plt.savefig('c:/tmp/grafico_bar.png')
    plt.close()

def create_graphic_pizza(data: dict):
    sizes = data['sizes']
    explode = data['explode']
    labels = data['labels']
    plt.pie(
        sizes,
        explode=explode,
        labels=labels,
        autopct='%1.1f%%',
        shadow=True,
        startangle=90
    )
    plt.title('Grafico Pizza')
    plt.savefig('c:/tmp/grafico_pie.png')
    plt.close()

def format_num(num, f=False):
        a = '{:,.0f}'.format(float(num)) if not f else '{:,.2f}'.format(float(num))
        b = a.replace(',', 'v')
        c = b.replace('.', ',')
        d = c.replace('v', '.')
        return f'{d}'

def get_week_day():
    num = date.today().weekday()
    sem = ("Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado", "Domingo")
    return f"{sem[num]}-feira" if num < 5 else f"{sem[num]}"


import torch
from torch import nn
from torch.nn.utils.rnn import pack_padded_sequence
import torch.nn.functional as F
from downstream.models.tdnnf import get_tdnnf_model

class TDNNFClassifier(nn.Module):
    """
        This class defines TDNNF architecture

        Args:
            input_dim: Dimension for the input features (int, required)
            output_dim: Dimension for the output (int, requred)
            config: configuration file containing tdnnf specific information
    """
    def __init__(self, input_dim, output_dim, config):
        super(TDNNFClassifier, self).__init__()

        self.output_subsampling = config['output_subsampling']
        self.pdrop = config['drop']
        self.size = config['size']
        self.input_dim = input_dim
        self.output_dim = output_dim

        self.tdnnf = get_tdnnf_model(
            input_dim,
            output_dim,
            self.size,
            self.pdrop
        )

    def forward(self, features):
        chain_out, xent_out = self.tdnnf(features)
        return chain_out, xent_out


from tictactoe.Game import *
import random
import torch as t
import pandas as pd

x_size = 9
a1_size = 81
a2_size = 45
a3_size = 9

model = t.nn.Sequential(
    t.nn.Linear(x_size, a1_size),
    t.nn.ReLU(),
    t.nn.Linear(a1_size, a2_size),
    t.nn.ReLU(),
    t.nn.Linear(a2_size, a3_size))

criterion = t.nn.MSELoss()
optimizer = t.optim.Adam(model.parameters())

pc_first_win_lose_confs = pd.read_csv("q_learning_meta/pc_first_win_lose_states.txt")
pc_first_win_lose_confs = dict(list(pc_first_win_lose_confs.itertuples(index=False, name=False)))

human_first_win_lose_confs = pd.read_csv("q_learning_meta/human_first_win_lose_states.txt")
human_first_win_lose_confs = dict(list(human_first_win_lose_confs.itertuples(index=False, name=False)))

epochs = 300000
epsilon = .1
get_random_valid_move = lambda g: random.choice(list(g.valid_moves.keys()))
get_best_move = lambda q, valid_moves: next(e for e in map(lambda e: e[0], sorted(list(enumerate(q[0])), reverse=True, key=lambda e: e[1])) if e in valid_moves)

avg_losses = []
losses = 0
counter = 0
import time
start = time.time()
for epoch in range(epochs):
    x_first = random.random() < .5
    game = Game(X if x_first else O)
    if x_first:
        game.make_move(get_random_valid_move(game))

    state = t.Tensor(game.board.reshape(1, -1))
    y_turn = True
    while True:
        if y_turn:
            q = model(state)
            valid_moves = game.valid_moves.keys()
            if random.random() < epsilon:
                move = get_random_valid_move(game)
            else:
                move = get_best_move(q, valid_moves)

            game.make_move(move)
            board_to_str = lambda b: ''.join(map(lambda s: str(s), b))

            # only train weights associated with the for sure output.
            # technically we can train every weight and that's better, since we know the whole winning/lose config, but this is just to practice reinforcement rythm
            # and then maybe practice using batch/experience-replay to get around this fact just to practice reinforcement rythms..
            y = t.zeros(q.data.shape)
            y[:] = q.data[:]

            if x_first:
                reward = human_first_win_lose_confs[board_to_str(game.board.reshape(-1))]
            else:
                reward = pc_first_win_lose_confs[board_to_str(game.board.reshape(-1))]

            y[0][move] = reward
            loss = criterion(q, y)
            losses += loss.item()
            counter += 1
            if counter % 1000 == 0:
                avg_losses.append(str(losses / 1000))
                losses = 0

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            y_turn = False
        else:
            game.make_move(get_random_valid_move(game))
            y_turn = True
        state = t.Tensor(game.board.reshape(1, -1))
        if game.is_over():
            break
    if epsilon > 0.05:
        epsilon -= 2 / epochs

t.save(model.state_dict(), "q_learning_meta/model.pth")
f = open("q_learning_meta/losses_over_time", "w")
f.write("\n".join(avg_losses))

end = time.time()
print(end - start)


class Audition:
    all = []
    def __init__(self, actor, location, hired, role):
        self.actor = actor
        self.location = location
        self.hired = hired
        self.role = role
        Audition.all.append( self )

    def call_back( self):
        self.hired = True


"""Write experimental results data to output files."""

# System
import logging
import os
from collections import namedtuple
from functools import reduce

# Third party
from pyrsistent import v, pmap
from cytoolz.itertoolz import interleave
from more_itertools import chunked
import numpy as np

ResultsDescriptor = namedtuple('ResultsDescriptor', ('interval', 'output_path', 'keys'))

def read_results(results_path):
    """Read into a numpy array all the values in the given file path."""
    return np.loadtxt(results_path, skiprows=1)

def results_logger():
    """Construct and return the reuslts logger."""
    logger = logging.getLogger("results_logger")
    if not logger.handlers:
        logger.propagate = False
        logger.setLevel(logging.INFO)
        ch = logging.StreamHandler()
        ch.setLevel(logging.INFO)
        formatter = logging.Formatter('%(message)s')
        ch.setFormatter(formatter)
        logger.addHandler(ch)
    return logger

def output_stdout(output_string, output_path):
    """Given some output_string, print it to stdout and write it to the given output_path."""
    results_logger().info(output_string)

def output_file(output_string, output_path):
    """Given some output_string, write it to the given output_path."""
    with open(output_path, 'a') as output_file:
        output_file.write(output_string + '\n')

def merge_results(results):
    """
    Given a list of dictionary results from episodes and the interesting keys, merge them into a single dictionary.
    Example: [{episode_id: 1, steps: 22}, {episode_id: 2, steps: 30}] -> {episode_id: [1, 2], steps: [22, 30]}
    """
    seed_dictionary = pmap({key: v() for key, _ in results[0].items()})
    return pmap(reduce(lambda result1, y: {key: value.append(y[key]) for key, value in result1.items()}, [seed_dictionary] + results))

def initialize_results(results_descriptor):
    """Remove any conflicting results file and create the new one with its header."""
    output_path = results_descriptor.output_path
    keys = results_descriptor.keys
    try:
        os.remove(output_path)
    except OSError:
        pass
    keys_string = ' '.join(key for key in keys)
    output_file(keys_string, output_path)

def write_results(results, results_descriptor):
    """Output the given results to terminal and to file."""
    output_path = results_descriptor.output_path
    keys = results_descriptor.keys
    value_vectors = (results[key] for key in keys)
    rows = chunked(interleave(value_vectors), len(keys))
    string_rows = map(lambda v: ' '.join(str(x) for x in v), rows)
    all_string_rows = '\n'.join(string_row for string_row in string_rows)
    keys_string = ' '.join(key for key in keys)
    output_stdout(keys_string + '\n' + all_string_rows, output_path)
    output_file(all_string_rows, output_path)


from classes.account import Account
from classes.address import Address

class College(Account):
  def __init__(self, name: str, address: Address, phone, enrollment: int) -> None:
    super().__init__(name, address, phone)
    self.enrollment = enrollment

  def generate_sql(self):
    return f"""
      INSERT into colleges(name, address1, city, state, zip, phone, soundex, enrollment)
      VALUES(%s, %s, %s, %s ,%s, %s, %s, %s)
    """

  def execute_sql(self, cursor, sql):
    cursor.execute(sql, (self.name,
        self.address.address_1,
        self.address.city,
        self.address.state,
        self.address.zip,
        self.phone,
        self.soundex,
        self.enrollment)
    )


# NOTE: Use your the virtual machine to execute this code and
#       keep the memory allocated for the machine to 2GB!

import time
import numpy
from sklearn.neighbors import KNeighborsRegressor

# load data
print("Loading training data ...")
data_train = numpy.genfromtxt("data/train.csv", comments="#", delimiter=",")
Xtrain, ytrain = data_train[:,:-1], data_train[:,-1]
print("Loaded training data: n=%i, d=%i" % (Xtrain.shape[0], Xtrain.shape[1]))

# training phase (DO NOT CHANGE PARAMETERS)
print("Fitting model ...")
model = KNeighborsRegressor(n_neighbors=10, algorithm="brute")
model.fit(Xtrain, ytrain)
print("Model fitted!")

# application phase (apply model to test set)
print("Loading testing data ...")
data_test = numpy.genfromtxt("data/test.csv", comments="#", delimiter=",")
Xtest, ytest = data_test[:,:-1], data_test[:,-1]
print("Loaded testing data: n=%i, d=%i" % (Xtest.shape[0], Xtest.shape[1]))

# create store and data sets
import h5py
store = h5py.File("data/test.csv.h5", 'w')
ytest = ytest.reshape((len(ytest),1))
dsetX = store.create_dataset("X", Xtest.shape, compression="lzf")
dsety = store.create_dataset("y", ytest.shape, compression="lzf")
dsetX[:,:] = Xtest
dsety[:,:] = ytest
store.close()

preds = []
chunk_size = 5000

start = time.time()

store = h5py.File("data/test.csv.h5", 'r')
dsetX = store.get("X")
dsety = store.get("y")

chunk_start = 0
chunk_end = chunk_size

while True:
    try:
        Xchunk, ychunk = dsetX[chunk_start:chunk_end], dsety[chunk_start:chunk_end]
        preds += list(model.predict(Xchunk))
        print("Predictions computed for %i patterns ..." % len(preds))
        chunk_start += chunk_size
        chunk_end += chunk_size
    except:
        break

end = time.time()

# output (here, 'preds' must be a list containing all predictions)
print("Predictions computed for %i patterns ...!" % len(preds))
print("Mean of predictions: %f" % numpy.mean(numpy.array(preds)))
print("Runtime for testing phase: %f" % (end-start))


import pygame
from pygame import Vector2

class Interactable:
    def __init__(self, pos: tuple, size: tuple):
        self.rect: pygame.Rect = pygame.Rect(pos, size)

        self.is_hovering_over: bool = False

        self.is_clicked: bool = False
        self._was_clicked: bool = False

        self.is_held: bool = False

        self.is_released: bool = True

    def update(self, mouse_pos: tuple, interaction_starter: bool) -> None:
        if self.rect.collidepoint(mouse_pos):
            self.is_hovering_over = True
            if interaction_starter:

                self.is_clicked = True if not self.is_clicked and not self._was_clicked else False

                if self.is_clicked and not self._was_clicked:
                    self._was_clicked = True

                self.is_held = True
                self.is_released = False
            else:
                self._was_clicked = False
                self.is_released = True
                self.is_held = False
        else:
            self.is_hovering_over = False
            self.is_clicked = False
            self._was_clicked = False
            self.is_held = False
            self.is_released = True

    def debug_draw(self, display: pygame.Surface, debug_color: tuple) -> None:
        pygame.draw.rect(display, debug_color, self.rect, 1)


from argparse import Namespace
import random

from aiohttp.web import Application
from prometheus_client import Metric

from . import (
    MetricConfig,
    PrometheusExporterScript,
)

class SampleScript(PrometheusExporterScript):
    """A sample exporter."""

    name = "prometheus-aioexporter-sample"
    default_port = 9091

    def configure(self, args: Namespace) -> None:
        self.create_metrics(
            [
                MetricConfig(
                    "a_gauge", "a gauge", "gauge", {"labels": ["foo", "bar"]}
                ),
                MetricConfig(
                    "a_counter", "a counter", "counter", {"labels": ["baz"]}
                ),
            ]
        )

    async def on_application_startup(self, application: Application) -> None:
        application["exporter"].set_metric_update_handler(self._update_handler)

    async def _update_handler(self, metrics: dict[str, Metric]) -> None:
        metrics["a_gauge"].labels(
            foo=random.choice(["this-foo", "other-foo"]),
            bar=random.choice(["this-bar", "other-bar"]),
        ).set(random.uniform(0, 100))
        metrics["a_counter"].labels(
            baz=random.choice(["this-baz", "other-baz"]),
        ).inc(random.choice(range(10)))

script = SampleScript()


import scrapy

class MyspiderSpider(scrapy.Spider):
    name = 'MySpider'
    start_urls = ['https://www.net-a-porter.com/en-in/shop/clothing/tops']

    def parse(self, response):
        self.logger.info('A response from %s just arrived!', response.url)

        for container in response.css("div.ProductListWithLoadMore52__listingGrid").css("a"):
         yield {
              'name':container.css("span.ProductItem24__name::text").get(),
              'brand':container.css("span.ProductItem24__designer::text").get(),
              'sale_price': float(container.css("span.PriceWithSchema9__value").css("span::text").get().replace("$","").replace(",","")),
              'original_price': float(container.css("span.PriceWithSchema9__value").css("span::text").get().replace("$","").replace(",","")),
              'image_url': container.css("img.Image18__image::attr(src)").get(),
              'product_page_url': container.attrib['href'],
              'product_category':"topwear"
            }

        next_page =  response.css("a.Pagination7__next").attrib['href']
        if next_page is not None:
            yield response.follow(next_page,callback=self.parse)


import os
os.environ['SIDEKIT'] = 'theano=false,libsvm=false,mpi=false'
from torch.autograd import Variable
from torch import optim
import pyro.optim as pyro_optim
import logging
import torch
from torch import nn
import numpy
from torch.utils.data import DataLoader
from torchvision import transforms
# from torch.optim.lr_scheduler import ReduceLROnPlateau
import h5py
from FaceDatasets import SiameseNetworkDatasetDifficult, SoftMaxDatabase
from FaceNetworks import SoftMaxNetwork, SiameseNetwork, GME_SiameseNetwork, GME_SoftmaxNetwork, VAE
from MetaEmbeddings import GaussianMetaEmbedding
from Config import ConfigNetwork, ConfigFaceDatasets
from utils_test import test_model
import sidekit
from sidekit.factor_analyser import fa_model_loop
import copy

__author__ = "Andreas Nautsch, Themos Stafylakis"
__maintainer__ = "Andreas Nautsch"
__email__ = "andreas.nautsch@h-da.de"
__status__ = "Development"
__docformat__ = 'reStructuredText'
__credits__ = ["Niko Brümmer, Adrian Bulat"]

def net_distance(data, net, epoch=None):
    sample0, sample1, label = data
    sample0, sample1, label = Variable(sample0).cuda(), Variable(sample1).cuda(), Variable(label).cuda()
    if ConfigNetwork.train_with_softmax:
        # note: still face dependend
        if ConfigNetwork.train_with_meta_embeddings:
            output1_a, output1_B = net(sample0, None)
            output2_a, output2_B = net(sample1, None)
            return GaussianMetaEmbedding.net_distance(data=(output1_a, output1_B, output2_a, output2_B, label), net=net, epoch=epoch)
        else:
            output1 = net(sample0, None)
            output2 = net(sample1, None)
            return SoftMaxNetwork.distance_pairwise_euclidean(output1, output2), label
    elif ConfigNetwork.train_vae:
            output1_a, output1_B, output2_a, output2_B = net(sample0,sample1)
            return VAE.net_distance(data=(output1_a, output1_B, output2_a, output2_B, label), net=net, epoch=epoch)
    else:
        if ConfigNetwork.train_with_meta_embeddings:
            output1_a, output1_B, output2_a, output2_B = net(sample0,sample1)
            return GaussianMetaEmbedding.net_distance(data=(output1_a, output1_B, output2_a, output2_B, label), net=net, epoch=epoch)
        else:
            # note: still face dependend
            output1, output2 = net(sample0, sample1)
            return SiameseNetwork.net_distance(data=(output1, output2, label), net=net, epoch=epoch)

def train_epoch(train_dataloader, net, optimizer, epoch, iteration_number):
    net.train()
    if ConfigNetwork.train_with_softmax:
        if ConfigNetwork.train_with_meta_embeddings:
            GME_SoftmaxNetwork.train_epoch(train_dataloader, net, optimizer, epoch, iteration_number)
        else:
            # same for ConfigNetwork.train_with_meta_embeddings:
            SoftMaxNetwork.train_epoch(train_dataloader, net, optimizer, epoch, iteration_number)
    elif ConfigNetwork.train_vae:
        VAE.train_epoch(train_dataloader, net, optimizer, epoch, iteration_number)
    else:
        if ConfigNetwork.train_with_meta_embeddings:
            # GME_SiameseNetwork.train_epoch(train_dataloader, net, optimizer, epoch, iteration_number)
            GaussianMetaEmbedding.train_epoch(train_dataloader, net, optimizer, epoch, iteration_number)
        else:
            SiameseNetwork.train_epoch(train_dataloader, net, optimizer, epoch, iteration_number)

def select_difficult_pairs(net, train_dataloader):
    net.eval()
    if not os.path.exists(ConfigNetwork.embeddings_file):
        dataset = SoftMaxDatabase(imageFolderDataset=ConfigFaceDatasets.dataset_class(root=ConfigFaceDatasets.training_dir),
                                  transform=train_dataloader.dataset.transform,
                                  should_invert=False)
        embeddings_loader = DataLoader(dataset,
                                       shuffle=False,
                                       num_workers=ConfigNetwork.num_workers,
                                       batch_size=ConfigNetwork.batch_size_train)
        with h5py.File(ConfigNetwork.embeddings_file, "a") as embd_file:
            for i, data in enumerate(embeddings_loader, 0):
                img0, label = data
                img0, label = Variable(img0).cuda(), Variable(label).cuda()
                output0 = net.forward_once(img0)
                embd_file.create_dataset("{}".format(i),
                                         data=numpy.column_stack((output0.data.cpu().numpy(), label.data.cpu().numpy())),
                                         compression="gzip",
                                         fletcher32=True)
        logging.critical('extracted embeddings for difficult pairs')

    if not os.path.exists(ConfigNetwork.select_difficult_pairs_idx + '_genuine.npy') or not os.path.exists(ConfigNetwork.select_difficult_pairs_idx + '_impostor.npy'):
        data = []
        with h5py.File(ConfigNetwork.embeddings_file, "r") as h5f:
            for key, value in h5f.items():
                data.append(value.value)
        data = numpy.concatenate(data)
        embeddings = data[:,:ConfigNetwork.embedding_size]
        embeddings = (embeddings.T / numpy.linalg.norm(embeddings, axis=1)).T # prepare cosine distance
        embedding_labels = data[:, ConfigNetwork.embedding_size:]
        subjects = numpy.unique(embedding_labels.flatten())
        # concat & score embeddings
        lbls = embedding_labels + 1 # all labels are IDs 0 to inf, however a "0" label destroy the later lbls@lbls-lbls**2 idea as any other label times zero is zero...
        # figure out labels, unselect same-sample comparison, i.e. diagonal
        split_num = int(1000)
        # assure #embeddings / split_num < ConfigNetwork.select_difficult_pairs_top_N /
        """
        if data.shape[0] / split_num > ConfigNetwork.select_difficult_pairs_top_N / 4:
            split_num = int(data.shape[0] / ConfigNetwork.select_difficult_pairs_top_N * 4)
        indice_tuples_genuine = numpy.zeros((ConfigNetwork.select_difficult_pairs_top_N, 3)) # idx1, idx2, score
        indice_tuples_impostor = numpy.zeros((ConfigNetwork.select_difficult_pairs_top_N, 3)) # idx1, idx2, score
        """
        # idx1, idx2, score, label1, label2
        indice_tuples_genuine = numpy.zeros((subjects.shape[0] * ConfigNetwork.select_difficult_pairs_topN_per_subject, 5))
        indice_tuples_impostor = numpy.zeros((subjects.shape[0] * ConfigNetwork.select_difficult_pairs_topN_per_subject, 5))
        indice_tuples_genuine[:,2] = -numpy.infty
        indice_tuples_impostor[:,2] = numpy.infty
        indice_tuples_genuine[:,3] = numpy.repeat(subjects, ConfigNetwork.select_difficult_pairs_topN_per_subject)
        indice_tuples_genuine[:,4] = indice_tuples_genuine[:,3]
        indice_tuples_impostor[:,3] = numpy.repeat(subjects, ConfigNetwork.select_difficult_pairs_topN_per_subject)
        indice_tuples_impostor[:,4] = -1
        splits = numpy.array_split(range(embeddings.shape[0]), split_num)
        split_lens = numpy.array([len(s) for s in splits])
        for ii, sid in enumerate(splits):
            if ii % 100 == 0:
                logging.debug('emb idx: {}'.format(ii))
            embedding_scores = - embeddings[sid,:] @ embeddings.T # negative cosine similarity
            offset = split_lens[:ii].sum()
            num_rows = embedding_scores.shape[0]
            num_cols = embedding_scores.shape[1]

            lbls_scores = numpy.abs((lbls[sid] @ lbls.T - lbls.flatten() ** 2))  # leaves zeros on same class, also: diag
            # also, remove duplicates due to symmetric comparison
            center_of_storm = numpy.eye(num_rows, dtype=bool)
            center_of_storm[numpy.tril_indices(num_rows)] = True
            eye_idx = numpy.hstack((
                numpy.ones((num_rows,offset), dtype=bool),
                center_of_storm,
                numpy.zeros((num_rows,num_cols - num_rows - offset), dtype=bool)
            ))
            embedding_scores[eye_idx] = -100  # exclude same image comparisons
            lbls_scores[eye_idx] = -100  # same sample = -100, tar = 0, non > 0

            # for each reference, find difficult pairs
            batch_subjects = numpy.unique(embedding_labels.flatten()[offset:offset+num_rows])
            for sidx, subj in enumerate(batch_subjects):
                subj_idx_in_list = subjects == subj

                batch_row_idx = (embedding_labels[offset:offset+num_rows] == subj).flatten()
                batch_row_pos = numpy.argwhere(batch_row_idx)

                subj_scores = embedding_scores[batch_row_idx,:]
                tar_idx = lbls_scores[batch_row_idx,:] == 0
                non_idx = lbls_scores[batch_row_idx,:] > 0
                tar_pos = numpy.argwhere(tar_idx)
                non_pos = numpy.argwhere(non_idx)

                tar = subj_scores[tar_idx]
                non = subj_scores[non_idx]

                tar_argsort = tar.argsort()[::-1]
                non_argsort = non.argsort()

                if len(tar_argsort) > 0:
                    subj_tar_in_list = indice_tuples_genuine[:,3] == subj
                    subj_tar_idx = numpy.argwhere(subj_tar_in_list).flatten()
                    subj_topN_tar_argsort = indice_tuples_genuine[subj_tar_in_list,3].argsort()[::-1]
                    tar_cnt = 0
                    for nidx in subj_topN_tar_argsort:
                        if tar_cnt >= tar_argsort.shape[0]:
                            break
                        if indice_tuples_genuine[subj_tar_idx[nidx],2] < tar[tar_argsort[tar_cnt]]:
                            idx_subj_scr = tar_pos[tar_argsort[tar_cnt]]
                            col = idx_subj_scr[1]
                            row = batch_row_pos[idx_subj_scr[0]]
                            indice_tuples_genuine[subj_tar_idx[nidx], 0:3] = numpy.array([offset + row, col, embedding_scores[row, col]]).T
                            tar_cnt += 1

                if len(non_argsort) > 0:
                    subj_non_in_list = indice_tuples_impostor[:,3] == subj
                    subj_non_idx = numpy.argwhere(subj_non_in_list).flatten()
                    subj_topN_non_argsort = indice_tuples_impostor[subj_non_in_list,3].argsort()
                    non_cnt = 0
                    for nidx in subj_topN_non_argsort:
                        if non_cnt >= non_argsort.shape[0]:
                            break
                        if indice_tuples_impostor[subj_non_idx[nidx],2] > non[non_argsort[non_cnt]]:
                            idx_subj_scr = non_pos[non_argsort[non_cnt]]
                            col = idx_subj_scr[1]
                            row = batch_row_pos[idx_subj_scr[0]]
                            indice_tuples_impostor[subj_non_idx[nidx], 0:3] = numpy.array([offset + row, col, embedding_scores[row, col]]).T
                            non_cnt += 1
        numpy.save(ConfigNetwork.select_difficult_pairs_idx + '_genuine_all.npy', indice_tuples_genuine)
        numpy.save(ConfigNetwork.select_difficult_pairs_idx + '_impostor_all.npy', indice_tuples_impostor)
        indice_tuples_genuine = indice_tuples_genuine[numpy.argwhere(
            ~numpy.isinf(indice_tuples_genuine[:, 2])).flatten(), :]
        indice_tuples_impostor = indice_tuples_impostor[numpy.argwhere(
            ~numpy.isinf(indice_tuples_impostor[:, 2])).flatten(), :]
        numpy.save(ConfigNetwork.select_difficult_pairs_idx + '_genuine.npy', indice_tuples_genuine)
        numpy.save(ConfigNetwork.select_difficult_pairs_idx + '_impostor.npy', indice_tuples_impostor)
        logging.critical('saved difficult pairs')

    indice_tuples_genuine = numpy.load(ConfigNetwork.select_difficult_pairs_idx + '_genuine.npy').astype(int)
    indice_tuples_impostor = numpy.load(ConfigNetwork.select_difficult_pairs_idx + '_impostor.npy').astype(int)

    # new dataloader
    diff_dataset = SiameseNetworkDatasetDifficult(
        imageFolderDataset=ConfigFaceDatasets.dataset_class(root=ConfigFaceDatasets.training_dir),
        indice_tuples_genuine=indice_tuples_genuine,
        indice_tuples_impostor=indice_tuples_impostor,
        transform=transforms.Compose([transforms.Scale((100, 100)),
                                      transforms.ToTensor()
                                      ]),
        should_invert=False)
    train_dataloader = DataLoader(diff_dataset,
                                  shuffle=False,
                                  num_workers=ConfigNetwork.num_workers,
                                  batch_size=ConfigNetwork.batch_size_train)
    logging.critical('swapped train data loader to difficult pairs')
    return train_dataloader

def train_net(net, train_dataloader, test_dataloader):
    if not os.path.exists(ConfigNetwork.modelname):
        last_model_loaded = False # True # False
        # inits
        iteration_number = 0
        for epoch in range(0,ConfigNetwork.train_number_epochs):
            """
            if ConfigNetwork.learning_rate_scheduler:
                optimizer = optim.Adam(net.parameters(),
                                       lr = ConfigNetwork.learning_rate)
                scheduler = ReduceLROnPlateau(optimizer, 'min')
            else:
            """
            epoch_learning_rate_exponent = max(0, epoch - (ConfigNetwork.learning_rate_defactor_after_epoch - 1))
            lr = ConfigNetwork.learning_rate * ConfigNetwork.learning_rate_defactor**epoch_learning_rate_exponent
            parameters = filter(lambda p: p.requires_grad, net.parameters())
            if ConfigNetwork.train_vae:
                optimizer = pyro_optim.Adam({'lr': lr})
            else:
                optimizer = optim.Adam(parameters, lr = lr)
            base_file_pattern = os.path.join(ConfigNetwork.storage_dir, '{}_epoch_{}'.format(ConfigNetwork.modelname, epoch))
            epoch_net_file = '{}_model'.format(base_file_pattern)
            if epoch < ConfigNetwork.freeze_ResNet_epochs:
                net.set_ResNet_requires_grad(requires_grad=False)
            else:
                net.set_ResNet_requires_grad(requires_grad=True)
            if not os.path.exists(epoch_net_file) and epoch == 0:
                # init meta embeddings network
                if ConfigNetwork.train_with_meta_embeddings:
                    logging.debug('init B with plda expectation')
                    if not os.path.exists(ConfigNetwork.embeddings_file):
                        dataset = SoftMaxDatabase(
                            imageFolderDataset=ConfigFaceDatasets.dataset_class(root=ConfigFaceDatasets.training_dir),
                            transform=train_dataloader.dataset.transform,
                            should_invert=False)
                        embeddings_loader = DataLoader(dataset,
                                                       shuffle=False,
                                                       num_workers=ConfigNetwork.num_workers,
                                                       batch_size=ConfigNetwork.batch_size_train)
                        softmax_net = net.to_softmaxNetwork()
                        softmax_net.normalize = False
                        with h5py.File(ConfigNetwork.embeddings_file, "a") as embd_file:
                            for i, data in enumerate(embeddings_loader, 0):
                                img0, label = data
                                img0, label = Variable(img0).cuda(), Variable(label).cuda()
                                output0 = super(SoftMaxNetwork, softmax_net).forward_once(img0)
                                embd_file.create_dataset("{}".format(i),
                                                         data=numpy.column_stack(
                                                             (output0.data.cpu().numpy(), label.data.cpu().numpy())),
                                                         compression="gzip",
                                                         fletcher32=True)
                            logging.critical('extracted embeddings')
                    if not os.path.exists(ConfigNetwork.embeddings_file_plda) or not os.path.exists(ConfigNetwork.embeddings_mean_file):
                        data = []
                        with h5py.File(ConfigNetwork.embeddings_file, "r") as h5f:
                            for key, value in h5f.items():
                                data.append(value.value)
                        data = numpy.concatenate(data)
                        embeddings = data[:,:ConfigNetwork.embedding_size]
                        embeddings_mean = embeddings.mean(0)
                        numpy.save(ConfigNetwork.embeddings_mean_file, embeddings_mean)
                        logging.debug('embeddings mean: {}'.format(embeddings.mean(0)))
                        embeddings -= embeddings.mean(0)
                        embeddings = (embeddings.T / numpy.linalg.norm(embeddings, axis=1)).T # prepare cosine distance
                        embedding_labels = data[:, ConfigNetwork.embedding_size:].squeeze()

                        s = sidekit.StatServer()
                        s.modelset = embedding_labels
                        s.segset = numpy.arange(embedding_labels.shape[0]).astype(str)
                        s.stat0 = numpy.ones((embedding_labels.shape[0], 1))
                        s.stat1 = copy.deepcopy(embeddings)
                        s.start = numpy.empty(embedding_labels.shape[0], dtype='|O')
                        s.stop = numpy.empty(embedding_labels.shape[0], dtype='|O')
                        s.validate()
                        ids = numpy.unique(s.modelset)
                        class_nb = ids.shape[0]

                        f = sidekit.FactorAnalyser()
                        rank_f = ConfigNetwork.embedding_size
                        f.plda(s, rank_f=rank_f)
                        f.write(ConfigNetwork.embeddings_file_plda)
                    else:
                        f = sidekit.FactorAnalyser(ConfigNetwork.embeddings_file_plda)

                    e_mu = torch.from_numpy(f.mean).type(torch.FloatTensor)
                    e_B = torch.from_numpy(numpy.linalg.inv(f.Sigma).diagonal()).type(torch.FloatTensor)
                    # e_B = torch.from_numpy(numpy.linalg.inv(f.Sigma)).type(torch.FloatTensor)
                    assert(isinstance(net, GME_SoftmaxNetwork))
                    net = GME_SoftmaxNetwork(num_train_classes=net.num_train_classes,pretrained_siamese_net=net.pretrained_net,expected_mu=e_mu,expected_B=e_B).cuda()
                    logging.debug('init B with plda done')

            if not os.path.exists(epoch_net_file):
                if last_model_loaded:
                    logging.critical('run validation on epoch {}'.format(epoch-1))
                    test_model(database_dir=test_dataloader,
                               net=net,
                               net_distance=net_distance,
                               epoch=None
                               )
                    last_model_loaded = False

                if ConfigNetwork.select_difficult_pairs_epoch is not None:
                    if epoch == ConfigNetwork.select_difficult_pairs_epoch:
                        train_dataloader = select_difficult_pairs(net, train_dataloader)

                # train an epoch
                net.train()
                train_epoch(train_dataloader=train_dataloader, net=net, optimizer=optimizer, epoch=epoch, iteration_number=iteration_number)
                torch.save(obj=net.state_dict(), f=epoch_net_file)
            else:
                net.load_state_dict(torch.load(epoch_net_file))
                logging.info('loaded model for epoch: {}'.format(epoch))
                last_model_loaded = True
                continue

            logging.critical('run validation on epoch {}'.format(epoch))
            test_model(database_dir=test_dataloader,
                       net=net,
                       net_distance=net_distance,
                       epoch=None
                       )
        torch.save(obj=net.state_dict(), f='{}'.format(ConfigNetwork.modelname))
        logging.info('training completed, model stored.')


from django.urls import path

from . import views

app_name = 'polls'
urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('<int:pk>/', views.DetailView.as_view(), name='detail'),
    # detailViewの場合pkの数字でレコードの特定がされる。だからこれがデフォルト。intは正規表現でintじゃないと受け付けない。https://qiita.com/dai-takahashi/items/7d0187485cad4418c073
    path('<int:pk>/results/', views.ResultsView.as_view(), name='results'), # 整数がマッチした場合、viewsのintegerの引数にvalue(pk)を代入
    path('<int:question_id>/vote/', views.vote, name='vote'),
]


import random, math

operators = ['+', '-', 'x', '/']

def build_calc_statement(max) -> str:
    operator = operators[random.randint(0, len(operators) - 1)]
    parameter_1 = random.randint(1, max)
    parameter_2 = random.randint(1, math.ceil(math.sqrt(max)))
    return f"calc {operator} {parameter_1} {parameter_2}"

def build_goto_statement(max) -> str:
    line = random.randint(1, max)
    return f"goto {line}"

def build_remove_statement(max) -> str:
    line = random.randint(1, max)
    return f"remove {line}"

def build_replace_statement(max) -> str:
    line_1 = random.randint(1, max)
    line_2 = random.randint(1, max)
    return f"replace {line_1} {line_2}"

def build_goto_calc_statement(line_total) -> str:
    while True:
        calc_statement = build_calc_statement(line_total)
        value = process_calc_statement(calc_statement)
        if value < line_total:
            break
    return f"goto {calc_statement}"

def process_calc_statement(statement) -> int:
    [_, operator, param_string_1, param_string_2] = statement.split()

    param_1 = int(param_string_1)
    param_2 = int(param_string_2)

    if operator == '+':
        return param_1 + param_2
    elif operator == '-':
        return param_1 - param_2
    elif operator == 'x':
        return param_1 * param_2
    elif operator == '/':
        return param_1 / param_2
    else:
        raise Exception("Unexpected Operator!")

def main():
    with open("step_4.txt", mode='w') as f:
        line_total = 10000

        for i in range(0, line_total):
            statement_dice_roll = random.randint(0, 3)
            if statement_dice_roll == 0:
                statement = build_goto_calc_statement(line_total)
            elif statement_dice_roll == 1:
                statement = build_goto_statement(line_total)
            elif statement_dice_roll == 2:
                statement = build_remove_statement(line_total)
            elif statement_dice_roll == 3:
                statement = build_replace_statement(line_total)
            statement += "\n"
            f.write(statement)

if __name__ == "__main__":
    main()


class Solution:
    # def combinationSum(self, candidates, target):
    #     """
    #     :type candidates: List[int]
    #     :type target: int
    #     :rtype: List[List[int]]
    #     """
    #     ret = []
    #     self.recursive(candidates, target, 0, [], ret)
    #     return ret
    #
    # def recursive(self, candidates, target, i, pre, ret):
    #     if i == len(candidates):
    #         return
    #
    #     st = []
    #     while target >= candidates[i]:
    #         target -= candidates[i]
    #         st.append(candidates[i])
    #
    #     if target == 0:
    #         ret.append(pre + st)
    #     else:
    #         self.recursive(candidates, target, i + 1, pre + st, ret)
    #     # print(st)
    #     while st:
    #         target += st.pop()
    #         self.recursive(candidates, target, i + 1, pre + st, ret)

    def combinationSum(self, candidates, target):
        """
        :type candidates: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        result = []
        candidates.sort()
        self.dfs(candidates, target, 0, [], result)
        return result

    def dfs(self, candidates, target, index, result, final):
        for i in range(index, len(candidates)):
            if target == candidates[i]:
                final.append(result + [candidates[i]])
                # print('condition1: ', result)
                return
            elif target > candidates[i]:
                self.dfs(candidates, target-candidates[i], i, result + [candidates[i]], final)

if __name__ == '__main__':
    solution = Solution()
    candidates = [2, 3, 5]
    target = 8
    print(solution.combinationSum(candidates, target))


nume1 = int(input('Informe o primeiro número: '))
nume2 = int(input('Informe o segundo número: '))
nume3 = int(input('Informe o terceiro número: '))
# Verificar o maior
maior = nume1
if nume2 > nume1 and nume2 > nume3:
    maior = nume2
if nume3 > nume1 and nume3 > nume2:
    maior = nume3
# Verificar o menor
menor = nume1
if nume2 < nume1 and nume2 < nume3:
    menor = nume2
if nume3 < nume1 and nume3 < nume2:
    menor = nume3

print('O maior número informado é: {}'.format(maior))
print('O menor número informado é: {}'.format(menor))


from tabulate import tabulate

from rrg.models import Contract
from rrg.models import Invoice
from rrg.models import Iitem

def remaining_payroll(session, employee):
    """
    gather sherees remaining payroll with invoice and invoice items lists to
    use to exclude from deletion
    """

    scontract = \
        session.query(Contract).filter(
            Contract.employee == employee)
    sherees_paychecks_due = session.query(Invoice).filter(
        Invoice.contract == scontract, Invoice.voided == 0,
        Invoice.prcleared == 0, Invoice.posted == 1)
    do_not_delete_items = []
    total_due = 0
    for pc in sherees_paychecks_due:
        iitems = session.query(Iitem).filter(Iitem.invoice == pc)
        pay = 0
        for i in iitems:
            do_not_delete_items.append(i)
            pay += i.quantity * i.cost
        total_due += pay
    return sherees_paychecks_due, do_not_delete_items, total_due

def employee_payroll_due_report(session, employee, format):
    """
    """
    sherees_paychecks_due, iitems, total = remaining_payroll(session, employee)
    res = dict(id=[], date=[], description=[], amount=[])
    res['id'] = [i.id for i in sherees_paychecks_due]
    res['date'] = [i.date for i in sherees_paychecks_due]
    res['description'] = [i.period_start for i in sherees_paychecks_due]
    for pc in sherees_paychecks_due:
        pay = 0
        for i in pc.invoice_items:
            pay += i.quantity * i.cost
        res['amount'].append(pay)
    res['id'].append('')
    res['date'].append('')
    res['description'].append('Total Due')
    res['amount'].append(total)
    if format == 'plain':
        return tabulate(res, headers='keys', tablefmt='plain')
    elif format == 'latex':
        report = ''
        report += '\n\section{Hourly}\n'
        report += tabulate(res, headers='keys', tablefmt='latex')
        return report