import os
import numpy as np
import pretty_midi as pyd
from tqdm import tqdm
from scipy.interpolate import interp1d
import yaml

def midi2matrix(midi, quaver):
        """
        Convert multi-track midi to a 3D matrix of shape (Track, Time, 128).
        Each cell is a integer number representing quantized duration.
        """
        pr_matrices = []
        programs = []
        quantization_error = []
        for track in midi.instruments:
            qt_error = [] # record quantization error
            pr_matrix = np.zeros((len(quaver), 128, 2))
            for note in track.notes:
                note_start = np.argmin(np.abs(quaver - note.start))
                note_end =  np.argmin(np.abs(quaver - note.end))
                if note_end == note_start:
                    note_end = min(note_start + 1, len(quaver) - 1) # guitar/bass plunk typically results in a very short note duration. These note should be quantized to 1 instead of 0.
                pr_matrix[note_start, note.pitch, 0] = note_end - note_start
                pr_matrix[note_start, note.pitch, 1] = note.velocity

                #compute quantization error. A song with very high error (e.g., triple-quaver songs) will be discriminated and therefore discarded.
                if note_end == note_start:
                    qt_error.append(np.abs(quaver[note_start] - note.start) / (quaver[note_start] - quaver[note_start-1]))
                else:
                    qt_error.append(np.abs(quaver[note_start] - note.start) / (quaver[note_end] - quaver[note_start]))

            control_matrix = np.ones((len(quaver), 128, 1)) * -1
            for control in track.control_changes:
                #if control.time < time_end:
                #    if len(quaver) == 0:
                #        continue
                control_time = np.argmin(np.abs(quaver - control.time))
                control_matrix[control_time, control.number, 0] = control.value

            pr_matrix = np.concatenate((pr_matrix, control_matrix), axis=-1)
            pr_matrices.append(pr_matrix)
            programs.append(track.program)
            quantization_error.append(np.mean(qt_error))

        return np.array(pr_matrices), programs, quantization_error

ACC = 4 # quantize each beat as 4 positions

# Get raw MIDI of Slakh2100 from https://zenodo.org/record/4599666.
slakh_root = './data/slakh2100_flac_redux/'
save_root = './data/Slakh2100'
for split in ['train', 'validation', 'test']:
    slakh_split = os.path.join(slakh_root, split)
    save_split = os.path.join(save_root, split)
    if not os.path.exists(save_split):
        os.makedirs(save_split)
    print(f'processing {split} set ...')
    for song in tqdm(os.listdir(slakh_split)):
        break_flag = 0

        all_src_midi = pyd.PrettyMIDI(os.path.join(slakh_split, song, 'all_src.mid'))
        for ts in all_src_midi.time_signature_changes:
            if not (((ts.numerator == 2) or (ts.numerator == 4)) and (ts.denominator == 4)):
                break_flag = 1
                break
        if break_flag:
            continue    # process only 2/4 and 4/4 songs

        tracks = os.path.join(slakh_split, song, 'MIDI')
        track_names = os.listdir(tracks)
        track_midi = [pyd.PrettyMIDI(os.path.join(tracks, track)) for track in track_names]
        track_meta = yaml.safe_load(open(os.path.join(slakh_split, song, 'metadata.yaml'), 'r'))['stems']

        if len(all_src_midi.get_beats()) >= max([len(midi.get_beats()) for midi in track_midi]):
            beats = all_src_midi.get_beats()
            downbeats = all_src_midi.get_downbeats()
        else:
            beats = track_midi[np.argmax([len(midi.get_beats()) for midi in track_midi])].get_beats()
            downbeats = track_midi[np.argmax([len(midi.get_beats()) for midi in track_midi])].get_downbeats()

        beats = np.append(beats, beats[-1] + (beats[-1] - beats[-2]))
        quantize = interp1d(np.array(range(0, len(beats))) * ACC, beats, kind='linear')
        quaver = quantize(np.array(range(0, (len(beats) - 1) * ACC)))

        pr_matrices = []
        programs = []
        dynamic_matrices = []

        break_flag = 0
        for idx, midi in enumerate(track_midi):
            meta = track_meta[track_names[idx].replace('.mid', '')]
            if meta['is_drum']:
                continue    #let's skip drum for now
            else:
                pr_matrix, _, track_qt = midi2matrix(midi, quaver)
                if track_qt[0] > .2:
                    break_flag = 1
                    break
                pr_matrices.append(pr_matrix[..., 0])
                dynamic_matrices.append(pr_matrix[..., 1:])
                programs.append(meta['program_num'])
        if break_flag:
            continue    #skip the pieces with very large quantization error. This pieces are possibly triple-quaver songs

        pr_matrices = np.concatenate(pr_matrices, axis=0)
        programs = np.array(programs)
        dynamic_matrices = np.concatenate(dynamic_matrices, axis=0)

        downbeat_indicator = np.array([int(t in downbeats) for t in quaver])

        np.savez_compressed(os.path.join(save_split, f'{song}.npz'),\
                    tracks = pr_matrices,\
                    programs = programs,\
                    db_indicator = downbeat_indicator,\
                    dynamics = dynamic_matrices)

        #break


# SPDX-License-Identifier: GPL-2.0
#
# Description:
# compile and install U-Boot for the tqm5200s board
# install U-Boot with BDI
# End:

from tbotlib import tbot

# call get u-boot source
tb.workfd = tb.c_ctrl
tb.statusprint("get u-boot source")
tb.eof_call_tc("tc_lab_get_uboot_source.py")

# call set toolchain
tb.statusprint("set toolchain")
tb.eof_call_tc("tc_lab_set_toolchain.py")

# call compile u-boot
tb.statusprint("compile u-boot")
tb.eof_call_tc("tc_lab_compile_uboot.py")

# copy files to tbot dir
tb.statusprint("copy files")
c = tb.workfd
so = "u-boot.bin"
ta = "/tftpboot/" + tb.config.tftpboardname + "/" + tb.config.ub_load_board_env_subdir
tb.eof_call_tc("tc_lab_cp_file.py", ch=c, s=so, t=ta)
ta = "System.map"
tb.eof_call_tc("tc_lab_cp_file.py", ch=c, s=so, t=ta)

tb.workfd = tb.c_ctrl
tb.eof_call_tc("tc_workfd_goto_uboot_code.py")

# check U-Boot version
tb.workfd = tb.c_ctrl
tb.config.tc_ub_get_version_file = "/tftpboot/" + tb.config.tftpboardname + "/" + tb.config.ub_load_board_env_subdir + '/u-boot.bin'
tb.config.tc_ub_get_version_string = 'U-Boot 20'
tb.eof_call_tc("tc_ub_get_version.py")
tb.uboot_vers = tb.config.tc_return

#call upd_uboot
tb.eof_call_tc("tc_lab_bdi_upd_uboot.py")

#check if u-boot works
tb.set_board_state("u-boot")

tb.end_tc(True)


import os

import wx
from timelinelib.wxgui.dialogs.getfilepath.controller import GetFilePathContoller
from timelinelib.wxgui.utils import display_information_message

FUNC_OPEN = 1
FUNC_SAVE_AS = 2
FUNC_NEW = 3

class GetFilePath(wx.FileDialog):

    EXTENSIONS = {
        FUNC_OPEN: ["timeline", "ics"],
        FUNC_SAVE_AS: ["timeline"],
        FUNC_NEW: ["timeline"],
    }

    STYLES = {
        FUNC_OPEN: wx.FD_OPEN,
        FUNC_SAVE_AS: wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT,
        FUNC_NEW: wx.FD_SAVE
    }

    TITLES = {
        FUNC_OPEN: _("Open Timeline"),
        FUNC_SAVE_AS:  _("Save Timeline As"),
        FUNC_NEW: _("Create Timeline"),
    }

    def __init__(self, parent, func, current_path):
        path = current_path or ""
        self._controller = GetFilePathContoller(self, func, path)
        wx.FileDialog.__init__(self,
                               parent,
                               message=self.TITLES[func],
                               defaultDir=self._controller.default_dir,
                               wildcard=self._controller.wildcards,
                               style=self.STYLES[func])

    @property
    def new_path(self):
        return self._controller.new_path()

def open_get_file_path_dialog(func, current_path=""):
    dialog = GetFilePath(None, func, current_path)
    if dialog.ShowModal() == wx.ID_OK:
        new_timeline_path = dialog.new_path
        if func == FUNC_NEW and os.path.exists(new_timeline_path):
            msg1 = _("The specified timeline already exists.")
            msg2 = _("Opening timeline instead of creating new.")
            display_information_message(_("Information"), f"{msg1}\n\n{msg2}", dialog)
    else:
        new_timeline_path = None
    dialog.Destroy()

    return new_timeline_path


from __future__ import print_function
import base64
from termcolor import colored
from datetime import datetime
import os

author = {
    "name":"gl4ssesbo1",
    "twitter":"https://twitter.com/gl4ssesbo1",
    "github":"https://github.com/gl4ssesbo1",
    "blog":"https://www.pepperclipp.com/"
}

needs_creds = True

variables = {
	"SERVICE":{
		"value":"ec2",
		"required":"true",
    	"description":"The service that will be used to run the module. It cannot be changed."
	},
	"INSTANCES":{
		"value":"",
		"required":"false",
        "description":"The instance IDs to query. Either use this option or the WORDLIST option."
	},
	"WORDLIST":{
		"value":"",
		"required":"false",
    	"description":"The wordlist of instance IDs. Either use this option or the INSTANCES option."
	}
}

description = "Lists User data of an Instance provided. Requires Secret Key and Access Key of an IAM that has access to it."

aws_command = "aws ec2 describe-instance-attribute --instance-id <instance ID> --region <region> --attribute userData --profile <profile>"

def exploit(profile, workspace):
	dt_string = (datetime.now()).strftime("%d_%m_%Y_%H_%M_%S")
	os.mkdir("./workspaces/{}/{}".format(workspace, dt_string))

	if not variables['INSTANCES']['value'] == "" and not variables['WORDLIST']['value'] == "":
		print(colored("[*] either put comma separated instances or instance wordlist.","red"))

	ids = []
	if not variables['INSTANCES']['value'] == "":
		ids = (variables['INSTANCES']['value']).split(",")

	elif not variables['WORDLIST']['value'] == "":
		file = open(variables['INSTANCES']['value'], "r")
		for line in file.readlines():
			ids.append(line)
		file.close()
	else:
		inst = profile.describe_instances()
		for x in inst["Reservations"]:
			for y in x["Instances"]:
				ids.append(str(y["InstanceId"]))

	for id in ids:
		response = profile.describe_instance_attribute(
			InstanceId=id,
			Attribute='userData'
		)

		if response['UserData']:
			file = "ec2_enum_user_data_{}".format(id)
			filename = "./workspaces/{}/{}_ec2_data/{}".format(workspace, dt_string, file)
			user_filename = open(filename, 'w')

			data = base64.b64decode(response['UserData']['Value'])
			print("------------------------------")
			print("{} {}".format(colored("InstanceID:","yellow",attrs=['bold']), response['InstanceId']))
			print("------------------------------")
			print("\t{}\t{}\n".format(colored("User data:","red",attrs=['bold']),colored(data,"blue")))
			user_filename.write(str(data))
			user_filename.close()

	print(colored("[*] Current user data is dumped on directiry '{}'. ".format("./workspaces/{}/{}".format(workspace, dt_string)),"yellow"))


