from notes import containNotes, noteToNumber, notesToChord, chordList, chordToNote, chordListAlt, notesToScale, scaleList, scaleListAlt, scaleToNote
from transitions.extensions import GraphMachine
from utils import send_text_message, send_menu_carousel, send_chord, send_not_found, send_chord_note, send_scale, send_scale_note, send_fsm, send_demo

class TocMachine():
    def __init__(self):
        self.notes = []
        self.chord = -1
        self.scale = -1
        self.machine = GraphMachine(
            model=self,
            **{
                "states":["start", "menu", "chord", "chordResult", "chordNote", "chordNoteRootnote", "chordNoteType"
                , "scale", "scaleResult", "scaleNote", "scaleNoteRootnote", "scaleNoteType", "fsm", "demo"],
                "transitions":[
                    {
                        "trigger": "advance",
                        "source": "*",
                        "dest": "fsm",
                        "conditions": "is_going_to_fsm",
                    },
                    {
                        "trigger": "advance",
                        "source": "*",
                        "dest": "menu",
                        "conditions": "is_going_to_menu",
                    },
                    {
                        "trigger": "advance",
                        "source": "menu",
                        "dest": "chord",
                        "conditions": "is_going_to_chord",
                    },
                    {
                        "trigger": "advance",
                        "source": "menu",
                        "dest": "chordNote",
                        "conditions": "is_going_to_chordNote",
                    },
                    {
                        "trigger": "advance",
                        "source": "chord",
                        "dest": "chordResult",
                        "conditions": "is_going_to_chordResult",
                    },
                    {
                        "trigger": "advance",
                        "source": "chordResult",
                        "dest": "chordResult",
                        "conditions": "is_going_to_chordResult",
                    },
                    {
                        "trigger": "advance",
                        "source": "chordNote",
                        "dest": "chordNoteRootnote",
                        "conditions": "is_going_to_chordNoteRootnote",
                    },
                    {
                        "trigger": "advance",
                        "source": "chordNoteRootnote",
                        "dest": "chordNoteType",
                        "conditions": "is_going_to_chordNoteType",
                    },
                    {
                        "trigger": "advance",
                        "source": "chordNoteType",
                        "dest": "chordNote",
                        "conditions": "is_going_back_to_chordNote",
                    },
                    {
                        "trigger": "advance",
                        "source": "chordNoteType",
                        "dest": "chordNoteType",
                        "conditions": "is_going_to_chordNoteType",
                    },
                    {
                        "trigger": "advance",
                        "source": "chordNoteType",
                        "dest": "chordNoteRootnote",
                        "conditions": "is_going_to_chordNoteRootnote",
                    },
                    {
                        "trigger": "advance",
                        "source": "menu",
                        "dest": "scale",
                        "conditions": "is_going_to_scale",
                    },
                    {
                        "trigger": "advance",
                        "source": "menu",
                        "dest": "scaleNote",
                        "conditions": "is_going_to_scaleNote",
                    },
                    {
                        "trigger": "advance",
                        "source": "scale",
                        "dest": "scaleResult",
                        "conditions": "is_going_to_scaleResult",
                    },
                    {
                        "trigger": "advance",
                        "source": "scaleResult",
                        "dest": "scaleResult",
                        "conditions": "is_going_to_scaleResult",
                    },
                    {
                        "trigger": "advance",
                        "source": "scaleNote",
                        "dest": "scaleNoteRootnote",
                        "conditions": "is_going_to_scaleNoteRootnote",
                    },
                    {
                        "trigger": "advance",
                        "source": "scaleNoteRootnote",
                        "dest": "scaleNoteType",
                        "conditions": "is_going_to_scaleNoteType",
                    },
                    {
                        "trigger": "advance",
                        "source": "scaleNoteType",
                        "dest": "scaleNote",
                        "conditions": "is_going_back_to_scaleNote",
                    },
                    {
                        "trigger": "advance",
                        "source": "scaleNoteType",
                        "dest": "scaleNoteType",
                        "conditions": "is_going_to_scaleNoteType",
                    },
                    {
                        "trigger": "advance",
                        "source": "scaleNoteType",
                        "dest": "scaleNoteRootnote",
                        "conditions": "is_going_to_scaleNoteRootnote",
                    },
                    {
                        "trigger": "advance",
                        "source": "chordNoteType",
                        "dest": "chordNote",
                        "conditions": "is_going_to_change_rootNote",
                    },
                    {
                        "trigger": "advance",
                        "source": "chordNoteType",
                        "dest": "chordNoteRootnote",
                        "conditions": "is_going_to_change_type",
                    },
                    {
                        "trigger": "advance",
                        "source": "scaleNoteType",
                        "dest": "scaleNote",
                        "conditions": "is_going_to_change_rootNote",
                    },
                    {
                        "trigger": "advance",
                        "source": "scaleNoteType",
                        "dest": "scaleNoteRootnote",
                        "conditions": "is_going_to_change_type",
                    },
                    {
                        "trigger": "advance",
                        "source": "fsm",
                        "dest": "menu",
                        "conditions": "is_going_to_menu",
                    },
                    {
                        "trigger": "advance",
                        "source": "menu",
                        "dest": "demo",
                        "conditions": "is_going_to_demo",
                    },
                    {
                        "trigger": "advance",
                        "source": "demo",
                        "dest": "menu",
                        "conditions": "is_going_to_menu",
                    },
                ],
                "initial":"start",
                "auto_transitions":False,
                "show_conditions":True,
            },
        )

    #condition
    def is_going_to_menu(self, event):
        text = event.message.text
        return "選單" in text

    def is_going_to_chord(self, event):
        text = event.message.text
        return "和弦" in text and "組成音" not in text

    def is_going_to_chordNote(self, event):
        text = event.message.text
        return "和弦" in text and "組成音" in text

    def is_going_to_chordResult(self, event):
        print("!" + str(self.notes))
        text = event.message.text
        if(len(containNotes(text)) != 0): self.notes = containNotes(text)
        return len(containNotes(text)) != 0

    def is_going_to_chordNoteRootnote(self, event):
        text = event.message.text
        if(len(containNotes(text)) != 0): self.notes = containNotes(text)
        return len(containNotes(text)) != 0

    def is_going_to_chordNoteType(self, event):
        self.chord = -1
        text = event.message.text
        text = text.strip()
        for i in range(len(chordList)):
            for j in range(len(chordList[i])):
                if text == chordList[i][j]:
                    self.chord = i
                    break
            else:
                continue
            break
        if self.chord == -1:
            for i in range(len(chordListAlt)):
                for j in range(len(chordListAlt[i])):
                    if str(text).lower() == chordListAlt[i][j]:
                        self.chord = i
                        break
                else:
                    continue
                break
        return self.chord != -1

    def is_going_back_to_chordNote(self, event):
        text = event.message.text
        return "重來" in text or "再來" in text or "重查" in text

    def is_going_to_scale(self, event):
        text = event.message.text
        return "音階" in text and "組成音" not in text

    def is_going_to_scaleNote(self, event):
        text = event.message.text
        return "音階" in text and "組成音" in text

    def is_going_to_scaleResult(self, event):
        text = event.message.text
        if(len(containNotes(text)) != 0): self.notes = containNotes(text)
        return len(containNotes(text)) != 0

    def is_going_to_scaleNoteRootnote(self, event):
        text = event.message.text
        if(len(containNotes(text)) != 0): self.notes = containNotes(text)
        return len(containNotes(text)) != 0

    def is_going_to_scaleNoteType(self, event):
        self.scale = -1
        text = event.message.text
        text = text.strip()
        for i in range(len(scaleList)):
            for j in range(len(scaleList[i])):
                if text == scaleList[i][j]:
                    self.scale = i
                    break
            else:
                continue
            break
        if self.scale == -1:
            for i in range(len(scaleListAlt)):
                for j in range(len(scaleListAlt[i])):
                    if str(text).lower() == scaleListAlt[i][j]:
                        self.scale = i
                        break
                else:
                    continue
                break
        return self.scale != -1

    def is_going_back_to_scaleNote(self, event):
        text = event.message.text
        return "重來" in text or "再來" in text or "重查" in text

    def is_going_to_change_rootNote(self, event):
        text = event.message.text
        return "更改" in text and "根音" in text

    def is_going_to_change_type(self, event):
        text = event.message.text
        return "更改" in text and "種類" in text

    def is_going_to_fsm(self, event):
        text = event.message.text
        return "fsm" in str(text).lower()

    def is_going_to_demo(self, event):
        text = event.message.text
        return "demo" in str(text).lower()

    #on enter
    def on_enter_menu(self, event):
        print("I'm entering menu")
        reply_token = event.reply_token
        send_menu_carousel(reply_token)

    def on_enter_chord(self, event):
        print("I'm entering chord")
        reply_token = event.reply_token
        text = "請輸入數個音符（A~G，可搭配升降記號）。我會想辦法告訴你他們可以組成的和弦。"
        send_text_message(reply_token, text)

    def on_enter_chordNote(self, event):
        print("I'm entering chordNote")
        reply_token = event.reply_token
        text = "請先輸入和弦的「根音」，或點選下方常用音符。"
        send_text_message(reply_token, text, "note")

    def on_enter_chordResult(self, event):
        print("I'm entering chordResult")
        reply_token = event.reply_token
        print(self.notes)
        root_note, whichChord = notesToChord(self.notes)
        if whichChord > -1:
            send_chord(reply_token, root_note, whichChord)
        else:
            send_not_found(reply_token)

    def on_enter_chordNoteRootnote(self, event):
        print("I'm entering chordNoteRootnote")
        reply_token = event.reply_token
        text = "請輸入和弦「種類」，或點選下方常用和弦。"
        send_text_message(reply_token, text, "chord")

    def on_enter_chordNoteType(self, event):
        print("I'm entering chordNoteType")
        reply_token = event.reply_token
        print(self.notes)
        text = chordToNote(self.notes, self.chord)
        send_chord_note(reply_token, self.notes[0], self.chord, text)

    def on_enter_scale(self, event):
        print("I'm entering scale")
        reply_token = event.reply_token
        text = "請輸入數個音符（A~G，可搭配升降記號）。我會想辦法告訴你他們可以組成的音階。"
        send_text_message(reply_token, text)

    def on_enter_scaleNote(self, event):
        print("I'm entering scaleNote")
        reply_token = event.reply_token
        text = "請先輸入音階的「根音」，或點選下方常用音符。"
        send_text_message(reply_token, text, "note")

    def on_enter_scaleResult(self, event):
        print("I'm entering scaleResult")
        reply_token = event.reply_token
        print(self.notes)
        root_note, whichScale = notesToScale(self.notes)
        if whichScale > -1:
            send_scale(reply_token, root_note, whichScale)
        else:
            send_not_found(reply_token)

    def on_enter_scaleNoteRootnote(self, event):
        print("I'm entering scaleNoteRootnote")
        reply_token = event.reply_token
        text = "請輸入音階「種類」，或點選下方常用音階。"
        send_text_message(reply_token, text, "scale")

    def on_enter_scaleNoteType(self, event):
        print("I'm entering chordNoteType")
        reply_token = event.reply_token
        print(self.notes)
        text = scaleToNote(self.notes, self.scale)
        send_scale_note(reply_token, self.notes[0], self.scale, text)

    def on_enter_fsm(self, event):
        print("I'm entering fsm")
        reply_token = event.reply_token
        send_fsm(reply_token)

    def on_enter_demo(self, event):
        print("I'm entering demo")
        reply_token = event.reply_token
        send_demo(reply_token)


