__version__ = '1.0.0' # current version

from tkinter import filedialog
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
import tkinter as tk
from ttkbootstrap.toast import ToastNotification
# other required imports
import pygame
import pydub
import threading

# create a window
root = ttk.Window(themename="darkly",  title="PyMusic V1.0")
# set the alpha of the window
root.attributes("-alpha", 0.8)
# set geometry of main window
root.geometry("307x220")
# set the icon of the window
#root.iconphoto(False, tk.Image(file='music.png'))
style = ttk.Style(theme='vapor')

###################
# Functions for the buttons
###################
def browseFiles():
    global file_path
    # plays all music files
    file_path = filedialog.askopenfilename(filetypes=[("Music Files", "*.mp3 *.wav *.ogg")])
    #file_path = filedialog.askopenfilename(filetypes=[("Music Files", "*.mp3")])
    if file_path:
        # get name of the song without the file extension
        SongTitleLbl.config(text="Song Title:  " + file_path.split("/")[-1])
        # renove the file extension from the file name
        SongTitleLbl.config(text="Song Title:  " + file_path.split("/")[-1].split(".")[0])
        # set song title label to the song title remove file type extension from the file name

 #thread to update time remaining
def  updatRemainder(file_path):
    thread = threading.Thread(target=updateRemainingTime, args=(file_path,))
    thread.start()

def updateRemainingTime(file_path):
    # get the length of the song
    audio = pydub.AudioSegment.from_mp3(file_path)
    length = len(audio)
    # get the current time of the song
    current_time = pygame.mixer.music.get_pos()
    # calculate the remaining time
    remaining_time = length - current_time
    # convert the time to minutes and seconds
    remaining_time = remaining_time / 1000
    minutes = remaining_time // 60
    seconds = remaining_time % 60
    # set the time remaining label to the remaining time
    TimeRemainingLbl.config(text="Time Remaining: " + str(int(minutes)) + ":" + str(int(seconds)))
    # call this function to update remaining time after every 100 milliseconds
    TimeRemainingLbl.after(100, updateRemainingTime, file_path)

def songPos(value):
    # using threading to update the slider
    thread = threading.Thread(target=findPositionThread , args=(value,))
    thread.start()
 # a function to find the postion of a song using the slider
def findPositionThread(value):
    # current postion and time in a song in milliseconds
    currentPostion = pygame.mixer.music.get_pos()
    currentTime = pygame.mixer.music.get_pos()
    # convert the value of the slider to milliseconds
    value = int(value) * 1000
    # if the value of the slider is greater than the current time of the song
    if value > currentTime:
        # calculate the difference between the value of the slider and the current time
        difference = value - currentTime
        # add the difference to the current time to get the new time
        newTime = currentTime + difference
        # set the new time of the song
        pygame.mixer.music.set_pos(newTime)
        # play current postion
        pygame.mixer.music.play(newTime)
    # if the value of the slider is less than the current time of the song
    elif value < currentTime:
        # calculate the difference between the value of the slider and the current time
        difference = currentTime - value
        # subtract the difference from the current time to get the new time
        newTime = currentTime - difference
        # set the new time of the song
        pygame.mixer.music.set_pos(newTime)
        # play from current time
        pygame.mixer.music.play(newTime)

def changeVolumeThread(value):
    # change the volume of the music
    pygame.mixer.music.set_volume(float(value) / 100)

# function to use keyboard to change volume
def keyboardVolume(event):
    # get the current volume
    currentVolume = pygame.mixer.music.get_volume()
    # if keyboard up is pressed increase the volume
    if event.keysym == "Up":
        # increase the volume by 0.1
        pygame.mixer.music.set_volume(currentVolume + 0.1)
        # set the volume slider to the new volume
        volumeSlider.set(pygame.mixer.music.get_volume() * 100)
    # if keyboard down is pressed decrease the volume
    elif event.keysym == "Down":
        # decrease the volume by 0.1
        pygame.mixer.music.set_volume(currentVolume - 0.1)
        # set the volume slider to the new volume
        volumeSlider.set(pygame.mixer.music.get_volume() * 100)

# create a function that uses threading to change the volume
def changeVolume(value):
    # create a thread
    thread = threading.Thread(target=changeVolumeThread, args=(value,))
    # start the thread
    thread.start()

def playMusic():
    pygame.init()
    updatRemainder(file_path)
    pygame.mixer.init()
    pygame.mixer.music.load(file_path)
    pygame.mixer.music.play()
    song = file_path.split("/")[-1].split(".")[0]
    toastsong("Now Playing", song, 3000)
    pass

def  pcheck():
    if pygame.mixer.music.get_busy() == True:
        pauseMusic()
    else:
        resumeMusic()
    root.after(100, pcheck)

def pauseMusic():
    pygame.mixer.music.pause()

def resumeMusic():
    pygame.mixer.music.unpause()
    pass

def stopMusic():
    pygame.mixer.music.stop()
    pass
# toast the song title
def toastsong(title, message, duration):
    toast = ToastNotification(
    title=title,
    message=message,
    duration=3000,)
    toast.show_toast()

####################
# add controls to window
#####################

# song title label
SongTitleframe = ttk.Labelframe(bootstyle="primary", text="Song Title")
# show in the center of the window
SongTitleframe.pack(fill=tk.BOTH, expand=True)
# create a label for file name and add it to frame
SongTitleLbl = ttk.Label(SongTitleframe, text="")
# show in the center of the window
SongTitleLbl.pack(fill=tk.BOTH, expand=True)

#  Time Remaining in a song
TimeRemainingframe = ttk.Labelframe(bootstyle="primary", text="Time Remaining")
# show in the center of the window
TimeRemainingframe.pack(fill=tk.BOTH, expand=True)
# create label for time remaining and add it to frame
TimeRemainingLbl = ttk.Label(TimeRemainingframe, text="")
# show in the center of the window
TimeRemainingLbl.pack(fill=tk.BOTH, expand=True)

# songp postion slider
songPositionSlider = tk.Scale(root, from_=0, to=100, orient=tk.HORIZONTAL, command=songPos , length=300, width=5, resolution=1)
# add song position slider to window
songPositionSlider.pack()
sep1 = ttk.Separator()
# add separator to window
sep1.pack(fill=tk.BOTH, expand=True)

# Volume slider & label
volumelable = ttk.Label(bootstyle="primary", text="Volume")
volumelable.pack()
volumeSlider = tk.Scale(root, from_=0, to=100, orient=tk.HORIZONTAL, command=changeVolumeThread , length=200, width=5, resolution=1)
# set volme to 100
volumeSlider.set(100)
volumeSlider.pack()

# sperator
sep = ttk.Separator()
# add separator to window
sep.pack(fill=tk.BOTH, expand=True)

# browse and open file button
browseBtn = ttk.Button(bootstyle="primary", text="Open File", command=browseFiles)
# add browse button to to side of the play button
browseBtn.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
#browseBtn.pack(fill=tk.BOTH, expand=True)

# play button
playbtn = ttk.Button(bootstyle="primary", text="Play", command=playMusic)
# add to the side of the browse button
playbtn.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

# pause button
pauseBtn = ttk.Button(bootstyle="primary", text="Pause", command=pcheck)
# add pause button to window to the side of the play button
pauseBtn.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

# stop button
stopBtn = ttk.Button(bootstyle="primary", text="Stop", command=stopMusic)
# add stop button to window to the side of the pause button
stopBtn.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

# call keyboardVolume function when  the up or down key is pressed
root.bind("<Up>", keyboardVolume)
# call keyboardVolume function when  the up or down key is pressed
root.bind("<Down>", keyboardVolume)
# get the current time of the song
# check if music is playing
# main loop
root.mainloop()


# -*- coding: utf-8 -*-
"""
Created on Sat Mar  5 20:24:50 2016

@author: alex
"""

from AlexRobotics.dynamic import Manipulator    as M
from AlexRobotics.control import ComputedTorque as CTC

import matplotlib.pyplot as plt
import numpy as np

""" Define system """

# Define dynamic system
R  =  M.ThreeLinkManipulator()

# Define controller
CTC_controller      = CTC.SlidingModeController( R )
CTC_controller.lam  = 1
CTC_controller.D    = 10

# Goal
q_d                 = np.array([ np.pi, -np.pi * 0.25 , -np.pi * 0.25 ])
dq_d                = np.zeros( R.dof )
x_d                 = R.q2x( q_d , dq_d)
CTC_controller.goal =  x_d

# Asign feedback law to the dynamic system
R.ctl = CTC_controller.ctl

""" Simulation and plotting """

# Ploting a trajectory
x0   = [0,1,1,0,0,0]
tf   = 10
dt   = 0.001
n    = int( tf / dt ) + 1