from __future__ import annotations

from dataclasses import asdict
from typing import TYPE_CHECKING

from open_precision.core.model import persist_arg
from open_precision.core.model.vehicle import Vehicle

if TYPE_CHECKING:
    from open_precision.system_hub import SystemHub

class VehicleManager:
    def __init__(self, manager: SystemHub):
        self._manager: SystemHub = manager
        self._manager.config.register_value(
            self,
            "vehicles",
            [
                {
                    "name": "example_vehicle",
                    "gps_receiver_offset": [1., 2., 3.],
                    "turn_radius_right": 70.3,
                    "turn_radius_left": 69.1,
                    "wheelbase": 3.2,
                }
            ],
        ).register_value(self, "current_vehicle_id", 0)

        self._current_vehicle_id = self._manager.config.get_value(
            self, "current_vehicle_id"
        )
        self._vehicles = []
        self.load_data()

    def cleanup(self):
        self.save_data()

    def load_data(self):
        # init objects from config data

        def make_vehicle_from_config_dict(conf_values: dict):
            obj = Vehicle()
            print("blub")
            print(obj)
            for key, value in conf_values.items():
                print("bla")
                print(type(value))
                print(value)
                setattr(obj, key, value)
            print("blub2")
            print(obj)
            return obj
        print(type(self._manager.config.get_value(self, "vehicles")[0]))
        print(self._manager.config.get_value(self, "vehicles")[0])
        self._vehicles: list[Vehicle] = [make_vehicle_from_config_dict(x) for x in self._manager.config.get_value(self, "vehicles")]

        self._current_vehicle_id: int = self._manager.config.get_value(
            self, "current_vehicle_id"
        )

    def save_data(self):
        # Vehicle objects are converted to a dict before storing
        self._manager.config.set_value(
            self, "current_vehicle_id", self._current_vehicle_id
        ).set_value(self, "vehicles", [asdict(vehicle) for vehicle in self._vehicles])

    @property
    def current_vehicle(self) -> Vehicle:
        return self._vehicles[self._current_vehicle_id]

    @current_vehicle.setter
    def current_vehicle(self, new_vehicle_id: int):
        self._current_vehicle_id = new_vehicle_id

    @property
    def vehicles(self) -> list[Vehicle]:
        return self._vehicles

    @vehicles.setter
    @persist_arg
    def vehicles(self, new_vehicles: list[Vehicle]):
        self._vehicles = new_vehicles


from typing import Optional

from refactor.tilde_essentials.evaluation import TestEvaluator
from refactor.tilde_essentials.split_criterion import SplitCriterionBuilder
from refactor.tilde_essentials.test_generation import TestGeneratorBuilder
from refactor.tilde_essentials.tree_node import TreeNode

class SplitInfo:
    """
    Contains the information about a split using a test on a set of training examples.

    """

    def __init__(self,
                 test,
                 examples_left,
                 examples_right,
                 score,
                 threshold,
                 split_criterion):
        self.test = test
        self.examples_left = examples_left
        self.examples_right = examples_right
        self.score = score
        self.threshold = threshold
        self.split_criterion = split_criterion

    # def get_test(self):
    #     return self.test
    #
    # @property
    # def get_examples_left(self):
    #     return self.examples_left
    #
    # def get_examples_right(self):
    #     raise NotImplementedError('abstract method')
    #
    # def get_score(self) -> float:
    #     raise NotImplementedError('abstract method')

    def get_split_criterion(self) -> str:
        """
        Returns 'gini' for Gini index, 'entropy' for information gain,
        'MSE' for mean squared error and 'MSA' for Mean Absolute Error
        :return:
        """
        raise NotImplementedError('abstract method')

    def passing_score(self) -> bool:
        return self.score >= self.threshold

class Splitter:
    """
    Finds the best test for splitting a node based on the node's training examples.
    It must be initialized with a SplitCriterion and TestEvaluator.
    Reports the split info using a SplitInfo object.
    """

    def __init__(self, split_criterion_str, test_evaluator: TestEvaluator,
                 test_generator_builder: TestGeneratorBuilder, verbose=False):
        self.split_criterion_str = split_criterion_str
        self.test_evaluator = test_evaluator
        self.test_generator_builder = test_generator_builder
        self.verbose=verbose

    def get_split(self, examples, current_node: TreeNode) -> Optional[SplitInfo]:
        current_best_split_info = None
        split_criterion = SplitCriterionBuilder.get_split_criterion(
            self.split_criterion_str,
            examples, current_node.get_labels(examples))

        generator = self.test_generator_builder.generate_possible_tests(examples, current_node)
        for candidate_test in generator:
            if self.verbose:
                print(candidate_test)
            examples_satisfying_test, examples_not_satisfying_test = self._split_examples(candidate_test, examples)

            candidate_test_score = split_criterion.calculate(examples_satisfying_test,
                                                             examples_not_satisfying_test
                                                             )
            if current_best_split_info is None or candidate_test_score > current_best_split_info.score:
                current_best_split_info = SplitInfo(test=candidate_test,
                                                    examples_left=examples_satisfying_test,
                                                    examples_right=examples_not_satisfying_test,
                                                    score=candidate_test_score,
                                                    threshold=split_criterion.get_threshold(),
                                                    split_criterion=split_criterion.get_name())
            # elif candidate_test_score > current_best_split_info.score:
            #     current_best_split_info = SplitInfo(test=can)
            #     current_best_split_info.test = candidate_test
            #     current_best_split_info.examples_left = examples_satisfying_test
            #     current_best_split_info.examples_right = examples_not_satisfying_test

        return current_best_split_info

    def _split_examples(self, test, examples):
        examples_satisfying_test = set()
        examples_not_satifying_test = set()

        for example in examples:
            succeeds_test = self.test_evaluator.evaluate(example, test)
            if succeeds_test:
                examples_satisfying_test.add(example)
            else:
                examples_not_satifying_test.add(example)
        return examples_satisfying_test, examples_not_satifying_test


"""
    This file dumps (set of) top-m playlists per
    pid of challenge set
    commandline arguments are:
    - invidx.json                   : {track_uri : {pid : freq}}
    - modifications.npy             : numpy.ndarray(lastmodified)
    - challenge_set_filtered.npy    : {pid : numpy.ndarray(track_uri)}

    for each challenge playlist, find set of m
    neighboring playlists and dump it on disk

    HyperParameter:
    1) M := How many neighbors to choose per query pid
"""

import sys
import json
import numpy as np

# hyper parameter
M = 4000

def error(args):
    if (len(args) != 4):
        print("ERROR: Command line args expected")
        exit(1)

def fetchDict(filename):
    with open(filename) as file_obj:
        data = json.load(file_obj)
    return data

def loadFromNpy(filename):
    """numpy array data type is chosen for memory efficiency."""
    return np.load(filename, allow_pickle = True)[()]

def dumpTopM(invidx, modifications, challenge_set):
    topm = {}
    # playlists of challenge set
    qpid_count = 1
    for qpid in challenge_set:
        print(qpid_count, 'the qpid processing...')
        qpid_count += 1
        qtracks = challenge_set[qpid]
        # set of playlist having atleast one
        #track in common with query playlist
        neighbors = set()
        for qtrack_uri in qtracks:
            neighbors.update(set(list(invidx[qtrack_uri].keys())))
        neighbors = [int(n) for n in neighbors]

        # subsampling playlists with latest edit made since epoch
        time_pid = [(-modifications[i], neighbors[i]) for i in range(len(neighbors))]
        del neighbors
        # first element of tuple is kept negative to sort
        # tuples in descending order of thier original magnitude
        time_pid = sorted(time_pid)
        # now choosing top m
        time_pid = time_pid[:M]

        # storing: qpid -> numpy.ndarray(pids)
        topm[qpid] = np.array([pid for (_, pid) in time_pid])
    # dumping to disk
    np.save("topm.npy", topm)

if __name__ == "__main__":
    error(sys.argv)
    invidx_filename = sys.argv[1]
    modifications_filename = sys.argv[2]
    challenge_set_filename = sys.argv[3]

    print("Loading invidx to memory...")
    invidx = fetchDict(invidx_filename)
    print("Loading modifications to memory...")
    modifications = loadFromNpy(modifications_filename)
    print("Loading challenge set to memory")
    challenge_set = loadFromNpy(challenge_set_filename)

    dumpTopM(invidx, modifications, challenge_set)

Session Based Nearest Neighbour/dump_top_m.py