def aver(array, n):
    y = 0
    for h in range(1, n+1):
        y = y+int(array[h])
    return y/n

f1 = open('score.txt', 'r')
list = {}
list_stu = {}
i = 0
k = 1
chinese = {}
math = {}
english = {}
number = {}
for line in f1:
    line = line.strip()
    list[i] = line
    i = i+1
peo_num = len(list)-1
for j in range(1, len(list)):
    list_stu[j] = list[j].split(",")
    number[k] = list_stu[j][0]
    chinese[k] = list_stu[j][1]
    math[k] = list_stu[j][2]
    english[k] = list_stu[j][3]
    k = k+1
print("chinese_average = %.2f" % aver(chinese, 5))
print("math_average = %.2f" % aver(math, 5))
print("english_average = %.2f" % aver(english, 5))

10215501435 week07 homework 1018/Dase_Intro_homework_week07_02.py


import os

TMP_DIR = "./tmp/"
CRAWLED_DIR = "./rawdata/"
LOG_DIR = "./log/"
REDAME_DIR = "./readme/"
CLEANED_DIR = "./cleaned/"
REPO_CODE_DIR = "../../DTEM_REPOS/"
SELECTED_REPO_PATH = "./export/selected_repos.json"
GHS_PATH = "./rawdata/ghs.csv"


from tf_lassonet.proximal import hier_prox_group
from typing import Optional, List
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Reshape
from tensorflow.keras import Model
import numpy as np
import tensorflow as tf

from dataclasses import dataclass

class LassoNet(tf.keras.Model):
    def __init__(self, model: Model, lambda_: float = 1.0, M:float=1):
        super().__init__()
        self.model = model
        input_n_dim = np.prod(self.model.input.shape[1:])
        output_dim = np.prod(model.layers[-1].output_shape[1:])

        self.skip = Dense(output_dim, use_bias=False)

        self.W = Dense(input_n_dim, use_bias=False)
        self.lambda_ = tf.Variable(lambda_, trainable=False)
        self.M = M

    def regularization(self):
        return tf.math.reduce_sum(tf.norm(self.skip.weights, ord='euclidean', axis=2))

    def input_mask(self):
        return tf.norm(self.skip.weights, ord='euclidean', axis=2) != 0

    def selected_count(self):
        return tf.math.reduce_sum(tf.cast(self.input_mask(), dtype=tf.int32), axis=1)

    def test_step(self, data):
        return super().test_step(data)

    def proximal_update(self):
        skip_star, W_star = hier_prox_group(
            self.skip.weights[0],
            self.W.weights[0],
            lambda_=self.optimizer.learning_rate * self.lambda_,
            lambda_bar=0,
            M=self.M,
        )

        self.skip.weights[0].assign(skip_star)

        self.W.weights[0].assign(W_star)

    def train_step(self, data):
        if len(data) == 3:
            x, y, sample_weight = data
        else:
            sample_weight = None
            x, y = data

        with tf.GradientTape() as tape:
            y_pred = self(x, training=True)
            loss = self.compiled_loss(
                y,
                y_pred,
                sample_weight=sample_weight,
                regularization_losses=self.losses,
            )

        trainable_vars = self.trainable_variables
        gradients = tape.gradient(loss, trainable_vars)

        self.optimizer.apply_gradients(zip(gradients, trainable_vars))

        self.proximal_update()

        self.compiled_metrics.update_state(y, y_pred, sample_weight=sample_weight)
        d = {m.name: m.result() for m in self.metrics}
        d['regularization'] = self.regularization()
        d['number_of_features'] = self.selected_count()
        return d

    def lambda_start(
        self,
        M=1,
        lambda_bar=0,
        factor=2,
    ):
        """Estimate when the model will start to sparsify."""

        def is_sparse(lambda_):

            beta = self.skip.weights[0]
            theta = self.W.weights[0]

            for _ in range(200):
                new_beta, theta = hier_prox_group(
                    beta,
                    theta,
                    lambda_=lambda_,
                    lambda_bar=lambda_bar,
                    M=M,
                )

                if tf.math.reduce_max(tf.math.abs(beta - new_beta)) < 1e-5:
                    break
                beta = new_beta

            v=  tf.math.reduce_sum(tf.cast((tf.norm(beta, ord=1, axis=0) == 0), tf.float32)).numpy()

            return v != 0

        start = 1e-6
        while not is_sparse(factor * start):
            start *= factor
        return start

    def call(self, inputs):
        input_shape = inputs.shape[1:]

        x_flattened = Flatten()(inputs)
        x = self.W(x_flattened)
        x = Reshape(input_shape)(x)
        x = self.model(x)
        x = self.skip(x_flattened) + x
        return x


import logging
from composite_field import CompositeField
from dateutil.relativedelta import relativedelta
from django.db import models
from django.utils.translation import ugettext_lazy as _

logger = logging.getLogger('debits')
"""The logger used by Debits."""

class Period(CompositeField):
    """Period (for example of recurring payment or of a trial subscription).

    It may be expressed in days, weeks, months, or years. Several units cannot be mixed:
    for example, it cannot be 2 months and 13 days."""
    UNIT_DAYS = 1
    UNIT_WEEKS = 2
    UNIT_MONTHS = 3
    UNIT_YEARS = 4

    period_choices = ((UNIT_DAYS, _("days")),  # different processors may support a part of it
                      (UNIT_WEEKS, _("weeks")),
                      (UNIT_MONTHS, _("months")),
                      (UNIT_YEARS, _("years")))
    """For Django :class:`~django.forms.ChoiceField`."""

    unit = models.SmallIntegerField()
    """days, weeks, months, or years."""

    count = models.SmallIntegerField()
    """The number of the units"""

    def __init__(self, unit=None, count=None):
        super().__init__()
        if unit is not None:
            self['unit'].default = unit
        if count is not None:
            self['count'].default = count

# The following functions do not work as a method, because
# CompositeField is replaced with composite_field.base.CompositeField.Proxy:

def period_to_string(period):
    """Human readable description of a period.

    Args:
        period: `Period` field.

    Returns:
        A human readable string."""
    hash = {e[0]: e[1] for e in Period.period_choices}
    return "%d %s" % (period.count, hash[period.unit])

def period_to_delta(period):
    """Convert :class:`Period` to :class:`relativedelta`."""
    return {
        Period.UNIT_DAYS: lambda: relativedelta(days=period.count),
        Period.UNIT_WEEKS: lambda: relativedelta(weeks=period.count),
        Period.UNIT_MONTHS: lambda: relativedelta(months=period.count),
        Period.UNIT_YEARS: lambda: relativedelta(years=period.count),
    }[period.unit]()


from primaires.interpreteur.masque.noeuds.base_noeud import BaseNoeud
from primaires.interpreteur.masque.noeuds.embranchement import Embranchement
from primaires.interpreteur.masque.fonctions import *
from primaires.interpreteur.masque.exceptions.erreur_validation \
        import ErreurValidation

class NoeudCommande(BaseNoeud):

    """Noeud encapsulant une commande.

    """

    def __init__(self, commande):
        """Constructeur du noeud et de la commande"""
        BaseNoeud.__init__(self)
        self.commande = commande
        self.nom = commande.nom_francais
        self.commande.noeud = self
        if self.commande.schema:
            schema = self.commande.schema
            self.construire_arborescence(schema)

    def construire_arborescence(self, schema):
        """Redirection vers la construction de la commande."""
        self.suivant = self.commande.construire_arborescence(schema)

    def __str__(self):
        """Fonction d'affichage"""
        res = str(self.commande)
        if self.suivant:
            res += " : " + str(self.suivant)

        return res

    @property
    def fils(self):
        """Retourne les fils, c'est-à-dire :
        -   le noeud éventuel du schéma de la commande ;
        -   les différents paramètres de la commande.

        """
        fils = Embranchement()
        for noeud_param in self.commande.parametres.values():
            fils.ajouter_fils(noeud_param)
        if self.suivant:
            fils.schema = self.suivant

        return fils

    def get_masque(self, nom_masque):
        """Récupère le masque nom_masque dans l'arborescence."""
        if self.suivant:
            return self.suivant.get_masque(nom_masque)

        for fils in self.fils:
            if fils.nom == nom_masque:
                return fils
            else:
                return fils.get_masque(nom_masque)

    def repartir(self, personnage, masques, commande):
        """Répartition d'un noeud commande.

        La commande est sous la forme d'une liste de caractères.

        """
        valide = self.commande.repartir(personnage, masques, commande)
        if valide:
            valide = self.fils.repartir(personnage, masques, commande)

        return valide

    def valider(self, personnage, dic_masques, tester_fils=True):
        """Méthode de validation du noeud commande"""
        return tester_fils and bool(self.fils) and  self.fils.valider(
                personnage, dic_masques)

    def interpreter(self, personnage, dic_masques):
        """Méthode d'interprétation"""
        self.commande.interpreter(personnage, dic_masques)
        for fils in self.fils:
            fils.interpreter(personnage, dic_masques)

    def afficher(self, personnage=None):
        """Retourne un affichage du masque pour les joueurs."""
        if personnage:
            msg = self.commande.get_nom_pour(personnage)
        else:
            msg = self.commande.nom_francais + "/" + self.commande.nom_anglais

        if self.suivant:
            msg += " " + self.suivant.afficher(personnage)

        return msg

    def extraire_masques(self, masques=None):
        """Extraction des masques de la commande."""
        if masques is None:
            masques = {}

        if self.suivant:
            self.suivant.extraire_masques(masques)

        if self.fils:
            self.fils.extraire_masques(masques)

        return masques