R.plot3DAnimation( x0 , tf , n , solver = 'euler' )

R.Sim.plot_CL( 'x' )
R.Sim.plot_CL( 'u' )

# Hold figures alive
plt.show()


import sqlite3
from sqlite3 import Error
from datetime import datetime
import re

# ===============>> CONEXÃO <<===============
conn = sqlite3.connect('Funcionarios.db')
cursor = conn.cursor()

# ===============>> VARIAVEIS G <<===============

Data = datetime.now().strftime('%d-%m-%Y')
Hora = datetime.now().strftime('%H:%M:%S')

cVermelho = "\033[1;31m"
cAmarelo = '\033[1;33m'
cVerde = '\033[1;32m'
cAzul = '\033[1;34m'
ResetF = "\033[0;0m"

# ===============>> INICIO <<===============

# =====>> MENU <<=====
def Menu():
  print(cAzul+"\n=========="+cVermelho+" MENU "+cAzul+"=========="+ResetF)
  print("O que deseja fazer? ")
  print("1 = Cadastrar Funcionario")
  print("2 = Consultar Funcionario")
  print("3 = Bater Ponto Entrada")
  print("4 = Bater Ponto Saída")
  print("5 = Fechar")
  print(cAzul+"=========================="+ResetF)
  resposta = input("\nO que deseja fazer: ")

  if resposta == "1":
    Cadastrar_Func()
  elif resposta == "2" :
    Consultar_Func()
  elif resposta == "3" :
    Ponto_Entrada()
  elif resposta == "4" :
    Ponto_Saida()
  elif resposta == "5" :
    exit()
  else :
    print("Valor inválido")
  Menu()

# =====>> CADASTRAR FUNCIONARIO <<=====

def Cadastrar_Func():
  print('\nIremos pedir algumas informações!\n')

# NOME
  while True:
    Nome = input("Nome: ")
    Quant = len(Nome)
    if Nome.isnumeric():
      print('\nFormato errado '+cVermelho+'(Somente Letras)\n'+ResetF)
    else:
      if(Quant >= 3):
        break
      else:
        print("\nO numero de digitos deve ser maior ou igual a " +cVermelho+"3"+ResetF+"!\n")

# NÚMERO DO CRACHÁ
  while True:
    N_Cracha = input("Número do Crachá "+cVermelho+"(Somente Números)"+ResetF+": ")
    Quant = len(N_Cracha)
    if N_Cracha.isnumeric():
      if(Quant > 0):
        break
      else:
        print("\nO numero de digitos deve ser maior que " +cVermelho+"0"+ResetF+"!\n")
    else:
      print('\nFormato errado '+cVermelho+'(Somente Numeros)\n'+ResetF)

# CPF
  while True:
    CPF = input("CPF "+cVermelho+"(Somente Números)"+ResetF+": ")
    Quant = len(CPF)
    if CPF.isnumeric():
      #if(Quant == 11):
      if (10 < Quant < 12):
        break
      else:
        print("\nO numero de digitos deve ser igual a " +cVermelho+"11"+ResetF+"!\n")
    else:
      print('\nFormato errado '+cVermelho+'(Somente Numeros)\n'+ResetF)

# E-MAIL
  while True:
    email = input("E-mail: ").lower()
    proc = re.search(".com", email)
    if(proc):
      break
      #print('\nTudo certo!\n')
    else:
      print('\nFormato de E-mail '+cVermelho+'ERRADO!\n'+ResetF)

# TELEFONE
  while True:
    Fone = input("Numero de Telefone: ")
    Quant = len(Fone)
    if Fone.isnumeric():
      if(Quant >= 8):
        break
      else:
        print("\nNumero errado. Exemplo: " +cVermelho+"22223333"+ResetF+"!\n")
    else:
      print('\nFormato errado '+cVermelho+'(Somente Números)\n'+ResetF)

# CIDADE
  while True:
    Cidade = input("Cidade: ")
    Quant = len(Cidade)
    if Cidade.isnumeric():
      print('\nFormato errado '+cVermelho+'(Somente Letras)\n'+ResetF)
    else:
      if(Quant >= 3):
        break
      else:
        print("\nO numero de digitos deve ser maior ou igual a " +cVermelho+"3"+ResetF+"!\n")

  Atendente = input("Atendente: ")

# PRINTs
  print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
  print(cAzul + "Nome:" + ResetF + " %s" % Nome)
  print(cAzul + "Cracha:" + ResetF + " %s" % N_Cracha)
  print(cAzul + "CPF:" + ResetF + " %s" % CPF)
  print(cAzul + "Email:" + ResetF + " %s" % email)
  print(cAzul + "Telefone:" + ResetF + " %s" % Fone)
  print(cAzul + "Cidade:" + ResetF + " %s" % Cidade)

 # Sql = "INSERT INTO Funcionarios (Nome, N_Cracha, CPF, Email, Fone, Cidade) VALUES ('"+Nome+"', '"+N_Cracha+"', '"+CPF+"', '"+email+"', '"+Fone+"', '"+Cidade+"')"

  Sql = " INSERT INTO Funcionarios(Nome, N_Cracha, CPF, Email, Fone, Cidade) VALUES(?,?,?,?,?,?)", (Nome, N_Cracha, CPF, email, Fone, Cidade,)

  cursor.execute(Sql)
  conn.commit()

  print(cVermelho + '\nCadastro realizado com sucesso!\n\n\n'+ ResetF)

# REALIZAR OUTRO CADASTRO
  a = input('Deseja realizar outro?['+cVerde+'S'+ResetF+'/'+cVermelho+'N'+ResetF+'] ').lower()
  if a == 's':
    Cadastrar_Func()
  else:
    Menu()

# =====>> CONSULTAR FUNCIONARIO <<=====
def Consultar_Func():
  while True:
    N_Cracha = input("Número do Crachá "+cVermelho+"(Somente Números)"+ResetF+": ")
    Quant = len(N_Cracha)
    if N_Cracha.isnumeric():
      if(Quant > 1):
        break
      else:
        print("\nO numero de digitos deve ser maior ou igual a " +cVermelho+"1"+ResetF+"!\n")
    else:
      print('\nFormato errado '+cVermelho+'(Somente Numeros)\n'+ResetF)

  #Sql = "SELECT * FROM Funcionarios WHERE (N_Cracha) VALUES ('"+N_Cracha+"')"
  Sql = "SELECT * FROM Funcionarios WHERE N_Cracha=?", (N_Cracha,)

  cursor.execute(Sql)

  rows = cursor.fetchall()

  for row in rows:
    print(row)

# =====>> PONTO ENTRADA FUNCIONARIO <<=====
def Ponto_Entrada():
  print("Teste3")

# =====>> PONTO SAIDA FUNCIONARIO <<=====
def Ponto_Saida():
  print("Teste4")

Menu()


"""Add IOC type

Revision ID: 6a3b3b627d45
Revises:
Create Date: 2022-01-01 23:40:35.283005

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.engine.reflection import Inspector
from sqlalchemy import engine_from_config
from sqlalchemy.engine import reflection

# revision identifiers, used by Alembic.
revision = '6a3b3b627d45'
down_revision = None
branch_labels = None
depends_on = None

def upgrade():
    # IOC types is created by post init if not existing
    # Now issue changes on existing tables and migrate IOC types
    # Add column ioc_type_id to IOC if not existing
    if not _table_has_column('ioc', 'ioc_type_id'):
        op.add_column('ioc',
                      sa.Column('ioc_type_id', sa.Integer, sa.ForeignKey('ioc_type.type_id'))
                      )

        # Add the foreign key of ioc_type to ioc
        op.create_foreign_key(
            constraint_name='ioc_ioc_type_id',
            source_table="ioc",
            referent_table="ioc_type",
            local_cols=["ioc_type_id"],
            remote_cols=["type_id"])

    if _table_has_column('ioc', 'ioc_type'):
        # Set schema and make migration of data
        t_ioc = sa.Table(
            'ioc',
            sa.MetaData(),
            sa.Column('ioc_id', sa.Integer, primary_key=True),
            sa.Column('ioc_value', sa.Text),
            sa.Column('ioc_type', sa.Unicode(length=50)),
            sa.Column('ioc_type_id', sa.ForeignKey('ioc_type.type_id')),
            sa.Column('ioc_tags', sa.Text),
            sa.Column('user_id', sa.ForeignKey('user.id')),
            sa.Column('ioc_misp', sa.Text),
            sa.Column('ioc_tlp_id', sa.ForeignKey('tlp.tlp_id'))
        )
        to_update = [('Domain', 'domain'), ('IP', 'ip-any'), ('Hash', 'other'), ('File', 'filename'),
                     ('Path', 'file-path'), ('Account', 'account'), ("Other", 'other')]

        # Migrate existing IOCs
        for src_up, dst_up in to_update:
            conn = op.get_bind()
            res = conn.execute(f"select ioc_id from ioc where ioc_type = '{src_up}';")
            results = res.fetchall()
            res = conn.execute(f"select type_id from ioc_type where type_name = '{dst_up}';")
            e_info = res.fetchall()

            if e_info:
                domain_id = e_info[0][0]

                for res in results:
                    conn.execute(t_ioc.update().where(t_ioc.c.ioc_id == res[0]).values(
                        ioc_type_id=domain_id
                    ))

        op.drop_column(
            table_name='ioc',
            column_name='ioc_type'
        )

    pass

def downgrade():
    pass

def _table_has_column(table, column):
    config = op.get_context().config
    engine = engine_from_config(
        config.get_section(config.config_ini_section), prefix='sqlalchemy.')
    insp = reflection.Inspector.from_engine(engine)
    has_column = False

    for col in insp.get_columns(table):
        if column != col['name']:
            continue
        has_column = True
    return has_column


"""Runtime level configuration for PyARPES.