import time
import math
import keyboard
import random
import threading
import sys
import win32api, win32con, win32gui
import ctypes
from PyQt5.QtGui import QImage, QPixmap, QColor, QFont
from PyQt5 import QtCore
from PyQt5.QtCore import QTimer, QEvent
from PyQt5.QtWidgets import QApplication, QGraphicsRectItem, QSlider, QFrame, QDesktopWidget, QMainWindow, QLabel, QVBoxLayout, QWidget, QPushButton
def GUIRun():
    class MainWindow(QMainWindow):
        def exitapp(self):
            print("bye bye")
            sys.exit()

        def __init__(self):
            super().__init__()
            self.setWindowTitle("Cool Overlay")
            self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
            #self.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents, True)
            self.setAttribute(QtCore.Qt.WA_NoChildEventsForParent, True)
            self.setAttribute(QtCore.Qt.WA_ShowWithoutActivating)
            self.setWindowFlags(QtCore.Qt.FramelessWindowHint
                                | QtCore.Qt.WindowStaysOnTopHint
                                | QtCore.Qt.WindowDoesNotAcceptFocus
                                #| QtCore.Qt.WindowTransparentForInput
                                )
            self.setFixedHeight(1080)
            self.setFixedWidth(1920)

            ######  Base Settings Widget ########

            self.settingsWidget = QFrame(self)
            self.settingsWidget.setFrameShape(QFrame.StyledPanel)
            #self.settingsWidget.setAttribute(QtCore.Qt.WA_TranslucentBackground)
            self.settingsWidget.setAttribute(QtCore.Qt.WA_ShowWithoutActivating)
            self.settingsWidget.setWindowFlags(QtCore.Qt.FramelessWindowHint
                                | QtCore.Qt.WindowStaysOnTopHint
                                | QtCore.Qt.WindowDoesNotAcceptFocus
                                #| QtCore.Qt.WindowTransparentForInput
                                )
            self.settingsWidget.setGeometry(int(self.width()/2-400),int((self.height()/2-225)),800,450)
            self.settingsWidget.setStyleSheet("background-color: rgb(110, 0, 106); border-style: inset; border-width: 1px; border-radius: 10px; border-color: rgb(34, 0, 33)")

            KeybindText = QLabel("Alt + M", self.settingsWidget)
            KeybindText.setFont(QFont("Arial", 10))
            KeybindText.setStyleSheet("border-width: 0px")
            KeybindText.move(4,2)

            XinexText = QLabel("XineX", self.settingsWidget)
            XinexText.setFont(QFont("Arial", 17))
            XinexText.setStyleSheet("border-width: 0px")
            XinexText.move(45,85)

            GamesButton = QPushButton("Games", self.settingsWidget)
            GamesButton.setStyleSheet("background-color: rgb(116, 0, 113); border-style: inset; border-width: 2px; border-radius: 5px; border-color: rgb(93, 0, 90)")
            GamesButton.setGeometry(10,125,130,40)

            AimbotButton = QPushButton("Aimbot", self.settingsWidget)
            AimbotButton.setStyleSheet("background-color: rgb(116, 0, 113); border-style: inset; border-width: 2px; border-radius: 5px; border-color: rgb(93, 0, 90)")
            AimbotButton.setGeometry(10,175,130,40)

            VisualsButton = QPushButton("Visuals", self.settingsWidget)
            VisualsButton.setStyleSheet("background-color: rgb(116, 0, 113); border-style: inset; border-width: 2px; border-radius: 5px; border-color: rgb(93, 0, 90)")
            VisualsButton.setGeometry(10,225,130,40)

            OtherButton = QPushButton("Other", self.settingsWidget)
            OtherButton.setStyleSheet("background-color: rgb(116, 0, 113); border-style: inset; border-width: 2px; border-radius: 5px; border-color: rgb(93, 0, 90)")
            OtherButton.setGeometry(10,275,130,40)

            CreditsButton = QPushButton("Credits", self.settingsWidget)
            CreditsButton.setStyleSheet("background-color: rgb(116, 0, 113); border-style: inset; border-width: 2px; border-radius: 5px; border-color: rgb(93, 0, 90)")
            CreditsButton.setGeometry(10,325,130,40)

            ExitButton = QPushButton("Exit", self.settingsWidget)
            ExitButton.setStyleSheet("background-color: rgb(116, 0, 113); border-style: inset; border-width: 2px; border-radius: 5px; border-color: rgb(93, 0, 90)")
            ExitButton.setGeometry(10,400,130,40)

            ##########  Games Settings  ##########

            self.GamesSettings = QFrame(self.settingsWidget)
            self.GamesSettings.setGeometry(150,0,650,450)
            self.GamesSettings.setStyleSheet("background-color: rgb(100, 10, 95); border-width: 3px; border-color: rgb(105, 5, 102); border-style: solid")
            self.GamesSettings.hide()

            ArsenalGame1 = QPushButton("Arsenal", self.GamesSettings)
            ArsenalGame1.setStyleSheet("background-color: rgb(120,30,115);border-style: outset; border-width: 3px; border-radius: 10px; border-color: rgb(39, 0, 54)")
            ArsenalGame1.setGeometry(40,40,120,140)

            CustomGame99 = QPushButton("Custom Model", self.GamesSettings)
            CustomGame99.setStyleSheet("background-color: rgb(120,30,115);border-style: outset; border-width: 3px; border-radius: 10px; border-color: rgb(39, 0, 54)")
            CustomGame99.setGeometry(545,420,100,25)

            ConceptGameText = QLabel("!!Concept!!", self.GamesSettings)
            ConceptGameText.setFont(QFont("Arial",70))
            ConceptGameText.setStyleSheet("border-style: none")
            ConceptGameText.move(100,200)

            ########  Aimbot Settings  #########

            self.AimbotSettings = QFrame(self.settingsWidget)
            self.AimbotSettings.setGeometry(150,0,650,450)
            self.AimbotSettings.setStyleSheet("background-color: rgb(100, 10, 95); border-width: 3px; border-color: rgb(105, 5, 102); border-style: solid")
            self.AimbotSettings.show()

            AimSectionLabel = QLabel("---- Aimbot ----", self.AimbotSettings)
            AimSectionLabel.setFont(QFont("Arial",15))
            AimSectionLabel.setStyleSheet("border-style: none")
            AimSectionLabel.move(15,15)

            AimSectionKeybind = QLabel("Bind: X", self.AimbotSettings)
            AimSectionKeybind.setFont(QFont("Arial",10))
            AimSectionKeybind.setStyleSheet("border-style: none;background-color: rgb(105, 5, 102);")
            AimSectionKeybind.move(150,20)

            AimSpeedSlider = QSlider(QtCore.Qt.Horizontal, self.AimbotSettings)
            AimSpeedSlider.setGeometry(15,50,120,20)
            AimSpeedSlider.setStyleSheet("border-style: none")
            AimSpeedSlider.setRange(0,100)

            AimSpeedText = QLabel("0.5x Aim Speed", self.AimbotSettings)
            AimSpeedText.setFont(QFont("Arial",10))
            AimSpeedText.setStyleSheet("background-color: rgb(105, 5, 102); border-style: none")
            AimSpeedText.move(150,50)

            AimPercissionSlider = QSlider(QtCore.Qt.Horizontal, self.AimbotSettings)
            AimPercissionSlider.setGeometry(15,100,120,20)
            AimPercissionSlider.setStyleSheet("border-style: none")
            AimPercissionSlider.setRange(0,100)

            AimPercissionText = QLabel("0.5x Percission", self.AimbotSettings)
            AimPercissionText.setFont(QFont("Arial",10))
            AimPercissionText.setStyleSheet("background-color: rgb(105, 5, 102); border-style: none")
            AimPercissionText.move(150, 100)

            BoxExpanderSlider = QSlider(QtCore.Qt.Horizontal, self.AimbotSettings)
            BoxExpanderSlider.setGeometry(15,150,120,20)
            BoxExpanderSlider.setStyleSheet("border-style: none")
            BoxExpanderSlider.setRange(0,20)

            BoxExpanderText = QLabel("5px Box Expand", self.AimbotSettings)
            BoxExpanderText.setFont(QFont("Arial",10))
            BoxExpanderText.setStyleSheet("background-color: rgb(105, 5, 102); border-style: none")
            BoxExpanderText.move(150,150)

            MinMoveSlider = QSlider(QtCore.Qt.Horizontal, self.AimbotSettings)
            MinMoveSlider.setGeometry(15,200,120,20)
            MinMoveSlider.setStyleSheet("border-style: none")
            MinMoveSlider.setRange(0,10)

            MinMoveText = QLabel("0px Min Move", self.AimbotSettings)
            MinMoveText.setFont(QFont("Arial", 10))
            MinMoveText.setStyleSheet("background-color: rgb(105, 5, 102); border-style: none")
            MinMoveText.move(150,200)

            HeadshotButton = QPushButton("Headshot: True", self.AimbotSettings)
            HeadshotButton.setText("Headshot: False")
            HeadshotButton.setStyleSheet("background-color: rgb(120,30,115);border-style: outset; border-width: 3px; border-radius: 10px; border-color: rgb(39, 0, 54)")
            HeadshotButton.setGeometry(15,225,120,35)

            AimbotMethodButton = QPushButton("Method: Closest", self.AimbotSettings)
            AimbotMethodButton.setText("Method: Confidence")
            AimbotMethodButton.setStyleSheet("background-color: rgb(120,30,115);border-style: outset; border-width: 3px; border-radius: 10px; border-color: rgb(39, 0, 54)")
            AimbotMethodButton.setGeometry(140,225,120,35)

            TriggerbotSectionText = QLabel("---- Triggerbot ----", self.AimbotSettings)
            TriggerbotSectionText.setFont(QFont("Arial",15))
            TriggerbotSectionText.setStyleSheet("border-style: none")
            TriggerbotSectionText.move(15,275)

            TriggerbotSectionKeybind = QLabel("Bind: None", self.AimbotSettings)
            TriggerbotSectionKeybind.setFont(QFont("Arial",10))
            TriggerbotSectionKeybind.setStyleSheet("background-color: rgb(105, 5, 102); border-style: none")
            TriggerbotSectionKeybind.move(180,280)

            TriggerbotConceptText = QLabel("!!Concept!!", self.AimbotSettings)
            TriggerbotConceptText.setFont(QFont("Arial", 30))
            TriggerbotConceptText.setStyleSheet("border-style: none")
            TriggerbotConceptText.move(15,325)

            SettingsSectionText = QLabel("---- Settings ----", self.AimbotSettings)
            SettingsSectionText.setFont(QFont("Arial", 15))
            SettingsSectionText.setStyleSheet("border-style: none")
            SettingsSectionText.move(470,15)

            AimOnOffButton = QPushButton("Aimbot ON", self.AimbotSettings)
            AimOnOffButton.setStyleSheet("background-color: rgb(120,30,115);border-style: outset; border-width: 3px; border-radius: 10px; border-color: rgb(39, 0, 54)")
            AimOnOffButton.setGeometry(480,45,120,35)

            TriggerOnOffButton = QPushButton("Trigger ON", self.AimbotSettings)
            TriggerOnOffButton.setStyleSheet("background-color: rgb(120,30,115);border-style: outset; border-width: 3px; border-radius: 10px; border-color: rgb(39, 0, 54)")
            TriggerOnOffButton.setGeometry(480,95,120,35)

            FOVSizeText = QLabel("243px FOV", self.AimbotSettings)
            FOVSizeText.setFont(QFont("Arial",10))
            FOVSizeText.setStyleSheet("background-color: rgb(105, 5, 102); border-style: none")
            FOVSizeText.move(508,140)

            FOVSizeSlider = QSlider(QtCore.Qt.Horizontal, self.AimbotSettings)
            FOVSizeSlider.setGeometry(480,160,120,20)
            FOVSizeSlider.setStyleSheet("border-style: none")
            FOVSizeSlider.setRange(1,1000)

            #####  Visuals Settings  ######

            self.VisualsSettings = QFrame(self.settingsWidget)
            self.VisualsSettings.setGeometry(150,0,650,450)
            self.VisualsSettings.setStyleSheet("background-color: rgb(100, 10, 95); border-width: 3px; border-color: rgb(105, 5, 102); border-style: solid")
            self.VisualsSettings.hide()

            ESPSectionLabel = QLabel("---- ESP ----", self.VisualsSettings)
            ESPSectionLabel.setFont(QFont("Arial", 15))
            ESPSectionLabel.setStyleSheet("border-style: none")
            ESPSectionLabel.move(15,15)

            ESPConceptLabel = QLabel("!!Concept!!", self.VisualsSettings)
            ESPConceptLabel.setFont(QFont("Arial",30))
            ESPConceptLabel.setStyleSheet("border-style: none")
            ESPConceptLabel.move(15,40)

            OtherSectionLabel = QLabel("---- Other ----", self.VisualsSettings)
            OtherSectionLabel.setFont(QFont("Arial",15))
            OtherSectionLabel.setStyleSheet("border-style: none")
            OtherSectionLabel.move(510,15)

            FOVOnOffButton = QPushButton("FOV Hidden", self.VisualsSettings)
            FOVOnOffButton.setStyleSheet("background-color: rgb(120,30,115);border-style: outset; border-width: 3px; border-radius: 10px; border-color: rgb(39, 0, 54)")
            FOVOnOffButton.setGeometry(510,50,120,35)

            StatsOnOffButton = QPushButton("Stats Hidden", self.VisualsSettings)
            StatsOnOffButton.setStyleSheet("background-color: rgb(120,30,115);border-style: outset; border-width: 3px; border-radius: 10px; border-color: rgb(39, 0, 54)")
            StatsOnOffButton.setGeometry(510,100,120,35)

            #####  Other Settings  ######

            self.OtherSettings = QFrame(self.settingsWidget)
            self.OtherSettings.setGeometry(150,0,650,450)
            self.OtherSettings.setStyleSheet("background-color: rgb(100, 10, 95); border-width: 3px; border-color: rgb(105, 5, 102); border-style: solid")
            self.OtherSettings.hide()

            Welp = QLabel("don't really know what to put here", self.OtherSettings)
            Welp.setStyleSheet("border-style: none")
            Welp.setFont(QFont("Arial",30))
            Welp.move(15,15)

            ConfigSaveButton = QPushButton("Save Config (Placeholder)", self.OtherSettings)
            ConfigSaveButton.setStyleSheet("background-color: rgb(120,30,115);border-style: outset; border-width: 3px; border-radius: 10px; border-color: rgb(39, 0, 54)")
            ConfigSaveButton.setGeometry(15,100,120,35)

            ConfigLoadButton = QPushButton("Load Config (Placeholder)", self.OtherSettings)
            ConfigLoadButton.setStyleSheet("background-color: rgb(120,30,115);border-style: outset; border-width: 3px; border-radius: 10px; border-color: rgb(39, 0, 54)")
            ConfigLoadButton.setGeometry(15,150,120,35)

            #####  Credits Page  #####

            self.CreditsPage = QFrame(self.settingsWidget)
            self.CreditsPage.setGeometry(150,0,650,450)
            self.CreditsPage.setStyleSheet("background-color: rgb(100, 10, 95); border-width: 3px; border-color: rgb(105, 5, 102); border-style: solid")
            self.CreditsPage.hide()

            GithubButton = QPushButton("Github", self.CreditsPage)
            GithubButton.setStyleSheet("background-color: rgb(120,30,115);border-style: outset; border-width: 3px; border-radius: 10px; border-color: rgb(39, 0, 54)")
            GithubButton.setGeometry(40,30,120,35)

            DiscordButton = QPushButton("Discord Server", self.CreditsPage)
            DiscordButton.setStyleSheet("background-color: rgb(120,30,115);border-style: outset; border-width: 3px; border-radius: 10px; border-color: rgb(39, 0, 54)")
            DiscordButton.setGeometry(270,30,120,35)

            YoutubeButton = QPushButton("Youtube", self.CreditsPage)
            YoutubeButton.setStyleSheet("background-color: rgb(120,30,115);border-style: outset; border-width: 3px; border-radius: 10px; border-color: rgb(39, 0, 54)")
            YoutubeButton.setGeometry(490,30,120,35)

            ThankYouText = QLabel("          Thank You for using this script!\n     If you find issues or have suggestions\nPlease add me on discord or submit a github issue", self.CreditsPage)
            ThankYouText.setStyleSheet("border-style: none")
            ThankYouText.setFont(QFont("Arial",15))
            ThankYouText.move(130,200)

            exit = QPushButton('Exit', self)
            exit.move(int(self.width()*0.9),int(self.height()*0.9))
            exit.clicked.connect(self.exitapp)

            self.placehold = QPushButton("CLICK", self)
            self.placehold.move(int(self.width()*0.5),int(self.height()*0.5))
            self.placehold.clicked.connect(self.placeclick)
            self.placehold.hide()

            self.settbutt = QPushButton('Settings', self)
            self.settbutt.move(int(self.width()*0.8),int(self.height()*0.9))
            self.settbutt.clicked.connect(self.opensett)

            self.stats = QLabel(self)
            #label.setFrameStyle(QFrame.Panel | QFrame.Sunken)
            self.stats.setText("oop")
            self.stats.move(int(self.width()/10),int(self.height()/10))

            self.timer = QTimer()
            self.timer.timeout.connect(self.update_ui)
            self.timer.start(10)

        def update_ui(self):
            self.stats.setText("fps: 59.028434237\ndets: 3\nconf: 32.6")

        def opensett(self):
            print("Opening settings")
            self.placehold.show()

        def placeclick(self):
            print("clickclack")
            self.placehold.hide()

        # def center(self):
        #     qr = self.frameGeometry()
        #     cp = QDesktopWidget().availableGeometry().center()
        #     qr.moveCenter(cp)
        #     self.move(qr.topLeft())

    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = MainWindow()
        ex.show()
        sys.exit(app.exec_())