import urllib3
from bs4 import BeautifulSoup
import os
import subprocess
import sys

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
http = urllib3.PoolManager()

global_num_issues = 0

group_name = sys.argv[1]
base_url = 'https://github.com/' + group_name

def get_parsed_page(url_requested):
	page_http = http.request('GET', url_requested)
	page_html = str(page_http.data.decode('utf-8'))
	return BeautifulSoup(page_html, 'html.parser')

def get_repo_security_labels(repo_url):
	parsed_labels_html = get_parsed_page(repo_url + '/labels?&q=security')
	return [element.text.strip() for element in parsed_labels_html.find_all("span", attrs={"class": "label-name"})]

def download_issue(repo_name, issue_id, security_related, gt_file, label=None):
	global group_name
	global global_num_issues

	issue_page = get_parsed_page(base_url + '/' + repo_name + '/issues/' + issue_id)

	issue_title = issue_page.find("h1", attrs={"class": "discussion-title"})
	if issue_title is None:
		issue_title = issue_page.find("span", attrs={"class": "js-issue-title"})
	if label is None:
		print("Downloading {} {}".format(repo_name, issue_id))
	else:
		print("Downloading {} {} <{}>".format(repo_name, issue_id, label))
	issue_title = issue_title.text.strip()

	issue_description_element = issue_page.find("div", attrs={"class": "discussion-starting-comment"})
	if issue_description_element is None:
		issue_description_element = issue_page.find("td", attrs={"class": "d-block comment-body markdown-body js-comment-body"})
	issue_description = issue_description_element.text

	global_issue_identifier = '{}_{}_{}'.format(group_name, repo_name, issue_id)
	with open('organizations/{}/{}/{}.txt'.format(group_name, repo_name, global_issue_identifier), 'w+') as issue_file:
		issue_file.write(issue_title + '\n\n')
		issue_file.write(issue_description)
	gt_file.write("{} {}\n".format(global_issue_identifier, '(1,0)' if security_related else '(0,1)'))

	global_num_issues += 1

def get_num_pages(parsed_html):
	pages = parsed_html.find("div", attrs={"class": "pagination"})
	if pages is None:
		return 1
	n_pages = int(pages.findChildren("a")[-2].text)
	return n_pages

def download_issues(repo_name, security_labels):
	global group_name

	if not os.path.exists('organizations/{}/{}'.format(group_name, repo_name)):
		os.makedirs('organizations/{}/{}'.format(group_name, repo_name))

	gt_file = open('organizations/{}/{}/ground_truth.txt'.format(group_name, repo_name), 'w+')
	# First download all issues with security tags
	num_security_issues = 0
	for label in security_labels:
		if any(ord(c) >= 128 for c in label):
			print("{} includes non-ascii characters, ignoring".format(label))
			continue

		needs_quotes = False
		if ' ' in label:
			needs_quotes = True
		url_label = label.replace(' ', '+').replace(':', '%3A')
		if needs_quotes:
			url_label = "\"" + url_label + "\""
		url_label = '%3A' + url_label
		security_issues_html = get_parsed_page(base_url + '/' + repo_name + '/issues?page=1&q=label' + url_label)
		n_pages = get_num_pages(security_issues_html)
		for page in range(1, n_pages + 1):
			issues_page_url = base_url + '/' + repo_name + '/issues?page={}&q=label{}'.format(page, url_label)
			security_issues_html = get_parsed_page(issues_page_url)
			issue_table = security_issues_html.find("div", attrs={"class": "js-navigation-container js-active-navigation-container"})
			if issue_table is None:
				print("No issues found for label: {}".format(label))
				continue
			issue_rows = issue_table.findChildren(recursive=False)
			for issue in issue_rows:
				issue_id = issue.attrs["id"][6:]
				download_issue(repo_name, issue_id, True, gt_file, label)
				num_security_issues += 1
	print("Found {} security-related issues".format(num_security_issues))
	if num_security_issues == 0:
		subprocess.call(['rm', '-rf', '{}/{}'.format(group_name, repo_name)])
		return

	security_labels = set(security_labels)
	# Then download an equal number of issues issues which do not have security tags
	num_non_security = 0
	parsed_issues_html = get_parsed_page(base_url + '/' + repo_name + '/issues?page=1&q=')
	n_pages = get_num_pages(parsed_issues_html)
	for page in range(1, n_pages + 1):

		issues_page_url = base_url + '/' + repo_name + '/issues?page={}&q='.format(page)
		parsed_issues_html = get_parsed_page(issues_page_url)
		issue_table = parsed_issues_html.find("div", attrs={"class": "js-navigation-container js-active-navigation-container"})
		if issue_table is None:
			issues_page_url = base_url + '/' + repo_name + '/pulls?page={}&q='.format(page)
			parsed_issues_html = get_parsed_page(issues_page_url)
			issue_table = parsed_issues_html.find("div", attrs={"class": "js-navigation-container js-active-navigation-container"})

		issue_rows = issue_table.findChildren(recursive=False)

		for issue in issue_rows:

			issue_labels_element = issue.find("span", attrs={"class": "labels"})
			if issue_labels_element is not None:
				has_security_label = False
				for label in issue_labels_element.findChildren():
					label = label.text.strip()
					if label in security_labels:
						has_security_label = True
						break
				if has_security_label:
					continue

			issue_id = issue.attrs["id"][6:]
			if num_non_security < num_security_issues:
				download_issue(repo_name, issue_id, False, gt_file)
				num_non_security += 1
			else:
				print("Finished downloading {} security issues and {} non-security issues for repo: {}".format(num_security_issues, num_non_security, repo_name))
				gt_file.close()
				return
	print("Finished downloading {} security issues and {} non-security issues for repo: {}".format(num_security_issues, num_non_security, repo_name))
	gt_file.close()

n_pages = get_num_pages(get_parsed_page(base_url))
for page in range(1, n_pages + 1):
	page_url = base_url + '?page=' + str(page)
	parsed_page_html = get_parsed_page(page_url)

	repos = [element.text.strip() for element in parsed_page_html.find_all("a", attrs={"itemprop":"name codeRepository"})]
	for repo in repos:
		print("Checking repo [{}] for security labels".format(repo))
		labels = get_repo_security_labels(base_url + '/' + repo)
		if len(labels) > 0:
			print("[{}] has labels: {}".format(repo, str(labels)))
			download_issues(repo, labels)
			print("Total issues downloaded: {}\n".format(global_num_issues))
		else:
			print("[{}] has no security labels\n".format(repo))

print("Final issue download count for ({}): {}".format(group_name, global_num_issues))


from flask import Flask

from redis import Redis

app = Flask('Gaze Capture Server')
conn = Redis()

@app.route("/")
def ping():

    rep = conn.get("updated")

    if rep is not None:
        return rep
    return ''

# get last frame gaze coords
#
@app.route("/gaze")
def gaze_track():

    rep = conn.get("gaze_coord")

    if rep is not None:
        return rep
    return ''

# get last blinks count
#
@app.route("/blink")
def blink_count():

    rep = conn.get("blinks")

    if rep is not None:
        return rep
    return ''

@app.route("/msg")
def msg():

    rep = conn.get("msg")

    if rep is not None:
        return rep
    return ''


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
        self.ans = []
        def trackPath(root,sum_,path,ans):
            if not root:
                return

            if sum_ == targetSum and not(root.left or root.right):
                self.ans.append(path.copy())

            if root.left:
                path.append(root.left.val)
                trackPath(root.left,sum_ + root.left.val,path,ans)

            if root.right:
                path.append(root.right.val)
                trackPath(root.right,sum_ + root.right.val,path,ans)
            leaf = path.pop()
            sum_ -= leaf

        if not root:
            return []

        trackPath(root,root.val,[root.val],[])
        return self.ans


# -*- coding: utf-8 -*-
"""
Created on Sun Jun  2 14:36:52 2019

@author: Darin
"""

import numpy as np
import scipy.sparse as sparse
import matplotlib.pyplot as plt
from FEM import FEM
import Material
import Interpolation
import Update
from Optimization import PyOpt

#def run():
fem = FEM()
Nelx = 100
Nely = 50
Dimensions = [0, 10, 0, 5]
fem.Create2DMesh(Dimensions, Nelx, Nely, maxLevels=10)
fem.SolveSystem = fem.SolveSystemDirect
ofst = 1e-10

lower = np.array([10-ofst, 2.5-ofst])
upper = np.array([10+ofst, 2.5+ofst])
loads = [0, -1e4]
loadSpecs = [{'lower':lower, 'upper':upper, 'force':loads}]
fem.AddLoad(loadSpecs)

lower = np.array([0-ofst, 0-ofst])
upper = np.array([0+ofst, 5+ofst])
bc = [0, 0]
bcSpecs = [{'lower':lower, 'upper':upper, 'disp':bc}]
fem.AddBC(bcSpecs)

#    springpoly = np.array([[-1, 0.5-ofst], [2, 0.5-ofst], [2, 0.5+ofst], [-1, 0.5+ofst]])
#    stiff = [1, 1]
#    springSpecs = [{'poly':springpoly, 'stiff':stiff}]
#    fem.AddSprings(springSpecs)

#fem.Plot()

fem.SetMaterial(Material.PlaneStressElastic(15e9, 0.3))

#update = Update.OCUpdateScheme(0.2, 0.5, np.linspace(0, 1, fem.nElem, endpoint=False),#0.5 * np.ones(fem.nElem),
#                               np.zeros(fem.nElem), np.ones(fem.nElem))
update = Update.MMA(0.5 * np.ones(fem.nElem), 1, np.zeros(fem.nElem), np.ones(fem.nElem))

opt = PyOpt(fem, update)

# Minimum feature size filter
radius = 1.5 * (Dimensions[1] - Dimensions[0]) / Nelx
opt.Filter = opt.ConstructDensityFilter(radius=radius, nElx=[Nelx, Nely])

# Maximum feature size filter
radius = 0.5 * (Dimensions[1] - Dimensions[0]) / Nelx
opt.R = opt.ConstructDensityFilter(radius=radius, nElx=[Nelx, Nely])
rowsum = opt.R.indptr[1:] - opt.R.indptr[:-1]
edge = rowsum.max() - rowsum
opt.R = opt.R.tocoo()
opt.R = sparse.csr_matrix((np.concatenate([1+0*opt.R.data, edge]),
                           (np.concatenate([opt.R.row, np.arange(opt.R.shape[0])]),
                            np.concatenate([opt.R.col, opt.R.shape[0]*np.ones(opt.R.shape[0], dtype=int)]))))