For the most part, this contains state related to:

1. UI settings
2. TeX and plotting
3. Data loading
4. Workspaces

This module also provides functions for loading configuration
in via external files, to allow better modularity between
different projects.
"""

# pylint: disable=global-statement

from dataclasses import dataclass, field
import warnings
import json
import os.path
import matplotlib
import pint

from pathlib import Path
from typing import Any, Dict, Optional

ureg = pint.UnitRegistry()

DATA_PATH = None
SOURCE_ROOT = str(Path(__file__).parent)

SETTINGS = {
    "interactive": {
        "main_width": 350,
        "marginal_width": 150,
        "palette": "magma",
    },
    "xarray_repr_mod": False,
    "use_tex": False,
}

# these are all set by ``update_configuration``
DOCS_BUILD = False
HAS_LOADED = False
FIGURE_PATH = None
DATASET_PATH = None

def warn(msg: str):
    """Conditionally render a warning using `warnings.warn`."""
    if DOCS_BUILD:
        return

    warnings.warn(msg)

def update_configuration(user_path: Optional[str] = None) -> None:
    """Performs an update of PyARPES configuration from a module.

    This is kind of Django/flask style but is somewhat gross. Probably
    I should refactor this at some point to use a settings management library.
    The best thing might be to look at what other projects use.

    Args:
        user_path: The path to the user configuration module. Defaults to None.
                   If None is provided then this is a noop.
    """
    global HAS_LOADED
    global FIGURE_PATH
    global DATASET_PATH

    if HAS_LOADED and user_path is None:
        return

    HAS_LOADED = True

    try:
        FIGURE_PATH = os.path.join(user_path, "figures")
        DATASET_PATH = os.path.join(user_path, "datasets")
    except TypeError:
        pass

CONFIG = {
    "WORKSPACE": {},
    "CURRENT_CONTEXT": None,
    "ENABLE_LOGGING": True,
    "LOGGING_STARTED": False,
    "LOGGING_FILE": None,
}

class WorkspaceManager:
    """A context manager for swapping workspaces temporarily.

    This is extremely useful for loading data between separate projects
    or saving figures to different projects.

    Example:
        You can use this to load data from another named workspace:

        >>> with WorkspaceManager("another_project"):      # doctest: +SKIP
        ...    file_5_from_another_project = load_data(5)  # doctest: +SKIP
    """

    def __init__(self, workspace: Optional[Any] = None) -> None:
        """Context manager for changing workspaces temporarily. Do not instantiate directly.

        Args:
            workspace: The name of the workspace to enter temporarily. Defaults to None.
        """
        self._cached_workspace = None
        self._workspace = workspace

    def __enter__(self) -> None:
        """Caches the current workspace and enters a new one.

        Raises:
            ValueError: If a workspace cannot be identified with the requested name.
        """
        global CONFIG
        self._cached_workspace = CONFIG["WORKSPACE"]

        if not self._workspace:
            return

        if not CONFIG["WORKSPACE"]:
            attempt_determine_workspace()

        workspace_path = Path(CONFIG["WORKSPACE"]["path"]).parent / self._workspace

        if workspace_path.exists():
            CONFIG["WORKSPACE"] = dict(CONFIG["WORKSPACE"])
            CONFIG["WORKSPACE"]["name"] = self._workspace
            CONFIG["WORKSPACE"]["path"] = str(workspace_path)
        else:
            raise ValueError("Could not find workspace: {}".format(self._workspace))

    def __exit__(self, *args: Any) -> None:
        """Clean up by resetting the PyARPES workspace."""
        global CONFIG
        CONFIG["WORKSPACE"] = self._cached_workspace

def workspace_matches(path: str) -> bool:
    """Determines whether a given path should be treated as a workspace.

    In the past, we used to define a workspace by several conditions together, including
    the presence of an anaysis spreadsheet containing extra metadata.

    This is much simpler now: a workspace just has a data folder in it.

    Args:
        path: The path we are chekcking.

    Returns:
        True if the path is a workspace and False otherwise.
    """
    contents = os.listdir(path)
    return any(sentinel in contents for sentinel in ["data", "Data"])

def attempt_determine_workspace(current_path=None):
    """Determines the current workspace, if working inside a workspace.

    Looks rootwards (upwards in the folder tree) for a workspace. When one is found,
    this sets the relevant configuration variables so that they are available.

    The logic for determining whether the current directory is a workspace
    has been simplified: see `workspace_matches` for more details.

    Args:
        current_path: Override for "os.getcwd". Defaults to None.
    """
    pdataset = os.getcwd() if DATASET_PATH is None else DATASET_PATH

    try:
        current_path = os.getcwd()
        for _ in range(3):
            if workspace_matches(current_path):
                CONFIG["WORKSPACE"] = {"path": current_path, "name": Path(current_path).name}
                return

            current_path = Path(current_path).parent
    except Exception:  # pylint: disable=broad-except
        pass

    CONFIG["WORKSPACE"] = {
        "path": pdataset,
        "name": Path(pdataset).stem,
    }

def load_json_configuration(filename: str):
    """Updates PyARPES configuration from a JSON file.

    Beware, this function performs a shallow update of the configuration.
    This can be adjusted if it turns out that there is a use case for
    nested configuration.

    Args:
        filename: A filename or path containing the settings.
    """
    with open(filename) as config_file:
        CONFIG.update(json.load(config_file))

try:
    from local_config import *  # pylint: disable=wildcard-import
except ImportError:
    warn(
        "Could not find local configuration file. If you don't "
        "have one, you can safely ignore this message."
    )

def override_settings(new_settings):
    """Deep updates/overrides PyARPES settings."""
    from arpes.utilities.collections import deep_update

    global SETTINGS
    deep_update(SETTINGS, new_settings)

def load_plugins() -> None:
    """Registers plugins/data-sources in the endstations.plugin module.

    Finds all classes which represents data loading plugins in the endstations.plugin
    module and registers them.

    If you need to register a custom plugin you should just call
    `arpes.endstations.add_endstation` directly.
    """
    import arpes.endstations.plugin as plugin
    from arpes.endstations import add_endstation
    import importlib

    skip_modules = {"__pycache__", "__init__"}
    plugins_dir = str(Path(plugin.__file__).parent)
    modules = os.listdir(plugins_dir)
    modules = [
        m if os.path.isdir(os.path.join(plugins_dir, m)) else os.path.splitext(m)[0]
        for m in modules
        if m not in skip_modules
    ]

    for module in modules:
        try:
            loaded_module = importlib.import_module("arpes.endstations.plugin.{}".format(module))
            for item in loaded_module.__all__:
                add_endstation(getattr(loaded_module, item))
        except (AttributeError, ImportError):
            pass

def is_using_tex() -> bool:
    """Whether we are configured to use LaTeX currently or not.

    Uses the values in rcParams for safety.

    Returns:
        True if matplotlib will use LaTeX for plotting and False otherwise.
    """
    return matplotlib.rcParams["text.usetex"]

@dataclass
class UseTex:
    """A context manager to control whether LaTeX is used when plotting.

    Internally, this uses `use_tex`, but it caches a few settings so that
    it can restore them afterwards.

    Attributes:
        use_tex: Whether to temporarily enable (use_tex=True) or disable
          (use_tex=False) TeX support in plotting.
    """

    use_tex: bool = False
    saved_context: Dict[str, Any] = field(default_factory=dict)

    def __enter__(self):
        """Save old settings so we can restore them later."""
        self.saved_context["text.usetex"] = matplotlib.rcParams["text.usetex"]
        self.saved_context["SETTINGS.use_tex"] = SETTINGS["use_tex"]

        # temporarily set the TeX configuration to the requested one
        use_tex(self.use_tex)

    def __exit__(self):
        """Reset configuration back to the cached settings."""
        SETTINGS["use_tex"] = self.saved_context["use_tex"]
        matplotlib.rcParams["text.usetex"] = self.saved_context["text.usetex"]

def use_tex(rc_text_should_use: bool = False):
    """Configures Matplotlib to use TeX.

    Does not attempt to perform any detection of an existing LaTeX
    installation and as a result, using this inappropriately can cause
    matplotlib to generate errors when you try to run standard plots.

    Args:
        rc_text_should_use: Whether to enable TeX. Defaults to False.
    """
    # in matplotlib v3 we do not need to change other settings unless
    # the preamble needs customization
    matplotlib.rcParams["text.usetex"] = rc_text_should_use
    SETTINGS["use_tex"] = rc_text_should_use

def setup_logging():
    """Configures IPython to log commands to a local folder.

    This is handled by default so that there is reproducibiity
    even in the case where the analyst has very poor Jupyter hygiene.

    This is by no means perfect. In particular, it could be improved
    substantively if anaysis products better referred to the logged record
    and not merely where they came from in the notebooks.
    """
    global CONFIG
    global HAS_LOADED
    if HAS_LOADED:
        return

    try:
        import IPython

        ipython = IPython.get_ipython()
    except ImportError:
        return

    try:
        if ipython.logfile:
            CONFIG["LOGGING_STARTED"] = True
            CONFIG["LOGGING_FILE"] = ipython.logfile
    except AttributeError:
        return

    try:
        if CONFIG["ENABLE_LOGGING"] and not CONFIG["LOGGING_STARTED"]:

            CONFIG["LOGGING_STARTED"] = True

            from arpes.utilities.jupyter import generate_logfile_path

            log_path = generate_logfile_path()
            log_path.parent.mkdir(exist_ok=True)

            ipython.magic("logstart {}".format(log_path))
            CONFIG["LOGGING_FILE"] = log_path
    except Exception as e:
        print(e)

setup_logging()
update_configuration()
load_plugins()


from transformers import GPT2LMHeadModel, GPT2Tokenizer
import numpy as np
import torch
from operator import itemgetter

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# defining model and tokenizer
model = GPT2LMHeadModel.from_pretrained('gpt2')
tokenizer = GPT2Tokenizer.from_pretrained('gpt2', add_prefix_space=True)
model.to(device)

# defining the scoring function using GPT-2
def score(tokens_tensor):
    loss = model(tokens_tensor, labels=tokens_tensor)[0]
    return np.exp(loss.cpu().detach().numpy())

# word_saliency_scorer function using GPU
def word_saliency_scorer(sentence):
    # tensor of sentence tokens by word
    # return_tensors='pt' argument returns a PyTorch tensor rather than a list (if using TensorFlow, change to 'tf')
    sentence_tokens = [tokenizer.encode(w, add_special_tokens=False) for w in sentence.split()]
    # perplexity score for entire sentence
    base_score = score(torch.tensor([token for word in sentence_tokens for token in word]).to(device))
    # finding length of sentence
    sentence_length = len(sentence_tokens)
    # calculating the word saliency for each word
    word_saliency = [((score(torch.tensor([t for w in sentence_tokens[:i]+sentence_tokens[i+1:] for t in w]).to(device))
                      - base_score) / base_score)
                     for i in range(sentence_length)]
    # returning each word along with saliency score
    sorted_pairs = list(zip(sentence.split(), word_saliency))
    return sorted_pairs

# return dictionary of complete word salience scores for given list of sentences
# saliency scores are saved every 100 sentences
words = {}
iteration = 0
for i in training_sentences:
  for index, word_score_pair in enumerate(word_saliency_scorer(i)):
    if word_score_pair[0] not in words:
      words[word_score_pair[0]] = [np.float(word_score_pair[1])]
    else:
      words[word_score_pair[0]].append(np.float(word_score_pair[1]))
  iteration += 1
  if iteration%100==0:
    print(iteration)
    with open('example_all_scores', 'w') as g:
      json.dump(words, g)
      g.close
with open('example_all_scores.json', 'w') as g:
  json.dump(words, g)
  g.close

# intermediate step
c = open('all_scores.json')
example_all_scores = json.load(c)

# return the average saliency score for each word in a data set given the saliency score of all of its occurrences
average_saliency_scores = {}
for word,scores in example_all_scores.items():
    average_saliency_scores[word] = sum(scores)/ float(len(scores)) if len(scores) else 0
f = open('average_scores.json', 'w')
json.dump(average_saliency_scores, f)
f.close()


import numpy as np
import matplotlib.pyplot as plt
from facial_keypoints_detection_input import *

file_path = 'dataset/'

def plot_image(image, target=None, title=None):
	plt.figure()

	plt.imshow(np.reshape(image, (96, 96)), cmap='gray')

	if target is not None:
		plt.plot(target[0::2], target[1::2], 'o')
		plt.title(title)

	return plt

def plot_loss_graph():
	train_validation_loss = load_train_validation_loss()

	train_loss = train_validation_loss['train_loss']
	validation_loss = train_validation_loss['validation_loss']

	plt.plot(train_loss, linewidth=3, label='Train')
	plt.plot(validation_loss, linewidth=3, label='Validation')

	plt.grid()
	plt.legend()
	plt.xlabel('Epoch')
	plt.ylabel('Loss')
	plt.yscale('log')

	return plt

if __name__ == '__main__':
	plot_loss_graph()
	plt.show()


import logging
from typing import Tuple

from bot.core.database import get_async_session
from bot.loggers.decorates import logging_decorator
from bot.models.users import User
from bot.schemes.users import UserIdScheme, UserScheme

logger = logging.getLogger(__name__)

class UserRepository:
    @staticmethod
    @logging_decorator(logger)
    async def get_or_create(user_id: int) -> Tuple[UserScheme, bool]:
        async with get_async_session() as session:
            user = await session.get(User, user_id)

            if not user:
                session.add(User(user_id=user_id))
                await session.commit()
                user = await session.get(User, user_id)
                return UserScheme.from_orm(user), True
            else:
                return UserScheme.from_orm(user), False


from pyspark import SparkConf, SparkContext, SQLContext

import logger

TEST_NAME = 'spk_single_df_filter_pd'
LOGGER = logger.get_logger(TEST_NAME)

# Specify some constants
URLPATH = "s3a://dask-avro-data/application-data/app-100*.avro"

# Start
LOGGER.info('START: Creating spark conf')
Sconf = SparkConf()
sc = SparkContext(appName="my_test", conf=Sconf)
sqlContext = SQLContext(sparkContext=sc)
LOGGER.info('FINISH: Finished creating spark conf')

URLPATH = "s3a://dask-avro-data/application-data/app-100*.avro"

LOGGER.info('START: Creating spark dataframe')
df = sqlContext.read.format("com.databricks.spark.avro").load(URLPATH)
LOGGER.info('FINISH: Spark dataframe created')

LOGGER.info('START: Starting to pandas..')
df = df.filter(df.payload.originationCountryCode == 'CAN').toPandas()
LOGGER.info('FINISH: hello pandas! %s', df.shape)

sc.stop()


from torchvision.models import resnet50, ResNet50_Weights
import torch.nn as nn

# GradCamModel class with a ResNet pretrained model
class GradCamModel(nn.Module):
    def __init__(self):
        super(GradCamModel, self).__init__()

        # define the resnet50
        # self.resnet = resnet50(pretrained=True)
        self.resnet = resnet50(weights=ResNet50_Weights.DEFAULT)

        # isolate the feature blocks
        self.features = nn.Sequential(self.resnet.conv1,
                                      self.resnet.bn1,
                                      self.resnet.relu,
                                      self.resnet.maxpool,
                                      self.resnet.layer1,
                                      self.resnet.layer2,
                                      self.resnet.layer3,
                                      self.resnet.layer4)

        # average pooling layer
        self.avgpool = self.resnet.avgpool

        # classifier
        self.classifier = self.resnet.fc

        # gradient placeholder
        self.gradient = None

    # hook for the gradients
    def activations_hook(self, grad):
        self.gradient = grad

    def get_gradient(self):
        return self.gradient

    def get_activations(self, x):
        return self.features(x)

    def forward(self, x):

        # extract the features
        x = self.features(x)

        # register the hook
        h = x.register_hook(self.activations_hook)

        # complete the forward pass
        x = self.avgpool(x)
        x = x.view((1, -1))
        x = self.classifier(x)

        return x


import colorsys
import os
import time

import numpy as np
import torch
import torch.nn as nn
from PIL import ImageDraw, ImageFont

from nets.yolo import YoloBody
from utils.utils import (cvtColor, get_anchors, get_classes, preprocess_input,
                         resize_image)
from utils.utils_bbox import DecodeBox
import cv2

class YOLO(object):
    _defaults = {
        "weight_path": '',  # 사전학습 가중치 설정, '' 미사용
        "classes_path": 'dataset/voc_names',  # 클래스 종류 경로

        "anchors_path": 'dataset/yolo_anchors.txt',  # 앵커 박스의 크기(보통 수정 x)
        "anchors_mask": [[6, 7, 8], [3, 4, 5], [0, 1, 2]],  # 스케일에 맞는 앵커 박스 정의(보통 수정 x)

        "input_shape": [416, 416],  # 입력영상의 크기(32배수) : 416, 608, ...
        "confidence": 0.5,  # 예측 결과 값이 0.5 이상만 사용
        "nms_iou": 0.3,  # NMS에서 제거되는 기준

        "letterbox_image": False,  # 입력영상으로 변환 과정에서 기존 비율의 유지 여부(테스트 결과 False가 더 좋음)
        "cuda": True,  # CUDA 사용여부
    }

    @classmethod
    def get_defaults(cls, n):
        if n in cls._defaults:
            return cls._defaults[n]
        else:
            return "Unrecognized attribute name '" + n + "'"

    # ---------------------------------------------------#
    # 초기화
    def __init__(self, **kwargs):

        self.__dict__.update(self._defaults)
        for name, value in kwargs.items():

            if name == 'model_params':
                for key, val in value.items():
                    setattr(self, key, val)
            else:
                setattr(self, name, value)

        self.class_names, self.num_classes = get_classes(self.classes_path)  # 클래스 종류와 갯수
        self.anchors, self.num_anchors = get_anchors(self.anchors_path)  # 앵커와 갯수

        # 앵커의 크기 복호화
        self.bbox_util = DecodeBox(self.anchors, self.num_classes, (self.input_shape[0], self.input_shape[1]),
                                   self.anchors_mask)

        # 클래스마다 다른색상으로 표시
        hsv_tuples = [(x / self.num_classes, 1., 1.) for x in range(self.num_classes)]
        self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
        self.colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), self.colors))

        self.model = self.generate()

    # ---------------------------------------------------#
    # 모델 생성
    def generate(self):

        model = YoloBody(self.anchors_mask, self.num_classes)

        device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

        model.load_state_dict(torch.load(self.weight_path, map_location=device))

        model = model.eval()
        print('{} model, anchors, and classes loaded.'.format(self.weight_path))

        if self.cuda:
            model = nn.DataParallel(model)
            model = model.cuda()

        return model

    # ---------------------------------------------------#
    # 이미지 탐지
    def detect_image(self, image):

        image_shape = np.array(np.shape(image)[0:2])
        image = cvtColor(image)  # RGB
        image_data = resize_image(image, (self.input_shape[1], self.input_shape[0]), self.letterbox_image)
        image_data = np.expand_dims(np.transpose(preprocess_input(np.array(image_data, dtype='float32')), (2, 0, 1)), 0)

        with torch.no_grad():
            images = torch.from_numpy(image_data)
            if self.cuda:
                images = images.cuda()

            # 예측
            outputs = self.model(images)
            outputs = self.bbox_util.decode_box(outputs)

            results = self.bbox_util.non_max_suppression(torch.cat(outputs, 1), self.num_classes, self.input_shape,
                                                         image_shape, self.letterbox_image, conf_thres=self.confidence,
                                                         nms_thres=self.nms_iou)

            if results[0] is None:
                return image

            top_label = np.array(results[0][:, 6], dtype='int32')
            top_conf = results[0][:, 4] * results[0][:, 5]
            top_boxes = results[0][:, :4]

        # 텍스트 설정
        font = ImageFont.truetype(font='data/simhei.ttf',
                                  size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
        thickness = int(max((image.size[0] + image.size[1]) // np.mean(self.input_shape), 1))

        # 결과 그리기
        for i, c in list(enumerate(top_label)):
            predicted_class = self.class_names[int(c)]
            box = top_boxes[i]
            score = top_conf[i]

            top, left, bottom, right = box

            top = max(0, np.floor(top).astype('int32'))
            left = max(0, np.floor(left).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom).astype('int32'))
            right = min(image.size[0], np.floor(right).astype('int32'))

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)
            label = label.encode('utf-8')
            print(label, top, left, bottom, right)

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i], outline=self.colors[c])
            draw.rectangle([tuple(text_origin), tuple(text_origin + label_size)], fill=self.colors[c])
            draw.text(text_origin, str(label, 'UTF-8'), fill=(0, 0, 0), font=font)
            del draw

        return image

    # ---------------------------------------------------#
    # 이미지 탐지(cv2)
    def detect_image_cv2(self, image):

        img = letterbox(image, new_shape=(640, 640))[0]

        img = img[:, :, ::-1].transpose(2, 0, 1)  # BGR to RGB, to 3x416x416
        img = np.ascontiguousarray(img)

        device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

        img = torch.from_numpy(img).to(device)
        img = img.half()
        #img = img.float()

        img /= 255.0  # 0 - 255 to 0.0 - 1.0
        if img.ndimension() == 3:
            img = img.unsqueeze(0)

        self.model.half()

        with torch.no_grad():

            # 예측
            outputs = self.model(img)
            outputs = self.bbox_util.decode_box(outputs)
            # ---------------------------------------------------------#
            #   pbox suppression
            # ---------------------------------------------------------#
            # results = self.bbox_util.non_max_suppression(torch.cat(outputs, 1), self.num_classes, self.input_shape,
            #            image_shape, self.letterbox_image, conf_thres = self.confidence, nms_thres = self.nms_iou)

            results = non_max_suppression(torch.cat(outputs, 1), self.confidence, self.nms_iou,
                                          classes=self.num_classes, agnostic=False)
            if results[0] is None:
                return image

            top_label = np.array(results[0][:, 6], dtype='int32')
            top_conf = results[0][:, 4] * results[0][:, 5]
            top_boxes = results[0][:, :4]

        # 텍스트 설정
        font = ImageFont.truetype(font='model_data/simhei.ttf',
                                  size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
        thickness = int(max((image.size[0] + image.size[1]) // np.mean(self.input_shape), 1))

        # 결과 그리기
        for i, c in list(enumerate(top_label)):
            predicted_class = self.class_names[int(c)]
            box = top_boxes[i]
            score = top_conf[i]

            top, left, bottom, right = box

            top = max(0, np.floor(top).astype('int32'))
            left = max(0, np.floor(left).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom).astype('int32'))
            right = min(image.size[0], np.floor(right).astype('int32'))

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)
            label = label.encode('utf-8')
            print(label, top, left, bottom, right)

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i], outline=self.colors[c])
            draw.rectangle([tuple(text_origin), tuple(text_origin + label_size)], fill=self.colors[c])
            draw.text(text_origin, str(label, 'UTF-8'), fill=(0, 0, 0), font=font)
            del draw

        return image

    def get_FPS(self, image, test_interval):
        image_shape = np.array(np.shape(image)[0:2])
        image = cvtColor(image)  # RGB
        image_data = resize_image(image, (self.input_shape[1], self.input_shape[0]), self.letterbox_image)
        image_data = np.expand_dims(np.transpose(preprocess_input(np.array(image_data, dtype='float32')), (2, 0, 1)), 0)

        images = torch.from_numpy(image_data)
        if self.cuda:
            images = images.cuda()

        t1 = time.time()
        for _ in range(test_interval):
            with torch.no_grad():

                outputs = self.model(images)
                outputs = self.bbox_util.decode_box(outputs)
                results = self.bbox_util.non_max_suppression(torch.cat(outputs, 1), self.num_classes, self.input_shape,
                                                             image_shape, self.letterbox_image,
                                                             conf_thres=self.confidence, nms_thres=self.nms_iou)

        t2 = time.time()
        tact_time = (t2 - t1) / test_interval
        return tact_time

    def get_map_txt(self, image_id, image, class_names, map_out_path):
        f = open(os.path.join(map_out_path, "detection-results/" + image_id + ".txt"), "w")
        image_shape = np.array(np.shape(image)[0:2])
        image = cvtColor(image)
        image_data = resize_image(image, (self.input_shape[1], self.input_shape[0]), self.letterbox_image)
        image_data = np.expand_dims(np.transpose(preprocess_input(np.array(image_data, dtype='float32')), (2, 0, 1)), 0)

        with torch.no_grad():
            images = torch.from_numpy(image_data)
            if self.cuda:
                images = images.cuda()

            outputs = self.model(images)
            outputs = self.bbox_util.decode_box(outputs)
            results = self.bbox_util.non_max_suppression(torch.cat(outputs, 1), self.num_classes, self.input_shape,
                                                         image_shape, self.letterbox_image, conf_thres=self.confidence,
                                                         nms_thres=self.nms_iou)

            if results[0] is None:
                return

            top_label = np.array(results[0][:, 6], dtype='int32')
            top_conf = results[0][:, 4] * results[0][:, 5]
            top_boxes = results[0][:, :4]

        for i, c in list(enumerate(top_label)):
            predicted_class = self.class_names[int(c)]
            box = top_boxes[i]
            score = str(top_conf[i])

            top, left, bottom, right = box
            if predicted_class not in class_names:
                continue

            f.write("%s %s %s %s %s %s\n" % (
                predicted_class, score[:6], str(int(left)), str(int(top)), str(int(right)), str(int(bottom))))

        f.close()
        return

# Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right
def xywh2xyxy(x):
    y = torch.zeros_like(x) if isinstance(x, torch.Tensor) else np.zeros_like(x)
    y[:, 0] = x[:, 0] - x[:, 2] / 2  # top left x
    y[:, 1] = x[:, 1] - x[:, 3] / 2  # top left y
    y[:, 2] = x[:, 0] + x[:, 2] / 2  # bottom right x
    y[:, 3] = x[:, 1] + x[:, 3] / 2  # bottom right y
    return y

import torchvision

# Resize image to a 32-pixel-multiple rectangle https://github.com/ultralytics/yolov3/issues/232
def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True):

    shape = img.shape[:2]  # current shape [height, width]
    if isinstance(new_shape, int):
        new_shape = (new_shape, new_shape)

    # Scale ratio (new / old)
    r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
    if not scaleup:  # only scale down, do not scale up (for better test mAP)
        r = min(r, 1.0)

    # Compute padding
    ratio = r, r  # width, height ratios
    new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
    dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]  # wh padding
    if auto:  # minimum rectangle
        dw, dh = np.mod(dw, 64), np.mod(dh, 64)  # wh padding
    elif scaleFill:  # stretch
        dw, dh = 0.0, 0.0
        new_unpad = (new_shape[1], new_shape[0])
        ratio = new_shape[1] / shape[1], new_shape[0] / shape[0]  # width, height ratios

    dw /= 2  # divide padding into 2 sides
    dh /= 2

    if shape[::-1] != new_unpad:  # resize
        img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
    top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
    left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
    img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)  # add border
    return img, ratio, (dw, dh)

# Performs Non-Maximum Suppression (NMS) on inference results
def non_max_suppression(prediction, conf_thres=0.1, iou_thres=0.6, merge=False, classes=None, agnostic=False):
    """

    Returns:
         detections with shape: nx6 (x1, y1, x2, y2, conf, cls)
    """
    if prediction.dtype is torch.float16:
        prediction = prediction.float()  # to FP32

    nc = prediction[0].shape[1] - 5  # number of classes
    xc = prediction[..., 4] > conf_thres  # candidates

    # Settings
    min_wh, max_wh = 2, 4096  # (pixels) minimum and maximum box width and height
    max_det = 300  # maximum number of detections per image
    time_limit = 10.0  # seconds to quit after
    redundant = True  # require redundant detections
    multi_label = nc > 1  # multiple labels per box (adds 0.5ms/img)

    t = time.time()
    output = [None] * prediction.shape[0]
    for xi, x in enumerate(prediction):  # image index, image inference
        # Apply constraints
        # x[((x[..., 2:4] < min_wh) | (x[..., 2:4] > max_wh)).any(1), 4] = 0  # width-height
        x = x[xc[xi]]  # confidence

        # If none remain process next image
        if not x.shape[0]:
            continue

        # Compute conf
        x[:, 5:] *= x[:, 4:5]  # conf = obj_conf * cls_conf

        # Box (center x, center y, width, height) to (x1, y1, x2, y2)
        box = xywh2xyxy(x[:, :4])

        # Detections matrix nx6 (xyxy, conf, cls)
        if multi_label:
            i, j = (x[:, 5:] > conf_thres).nonzero(as_tuple=False).T
            x = torch.cat((box[i], x[i, j + 5, None], j[:, None].float()), 1)
        else:  # best class only
            conf, j = x[:, 5:].max(1, keepdim=True)
            x = torch.cat((box, conf, j.float()), 1)[conf.view(-1) > conf_thres]

        # Filter by class
        if classes:
            x = x[(x[:, 5:6] == torch.tensor(classes, device=x.device)).any(1)]

        # Apply finite constraint
        # if not torch.isfinite(x).all():
        #     x = x[torch.isfinite(x).all(1)]

        # If none remain process next image
        n = x.shape[0]  # number of boxes
        if not n:
            continue

        # Sort by confidence
        # x = x[x[:, 4].argsort(descending=True)]

        # Batched NMS
        c = x[:, 5:6] * (0 if agnostic else max_wh)  # classes
        boxes, scores = x[:, :4] + c, x[:, 4]  # boxes (offset by class), scores
        i = torchvision.ops.boxes.nms(boxes, scores, iou_thres)
        if i.shape[0] > max_det:  # limit detections
            i = i[:max_det]
        if merge and (1 < n < 3E3):  # Merge NMS (boxes merged using weighted mean)
            try:  # update boxes as boxes(i,4) = weights(i,n) * boxes(n,4)
                iou = box_iou(boxes[i], boxes) > iou_thres  # iou matrix
                weights = iou * scores[None]  # box weights
                x[i, :4] = torch.mm(weights, x[:, :4]).float() / weights.sum(1, keepdim=True)  # merged boxes
                if redundant:
                    i = i[iou.sum(1) > 1]  # require redundancy
            except:  # possible CUDA error https://github.com/ultralytics/yolov3/issues/1139
                print(x, i, x.shape, i.shape)
                pass

        output[xi] = x[i]
        if (time.time() - t) > time_limit:
            break  # time limit exceeded

    return output

def box_iou(box1, box2):
    # https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py
    """
    Return intersection-over-union (Jaccard index) of boxes.
    Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
    Arguments:
        box1 (Tensor[N, 4])
        box2 (Tensor[M, 4])
    Returns:
        iou (Tensor[N, M]): the NxM matrix containing the pairwise
            IoU values for every element in boxes1 and boxes2
    """

    def box_area(box):
        # box = 4xn
        return (box[2] - box[0]) * (box[3] - box[1])

    area1 = box_area(box1.T)
    area2 = box_area(box2.T)

    # inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2)
    inter = (torch.min(box1[:, None, 2:], box2[:, 2:]) - torch.max(box1[:, None, :2], box2[:, :2])).clamp(0).prod(2)
    return inter / (area1[:, None] + area2 - inter)  # iou = inter / (area1 + area2 - inter)


import requests
from bs4 import BeautifulSoup
from datetime import datetime
import time
import ctypes

url = input("Enter your product URL: ")
amount_of_time = input("For how long do you want to check the price (in minutes): ")
aimed_price = input("The price I'm looking for: ")

headers = {'User Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36'}
url_source = requests.get(url.strip(), headers=headers)
soup = BeautifulSoup(url_source.content, "lxml")

# NAME OF THE PRODUCT
name = soup.find(class_ ='page-title').text

# PRICE OF THE PRODUCT
price = soup.find(class_= 'product-new-price').text.strip()

# .99 or .00 from the price
for sup_price in soup.find_all('div', class_ = 'product-highlight product-page-pricing'):
    sup_extract =  sup_price.find('p', class_ = 'product-new-price')
    sup_extract1 = sup_extract.find('sup')
    sup = sup_extract1.text

full_price = (float(price[:-6].replace('.', '')) + (float(sup) / 100))

# TIME SPAN
time_to_end = time.time() + 60 * float(amount_of_time)

while time.time() < time_to_end:
    print("Name:  ", name.strip())
    print('Price: ', full_price)
    print('Time:  ',datetime.now().replace(microsecond=0))
    print('-'*40)
    time.sleep(1)  # Updates once per second
    if full_price <= float(aimed_price):
        MessageBox = ctypes.windll.user32.MessageBoxW
        MessageBox(None, 'Hello', 'Window title', 0)
        break


import datetime
import subprocess
import typing

import polib

from ..core import FileTransformer

def dictToPo(dic: typing.Mapping[str, str]) -> polib.POFile:
	poF = polib.POFile(encoding="utf-8")

	dt = str(datetime.datetime.now(tz=datetime.timezone.utc))
	poF.metadata.update(
		{
			"Project-Id-Version": "emojifilt",
			"POT-Creation-Date": dt,
			"PO-Revision-Date": dt,
			"Language": "emoji",
			"Content-Type": "text/plain; charset=UTF-8",
		}
	)

	for k, v in dic.items():
		poF.append(polib.POEntry(msgid=k, msgstr=v))

	poF.sort()
	return poF

def dumpPo(dic: typing.Mapping[str, str]):
	return str(dictToPo(dic))

def compileMoToPo(poSource: str) -> bytes:
	"""`polib` built-in compilation doesn't create a hashtable"""
	return bytes(subprocess.run(["msgfmt", "-o", "-", "-"], input=poSource.encode("utf-8"), capture_output=True).stdout)