GUIRun()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/5/28 20:48
# @Author  : Matrix
# @Site    :
# @File    : manage.py
# @Software: PyCharm
import os
import sys
import logging.config
import app.database.models
from app import create_app
from app.database import db
from app.exts import manager
from app.user.controller import new_user
from logging.handlers import TimedRotatingFileHandler

__author__ = 'blackmatrix'

# command line env
if sys.argv and len(sys.argv) > 1 and '-env' in sys.argv[1]:
    app_config = sys.argv[1][sys.argv[1].find('=') + 1:]
else:
    app_config = None

flask_app = create_app(app_config)

# Flask-Logger
if not os.path.exists('logs'):
    os.mkdir('logs')
# 默认级别为ERROR，设置为DEBUG，记录INFO和DEBUG级别的日志
logging.basicConfig(level=logging.DEBUG)
logfile = os.path.abspath('logs/manage.log')
# 每个日志512k,保留10个日志文件
fh = TimedRotatingFileHandler('logs/manage.log', 'midnight', 1, 10, encoding="utf-8")
ch = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s - [%(filename)s:%(lineno)s]')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
flask_app.logger.addHandler(fh)
flask_app.logger.addHandler(ch)

@manager.command
def devserver():
    flask_app.run(host=flask_app.config['HOST'],  port=flask_app.config['PORT'])

@manager.command
def initadmin(email, user_name, password):
    new_user(email, user_name, password)

@manager.command
def createdb():
    db.create_all()

@manager.command
def dropdb():
    db.drop_all()

# @manager.command
# def celery():
    # from app.extensions import celery as celery_app
    # with flask_app.app_context():
    #     celery_app.start('-A runcelery.celery worker --loglevel=info')
    # cmd = 'env={config} celery -A manage.flask_celery worker --loglevel=info'.format(config=app_config)
    # os.system(cmd)
#
#
# @manager.command
# def runserver():
#     cmd = 'env={config} gunicorn -k gevent -w {workers} -b {host}:{port} manage:flask_app'.format(
#         config=app_config,
#         workers=flask_app.config['WORKS'],
#         host=flask_app.config['HOST'],
#         port=flask_app.config['PORT'])
#     os.system(cmd)

if __name__ == '__main__':
    manager.run()


import streamlit as st
from inspect import signature
from sklearn.utils import all_estimators
from xgboost import XGBClassifier, XGBRegressor
from catboost import CatBoostClassifier, CatBoostRegressor
from lightgbm import LGBMClassifier, LGBMRegressor
from sklearn.metrics import (
    accuracy_score,
    precision_score,
    recall_score,
    f1_score,
    mean_squared_error,
    mean_absolute_error,
    r2_score,
    explained_variance_score,
    max_error,
    mean_absolute_percentage_error,
    median_absolute_error,
    mean_squared_log_error,
    mean_poisson_deviance,
    mean_gamma_deviance,
    mean_tweedie_deviance,
    roc_auc_score,
)

from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
import pandas as pd
import numpy as np

def models(model_type):
    """
    Function to return all the models for the specified model type.

    Args:
        model_type: The type of model to get. (Regression or Classification)

    Returns:
        A list of models for the specified model type.
    """
    if model_type == "Regression":
        regressorModels = all_estimators(type_filter="regressor")
        regressorModels.append(("XGBoost", XGBRegressor))
        regressorModels.append(("CatBoost", CatBoostRegressor))
        regressorModels.append(("LightGBM", LGBMRegressor))

        for name, estimator in regressorModels:
            if requiresPositionalArgument(estimator):
                regressorModels.remove((name, estimator))
        return regressorModels

    else:
        classifierModels = all_estimators(type_filter="classifier")
        classifierModels.append(("XGBoost", XGBClassifier))
        classifierModels.append(("CatBoost", CatBoostClassifier))
        classifierModels.append(("LightGBM", LGBMClassifier))

        for name, estimator in classifierModels:
            if requiresPositionalArgument(estimator):
                classifierModels.remove((name, estimator))
        return classifierModels