fem.Initialize()

from Functions import Compliance, Volume, Stability
opt.AddFunction(Compliance, 1., 0, 1, 'objective')
opt.AddFunction(Volume, 0.4, 0, 1, 'constraint')

for penal in np.linspace(1,4,7):
    print("Penalty set to %1.2f" % penal)
    opt.SetInterpolation(Interpolation.SIMP_CUT(opt.Filter, opt.R, 50, penal,
                                                0, 0.01, minStiff=1e-10))
    opt.Optimize(maxit=30, plt_freq=10)


#!/usr/bin/env python3

import sys, os

def main():
    v = sys.version_info
    print('Python version {}.{}.{}'.format(*v))

    w = sys.platform
    print(w)

    z = os.name
    print(z)

    # y = os.getenv('PATH')  # imprime variaveis de ambiente
    # print(y)

    i = os.getcwd()  # imprime diretorio local
    print(i)

if __name__=="__main__":main()

"""Mais sobre modulos acessem: https://docs.python.org/3/library/
"""


from typing import List
import copy

import numpy as np
from gymnasium.utils import EzPickle
from pettingzoo.utils.conversions import parallel_wrapper_fn

from pettingzoo.mpe._mpe_utils.core import Agent, Landmark, World, Entity
from pettingzoo.mpe._mpe_utils.scenario import BaseScenario
from pettingzoo.mpe._mpe_utils.simple_env import SimpleEnv, make_env

class raw_env(SimpleEnv):
    def __init__(self, max_cycles=500, num_coins = 20, continuous_actions=True, render_mode=None):
        EzPickle.__init__(self, max_cycles, num_coins, continuous_actions, render_mode)
        scenario = Scenario()
        world = scenario.make_world(num_coins = num_coins)
        super().__init__(
            scenario=scenario,
            world=world,
            # render_mode=render_mode,
            max_cycles=max_cycles,
            continuous_actions=continuous_actions,
        )
        self.infos = scenario.infos
        self.metadata["name"] = "coin_game"

    def step(self, action):
        if self.dones[self.agent_selection]:
            return self._was_done_step(action)
        cur_agent = self.agent_selection
        current_idx = self._index_map[self.agent_selection]
        next_idx = (current_idx + 1) % self.num_agents
        self.agent_selection = self._agent_selector.next()

        self.current_actions[current_idx] = action

        if next_idx == 0:
            self._execute_world_step()
            self.infos = copy.deepcopy(self.scenario.infos)
            self.steps += 1
            if self.steps >= self.max_cycles or (len(self.world.landmarks) == 0):  # if all coin were picked, the game terminals
                for a in self.agents:
                    self.dones[a] = True
        else:
            self._clear_rewards()

        self._cumulative_rewards[cur_agent] = 0
        self._accumulate_rewards()

env = make_env(raw_env)
parallel_env = parallel_wrapper_fn(env)

class Scenario(BaseScenario):
    def make_world(self, num_agents_per_type = 1, num_coins = 20,
                        observe_range = 0.5, num_goals = 5):
        world = World()
        # basic flags
        self.has_coin = False
        self.types = ['red', 'blue']
        #
        self.num_agents = num_agents_per_type*2
        self.num_coins = num_coins
        self.observe_range = observe_range
        self.num_goals = num_goals
        self.init_bound = max(1, num_coins / 20)
        # add agents
        world.agents = [Agent() for _ in range(self.num_agents)]
        for i, agent in enumerate(world.agents):
            agent.type = 0 if i < num_agents_per_type else 1
            index = num_agents_per_type - i if agent.type else i
            agent.name = f"{self.types[agent.type]}_agent_{index}"
            agent.collide = True
            agent.silent = True
        #* coin will be added in `.reset_world()`

        # used for send information to Env during each `.step()`
        self.info: dict[str, list[int]] = dict()
        self.infos = {}
        # self.info[agent] = [num1, num2]
        # num1 and num2 are int, which means, correspondly, the total number of picked coins with the same color and the number of those with different color
        return world

    def reset_world(self, world, np_random):
        self.np_random = np_random
        # random properties for agents
        for i, agent in enumerate(world.agents):
            agent.color = [np.array([1, 0, 0]), np.array([0, 0, 1])][agent.type]
        # random properties for landmarks
        world.landmarks = [Landmark() for _ in range(self.num_coins)]
        for i, landmark in enumerate(world.landmarks):
            landmark.state.p_pos = np_random.uniform(-self.init_bound, +self.init_bound, world.dim_p)
            landmark.state.p_vel = np.zeros(world.dim_p)
            landmark.type = None    # to be set in .generate_coin()
            landmark.type = 0 if i < self.num_coins/2 else 1  #* equal number of both type
            landmark.name = f"{self.types[landmark.type]}_coin"
            landmark.size = 0.03    # I could not find how to control the `alpha` of entities
        # set random initial states
        for agent in world.agents:
            agent.state.p_pos = np_random.uniform(-self.init_bound, +self.init_bound, world.dim_p)
            agent.state.p_vel = np.zeros(world.dim_p)
            agent.state.c = np.zeros(world.dim_c)

        self.generate_coin(world)
        self.info: dict[str, list[int]] = {agent.name: [0, 0] for agent in world.agents}
        self.infos = {agent.name: self.info for agent in world.agents}    # lazy reference
        self.checked_rew = {agent: False for agent in world.agents}
        self.cur_rew = {agent: 0 for agent in world.agents}

    def reward(self, agent: Agent, world: World):
        _agent = agent
        if self.checked_rew[agent]:
            # if every agent called the `.reward()` function, recaculate the latest collision state of coins
            # self.info: dict[str, list[bool]] = {agent.name: [] for agent in world.agents}
            self.checked_rew = {agent: False for agent in world.agents}
            self.cur_rew = {agent: 0 for agent in world.agents}

            left_coins = []
            for coin in world.landmarks:
                flag = False
                for agent in world.agents:
                    dis_min = agent.size + coin.size
                    dis = np.linalg.norm(agent.state.p_pos - coin.state.p_pos)
                    if dis < dis_min:
                        flag = True
                        self.info[agent.name][coin.type == agent.type] += 1
                        self.cur_rew[agent] += 1
                        # once pick up a coin, get 1 reward
                        if coin.type != agent.type:
                            # if pick up others' coin, others get -1 punishment
                            for other in world.agents:
                                if other.type != agent.type:
                                    self.cur_rew[other] -= 1
                if not flag:
                    left_coins.append(coin)

            # delete picked-up coin
            world.landmarks = left_coins

        self.checked_rew[_agent] = True
        reward = self.cur_rew[_agent]
        return reward

    def observation(self, agent: Agent, world: World):
        # get positions of all entities in this agent's reference frame
        self_pos = []
        self_vel = []
        oppo_pos = []
        coin_obs = []
        num_goal = self.num_goals
        for entity in world.agents:
            if entity is agent:
                self_pos.append(entity.state.p_pos)
                self_vel.append(entity.state.p_vel)
            else:
                oppo_pos.append(entity.state.p_pos - agent.state.p_pos)

        dis = []
        for coin in world.landmarks:
            same_type = np.ones(1) if coin.type == agent.type else np.ones(1)*-1
            pos = coin.state.p_pos - agent.state.p_pos
            coin_obs.append(np.concatenate([pos, same_type]))
            dis.append(np.linalg.norm(pos))

        # deal with observation of goals
        num_left_coins = len(world.landmarks)
        indices = np.argsort(dis)
        holder = np.zeros(3)

        coin_obs = [coin_obs[i] for i in indices[:min(num_goal, num_left_coins)]] + [holder] * max(0 ,num_goal - num_left_coins)

        return np.concatenate(self_pos + self_vel + oppo_pos + coin_obs)

    def generate_coin(self,world: World):
        np_random: np.random.Generator = self.np_random

        coins_before = []
        for coin in world.landmarks:
            while self.check_collide(coin, world.agents + coins_before):
                coin.state.p_pos = np_random.uniform(-self.init_bound, +self.init_bound, world.dim_p)
            # coin.type = np_random.integers(0, 2)  # random number of type setting
            coin.color = [np.array([1, 0, 0]), np.array([0, 0, 1])][coin.type]
            coins_before.append(coin)

    def check_collide(self, coin: Landmark, entity: List[Entity]):
        for agent in entity:
            dis = np.sum(np.square(agent.state.p_pos - coin.state.p_pos))
            dis_min = np.square(agent.size + coin.size)
            if dis < dis_min:
                return True
        return False


import redis
import time
import sys
import ast

r = redis.Redis(host='localhost', port=6379, db=0)

def subscribeChannel(channels):
    p = r.pubsub()
    p.subscribe(channels)

    while True:
        message = p.get_message()
        if message:
            print(message)
        time.sleep(0.01)

def publishChannel(channel,msg):
    r.publish(channel, msg)

if __name__ == "__main__":
    args = sys.argv

    if args[1]=='subscribeChannel':

        args[2]=bytes(args[2],"utf-8")
        args[2] = args[2].decode("UTF-8")
        args[2]= ast.literal_eval(args[2])
        for channel in args[2]:
            print(f"subscribed to : {channel}\n")
        globals()[args[1]](args[2])

    elif args[1]=='publishChannel':
        print(f"Published Message : {args[3]}")
        globals()[args[1]](args[2],args[3])


import random

def guess_the_num():
    r = random.randint(1,20)
    print("Hello! What is your name?")
    name = input()
    print("Well,", name + ',',"I am thinking of a number between 1 and 20." )
    print('Take a guess.')
    num = int(input())
    guesses = 1
    while(num != r):
        if num < r:
            print('Your guess is too low.')
            print('Take a guess.')
        else:
            print('Your guess is too high')
            print('Take a guess.')
        num = int(input())
        guesses += 1
    else:
        print('Good job,',name + '!','You guessed my number in', guesses,'guesses!')

guess_the_num()


# Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

# The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

class Solution(object):
    def isValid(self, s):
        st = []
        for c in s:
            if c == '(':
                st.append(')')
            elif c == '[':
                st.append(']')
            elif c == '{':
                st.append('}')
            elif len(st) == 0 or st.pop() != c:
                return False
        return len(st) == 0


# 105. 从前序与中序遍历序列构造二叉树
# 给定一棵树的前序遍历 preorder 与中序遍历  inorder。请构造二叉树并返回其根节点。

# 关键点在于理解前序遍历和中序遍历结果的特点。参考 https://labuladong.gitee.io/algo/2/18/23/
# 下面的代码在递归时使用了值传递，这样函数更加简洁，可以改为传递引用加 index 位置以提升效率

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