def dumpMoWithHashtable(dic: typing.Mapping[str, str]) -> bytes:
	return compileMoToPo(dumpPo(dic))

poSerializer = FileTransformer("libintl_po", dumpPo, None, str, typing.Mapping[str, str], "po")
moSerializerWithHashtable = FileTransformer("libintl_mo_with_hashtable", dumpMoWithHashtable, None, bytes, typing.Mapping[str, str], "mo")


import requests
import json
from bs4 import BeautifulSoup
import datetime as dt
import time
from os import rename

#event_url = "imagine-dragons-evolve-world-tour/floor/c935b272-3019-48b9-877e-77f7f95b9a61/510954"
#url = "https://www.ticketswap.nl/event/"+event_url

def get_event_stats(url):
    page = requests.get(url)
    soup = BeautifulSoup(page.text, 'lxml')
    available = -1
    sold = -1
    wanted = -1
    try:
        available = soup.find("div", {'class' : 'counter counter-available'}).find("div", {'class' : 'counter-value'}).text
        sold = soup.find("div", {'class' : 'counter counter-sold'}).find("div", {'class' : 'counter-value'}).text
        wanted = soup.find("div", {'class' : 'counter counter-wanted'}).find("div", {'class' : 'counter-value'}).text
    except:
        name = url[31:]
        name = name[:name.index("/")].split("-")
        print(name)
    return [int(available), int(sold), int(wanted)]