def eval(X_train, y_train, X_test, y_test, model, eval_metrics):
    """
    Function to evaluate the model and return the evaluation results.

    Args:
        X_train: The training data.
        y_train: The training target.
        X_test: The testing data.
        y_test: The testing target.
        model: The model to evaluate.
        eval_metrics: The evaluation metrics to use.

    Returns:
        The predictions from the model and the evaluation results.
    """
    try:
        if isinstance(model, type):
            model = model()
        else:
            model = model
        model.fit(X_train, y_train)
        y_pred = model.predict(X_test)

        evaluation_results = {}

        metric_functions = {
            "Accuracy": accuracy_score,
            "Precision": precision_score,
            "Recall": recall_score,
            "F1": f1_score,
            "AUC": roc_auc_score,
            "MSE": mean_squared_error,
            "RMSE": lambda y_true, y_pred: mean_squared_error(
                y_true, y_pred, squared=False
            ),
            "MAE": mean_absolute_error,
            "R2": r2_score,
            "Adjusted R2": lambda y_true, y_pred: 1
            - (1 - r2_score(y_true, y_pred))
            * (len(y_true) - 1)
            / (len(y_true) - X_test.shape[1] - 1),
            "Explained Variance": explained_variance_score,
            "Max Error": max_error,
            "Mean Absolute Percentage Error": mean_absolute_percentage_error,
            "Median Absolute Error": median_absolute_error,
            "Mean Squared Log Error": mean_squared_log_error,
            "Mean Poisson Deviance": mean_poisson_deviance,
            "Mean Gamma Deviance": mean_gamma_deviance,
            "Mean Tweedie Deviance": mean_tweedie_deviance,
        }

        for metric in eval_metrics:
            if metric in metric_functions:
                score = metric_functions[metric](y_test, y_pred)
                evaluation_results[metric] = score
            else:
                raise ValueError("Invalid metric")
        return y_pred, evaluation_results
    except:
        raise ValueError("Invalid parameters")

def featureExtraction(X_train, X_test, slider):
    """
    Function to perform PCA and return the top k components.

    Args:
        X_train: The training data.
        X_test: The testing data.
        slider: The number of components to use for PCA.

    Returns:
        The training and testing data with the top k components.
    """

    mean_value_train = np.nanmean(X_train)
    mean_value_test = np.nanmean(X_test)
    nan_indices_train = np.isnan(X_train)
    nan_indices_test = np.isnan(X_test)
    X_train[nan_indices_train] = mean_value_train
    X_test[nan_indices_test] = mean_value_test

    scalar = StandardScaler()
    X_train = scalar.fit_transform(X_train)
    X_test = scalar.transform(X_test)

    pca = PCA(n_components=slider)
    X_train = pca.fit_transform(X_train)
    X_test = pca.transform(X_test)

    return X_train, X_test

def training(
    model_type,
    model_selection_radio,
    model_selector,
    X_train,
    y_train,
    X_test,
    y_test,
    eval_metrics,
    comparison_metrics,
    slider=None,
):
    """
    Function to train the either a single model or multiple models and display the results.

    Args:
        model_type : The type of model to train. (Regression or Classification)
        model_selection_radio: The type of model selection to use. (Comparative Analysis or Individual Model Selection)
        model_selector: The model to train. (Only used for Individual Model Selection)
        X_train: The training data.
        y_train: The training target.
        X_test: The testing data.
        y_test: The testing target.
        eval_metrics: The evaluation metrics to use.
        comparison_metrics: The comparison metric to use.
        slider: The number of components to use for PCA.

    Returns:
        The predictions from the model.

    """

    if slider is not None and slider > 0 and slider <= X_train.shape[1]:
        X_train, X_test = featureExtraction(X_train, X_test, slider)
    elif slider is None:
        pass
    else:
        raise ValueError("Invalid slider value")

    if model_selection_radio == "Comparative Analysis":
        st.header("Comparative Analysis")
        comparison_results = {}

        for name, model in models(model_type):
            try:
                y_pred, evaluation_results = eval(
                    X_train, y_train, X_test, y_test, model, eval_metrics
                )

                model_evaluation = {}
                for metrics, score in evaluation_results.items():
                    model_evaluation[metrics] = score

                comparison_results[name] = model_evaluation
            except:
                pass

        st.subheader("Best Model")
        best_model_name = max(
            comparison_results,
            key=lambda model_name: comparison_results[model_name][comparison_metrics],
        )
        st.write("Best Model:", best_model_name)
        st.subheader("Evaluation Results")
        results = pd.DataFrame(comparison_results)
        results = results.transpose()
        results = results.reset_index()
        results = results.rename(columns={"index": "Model"})
        st.table(results.style.set_properties(**{"text-align": "center"}))
        return 0

    else:
        st.header("Single Model")
        st.markdown(
            "
Model Evaluation Results
",
            unsafe_allow_html=True,
        )

        model = [est for name, est in models(model_type) if name == model_selector][0]

        y_pred, evaluation_results = eval(
            X_train, y_train, X_test, y_test, model, eval_metrics
        )
        best_model = pd.DataFrame({"Model": [model_selector], **evaluation_results})
        st.table(best_model.style.set_properties(**{"text-align": "center"}))
        return y_pred

def requiresPositionalArgument(model_class):
    """
    Check if a scikit-learn estimator class requires any positional arguments.

    Args:
        model_class: The scikit-learn estimator class.

    Returns:
        A boolean indicating whether the model class requires positional arguments.
    """
    is_positional = False
    constructor = signature(model_class)
    parameters = constructor.parameters
    for arg_name, arg_info in parameters.items():
        if arg_info.default == arg_info.empty:
            is_positional = True

    return is_positional


from typing import List
import os
import requests
from lxml import html, etree
from .document import Documents
import lxml

BASE_URL = "https://www.sec.gov"

class Company():

    def __init__(self, name, cik, user_agent, timeout=10):
        self.name = name
        self.cik = cik
        self.url = f"https://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&CIK={cik}"
        self.timeout = timeout
        self._document_urls = []
        self.user_agent = user_agent
        self.get_company_info()

    @property
    def document_urls(self):
      return list(set(self._document_urls))

    def _get(self, url):
      return requests.get(url, timeout=self.timeout, headers={'User-Agent':self.user_agent})

    def get_company_info(self):
        page = html.fromstring(self._get(self.url).content)
        companyInfo = page.xpath("//div[@class='companyInfo']")[0] if page.xpath("//div[@class='companyInfo']") else None
        if companyInfo is not None:
          indentInfo = companyInfo.getchildren()[1]
          self.sic = indentInfo.getchildren()[1].text if len(indentInfo.getchildren()) > 2 else ""
          self.us_state = indentInfo.getchildren()[3].text if len(indentInfo.getchildren()) > 4 else ""

        self._document_urls = [ BASE_URL + elem.attrib["href"]
            for elem in page.xpath("//*[@id='documentsbutton']") if elem.attrib.get("href")]

    def get_filings_url(self, filing_type="", prior_to="", ownership="include", no_of_entries=100) -> str:
        url = self.url + "&type=" + filing_type + "&dateb=" + prior_to + "&owner=" +  ownership + "&count=" + str(no_of_entries)
        return url

    def get_all_filings(self, filing_type="", prior_to="", ownership="include", no_of_entries=100) -> lxml.html.HtmlElement:
      url = self.get_filings_url(filing_type, prior_to, ownership, no_of_entries)
      page = self._get(url)
      return html.fromstring(page.content)

    def _group_document_type(self, tree, document_type):
      result = []
      grouped = []
      for i, elem in enumerate(tree.xpath('//*[@id="seriesDiv"]/table/tr')):
        if i == 0:
          continue
        url = elem.xpath("td")[1].getchildren()[0].attrib["href"]
        grouped.append(url)
        if elem.xpath("td")[0].text == document_type:
          result.append(grouped)
          grouped = []
      return result

    def get_document_type_from_10K(self, document_type, no_of_documents=1) -> List[lxml.html.HtmlElement]:
      tree = self.get_all_filings(filing_type="10-K")
      url_groups = self._group_document_type(tree, "10-K")[:no_of_documents]
      result = []
      for url_group in url_groups:
        for url in url_group:
          url = BASE_URL + url
          self._document_urls.append(url)
          content_page = Company.get_request(url)
          table = content_page.find_class("tableFile")[0]
          for row in table.getchildren():
            if document_type in row.getchildren()[3].text:
              href = row.getchildren()[2].getchildren()[0].attrib["href"]
              href = BASE_URL + href
              href = href.replace("ix?doc=/", "") # required for new iXBRL to HTML
              if len(href.split("/")[-1].split(".")) == 1:
                continue
              doc = Company.get_request(href)
              result.append(doc)
      return result

    def get_data_files_from_10K(self, document_type, no_of_documents=1, isxml=False) -> List[lxml.html.HtmlElement]:
      tree = self.get_all_filings(filing_type="10-K")
      url_groups = self._group_document_type(tree, "10-K")[:no_of_documents]
      result = []
      for url_group in url_groups:
        for url in url_group:
          url = BASE_URL + url
          self._document_urls.append(url)
          content_page = Company.get_request(url)
          tableFile = content_page.find_class("tableFile")
          if len(tableFile) < 2:
            continue
          table = tableFile[1]
          for row in table.getchildren():
            if document_type in row.getchildren()[3].text:
              href = row.getchildren()[2].getchildren()[0].attrib["href"]
              href = BASE_URL + href
              doc = Company.get_request(href, isxml=isxml)
              result.append(doc)
      return result

    @classmethod
    def __get_documents_from_element__(cls, elem, as_documents=False):
      url = BASE_URL + elem.attrib["href"]
      if as_documents:
        return Documents(url)
      else:
        content_page = Company.get_request(url)
        table = content_page.find_class("tableFile")[0]
        last_row = table.getchildren()[-1]
        href = last_row.getchildren()[2].getchildren()[0].attrib["href"]
        href = BASE_URL + href
        return Company.get_request(href)

    def get_10Ks(self, no_of_documents=1, as_documents=False) -> List[lxml.html.HtmlElement]:
      tree = self.get_all_filings(filing_type="10-K")
      elems = tree.xpath('//*[@id="documentsbutton"]')[:no_of_documents]
      result = []
      for elem in elems:
          doc = Company.__get_documents_from_element__(elem, as_documents=as_documents)
          result.append(doc)
      return result

    def get_10K(self) -> List[lxml.html.HtmlElement]:
      return self.get_10Ks(no_of_documents=1)[0]

    @classmethod
    def get_request(cls, href, isxml=False, timeout=10):
        #print(dir(cls.__class__.__getattribute__.classAttr))
        page = requests.get(href, timeout=timeout, headers={'User-Agent': 'Testing Testing test@example.com'})
        if isxml:
          p = etree.XMLParser(huge_tree=True)
          return etree.fromstring(page.content, parser=p)
        else:
          return html.fromstring(page.content)

    @classmethod
    def get_documents(cls, tree: html.HtmlElement, no_of_documents=1, debug=False, as_documents=False) -> List:
        elems = tree.xpath('//*[@id="documentsbutton"]')[:no_of_documents]
        result = []
        for elem in elems:
            filing = Company.__get_documents_from_element__(elem, as_documents=as_documents)
            result.append(filing)

        if len(result) == 1:
            return result[0]
        return result

    @classmethod
    def get_CIK_from_company(cls, company_name):
        tree = cls.get_request("https://www.sec.gov/cgi-bin/browse-edgar?company=" + company_name)
        CIKList = tree.xpath('//*[@id="seriesDiv"]/table/tr[*]/td[1]/a/text()')
        names_list = []
        for elem in tree.xpath('//*[@id="seriesDiv"]/table/tr[*]/td[2]'):
            names_list.append(elem.text_content())
        return list(zip(CIKList, names_list))