class Solution:
    def buildTree(self, preorder: list[int], inorder: list[int]) -> TreeNode:
        if len(preorder) == 0:
            return None
        root_val = preorder[0]
        root = TreeNode(root_val)

        if len(preorder) == 1:
            return root

        root_pos_in_inorder = 0
        for i, v in enumerate(inorder):
            if v == root_val:
                root_pos_in_inorder = i
                break

        left_len = root_pos_in_inorder

        left_preorder = preorder[1:left_len + 1]
        right_preorder = preorder[left_len+1:]
        left_inorder = inorder[:root_pos_in_inorder]
        right_inorder = inorder[root_pos_in_inorder+1:]

        root.left = self.buildTree(left_preorder, left_inorder)
        root.right = self.buildTree(right_preorder, right_inorder)

        return root


import unittest

# from SNDG.Structure.StructureVariant import StructureVariant
from SNDG import execute
from io import StringIO
import tempfile
import json

class TestVcfSnpeffIO(unittest.TestCase):

    def test_residues_mapping(self):
        stdout = tempfile.NamedTemporaryFile()
        execute("python3 -m SNDG.Structure.StructureVariant residues -i ./test/prot2.fasta --pdb_data /tmp/data -s 2VYI"
                , stdout=stdout)

        with open(stdout.name) as output:
            contents = output.read()
        stdout.close()
        expected = ' '.join("""pdb     chain   resid   alt     ref     pos     pdb_pos
2vyi    A       81      P       G       23      1
2vyi    A       160     K       A       160     77
2vyi    B       160     K       A       160     74""".split())
        self.assertEqual(
            expected, ' '.join(contents.strip().split())
        )

    def test_residues_ann(self):
        stdout = tempfile.NamedTemporaryFile()
        execute("cat test/test.tbl | python -m SNDG.Structure.StructureVariant ann --pdb_data /tmp/data "
                , stdout=stdout)
        with open(stdout.name) as output:
            contents = output.read()
        stdout.close()
        anns = [json.loads(x) for x in contents.split("###") if x.strip()]
        self.assertEqual(4, len(anns))
        r2vyi_B_160_K = [x["ann"] for x in anns if x["residue"] == "2vyi_B_160_K"][0]

        self.assertEqual(0.705, r2vyi_B_160_K["pockets"][0]["druggabilitty"])

        r1azm_A_91_P = [x["ann"] for x in anns if x["residue"] == "1azm_A_91_P"][0]
        self.assertTrue("BINDING SITE FOR RESIDUE AZM A 262" in
                        [x["details"] for x in r1azm_A_91_P["binding"]])

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


from operator import pow

class calculator:
    OPERATORS = {}

    @classmethod
    def register_op(cls, op):
        def inner_decorator(orig_function):
            cls.OPERATORS[op] = orig_function
            return orig_function
        return inner_decorator

    @classmethod
    def calculate(cls, x, op, y):
        if op in cls.OPERATORS:
            return cls.OPERATORS[op](x, y)
        else:
            raise TypeError(f"Unknown operator {op}")

    @classmethod
    def show_ops(cls):
        for op, function in cls.OPERATORS.items():
            print(f"{op:2} {function.__name__}")

if __name__ == "__main__":
    @calculator.register_op('+')
    def my_add(x, y):
        return x + y
    # or do it with a lot more typing,
    # typing 'my_add' 2 extra times
    # my_add = calculator.register_op('+')(my_add)

    @calculator.register_op('-')
    def my_subtract(x, y):
        return x - y

    @calculator.register_op('*')
    def my_multiply(x, y):
        return x * y

    # decorate existing function from a module
    pow = calculator.register_op('**')(pow)

    print(calculator.calculate(5, '+', 7))
    print(calculator.calculate(100, '-', 72))
    try:
        print(calculator.calculate(3, '*', 14))
    except TypeError as err:
        print(err)
    print(calculator.calculate(2, '**', 5))

    calculator.show_ops()


import socket, select, sys
from ModelCliente import Client
import utils
if __name__ == "__main__":
    try:
        cliente = Client(sys.argv[1],int(sys.argv[2]),sys.argv[3])
        cliente.connect()
        cliente.prompt()

    except IndexError:
        print('inform Host, Port and your Name')
        sys.exit()

    try :

        while 1:
            socklist = [sys.stdin,cliente.socket]
            read_sockets, write_sockets, error_sockets = select.select(socklist , [], [])

            for sock in read_sockets:
                if sock == cliente.socket:
                     cliente.getData(sock)

                else :
                    msg = str(sys.stdin.readline()).encode()
                    cliente.post(msg)
                    cliente.prompt()

    except Exception as e:
        print(e)
        print('Desconectado')
        cliente.disconnect()
        sys.exti()
    except KeyboardInterrupt as e:
        cliente.disconnect()
        print('Desconectado')
        sys.exit()


from flask import Blueprint, render_template, redirect, url_for, request, \
        flash, jsonify
from flask.ext.login import login_required, login_user, logout_user, \
        current_user
from datetime import datetime, timedelta
from sklearn.metrics import silhouette_score
from sklearn.cluster import MiniBatchKMeans as KMeans
import numpy as np

from .. import db
from ..models.user import User
from ..models.device import Device
from ..models.measure import MeasurePoint, MeasureValue, SENSOR_NAMES

bp = Blueprint("analysis", __name__)

@bp.route('/')
def index():
    return render_template('analysis/index.html')

@bp.route('/status/')
def status():
    context = {}
    context['total_users'] = User.query.count()
    context['total_devices'] = Device.query.count()
    context['total_measures'] = MeasurePoint.query.count()

    return render_template('analysis/status.html', **context)

@bp.route('/maps/')
def maps():
    return render_template('analysis/maps.html')

def _point_to_json_dict(point):
    item = dict(timestamp=point.timestamp.timestamp(),
                deviceId=point.device_id,
                latitude=point.latitude, longitude=point.longitude,
                accuracy=point.accuracy, values=[])
    for value in point.values.all():
        item['values'].append(dict(type=value.type_name, value=value.value))
    return item

@bp.route('/maps/area/')
def area():
    args = request.args.get
    try:
        date_from = datetime.strptime(args('from'), '%Y-%m-%d')
    except (ValueError, TypeError):
        date_from = None
    try:
        date_to = datetime.strptime(args('to'), '%Y-%m-%d')
        date_to += timedelta(days=1)
    except (ValueError, TypeError):
        date_to = None

    points = MeasurePoint.query
    points = points.filter(MeasurePoint.longitude > args('west')
                  ).filter(MeasurePoint.longitude < args('east')
                  ).filter(MeasurePoint.latitude < args('north')
                  ).filter(MeasurePoint.latitude > args('south'))
    if date_from is not None:
        points = points.filter(MeasurePoint.timestamp >= date_from)
    if date_to is not None:
        points = points.filter(MeasurePoint.timestamp < date_to)

    measures = [_point_to_json_dict(p) for p in points]
    return jsonify(count=len(measures), points=measures)

CLUSTER_SENSOR_TYPES = ['Temperature', 'Humidity',
                        'OxidizingGas', 'ReducingGas']

@bp.route('/maps/cluster/', methods=['POST'])
def do_clustering():
    keys = request.get_json()

    points = []
    values = []
    types = set(CLUSTER_SENSOR_TYPES)
    for id, timestamp in keys:
        time = datetime.fromtimestamp(timestamp)
        point = MeasurePoint.query.get((id, time))
        value = {v.type_name: v.value for v in point.values.all()}
        if not set(value.keys()) >= types:
            continue
        # Normalization:
        value['OxidizingGas'] /= 1000
        value['ReducingGas'] /= 10000
        points.append(point)
        values.append(value)

    X = [[v[t] for t in CLUSTER_SENSOR_TYPES]
         for v in values]
    X = np.array(X)
    k = KMeans(n_clusters=2, init='k-means++')
    k.fit(X)
    score = silhouette_score(X, k.labels_)

    groups = [[_point_to_json_dict(p) for p, l in
              zip(points, k.labels_) if l == i] for i in
              range(max(k.labels_) + 1)]

    return jsonify(score=score, groups=groups)


import numpy as np
import PyFileIO as pf
from .ListFiles import ListFiles
from . import Globals
import os

def _ReadCSV(fname):

	data = pf.ReadASCIIData(fname,Header=False,dtype=[('x','float64'),('y','float64')])

	L = np.sqrt(data.x**2 + data.y**2)
	Phi = np.arctan2(data.y,data.x)

	#extract date and time fromt he file name
	bname = os.path.splitext(os.path.basename(fname))[0]
	ds,ts = bname.split('-')
	Date = np.int32(ds)
	ut = np.float64(ts)

	return Date,ut,L,Phi

def _FindCSV():

	#search directories

	#firstly lookin the module/__data/csv path
	mdata = Globals.ModuleDataPath + 'csv/'
	mfiles = ListFiles(mdata)
	keep = np.zeros(mfiles.size,dtype='bool')
	for i in range(0,mfiles.size):
		if os.path.splitext(mfiles[i])[-1] == '.csv':
			keep[i] = True
	mfiles = mfiles[keep]

	#secondly look in the data path
	ddata = Globals.DataPath + 'csv/'
	if os.path.isdir(ddata):
		dfiles = ListFiles(ddata)
		keep = np.zeros(dfiles.size,dtype='bool')
		for i in range(0,dfiles.size):
			if os.path.splitext(dfiles[i])[-1] == '.csv':
				keep[i] = True
		dfiles = dfiles[keep]
	else:
		dfiles = np.array([],dtype='object')

	#join them
	csvfiles = np.append(mfiles,dfiles)

	return csvfiles

def _ReadAllCSV():

	files = _FindCSV()

	#read each one
	Date = []
	ut = []
	csv = []
	for i,f in enumerate(files):
		print('\rReading CSV {:d} of {:d}'.format(i+1,files.size),end='')
		d,t,L,Phi = _ReadCSV(f)
		Date.append(d)
		ut.append(t)
		csv.append((L,Phi,None,None))
	print()

	return Date,ut,csv


from wsgiref.simple_server import make_server
import json
import urllib.request
import conf

# The application interface is a callable object
def application(environ, start_response):

    # Build the response body possibly
    # using the supplied environ dictionary
    response_body = 'Request method: %s' % environ['REQUEST_METHOD']

    # HTTP response code and message
    status = '200 OK'

    # HTTP headers expected by the client
    # They must be wrapped as a list of tupled pairs:
    # [(Header name, Header value)].
    response_headers = [
        ('Content-Type', 'text/plain'),
        ('Content-Length', str(len(response_body)))
    ]

    # Send them to the server using the supplied function
    start_response(status, response_headers)

    # Return the response body. Notice it is wrapped
    # in a list although it could be any iterable.
    return [response_body.encode()]