def get_event_title(url):
    page = requests.get(url)
    soup = BeautifulSoup(page.text, 'lxml')

    return soup.find("h1", {'itemprop' : 'summary'}).find("a").text

def get_event_date(url):
    page = requests.get(url)
    soup = BeautifulSoup(page.text, 'lxml')

    monthdict = {'januari' : '1', 'februari' : '2', 'maart' : '3', 'april' : '4', 'mei' : '5', 'juni' : '6', 'juli' : '7', 'augustus' : '8', 'september' : '9', 'oktober' : '10', 'november' : '11', 'december' : '12',}

    date = soup.find("span", {'class' : 'date'}).text[1:]

    try:
        date = date[:date.index("-")-1]
    except:
        pass

    date = date[date.index(" ")+1:]
    day = date[:date.index(" ")]
    month = monthdict[date[date.index(" ")+1:-5]]
    year = date[-4:]

    event_date = dt.datetime(int(year), int(month), int(day), 21)
    t = dt.datetime.today()
    today = dt.datetime(t.year, t.month, t.day, t.hour)

    event_timestamp = time.mktime(event_date.timetuple())
    return event_timestamp

def get_date_diff(event_timestamp):
    today_timestamp = int(time.time())

    diff_s = event_timestamp - today_timestamp
    diff_h = int(diff_s/3600.0)

    return diff_h