import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, SimpleRNN, Dropout, Bidirectional, Conv1D, Flatten       # 보통 첫글자가 소문자면 함수(기능), 대문자면 클래스(집단안에 모든 개체들과 액션들?)

#1. 데이터
x = np.array([[1,2,3],                               #array는 행렬을 의미! 모든 연산은 numpy로 한다.
             [2,3,4],                                #x에서 [1,2,3]으로 짜른 범위는 timesteps, 그 안의 1은 feature
             [3,4,5],
             [4,5,6]])
y = np.array([4,5,6,7])

print(x.shape, y.shape)   # (4, 3) (4,)

# input_shape = (batch_size, timesteps, feature)
# input_shape = (행, 열, 몇개씩 짜르는지!!!)
x = x.reshape(4, 3, 1)

#2. 모델구성
model = Sequential()
# model.add(SimpleRNN(10, input_shape=(3,1), return_sequences=True))   # '3':timesteps(나눈구간의 feature의 갯수), '1':input(feature)
# model.add(Bidirectional(SimpleRNN(10),input_shape=(3,1)))    #  Bidirectional으로 RNN을 랩핑한다
model.add(Conv1D(10, 2, input_shape=(3,1)))    # 10은 필터, 2는 커널사이즈
model.add(Dense(10, activation='relu'))
model.add(Flatten())
model.add(Dense(1))
model.summary()
'''
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
conv1d (Conv1D)              (None, 2, 10)             30     ---------> Conv1D 사용으로 파리미터 갯수가 줄어 속도 향상
_________________________________________________________________
dense (Dense)                (None, 2, 10)             110    ---------> Dense는 들어오는 차원의 형태로 받아들이고 나타냄(무조건 2차원의 형태만 가능한것이 아님)
_________________________________________________________________
flatten (Flatten)            (None, 20)                0      ---------> Flatten의 위치는 상관없음. 2차원으로 output하기 위한 요소.
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 21
=================================================================
Total params: 161
Trainable params: 161
Non-trainable params: 0
_________________________________________________________________
'''

# #3. 컴파일, 훈련
# model.compile(loss='mae', optimizer='adam')      # optimizer는 loss를 최적화한다
# model.fit(x, y, epochs=100, batch_size=1)

# #4. 평가, 예측
# model.evaluate(x, y)

# x2 = np.array([5,6,7]).reshape(1,3,1)

# result = model.predict(x2)
# print(result)


# leetcode question 72: Edit distance
# time and space = O(mn)

class Solution:
    def minDistance(self, word1: str, word2: str) -> int:
        len_word1 = len(word1)
        len_word2 = len(word2)

        if len_word1 == 0:
            return len_word2

        if len_word2 == 0:
            return len_word1

        dp = [[None for _ in range(len_word2 + 1)]for _ in range(len_word1 + 1)]

        # fill last col
        for row in range(len_word1 + 1):
            dp[row][len_word2] = len_word1 - row

        # filling last row
        for col in range(len_word2 + 1):
            dp[len_word1][col] = len_word2 - col

        # dp - top down
        for i in reversed(range(len_word1)):
            for j in reversed(range(len_word2)):
                if word1[i] == word2[j]:
                    dp[i][j] = dp[i+1][j+1]
                else:
					# 1 + compare remaining elements
                    dp[i][j] = 1 + min(
                        dp[i+1][j+1],
                        dp[i+1][j],
                        dp[i][j+1]
                    )

        return dp[0][0]

AlgoExpert/CodingProb/Dp/4. Levenshtein Distance/72.Edit Distance/solution/TopDowndp.py


from primaires.interpreteur.contexte import Contexte
from primaires.joueur.joueur import Joueur

class Suppression(Contexte):
    """Contexte permettant la suppression d'un personnage. Le client
    doit sélectionner l'un de ses personnages. Une confirmation lui
    est demandée (utilisation de la variable self.confirmation),
    puis le personnage ciblé est supprimé.

    Dans tous les cas, après suppression ou annulation de la part du
    client, on revient au contexte de choix d'un personnage.

    """
    nom = "personnage:suppression:suppression"

    def __init__(self, pere):
        """Constructeur du contexte"""
        Contexte.__init__(self, pere)
        self.opts.rci_ctx_prec = "connex:connexion:choix_personnages"
        self.confirmation = -1

    def accueil(self):
        """Message d'accueil du contexte"""
        ret = \
            "\n|tit|--= Suppression d'un personnage =--|ff|\n" \
            "Choisissez le |ent|personnage|ff| à supprimer parmi la liste " \
            "ci-dessous :\n"

        for i, joueur in enumerate(self.pere.compte.joueurs):
            no = " |cmd|" + str(i + 1) + "|ff|"
            ret += "\n" + no + " pour supprimer |ent|{0}|ff|".format( \
                joueur.nom.capitalize())

        return ret

    def get_prompt(self):
        """Message de prompt"""
        if self.confirmation < 0:
            return "Votre choix : "
        else:
            return "Nom du personnage : "

    def interpreter(self, msg):
        """méthode d'interprétation"""
        if self.confirmation < 0:
            if not msg.isdecimal():
                self.pere.envoyer("|err|Votre choix est invalide.|ff|")
            else:
                choix = int(msg) - 1
                if choix < 0 or choix >= len(self.pere.compte.joueurs):
                    self.pere.envoyer("|err|Aucun personnage ne " \
                            "correspond à ce numéro.|ff|")
                else:
                    #On enclenche la procédure de confirmation
                    self.confirmation = choix
                    self.pere.envoyer("Veuillez confirmer la suppression " \
                        "de ce personnage en entrant son |ent|nom|ff|.")
        else:
            msg = msg.lower()
            nom = self.pere.compte.joueurs[self.confirmation].nom.lower()
            if nom != msg:
                self.pere.envoyer("|err|Le nom entré est incorrect|ff|")
                self.migrer_contexte(self.nom)
            else:
                joueur = self.pere.compte.joueurs[self.confirmation]
                del self.pere.compte.joueurs[self.confirmation]
                joueur.detruire()
                self.pere.envoyer("|att|Personnage supprimé !|ff|")
                self.migrer_contexte(self.opts.rci_ctx_prec)


import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_pose = mp.solutions.pose

name = "blue_overhang"
cap = cv2.VideoCapture(f'../test_examples/videos/{name}.mp4')

frame_width = int(cap.get(3))
frame_height = int(cap.get(4))

size = (frame_width, frame_height)

result = cv2.VideoWriter(f'output_{name}.mp4',
                                 cv2.VideoWriter_fourcc(*'MJPG'),
                                                          1, size)
# For webcam input:
with mp_pose.Pose(
    min_detection_confidence=0.5,
    min_tracking_confidence=0.5) as pose:
  while cap.isOpened():
    success, image = cap.read()
    if not success:
      print("Ignoring empty camera frame.")
      # If loading a video, use 'break' instead of 'continue'.
      break

    # To improve performance, optionally mark the image as not writeable to
    # pass by reference.
    image.flags.writeable = False
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    results = pose.process(image)

    # Draw the pose annotation on the image.
    image.flags.writeable = True
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    mp_drawing.draw_landmarks(
        image,
        results.pose_landmarks,
        mp_pose.POSE_CONNECTIONS,
        landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style())
    result.write(image)
    cv2.imshow('MediaPipe Pose', image)
    if cv2.waitKey(5) & 0xFF == 27:
      break
  cap.release()
  result.release()
cv2.destroyAllWindows()


import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.maximize_window()
driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe")
time.sleep(2)
driver.switch_to.frame(driver.find_elements(By.CSS_SELECTOR,'iframe#iframeResult')[0])

text = driver.find_element(By.XPATH,"//h1[contains(text(),'The iframe element')]").text
print('before: ', text)
driver.switch_to.frame(driver.find_element(By.XPATH,"//iframe[@title]"))
driver.find_element(By.XPATH,"//a[@id='w3loginbtn']").click()
driver.switch_to.parent_frame()
text = driver.find_element(By.XPATH,"//h1[contains(text(),'The iframe element')]").text
print('after: ', text)
driver.switch_to.default_content()
# driver.switch_to.parent_frame() both holds good - default content & parent_frame & driver.switch_to.window(driver.window_handles[0])
size = driver.find_element(By.CSS_SELECTOR,'span#framesize').text
print(size)

frames = driver.find_elements(By.TAG_NAME,'iframe')
for frame in frames:
    print(frame.get_attribute("id"))

driver.switch_to.new_window('tab')
driver.get("https://www.inviul.com/getwindowhandle-window-switching/")
# driver.execute_script()
actions = ActionChains(driver)
actions.move_to_element(driver.find_element(By.XPATH,"//span[contains(text(),'Hit Me')]")).perform()
driver.find_element(By.XPATH,"//span[contains(text(),'Hit Me')]").click()
parentWindow = driver.current_window_handle
print(parentWindow)
windows=driver.window_handles
for window in windows:
    if window != parentWindow:
        driver.switch_to.window(window)
    else:
        pass
    print(window)


from bson import ObjectId, errors

class ObjectIdStr(str):
    @classmethod
    def __get_validators__(cls):
        yield cls.validate

    @classmethod
    def validate(cls, v):
        v = ObjectId(v)
        if not isinstance(v, ObjectId):
            raise ValueError("Not a valid ObjectId")
        return str(v)

    @classmethod
    def is_object_id(cls, v):
        try:
            v = ObjectId(v)
        except errors.InvalidId:
            return False
        if not isinstance(v, ObjectId):
            return False
        return True


from patterns.command.callback.handler import Callback

from pathlib import Path

class AsyncCallbackHandler(Callback):

    def on_error(self, message):
        file_name = Path(__file__).parent / 'tests/error.txt'
        with(open(file_name, 'w')) as f:
            f.write(message)

    def on_success(self, message):
        file_name = Path(__file__).parent / 'tests/success.txt'
        with(open(file_name, 'w')) as f:
            f.write(message)


count = 1
while count <= 5:

    try:
        number = int(input('Enter a number: '))
        print('100 divide by a number {} is {}'.format(number, 100/number))
        break
    except ZeroDivisionError:
        print('Please do not provide 0 as an input')
    count += 1


import pygame
import os
import random