def public_folder(environ, start_response):

    # Construct the API request headers
    headers = {
        "Content-type": "application/json",
        "Authorization": "Bearer " + conf.oauth_token
    }

    # Get the filename from the request
    path = environ.get("PATH_INFO", "")

    # First see if the file has been shared before
    data = json.dumps( { "path": path } ).encode("utf-8")

    # Do the request
    req = urllib.request.Request("https://api.dropboxapi.com/2/sharing/list_shared_links",
                                data=data, headers=headers)

    try:
        # If we get here the request was successful

        # Get the reponse, decode it and tranform the json in a dictionary
        response = urllib.request.urlopen(req)
        data = json.loads(response.read().decode('utf8'))

        # get the url from the dictionary
        url = data["links"][0]["url"]

        # Return the HTTP response code and location
        status = "302 Found"
        start_response(status, [("Location", url)])

        return [b'']

    except urllib.error.URLError as e:
        # error code 409 means the file is not shared yet
        if e.code != 409:

            # We have hit an unexpected error
            print(e.reason)
            print(e.read().decode("utf8", 'ignore'))
            start_response("500 Internal server error")
            return [b'']

    # New file. Not shared yet, share it now
    data = json.dumps( { "path": path, "settings": { "requested_visibility": "public" } } ).encode("utf-8")
    req = urllib.request.Request("https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings",
                                data=data, headers=headers)

    try:
        response = urllib.request.urlopen(req)
        data = json.loads(reponse.read().decode('utf8'))

        #print(response.read().decode('utf8'))
        print(data)
        print(data[0]["url"])

        # Return the HTTP response code and location
        status = "302 Found"
        start_response(status, [("Location", url)])

        return [b'']

    except urllib.error.URLError as e:

        if e.code == 409:

            # The requested file is not there
            status = "404 Not Found"
            response_body = 'File Not Found: %s' % path

            # HTTP headers expected by the client
            # They must be wrapped as a list of tupled pairs:
            # [(Header name, Header value)].
            response_headers = [
                ('Content-Type', 'text/plain'),
                ('Content-Length', str(len(response_body)))
            ]

            start_response(status, response_headers)
            return [response_body.encode()]

        print(e.reason)
        print(e.read().decode("utf8", 'ignore'))

        start_response("500 Internal server error")
        return [b'']

#
# https://docs.python.org/3.6/library/wsgiref.html
#

def main():

    if conf.oauth_token == "" or conf.public_folder == "":
        print("Missing configuration!")
        print("Make sure the values in 'conf.py' are set.")
        return

    # Instantiate the server
    httpd = make_server('localhost', 8080, public_folder)

    # Wait for a single request, serve it and quit
    httpd.handle_request()

    # Start the webserver
    #httpd.serve_forever()

if __name__ == "__main__":
    main()


class Node:
    def __init__(self, data, next):
        self.data = data
        self.next = next

class LinkedList:
    def __init__(self):
        self.head = None

    def insert_beginning(self, data):
        node = Node(data, self.head)
        self.head = node

    def print(self):
        if self.head is None:
            print("Empty list")
            return

        itr = self.head
        listr = ''

        while itr:
            listr += str(itr.data) + '-->'
            itr = itr.next
        print(listr)

if __name__ == '__main__':
    li = LinkedList()
    li.insert_beginning(5)
    li.insert_beginning(89)
    li.print()


from keras.models import Sequential
from keras.optimizers import RMSprop
from keras.callbacks import EarlyStopping, CSVLogger, TensorBoard

import math
import numpy as np
import os
import json
import pickle
import keras.models

from . import utils
from .callbacks import CaptionCallback
from .vocab import Vocab
from .layers import *
from .postprocess import post_process

class CaptionModel:
    def __init__(self, vocab, img_size=(128, 128),
                 sentence_len=20, dropout=0.0, connector_dim=1000, save_dir='models/default',
                 img_loader=utils.load_image):
        self.img_size = img_size
        self.sentence_len = sentence_len
        self.save_dir = save_dir
        self.dropout = dropout
        self.vocab = vocab
        self.connector_dim = connector_dim
        self.img_loader = img_loader

        self.model = Sequential()

    def compile(self, optimizer=RMSprop()):
        self.model.compile(optimizer, loss='categorical_crossentropy',
                           metrics=['acc'], sample_weight_mode='temporal')

    def train(self, img_paths, captions, val_img, val_captions,
              epochs=100, batch_size=10, initial_epoch=0):
        os.makedirs(self.save_dir, exist_ok=True)

        per_epoch = math.ceil(len(captions) / batch_size)
        val_per_epoch = math.ceil(len(val_captions) / batch_size)
        callbacks = [CaptionCallback(self, monitor='loss', samples=img_paths[:5] + val_img[:5]),
                     CSVLogger(os.path.join(self.save_dir, 'epochs.csv'))]

        _gen = self._generate_batch(img_paths, captions, batch_size)
        _val_gen = self._generate_batch(val_img, val_captions, batch_size)
        self.model.fit_generator(_gen, per_epoch, shuffle=False, max_queue_size=5,
                                 epochs=epochs, initial_epoch=initial_epoch,
                                 validation_data=_val_gen, validation_steps=val_per_epoch,
                                 callbacks=callbacks)

    def evaluate(self, test_img, test_captions, batch_size=10):
        steps = math.ceil(len(test_captions) / batch_size)
        metrics = self.model.evaluate_generator(self._generate_batch(test_img, test_captions, batch_size),
                                                steps=steps, max_queue_size=3)
        return metrics

    def caption(self, image, postprocess=True):
        if type(image) == str:
            return self.caption(self.img_loader(image, self.img_size))
        else:
            assert image.shape[0] == self.img_size[0]
            assert image.shape[1] == self.img_size[1]

            m = np.zeros((1, self.img_size[0], self.img_size[1], 3))
            m[0, :, :, :] = image
            probs = self.model.predict(m, batch_size=1, verbose=0)
            word_idx = np.argmax(probs, axis=-1)
            caption = ''
            # noinspection PyTypeChecker
            for idx in word_idx[0]:
                word = self.vocab[idx]
                if word == Vocab.END_TOKEN:
                    break
                caption += word + ' '

            if postprocess:
                caption = post_process(caption)
            return caption

    def caption_batch(self, images, image_ids=None, to_json=False,
                      json_file=None, batch_size=32, postprocess=True):
        captions = []

        _img_mat = np.zeros((batch_size, self.img_size[0], self.img_size[1], 3))
        img_len = len(images)

        for i in range(batch_size, img_len + 1, batch_size):
            print(' Caption %d/%d...' % (i, img_len), end='\r')

            _images = images[i-batch_size:i]
            if len(_images) != batch_size:
                _img_mat = np.zeros((len(_images), self.img_size[0], self.img_size[1], 3))
            for j, _path in enumerate(_images):
                _img_mat[j, :, :] = self.img_loader(_path, self.img_size)
            probs = self.model.predict_on_batch(_img_mat)
            word_idx = np.argmax(probs, axis=-1)

            for j in range(len(_images)):
                _caption = ''
                for idx in word_idx[j, :]:
                    word = self.vocab[idx]
                    if word == Vocab.END_TOKEN:
                        break
                    _caption += word + ' '
                if postprocess:
                    _caption = post_process(_caption)
                captions.append(_caption)
        print()

        if not to_json:
            return captions
        else:
            jd = []
            for _caption, _id in zip(captions, image_ids):
                jd.append({'image_id': _id, 'caption': _caption})
            if json_file is not None:
                print(' Saving...')
                with open(json_file, 'wt') as f:
                    json.dump(jd, f)

            return json.dumps(jd)

    def _generate_batch(self, img_paths, captions, batch_size):
        _img_mat = np.zeros((batch_size, self.img_size[0], self.img_size[1], 3))
        _cap_mat = np.zeros((batch_size, self.sentence_len, self.vocab.size))
        _cap_mask = np.zeros((batch_size, self.sentence_len))
        tokens = []
        for c in captions:
            tokens.append(self.vocab.tokenizer(c))

        while True:
            for i in range(batch_size, len(captions), batch_size):
                _img_paths = img_paths[i - batch_size:i]
                _captions = tokens[i - batch_size:i]

                for j, _path in enumerate(_img_paths):
                    _img_mat[j, :, :] = self.img_loader(_path, self.img_size)

                for j, _caption in enumerate(_captions):
                    _cap_mat[j, :, :] = self.vocab.encode_sentence(_caption, length=self.sentence_len)
                    _cap_mask[j, :] = self.vocab.mask_sentence(_caption, length=self.sentence_len)

                yield _img_mat, _cap_mat, _cap_mask

    def _generate_image_batch(self, img_paths, batch_size):
        _img_mat = np.zeros((batch_size, self.img_size[0], self.img_size[1], 3))
        while True:
            for i in range(batch_size, len(img_paths), batch_size):
                _img_paths = img_paths[i - batch_size:i]
                for j, _path in enumerate(_img_paths):
                    _img_mat[j, :, :] = self.img_loader(_path, self.img_size)

            yield _img_mat

    def summary(self):
        self.model.summary()

    def save(self):
        os.makedirs(self.save_dir, exist_ok=True)
        f1 = open(os.path.join(self.save_dir, 'configs.pkl'), 'wb')
        f2 = open(os.path.join(self.save_dir, 'vocab.pkl'), 'wb')

        self.model.save(os.path.join(self.save_dir, 'model.hdf5'))
        configs = {
            'img_size': self.img_size,
            'sentence_len': self.sentence_len,
            'dropout': self.dropout,
            'img_loader': self.img_loader,
            'save_dir': self.save_dir
        }
        pickle.dump(configs, f1)
        pickle.dump(self.vocab, f2)
        f1.close()
        f2.close()

    @classmethod
    def load(cls, load_dir):
        f1 = open(os.path.join(load_dir, 'configs.pkl'), 'rb')
        f2 = open(os.path.join(load_dir, 'vocab.pkl'), 'rb')

        configs = pickle.load(f1)
        vocab = pickle.load(f2)
        model = keras.models.load_model(os.path.join(load_dir, 'model.hdf5'),
                                        custom_objects={
                                            'DecoderLSTM': DecoderLSTM,
                                            'DecoderLSTMCell': DecoderLSTMCell
                                        })
        f1.close()
        f2.close()

        cm = cls(vocab, **configs)
        cm.model = model
        return cm

    @classmethod
    def migrate(cls, model_path, vocab_path, **configs):
        f = open(vocab_path, 'rb')
        vocab = pickle.load(f)
        f.close()

        model = keras.models.load_model(
            model_path,
            custom_objects={
                'DecoderLSTM': DecoderLSTM,
                'DecoderLSTMCell': DecoderLSTMCell
            }
        )

        cm = cls(vocab, **configs)
        cm.model = model
        return cm