from typing import Optional

import requests
from bs4 import BeautifulSoup

DEFAULT_HEADERS = {
    "User-Agent": (
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
        "AppleWebKit/537.36 (KHTML, like Gecko) "
        "Chrome/74.0.3729.169 Safari/537.36"
    )
}

def make_request(
    url: str,
    headers: Optional[dict] = None,
    proxies: Optional[dict] = None,
    soup: bool = False,
) -> Optional[requests.Response]:
    """ Make HTTP Get Request, return response or bs4 if status code is 200,
    None if 404 else raise error.
    """
    if headers is None:
        headers = DEFAULT_HEADERS
    try:
        response = requests.get(url, headers=headers, proxies=proxies)
    except requests.exceptions.MissingSchema:
        raise ValueError("Incorrect URL provided.")
    if response.status_code == 404:
        return None
    response.raise_for_status()
    return BeautifulSoup(response.content, "html.parser") if soup else response


#!/usr/bin/env python

"""
Install.py tool to download, unpack, build, and link to the plumed2 library
used to automate the steps described in the README file in this dir
"""

from __future__ import print_function
import sys, os, platform, subprocess, shutil
from argparse import ArgumentParser

sys.path.append('..')
from install_helpers import get_cpus, fullpath, geturl, checkmd5sum, getfallback