OBSTACLE_IMG = [pygame.transform.scale(pygame.image.load(os.path.join("imgs", "tree-sm-1.png")), (30, 40)),
                pygame.transform.scale(pygame.image.load(os.path.join("imgs", "tree-sm-2.png")), (50, 40)),
                pygame.transform.scale(pygame.image.load(os.path.join("imgs", "tree-md-1.png")), (40, 47)),
                pygame.transform.scale(pygame.image.load(os.path.join("imgs", "tree-lg-3.png")), (70, 55)),
                pygame.transform.scale(pygame.image.load(os.path.join("imgs", "tree-lg-4.png")), (90, 60))]

class Obstacle:

    def __init__(self, prev_x, y):
        self.x = prev_x + random.randint(300, 700)
        self.y = y
        self.img = OBSTACLE_IMG[random.randint(0, len(OBSTACLE_IMG)-1)]
        self.passed = False

    def move(self):
        self.x -= self.VEL

    def draw(self, win):
        win.blit(self.img, (self.x, self.y - self.img.get_rect().height))

    def collide(self, dino):
        dino_mask = dino.get_mask()
        obstacle_mask = pygame.mask.from_surface(self.img)

        obstacle_offset = (int(self.x) - dino.x + dino.img.get_rect().width, self.y - round(dino.y))

        x_point = dino_mask.overlap(obstacle_mask, obstacle_offset)

        if x_point:
            return True

        return False

    def initVel(self):
        Obstacle.VEL = 4

    def updateVel(self, dv):
        Obstacle.VEL *= (1+dv)


from typing import Iterable, List, Tuple, Union

import numpy as np
import paddle
from paddlenlp.generation import LogitsProcessor
from paddlenlp.transformers import PretrainedTokenizer

HistoryType = List[Tuple[str, str]]
TokensType = List[int]

def get_stop_words_ids(chat_format, tokenizer):
    if chat_format == "raw":
        stop_words_ids = [tokenizer.encode("Human:"), [tokenizer.eod_id]]
    elif chat_format == "chatml":
        stop_words_ids = [[tokenizer.im_end_id], [tokenizer.im_start_id]]
    else:
        raise NotImplementedError(f"Unknown chat format {chat_format!r}")
    return stop_words_ids

def make_context(
    tokenizer: PretrainedTokenizer,
    query: str,
    history: List[Tuple[str, str]] = None,
    system: str = "",
    max_window_size: int = 6144,
    chat_format: str = "chatml",
):
    if history is None:
        history = []
    if chat_format == "chatml":
        im_start, im_end = "<|im_start|>", "<|im_end|>"
        im_start_tokens = [tokenizer.im_start_id]
        im_end_tokens = [tokenizer.im_end_id]
        nl_tokens = tokenizer.encode("\n")["input_ids"]

        def _tokenize_str(role, content):
            return (
                f"{role}\n{content}",
                tokenizer.encode(role, allowed_special=set(tokenizer.IMAGE_ST))["input_ids"]
                + nl_tokens
                + tokenizer.encode(content, allowed_special=set(tokenizer.IMAGE_ST))["input_ids"],
            )

        system_text, system_tokens_part = _tokenize_str("system", system)
        system_tokens = im_start_tokens + system_tokens_part + im_end_tokens
        raw_text = ""
        context_tokens = []
        for turn_query, turn_response in reversed(history):
            query_text, query_tokens_part = _tokenize_str("user", turn_query)
            query_tokens = im_start_tokens + query_tokens_part + im_end_tokens
            if turn_response is not None:
                response_text, response_tokens_part = _tokenize_str("assistant", turn_response)
                response_tokens = im_start_tokens + response_tokens_part + im_end_tokens
                next_context_tokens = nl_tokens + query_tokens + nl_tokens + response_tokens
                prev_chat = f"\n{im_start}{query_text}{im_end}\n{im_start}{response_text}{im_end}"
            else:
                next_context_tokens = nl_tokens + query_tokens + nl_tokens
                prev_chat = f"\n{im_start}{query_text}{im_end}\n"
            current_context_size = len(system_tokens) + len(next_context_tokens) + len(context_tokens)
            if current_context_size < max_window_size:
                context_tokens = next_context_tokens + context_tokens
                raw_text = prev_chat + raw_text
            else:
                break
        context_tokens = system_tokens + context_tokens
        raw_text = f"{im_start}{system_text}{im_end}" + raw_text
        context_tokens += (
            nl_tokens
            + im_start_tokens
            + _tokenize_str("user", query)[1]
            + im_end_tokens
            + nl_tokens
            + im_start_tokens
            + tokenizer.encode("assistant")["input_ids"]
            + nl_tokens
        )
        raw_text += f"\n{im_start}user\n{query}{im_end}\n{im_start}assistant\n"
    elif chat_format == "raw":
        raw_text = query
        context_tokens = tokenizer.encode(raw_text)["input_ids"]
    else:
        raise NotImplementedError(f"Unknown chat format {chat_format!r}")
    return raw_text, context_tokens

def _decode_default(
    tokens: List[int],
    *,
    stop_words: List[str],
    eod_words: List[str],
    tokenizer: PretrainedTokenizer,
    raw_text_len: int,
    verbose: bool = False,
    return_end_reason: bool = False,
    errors: str = "replace"
):
    trim_decode_tokens = tokenizer.decode(tokens, errors=errors)[raw_text_len:]
    if verbose:
        print("\nRaw Generate: ", trim_decode_tokens)
    end_reason = f"Gen length {len(tokens)}"
    for stop_word in stop_words:
        trim_decode_tokens = trim_decode_tokens.replace(stop_word, "").strip()
    for eod_word in eod_words:
        if eod_word in trim_decode_tokens:
            end_reason = f"Gen {eod_word!r}"
        trim_decode_tokens = trim_decode_tokens.split(eod_word)[0]
    trim_decode_tokens = trim_decode_tokens.strip()
    if verbose:
        print("\nEnd Reason:", end_reason)
        print("\nGenerate: ", trim_decode_tokens)
    if return_end_reason:
        return trim_decode_tokens, end_reason
    else:
        return trim_decode_tokens

def _decode_chatml(
    tokens: List[int],
    *,
    stop_words: List[str],
    eod_token_ids: List[int],
    tokenizer: PretrainedTokenizer,
    raw_text_len: int,
    context_length: int,
    verbose: bool = False,
    return_end_reason: bool = False,
    errors: str = "replace"
):
    end_reason = f"Gen length {len(tokens)}"
    eod_token_idx = context_length
    for eod_token_idx in range(context_length, len(tokens)):
        if tokens[eod_token_idx] in eod_token_ids:
            end_reason = f"Gen {tokenizer.decode([tokens[eod_token_idx]])!r}"
            break
    trim_decode_tokens = tokenizer.decode(tokens[context_length:eod_token_idx], errors=errors)
    if verbose:
        print("\nRaw Generate w/o EOD:", tokenizer.decode(tokens, errors=errors))
        print("\nRaw Generate:", trim_decode_tokens)
        print("\nEnd Reason:", end_reason)
    for stop_word in stop_words:
        trim_decode_tokens = trim_decode_tokens.replace(stop_word, "").strip()
    trim_decode_tokens = trim_decode_tokens.strip()
    if verbose:
        print("\nGenerate:", trim_decode_tokens)
    if return_end_reason:
        return trim_decode_tokens, end_reason
    else:
        return trim_decode_tokens

def decode_tokens(
    tokens: Union[paddle.Tensor, TokensType],
    tokenizer: PretrainedTokenizer,
    raw_text_len: int,
    context_length: int,
    chat_format: str,
    verbose: bool = False,
    return_end_reason: bool = False,
    errors: str = "replace",
) -> str:
    if paddle.is_tensor(x=tokens):
        tokens = tokens.cpu().numpy().tolist()
    if chat_format == "chatml":
        return _decode_chatml(
            tokens,
            stop_words=[],
            eod_token_ids=[tokenizer.im_start_id, tokenizer.im_end_id],
            tokenizer=tokenizer,
            raw_text_len=raw_text_len,
            context_length=context_length,
            verbose=verbose,
            return_end_reason=return_end_reason,
            errors=errors,
        )
    elif chat_format == "raw":
        return _decode_default(
            tokens,
            stop_words=["<|endoftext|>"],
            eod_words=["<|endoftext|>"],
            tokenizer=tokenizer,
            raw_text_len=raw_text_len,
            verbose=verbose,
            return_end_reason=return_end_reason,
            errors=errors,
        )
    else:
        raise NotImplementedError(f"Unknown chat format {chat_format!r}")

class StopWordsLogitsProcessor(LogitsProcessor):
    """
    :class:`transformers.LogitsProcessor` that enforces that when specified sequences appear, stop geration.

    Args:
        stop_words_ids (:obj:`List[List[int]]`):
            List of list of token ids of stop ids. In order to get the tokens of the words
            that should not appear in the generated text, use :obj:`tokenizer(bad_word,
            add_prefix_space=True).input_ids`.
        eos_token_id (:obj:`int`):
            The id of the `end-of-sequence` token.
    """

    def __init__(self, stop_words_ids: Iterable[Iterable[int]], eos_token_id: int):
        if not isinstance(stop_words_ids, List) or len(stop_words_ids) == 0:
            raise ValueError(f"`stop_words_ids` has to be a non-emtpy list, but is {stop_words_ids}.")
        if any(not isinstance(bad_word_ids, list) for bad_word_ids in stop_words_ids):
            raise ValueError(f"`stop_words_ids` has to be a list of lists, but is {stop_words_ids}.")
        if any(
            any(not isinstance(token_id, (int, np.integer)) or token_id < 0 for token_id in stop_word_ids)
            for stop_word_ids in stop_words_ids
        ):
            raise ValueError(
                f"Each list in `stop_words_ids` has to be a list of positive integers, but is {stop_words_ids}."
            )
        self.stop_words_ids = list(filter(lambda bad_token_seq: bad_token_seq != [eos_token_id], stop_words_ids))
        self.eos_token_id = eos_token_id
        for stop_token_seq in self.stop_words_ids:
            assert len(stop_token_seq) > 0, "Stop words token sequences {} cannot have an empty list".format(
                stop_words_ids
            )

    def __call__(self, input_ids: paddle.Tensor, scores: paddle.Tensor) -> paddle.Tensor:
        stopped_samples = self._calc_stopped_samples(input_ids)
        for i, should_stop in enumerate(stopped_samples):
            if should_stop:
                scores[i, self.eos_token_id] = float(2**15)
        return scores

    def _tokens_match(self, prev_tokens: paddle.Tensor, tokens: List[int]) -> bool:
        if len(tokens) == 0:
            return True
        elif len(tokens) > len(prev_tokens):
            return False
        elif prev_tokens[-len(tokens) :].tolist() == tokens:
            return True
        else:
            return False

    def _calc_stopped_samples(self, prev_input_ids: Iterable[int]) -> Iterable[int]:
        stopped_samples = []
        for prev_input_ids_slice in prev_input_ids:
            match = False
            for stop_token_seq in self.stop_words_ids:
                if self._tokens_match(prev_input_ids_slice, stop_token_seq):
                    match = True
                    break
            stopped_samples.append(match)
        return stopped_samples