# Max height of tree is 5

import pandas as pd
import numpy as np
buff = np.finfo(float).eps
import random

def entropy(data):
    target = data.columns[-1]
    entropy = 0
    values = pd.unique(data[target])
    for value in values:
        prob = data[target].value_counts()[value]/len(data[target])
        entropy += -prob * (np.log(prob))
    #print(entropy)

    return entropy

def entropy_attribute(data,attribute):
    target = data.columns[-1]

    values = pd.unique(data[attribute])
    targets = pd.unique(data[target])
    entropy = 0
    for value in values:
        temp = 0
        for t in targets:
            n = len(data[attribute][data[attribute]==value][data[target]==t])
            d = len(data[attribute][data[attribute]==value])
            f = n/(d+buff)  # buff added to not make denominator zero
            temp += -f * (np.log(f + buff))

        entropy += (d/len(data))*temp
        #print(entropy)
        return abs(entropy)

def best(data):
    entopy_attr = []
    attributes = list(data.columns)[:-1]
    l = []
    for attribute in attributes:
        #print(attribute)
        l.append(entropy(data) - entropy_attribute(data,attribute))

    #print(ig)
    return data.columns[:-1][np.argmax(l)]

def get_table(data, node,value):
    return data[data[node] == value].reset_index(drop=True)

def makedtree(data,height,dtree=None):
    target = data.columns[-1]
    #print(target)
    node = best(data)

    values = np.unique(data[node])

    if dtree is None:
        dtree={}
        dtree[node] = {}

    for value in values:

        table = get_table(data,node,value)
        classes,count = np.unique(table[target],return_counts=True)
        if len(count)==1 or height==5:
            if(len(count) == 1):
                dtree[node][value] = classes[0]
            else:
                dtree[node][value] = classes[np.argmax(count)]
        else:
            dtree[node][value] = makedtree(table,height+1)

    return dtree

data1 = pd.read_csv('weather.csv')
data2 = pd.read_csv('train.csv')

#data1.head(4)

#data2.head(4)

# Building dtree 1
tree1 = makedtree(data1,0,None)
#print(tree1)

# Building dtree 2
tree2 = makedtree(data2,0,None)
#print(tree2)

def print_tree(dtree,width):

    for key in dtree.keys():
        for value in list(dtree[key].keys()):
            tree_value = dtree[key][value]
            if(type(tree_value) is dict):
                print("| "*width + key + ' = ' + str(value))
            else:
                print("| "*width + key + ' = ' + str(value),end = '')

            #print(tree_value)
            if(type(tree_value) is dict):
                print_tree(tree_value,width+1)
            else:
                print(": " + tree_value)

print("Tree 1")
print_tree(tree1,0)
print()

print("Tree 2")
print_tree(tree2,0)
print()

# Prediction

def predict(data,dtree):

    prediction = -1
    for value in dtree.keys():
        recurse_value = data[value]

        tree_value = dtree[value][recurse_value]
        if type(tree_value) is dict:
            #print(tree_value)
            prediction = predict(data,tree_value)

        else:
            prediction = tree_value

    if(prediction == -1):
        return None

    return prediction

print()
print("Data 1 Predictions")
p = 0
n = 0
for j in range(0,5):
    random_no = random.randint(0,len(data1)-1)
    print("Test Input " + str(j+1))
    print(data1.iloc[random_no][:-1])
    print('\033[1m',end='')
    pred = predict(data1.iloc[random_no][:-1],tree1)
    print("Prediction = {}".format(pred))
    print('\033[0m')
    if(data1.iloc[random_no][-1] == pred):
          p += 1
    else:
          n += 1
    print()
print('Accuracy = {}'.format(p/(p+n)))
print()

print("Data 2 Predictions")
p = 0
n = 0
for j in range(0,5):
    random_no = random.randint(0,len(data2)-1)
    print("Test Input " + str(j+1))
    print(data2.iloc[random_no][:-1])
    print('\033[1m',end='')
    pred = predict(data2.iloc[random_no][:-1],tree2)
    print("Prediction = {}".format(pred))
    print('\033[0m')
    if(data2.iloc[random_no][-1] == pred):
          p += 1
    else:
          n += 1
    print()
    #print(p,n)

print('Accuracy = {}'.format(p/(p+n)))

# Accuracy coming high because we are using random examples from the training dataset


def Part1(lines):
    counters = {}
    counters[1] = 0
    counters[2] = 0
    counters[3] = 0
    chargeStart = 0
    last = lines[len(lines) - 1]
    print(last)
    while chargeStart < last:
        if (chargeStart + 1) in lines:
            chargeStart += 1
            counters[1] += 1
        elif (chargeStart + 2) in lines:
            chargeStart += 2
            counters[2] += 1
        elif (chargeStart + 3) in lines:
            chargeStart += 3
            counters[3] += 1

    return counters

with open(r"day10-input.txt") as file:
    lines = [int(x.strip()) for x in file.readlines()]

lines.sort()
device = lines[len(lines) - 1] + 3
lines.append(device)
print(lines)

counters = Part1(lines)

print(counters)
print(counters[1] * counters[3])


class Solution:
    def divide(self, dividend: int, divisor: int) -> int:
        ans=0
        dividendABS=abs(dividend)
        divisorABS=abs(divisor)
        while dividendABS>=divisorABS:
            tmp=divisorABS
            mult=1
            while dividendABS>=tmp:
                dividendABS-=tmp
                tmp+=tmp
                ans+=mult
                mult+=mult

        if (dividend>0 and divisor<0) or (dividend<0 and divisor>0):
            ans=0-ans
        if ans>2147483647:
            ans=2147483647
        elif ans<-2147483648:
            ans=-2147483648
        return ans

        '''
        if abs(divisor)==1:
            if divisor<0:
                if dividend<=-2147483648:
                    return 2147483647
                elif dividend>2147483647:
                    return 0-2147483648
                return 0-dividend
            else:
                if dividend>2147483647:
                    return 2147483647
                elif dividend<-2147483648:
                    return -2147483648
                return dividend
        ans=0
        if dividend>0:
            if divisor>0:
                while dividend>=divisor:
                    ans+=1
                    dividend-=divisor
            else:
                while dividend>=abs(divisor):
                    ans-=1
                    dividend+=divisor
        else:
            if divisor>0:
                while abs(dividend)>=divisor:
                    ans-=1
                    dividend+=divisor
            else:
                while abs(dividend)>=abs(divisor):
                    ans+=1
                    dividend-=divisor
        if ans>2147483647:
            return 2147483647
        elif ans<-2147483648:
            return -2147483648
        return ans
        '''


from typing import Union

import ipywidgets as wid
from qiskit.providers.fake_provider.fake_backend import FakeBackend
from qiskit.providers.ibmq.visualization.interactive import iplot_gate_map
from qiskit.providers.ibmq.ibmqbackend import IBMQBackend
from qiskit.providers.ibmq.utils.converters import duration_difference

from .utils import get_next_reservation

def config_tab(backend: Union[IBMQBackend, FakeBackend]) -> wid.GridBox:
    """The backend configuration widget.

    Args:
        backend: Display configuration widget for this backend.

    Returns:
        A widget containing backend configuration information.
    """
    status = backend.status().to_dict()
    config = backend.configuration().to_dict()
    next_resrv = get_next_reservation(backend)
    if next_resrv:
        reservation_str = "in {} ({}m)".format(duration_difference(next_resrv.start_datetime),
                                               next_resrv.duration)
    else:
        reservation_str = '-'

    config_dict = {**status, **config}
    config_dict['reservation'] = reservation_str

    upper_list = ['n_qubits']

    if 'quantum_volume' in config.keys():
        if config['quantum_volume']:
            upper_list.append('quantum_volume')

    upper_list.extend(['operational',
                       'status_msg', 'pending_jobs', 'reservation',
                       'backend_version', 'basis_gates',
                       'max_shots', 'max_experiments'])

    lower_list = list(set(config_dict.keys()).difference(upper_list))
    # Remove gates because they are in a different tab
    lower_list.remove('gates')
    # Look for hamiltonian
    if 'hamiltonian' in lower_list:
        htex = config_dict['hamiltonian']['h_latex']
        config_dict['hamiltonian'] = "$$%s$$" % htex

    upper_str = "
"
    upper_str += """"""

    footer = "
"

    # Upper HBox widget data

    upper_str += "
Property	Value
"
    for key in upper_list:
        upper_str += "
%s	%s
" % (
            key, config_dict[key])
    upper_str += footer

    upper_table = wid.HTMLMath(
        value=upper_str, layout=wid.Layout(width='100%', grid_area='left'))

    img_child = []
    if not config['simulator']:
        img_child = [iplot_gate_map(backend, as_widget=True)]

    image_widget = wid.HBox(children=img_child,
                            layout=wid.Layout(grid_area='right',
                                              max_height='350px',
                                              margin='0px 0px 0px 0px',
                                              display='flex-inline',
                                              align_items='center',
                                              justify_content='center',
                                              width='auto'))

    lower_str = "
"
    lower_str += """"""
    lower_str += "

"
    for key in lower_list:
        if key != 'name':
            lower_str += "
%s	%s
" % (
                key, config_dict[key])
    lower_str += footer

    lower_table = wid.HTMLMath(value=lower_str,
                               layout=wid.Layout(width='auto',
                                                 grid_area='bottom'))

    grid = wid.GridBox(children=[upper_table, image_widget, lower_table],
                       layout=wid.Layout(max_height='500px',
                                         margin='10px',
                                         overflow='hidden scroll',
                                         grid_template_rows='auto auto',
                                         grid_template_columns='33% 21% 21% 21%',
                                         grid_template_areas='''
                                         "left right right right"
                                         "bottom bottom bottom bottom"
                                         ''',
                                         grid_gap='0px 0px'))

    return grid


#!/usr/bin/env python3
import numpy as np
import random
import math as m
from MnnClassifier import MNNClassifier

def scale_X(X):
    c = X.shape[1]
    for v in range(c):
        mi = min(X[:,v])
        X[:,v] = X[:,v]-mi
        ma = max(X[:,v])
        X[:,v] = X[:,v]/ma
    return X