parser = ArgumentParser(prog='Install.py',
                        description="LAMMPS library build wrapper script")

# settings

version = "2.8.2"
mode = "static"

# help message

HELP = """
Syntax from src dir: make lib-plumed args="-b"
                 or: make lib-plumed args="-b -v 2.8.2"
                 or: make lib-plumed args="-p /usr/local/plumed2 -m shared"

Syntax from lib dir: python Install.py -b -v 2.8.2
                 or: python Install.py -b
                 or: python Install.py -p /usr/local/plumed2 -m shared

Example:

make lib-plumed args="-b"   # download/build in lib/plumed/plumed2
make lib-plumed args="-p $HOME/plumed2 -m shared" # use existing Plumed2 installation in $HOME/plumed2
"""

# known checksums for different PLUMED versions. used to validate downloads.
checksums = { \
        '2.4.4' : '71ed465bdc7c2059e282dbda8d564e71', \
        '2.5.7' : '1ca36226fdb8110b1009aa61d615d4e5', \
        '2.6.6' : '6b470dcdce04c221ea42d8500b03c49b', \
        '2.7.6' : 'fb8c0ec10f97a9353eb123a5c4c35aa6', \
        '2.8.1' : '6bfe72ebdae63dc38a9ca27d9b0e08f8', \
        '2.8.2' : '599092b6a0aa6fff992612537ad98994', \
        }

# parse and process arguments

pgroup = parser.add_mutually_exclusive_group()
pgroup.add_argument("-b", "--build", action="store_true",
                    help="download and build the plumed2 library")
pgroup.add_argument("-p", "--path",
                    help="specify folder of existing plumed2 installation")
parser.add_argument("-v", "--version", default=version, choices=checksums.keys(),
                    help="set version of plumed to download and build (default: %s)" % version)
parser.add_argument("-m", "--mode", default=mode, choices=['static', 'shared', 'runtime'],
                    help="set plumed linkage mode: static (default), shared, or runtime")

args = parser.parse_args()

# print help message and exit, if neither build nor path options are given
if not args.build and not args.path:
  parser.print_help()
  sys.exit(HELP)

buildflag = args.build
pathflag = args.path is not None
plumedpath = args.path
mode = args.mode
version = args.version

homepath = fullpath('.')
homedir = "%s/plumed2" % (homepath)

if pathflag:
    if not os.path.isdir(plumedpath):
      sys.exit("Plumed2 path %s does not exist" % plumedpath)
    homedir = fullpath(plumedpath)
    if not os.path.isdir(os.path.join(homedir, 'include', 'plumed', 'core')):
      sys.exit("No Plumed2 installation found at %s" % plumedpath)

# download and unpack plumed2 tarball

if buildflag:
  url = "https://github.com/plumed/plumed2/releases/download/v%s/plumed-src-%s.tgz" % (version, version)
  filename = "plumed-src-%s.tar.gz" % version
  fallback = getfallback('plumed', url)
  print("Downloading plumed  ...")
  try:
    geturl(url, filename)
  except:
    geturl(fallback, filename)

  # verify downloaded archive integrity via md5 checksum, if known.
  if version in checksums:
    if not checkmd5sum(checksums[version], filename):
      print("Checksum did not match. Trying fallback URL", fallback)
      geturl(fallback, filename)
      if not checkmd5sum(checksums[version], filename):
        sys.exit("Checksum for plumed2 library does not match for fallback, too.")

  print("Unpacking plumed2 source tarball ...")
  if os.path.exists("%s/plumed-%s" % (homepath, version)):
    shutil.rmtree("%s/plumed-%s" % (homepath, version))
  if os.path.exists(homedir):
    shutil.rmtree(homedir)
  cmd = 'cd "%s"; tar -xzvf %s' % (homepath, filename)
  subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
  os.remove(os.path.join(homepath, filename))

  # build plumed
  print("Building plumed ...")
  n_cpus = get_cpus()
  cmd = 'cd %s/plumed-%s; ./configure --prefix=%s --enable-modules=all --enable-static-patch ; make -j%d ; make install' % (homepath, version, homedir, n_cpus)
  try:
    txt = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
    print(txt.decode('UTF-8'))
  except subprocess.CalledProcessError as e:
    print("Make failed with:\n %s" % e.output.decode('UTF-8'))
    sys.exit(1)