import com.google.common.collect.Lists as Lists
import java.lang.Object as Object
import java.nio.file.Paths as Paths
import org.biojava.nbio.structure.AminoAcidImpl as Group
import org.biojava.nbio.structure.AtomImpl as Atom
import org.biojava.nbio.structure.ChainImpl as ChainImpl
import org.biojava.nbio.structure.Element as Element
import org.biojava.nbio.structure.PDBCrystallographicInfo as PdbCryst
import org.biojava.nbio.structure.PDBHeader as PdbHeader
import org.biojava.nbio.structure.ResidueNumber as ResidueNumber
import org.biojava.nbio.structure.xtal.BravaisLattice as BravaisL
import org.biojava.nbio.structure.xtal.SpaceGroup as SpaceGroup
import re as re

import org.mdconverter.api.ReadStructure as ReadStructure
import org.mdconverter.api.plugin.InvalidInputException as Invalid

class ReadFile(ReadStructure, Object):
    class Pattern:
        HEADER_DEF_PATTERN = "^@<[A-Z]*>MOLECULE$"
        HEADER_PATTERN = "^([ \\t]*[0-9]+){5}$"
        ATOM_DEF_PATTERN = "^@<[A-Z]*>ATOM$"
        ATOM_PATTERN = "^[ \\t]*[0-9]+ [A-Z0-9]{1,}([ \\t]*-?[0-9]+.[0-9]+){3} [A-z0-9]+[ \\t]+[0-9] [A-Z]{3,}[ \\t]+-?[0-9]+.[0-9]+$"
        BOND_DEF_PATTERN = "^@<[A-Z]*>BOND$"
        STRUCTURE_DEF_PATTERN = "^@<[A-Z]*>SUBSTRUCTURE$"

    highestX = 0
    highestY = 0
    highestZ = 0
    lowestX = 0
    lowestY = 0
    lowestZ = 0

    def __init__(self):
        global highestX, highestY, highestZ, lowestX, lowestY, lowestZ
        highestX = 0
        highestY = 0
        highestZ = 0
        lowestX = 0
        lowestY = 0
        lowestZ = 0

    def readFileToStructure(self, path, structure):
        header = PdbHeader()
        header.setTitle(Paths.get(path).getFileName().toString())
        structure.setPDBHeader(header)
        model = ReadFile.getModelFromFile(self, path)
        structure.setChains(Lists.newArrayList(model))
        info = PdbCryst()
        info.setSpaceGroup(SpaceGroup(0, 1, 1, "P 1", "P 1", BravaisL.CUBIC))
        info.setCrystalCell(ReadFile.getBox(self, info.getSpaceGroup().getBravLattice().getExampleUnitCell()))
        header.setCrystallographicInfo(info)
        return structure

    def getModelFromFile(self, path):
        with open(path) as f:
            all_lines = f.readlines()
        chain = ChainImpl()
        chain.setAtomGroups(ReadFile.getGroups(self, all_lines))
        return chain

    def getGroups(self, all_lines):
        groups = []
        area = ''
        atom_count = 0
        atom_check = 0
        bond_count = 0
        sub_count = 0
        sub_check = 0
        for idx in range(len(all_lines)):
            if area == '':
                m = re.match(ReadFile.Pattern.HEADER_DEF_PATTERN, all_lines[idx])
                if m is not None:
                    area = 'header'
                    continue
                else:
                    m = re.match(ReadFile.Pattern.ATOM_DEF_PATTERN, all_lines[idx])
                    if m is not None:
                        area = 'atom'
                        continue
                    else:
                        m = re.match(ReadFile.Pattern.STRUCTURE_DEF_PATTERN, all_lines[idx])
                        if m is not None:
                            area = 'sub'
                            continue
                        else:
                            continue
            if area == 'header':
                m = re.match(ReadFile.Pattern.HEADER_PATTERN, all_lines[idx])
                if m is not None:
                    words = all_lines[idx].split()
                    atom_count = int(words[0])
                    bond_count = int(words[1])
                    sub_count = int(words[2])
                    for i in range(sub_count):
                        groups.append(Group())
                    area = ''
                else:
                    continue
            if area == 'atom':
                m = re.match(ReadFile.Pattern.ATOM_PATTERN, all_lines[idx])
                if m is not None:
                    atom_check += 1
                    words = all_lines[idx].split()
                    atom = ReadFile.getAtom(self, words)
                    if len(words) > 5:
                        group = groups[int(words[6]) - 1]
                        if group.getPDBName() is None:
                            group.setPDBName(words[7])
                            test = ResidueNumber.fromString(words[6])
                            test.setSeqNum(int(words[6]))
                            group.setResidueNumber(test)
                        group.addAtom(atom)
                    else:
                        groups[0].addAtom(atom)
                else:
                    if atom_check != atom_count:
                        raise Invalid('Atom definition count do not equal found atoms')
                    area = ''
                    idx -= 1
                    continue
            if area == 'sub':
                sub_check = 1
                group = groups[sub_check - 1]
                words = all_lines[idx].split()
                if group.getPDBName is None:
                    group.setPDBName(words[1])
                    group.setResidueNumber(int(words[0]))
        if sub_check != sub_count:
            raise Invalid('Substructure definition count do not equal found substructures')
        return groups

    def getAtom(self, words):
        global highestX, highestY, highestZ, lowestX, lowestY, lowestZ
        atom = Atom()
        atom.setOccupancy(1)
        atom.setPDBserial(int(words[0]))
        atom.setName(words[1])
        x = float(words[2])
        y = float(words[3])
        z = float(words[4])
        if highestX <= x:
            highestX = x
        elif lowestX >= x:
            lowestX = x
        if highestY <= y:
            highestY = y
        elif lowestY >= y:
            lowestY = y
        if highestZ <= z:
            highestZ = z
        elif lowestZ >= z:
            lowestZ = z
        atom.setX(x)
        atom.setY(y)
        atom.setZ(z)
        atom.setAltLoc(' ')
        atom.setElement(Element.valueOf(atom.getName()[:1]))
        return atom

    def getBox(self, cell):
        global highestX, highestY, highestZ, lowestX, lowestY, lowestZ
        doubles = [highestX + (-lowestX), highestY + (-lowestY), highestZ + (-lowestZ)]
        cell.setA(doubles[0])
        cell.setB(doubles[1])
        cell.setC(doubles[2])
        return cell


from pyramid.response import FileResponse

from nextgisweb.env import env

from nextgisweb.resource import ResourceScope, resource_factory

from .model import FormbuilderForm

def formbuilder_form_ngfp(resource, request):
    request.resource_permission(ResourceScope.read)

    fn = env.file_storage.filename(resource.ngfp_fileobj)

    response = FileResponse(fn, request=request)
    response.content_disposition = "attachment; filename=%d.ngfp" % resource.id

    return response

def setup_pyramid(comp, config):
    config.add_route(
        "formbuilder.formbuilder_form_ngfp",
        "/api/resource/{id:uint}/ngfp",
        factory=resource_factory,
    ).get(formbuilder_form_ngfp, context=FormbuilderForm)


#!/usr/bin/env python3

from sys import argv

if __name__ == '__main__':
    if len(argv) != 2:
        exit(0)
    words = []
    with open(argv[1]) as input_file:
        for line in input_file:
            word = line[:-1]
            words.append(word)
    words.sort()
    for word in words:
        print('{}'.format(word))


from django.shortcuts import get_object_or_404
from rest_framework import viewsets
from rest_framework import mixins, filters
from rest_framework.pagination import LimitOffsetPagination
from rest_framework.permissions import IsAuthenticatedOrReadOnly

from posts.models import Follow, Group, Post
from .permissions import AuthorOrReadOnly
from .serializers import (
    FollowSerializer,
    CommentSerializer,
    GroupSerializer,
    PostSerializer
)

class PostViewSet(viewsets.ModelViewSet):
    queryset = Post.objects.all()
    serializer_class = PostSerializer
    permission_classes = (AuthorOrReadOnly,)
    pagination_class = LimitOffsetPagination

    def perform_create(self, serializer):
        serializer.save(author=self.request.user)

class CommentViewSet(viewsets.ModelViewSet):
    serializer_class = CommentSerializer
    permission_classes = (AuthorOrReadOnly,)

    def get_queryset(self):
        post = get_object_or_404(Post, id=self.kwargs.get('post_id'))
        queryset = post.comments
        return queryset

    def perform_create(self, serializer):
        post = get_object_or_404(Post, id=self.kwargs.get('post_id'))
        serializer.save(author=self.request.user, post=post)

class GroupViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Group.objects.all()
    serializer_class = GroupSerializer
    permission_classes = (IsAuthenticatedOrReadOnly,)

class ListCreateViewset(
    mixins.ListModelMixin,
    mixins.CreateModelMixin,
    viewsets.GenericViewSet
):
    pass

class FollowViewSet(ListCreateViewset):
    serializer_class = FollowSerializer
    filter_backends = (filters.SearchFilter,)
    search_fields = ('following__username',)

    def get_queryset(self):
        return Follow.objects.filter(user=self.request.user)

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)


# Model class must be defined somewhere
import torch
from torchvision import transforms
from configs import *
import cv2
from PIL import Image

transform = transforms.Compose([
            transforms.Resize((IMAGE_SIZE, IMAGE_SIZE)),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406],
                             [0.229, 0.224, 0.225]),
            ])
image = cv2.imread("./data_real/image/29C67895-1678850783658.jpg")

def process_img(opencv_image):
    color_converted = cv2.cvtColor(opencv_image, cv2.COLOR_BGR2RGB)
    pil_image=Image.fromarray(color_converted)
    img_t = transform(pil_image)
    return torch.unsqueeze(img_t, 0)

batch_t = process_img(image).to("cuda")
model = torch.load("best.pt").to("cuda")
model.train()
out = model(batch_t)
list_coor = out.tolist()[0]
print(list_coor)

# draw
colors = [(255, 0, 0), (0, 255, 0), (0, 0 , 255), (255, 255, 0)]
h, w = image.shape[:2]
for i in range(4):
    i=i*2
    cv2.circle(image, (int(list_coor[i]*w), int(list_coor[i+1]*h)), 3, colors[1], 3)
cv2.imshow("image",image)
cv2.waitKey(0)