import unittest

import requests
import xmltodict
from HTMLTestRunner import HTMLTestRunner

from para_bank import ParaBank
from test_data import ParaBankTestData

class TestParaBank(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.para_bank = ParaBank()
        cls.para_bank.admin_page.visit()
        cls.para_bank.admin_page.initialize_database(
            ParaBankTestData.initial_amount, ParaBankTestData.minimum_amount
        )

    @classmethod
    def tearDownClass(cls):
        cls.para_bank.quit()

    def setUp(self):
        self.clean_database()
        self.register()

    def test_registration(self):
        response_content = self.get_customer_personal_data(ParaBankTestData.customer_id)
        self.assertEqual(
            ParaBankTestData.first_name, response_content["customer"]["firstName"]
        )
        self.assertEqual(
            ParaBankTestData.last_name, response_content["customer"]["lastName"]
        )
        self.assertEqual(
            ParaBankTestData.address, response_content["customer"]["address"]["street"]
        )
        self.assertEqual(
            ParaBankTestData.city, response_content["customer"]["address"]["city"]
        )
        self.assertEqual(
            ParaBankTestData.state, response_content["customer"]["address"]["state"]
        )
        self.assertEqual(
            ParaBankTestData.zip_code,
            response_content["customer"]["address"]["zipCode"],
        )
        self.assertEqual(
            ParaBankTestData.phone_number, response_content["customer"]["phoneNumber"]
        )
        self.assertEqual(ParaBankTestData.ssn, response_content["customer"]["ssn"])

    def test_logout_and_login(self):
        self.para_bank.index_page.logout()
        self.para_bank.index_page.login(
            ParaBankTestData.username, ParaBankTestData.password
        )

    def test_clean_database(self):
        expected_state_code = 400
        self.clean_database()
        response = requests.get(
            f"http://parabank.parasoft.com/parabank/services/bank/customers/{ParaBankTestData.customer_id}"
        )
        self.assertEqual(response.status_code, expected_state_code)

    def test_open_savings_account(self):
        self.open_new_account()
        response_content = self.get_customer_account_data(ParaBankTestData.customer_id)
        self.assertEqual(
            self.para_bank.open_account_page.new_account_number,
            response_content["accounts"]["account"][1]["id"],
        )
        self.assertEqual("SAVINGS", response_content["accounts"]["account"][1]["type"])
        self.assertEqual(
            ParaBankTestData.minimum_amount,
            response_content["accounts"]["account"][1]["balance"],
        )

    def test_total_amount(self):
        self.para_bank.overview_page.visit()
        self.assertEqual(
            "$" + ParaBankTestData.initial_amount, self.para_bank.overview_page.total
        )

    def test_apply_for_loan(self):
        self.para_bank.request_load_page.visit()
        self.para_bank.request_load_page.request_a_load(
            ParaBankTestData.loan_amount,
            ParaBankTestData.down_payment,
            ParaBankTestData.account_number,
        )
        response_content = self.get_customer_account_data(ParaBankTestData.customer_id)
        self.assertEqual(
            self.para_bank.open_account_page.new_account_number,
            response_content["accounts"]["account"][1]["id"],
        )
        self.assertEqual("LOAN", response_content["accounts"]["account"][1]["type"])
        self.assertEqual(
            ParaBankTestData.loan_amount,
            response_content["accounts"]["account"][1]["balance"],
        )
        self.assertEqual(
            float(ParaBankTestData.initial_amount)
            - float(ParaBankTestData.down_payment),
            float(response_content["accounts"]["account"][0]["balance"]),
        )

    def test_get_transactions_for_date_range(self):
        self.open_new_account()
        self.para_bank.find_transactions_page.visit()
        self.para_bank.find_transactions_page.find_by_date_range(
            ParaBankTestData.from_date, ParaBankTestData.to_date
        )
        self.para_bank.find_transactions_page.click_first_transaction_result()
        response_content = self.get_account_transaction_data(
            ParaBankTestData.account_number
        )
        self.assertEqual(
            self.para_bank.find_transactions_page.transaction_id,
            response_content["transactions"]["transaction"]["id"],
        )

    def register(self) -> None:
        self.para_bank.register_page.visit()
        self.para_bank.register_page.sign_up(
            ParaBankTestData.first_name,
            ParaBankTestData.last_name,
            ParaBankTestData.address,
            ParaBankTestData.city,
            ParaBankTestData.state,
            ParaBankTestData.zip_code,
            ParaBankTestData.phone_number,
            ParaBankTestData.ssn,
            ParaBankTestData.username,
            ParaBankTestData.password,
            ParaBankTestData.confirm,
        )

    def clean_database(self) -> None:
        self.para_bank.admin_page.visit()
        self.para_bank.admin_page.clean()

    def get_customer_personal_data(self, customer_id: str) -> dict:
        response = requests.get(
            f"https://parabank.parasoft.com/parabank/services/bank/customers/{customer_id}"
        )
        return xmltodict.parse(response.content)

    def get_customer_account_data(self, customer_id: str) -> dict:
        response = requests.get(
            f"https://parabank.parasoft.com/parabank/services/bank/customers/{customer_id}/accounts"
        )
        return xmltodict.parse(response.content)

    def get_account_transaction_data(self, account_number: str) -> dict:
        response = requests.get(
            f"https://parabank.parasoft.com/parabank/services/bank/accounts/{account_number}/transactions"
        )
        return xmltodict.parse(response.content)

    def open_new_account(self) -> None:
        self.para_bank.open_account_page.visit()
        self.para_bank.open_account_page.open_new_account(
            ParaBankTestData.account_type, ParaBankTestData.account_number
        )

if __name__ == "__main__":
    unittest.main(
        testRunner=HTMLTestRunner(
            output="reports", report_name="para-bank-html-report", open_in_browser=True
        )
    )


#!/bin/python3

import os
from argparse import ArgumentParser
from socket import socket
from time import sleep

EOT = b'\x04'

def read(sock: socket, bufsize: int = 1024):
    more = True
    while more:
        data = sock.recv(bufsize)
        yield data.removesuffix(EOT)
        more = not data.endswith(EOT)

if __name__ == '__main__':

    parser = ArgumentParser('server')
    parser.add_argument('--host', '-H', help='IP address or hostname to bind to', type=str, default='localhost')
    parser.add_argument('--port', '-p', help='port to bind to', type=int, default=os.getenv('PORT', 3000))
    args = parser.parse_args()

    local_addr = args.host, args.port

    lstn = socket()
    lstn.bind(local_addr)
    lstn.listen(1)
    while True:
        print(f'Listening on {lstn.getsockname()}...')
        conn, peer_addr = lstn.accept()
        print(f'Connected to {peer_addr}.')
        with conn:
            # raw_req = conn.recv(128)
            # while not raw_req.endswith(EOT):
            #     req = raw_req.decode()
            #     conn.sendall(req.upper().encode())
            #     raw_req = conn.recv(128)
            # else:
            #     req = raw_req.removesuffix(EOT).decode()
            #     conn.sendall(req.upper().encode())
            for data in read(conn, bufsize=32):
                sleep(1.0)
                conn.sendall(data.decode().upper().encode())


import pandas as pd
import numpy as np

# （一）创建一个序列
# import random
# array = []
# for _ in range(10):
#     array.append(random.randrange(1,21,1))
# print(array)
# array[8] = np.nan
# series = pd.Series(array)
# print(series)
# print(series.values[1])
# print(series.values[3])
# print(series.values[5])
# print(series.median())
# （二）创建一个数据框
# import random
# dates = pd.Series(pd.date_range(start='10/1/2022', end='10/07/2022'))
# print(dates)
# array = []
# for _ in range(28):
#     array.append(random.random())
# arr = np.array(array).reshape((7,4))
# df = pd.DataFrame(arr,index=dates,columns=['A','B','C','D'])
# print(df)
# # 查看每一列数据的类型
# columns=['A','B','C','D']
# for i in columns:
#     print(i,df[i].dtypes)
# # 4 - 7print(df.columns)
# print(df.index)
# print(df.tail(3))
# print(df.describe())
# # 8 - 10
# df.sort_index(inplace=True,ascending=False)
# print(df)
# df.sort_values(by='A',inplace=True)
# print(df)
# print(df.T)

# （三）数据子集的获取
# import random
# dates = pd.Series(pd.date_range(start='10/1/2022', end='10/07/2022'))
# array = []
# for _ in range(28):
#     array.append(random.random())
# arr = np.array(array).reshape((7,4))
# df = pd.DataFrame(arr,index=dates,columns=['A','B','C','D'])
#
# series = df.loc[ : ,['A']]
# print(type(series))
# print(series)
#
# df = df.reset_index()
# print(df)
# print(df.loc[1:3])
# print(df.iloc[[2,4,5],[1,2]])
# print(df.loc[[1]])
# print(df.loc[:,['A','B']])
# # print(df[df>1]) ????

# （四）数据框的操作
# import random
# dates = pd.Series(pd.date_range(start='10/1/2022', end='10/07/2022'))
# array = []
# for _ in range(28):
#     array.append(random.random())
# arr = np.array(array).reshape((7,4))
# df = pd.DataFrame(arr,index=dates,columns=['A','B','C','D'])
#
# series = pd.Series([1,2,3,4,5,6,7],index=pd.date_range(start='10/2/2022', end='10/08/2022'))
# print(series)
#
# df['E'] = series
# print(df)
# print(df.isnull().sum())
# df.fillna(value=df['E'].mean(),inplace=True)
# print(df)

# （五）应用练习
df = pd.read_csv("titanic.csv")
# print(df.shape)
# print(df.info())
# print((df.head(10)))

df.drop(axis=1, labels=['Name', 'Ticket', 'Cabin'], inplace=True)
print(df.describe())

print(df.isnull().any())
df.fillna(value={'Age': df['Age'].mean(),
                 'Embarked': df['Embarked'].mode()[0]},
          inplace=True)
print(df.isnull().any())

from sklearn import preprocessing

f_names = ['Sex', 'Embarked']
for x in f_names:
    label = preprocessing.LabelEncoder()
    df[x] = label.fit_transform(df[x])
# print(df['Embarked'])

t = pd.pivot_table(data=df, index='Sex', columns='Survived', aggfunc={'Survived': 'count'})
print(t)

t1 = pd.pivot_table(data=df, index=['Survived', 'Sex'], aggfunc={'Age': 'max', 'Pclass': 'count', 'Parch': 'count'})
print(t1)

df.to_csv("titanic_preprocessing.csv")


#!/usr/bin/env python3

import sys
import mido
import queue
import threading
import os.path
from datetime import datetime, timedelta
from mido import Message, MidiFile, MidiTrack

from pathlib import Path

from .midinames import getLyForMidiNote, getMidiInstrumentName
from .commands import LEAD_CHANNEL, PAD_CHANNEL, DRUM_CHANNEL
from .utils.cmdutils import *

LILYPOND_VERSION = "2.18.2"

_numbers_names = {"0": "Zero", "1":"One", "2":"Two", "3":"Three", "4":"Four",
"5":"Five", "6":"Six", "7":"Seven", "8":"Eight", "9":"Nine"}

class LilypondFile:
    def __init__(self):
        self.title = None
        self.key = None
        self._file_comments = []
        self.header = []
        self.chords = []
        self.melody = []
        self.lyrics = []
        self.make_pdf = True
        self.make_midi = True
        self.clef = None
        self.bpm = None
        self.time_nu = None
        self.time_de = None
        self.poet = None
        self.copyright = None
        self.tagline = None
        self.filename = None

    def is_empty(self):
        empty = True
        for m in self.melody:
            if len(m) == 2 and m[-1] > 0:
                empty = False
        for c in self.chords:
            if len(c) == 2 and c[-1] > 0:
                empty = False
        return empty and not self.lyrics

    def add_time(self, nu, de):
        if not self.melody:
            self.time_nu = int(nu)
            self.time_de = int(de)
        else:
            if self.time_de is None:
                self.add_chunk("\\candenzaOff", channel=LEAD_CHANNEL)
            if nu is None and de is not None:
                nu = de
            if de is None:
                self.add_chunk("\\candenzaOn", channel=LEAD_CHANNEL)
                self.time_nu = self.time_de = None
            else:
                self.time_nu = int(nu)
                self.time_de = int(de)
                self.add_chunk("    \\time {}/{}".format(self.time_nu, self.time_de), channel=LEAD_CHANNEL)

    def add_chunk(self, chunk, channel=None):
        if channel is None:
            self.header.append(chunk)
        elif channel == LEAD_CHANNEL:
            if len(self.melody) == 0 or not isinstance(self.melody[-1], list):
                self.melody.append(["r", 0, chunk])
            else:
                self.melody[-1].append(chunk)
        elif channel == PAD_CHANNEL:
            if len(self.chords) == 0 or not isinstance(self.chords[-1], list):
                self.chords.append(["r", 0, chunk])
            else:
                self.chords[-1].append(chunk)
        else:
            cmt = "%({}) {}".format(channel, chunk)
            self._file_comments.append(cmt)

    def _markup(self, what, text):
        ret = ["   ", what, "= \markup {"]
        if text is not None:
            for w in text.split():
                ret.append(quote_as_needed(w))
        ret.append("}")
        return " ".join(ret)

    def __format__(self, fmt):
        global LILYPOND_VERSION
        result = []
        if LILYPOND_VERSION is not None:
            result.append('\\version "{}"'.format(LILYPOND_VERSION))
            result.append("")
        if self._file_comments:
            result.extend(self._file_comments)
            result.append("")
        result.append("\\header {")
        result.append(self._markup("title", self.title))
        result.append(self._markup("poet", self.poet))
        result.append(self._markup("copyright", self.copyright))
        if self.tagline:
            result.append(self._markup("tagline", self.tagline))
        result.extend(self.header)
        result.append("}")
        result.append("")
        result.append("global = {")
        if self.time_nu and self.time_de:
            result.append("    \\time {}/{}".format(self.time_nu, self.time_de))
        if self.key:
            result.append("    \\key {}".format(self.key))
        if self.bpm:
            if self.time_de is None:
                result.append("    \\tempo {}={}".format(4, self.bpm))
            else:
                result.append("    \\tempo {}={}".format(self.time_de, self.bpm))
        result.append("}")
        result.append("")

        result.append("chordNames = {")
        result.append("    \\global")
        result.append("    \\set chordChanges = ##t")

        for n in self.chords:
            if isinstance(n, str):
                result.append(n)
            else:
                if n[1] > 0:
                    ch = lengthen_notes(n[0], n[1], self.time_nu, self.time_de)
                    if len(ch) == 1:
                        # result.append("    {} % {} 1/4 notes at {} BPM".format(ch[0], n[1], self.bpm))
                        result.append("    {}".format(ch[0]))
                    else:
                        result.append(" ".join(ch))
                        # result.append("            % {} 1/4 notes at {} BPM".format(n[1], self.bpm))
                if len(n) > 2:
                    for c in n[2:]:
                        result.append("            {}".format(c))
        result.append("}")
        result.append("")

        result.append("melody = {")
        result.append("    \\global")
        for n in self.melody:
            if isinstance(n, str):
                result.append(n)
            else:
                if n[1] > 0:
                    ch = lengthen_notes(n[0], n[1], self.time_nu, self.time_de)
                    if len(ch) == 1:
                        result.append("    {}".format(ch[0]))
                        # result.append("    {} % {} 1/4 notes at {} BPM".format(ch[0], n[1], self.bpm))
                    else:
                        result.append(" ".join(ch))
                        # result.append("            % {} 1/4 notes at {} BPM".format(n[1], self.bpm))

                if len(n) > 2:
                    for c in n[2:]:
                        result.append("            {}".format(c))
        result.append('            \\bar "|."')
        result.append("}")
        result.append("")

        if self.lyrics:
            if isinstance(self.lyrics[0], str):
                result.append("words = \\lyricmode {")
                result.append('%    \\set stanza = #"1. "')
                for lne in " ".join(self.lyrics).split("\n"):
                    result.append('    {}'.format(lne))
                result.append("}")
                result.append("")
            else:
                for idx in range(len(self.lyrics)):
                    sufx = number_suffix(idx + 1, len(self.lyrics))
                    result.append("".join(["words", sufx, " = {"]))
                    result.append('%    \\set stanza = #"{}. "'.format(idx+1))
                    for lne in " ".join(self.lyrics[idx]).split("\n"):
                        result.append('    {}'.format(lne))
                    result.append("}")
                    result.append("")

        result.append("\\score {")
        result.append("    <<")
        result.append("        \\new ChordNames \\chordNames")
        result.append("        \\new FretBoards \\chordNames")
        result.append("        \\new Staff \\new Voice \\with {")
        result.append('            \\remove "Note_heads_engraver"')
        result.append('            \\consists "Completion_heads_engraver"')
        result.append('            \\remove "Rest_engraver"')
        result.append('            \\consists "Completion_rest_engraver"')
        result.append("        } \\melody")

        if self.lyrics:
            if isinstance(self.lyrics[0], str):
                result.append("".join(["        \\addlyrics { \\words }"]))
            else:
                for idx in range(len(self.lyrics)):
                    sufx = number_suffix(idx + 1, len(self.lyrics))
                    result.append("".join(["        \\addlyrics { \\words", sufx," }"]))
        result.append("    >>")
        if self.make_pdf:
            result.append("    \\layout { }")
        if self.make_midi:
            result.append("    \\midi { }")
        result.append("}")
        result.append("")
        return "\n".join(result)

class ExportLy:
    filenm = None
    nu = None
    de = None

    def __init__(self, config, filenm, title=None):
        self.filenm = filenm
        self.reconfigure(config)
        self.lilies = []
        self.lily = LilypondFile()
        self.lilies.append(self.lily)
        if title is not None:
            self.lily.title = title
        self.sync_count = 0
        self.lead_time = 0
        self.pad_time = 0

    def reconfigure(self, config):
        pass

    def start(self):
        pass

    def end(self):
        usable = []
        for lil in self.lilies:
            if not lil.is_empty():
                usable.append(lil)
        if len(usable) == 1:
            with self.filenm.open("wt") as out:
                out.write(format(usable[0]))
        else:
            i = 0
            for lily in usable:
                i += 1
                if lily.filename is None:
                    filename = self.filenm.with_name("{}-{}{}".format(self.filenm.stem, i, self.filenm.suffix))
                else:
                    filename = self.filenm.joinpath(lily.filename)
                with filename.open("wt") as out:
                    out.write(format(lily))

    def new_track(self, *, bpm = 120, lily_key = r"c \major", copyright = None, tagline = None, poet=None,
                  time="common", title, filename = None, **kwargs):
        self.lily = LilypondFile()
        self.lilies.append(self.lily)
        self.lily.bpm = bpm
        self.beat = mido.bpm2tempo(int(bpm)) / 1000
        t = parse_timing(time)
        if t is not None:
            t = t.split("/")
            self.lily.time_nu = int(t[0])
            self.lily.time_de = int(t[1])
        self.lily.title = title
        self.lily.poet = poet
        self.lily.key = lily_key
        self.lily.copyright = copyright
        self.lily.tagline = tagline
        self.lily.filename = filename

    def unknown_track(self):
        self.lily = self.lilies[0]

    def player(self):
        pass

    def sync_comment(self, cmt):
        sync = "% [sync@{}] {}".format(self.sync_count, cmt)
        self.sync_count += 1
        self.lily.add_chunk(sync, channel=LEAD_CHANNEL)
        self.lily.add_chunk(sync, channel=PAD_CHANNEL)

    def feed_comment(self, cmt, channel=None):
        cmt = "% {}".format(cmt)
        if channel is None:
            self.lily._file_comments.append(cmt)
        else:
            self.lily.add_chunk(cmt, channel=channel)

    def _add_time(self, time):
        self.lead_time += time
        self.pad_time += time

    def feed_time(self, nu, de):
        if nu is None or de is None:
            self.nu = self.de = None
        elif self.nu != nu or self.de != de:
            self.lily.add_time(nu, de)

    def feed_other(self, cmd, channel=None):
        note = None
        if cmd == "rest":
            pass
        else:
            self.feed_comment(cmd, channel=channel)
            return

    def _feed_instrument(self, cmd, *, channel):
        lilyinstr = getMidiInstrumentName(cmd.program)
        if lilyinstr is None or lilyinstr == "":
            self.feed_comment("Unknown MIDI Instrument numbered {}".format(cmd.program), channel=channel)
        else:
            self.lily.add_chunk('\\set midiInstrument = #"{}"'.format(lilyinstr.lower()), channel=channel)

    def _feed_note(self, what_type, chunk, channel=None):
        if what_type == "note_on":
            if channel == LEAD_CHANNEL:
                if self.lead_time > 0:
                    self.lily.melody.append(["r", self.lead_time / self.beat])
                    self.lead_time = 0
                self.lily.melody.append([chunk, 0])
            elif channel == PAD_CHANNEL:
                if self.pad_time > 0:
                    self.lily.chords.append(["r", self.pad_time / self.beat])
                    self.pad_time = 0
                self.lily.chords.append([chunk, 0])
            else:
                self.feed_comment("[{}] {}".format(what_type, chunk), channel=channel)
        elif what_type == "note_off":
            if channel == LEAD_CHANNEL:
                if len(self.lily.melody) > 0 and self.lily.melody[-1][1] == 0:
                    self.lily.melody[-1][1] = self.lead_time / self.beat
                else:
                    self.lily.melody.append(["r", self.lead_time / self.beat])
                self.lead_time = 0
            elif channel == PAD_CHANNEL:
                if len(self.lily.chords) > 0 and self.lily.chords[-1][1] == 0:
                    self.lily.chords[-1][1] = self.pad_time / self.beat
                else:
                    self.lily.chords.append(["r", self.pad_time / self.beat])
                self.pad_time = 0
            else:
                self.feed_comment("[{}] {}".format(what_type, chunk), channel=channel)
        else:
            self.feed_comment("[{}] {}".format(what_type, chunk), channel=channel)

    def feed_lyric(self, lyric):
        if lyric is None or lyric == "":
            return
        if lyric.startswith("/"):
            self.lily.lyrics.append("\n")
            lyric = lyric[1:]
        elif lyric.startswith("\\"):
            self.lily.lyrics.append("\n\n")
            lyric = lyric[1:]
        if lyric.endswith(" --"):
            self.lily.lyrics.append(quote_as_needed(lyric[:-3]))
            self.lily.lyrics.append("--")
        elif lyric.endswith(" __"):
            self.lily.lyrics.append(quote_as_needed(lyric[:-3]))
            self.lily.lyrics.append("__")
        else:
            self.lily.lyrics.append(quote_as_needed(lyric))

    def feed_midi(self, *what, abbr=None, channel=None, time=None):
        if len(what) == 0:
            return
        if abbr == "panic":
            self.sync_comment("all notes off")
            return
        if channel is None:
            channel = what[0].channel
        if time is None:
            time = what[0].time
        else:
            what[0].time = time
        self._add_time(time)
        if channel == DRUM_CHANNEL:
            # can't manage this here
            return
        what_type = what[0].type

        chunk = None
        if what_type == "note_on":
            chunk = []
            if len(what) > 1:
                chunk.append("<")
            for w in what:
                chunk.append(" ")
                chunk.append(getLyForMidiNote(w.note))
            if len(what) > 1:
                chunk.append(" >")
            chunk = "".join(chunk).strip()
            self._feed_note(what_type, chunk, channel=channel)
        elif what_type == "note_off":
            self._feed_note(what_type, chunk, channel=channel)
        elif what_type == "program_change":
            self._feed_instrument(what[0], channel=channel)
        else:
            self.feed_comment("{!r}".format(what), channel=channel)

def lengthen_notes(note, r = 1, nu = 4, de = 4):
    ch = []
    nude = 1
    if nu is not None and de is not None:
        nude = nu / de
    r = r / 4
    if nude >= 1:
        while r > 0.8:
            if len(ch) == 0:
                ch.append("    {}1".format(note))
            else:
                ch.append(" ~ {}".format(note))
            r -= 1.0
    if nude >= 0.5:
        while r > 0.4:
            if len(ch) == 0:
                ch.append("    {}2".format(note))
            else:
                ch.append(" ~ {}2".format(note))
            r -= 0.5
    while r > 0.2:
        if len(ch) == 0:
            ch.append("    {}4".format(note))
        else:
            ch.append(" ~ {}4".format(note))
        r -= 0.25
    if r > 0.1:
        if len(ch) == 0:
            ch.append("    {}8".format(note))
        else:
            ch.append(" ~ {}8".format(note))
        r -= 0.125
    if r > 0:
        if len(ch) == 0:
            ch.append("    {}16".format(note))
        else:
            ch.append(" ~ {}16".format(note))
    return ch

def number_suffix(idx, totl):
    global _numbers_names
    if totl < 26:
        return chr(ord('A') + idx)
    ret = []
    for c in str(totl):
        if c in _numbers_names:
            ret.append(_numbers_names[c])
        else:
            ret.append(c)
    return "".join(ret)

def quote_as_needed(text):
    if text.isalpha():
        return text
    return '"{}"'.format(text.replace("\\","\\\\").replace('"', r'\"'))


import http.client
import json
from config.config import Config

test_project_id = 'test'
port = Config.get_config('port')

def get_token():
    conn = http.client.HTTPConnection("localhost", port)
    payload = ''
    headers = {
      'Authorization': 'Basic YWRtaW46cGFzc3dvcmQ='
    }
    conn.request("POST", "/api/access_token", payload, headers)
    res = conn.getresponse()
    assert res.status == 201, res.status
    data = res.read()
    access_token = json.loads(data.decode("utf-8")).get('access_token', None)
    assert access_token
    print(data.decode("utf-8"))
    return access_token

def post_test_result(project_id, auth_token: str, body: dict):
    conn = http.client.HTTPConnection("localhost", port)
    payload = json.dumps(body)
    headers = {
        'Authorization': f'Bearer {auth_token}',
        'Content-Type': 'application/json'
    }
    conn.request("POST", f"/api/projects/{project_id}/test_results", payload, headers)
    res = conn.getresponse()
    data = res.read()
    print(data)
    updated = json.loads(data.decode("utf-8")).get('updated', 0)
    return updated

def get_test_results(project_id):
    test_results = [
        {
            "case_id": "feature1.001",
            "project": project_id,
            "testrun_id": "2019-11-20 10:27:26",
            "case_result": "error",
            "suite_name": "sanity",
            "env": "dev0",
            "stdout": "a b c d",
            "traceback": "t1 t2 t3",
            "duration": 1.2
        },
        {
            "case_id": "feature1.002",
            "project": project_id,
            "testrun_id": "2019-11-20 10:27:26",
            "case_result": "success",
            "suite_name": "sanity",
            "env": "dev0",
            "stdout": "a b c d",
            "traceback": "t1 t2 t3",
            "duration": 2.7
        },
        {
            "case_id": "feature2.003",
            "project": project_id,
            "testrun_id": "2019-11-20 10:27:26",
            "case_result": "failure",
            "suite_name": "sanity",
            "env": "dev0",
            "stdout": "a b",
            "traceback": "t1 t2 t3",
            "duration": 3.9
        },
        {
            "case_id": "feature1.001",
            "project": project_id,
            "testrun_id": "2018-11-20 10:27:26",
            "case_result": "success",
            "suite_name": "sanity",
            "env": "dev1",
            "stdout": "a b c",
            "traceback": "t1 t2 t3",
            "duration": 1.3
        },
        {
            "case_id": "feature1.002",
            "project": project_id,
            "testrun_id": "2018-11-20 10:27:26",
            "case_result": "success",
            "suite_name": "sanity",
            "env": "dev1",
            "stdout": "d e",
            "traceback": "t1",
            "duration": 1.9
        },
        {
            "case_id": "feature2.003",
            "project": project_id,
            "testrun_id": "2018-11-20 10:27:26",
            "case_result": "failure",
            "suite_name": "sanity",
            "env": "dev1",
            "stdout": "f g",
            "traceback": "t1 t2",
            "duration": 2.1
        }
    ]
    return test_results

if __name__ == '__main__':
    auth_token = get_token()
    test_results = get_test_results(test_project_id)
    updated = 0
    for res in test_results:
        post_test_result(test_project_id, auth_token, res)


# -*- coding: utf-8 -*-
"""
Created on Tue Nov 14 11:36:37 2023

@author: Digi_2
"""

import argparse
import joblib

import os
import subprocess
import sys

import pandas as pd
import numpy
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import accuracy_score
from sklearn.metrics import f1_score
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import itertools
from matplotlib import pyplot as plt
import pickle
import seaborn as sns
current_directory=os.path.dirname(os.path.abspath(__file__))
temp_folder=os.path.join(current_directory,'..','temp')

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--te',type=str,help='Path for the test file')
    parser.add_argument('--tr',type=str,help='Path for the train file')
    parser.add_argument('--o',type=str,help='Path for the output file')
    parser.add_argument('--f',type=str,help='Path for the features')
    parser.add_argument('--ne',type=str,help='Path for the number of estimators')
    parser.add_argument('--c',type=str,help='Path for the criterion')
    parser.add_argument('--md',type=str,help='Path for the maximum depth of the threes')
    parser.add_argument('--ms',type=str,help='Path for the minimum number of simples required to Split the internal node')
    parser.add_argument('--mns',type=str,help='Path for the minimum number of samples required to be a leaf node')
    parser.add_argument('--mwf',type=str,help='Path for the minimum weight fraction of the total sum of weights')
    parser.add_argument('--mf',type=str,help='Path for the maximum number of features')
    parser.add_argument('--bt',type=str,help='Path for the bootstrap')
    parser.add_argument('--s',type=str,help='Path for the scoring')
    parser.add_argument('--nj',type=str,help='Path for the number of jobs')

    args=parser.parse_args()

    test_file=args.te
    print("Test file located in " + test_file)
    train_file=args.tr
    print("Train file located in " + train_file)
    output_directory=args.o
    print("Output directory is " + output_directory)
    features2include=args.f
    print("Features to include = " + features2include)
    n_estimators=args.ne
    print("Number of estimators = " + n_estimators)
    criterion=args.c
    print("Criterion chosen = " + criterion)
    max_depth=args.md
    print("Maximum depth of the tree = " + max_depth)
    min_samples_split=args.ms
    print("Minimum number of samples required to split an internal node = " + min_samples_split)
    min_samples_leaf=args.mns
    print("Minimum number of samples required to be at a leaf node = " + min_samples_leaf)
    min_weight_fraction_leaf=args.mwf
    print("Minimum weighted fraction of the sum total of weights (of all the input samples) required to be at a leaf node = " + min_weight_fraction_leaf)
    max_features=args.mf
    print("Number of features to consider = " + max_features)
    bootstrap=args.bt
    print("Bootstrap = " + bootstrap)
    scoring=args.s
    print("Scoring = " + scoring)
    n_jobs=args.nj
    print("Number of jobs = " + n_jobs)

    labels2include=['Classification']

    #Store in a Pandas dataframe the content of the file
    pcd_training=pd.read_csv(train_file,delimiter=' ')
    #Store in a Pandas dataframe the content of the file
    pcd_testing=pd.read_csv(test_file,delimiter=' ')
    #Clean the dataframe, and drop all the line that contains a NaN (Not a Number) value.
    pcd_training.dropna(inplace=True)
    pcd_testing.dropna(inplace=True)
    #Create training and testing
    labels_training=pcd_training[labels2include]

    with open(output_directory + "\\features.txt", "r") as file:
        features2include = [line.strip().split(',') for line in file]
    features=pcd_training[features2include[0]]

    #features_train = MinMaxScaler().fit_transform(features)
    features_training=features
    labels_testing=pcd_testing[labels2include]
    features=pcd_testing[features2include[0]]
    #features_evaluation = MinMaxScaler().fit_transform(features)
    features_testing=features
    X_test=features_testing
    y_test=labels_testing.to_numpy()

    X_train=features_training
    y_train=labels_training.to_numpy()

    #*******************BEST MODEL***************************************
    best_conf = {'ne' : 0, 'md' : 0, 'mf': 0, 'bt':0}
    best_f1 = 0

    n_estimators = int(n_estimators)
    max_depth = int(max_depth)
    min_samples_split = int(min_samples_split)
    min_samples_leaf = int(min_samples_leaf)
    min_weight_fraction_leaf=int(min_weight_fraction_leaf)
    n_jobs=int(n_jobs)

    for ne, md, mf, bt, c, ms, mns, mwf in list(itertools.product([n_estimators], [max_depth], [max_features], [bootstrap], [criterion], [min_samples_split], [min_samples_leaf], [min_weight_fraction_leaf])):
        bt = bt.lower() == 'true'
        if not bt:
            obb = False
        else:
            obb = True

        rf_classifier = RandomForestClassifier(
            n_estimators=ne,
            max_depth=md,
            max_features=mf,
            random_state=42,
            oob_score=obb,
            bootstrap=bt,
            criterion=c,
            min_samples_split=ms,
            min_samples_leaf=mns,
            min_weight_fraction_leaf=mwf,
            # scoring=s,
            n_jobs=-1
            )
    rf_classifier.fit(X_train,y_train.ravel())
    test_rf_predictions = rf_classifier.predict(X_test)
    # Compute metrics and update best model
    acc = accuracy_score(y_test.ravel(), test_rf_predictions)
    f1 = f1_score(y_test.ravel(), test_rf_predictions, average='weighted')
    # Update best configuration
    if f1 > best_f1:
       best_conf['ne'] = ne
       best_conf['md'] = md
       best_conf['mf'] = mf
       best_conf['bt'] = bt
       best_f1 = f1
    if obb==True:
      print('\tne: {}, md: {}, mf: {},  bt: {}- acc: {} f1: {} oob_score: {}'.format(ne, md,mf,bt, acc, f1, rf_classifier.oob_score_))
    else:
      print('\tne: {}, md: {}, mf: {},  bt: {}- acc: {} f1: {}'.format(ne, md,mf,bt, acc, f1))

    print('Best parameters: ne: {}, md: {}, mf: {},  bt: {}'.format(best_conf['ne'], best_conf['md'], best_conf['mf'],best_conf['bt']))

    #*******************IMPORTANCE***************************************
    print('Parameters used for the final Random Forest classificator: ne: {}, md: {}, bt: {}'.format(best_conf['ne'], best_conf['md'],best_conf['bt']))
    if best_conf['bt']==False:
      rf_classifier = RandomForestClassifier(n_estimators = best_conf['ne'],max_depth= best_conf['md'],bootstrap=best_conf['bt'],n_jobs=n_jobs,random_state=42)
    else:
      rf_classifier = RandomForestClassifier(n_estimators = best_conf['ne'],max_depth= best_conf['md'],bootstrap=best_conf['bt'],n_jobs=n_jobs,random_state=42,oob_score=True)
    rf_classifier.fit(X_train,y_train.ravel())

    fi = pd.DataFrame({'feature': list(X_train),'importance': rf_classifier.feature_importances_}).sort_values('importance', ascending = True)
    fig, ax = plt.subplots(figsize=(15,20))
    ax.barh(fi['feature'], fi['importance'])
    fi.to_csv(output_directory+'/Feature_importance.csv', encoding = 'utf-8-sig')
    plt.savefig(output_directory+'/Feature_importance.jpg')

    #*******************CONFUSION MATRIX***************************************

    fig, ax = plt.subplots(figsize=(5,5))
    test_rf_predictions = rf_classifier.predict(X_test)
    sns.heatmap(confusion_matrix(y_test.ravel(),test_rf_predictions), annot=True,cmap='Blues',fmt='d')
    plt.savefig(output_directory+'/Confusion_matrix.jpg')
    #*******************MATRIZ CLASSIFICATION***************************************
    print(classification_report(y_test, test_rf_predictions,digits=3))

    #ESCRITURA MODELO FINAL
    pcd_testing['Predictions']=test_rf_predictions
    pcd_testing[['X','Y','Z','Predictions']].to_csv(os.path.join(output_directory, 'predictions.txt'), index=None)
    pickle.dump(rf_classifier, open(output_directory+"/random_forest.pkl", 'wb'))

if __name__=='__main__':
	main()


from configparser import ConfigParser
import os

# The path neeeds to be changed accordingly
# based on the location of the config.ini
# Config.ini is the editable cofig file for the database

PATH = os.getcwd() + '/DataBase/config.ini'

def get_config(filename= PATH,section='mysql'):
    parser = ConfigParser()
    parser.read(filename)
    db_conf = {}
    if parser.has_section(section):
        key_val_list = parser.items(section)
        db_conf = dict(key_val_list)

    return db_conf


import re
import six
import string
import yaml
import os

from contextlib import closing

from pywb.utils.timeutils import timestamp_to_http_date
from pywb.utils.wbexception import BadRequestException

LINK_SPLIT = re.compile(',\s*(?=[<])')
LINK_SEG_SPLIT = re.compile(';\s*')
LINK_URL = re.compile('<(.*)>')
LINK_PROP = re.compile('([\w]+)="([^"]+)')

BUFF_SIZE = 16384

#=============================================================================
class MementoException(BadRequestException):
    pass

#=============================================================================
class MementoUtils(object):
    @staticmethod
    def parse_links(link_header, def_name='timemap'):
        links = LINK_SPLIT.split(link_header)
        results = {}
        mementos = []

        for link in links:
            props = LINK_SEG_SPLIT.split(link)
            m = LINK_URL.match(props[0])
            if not m:
                raise MementoException('Invalid Link Url: ' + props[0])

            result = dict(url=m.group(1))
            key = ''
            is_mem = False

            for prop in props[1:]:
                m = LINK_PROP.match(prop)
                if not m:
                    raise MementoException('Invalid prop ' + prop)

                name = m.group(1)
                value = m.group(2)

                if name == 'rel':
                    if 'memento' in value:
                        is_mem = True
                        result[name] = value
                    elif value == 'self':
                        key = def_name
                    else:
                        key = value
                else:
                    result[name] = value

            if key:
                results[key] = result
            elif is_mem:
                mementos.append(result)

        results['mementos'] = mementos
        return results

    @staticmethod
    def make_timemap_memento_link(cdx, datetime=None, rel='memento', end=',\n'):
        url = cdx.get('load_url')
        if not url:
            url = 'file://{0}:{1}:{2}'.format(cdx.get('filename'), cdx.get('offset'), cdx.get('length'))

        memento = '<{0}>; rel="{1}"; datetime="{2}"; src="{3}"' + end

        if not datetime:
            datetime = timestamp_to_http_date(cdx['timestamp'])

        return memento.format(url, rel, datetime, cdx.get('source', ''))

    @staticmethod
    def make_timemap(cdx_iter):
        # get first memento as it'll be used for 'from' field
        try:
            first_cdx = six.next(cdx_iter)
            from_date = timestamp_to_http_date(first_cdx['timestamp'])
        except StopIteration:
            first_cdx = None
            return

        # first memento link
        yield MementoUtils.make_timemap_memento_link(first_cdx, datetime=from_date)

        prev_cdx = None

        for cdx in cdx_iter:
            if prev_cdx:
                yield MementoUtils.make_timemap_memento_link(prev_cdx)

            prev_cdx = cdx

        # last memento link, if any
        if prev_cdx:
            yield MementoUtils.make_timemap_memento_link(prev_cdx, end='\n')

    @staticmethod
    def make_link(url, type):
        return '<{0}>; rel="{1}"'.format(url, type)

#=============================================================================
class ParamFormatter(string.Formatter):
    def __init__(self, params, name='', prefix='param.'):
        self.params = params
        self.prefix = prefix
        self.name = name

    def get_value(self, key, args, kwargs):
        # First, try the named param 'param.{name}.{key}'
        if self.name:
            named_key = self.prefix + self.name + '.' + key
            value = self.params.get(named_key)
            if value is not None:
                return value

        # Then, try 'param.{key}'
        named_key = self.prefix + key
        value = self.params.get(named_key)
        if value is not None:
            return value

        # default to just '{key}'
        value = kwargs.get(key, '')
        return value

#=============================================================================
def res_template(template, params, **extra_params):
    formatter = params.get('_formatter')
    if not formatter:
        formatter = ParamFormatter(params)
    res = formatter.format(template, url=params.get('url', ''), **extra_params)

    return res

#=============================================================================
def StreamIter(stream, header1=None, header2=None, size=BUFF_SIZE):
    with closing(stream):
        if header1:
            yield header1

        if header2:
            yield header2

        while True:
            buff = stream.read(size)
            if not buff:
                break
            yield buff

#=============================================================================
def chunk_encode_iter(orig_iter):
    for chunk in orig_iter:
        if not len(chunk):
            continue
        chunk_len = b'%X\r\n' % len(chunk)
        yield chunk_len
        yield chunk
        yield b'\r\n'

    yield b'0\r\n\r\n'

#=============================================================================
def load_config(main_env_var, main_default_file='',
                overlay_env_var='', overlay_file=''):

    configfile = os.environ.get(main_env_var, main_default_file)

    if configfile:
        # Load config
        with open(configfile, 'rb') as fh:
            config = yaml.load(fh)

    else:
        config = {}

    overlay_configfile = os.environ.get(overlay_env_var, overlay_file)

    if overlay_configfile:
        with open(overlay_configfile, 'rb') as fh:
            config.update(yaml.load(fh))

    return config


""" Implement Queue with Max API """

from collections import namedtuple

Element = namedtuple('Element', ['value', 'prev_max'])
class MaxQueue:

    def __init__(self) -> None:
        self.elements = []
        self.max_seen = float('-inf')

    def enqueue(self, x) -> None:
        self.elements.append(Element(x, self.max_seen))
        self.max_seen = max(self.max_seen, x)

    def dequeue(self) -> int:
        if not self.elements:
            return -1
        removed = self.elements.pop()
        self.max_seen = removed.prev_max
        return removed.value

    def max(self) -> int:
        return self.max_seen

queue = MaxQueue()
queue.enqueue(1)
queue.enqueue(2)
print(queue.max())
print(queue.dequeue())
print(queue.max())


# -*- coding: utf-8 -*-
'''
Created March 2019

@author: Ed

Squaring decision: manual or auto?
'''

import kivy
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import sys, os
from asmcnc.skavaUI import popup_info
from datetime import datetime

Builder.load_string("""

<StopOrResumeDecisionScreen>:

    pause_reason_label:pause_reason_label
    pause_description_label:pause_description_label

    canvas:
        Color:
            rgba: hex('#E5E5E5FF')
        Rectangle:
            size: self.size
            pos: self.pos

    BoxLayout:
        spacing: 0
        padding: 20
        orientation: 'vertical'

        BoxLayout:
            orientation: 'vertical'
            spacing: 00
            padding: (0,0,0,10)
            size_hint_y: 5

            Label:
                id: pause_reason_label
                size_hint_y: 0.6
                markup: True
                font_size: '30px'
                valign: 'center'
                halign: 'center'
                size:self.texture_size
                text_size: self.size
                color: hex('#333333ff')

            Label:
                id: pause_description_label
                size_hint_y: 2.4
                markup: True
                font_size: '18px'
                valign: 'center'
                halign: 'center'
                size:self.texture_size
                text_size: self.size
                color: hex('#333333ff')

        BoxLayout:
            orientation: 'horizontal'
            spacing: 0
            size_hint_y: 3

            Button:
                size_hint_x: 1
                background_color: hex('#FFFFFF00')
                on_press: root.cancel_job()
                BoxLayout:
                    size: self.parent.size
                    pos: self.parent.pos
                    Image:
                        source: "./asmcnc/skavaUI/img/cancel_from_pause.png"
                        size: self.parent.width, self.parent.height
                        allow_stretch: True

            Button:
                size_hint_x: 0.3
                background_color: hex('#FFFFFF00')
                on_press: root.popup_help()
                BoxLayout:
                    size: self.parent.size
                    pos: self.parent.pos
                    Image:
                        source: "./asmcnc/skavaUI/img/help_btn_yellow_round.png"
                        size: self.parent.width, self.parent.height
                        allow_stretch: True

            Button:
                size_hint_x: 1
                background_color: hex('#FFFFFF00')
                on_press: root.resume_job()
                BoxLayout:
                    size: self.parent.size
                    pos: self.parent.pos
                    Image:
                        source: "./asmcnc/skavaUI/img/resume_from_pause.png"
                        size: self.parent.width, self.parent.height
                        allow_stretch: True

""")

def log(message):
    timestamp = datetime.now()
    print (timestamp.strftime('%H:%M:%S.%f' )[:12] + ' ' + str(message))

class StopOrResumeDecisionScreen(Screen):

    reason_for_pause = None
    return_screen = 'lobby'

    # GOS TO: https://www.yetitool.com/SUPPORT/KNOWLEDGE-BASE/routing-tools-sc0-sc1-troubleshooting-overload-during-operation
    qr_spindle_overload = "./asmcnc/skavaUI/img/qr_spindle_overload.png"

    # GOS TO: https://www.yetitool.com/SUPPORT/KNOWLEDGE-BASE/smartbench-extra-features-precision-pro-yetipilot-error-screens
    qr_yetipilot_low_feed = "./asmcnc/skavaUI/img/qr_low_feed_rate.png"

    # GOS TO: https://www.yetitool.com/SUPPORT/KNOWLEDGE-BASE/smartbench-extra-features-precision-pro-yetipilot-error-screen-spindle-data-error
    qr_yetipilot_no_data = "./asmcnc/skavaUI/img/qr_no_spindle_data.png"

    # GOS TO: https://www.yetitool.com/SUPPORT/KNOWLEDGE-BASE/smartbench-extra-features-precision-pro-yetipilot-error-screen-spindle-motor-health-check
    qr_health_check = "./asmcnc/skavaUI/img/qr_health_check_failed.png"

    # default
    qr_source = qr_spindle_overload

    def __init__(self, **kwargs):

        super(StopOrResumeDecisionScreen, self).__init__(**kwargs)
        self.sm=kwargs['screen_manager']
        self.m=kwargs['machine']
        self.jd = kwargs['job']
        self.db = kwargs['database']
        self.l=kwargs['localization']

    def popup_help(self):

        info = (
            self.l.get_bold('Cancel')  + '[b]' +  " (X)" +'[/b]' + \
            "\n" + \
            self.l.get_str("Pressing cancel will cancel the job.") + \
            "\n\n" + \
            self.l.get_bold('Resume') + '[b]' +  " (>)" +'[/b]' + \
            "\n" + \
            self.l.get_str("Pressing resume will continue the job from the point at which it was paused.")
        )

        if self.reason_for_pause == 'job_pause':
            popup_info.PopupInfo(self.sm, self.l, 500, info)

        else:
            info += (
                "\n\n" + \
                self.l.get_bold('Scan the QR code to learn more about this error.') + \
                "\n" + \
                self.l.get_bold("Or visit <URL>").replace('<URL>', 'www.yetitool.com/support > Knowledge Base')
            )

            popup_info.PopupQRInfo(self.sm, self.l, 500, info, self.qr_source)

    def on_pre_enter(self):
        self.update_strings()

    def update_strings(self):

        # Update go screen button in case this screen was called from outside go screen (e.g. spindle overload)
        try: self.sm.get_screen('go').start_or_pause_button_image.source = "./asmcnc/skavaUI/img/resume.png"
        except: pass

        if self.reason_for_pause == 'job_pause':
            self.pause_reason_label.text = self.l.get_str("SmartBench is paused.")
            self.pause_description_label.text = self.l.get_str("You may resume, or cancel the job at any time.")

        if self.reason_for_pause == 'spindle_overload':

            self.pause_reason_label.text = self.l.get_str("Spindle motor was overloaded!").replace(self.l.get_str('overloaded'), self.l.get_bold('overloaded'))
            self.pause_description_label.text = (

                self.l.get_str('SmartBench has automatically stopped the job because it detected the spindle was starting to overload.') + \
                "\n" + \
                self.l.get_str(
                    'You may resume, but we recommend you allow the spindle to cool off first.'
                    ).replace(self.l.get_str('You may resume'),self.l.get_bold('You may resume')) + \
                "\n" + \
                self.l.get_str('Try adjusting the speeds and feeds to reduce the load on the spindle, or adjust the job to reduce chip loading.') + " " + \
                self.l.get_str('Check extraction, air intake, exhaust, worn brushes, work-holding, blunt cutters or anything else which may strain the spindle.')
                )

            self.qr_source = self.qr_spindle_overload

        if self.reason_for_pause == 'yetipilot_low_feed':

            self.pause_reason_label.text = self.l.get_str("Feed rate too slow!")
            self.pause_description_label.text = (

                self.l.get_str('YetiPilot has tried to reduce the feed rate to less than 10% of the feed rate in the job file.') + \
                "\n\n" + \
                self.l.get_str("This may be because the chosen feed rate in the job file was set too high, or because of a problem with the cut which means the Spindle motor's target power cannot be reached.") + \
                " " + \
                self.l.get_str('Press "?" for more information.') + "\n\n" + \
                self.l.get_bold('We recommend that you cancel the job and correct the issue.') + " " + \
                self.l.get_str('Or, you may resume the job with YetiPilot initially disabled.').replace(self.l.get_str('Or, you may resume'),self.l.get_bold('Or, you may resume')) + " " + \
                self.l.get_str('If you choose to resume, SmartBench may struggle.')
                )

            self.qr_source = self.qr_yetipilot_low_feed

        if self.reason_for_pause == 'yetipilot_spindle_data_loss':

            self.pause_reason_label.text = self.l.get_str("Can't read spindle data!")
            self.pause_description_label.text = (

                self.l.get_str('Cannot read the data from the SC2 Spindle motor, which is needed to measure the load.') + \
                "\n\n" + \
                self.l.get_str("Please check that you are using your SC2 Spindle motor, and check that your data cable is connected.") + \
                " " + \
                self.l.get_str('Press "?" for more information.') + "\n\n" + \
                self.l.get_str('You may resume the job with YetiPilot disabled, or cancel the job altogether.').replace(self.l.get_str('You may resume'),self.l.get_bold('You may resume'))
                )

            self.qr_source = self.qr_yetipilot_no_data

        if self.reason_for_pause == 'spindle_health_check_failed':

            self.pause_reason_label.text = self.l.get_str("Spindle motor health check failed!")
            self.pause_description_label.text = (

                self.l.get_str('Spindle motor load is too high.') + "\n\n" + \
                self.l.get_str("Please check that your SC2 Spindle motor is clamped correctly.") + "\n\n" + \
                self.l.get_str('If you continue without resolving the issue, it will result in accelerated wear of the Spindle motor.') + "\n\n" + \
                self.l.get_str('You may resume the job with YetiPilot disabled, or cancel the job altogether.').replace(self.l.get_str('You may resume'),self.l.get_bold('You may resume'))
                )

            self.qr_source = self.qr_health_check

        self.update_font_size(self.pause_description_label)

    def cancel_job(self):
        popup_info.PopupConfirmJobCancel(self.sm, self.l)

    def confirm_job_cancel(self):
        self.m.stop_from_soft_stop_cancel()
        self.m.s.is_ready_to_assess_spindle_for_shutdown = True # allow spindle overload assessment to resume

        self.sm.get_screen('job_incomplete').prep_this_screen('cancelled', event_number=False)
        self.sm.current = 'job_incomplete'

    def resume_job(self):

        if self.reason_for_pause == 'yetipilot_low_feed':
            self.sm.get_screen('go').yp_widget.disable_yeti_pilot()

        self.sm.current = self.return_screen

    def update_font_size(self, value):
        text_length = self.l.get_text_length(value.text)

        if text_length > 700:
            value.font_size = 16
        elif text_length > 500:
            value.font_size = 17
        else:
            value.font_size = 18


"""Find euclidean lengths of skeleton segments."""
import os
import cv2
import numpy as np
from plantcv.plantcv import params
from plantcv.plantcv import outputs
from plantcv.plantcv import fatal_error
from plantcv.plantcv import color_palette
from plantcv.plantcv._debug import _debug
from plantcv.plantcv._helpers import _cv2_findcontours
from plantcv.plantcv.morphology import find_tips
from scipy.spatial.distance import euclidean

def segment_euclidean_length(segmented_img, objects, label=None):
    """Use segmented skeleton image to gather euclidean length measurements per segment.

    Inputs:
    segmented_img = Segmented image to plot lengths on
    objects       = List of contours
    label         = Optional label parameter, modifies the variable name of
                    observations recorded (default = pcv.params.sample_label).

    Returns:
    labeled_img      = Segmented debugging image with lengths labeled

    :param segmented_img: numpy.ndarray
    :param objects: list
    :param label: str
    :return labeled_img: numpy.ndarray
    """
    # Set lable to params.sample_label if None
    if label is None:
        label = params.sample_label

    x_list = []
    y_list = []
    segment_lengths = []
    # Create a color scale, use a previously stored scale if available
    rand_color = color_palette(num=len(objects), saved=True)

    labeled_img = segmented_img.copy()
    # Store debug
    debug = params.debug
    params.debug = None

    for i, cnt in enumerate(objects):
        # Store coordinates for labels
        x_list.append(objects[i][0][0][0])
        y_list.append(objects[i][0][0][1])

        # Draw segments one by one to group segment tips together
        finding_tips_img = np.zeros(segmented_img.shape[:2], np.uint8)
        cv2.drawContours(finding_tips_img, objects, i, (255, 255, 255), 1, lineType=8)
        segment_tips = find_tips(finding_tips_img)
        tip_objects, _ = _cv2_findcontours(bin_img=segment_tips)
        points = []
        if not len(tip_objects) == 2:
            fatal_error("Too many tips found per segment, try pruning again")
        for t in tip_objects:
            # Gather pairs of coordinates
            x, y = t.ravel()
            coord = (x, y)
            points.append(coord)

        # Draw euclidean distance lines
        cv2.line(labeled_img, points[0], points[1], rand_color[i], 1)

        # Calculate euclidean distance between tips of each contour
        segment_lengths.append(float(euclidean(points[0], points[1])))

    segment_ids = []
    # Reset debug mode
    params.debug = debug

    # Put labels of length
    for c, value in enumerate(segment_lengths):
        text = "{:.2f}".format(value)
        w = x_list[c]
        h = y_list[c]
        cv2.putText(img=labeled_img, text=text, org=(w, h), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                    fontScale=params.text_size, color=(150, 150, 150), thickness=params.text_thickness)
        segment_ids.append(c)

    outputs.add_observation(sample=label, variable='segment_eu_length', trait='segment euclidean length',
                            method='plantcv.plantcv.morphology.segment_euclidean_length', scale='pixels', datatype=list,
                            value=segment_lengths, label=segment_ids)

    _debug(visual=labeled_img, filename=os.path.join(params.debug_outdir, f"{params.device}_segment_eu_lengths.png"))

    return labeled_img


import discord
from discord.ext import commands
from discord import app_commands
from src.util.utils import Utils
from src.util.logger import Logger
from src.helper.config import Config
from src.steam.checker import Checker
from src.helper.datetime import DateTime
from src.handler.xp_handler import XpHandler
from src.manager.xp_manager import XpManager
from src.manager.guild_manager import GuildManager
from src.helper.trackeduser_class import TrackedUser
from src.manager.admin_mode_manager import AdminModeManager

class AddUser(commands.Cog):
    def __init__(self, bot: commands.Bot):
        self.bot = bot
        self.config = Config()
        self.checker = Checker()
        self.database = XpManager()
        self.xp_handler = XpHandler()
        self.logger = Logger(self.bot)
        self.datetime_helper = DateTime()
        self.guild_manager = GuildManager()
        self.admin_mode_manager = AdminModeManager()

    # Add user command
    @app_commands.command(name="add_user", description="Add a user to the xp tracker database.")
    @app_commands.describe(
        id="The steamid64/vanity/profile url of the user you want to start to track.",
        hidden="If the command should be hidden from other users or not."
    )
    async def add_track_user(self, interaction: discord.Interaction, id: str, hidden: bool = True):
        await interaction.response.defer(ephemeral=hidden)

        # Clean the username
        username = Utils.clean_discord_username(f"{interaction.user.name}#{interaction.user.discriminator}")

        # Send the loading message
        added_message = await interaction.followup.send(f"{self.config.loading_green_emoji_id} Trying to add id `{id}` to the tracker database.", ephemeral=hidden)

        admin_mode = self.admin_mode_manager.get_admin_mode(interaction.guild_id)

        # Check if the admin mode is set
        if admin_mode is None:
            await added_message.edit(content=f"{self.config.red_cross_emoji_id} Couldn't get the admin mode status for this server. Tell the owner to set it.")
            await self.logger.discord_log(f"✅ {username} tried to add an id to the tracker database, but couldn't get the admin mode status.")
            return

        # Check if the user has permissions to use this command
        if not interaction.user.guild_permissions.administrator and admin_mode:
            await added_message.edit(content=f"{self.config.red_cross_emoji_id} The admin mode is enabled. Only administrators can use this command.")
            await self.logger.discord_log(f"✅ {username} tried to add an id to the tracker database, but the admin mode is enabled.")
            return

        # Check if the id is valid and get the data
        success, steamid64, nickname, avatar = self.checker.get_persona(id)

        # If the id is not valid
        if not success:
            await added_message.edit(content=f"The id `{id}` is not a valid ID.")
            await self.logger.discord_log(f"✅ {username} tried to add an invalid id to the tracker database.")
            return

        # Check if the user is already being tracked
        if self.database.get_user_by_steam_id(steamid64) is not None:
            await added_message.edit(content=f"The id `{id}` is already being tracked. If you want to update the user's guild, whoever added it needs to use the `/change_user_guild` command.")
            await self.logger.discord_log(f"✅ {username} tried to add an already tracked id to the tracker database.")
            return

        # Check if the guild has a tracker channel set
        if not self.guild_manager.guild_exists(interaction.guild_id):
            await added_message.edit(content=f"{self.config.red_cross_emoji_id} This server doesn't have a tracker channel set. Tell an admin to set it.")
            await self.logger.discord_log(f"✅ {username} tried to add an id to the tracker database, but the guild doesn't have a tracker channel set.")
            return

        # Get the level and XP
        try:
            level, xp, remaining_xp, percentage = await self.xp_handler.get_user_level_and_xp(steamid64)
        except Exception as e:
            await added_message.edit(content=f"{self.config.red_cross_emoji_id} Couldn't get level and XP for `{id}`, couldn't add him to the tracker database. Error: {e}")
            await self.logger.discord_log(f"✅ {username} tried to add an id to the tracker database, but couldn't get the level and XP.")
            return

        # Add the user to the database
        added_user = TrackedUser(steamid64, interaction.user.id, interaction.guild.id, level, xp, 0, 0)
        try:
            self.database.add_user(added_user)
        except Exception as e:
            await added_message.edit(content=f"Couldn't add id {id} to the tracker database. Error: {e}")
            await self.logger.discord_log(f"✅ {username} tried to add an id to the tracker database, but couldn't add him to the database.")
            return

        # Send the embed
        embed = discord.Embed(title=f"{self.config.green_tick_emoji_id} Successfully added `{nickname}`", url=f"https://steamcommunity.com/profiles/{steamid64}", color=0x08dbf8c)

        embed.set_author(name=f"Tracker", icon_url=self.config.csgo_tracker_logo, url="https://kwayservices.top")
        embed.set_thumbnail(url=avatar)
        embed.add_field(name="SteamID64", value=f"`{steamid64}`", inline=True)
        embed.add_field(name="Current level", value=f"`{level}`", inline=True)
        embed.add_field(name="Current XP", value=f"`{xp}`", inline=True)

        embed.set_footer(text=f"CSGO Tracker • Requested by {username}", icon_url=self.config.csgo_tracker_logo)
        embed.set_image(url=self.config.rainbow_line_gif)
        embed.timestamp = self.datetime_helper.get_current_timestamp()

        # Edit the message with the embed and log it
        await added_message.edit(content=f"{self.config.green_tick_emoji_id} Request completed.", embed=embed)
        await self.logger.discord_log(f"✅ {username} added `{nickname}` to the tracker database.")

    # In case of an error
    @add_track_user.error
    async def add_track_user_error(self, interaction: discord.Interaction, error: app_commands.AppCommandError):
        if isinstance(error, app_commands.errors.MissingPermissions):
            await interaction.response.send_message(f"{self.config.red_cross_emoji_id} You don't have permissions to use this command.", ephemeral=True)
        else:
            await interaction.response.send_message(f"Error: {error}", ephemeral=True)

async def setup(bot: commands.Bot):
    # Add the cog and log it
    await bot.add_cog(AddUser(bot))
    return Logger().log("INFO", "Add user command loaded!")


# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

import json
import base64

data = {}
f_in = open('/home/arunken/flo.jpg', mode='rb')
imFile = f_in.read()
data['img'] = base64.encodebytes(imFile).decode("utf-8")

jsonData = json.dumps(data)
print(jsonData)


import numpy as np
import matplotlib.pyplot as knn_plt
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from data.DataAnalises import X_non_treated, X_treated, y_non_treated, y_treated
from utils.nonTreatedDataUtils import indexConvert, indexRevert as indexRevert_non_treated
from utils.treatedDataUtils import indexConvert as indexConvert_treated, indexRevert as indexRevert_treated

def formatAccuracy(knn, X_t, y_t):
    return "{:.2f}".format(knn.score(X_t, y_t) * 100)

X_non_treated = indexConvert(X_non_treated)
X_treated = indexConvert_treated(X_treated)

# NOTE - Treinando o modelo sem tratamento
X_train_non_treated, X_test_non_treated, y_train_non_treated, y_test_non_treated = train_test_split(
    X_non_treated, y_non_treated, test_size=0.3, random_state=42, stratify=y_non_treated)

# NOTE - Treinando o modelo com tratamento
X_train_treated, X_test_treated, y_train_treated, y_test_treated = train_test_split(
    X_treated, y_treated, test_size=0.3, random_state=42, stratify=y_treated)

# neighbors = np.arange(1, 25)
# train_accuracy_non_treated = np.empty(len(neighbors))
# test_accuracy_non_treated = np.empty(len(neighbors))
# train_accuracy_treated = np.empty(len(neighbors))
# test_accuracy_treated = np.empty(len(neighbors))

# for i, k in enumerate(neighbors):
#     # Configurando um classificador knn com k vizinhos
#     knn_non_treated = KNeighborsClassifier(n_neighbors=k)
#     knn_treated = KNeighborsClassifier(n_neighbors=k)

#     # Utilizando o classificador para treinar os dados
#     knn_non_treated.fit(X_train_non_treated, y_train_non_treated)
#     knn_treated.fit(X_train_treated, y_train_treated)

#     # Calculando a acurácia do classificador em treino
#     train_accuracy_non_treated[i] = knn_non_treated.score(
#         X_train_non_treated, y_train_non_treated)
#     train_accuracy_treated[i] = knn_treated.score(
#         X_train_treated, y_train_treated)

#     # Calculando a acurácia do classificador em teste
#     test_accuracy_non_treated[i] = knn_non_treated.score(
#         X_test_non_treated, y_test_non_treated)
#     test_accuracy_treated[i] = knn_treated.score(
#         X_test_treated, y_test_treated)

# Gerar gráfico
# knn_plt.title('k-NN Variação de números de vizinhos')
# knn_plt.plot(neighbors, test_accuracy_non_treated,
#              label='Testando acurácia sem tratamento')
# knn_plt.plot(neighbors, train_accuracy_non_treated,
#              label='Treinando acurácia sem tratamento')
# knn_plt.legend()
# knn_plt.xlabel('Números de vizinhos')
# knn_plt.ylabel('Acurácia')
# knn_plt.show()

# Gerar gráfico
# knn_plt.title('k-NN Variação de números de vizinhos')
# knn_plt.plot(neighbors, test_accuracy_treated,
#              label='Testando acurácia com tratamento')
# knn_plt.plot(neighbors, train_accuracy_treated,
#              label='Treinando acurácia com tratamento')
# knn_plt.legend()
# knn_plt.xlabel('Números de vizinhos')
# knn_plt.ylabel('Acurácia')
# knn_plt.show()

knn_non_treated = KNeighborsClassifier(n_neighbors=8)
knn_non_treated.fit(X_train_non_treated, y_train_non_treated)

knn_treated = KNeighborsClassifier(n_neighbors=17)
knn_treated.fit(X_train_treated, y_train_treated)

formated_non_treated = formatAccuracy(knn_non_treated, X_test_non_treated, y_test_non_treated)
formated_treated = formatAccuracy(knn_treated, X_test_treated, y_test_treated)
accuracy_non_treated = formated_non_treated.__str__()
accuracy_treated = formated_treated.__str__()

example_non_treated = [35, 0, 106967, 0, 11, 0, 5, 2, 0, 1, 3000, 0, 40, 0]
example_treated = [1, 0, 0, 0, 5, 2, 0, 1, 0, 0, 1]
person_data_non_treated = knn_non_treated.predict([example_non_treated])
person_data_treated = knn_treated.predict([example_treated])

print('Acurácia sem tratamento: ' + accuracy_non_treated + '%')
print('Acurácia com tratamento: ' + accuracy_treated + '%\n')
print(indexRevert_non_treated(example_non_treated))
print('Predição de income sem tratamento:' + person_data_non_treated[0].__str__() + ' dólares /ano\n')
print(indexRevert_treated(example_treated))
print('Predição de income com tratamento:' + person_data_treated[0].__str__() + ' dólares /ano')


# ============================================================
# module     : rd-plot
# author     : peter.swinburne@fortaleza-consulting.com
# date       : 05/21/2021
# brief      : log plot reading utility
# updates by :
# ============================================================

##TO DO -> support for log, support for black color curves

import cv2
import numpy as np
from tkinter import ttk
from tkinter import filedialog
from tkinter import *
import tkinter as tk
from tkinter import Tk
from numpy.core.defchararray import upper
import ctypes
from numpy import asarray
import PIL
from PIL import Image
import pandas as pd

user_depth = 0
user_color = 0
user_xmin = 0
user_xmax = 0
user_ymin = 0
user_ymax = 0
edges = np.zeros((2,2),np.int)
counter = 0

def get_edges():
    global col1, col2, image

    Tk().withdraw()
    filename = filedialog.askopenfilename()
    im = cv2.imread(filename)

    #resize to fit window
    image = cv2.resize(im, (1000,800))

    #dialog with instructions
    ctypes.windll.user32.MessageBoxW(0, "Please select your desired track's left and right boundaries", "Color Curve Digitizer", 1)

    while True:

        for x in range (0,2):
                cv2.circle(image, (edges[x][0],edges[x][1]),2,(0,0,0),cv2.FILLED)

        if counter == 2:
        #collect first and last columns
            col1 = (edges[0][0])
            col2 = (edges[1][0])

            cv2.destroyAllWindows()

            break #continue once two edges are detected

        cv2.imshow("Image", image)
        cv2.setMouseCallback("Image", findEdges)
        cv2.waitKey(1)

def user_input_win():
    global user_color, user_xmin, user_xmax, user_ymin, user_ymax
    global col1, col2, color_entry, r, color
    global xmax_entry, xmin_entry, ymax_entry, ymin_entry

    root = tk.Tk()
    root.title("Color Curve Digitizer")

    #center window
    w = 250
    h = 300
    ws = root.winfo_screenwidth()
    hs = root.winfo_screenheight()
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    root.geometry('%dx%d+%d+%d' % (w,h,x,y))

    depth = tk.StringVar()
    xmin = tk.StringVar()
    xmax = tk.StringVar()
    ymin = tk.StringVar()
    ymax = tk.StringVar()

    #input frame
    entry = ttk.Frame(root)
    entry.pack(padx=2, pady=0, fill='x', expand=True)

    xmin_label = ttk.Label(entry, text = "X Min").pack()
    xmin_entry = ttk.Entry(entry, textvariable = xmin, justify = 'center')
    xmin_entry.pack()

    xmax_label = ttk.Label(entry, text = "X Max").pack()
    xmax_entry = ttk.Entry(entry, textvariable = xmax, justify='center')
    xmax_entry.pack()

    ymin_label = ttk.Label(entry, text = "Y Min").pack()
    ymin_entry = ttk.Entry(entry, textvariable = ymin, justify = 'center')
    ymin_entry.pack()

    ymax_label = ttk.Label(entry, text = "Y Max").pack()
    ymax_entry = ttk.Entry(entry, textvariable = ymax, justify='center')
    ymax_entry.pack()

    colors = [('Green', 'G'),('Red', 'R'),('Blue', 'B'),('Black', 'BL')]
    color_entry = tk.StringVar(entry) #pass name of frame containing radiobuttons to update button variable
    color_entry.set(" ")

    colors_label = ttk.Label(entry, text = "Track Color").pack()
    for text, color in colors:
        ttk.Radiobutton(entry, text=text, variable=color_entry, value=color).pack(fill='y', expand=True)

    #button
    digitize_button = ttk.Button(root, text="Digitize", command=masking)
    digitize_button.pack(side='bottom')

    root.mainloop()

def findEdges(event,x,y, flags, params):
    global counter, user_depth, user_color, col1, col2
    global depth_entry, color_entry, track_slice

    if event == cv2.EVENT_LBUTTONDOWN:
        edges[counter] = x,y
        counter = counter + 1

def masking():
    global user_color, edges, col1, col2, color_entry, newfile
    global user_xmin, user_xmax, user_ymin, user_ymax, rescaled_slice
    #create img from track selection
    np.set_printoptions(threshold=np.inf)
    user_color = color_entry.get()

    new_image = image[0:int(8000),int(col1):int(col2)]

    #convert selection to HSV for masking
    hsv = cv2.cvtColor(new_image, cv2.COLOR_BGR2HSV)

    #set color boundaries
    upper_green = np.array([180,255,235])
    lower_green = np.array([12,35,15])
    upper_blue = np.array([185, 185, 244])
    lower_blue = np.array([60, 50, 255])
    upper_red = np.array([10, 255, 255])
    lower_red = np.array([0, 100, 50])

    #create masks
    green_mask = cv2.inRange(hsv, lower_green, upper_green)
    red_mask = cv2.inRange(hsv, lower_red, upper_red)
    blue_mask = cv2.inRange(hsv, lower_blue, upper_blue)

    #create masked images based on user color selection
    if user_color=='G':
        track_slice = cv2.bitwise_and(new_image, new_image, mask=green_mask)

    elif user_color=='R':
        track_slice = cv2.bitwise_and(new_image, new_image, mask=red_mask)

    elif user_color=='B':
        track_slice = cv2.bitwise_and(new_image, new_image, mask=blue_mask)

    trackimg = cv2.imwrite("selection.png", track_slice, [cv2.IMWRITE_PNG_COMPRESSION, 0])

    ## 1. calculate position of color pixels
    im = Image.open('selection.png')
    im2arr = np.array(im)
    width, depth = im.size
    edge_offset = 8
    vals = np.zeros((depth,2))
    for y in range(0,depth):
        vals[y,0] = y
        for x in range(edge_offset, width - edge_offset):
            pix = im.getpixel((x,y))
            if (pix[0] != 0):
                val_s = x
                val_e = 0
                for x2 in range(x+1, width):
                    pix = im.getpixel((x2, y))
                    if pix[0] != 0:
                        continue
                    else:
                        val_e = x2 - 1
                        break
                x_val = val_s + (int((val_e - val_s) / 2))
                vals[y,1] = x_val
                break

    #detect holes in data
    for y in range(0, depth):
        if vals[y,1] == 0.0:
            if  y != depth - 1:
                if (vals[y+1,1] != 0.0):
                    # average the mid point
                    vals[y,1] = (vals[y-1,1] + vals[y+1,1]) / 2.0
                else:
                    # use the last point
                    vals[y, 1] = vals[y - 1, 1]
            else:
                vals[y, 1] = vals[y - 1, 1]

    df = pd.DataFrame(vals)

    #find min,max of df
    min_x = df[0].min()
    max_x = df[0].max()

    min_y = df[1].min()
    max_y = df[1].max()

    ## get user min, max
    user_xmax = int(xmax_entry.get())
    user_xmin = int(xmin_entry.get())

    user_ymax = int(ymax_entry.get())
    user_ymin = int(ymin_entry.get())

    ## column range max - min, user range user max - user min
    range_x = max_x - min_x
    range_y = max_y - min_y

    user_rangex = user_xmax - user_xmin
    user_rangey = user_ymax - user_ymin

    ## expansion rate is user range/column range
    exprate_x = (user_rangex / range_x)
    exprate_y = (user_rangey / range_y)

    ## scale
    df[0] = df[0].apply(lambda x: (x - min_x)*exprate_x + user_xmin)
    df[1] = df[1].apply(lambda x: (x - min_y)*exprate_y + user_ymin)

    ## 10. save new array as csv
    df.to_csv("processed.csv", header=["D", "V"], float_format="%.3f", index=False)

    quit_window()

def quit_window():
    root = tk.Tk()
    root.title("Color Curve Digitizer")

    #center window
    newfile = "'processed.csv'"
    w = 400
    h = 50
    ws = root.winfo_screenwidth()
    hs = root.winfo_screenheight()
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    root.geometry('%dx%d+%d+%d' % (w,h,x,y))

    depth_label = ttk.Label(root, text = "Your selection has been processed and saved as {}.".format(newfile), anchor=tk.CENTER)
    depth_label.pack(padx=5, pady=5, fill='x', expand=True)

    Button(root, text="Done", command=quit).pack()

    root.mainloop()

if __name__ == '__main__':

    get_edges()
    user_input_win()


import os
from PIL import Image
from io import BytesIO
import base64
import pickle

qa_dir = '../../../datasets/PathVQA/qas'
img_dir = '../../../datasets/PathVQA/images'
output_dir = '../../../datasets/finetuning/PathVQA'

if not os.path.exists(output_dir):
	os.mkdir(output_dir)

def extraction(mode):
	if mode == 'train':
		path = os.path.join(qa_dir, 'train_vqa.pkl')
	elif mode == 'val':
		path = os.path.join(qa_dir, 'val_vqa.pkl')
	elif mode == 'test':
		path = os.path.join(qa_dir, 'test_vqa.pkl')

	output_file_name = os.path.join(output_dir, mode+'.tsv')
	index = 0

	with open(output_file_name, 'w') as out:
		with open(path, "rb") as input_file:
			data = pickle.load(input_file)

		for item in data:
			img_id = item['img_id']
			question_id = str(item['question_id'])
			question = item['sent']
			confident_ans = '1|!+' + list(item['label'].keys())[0]

			# image string64base
			img_path = os.path.join(img_dir, mode, img_id + '.jpg')
			img = Image.open(img_path)
			img_buffer = BytesIO()
			img.save(img_buffer, format=img.format)
			byte_data = img_buffer.getvalue()
			base64_str = base64.b64encode(byte_data)
			base64_str = base64_str.decode("utf-8")

			# question_id, image_id, question, answer (with confidence), predicted object labels (set to empty string), image (base64 string)
			out.write(question_id + '\t' + img_id + '\t' + question + '\t' + confident_ans + '\t' + str('') + '\t' + base64_str + '\n')
			index += 1
			if index% 1000 == 0:
				print("finish '{}' instance {}".format(mode, index))

	print("Completed! totally {} '{}' instances".format(index, mode))
	return index

if __name__ == '__main__':
	total = 0
	for mode in ['train', 'val', 'test']:
		num = extraction(mode)
		total += num
	print("Completed! totally {} instances".format(total))


from typing import Optional

import numpy as np

from banditpylib import argmax_or_min
from banditpylib.arms import PseudoArm
from banditpylib.data_pb2 import Context, Actions, Feedback
from .utils import MABFixedBudgetBAILearner

class Uniform(MABFixedBudgetBAILearner):
  """Uniform sampling policy

  Play each arm the same number of times and then output the arm with the
  highest empirical mean.

  :param int arm_num: number of arms
  :param int budget: total number of pulls
  :param Optional[str] name: alias name
  """
  def __init__(self, arm_num: int, budget: int, name: Optional[str] = None):
    super().__init__(arm_num=arm_num, budget=budget, name=name)

  def _name(self) -> str:
    return 'uniform'

  def reset(self):
    self.__pseudo_arms = [PseudoArm() for arm_id in range(self.arm_num)]
    self.__best_arm = None
    self.__stop = False

  def actions(self, context: Context) -> Actions:
    del context

    actions = Actions()

    if not self.__stop:
      # Make sure each arm is sampled at least once
      pulls = np.random.multinomial(self.budget - self.arm_num,
                                    np.ones(self.arm_num) / self.arm_num,
                                    size=1)[0]
      for arm_id in range(self.arm_num):
        arm_pull = actions.arm_pulls.add()
        arm_pull.arm.id = arm_id
        arm_pull.times = pulls[arm_id] + 1

      self.__stop = True

    return actions

  def update(self, feedback: Feedback):
    for arm_feedback in feedback.arm_feedbacks:
      self.__pseudo_arms[arm_feedback.arm.id].update(
          np.array(arm_feedback.rewards))
    if self.__stop:
      self.__best_arm = argmax_or_min(
          [arm.em_mean for arm in self.__pseudo_arms])

  @property
  def best_arm(self) -> int:
    if self.__best_arm is None:
      raise Exception('I don\'t have an answer yet.')
    return self.__best_arm


import re
from typing import List, Tuple

import numpy as np

TURN_ON, TURN_OFF, TOGGLE = 0, 1, 2
Instruction = Tuple[int, slice, slice]
RE_INSTRUCTION = re.compile(r"(.*) (\d+),(\d+) through (\d+),(\d+)")

def parse_instructions(data: str) -> List[Instruction]:
    instructions = []
    for line in data.splitlines():
        op, x1, y1, x2, y2 = RE_INSTRUCTION.fullmatch(line).groups()
        if op == "turn on":
            action = TURN_ON
        elif op == "turn off":
            action = TURN_OFF
        elif op == "toggle":
            action = TOGGLE
        else:
            raise ValueError(f"Invalid input: {line}")
        slice_x = slice(int(x1), int(x2) + 1)
        slice_y = slice(int(y1), int(y2) + 1)
        instructions.append((action, slice_x, slice_y))
    return instructions

def run_simple_lamps(instructions: List[Instruction], dim=(1000, 1000)):
    lamps = np.zeros(dim, dtype=bool)
    for action, slice_x, slice_y in instructions:
        if action == TURN_ON:
            lamps[slice_x, slice_y] = True
        elif action == TURN_OFF:
            lamps[slice_x, slice_y] = False
        else:  # Toggle
            lamps[slice_x, slice_y] ^= True
    return np.sum(lamps)

def run_controllable_lamps(instructions: List[Instruction], dim=(1000, 1000)):
    lamps = np.zeros(dim, dtype=int)
    for action, slice_x, slice_y in instructions:
        if action == TURN_ON:
            lamps[slice_x, slice_y] += 1
        elif action == TURN_OFF:
            lamps[slice_x, slice_y] = np.maximum(lamps[slice_x, slice_y] - 1, 0)
        else:  # Toggle
            lamps[slice_x, slice_y] += 2
    return np.sum(lamps)

def main(data: str):
    instructions = parse_instructions(data)
    yield run_simple_lamps(instructions)
    yield run_controllable_lamps(instructions)


#!/usr/bin/env python
"""

Plot distribution of levels

"""
import csv
import config
import matplotlib.pyplot as plt

config.setup()

# load data
data = []
with open(config.data_path + '/levels.csv', 'rb') as csvfile:
  spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
  for row in spamreader:
    data.append(int(row[0]))

# make data into histrogram
total = len(data)
xs = range(max(data) + 1)
ys = map(lambda x: float(len(filter(lambda i: i == x, data)))/total, xs)

width = 0.7
fig, ax = plt.subplots()
rects = ax.bar([i+(1.-width)/2. for i in xs], ys, width, color=config.colors()[0])

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

plt.gca().set_xlim([min(data),max(data) + 1])

c = 0
for rect in rects:
  height = rect.get_height()
  ax.text(rect.get_x() + rect.get_width()/2., height, '%d' % int(round(height*total)),
          ha='center', va='bottom', size=11)
  ax.text(rect.get_x() + rect.get_width()/2., -0.004, str(c),
          ha='center', va='top', size=14)
  c += 1

rects[0].set_facecolor(config.colors()[1])

plt.xlabel('Stratum', labelpad=25)
plt.ylabel('Fraction of formulas')
plt.xticks([])

plt.subplots_adjust(left=0.13, bottom=0.18)
plt.savefig(config.output + '/levels.pdf')


import streamlit as st
from streamlit_extras.switch_page_button import switch_page
from st_pages import hide_pages, Page, show_pages
from PIL import Image
from pathlib import Path
from pages.custom_components import *

st.set_page_config(initial_sidebar_state="expanded", layout="wide")
hide_pages(get_local_pages())
current_step = 1
if st.session_state.current_lang == "English":
    st.session_state.current_page = "Find Court"
    st.session_state.current_index = 3
else:
    st.session_state.current_page = "Gericht Finden"
    st.session_state.current_index = 3

# initialize session state attributes
question_steps, document_steps = get_question_dicts()
for attr in list(question_steps.keys()) + list(document_steps.keys()):
    if attr not in st.session_state.keys():
        st.session_state[attr] = None

show_navbar()
show_sidebar()

# Content: Question
chapter_spacer()
if st.session_state.current_lang == "English":
    st.subheader("To which area of law is your case assigned?")
else:
    st.subheader("Zu welchem Rechtsgebiet ist Ihr Fall zugehörig?")
st.progress((1.0 / 7) * current_step)
if st.session_state.current_lang == "English":
    st.markdown('
'
                'TEKO is currently only able to provide support in tenancy law. For other issues, finding a court by '
                'yourself might even not be possible. In the context of a criminal proceeding, a court cannot be selected '
                'by a citizen. Please refer to your subpoena for the appropriate court of a criminal proceeding.'
                '
', unsafe_allow_html=True)
else:
    st.markdown('
'
                'TEKO kann derzeit nur im Mietrecht unterstützen. Bei anderen Angelegenheiten ist es möglicherweise '
                'gar nicht möglich, selbst ein Gericht zu finden. Im Rahmen eines Strafverfahrens kann ein Bürger '
                'kein Gericht wählen. Das zuständige Gericht eines Strafverfahrens entnehmen Sie bitte Ihrer Vorladung.'
                '
', unsafe_allow_html=True)

if st.session_state.current_lang == "English":
    question_crime = st.selectbox(label="", options=("Tenancy Law", "Criminal Proceeding", "Other"))
    next_question = st.button("Next Question")
else:
    question_crime = st.selectbox(label="", options=("Mietrecht", "Strafrecht", "Sonstiges"))
    next_question = st.button("Nächste Frage")

if next_question and question_crime in ["Tenancy Law", "Mietrecht"]:
    st.session_state.question_crime = question_crime
    switch_page("question_subject")
elif next_question and question_crime not in ["Tenancy Law", "Mietrecht"]:
    st.error("TEKO is currently only able to provide support in tenancy law!")


from datetime import date, timedelta
import urllib.request
import zipfile
import csv
import redis
import redisconfig
import os

class ExtractBhavCopy:
    baseUrl = "https://www.bseindia.com/download/BhavCopy/Equity/"
    finalUrl = None
    csvData = []
    lastdayDate = None
    csvFileName = None

    def __init__(self):
        dayInterval = 1
        if(date.today().weekday() == 6):  # check for sunday
            dayInterval = 2
        elif ((date.today().weekday() == 0)):  # check for monday
            dayInterval = 3
        self.lastdayDate = (date.today() - timedelta(dayInterval))\
            .strftime("%d%m%y")
        zipFileName = "EQ" + self.lastdayDate + "_CSV.ZIP"
        self.finalUrl = self.baseUrl + zipFileName
        self.csvFileName = "EQ" + self.lastdayDate + ".CSV"
        # print(self.finalUrl)
        try:
            urllib.request.urlretrieve(self.finalUrl, "zipFiles/"
                                       + zipFileName)
        except:
            print("Error downloading extract")
        try:
            zipRef = zipfile.ZipFile("zipFiles/" + zipFileName, 'r')
            zipRef.extractall("csvExtract")
            zipRef.close()
            self.filterCsv()
        except:
            print("Error extracting ZIP file")

    def filterCsv(self):
        with open("csvExtract/" + self.csvFileName, "r") as originalCsv,\
                open("csvExtract/filtered" + self.csvFileName, "w",
                     newline="") as filteredCsv:
            reader = csv.reader(originalCsv)
            writer = csv.writer(filteredCsv)
            for row in reader:
                del row[2:4]
                del row[6:]
                writer.writerow(row)
        self.parseCsv()

    def parseCsv(self):
        with open("csvExtract/filtered" + self.csvFileName, 'r') as csvFile:
            csvData = csv.DictReader(csvFile)
            for row in csvData:
                # print(dict(row))
                self.csvData.append(dict(row))
        # print(self.csvData)

class LoadBhavCopy:
    r = None

    def __init__(self):
        try:
            redistogo_url = os.environ.get("REDISTOGO_URL")
            if (not redistogo_url):
                pool = redis.ConnectionPool(host=redisconfig.host,
                                            port=redisconfig.port,
                                            db=redisconfig.db,
                                            decode_responses=redisconfig
                                            .decode_responses_value)
                self.r = redis.Redis(connection_pool=pool)
            else:
                self.r = redis.from_url(redistogo_url,
                                        decode_responses=redisconfig
                                        .decode_responses_value)
        except:
            print("Error Connecting to Redis")
        self.loadData()

    def loadData(self):
        self.r.flushall()
        for idx, item in enumerate(ExtractBhavCopy.csvData):
            if(idx < 10):
                self.r.rpush("BhavCopy", item)
            self.r.hmset(item["SC_NAME"], item)
        # print(self.r.lrange("BhavCopy", 0, -1))

ExtractBhavCopy()
LoadBhavCopy()


# 괄호 회전하기
# https://programmers.co.kr/learn/courses/30/lessons/76502

from collections import deque

def right(sick):
    # if s가 옳은 거면
    plus = ['(','[','{']
    minus = [')',']','}']
    zero = 0
    stack = deque()
    for j in range(len(sick)):
        if sick[j] in plus :
            stack.append(sick[j])
        else :
            if len(stack) == 0 :
                return False
            if minus.index(sick[j]) != plus.index(stack.pop()) :
                return False

    if len(stack) != 0 : return False
    return True

def solution(s) :
    answer =  0
    print("받은 문자열은 :  " + s )
    for i in range(len(s)):
        temp = s[i:] + s[:i]
        print(temp)
        if right(temp) == True :
            answer += 1
    print("계산 끝")
    return answer

a = "[](){}"
b = "}]()[{"
c = "[)(]"
d = "}}}"

print(solution(a))
print(solution(b))
print(solution(c))
print(solution(d))

# 다른사람의 풀이랑 비슷하다.

27.괄호회전하기.py