# create 2 links in lib/plumed to plumed2 installation dir

print("Creating links to plumed2 include and lib files")
if os.path.isfile("includelink") or os.path.islink("includelink"):
  os.remove("includelink")
if os.path.isfile("liblink") or os.path.islink("liblink"):
  os.remove("liblink")
os.symlink(os.path.join(homedir, 'include'), 'includelink')
libpath = os.path.join(homedir, 'lib64')
if not os.path.exists(libpath):
  libpath = os.path.join(homedir, 'lib')
os.symlink(libpath, 'liblink')
if os.path.isfile("Makefile.lammps.%s" % mode):
  print("Creating Makefile.lammps")
  plumedinc = os.path.join('liblink', 'plumed', 'src', 'lib', 'Plumed.inc.' + mode)
  lines1 = open(plumedinc, 'r').readlines()
  if (platform.system() == 'Darwin' and os.path.isfile("Makefile.lammps.%s.macosx" % mode)):
    lines2 = open("Makefile.lammps.%s.macosx" % mode, 'r').readlines()
  else:
    lines2 = open("Makefile.lammps.%s" % mode, 'r').readlines()
  fp = open("Makefile.lammps", 'w')
  fp.write("PLUMED_LIBDIR=" + os.path.join(homedir, "lib\n"))
  for line in lines1:
    fp.write(line)
  for line in lines2:
    fp.write(line)
  fp.close()


import sys

from PyQt5 import QtGui
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication,QMainWindow,QLabel,QSlider,QLineEdit
from PyQt5.QtGui import QPixmap
class window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title="PyQt Winndow"
        self.top=100
        self.left=100
        self.height=500
        self.width= 680
        self.setWindowIcon(QtGui.QIcon('coala.jpg'))
        self.Set()
    def Set(self):
        # self.line=QLineEdit(self)
        # self.line.move(200,200)
        self.sl=QSlider(Qt.Horizontal,self)
        self.sl.move(200,450)
        self.label=QLabel(self)
        self.label.setPixmap(QPixmap('vai.jpeg'))
        self.label.setGeometry(60,100,150,120)
        self.sl.valueChanged.connect(self.dsd)

        self.setWindowTitle(self.title)
        self.setGeometry(self.top,self.left,self.width,self.height)
        self.show()
    def dsd(self,value):
        if value == 0:
            self.label.setPixmap(QPixmap('vai.jpeg'))
            self.label.setGeometry(60,100,150,120)
        elif value >30:
            self.label.setPixmap(QPixmap('save.png'))
            self.label.setGeometry(60, 100, 150, 120)
        elif value >60:
            self.label.setPixmap(QPixmap('delete.png'))
            self.label.setGeometry(60, 100, 150, 120)
        else:
            self.label.setPixmap(QPixmap('copy.png'))
            self.label.setGeometry(60, 100, 150, 120)
app=QApplication([])
win=window()
app.exec_()


import pytest

from textwrap import dedent
from collections import OrderedDict

from .utils import Server, Page
from .runners.click import ClickRunner
from .runners.docker import DockerRunner
from .runners.brew import BrewRunner

runners = OrderedDict({
    'click': ClickRunner,
    'docker': DockerRunner,
    'brew': BrewRunner,
})

@pytest.fixture(scope="session", params=list(runners.values()), ids=list(runners.keys()))
def runner(request):
    """ Invoke standalone/internal cli interface(s) """

    runner = request.param()
    yield runner
    del runner

@pytest.fixture
def server():
    s = Server()
    yield s
    s.destroy()

@pytest.fixture
def servers():
    s1 = Server()
    s2 = Server()
    yield (s1, s2)
    s1.destroy()
    s2.destroy()

@pytest.fixture
def simple_site(server):
    """ simple configuration for routing and indexation testing
        easy to calculate what addresses will work and whats not.
        path       status        external       internal
        /           200          0              2
        /about      200          2              0
        /projects   200          3              1 (not existing)
    """
    INDEX_PAGE = dedent(
        """\

~ /index - /about - /projects

hello all

        """)

    ABOUT_PAGE = dedent(
        """\

~ /index - /about - /projects

        github.com -
        made.ua

        """)

    PROJECTS_PAGE = dedent(
        """\

~ /index - /about - /projects

        deadlinks -
        gobyexample -
        gobyexample -
        (wordpress) debug bar rewrite rules -
        secret project

        """)

    return server.router({
        '^/$': Page(INDEX_PAGE).exists(),
        "^/about/?$": Page(ABOUT_PAGE).exists(),
        "^/projects/?$": Page(PROJECTS_PAGE).exists(),
    })

@pytest.fixture
def site_with_links(server):
    INDEX_PAGE = dedent(
        """\
            arguments

            (1) existing link
            (2)existing link
            (3) existing link

            (5) existing link
            (5) existing link
            (6) existing link

            (7) existing link
            (8) existing link
            (9) existing link

coverage run --source jedi -m py.test
            relative links

            (10)existing link
            (11)existing link
            (12)existing link

            two in one out

            (13)existing link #10
            (14)existing link #10

            not existing links

            <ahref=link-6 style=background:#f00;>this isn't a link at all
            js
            just href
            just href
            just href
            just href
            just href
            just href
            just href

            external links

            (15) http google
            (16) google org
            (17) example net (port 80)
            (18) example.com

            external dead links

            (19) localhost: 21
            (20) lolhost: 90
            (21) spaces in domain
            (22) hostname (lol)
            (23) mistyped  link

            more links

            (24) more link
        """)

    OTHER_PAGE = dedent(
        """\
            more links for crawler
            (1) seen link
            (24) unseen link
            (25) mistyped  link 2
        """)

    site_with_links = server.router({
        '^/$': Page(INDEX_PAGE).exists(),
        'link-\d{1,}': Page("ok").exists(),
        'limk-\d{1,}': Page("typo in path").not_exists(),
        '^/more/links/?$': Page(OTHER_PAGE).exists(),
    })
    return site_with_links


import sys

import requests

from bs4 import BeautifulSoup as bs

from models import Weather, Base, engine, db_session

city = sys.argv[1]
print('getting city', city)

def get_weather(place):
    place = place.replace(" ", "-")
    url = "https://www.weather-forecast.com/locations/" + place + "/forecasts/latest"
    r = requests.get(url)
    soup = bs(r.content, "lxml")
    weather = soup.findAll("span", {"class": "phrase"})[0].text
    w = Weather(city=place, response=weather)
    db_session.add(w)
    db_session.commit()
    return weather

print(get_weather(city))
sys.stdout.flush()

def init_db():
    # import all modules here that might define models so that
    # they will be registered properly on the metadata.  Otherwise
    # you will have to import them first before calling init_db()
    Base.metadata.create_all(bind=engine)

init_db()


import pandas as pd
import urllib
import urllib.request
import json

def main():
    url = "https://ephtracking.cdc.gov:443/apigateway/api/v1/" + \
            "getCoreHolder/441/2/ALL/ALL/2011,2012,2013,2013,2014,2015/0/0"
    # Reading the json as a dict
    with urllib.request.urlopen(url) as json_data:
        data = json.load(json_data)
    dataset = pd.DataFrame.from_dict(data['pmTableResultWithCWS'])
    # Datatype of dataValue is object, change it into numeric
    dataset['dataValue'] = pd.to_numeric(data['dataValue'], errors='coerce')

    # 1. get the means of all values for each county using "groupby" method
    result = data.groupby(['title','year','geoId'])['dataValue'].mean()
    result = result.to_frame().reset_index()
    result.columns = ['Location','Year','GeoId','Value']
    # 2. split the location column into two column("county","state")
    result[['County','State']] = result['Location'].str.split(',', n=1,
                                                              expand=True)
    del result['Location']
    result['Quality'].dropna(inplace=True)
    result.to_csv("water_clean.csv",index=False)


import razorpay
from datetime import datetime
from .settings import KEY_ID,  KEY_SECRET

client = razorpay.Client(auth=(KEY_ID, KEY_SECRET))

def order(username, email, fname, lname, amount_rs, currency='INR'):
    amount = int(amount_rs) * 100
    id = username + "@" + str(datetime.now()).split(".")[0].replace(":", "").replace("-", "").replace(" ", "")
    notes = {"email" : email, "name" : f"{fname} {lname}"}

    data = { "amount": amount, "currency": currency, "receipt": id, "notes" : notes }
    order = client.order.create(data=data)

    return order

def verify_payment_request(data):
    client.utility.verify_payment_signature(data)