if __name__ == '__main__':
    from sklearn.datasets import make_blobs

    X, t = make_blobs(n_samples=[400, 800, 400], centers=[[0, 0], [1, 2], [2, 3]],
                      n_features=2, random_state=2019)
    #scaler X
    X = scale_X(X)
    indices = np.arange(X.shape[0])
    random.seed(2020)
    random.shuffle(indices)
    X_train = X[indices[:800], :]
    X_val = X[indices[800:1200], :]
    X_test = X[indices[1200:], :]
    t_train = t[indices[:800]]
    t_val = t[indices[800:1200]]
    t_test = t[indices[1200:]]
    indices[:10]
    t2_train = t_train == 1
    t2_train = t2_train.astype('int')
    t2_val = (t_val == 1).astype('int')
    t2_test = (t_test == 1).astype('int')
    print("mnn:")
    mnn = MNNClassifier(dim_hidden=60,eta=0.001)
    mnn.fit(X_train,t_train,epochs=1000)
    print(mnn.accuracy(X_val,t_val))
    print('mnn2:')
    mnn2 = MNNClassifier(dim_hidden=60,eta=0.001)
    mnn2.fit_earlystopping(X_train,t_train,X_val,t_val)
    print("done med fit")
    print(mnn2.accuracy(X_val, t_val))


import torch
import torch.nn as nn
import torch.nn.functional as F

from utils.masking import TriangularCausalMask, ProbMask
from models.encoder import Encoder, EncoderLayer, ConvLayer, EncoderStack
from models.decoder import Decoder, DecoderLayer
from models.attn import FullAttention, ProbAttention, AttentionLayer
from models.embed import DataEmbedding
from models.functions import ReverseLayerF

class Informer(nn.Module):
    def __init__(self, args, OP_features, c_out, seq_len,
                factor=5, d_model=512, n_heads=8, e_layers=3, d_layers=2, d_ff=512,
                dropout=0.0, attn='prob', embed='fixed', activation='gelu',
                output_attention = False, distil=True, mix=True,
                device=torch.device('cuda:0')):
        super(Informer, self).__init__()

        if OP_features == True:
            enc_in, dec_in = 20,20

        elif OP_features == False:
            enc_in, dec_in = 14,14

        self.args = args
        self.seq_len = seq_len
        self.attn = attn
        self.output_attention = output_attention

        self.dropout = nn.Dropout(self.args.dropout)
        # Attention
        Attn = ProbAttention if attn=='prob' else FullAttention

        # Encoding
        self.enc_embedding = DataEmbedding(enc_in, d_model, embed, dropout)

        # Private Encoder
        self.encoder_private = Encoder(
            [
                EncoderLayer(
                    AttentionLayer(Attn(False, factor, attention_dropout=dropout, output_attention=output_attention),
                                d_model, n_heads, mix=False),
                    d_model,
                    d_ff,
                    dropout=dropout,
                    activation=activation
                ) for l in range(e_layers)
            ],
            [
                ConvLayer(
                    d_model
                ) for l in range(e_layers-1)
            ] if distil else None,
            norm_layer=torch.nn.LayerNorm(d_model)
        )

        # Share Encoder
        self.encoder_share = Encoder(
            [
                EncoderLayer(
                    AttentionLayer(Attn(False, factor, attention_dropout=dropout, output_attention=output_attention),
                                d_model, n_heads, mix=False),
                    d_model,
                    d_ff,
                    dropout=dropout,
                    activation=activation
                ) for l in range(e_layers)
            ],
            [
                ConvLayer(
                    d_model
                ) for l in range(e_layers-1)
            ] if distil else None,
            norm_layer=torch.nn.LayerNorm(d_model)
        )

        '''
        #FCL RUL predictor
        self.RUL_predictor = nn.Sequential()
        self.RUL_predictor.add_module('r_fc1',nn.Linear(d_model,256))
        self.RUL_predictor.add_module('r_bn1', nn.BatchNorm1d(self.seq_len))
        self.RUL_predictor.add_module('r_relu1', nn.ReLU(True))
        self.RUL_predictor.add_module('r_drop1', nn.Dropout())
        self.RUL_predictor.add_module('r_fc2', nn.Linear(256, 128))
        self.RUL_predictor.add_module('r_bn2', nn.BatchNorm1d(self.seq_len))
        self.RUL_predictor.add_module('r_relu2', nn.ReLU(True))
        self.RUL_predictor.add_module('r_drop2', nn.Dropout())
        self.RUL_predictor.add_module('r_fc3', nn.Linear(128, 64))
        self.RUL_predictor.add_module('r_fc4', nn.Linear(64, 1))
        '''

        #decoder RUL predictor
        # Decoder
        self.decoder = Decoder(
            [
                DecoderLayer(
                    AttentionLayer(Attn(True, factor, attention_dropout=dropout, output_attention=False),
                                d_model, n_heads, mix=mix),  #d_model
                    AttentionLayer(FullAttention(False, factor, attention_dropout=dropout, output_attention=False),
                                d_model, n_heads, mix=False),  #d_model
                    d_model,
                    d_ff,
                    dropout=dropout,
                    activation=activation,
                )
                for l in range(d_layers)
            ],
            norm_layer=torch.nn.LayerNorm(d_model)
        )
        self.projection1 = nn.Linear(d_model, 128, bias=True) #d_model
        self.projection2 = nn.Linear(128, 64, bias=True) #d_model
        self.projection3 = nn.Linear(64, 1, bias=True)
        self.relu = nn.ReLU(True)

        #domain classifier
        self.domain_classifier = nn.Sequential()
        self.domain_classifier.add_module('c_fc1', nn.Linear(d_model, 128))
        self.domain_classifier.add_module('c_bn1', nn.BatchNorm1d(18))
        self.domain_classifier.add_module('c_relu1', nn.ReLU(True))
        self.domain_classifier.add_module('c_drop1', nn.Dropout())
        self.domain_classifier.add_module('c_fc2', nn.Linear(128, 64))
        self.domain_classifier.add_module('c_bn2', nn.BatchNorm1d(18))
        self.domain_classifier.add_module('c_relu2', nn.ReLU(True))
        self.domain_classifier.add_module('c_fc3', nn.Linear(64, 1))
        self.domain_classifier.add_module('c_softmax', nn.LogSoftmax(dim=1))

        #domain discriminator
        self.domain_discriminator = nn.Sequential()
        self.domain_discriminator.add_module('d_fc1', nn.Linear(d_model, 128))
        self.domain_discriminator.add_module('d_bn1', nn.BatchNorm1d(18))
        self.domain_discriminator.add_module('d_relu1', nn.ReLU(True))
        self.domain_discriminator.add_module('d_fc2', nn.Linear(128, 64))
        self.domain_discriminator.add_module('d_bn2', nn.BatchNorm1d(18))
        self.domain_discriminator.add_module('d_relu2', nn.ReLU(True))
        self.domain_discriminator.add_module('d_fc3', nn.Linear(64, 1))
        self.domain_discriminator.add_module('d_softmax', nn.LogSoftmax(dim=1))

    def forward(self, x, flag, alpha,
                enc_self_mask=None, dec_self_mask=None, dec_enc_mask=None):

        #embedding
        x_emd = self.enc_embedding(x)

        #private encoder & share encoder
        private_feature, private_attns = self.encoder_private(x_emd, attn_mask=enc_self_mask)  #out size  bs*seq_len/2*d_model  64*24*512
        share_feature, share_attns = self.encoder_share(x_emd, attn_mask=enc_self_mask)

        #RUL predictor
        #features = torch.cat((private_feature,share_feature),dim=1)
        #Y_RUL = self.RUL_predictor(features)
        Y_RUL = self.decoder(private_feature,share_feature, x_mask=dec_self_mask, cross_mask=dec_enc_mask)
        #Y_RUL = self.decoder(x_emd,features, x_mask=dec_self_mask, cross_mask=dec_enc_mask)
        Y_RUL = self.dropout(self.projection1(Y_RUL))
        Y_RUL = self.dropout(self.projection2(Y_RUL))
        Y_RUL = self.projection3(Y_RUL)
        Y_RUL = Y_RUL[:,-1,:]

        #domain classifier
        class_output = self.domain_classifier(private_feature)

        #domain discriminator
        if flag in ['train_source','train_target']:
            share_feature = ReverseLayerF.apply(share_feature, alpha)
        domain_output = self.domain_discriminator(share_feature)

        return Y_RUL, class_output, domain_output, share_feature, private_feature


DEBUG = True

def parseInt(s, f, v):
    if type(s) == float:
        if s.is_integer():
            s = int(s)
        else:
            return "wop"
    if f == "m": # Multiply
        return s * v[0]
    elif f == "s": # Subtract
        return s - v[0]
    elif f == "a": # Add
        return s + v[0]
    elif f == "d": # Divide
        return s / v[0]
    elif f == "e": # Exponent
        return s ** v[0]
    elif f == "f": # Flip Sign
        return s * -1
    elif f == "i": # Include Digit
        return int(str(s) + str(v[0]))
    elif f == "r": # Remove digit
        return s // 10
    elif f == "c": # Change Sign
        return int(str(s).replace(str(v[0]), str(v[1])))
    elif f == "n": # Flip Number
        if s > 0:
            return int(str(s)[::-1])
        else:
            return s
    else:
        return 0

st = int(input("Start:"))
g = int(input("Goal:"))
n = int(input("Amount of Functions:"))
c = [[0] for a in range(n)]
for b in range(n):
    a = input("Number " + str(b) + ":").split()
    c[b][0] = a.pop(0)
    c[b].append(list(map(int, a)))
if DEBUG:
    print("Working with:", c)

q = [parseInt(st, c[z][0], c[z][1]) for z in range(n)]
stp = [[c[z]] for z in range(n)]
if DEBUG:
    print("Current queue", q)
    print("Current steps", stp)

cn = False
sc = -1

while len(stp[len(stp) - 1]) < 10:
    cn = q.pop(0)
    sc += 1
    if cn == g:
        print("-------------------------------------------")
        print(":: Reached goal of", g, "in", len(stp[sc]), "steps!")
        print(":: Steps required:", stp[sc])
        break
    for b in range(n):
        z = stp[sc][:]
        z.append(c[b])
        y = parseInt(cn, c[b][0], c[b][1])
        if y != "wop":
            q.append(y)
            stp.append(z)
            if DEBUG:
                print("Queue updated:", q)
        else:
            if DEBUG:
                print("Queue was not updated..")
print(":: Script Stopped")