# -*- coding: utf-8 -*-
#
# Small helper functions for system services
#
# !!! This file is python 3.x compliant !!!
#

import logging
import os
import re
import shlex
import signal
import subprocess
import sys

try:
    import requests
    import urllib3

    urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
except ImportError:
    pass

_logger = logging.getLogger("ultimarc")

class TerminalColors(object):
    """
    Simple class for setting terminal colors.
    https://en.wikipedia.org/wiki/ANSI_escape_code
    """

    reset = '\033[0m'
    bold = '\033[1m'
    underline = '\033[4m'

    fg_black = '\033[38;5;0m'
    fg_red = '\033[38;5;1m'
    fg_green = '\033[38;5;2m'
    fg_yellow = '\033[38;5;3m'
    fg_blue = '\033[38;5;4m'
    fg_magenta = '\033[38;5;5m'
    fg_cyan = '\033[38;5;6m'
    fg_white = '\033[38;5;7m'

    fg_bright_black = '\033[38;5;8m'
    fg_bright_red = '\033[38;5;9m'
    fg_bright_green = '\033[38;5;10m'
    fg_bright_yellow = '\033[38;5;11m'
    fg_bright_blue = '\033[38;5;12m'
    fg_bright_magenta = '\033[38;5;13m'
    fg_bright_cyan = '\033[38;5;14m'
    fg_bright_white = '\033[38;5;15m'

    bg_black = '\033[48;5;0m'
    bg_red = '\033[48;5;1m'
    bg_green = '\033[48;5;2m'
    bg_yellow = '\033[48;5;3m'
    bg_blue = '\033[48;5;4m'
    bg_magenta = '\033[48;5;5m'
    bg_cyan = '\033[48;5;6m'
    bg_white = '\033[48;5;7m'

    bg_bright_black = '\033[48;5;8m'
    bg_bright_red = '\033[48;5;9m'
    bg_bright_green = '\033[48;5;10m'
    bg_bright_yellow = '\033[48;5;11m'
    bg_bright_blue = '\033[48;5;12m'
    bg_bright_magenta = '\033[48;5;13m'
    bg_bright_cyan = '\033[48;5;14m'
    bg_bright_white = '\033[48;5;15m'

    _default_format = ''
    _default_background = ''
    _default_foreground = ''

    def custom_fg_color(self, index: int) -> str:
        """
        Get a custom color seq
        :param index: intger 0 - 255
        :return: string
        """
        return f'\033[38;5;{index}m'

    def custom_bg_color(self, index: int) -> str:
        """
        Get a custom color seq
        :param index: intger 0 - 255
        :return: string
        """
        return f'\033[48;5;{index}m'

    def set_default_formatting(self, *args):
        """
        Set the default colors for formatting.
        :param args: list of colors.
        """
        self._default_format = ''
        for arg in args:
            self._default_format += arg

    def set_default_background(self, *args):
        """
        Set default background colors
        :param args: list of colors
        """
        self._default_background = ''
        for arg in args:
            self._default_background += arg

    def set_default_foreground(self, *args):
        """
        Set default foreground colors
        :param args: list of colors
        """
        self._default_foreground = ''
        for arg in args:
            self._default_foreground += arg

    def fmt(self, line: str, *args) -> str:
        """
        Color a line of text
        :param line: string
        :param args: list of colors.
        :return: string
        """
        if not args:
            l = self._default_format
        else:
            l = ''
            for arg in args:
                l += arg

        l += str(line)
        l += self.reset

        l += self._default_background
        l += self._default_foreground

        return l

tc = TerminalColors()

class _ToolLoggingFormatter(logging.Formatter):
    """
    Add colorization to logging messages.
    """

    _color = TerminalColors()

    def format(self, record):
        msg = super(_ToolLoggingFormatter, self).format(record)
        if record.levelno == logging.DEBUG:
            msg = self._color.fmt(msg, self._color.fg_cyan)
        elif record.levelno == logging.ERROR:
            msg = self._color.fmt(msg, self._color.fg_bright_red)
        elif record.levelno == logging.WARNING:
            msg = self._color.fmt(msg, self._color.fg_bright_yellow)

        return msg

def setup_logging(logger, progname, debug=False, quiet=False, logfile=None):
    """
  Setup Python logging
  :param logger: Handle to logger object
  :param progname: Name of application or service
  :param debug: True if debugging enabled
  :param quiet: True if quiet output selected.
  :param logfile: Path and filename to log file to output to
  :return: Nothing
  """
    if not logger:
        return False

    # Set our logging options and formatter now that we have the program arguments.
    if debug:
        logging.basicConfig(filename=os.devnull, datefmt="%Y-%m-%d %H:%M:%S", level=logging.DEBUG)
        formatter = _ToolLoggingFormatter("%(asctime)s {0}: %(levelname)s: %(message)s".format(progname))
    else:
        logging.basicConfig(filename=os.devnull, datefmt="%Y-%m-%d %H:%M:%S",
                            level=logging.WARNING if quiet else logging.INFO)
        # formatter = logging.Formatter('%(levelname)s: {0}: %(message)s'.format(progname))
        formatter = _ToolLoggingFormatter("%(message)s")

    # Setup stream logging handler
    handler = logging.StreamHandler(sys.stdout)
    handler.flush = sys.stdout.flush
    handler.setFormatter(formatter)

    logger.addHandler(handler)

    # Setup file logging handler
    if logfile:

        # make sure the path exists
        logpath = os.path.dirname(os.path.abspath(os.path.expanduser(logfile)))

        if not os.path.exists(logpath):
            os.makedirs(logpath)

        handler = logging.FileHandler(logfile)
        handler.setFormatter(logging.Formatter("%(asctime)s {0}: %(levelname)s: %(message)s".format(progname)))

        logger.addHandler(handler)

def which(program):
    """
  Find the path for a given program
  http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
  :param program: name of executable file to find
  """

    try:
        path_spec = os.environ["PATH"]
    except KeyError:
        # for weird times when we don't have a good environment
        path_spec = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin"

    def is_exe(fpath):
        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

    # pylint: disable=unused-variable
    fpath, fname = os.path.split(program)
    if fpath:
        if is_exe(program):
            return program
    else:
        for path in path_spec.split(os.pathsep):
            path = path.strip('"')
            exe_file = os.path.join(path, program)
            if is_exe(exe_file):
                return exe_file

    return None

def run_external_program(args, cwd=None, env=None, shell=False, debug=False):
    """
  Run an external program, arguments
  :param args: program name plus arguments in a list
  :param cwd: Current working directory
  :param env: A modified environment to use
  :param shell: Use shell to execute command
  :param debug: Add '--debug' to args if '--debug' is in sys.argv
  :return: exit code, stdoutdata, stderrdata
  """

    if not args:
        _logger.debug("run_external_program: bad arguments")
        return -1, None, None

    if debug is True and "--debug" in sys.argv and "--debug" not in args:
        args.append("--debug")

    _logger.debug("external: {0}".format(os.path.basename(args[0])))

    p = subprocess.Popen(args, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env, shell=shell)
    stdoutdata, stderrdata = p.communicate()
    p.wait()

    if isinstance(stdoutdata, (bytes, bytearray)):
        stdoutdata = stdoutdata.decode("utf-8")
    if isinstance(stderrdata, (bytes, bytearray)):
        stderrdata = stderrdata.decode("utf-8")

    return p.returncode, stdoutdata, stderrdata

def is_valid_email(email):
    """
  Validate email parameter is a valid formatted email address
  :param email: string containing email address
  :return: True if email is valid otherwise False
  """
    if not email:
        return False

    return bool(re.match("^.+@(\[?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?)$", email))

def signal_process(name, signal_code=signal.SIGHUP):
    """
  Send a signal to a program
  :param name: name of the executable, not a systemd service name
  :param signal_code: signal code from signal object
  """
    pid = None

    prog = which("pidof")

    if not prog:
        _logger.error('unable to locate "pidof" executable.')
        return False

    # Get the process id
    try:

        args = shlex.split("{0} {1}".format(prog, name))
        pid = int(subprocess.check_output(args).decode("utf-8").strip())
    except subprocess.CalledProcessError:
        _logger.error("failed to get program pid ({0})".format(name))

    # Send signal to process
    if pid:
        os.kill(pid, signal_code)
        _logger.debug("send signal to {0} complete".format(name))
        return True

    _logger.debug("send signal to {0} failed".format(name))

    return False

def pid_is_running(pid: int):
    """
  Check For the existence of a unix pid.
  :param pid: integer ID of this process
  :return: True if process with this ID is running, otherwise False
  """
    # See if there is a currently running mysqld instance
    # pylint: disable=unused-variable
    args = ['ps', '-eo', 'ruid,pid,ppid,args']
    code, so, se = run_external_program(args=args)
    if code == 0:
        lines = so.split('\n')
        for line in lines:
            if str(pid) in line:
                while '  ' in line:
                    line = line.replace('  ', ' ')
                if pid == int(line.strip().split(' ')[1]) and '<defunct>' not in line:
                    return True
    return False

def get_process_pids(matches: list) -> list:
    """
    Get the pids of a currently running process.  May match more than one running process.
    :param matches: parts of process command to match in process list
    :return: List of PIDs
    """
    pids = list()

    if not matches or len(matches) == 0:
        raise ValueError('matches list may not be empty.')

    for match in matches:
        if not isinstance(match, str):
            raise ValueError('invalid match value, must be string.')

    # See if there is a currently running mysqld instance
    # pylint: disable=unused-variable
    args = ['ps', '-ef']
    code, so, se = run_external_program(args=args)
    if code == 0:
        lines = so.split('\n')
        for line in lines:
            no_match = False
            for match in matches:
                if match not in line:
                    no_match = True
                    break

            if no_match is True:
                continue
            while '  ' in line:
                line = line.replace('  ', ' ')
            pids.append(int(line.split(' ')[1]))

    return pids

def write_pidfile_or_die(progname: str, pid_file: str = None):
    """
  Attempt to write our PID to the given PID file or raise an exception.
  :param progname: Name of this program
  :param pid_file: an alternate path and pid file to use
  :return: pid path and filename
  """
    if not pid_file:
        home = os.path.expanduser("~")
        pid_path = os.path.join(home, ".local/run")
        pid_file = os.path.join(pid_path, "{0}.pid".format(progname))
    else:
        pid_path = os.path.dirname(pid_file)

    if not os.path.exists(pid_path):
        os.makedirs(pid_path)

    if os.path.exists(pid_file):
        pid = int(open(pid_file).read())

        if pid_is_running(pid):
            _logger.warning("program is already running, aborting.")
            raise SystemExit

        else:
            os.remove(pid_file)

    open(pid_file, "w").write(str(os.getpid()))

    return pid_file

def remove_pidfile(progname: str, pid_file: str = None):
    """
  Remove the PID file for the given program
  :param progname: Name of this program
  :param pid_file: an alternate pid file to use
  """
    if not pid_file:
        home = os.path.expanduser("~")
        pid_path = os.path.join(home, ".local/run")
        pid_file = os.path.join(pid_path, "{0}.pid".format(progname))

    if os.path.exists(pid_file):
        os.remove(pid_file)

def print_progress_bar(iteration, total, prefix="", suffix="", decimals=1, bar_length=90, fill="█"):
    """
  Call in a loop to create terminal progress bar.
  https://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console
  https://gist.github.com/aubricus/f91fb55dc6ba5557fbab06119420dd6a
  :param iteration: Required  : current iteration (Int)
  :param total: Required  : total iterations (Int)
  :param prefix: Optional  : prefix string (Str)
  :param suffix: Optional  : suffix string (Str)
  :param decimals: Optional  : positive number of decimals in percent complete (Int)
  :param bar_length: Optional  : character length of bar (Int)
  :param fill: Optional  : bar fill character (Str)
  """
    str_format = "{0:." + str(decimals) + "f}"
    percents = str_format.format(100 * (iteration / float(total)))
    filled_length = int(round(bar_length * iteration / float(total)))
    bar = fill * filled_length + "-" * (bar_length - filled_length)

    sys.stdout.write("\r{0} [{1}] {2}{3} {4}".format(prefix, bar, percents, "%", suffix))

    if iteration == total:
        sys.stdout.write("\n")
    sys.stdout.flush()

def git_project_root(path=None):
    """
    Figure out the git project top level directory.
    :param path: optional: path to check.
    :return: Git project root path or None
    """
    cwd = os.curdir
    if path:
        if not os.path.exists(path):
            raise ValueError('Invalid directory path argument')
        os.chdir(path)

    args = ['git', 'rev-parse', '--show-toplevel']
    # pylint: disable=unused-variable
    code, so, se = run_external_program(args=args)

    os.chdir(cwd)

    if code == 0:
        return so.strip()

    return None


import tempfile
import uuid
from abc import abstractmethod
from pathlib import Path
from typing import Any, Optional, Type

import numpy as np
from sklearn.base import TransformerMixin
from sklearn.preprocessing import MinMaxScaler as OrgMinMaxScaler

from tidewater.datatypes.base import NumpyType
from tidewater.datatypes.model import SKLearnModel
from tidewater.transformers.base import Transformer, TransformerMode
from tidewater.transformers.interface import InputInterface, OutputInterface

class Scaler(Transformer):
    def __init__(self, **kwargs: Any):
        super().__init__(**kwargs)
        self.scaler: Optional[Any] = None

    def _train(self, X: np.ndarray, y: Optional[np.ndarray] = None, **kwargs: Any) -> SKLearnModel:
        self.scaler = self._scaler_class()()
        self.scaler.fit(X)
        fname = str(uuid.uuid1())
        return SKLearnModel.from_transformer(self.scaler, self._intermediate_results_dir / fname)

    def _transform(self, X: np.ndarray, **kwargs: Any) -> np.ndarray:
        if self.scaler is None:
            self._train(X)
        if self.scaler is not None:
            result: np.ndarray = self.scaler.transform(X)
            return result
        else:
            raise ValueError("Scaler is not trained")

    def execute(self, **kwargs: Any) -> None:
        array = self.get_input_value("data")[0]
        assert array is not None and isinstance(array, NumpyType), "The input values for the Scaler are not valid."

        if self._transformer_mode == TransformerMode.TRANSFORMING:
            model = self.get_input_value("model")[0]
            assert model is not None and isinstance(
                model, SKLearnModel
            ), "The input values for the Scaler are not valid."
            self.scaler = model.materialize()

            scaled = self._transform(array.to_2d())
            self.set_output_value(data=NumpyType(ndarray=scaled))
        else:
            model = self._train(array.to_2d())
            self.set_output_value(model=model)

    @staticmethod
    def _build_train_input_interface() -> InputInterface:
        return InputInterface(data=NumpyType)

    @staticmethod
    def _build_transform_input_interface() -> InputInterface:
        return InputInterface(data=NumpyType, model=SKLearnModel)

    @staticmethod
    def _build_train_output_interface() -> OutputInterface:
        return OutputInterface(model=SKLearnModel)

    @staticmethod
    def _build_transform_output_interface() -> OutputInterface:
        return OutputInterface(data=NumpyType)

    @staticmethod
    @abstractmethod
    def _scaler_class() -> Type:
        """
        This method returns an SKLearn Scaler.
        """
        ...

class MinMaxScaler(Scaler):
    @staticmethod
    def _scaler_class() -> Type:
        return OrgMinMaxScaler  # type: ignore


"""A simple example of broadcasting a light sensor's data as a percentage.

This was tested on a UnexpectedMaker FeatherS2 board.
"""
# pylint: disable=import-error,no-member,unused-argument,invalid-name
import time
import analogio
import board
import socketpool  # type: ignore
import wifi  # type: ignore
from adafruit_minimqtt.adafruit_minimqtt import MQTT, MMQTTException
from circuitpython_homie import HomieDevice, HomieNode
from circuitpython_homie.recipes import PropertyPercent

# Get wifi details and more from a secrets.py file
try:
    from secrets import wifi_settings, mqtt_settings
except ImportError as exc:
    raise RuntimeError(
        "WiFi and MQTT secrets are kept in secrets.py, please add them there!"
    ) from exc

print("Connecting to", wifi_settings["ssid"])
wifi.radio.connect(**wifi_settings)
print("Connected successfully!")
print("My IP address is", wifi.radio.ipv4_address)

print("Using MQTT broker: {}:{}".format(mqtt_settings["broker"], mqtt_settings["port"]))
pool = socketpool.SocketPool(wifi.radio)
mqtt_client = MQTT(**mqtt_settings, socket_pool=pool)

# create a light_sensor object for analog readings
light_sensor_pin = board.IO4  # change this accordingly
light_sensor = analogio.AnalogIn(light_sensor_pin)

# create the objects that describe our device
device = HomieDevice(mqtt_client, "my device name", "lib-light-sensor-test-id")
ambient_light_node = HomieNode("ambient-light", "Light Sensor")
ambient_light_property = PropertyPercent("brightness")

# append the objects to the device's attributes
ambient_light_node.properties.append(ambient_light_property)
device.nodes.append(ambient_light_node)

def on_disconnected(client: MQTT, user_data, rc):
    """Callback invoked when connection to broker is terminated."""
    print("Reconnecting to the broker.")
    client.reconnect()
    device.set_state("ready")

mqtt_client.on_disconnect = on_disconnected
mqtt_client.on_connect = lambda *args: print("Connected to the MQTT broker!")

# connect to the broker and publish/subscribe the device's topics
device.begin(keep_alive=3000)
# keep_alive must be set to avoid the device's `$state` being considered "lost"

# a forever loop
try:
    refresh_last = time.time()
    while True:
        try:
            now = time.time()
            if now - refresh_last > 0.5:  # refresh every 0.5 seconds
                refresh_last = now
                assert mqtt_client.is_connected()
                value = device.set_property(
                    ambient_light_property, light_sensor.value / 65535 * 100
                )
                print("light sensor value:", value, end="\r")
        except MMQTTException:
            print("\n!!! Connection with broker is lost.")
except KeyboardInterrupt:
    device.set_state("disconnected")
    print()  # move cursor to next line
    mqtt_client.on_disconnect = lambda *args: print("Disconnected from broker")
    mqtt_client.disconnect()


from typing import Optional
from user import User

class InconsistentException(Exception):
    """
    Exception raised when the social network is inconsistent.
    """
    def __init__(self, friend, user):
        Exception.__init__(self, f"Inconsistent social network. User '{friend}' is friend with user '{user}' but the "
                                 f"reverse is not true.")

class SocialNetwork:
    """
    Encapsulation of the users and their friends.
    """
    users: dict[str, User]
    common_friends: dict[(str, str), int]

    def __init__(self):
        """
        Initialises the map of users to empty and common friends to None.
        """
        self.users = {}
        self.common_friends = None

    def add_user(self, user_name: str) -> User:
        """
        Adds a user to the network if it does not exist.
        :param user_name: name of the user to add.
        :return: user object corresponding to the given name.
        """
        if user_name not in self.users:
            self.users[user_name] = User(user_name)
        return self.users[user_name]

    def add_friend(self, user_name: str, friend_name: str) -> User:
        """
        Associates a user to a new friend.
        :param user_name: name of user to associate.
        :param friend_name: name of friend to associate with.
        :return: user object corresponding to the friend.
        """
        user: User = self.add_user(user_name)
        friend: User = self.add_user(friend_name)
        user.add_friend(friend_name)
        # friend.add_friend(user_name)
        return friend

    def get_user(self, user_name: str) -> Optional[User]:
        """
        Gets the user associated with given name, if any.
        :param user_name: name of user to get.
        :return: user object corresponding to the name or None.
        """
        if user_name in self.users:
            return self.users[user_name]
        return None

    def get_friends(self, user_name: str) -> list[User]:
        """
        Gets the list of objects corresponding to the friends of the given user.
        :param user_name: name of user to get friends of, if any.
        :return: list of friend objects or empty list if user does not exist.
        """
        user: User = self.get_user(user_name)
        if user is None:
            return []
        result: list[User] = []
        for friend_name in user.friend_names:
            friend: User = self.get_user(friend_name)
            result += [friend]
        return result

    # Feature 2 i.
    def generate_common_friends(self, common_friends: dict[(str, str), int]) -> dict[str, list[int]]:
        """
        Generates a matrix containing the number of common friends for every pair of users in the social network.
        :param common_friends: dictionary containing a pair of users as the keys and the number of common friends as
        the values.
        :return: dictionary containing each username as keys and lists of common friends associated with
        the other users as the values.
        """
        common_matrix: dict = {}
        for name in sorted(self.users.keys()):
            common_matrix[name] = []
        for user in common_matrix:
            ln: dict[str, int] = {}
            if common_friends is None:
                common_friends: dict = self._compute_common_friends()
            for (n1, n2) in common_friends:
                if user == n1:
                    ln[n2] = common_friends[(n1, n2)]
            u: User = self.users[user]
            ln[user]: list = len(u.friend_names)
            for n2 in sorted(ln.keys()):
                common_matrix[user] += [ln[n2]]
        return common_matrix

    def get_common_friends(self) -> dict[(str, str), int]:
        """
        Gets the number of common friends for each pair of friends in the social network.
        :return: dictionary containing number of common friends for each pair.
        """
        if self.common_friends is None:
            self.common_friends = self._compute_common_friends()
        return self.common_friends

    def _compute_common_friends(self) -> dict[(str, str), int]:
        result: dict[(str, str), int] = {}
        for name1 in self.users.keys():
            for name2 in self.users.keys():
                if name1 != name2:
                    result[(name1, name2)] = self._compute_friends_count(name1, name2)
        return result

    def _compute_friends_count(self, name1: str, name2: str) -> int:
        l1: list = self.users[name1].friend_names
        l2: list = self.users[name2].friend_names
        common_num: int = 0
        for n in l1:
            if n == name1 or n == name2:
                continue
            if n in l2:
                common_num += 1
        return common_num

    # Feature 2 ii.
    def recommend_friend(self, user_name: str) -> Optional[str]:
        """
        Gets a username as a parameter and recommends a new friend based on the number of common friends with that
        potential friend.
        :param user_name: name of a user seeking a new friend.
        :return: recommended friend or None.
        """
        u: User = self.users[user_name]
        if not u.friend_names:
            return None
        cf: dict[(str, str), int] = self.get_common_friends()
        max_common: int = 0
        result = None
        for (n1, n2) in cf.keys():
            if n1 == user_name:
                if n2 in u.friend_names:
                    continue
                if cf[(n1, n2)] > max_common:
                    max_common = cf[(n1, n2)]
                    result = n2
        return result

    # Feature 3 iii.
    def get_user_relationship(self, name: str) -> dict[str, list[str]]:
        """
        Gets a list of friends corresponding to the given user.
        :param name: name of user whose friends shall be returned.
        :return: dictionary containing strings as keys and lists of strings as their value.
        """
        result: dict[str, list[str]] = {}
        user: User = self.users[name]
        result[name] = user.friend_names
        return result

    # Feature 3 iv.
    def get_indirect_relationships(self) -> dict[str, list[str]]:
        """
        Gets indirect relationships between users (i.e. friend of a friend of a user).
        :return: dictionary containing strings as keys and lists of strings as their value.
        """
        names = self.compute_friendships()
        indirect_friends: dict[str, list[str]] = {}
        for u in names:
            indirect_friends[u] = []
        for n1 in names:
            for n2 in names[n1]:
                for n3 in names[n2]:
                    if n1 != n3 and n3 not in names[n1]:
                        indirect_friends[n1] += [n3]
        return indirect_friends

    def compute_friendships(self) -> dict[str, list[str]]:
        """
        Adds the usernames (as keys) and their friends (as values) to a dictionary 'names'.
        :return: dictionary containing strings as keys and lists of strings as their value.
        """
        names: dict[str, list[str]] = {}
        for name in sorted(self.users.keys()):
            user: User = self.users[name]
            names[name]: list = user.friend_names
        return names

    # Feature 3 v.
    def validate(self):
        """
        Checks if a user is friends with another user and vice versa.
        :return: None
        """
        for user in self.users:
            u: User = self.users[user]
            for friend in u.friend_names:
                f: User = self.users[friend]
                if user in f.friend_names:
                    pass
                else:
                    raise InconsistentException(friend, user)


import nltk
from bs4 import BeautifulSoup
from write import writeDict

def buildRawFreqVectors():
    f1=open('e960401.htm')
    t=f1.read()
    t_lower=t.lower()

    '''lxml is the best available HTML parser for BeautifulSoup,
    lxml deletes markup and represents letters with clitics correctly'''
    soup = BeautifulSoup(t_lower, 'lxml')
    text = soup.get_text()
    tokens=nltk.Text(nltk.word_tokenize(text))
    vocabulary=list(set(tokens))

    f2=open('contexts.txt', encoding='utf-8')
    contexts=f2.readlines()

    rawFreqVectorsDict={}

    for context in contexts:
        words=context.split()
        vector=[]
        for voc in vocabulary:
            if voc in words[1:]:
                vector.append(words[1:].count(voc))
            else:
                vector.append(0)
        rawFreqVectorsDict[words[0]]=vector

    return rawFreqVectorsDict

'''test if run as application'''
if __name__=='__main__':
    rawFreqVectorsDict=buildRawFreqVectors()
    writeDict(rawFreqVectorsDict, 'rawFrequencyVectors.txt')


from typing import List

from helm.common.authentication import Authentication
from helm.proxy.services.server_service import ServerService
from helm.benchmark.metrics.metric_service import MetricService
from .tokenizer_service import TokenizerService

TEST_PROMPT: str = (
    "The Center for Research on Foundation Models (CRFM) is "
    "an interdisciplinary initiative born out of the Stanford "
    "Institute for Human-Centered Artificial Intelligence (HAI) "
    "that aims to make fundamental advances in the study, development, "
    "and deployment of foundation models."
)

GPT2_TEST_TOKEN_IDS: List[int] = [
    464,
    3337,
    329,
    4992,
    319,
    5693,
    32329,
    357,
    9419,
    23264,
    8,
    318,
    281,
    987,
    40625,
    10219,
    4642,
    503,
    286,
    262,
    13863,
    5136,
    329,
    5524,
    12,
    19085,
    1068,
    35941,
    9345,
    357,
    7801,
    40,
    8,
    326,
    12031,
    284,
    787,
    7531,
    14901,
    287,
    262,
    2050,
    11,
    2478,
    11,
    290,
    14833,
    286,
    8489,
    4981,
    13,
]

GPT2_TEST_TOKENS: List[str] = [
    "The",
    " Center",
    " for",
    " Research",
    " on",
    " Foundation",
    " Models",
    " (",
    "CR",
    "FM",
    ")",
    " is",
    " an",
    " inter",
    "disciplinary",
    " initiative",
    " born",
    " out",
    " of",
    " the",
    " Stanford",
    " Institute",
    " for",
    " Human",
    "-",
    "Cent",
    "ered",
    " Artificial",
    " Intelligence",
    " (",
    "HA",
    "I",
    ")",
    " that",
    " aims",
    " to",
    " make",
    " fundamental",
    " advances",
    " in",
    " the",
    " study",
    ",",
    " development",
    ",",
    " and",
    " deployment",
    " of",
    " foundation",
    " models",
    ".",
]

GPT4_TEST_TOKEN_IDS: List[int] = [
    791,
    5955,
    369,
    8483,
    389,
    5114,
    27972,
    320,
    9150,
    26691,
    8,
    374,
    459,
    88419,
    20770,
    9405,
    704,
    315,
    279,
    31788,
    10181,
    369,
    11344,
    7813,
    87143,
    59294,
    22107,
    320,
    39,
    15836,
    8,
    430,
    22262,
    311,
    1304,
    16188,
    31003,
    304,
    279,
    4007,
    11,
    4500,
    11,
    323,
    24047,
    315,
    16665,
    4211,
    13,
]

GPT4_TEST_TOKENS: List[str] = [
    "The",
    " Center",
    " for",
    " Research",
    " on",
    " Foundation",
    " Models",
    " (",
    "CR",
    "FM",
    ")",
    " is",
    " an",
    " interdisciplinary",
    " initiative",
    " born",
    " out",
    " of",
    " the",
    " Stanford",
    " Institute",
    " for",
    " Human",
    "-C",
    "entered",
    " Artificial",
    " Intelligence",
    " (",
    "H",
    "AI",
    ")",
    " that",
    " aims",
    " to",
    " make",
    " fundamental",
    " advances",
    " in",
    " the",
    " study",
    ",",
    " development",
    ",",
    " and",
    " deployment",
    " of",
    " foundation",
    " models",
    ".",
]

def get_tokenizer_service(local_path: str) -> TokenizerService:
    service = ServerService(base_path=local_path, root_mode=True)
    return MetricService(service, Authentication("test"))


import pandas as pd
from botocore.exceptions import ClientError

def get_most_recent_date(table):
    """
    If the table contains data, returns the most recent date; else, returns None.
    """
    response = table.scan()

    data = response['Items']
    most_recent_date = pd.Timestamp("2000-01-01")

    if data:
        data_dates = [item['date'] for item in data]
        for date in data_dates:
            date_timestamp = pd.Timestamp(date)
            if date_timestamp > most_recent_date:
                most_recent_date = date_timestamp
        return most_recent_date
    else:
        return None

def add_rows_to_database(rows_to_add: pd.DataFrame, table):
    """
    Adds dataset rows to the database; returns number of rows added to the database.
    """
    batch_size = 100
    batch = []
    rows_added = 0

    for row in rows_to_add.itertuples():
        if len(batch) >= batch_size:
            write_to_database(batch, table)
            batch.clear()
        batch.append(row)
        rows_added += 1

    if batch:
        write_to_database(batch, table)

    return rows_added

def write_to_database(rows, table):
    """
    Writes one batch of up to 100 items to the database.
    """
    try:
        with table.batch_writer() as batch:
            for row in rows:
                batch.put_item(
                    Item={
                        'date': str(row.date),
                        'cases': row.cases,
                        'deaths': row.deaths,
                        'recovered': row.recovered,
                    }
                )
    except ClientError as e:
        print(e)
        print("Error executing batch writer.")

# class CovidDayStats:
#     def __init__(self, datestring, cases, deaths, recovered):
#         self.datestring = datestring
#         self.cases = cases
#         self.deaths = deaths
#         self.recovered = recovered
#
#     def date_as_timestamp(self):
#         date = pd.Timestamp(self.datestring)
#         return date
#
#     def __str__(self):
#         return f"date: {self.datestring} / cases: {self.cases} / deaths: {self.deaths} / recovered: {self.recovered}"
#
#
# class CovidDataContainer:
#     def __init__(self):
#         self.data_dict = {}
#
#     def add_day(self, day):
#         self.data_dict[day.date_as_timestamp()] = day
#
#     def get_day_with_timestamp(self, timestamp):
#         return self.data_dict[timestamp]
#
#     def get_most_recent_date(self):
#         most_recent_date = pd.Timestamp("2010-01-01")
#         for key in self.data_dict:
#             key_timestamp = pd.Timestamp(key)
#             if key_timestamp > most_recent_date:
#                 most_recent_date = key_timestamp
#         return most_recent_date
#
#     def __len__(self):
#         return len(self.data_dict)
#
#     def __str__(self):
#         return f"A Covid data container with {len(self.data_dict)} rows."


import queue
import random
import threading
import time

import pythonosc.udp_client

def get_random_hr():
	return random.normalvariate(80, 15)

def hr_beat_thread(q, beat):
	baseline = q.get()
	while True:
		try:
			baseline = q.get(block=False)
		except queue.Empty: pass

		variance = random.normalvariate(0, 2)

		st = 60 / (baseline + variance)
		if st > 0: time.sleep(st)
		beat()

def main():
	osc_client = pythonosc.udp_client.SimpleUDPClient('10.88.0.16', 11543)
	baseline_exp = 0

	last_tick_time = time.monotonic()
	def beat():
		nonlocal last_tick_time
		now = time.monotonic()
		dt = now - last_tick_time
		last_tick_time = now

		bpm = 60 / dt
		print("tick %.2f (%.3f)" % (bpm, dt))
		osc_client.send_message('/heartbeat', bpm)

	hr_beat_queue = queue.Queue()
	threading.Thread(target=hr_beat_thread, args=(hr_beat_queue, beat)).start()

	while True:
		now = time.monotonic()

		if now > baseline_exp:
			baseline = get_random_hr()
			duration = random.normalvariate(30, 10)
			baseline_exp = now + duration
			print("Baseline now %d for %2.1fs" % (baseline, duration))
			hr_beat_queue.put(baseline)

		time.sleep(1)

if __name__ == '__main__':
	main()


import os
import imageio
import matplotlib.pyplot as plt
from PIL import Image
from pydotplus import graph_from_dot_data
from sklearn.tree import export_graphviz

# 可视化整颗决策树
# filled=Ture添加颜色，rounded增加边框圆角
# out_file=None直接把数据赋给dot_data，不产生中间文件.dot
dot_data = export_graphviz(tree, filled=True, rounded=True,
                           class_names=['山鸢尾', '杂色鸢尾', '维吉尼亚鸢尾'],
                           feature_names=['花瓣长度', '花瓣宽度'], out_file=None)
graph = graph_from_dot_data(dot_data)
if not os.path.exists('代码-决策树.png'):
    graph.write_png('代码-决策树.png')

# 等比例改变图片大小
def cut_img(img_path, new_width, new_height=None):
    img = Image.open(img_path)
    width, height = img.size
    if new_height is None:
        new_height = int(height * (new_width / width))
    new_img = img.resize((new_width, new_height), Image.ANTIALIAS)
    os.remove(img_path)
    new_img.save(img_path)
    new_img.close()

cut_img('代码-决策树.png', 500)

# 只是为了展示图片，没有其他作用
img = imageio.imread('代码-决策树.png')
plt.imshow(img)
plt.show()


from .client import clients
from .handler import handler
import asyncio
import logging

logging.basicConfig(level=logging.DEBUG)

async def handle_incoming(reader, writer):
    client = clients.new(reader, writer, handler)
    while True:
        line = await reader.readline()
        if reader.at_eof() or client not in client.manager:
            logging.info("Client disconnected: {}".format(client.id))
            return client.done()

        else:
            handler.handle(client, line)

loop = asyncio.get_event_loop()
clients.loop = loop

coro = asyncio.start_server(handle_incoming, "127.0.0.1", 6667)
server = loop.run_until_complete(coro)
loop.run_forever()


# coding=utf-8
import logging
import os
import flask_restplus
from flask import request, redirect

from file_management import services
from file_management.extensions import Namespace
from file_management.helpers import decode_token
from file_management.services.pending_register import send_confirm_email
from file_management.repositories.files import insert, utils

# from file_management.api import requests, responses
from . import requests, responses

__author__ = 'LongHB'
_logger = logging.getLogger(__name__)

ns = Namespace('register', description='Register operations')

_register_req = ns.model('register_req', requests.register_user_req)
_register_res = ns.model('register_res', responses.pending_register_res)

@ns.route('/', methods=['GET', 'POST'])
class Registers(flask_restplus.Resource):
    @ns.expect(_register_req, validate=True)
    @ns.marshal_with(_register_res)
    def post(self):
        "validate register data, add data to pending register table and send confirm email"
        data = request.args or request.json
        pending_register = services.pending_register.create_pending_register(
            **data)
        send_confirm_email(**data)
        return pending_register

@ns.route('/confirm_email/<token>', methods=['GET'])
class Confirm_email(flask_restplus.Resource):

    def get(self, token):
        "checking jwt token in param and add new user to user table"
        email = decode_token(token)
        user = services.user.confirm_user_by_email(email)

        user_inf = user.to_display_dict()
        user_id = user_inf['user_id']
        # create home
        insert.insert(str(user_id), "home", 0, "0", str(user_id), "folder", "", "")
        folders = utils.get_ancestors(str(user_id))
        path_upload = '/'.join(folders)
        if not os.path.exists(path_upload):
            os.makedirs(path_upload)
        return redirect("http://ufile.ml")


#! /usr/bin/env python3

from abc import ABC, abstractmethod
import argparse
import datetime
import dotenv
import logging
import logging.handlers
import numpy as np
from typing import Optional, Tuple
import os
import pathlib
import pyaudio
from scipy.signal import argrelmax
import subprocess
import shlex
import time
import wave

RATE: int = 8000            # サンプリング周波数、フレーム数
CHUNK: int = int(RATE / 2)  # PyAudioで一度に取得するサンプリング数. サンプリング周波数の半分。0.5秒分
FORMAT = pyaudio.paInt16  # フォーマット
CHANNELS: int = 1  # チャンネル数 （モノラル）

FREQ_1ST: float = 849.0  # ピンポーンのピンの周波数
FREQ_2ND: float = 680.0  # ピンポーンのポーンの周波数

WINDOW_IN_SECONDS: float = 1.5  # ピンポーンを検出を試みる区間の秒数(window)
WINDOW_IN_FRAMES: int = int(WINDOW_IN_SECONDS * RATE)  # ピンポーン検出を試みる区間のフレーム数

logger: Optional[logging.Logger] = None

# 音入力のインターフェース
class FrameReader(ABC):
    @abstractmethod
    def open(self) -> bool:
        pass

    @abstractmethod
    def should_open_again(self) -> bool:
        pass

    @abstractmethod
    def read(self, chunk=CHUNK) -> Optional[bytes]:
        pass

    @abstractmethod
    def close(self) -> bool:
        pass

# 音入力としてwavファイルを利用するときのFrameReader。デバッグ用
class WavFrameReader(FrameReader):
    def __init__(self, wavFilePath: pathlib.Path) -> None:
        super().__init__()

        self._wavFilePath = wavFilePath
        self._wavFile = None

    def open(self) -> bool:
        self._wavFile = wave.open(str(self._wavFilePath), 'rb')
        if not self._wavFile:
            logger.error(f"Failed to open the wav file. {self._wavFilePath}")
            return False

        if self._wavFile.getframerate() != RATE:
            logger.error(f"Invalid framerate . {self._wavFile.getframerate()}")
            return False

        if self._wavFile.getnchannels() != CHANNELS:
            logger.error(f"Invalid channels . {self._wavFile.getnchannels()}")
            return False

        if self._wavFile.getsampwidth() != 2:
            logger.error(f"Invalid width . {self._wavFile.getsamwidth()}")
            return False

        return True

    def should_open_again(self) -> bool:
        return False

    def read(self, chunk=CHUNK) -> Optional[bytes]:
        frames = self._wavFile.readframes(chunk)
        if not frames:
            return None

        return frames

    def close(self) -> bool:
        self._wavFile.close()
        self._wavFile = None
        return True

# 音入力としてmicを利用するときのFrameReader。
class MicFrameReader(FrameReader):
    def __init__(self) -> None:
        super().__init__()
        self._p = None
        self._stream = None

    def open(self) -> bool:
        self._p = pyaudio.PyAudio()

        input_device_index = -1

        for host_index in range(0, self._p.get_host_api_count()):
            logger.info(f"host: {self._p.get_host_api_info_by_index(host_index)}")
            for device_index in range(0, self._p.get_host_api_info_by_index(host_index)['deviceCount']):
                device_info = self._p.get_device_info_by_host_api_device_index(host_index, device_index)
                logger.info(f"device: {device_info}")

                if device_info['name'] == os.environ.get("AUDIO_DEVICE"):
                    input_device_index = device_info["index"]
                    break
            else:
                continue
            break

        if input_device_index < 0:
            self.close()
            return False

        logger.info(f"========= {input_device_index}")
        try:
            self._stream = self._p.open(
                format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                input_device_index=input_device_index,
                frames_per_buffer=CHUNK
            )
        except Exception as e:
            logger.error(e)
            self.close()
            return False

        return True

    def should_open_again(self) -> bool:
        return True

    def read(self, chunk=CHUNK) -> Optional[bytes]:
        if not self._stream.is_active():
            return None

        frames = self._stream.read(CHUNK)
        return frames

    def close(self) -> bool:
        if self._stream:
            self._stream.stop_stream()
            self._stream.close()
            self._stream = None

        if self._p:
            self._p.terminate()
            self._p = None

        return True

def setup_logger(name, console=False, level=logging.INFO, logfile='LOGFILENAME.txt') -> logging.Logger:

    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)

    fmt = "%(asctime)s %(thread)d %(levelname)s %(name)s :%(message)s"

    # create file handler which logs even DEBUG messages
    fh = logging.handlers.RotatingFileHandler(logfile, maxBytes=1000000, backupCount=10)
    fh.setLevel(level)
    fh_formatter = logging.Formatter(fmt)
    fh.setFormatter(fh_formatter)
    logger.addHandler(fh)

    if console:
        ch = logging.StreamHandler()
        ch.setLevel(level)
        ch_formatter = logging.Formatter(fmt)
        ch.setFormatter(ch_formatter)
        logger.addHandler(ch)

    return logger

# FFTをする。
def fft(frames: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:

    x = np.fft.fft(frames)
    freq = np.fft.fftfreq(len(frames), d=1.0 / RATE)

    x = x[:int(len(x) / 2)]
    freq = freq[:int(len(freq) / 2)]
    amp = np.sqrt(x.real ** 2 + x.imag ** 2)

    return (amp, freq)

# Peakを求める。
def findpeaks(x: np.ndarray, y: np.ndarray, n: int = 50, w: int = 100) -> Tuple[np.ndarray, np.ndarray]:
    index_all = argrelmax(y, order=w)                # scipyのピーク検出
    index = []                                                      # ピーク指標の空リスト
    peaks = []                                                      # ピーク値の空リスト

    # n個分のピーク情報(指標、値）を格納
    for i in range(min(len(index_all[0]), n)):
        index.append(index_all[0][i])
        peaks.append(y[index_all[0][i]])
    index = np.array(index) * x[1]                                  # xの分解能x[1]をかけて指標を物理軸に変換
    peaks = np.array(peaks)

    return index, peaks

# ピークに、指定された周波数が含まれるかチェック
def has_freq(freqs: np.ndarray, peaks: np.ndarray, target: float) -> bool:
    for freq, peak in zip(freqs.tolist(), peaks.tolist()):
        if target - 2 <= freq and freq <= target + 2:
            logger.info(f"peak {target}, {freq} {peak}")
            if peak > 20000:
                return True

    return False

    # l = freq.tolist()
    # res = list(filter(lambda x: target - 5 <= x and x <= target + 5, l))
    # return bool(res)

# Alexa Echo に 話させる。
def speak_alexa() -> None:
    device = os.environ.get("ALEXA_DEVICE")
    cmd = f'./alexa_remote_control.sh -d "{device}" -e "speak: チャイムが鳴りました"'
    logging.debug(cmd)
    # cmd = './alexa_remote_control.sh -d "ALL" -e "speak: チャイムが鳴りました"'
    subprocess.run(shlex.split(cmd))

def save_wav(folder: pathlib.Path, frames) -> None:
    now = datetime.datetime.now()
    filename = "detect-" + now.strftime('%Y%m%d_%H%M%S') + '.wav'
    path = pathlib.Path(folder, filename)
    logging.info(f"save wav file. {path}")
    wav_file = wave.open(str(path), "wb")
    wav_file.setnchannels(CHANNELS)
    wav_file.setsampwidth(2)
    wav_file.setframerate(RATE)
    wav_file.writeframes(b"".join(frames))
    wav_file.close()

def main() -> None:
    dotenv.load_dotenv(verbose=True)

    parser = argparse.ArgumentParser()
    parser.add_argument("-v", "--verbose", help="verbose", action="count", default=0)
    parser.add_argument("-c", "--console", help="console output", action='store_true')
    parser.add_argument("-w", "--wav_folder", help="Output folder to store detected wav filess", default=".")
    args = parser.parse_args()

    logging_level = logging.INFO if args.verbose == 0 else logging.DEBUG
    global logger
    logger = setup_logger(__name__, args.console, logging_level, "intercom.log")

    wav_folder = pathlib.Path(args.wav_folder)
    if (not wav_folder.exists()):
        wav_folder.mkdir(parents=True)

    # frame_reader: FrameReader = WavFrameReader('test-data/sample1.wav')
    frame_reader: FrameReader = MicFrameReader()

    # Open FrameReader
    while True:
        logger.info("Try to open")
        if frame_reader.open():
            logger.info("Success")
            break

        if not frame_reader.should_open_again():
            frame_reader.close()
            return

        time.sleep(1)

    counter: int = 0
    skip_count: int = 0
    frames_list = []
    np_frames_list = []
    while True:
        # Read frames
        frames = frame_reader.read()
        if not frames:
            logger.info("End of frames")
            break

        logger.debug(f"frames: {type(frames), {len(frames)}}")
        if counter == 0:
            logger.info(".")
        counter = counter + 1 if counter < 10 else 0

        if frames is None:
            logger.info("End of frames")
            break

        skip_count = max(0, skip_count - 1)

        frames_list.append(frames)
        np_frames_list.append(np.frombuffer(frames, dtype='int16'))

        if len(frames_list) * CHUNK < WINDOW_IN_FRAMES:
            continue

        if len(frames_list) * CHUNK > WINDOW_IN_FRAMES:
            frames_list = frames_list[1:]
            np_frames_list = np_frames_list[1:]

        # FFT
        amp, freq = fft(np.concatenate(np_frames_list))

        # Find peaks
        index, peaks = findpeaks(freq, amp)

        # Detect
        if has_freq(index, peaks, FREQ_1ST) and has_freq(index, peaks, FREQ_2ND):
            logger.info(f"detect!!! {skip_count}")

            if skip_count == 0:
                speak_alexa()
                save_wav(wav_folder, frames_list)

            skip_count = 5

    frame_reader.close()

if __name__ == "__main__":
    main()

    # dotenv.load_dotenv(verbose=True)
    # speak_alexa()


# -*- coding: utf-8 -*-
"""
Created on Thu Dec 26 12:59:38 2019

@author: thiya
"""

import nltk
import random

#cleaning the text
import re
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
from nltk.stem import WordNetLemmatizer

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

ps = PorterStemmer()
wordnet = WordNetLemmatizer()

# Keyword Matching
GREETING_INPUTS = ("hello", "hi", "greetings", "sup", "what's up","hey",)
GREETING_RESPONSES = ["hi", "hey", "*nods*", "hi there", "hello", "I am glad! You are talking to me"]

#Reading in the corpus
with open('chatbot.txt','r', encoding='utf8', errors ='ignore') as fin:
    raw = fin.read()

sentences = nltk.sent_tokenize(raw)

#Preprocessing
corpus=[]
for i in range(len(sentences)):
    prereview = re.sub('[^a-zA-Z]', ' ', sentences[i])
    prereview = prereview.lower()
    prereview = prereview.split()
    prereview = [wordnet.lemmatize(word) for word in prereview if word not in set(stopwords.words("english"))]
    prereview = ' '.join(prereview)
    corpus.append(prereview)

def greeting(greet):
    """If user's input is a greeting, return a greeting response"""
    for word in greet.split():
        if word.lower() in GREETING_INPUTS:
            return random.choice(GREETING_RESPONSES)

def response(user_response):
    review = re.sub('[^a-zA-Z]', ' ', user_response)
    review = review.lower()
    review = review.split()
    review = [wordnet.lemmatize(word) for word in review if word not in set(stopwords.words("english"))]
    review = ' '.join(review)
    corpus.append(review)

    #creating the TF-IDF model
    cv = TfidfVectorizer()
    X = cv.fit_transform(corpus)

    vals = cosine_similarity(X[-1], X)
    idx=vals.argsort()[0][-2]
    flat = vals.flatten()
    flat2=flat.copy()
    flat.sort()
    req_tfidf = flat[-2]

    if(req_tfidf==0):
        robo_response="I am sorry! I don't understand you"
    else:
        robo_response = sentences[idx]

    corpus.pop()
    return robo_response

flag=True
print("Flexi: My name is Flexi. I will answer your queries about Chatbots. If you want to exit, type Bye!")
while(flag==True):
    user_response = input("You :")
    user_response=user_response.lower()
    if(user_response!='bye'):
        if(user_response=='thanks' or user_response=='thank you' ):
            flag=False
            print("Flexi: You are welcome..")
        else:
            if(greeting(user_response)!=None):
                print("Flexi: "+greeting(user_response))
            else:
                print("Flexi: ",end="")
                print(response(user_response))
#                sent_tokens.remove(user_response)
    else:
        flag=False
        print("Flexi: Bye! take care..")


import numpy as np
from sklearn.svm import SVC
from sklearn.preprocessing import Normalizer
import cv2
from PIL import Image
from numpy import expand_dims
from keras.models import load_model
import pickle
import datetime

old_value = ""

def get_face_embedding(model,face_pixels):
    face_pixels = face_pixels.astype('float32')
    mean, std = face_pixels.mean(), face_pixels.std()
    face_pixels = ( face_pixels - mean ) / std
    sample = expand_dims(face_pixels,axis = 0)
    yhat = model.predict(sample)
    return yhat[0]

model = load_model('Model/Facenet_keras.h5')
with open('./Model/Finalized_FaceVerificationV0.1.model','rb') as file:
    svc_model = pickle.load(file)

def User_Login(img,result_face_array,old_value,i):

    time1 = datetime.datetime.now()

    x, y, width, height = result_face_array[0]['box']
    x1, y1, x2, y2 = abs(x), abs(y), x + width, y + height
    image_arr = np.array(img)
    face = image_arr[y1:y2, x1:x2]
    cropped_face = Image.fromarray(face)
    cropped_face_resize = cropped_face.resize(((160,160)))
    face_array = np.array(cropped_face_resize)

    embedding = get_face_embedding(model,face_array)
    in_encoder = Normalizer(norm ='l2')
    new_embedding = in_encoder.transform(embedding.reshape(-1,1))
    embedding_result = svc_model.predict(new_embedding.reshape(1,128))

    print('Embedding Result: ',embedding_result)
    new_value = embedding_result[0]
    print('Old Value: ',old_value)
    print('New Value: ',new_value)
    capture_it = False
    if old_value != new_value:
        capture_it = True

    bounding_box = result_face_array[0]['box']
    return new_value, bounding_box,capture_it


import pygame
from pygame.sprite import Sprite

# 存放飞船相关信息
class Ship(Sprite):

    # 初始化飞船
    def __init__(self, screen):
        super().__init__()
        self.screen = screen
        self.ship_speed_factor = 0.5
        # 设置飞船图像
        self.image = pygame.image.load("images/ship.bmp")

        # 获取图片和屏幕的形状
        self.rect = self.image.get_rect()
        self.screen_rect = self.screen.get_rect()

        # 设置飞船的初始位置(底部中间),conterx表示图像的水平中心，bottom表示图像底部
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        # rect的centerx属性只能存储整数，所以设置一个临时变量存储飞船当前位置
        self.center = float(self.rect.centerx)

        # 移动标志
        self.moving_right = False
        self.moving_left = False

    # 更新飞船位置
    def update(self):
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.center += self.ship_speed_factor
        if self.moving_left and self.rect.left > 0:
            self.center -= self.ship_speed_factor
        self.rect.centerx = self.center

    def blitme(self):
        # 放置飞船，把self.images放到self.rect位置
        self.screen.blit(self.image, self.rect)

    # 将飞船左右居中
    def center_ship(self):
        self.center = self.screen_rect.centerx


from __future__ import annotations

from django.conf import settings
from django.http import HttpRequest

def get_scheme(request: HttpRequest) -> str:
    scheme = "https"
    if settings.DEBUG and request is not None:
        if "HTTP_X_FORWARDED_PROTO" in request.META:
            scheme = request.META["HTTP_X_FORWARDED_PROTO"]
        else:
            scheme = request.scheme
    return scheme


#!/usr/bin/env python3

import numpy as np
import matplotlib.pyplot as plt

import scipy.special as spl
import numpy.linalg as linalg

def f(g, w, x):
    return (x / w) ** (g - 1) * np.exp(- x / w) / (w * spl.gamma(g))

def fy(y, i, params):
    (gt, wt, gp, wp) = params
    return f(gt[i], wt[i], y[0]) * f(gp[i], wp[i], y[1])

def compute_alpha(A, y, params):
    num_states = A.shape[0]
    Lk = y.shape[0]
    alpha = np.zeros((Lk, num_states))
    alpha[0, 0] = 1
    for n in range(1, Lk):
        for j in range(num_states):
            alpha[n, j] = np.sum(alpha[n-1, :] * A[:, j]) * fy(y[n], j, params)
    return alpha

def compute_beta(A, y, params):
    num_states = A.shape[0]
    Lk = y.shape[0]
    beta= np.zeros((Lk, num_states))
    beta[-1, :] = 1
    for n in range(Lk-2, 0, -1):
        for i in range(num_states):
            for j in range(num_states):
                beta[n, i] += A[i, j] * fy(y[n+1], j, params) * beta[n+1, j]
    return beta

def update(A, y, alpha, beta, params):
    num_states = A.shape[0]
    Lk = y.shape[0]
    gtwt = np.zeros(num_states)
    gtwt2 = np.zeros(num_states)
    gpwp = np.zeros(num_states)
    gpwp2 = np.zeros(num_states)
    A_new = A.copy()
    (gt, wt, gp, wp) = params
    for i in range(num_states):
        for j in range(num_states):
            fyn = []
            for n in range(Lk):
                fyn.append( fy(y[n], j, params) )
            fyn = np.array(fyn)
            A_new[i, j] = np.sum(alpha[:-1, i] * A[i, j] * fyn[1:] * beta[1:, j]) / np.sum(alpha[:-1, i] * beta[:-1, i])
        gtwt[i] = np.sum(alpha[:, i] * beta[:, i] * y[:, 0]) / np.sum(alpha[:-1, i] * beta[:-1, i])
        gpwp[i] = np.sum(alpha[:, i] * beta[:, i] * y[:, 1]) / np.sum(alpha[:-1, i] * beta[:-1, i])
        gtwt2[i] = np.sum(alpha[:, i] * beta[:, i] * (y[:, 0] - gtwt[i])**2) / np.sum(alpha[:-1, i] * beta[:-1, i])
        gtwt2[i] = np.sum(alpha[:, i] * beta[:, i] * (y[:, 1] - gpwp[i])**2) / np.sum(alpha[:-1, i] * beta[:-1, i])

    # Get back parameters
    i1 = (abs(gtwt) > 1e-5)
    i2 = (abs(gpwp) > 1e-5)
    wt[i1] = gtwt2[i1] / gtwt[i1]
    wp[i2] = gpwp2[i2] / gpwp[i2]
    i3 = (abs(wt) > 1e-5)
    i4 = (abs(wp) > 1e-5)
    gt[i3] = gtwt[i3] / wt[i3]
    gp[i4] = gpwp[i4] / wp[i4]

    new_params = (gt, wt, gp, wp)
    return (A_new, new_params)

if __name__ == '__main__':

    y = np.load('bursts_5.npy')

    num_states = 3
    A = np.ones((num_states, num_states)) / num_states
    q = np.ones(num_states) / num_states

    dmin = y[:, 0].min()
    dmax = y[:, 0].max()
    bmin = y[:, 0].min()
    bmax = y[:, 0].max()

    #mut = np.arange(num_states) * (dmax - dmin) / (num_states+1) + 1
    #mup = np.arange(num_states) * (bmax - bmin) / (num_states+1) + 1
    #sigmat = (dmax - dmin) / (5 * (num_states+1))
    #sigmap = (bmax - bmin) / (5 * (num_states+1))

    mut = np.array([10, 30, 100])
    mup = np.array([200, 800, 2000])
    sigmat = np.array([100, 900, 1e4])
    sigmap = np.array([200, 800, 2000])

    # Initialize params
    wt = sigmat ** 2 / mut
    wp = sigmap ** 2 / mup
    gt = mut / wt
    gp = mup / wp

    params = (gt, wt, gp, wp)

    for k in range(1):
        alpha = compute_alpha(A, y, params)
        beta = compute_beta(A, y, params)
        print(alpha)
        print(beta)
        (A_new, new_params) = update(A, y, alpha, beta, params)
        A = A_new
        params = new_params
        print(k)
        print(params)
        print(A)
        print('-------')

    (eigvals, eigvecs) = linalg.eig(A)
    index = np.argmin(abs(eigvals - 1))    # Find the eigenvalue closest to 1
    q = eigvecs[:, index]

    print(q)


import time
import copy
from typing import List

from cassandra import ConsistencyLevel
from cassandra.cluster import Cluster, Session
from cassandra.query import SimpleStatement

from market_app.models.api_models import ApartmentOfferAveragePrice, CompanyStatisticResult
from market_app.models.db_models.cassandra_models import Offer, Apartment, Owner
from market_app.repositories.uuid_util import convert_text_into_uuid, generate_uuid, convert_uuid_into_text

class CassandraOfferRepository:
    def __init__(self, session: Session):
        self.session = session
        self.key_space = "market_app"

    def get_offers_by_city_and_address(self, city: str, address: str) -> List[Offer]:
        query = f"SELECT * FROM {self.key_space}.offers_by_city_and_street WHERE address_city = %s AND address_street = %s"
        rows = self.session.execute(query, (city, address))
        return self.__map_from_rows(rows);

    def get_offers_by_city_and_price_range(self, city: str, min_price: float, max_price: float) -> List[Offer]:
        query = SimpleStatement(
            f"SELECT * FROM {self.key_space}.offers_by_city_and_price WHERE address_city=%s AND price>=%s AND price<=%s",
            consistency_level=ConsistencyLevel.QUORUM
        )
        rows = self.session.execute(query, (city, min_price, max_price))
        return self.__map_from_rows(rows)

    def get_offers_by_company_name(self, company_name: str) -> List[Offer]:
        query = SimpleStatement(
            "SELECT * FROM offers_by_company_name WHERE company_name=%s",
            consistency_level=ConsistencyLevel.QUORUM
        )
        rows = self.session.execute(query, company_name)
        return self.__map_from_rows(rows)

    def get_offers_by_city_and_price_and_id(self, city: str, price: float, offer_id: str) -> Offer | None:
        offer_uuid = convert_text_into_uuid(offer_id)
        query = SimpleStatement(
            "SELECT * FROM offers_by_city_and_price WHERE address_city=%s AND price=%s AND offer_id=%s",
            consistency_level=ConsistencyLevel.QUORUM
        )
        row = self.session.execute(query, (city, price, offer_uuid)).one()
        if not row:
            return None
        return self.__map_from_row(row)

    def get_offer_basic_by_id(self, offer_id: str) -> Offer:
        offer_uuid = convert_text_into_uuid(offer_id)
        query = SimpleStatement(
            "SELECT * FROM offers_basic WHERE offer_id=%s",
            consistency_level=ConsistencyLevel.QUORUM
        )
        rows = self.session.execute(query, (offer_uuid,))
        mapped_rows = self.__map_from_rows(rows)
        return mapped_rows[0] if len(mapped_rows) > 0 else None

    def get_average_price_by_city(self, city_filter) -> List[ApartmentOfferAveragePrice]:
        city_filter_query = ""
        if city_filter:
            city_filter_query = f" WHERE address_city ='{city_filter}'";

        query = SimpleStatement(
            f"SELECT address_city, avg(price) as avg_price, avg(price/area) as avg_price_per_m2"
            f" FROM offers_by_city_and_price"
            f"{city_filter_query}"
            f" group by address_city"
        )

        rows = self.session.execute(query)
        return list(map(lambda x: self.__map_average_price(x), rows))

    def get_statistic_by_company(self, company_filter) -> List[CompanyStatisticResult]:
        company_filter_query = ""
        if company_filter:
            company_filter_query = f" WHERE company_name ='{company_filter}'";

        query = SimpleStatement(f"select company_name,"
                                f"avg(price)  as avg_price,"
                                f"avg(price/area) as avg_price_per_m2,"
                                f"count(*) as sales_offer_count "
                                f"{company_filter_query} "
                                f"from market_app.offers_by_company_name group by company_name"
                                )

        rows = self.session.execute(query)
        return list(map(lambda x: self.__map_company_statistic(x), rows))

    def delete(self, offer_id: str):
        offer_basic = self.get_offer_basic_by_id(offer_id)
        if not offer_basic:
            raise ValueError("Not found offer with provided id")

        query = SimpleStatement(
            "DELETE FROM offers_by_company_name WHERE company_name=%s AND offer_id=%s",
            consistency_level=ConsistencyLevel.QUORUM
        )
        offer_id_uuid = convert_text_into_uuid(offer_id)
        if offer_basic.company_name:
            self.session.execute(query, (offer_basic.company_name, offer_id_uuid))

        query = SimpleStatement(
            "DELETE FROM offers_by_city_and_street WHERE address_city=%s AND address_street=%s AND offer_id=%s",
            consistency_level=ConsistencyLevel.QUORUM
        )
        self.session.execute(query, (offer_basic.address_city, offer_basic.address_street, offer_id_uuid))

        query = SimpleStatement(
            "DELETE FROM offers_by_city_and_price WHERE address_city=%s AND price=%s AND offer_id=%s",
            consistency_level=ConsistencyLevel.QUORUM
        )
        self.session.execute(query, (offer_basic.address_city, offer_basic.price, offer_id_uuid))

    def insert(self, offer: Offer) -> Offer:
        offer_id = generate_uuid()
        copied_offer = copy.copy(offer)
        copied_offer.offer_id = offer_id
        self.__insert_into_offers_basic(copied_offer)
        self.__insert_into_offers_by_city_and_address(copied_offer)

        if copied_offer.company_name:
            self.__insert_into_offers_by_company_name(copied_offer)
        self.__insert_into_offers_by_city_and_price(copied_offer)
        return copied_offer

    def __insert_into_offers_by_city_and_address(self, offer: Offer):
        query = "INSERT INTO offers_by_city_and_street (address_city, address_street, offer_id, title, price, area, status, owner_id, apartment_id) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
        self.session.execute(query, (
            offer.address_city, offer.address_street, offer.offer_id, offer.title, offer.price, offer.area,
            offer.status,
            offer.owner_id, offer.apartment_id))

    def __insert_into_offers_by_city_and_price(self, offer: Offer):
        query = "INSERT INTO offers_by_city_and_price (address_city, address_street, price, offer_id, title, area, status, owner_id, apartment_id) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
        self.session.execute(query, (
            offer.address_city, offer.address_street, offer.price, offer.offer_id, offer.title, offer.area,
            offer.owner_id, offer.status,
            offer.apartment_id))

    def __insert_into_offers_by_company_name(self, offer: Offer):
        query = "INSERT INTO offers_by_company_name (company_name, offer_id, title, price, area, status, owner_id, apartment_id) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
        self.session.execute(query, (
            offer.company_name, offer.offer_id, offer.title, offer.price, offer.area, offer.status, offer.owner_id,
            offer.apartment_id))

    def __insert_into_offers_basic(self, offer):
        query = "INSERT INTO offers_basic (offer_id, address_city, address_street, company_name, price) VALUES (%s, %s, %s, %s, %s)"
        self.session.execute(query, (
            offer.offer_id, offer.address_city, offer.address_street, offer.company_name, offer.price))

    def update(self, offer: Offer):
        self.__update_offers_by_city_and_address(offer)
        self.__update_offers_by_city_and_price(offer)
        if offer.company_name:
            self.__update_offers_by_company_name(offer)

    def __update_offers_by_city_and_address(self, offer: Offer):
        query = "UPDATE offers_by_city_and_street SET title=%s, price=%s, area=%s, status=%s, owner_id=%s, apartment_id=%s WHERE address_city=%s AND address_street=%s AND offer_id=%s"
        self.session.execute(query, (
            offer.title, offer.price, offer.area, offer.status, offer.owner_id, offer.apartment_id, offer.address_city,
            offer.address_street, convert_text_into_uuid(offer.offer_id)))

    def __update_offers_by_company_name(self, offer: Offer):
        query = "UPDATE offers_by_company_name SET title=%s, price=%s, area=%s, status=%s, owner_id=%s, apartment_id=%s WHERE company_name=%s AND offer_id=%s"
        self.session.execute(query, (
            offer.title, offer.price, offer.area, offer.status, offer.owner_id, offer.apartment_id, offer.company_name,
            convert_text_into_uuid(offer.offer_id)))

    def __update_offers_by_city_and_price(self, offer: Offer):
        query = "UPDATE offers_by_city_and_price SET title=%s, area=%s, status=%s, owner_id=%s, apartment_id=%s WHERE address_city=%s AND price=%s AND offer_id=%s"
        self.session.execute(query, (
            offer.title, offer.area, offer.status, offer.owner_id, offer.apartment_id, offer.address_city,
            offer.price, convert_text_into_uuid(offer.offer_id)))

    def __map_from_row(self, single_row) -> Offer:
        return Offer(single_row.address_city,
                     single_row.address_street,
                     convert_uuid_into_text(single_row.offer_id),
                     single_row.title if hasattr(single_row, 'title') else None,
                     single_row.price,
                     single_row.area if hasattr(single_row, 'area') else None,
                     single_row.status if hasattr(single_row, 'status') else None,
                     single_row.owner_id if hasattr(single_row, 'owner_id') else None,
                     single_row.apartment_id if hasattr(single_row, 'apartment_id') else None,
                     single_row.company_name if hasattr(single_row, 'company_name') else None
                     )

    def __map_from_rows(self, rows) -> List[Offer]:
        return [self.__map_from_row(row) for row in rows]

    # Example usage
    def __map_average_price(self, row) -> ApartmentOfferAveragePrice:
        return ApartmentOfferAveragePrice(city=row.address_city,
                                          avg_price=row.avg_price,
                                          avg_price_per_m2=row.avg_price_per_m2
                                          )

    def __map_company_statistic(self, row) -> CompanyStatisticResult:
        return CompanyStatisticResult(company_name=row.company_name,
                                      avg_price=row.avg_price,
                                      avg_price_per_m2=row.avg_price_per_m2,
                                      sales_offer_count=row.sales_offer_count
                                      )

#
#
# repository = CassandraOfferRepository("market_app", "offers_by_city_and_street")
#
# row = repository.get_offers_by_city_and_address('Kraków', 'Krakowska')
#
# #
# # # Insert a new row
# # repository.insert("New York", "123 Main St", 456, "New Offer", 1000, 500, 1, 2)
# #
# # # Update an existing row
# # repository.update(offer_id, "Updated Offer", 1500)
#
# offer = Offer("New York", "Fifth Avenue", '31f72b98-8b75-11ed-afb3-78b58af1d5d7', "Luxury apartment2", 1000000.0, 50.0,
#               1, 1, "MAIN_COMPANY")
# repository.update(offer)


import torch
import tqdm
from torch import Tensor
from torch.nn import Module
from torch.utils.data import DataLoader
from torch_geometric.typing import EdgeType
from typing import NamedTuple
from . import get_device

__all__ = ('rank_k',)

class Ranking(NamedTuple):
    score: Tensor
    label: Tensor
    count: Tensor
    item: Tensor
    user: Tensor

@torch.no_grad()
def rank_k(
    module: type[Module],
    loader: type[DataLoader],
    *,
    pred_fn: callable,
    at_k: int = 20,
    edge_type: EdgeType,
    device: torch.device = None,
    verbose: bool = False
) -> Ranking:

    # Resolves the device.
    device = get_device(device)
    # Empties the GPU cache, if that device is set.
    if device.type == 'cuda':
        torch.cuda.empty_cache()
    # Setting up the model for evaluation.
    module = module.to(device).eval()
    # Unpacks the given edge type.
    src_node, _, dst_node = edge_type

    # Identifies the total number of source node and their respective IDs.
    gbl_idx = loader.data[edge_type].edge_label_index[0].unique()
    gbl_uid = loader.data[src_node].n_id[gbl_idx].to(device)

    # Wraps the loader in a progress tracker object, if verbose is set.
    if verbose is True:
        loader_ = tqdm.tqdm(loader, mininterval=1., position=0, leave=True)
    else:
        loader_ = loader
    # Creates the hash-map that is to be housing the output buffers.
    score_buffer = [list() for _ in gbl_uid]
    label_buffer = [list() for _ in gbl_uid]
    id_buffer = [list() for _ in gbl_uid]
    # Iterates over the data-loader's batches.
    for batch in loader_:

        # Predicts edge-wise scores.
        edge_score = pred_fn(module,
            data=batch,
            edge_type=edge_type,
            device=device
        )

        # Unpacks the edge label index.
        src_idx, dst_idx = batch[edge_type].edge_label_index
        # Derives the source and destination node IDs.
        src_id = batch[src_node].n_id[src_idx]
        dst_id = batch[dst_node].n_id[dst_idx]
        # Extracts the edge labels and destination node IDs.
        edge_label = batch[edge_type].edge_label

        # Identifies the unique source node IDs in the batch.
        src_uid = src_id.unique()
        # Infers the relevant source nodes in the tracked set.
        out_idx = (
            gbl_uid.unsqueeze(1)
                ==
            src_uid.unsqueeze(0)
        ).any(dim=-1).nonzero().ravel()
        # Generates the relevant nodes' element mask.
        out_msk = gbl_uid[out_idx].unsqueeze(1) == src_id.unsqueeze(0)

        # Updates the relevant source nodes' buffers.
        for idx, msk in zip(out_idx, out_msk):
            score_buffer[idx].append(edge_score[msk].cpu())
            label_buffer[idx].append(edge_label[msk].cpu())
            id_buffer[idx].append(dst_id[msk].cpu())

    # Concatenates all sub-buffers for the score, label and ID buffers.
    scores = [torch.cat(buffer) for buffer in score_buffer]
    labels = [torch.cat(buffer) for buffer in label_buffer]
    ids = [torch.cat(buffer) for buffer in id_buffer]

    # Initializes the positive edge counter.
    positive_count = []
    # Initializes the top-k score, label and id buffers.
    score_top, label_top, id_top = [], [], []
    # Computes the top-k item scores, labels and ids.
    for scores, labels, ids in zip(scores, labels, ids):

        # Computes the top-k values and their indices.
        scores, index = torch.topk(scores, k=at_k)

        # Saves the top-k scores, labels and ids.
        score_top.append(scores)
        label_top.append(labels[index])
        id_top.append(ids[index])

        # Counts the total number of positive edges.
        positive_count.append(labels.sum())

    # Reformats the top-k buffers to pure tensors.
    score = torch.stack(score_top)
    label = torch.stack(label_top)
    id = torch.stack(id_top)
    count = torch.tensor(positive_count)

    # Returns the top-k labels and the total positive edge count.
    return Ranking(score, label, count, item=id, user=gbl_uid)


# -*- coding: utf-8 -*-
"""
Created on Sat Jan  7 01:00:02 2017

@author: yxl
"""

from sciapp.action import Free
import wx,os
from imagepy import root_dir
from imagepy.app import loader, ConfigManager, DocumentManager
from wx.py.editor import EditorFrame
from sciwx.text import MDPad
from glob import glob

class Plugin ( wx.Panel ):
    title = 'Plugin Tree View'
    single = None
    def __init__( self, parent, app=None):
        wx.Panel.__init__ ( self, parent, id = wx.ID_ANY,
                            pos = wx.DefaultPosition, size = wx.Size( 500,300 ),
                            style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
        self.app = app
        bSizer1 = wx.BoxSizer( wx.HORIZONTAL )

        self.tre_plugins = wx.TreeCtrl( self, wx.ID_ANY, wx.DefaultPosition,
                                        wx.DefaultSize, wx.TR_DEFAULT_STYLE )
        self.tre_plugins.SetMinSize( wx.Size( 300,-1 ) )

        bSizer1.Add( self.tre_plugins, 0, wx.ALL|wx.EXPAND, 5 )
        bSizer3 = wx.BoxSizer( wx.VERTICAL )
        bSizer4 = wx.BoxSizer( wx.HORIZONTAL )

        self.m_staticText2 = wx.StaticText( self, wx.ID_ANY, "Plugin Information",
                                            wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText2.Wrap( -1 )
        bSizer4.Add( self.m_staticText2, 0, wx.ALL, 5 )

        self.m_staticText3 = wx.StaticText( self, wx.ID_ANY, "[SourceCode]",
                                            wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText3.Wrap( -1 )
        self.m_staticText3.SetForegroundColour(
            wx.SystemSettings.GetColour( wx.SYS_COLOUR_HIGHLIGHT ) )

        bSizer4.Add( self.m_staticText3, 0, wx.ALL, 5 )
        bSizer3.Add( bSizer4, 0, wx.EXPAND, 5 )

        self.txt_info = MDPad( self )
        bSizer3.Add( self.txt_info, 1, wx.ALL|wx.EXPAND, 5 )

        bSizer1.Add( bSizer3, 1, wx.EXPAND, 5 )
        self.SetSizer( bSizer1 )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.tre_plugins.Bind( wx.EVT_TREE_ITEM_ACTIVATED, self.on_run )
        self.tre_plugins.Bind( wx.EVT_TREE_SEL_CHANGED, self.on_select )
        self.m_staticText3.Bind( wx.EVT_LEFT_DOWN, self.on_source )
        self.plg = None
        self.load()

    def addnode(self, parent, data):
        for i in data:
            if i=='-':continue
            if isinstance(i, tuple):
                item = self.tre_plugins.AppendItem(parent, i[0].title)
                self.tre_plugins.SetItemData(item, i[0])
                self.addnode(item, i[1])
            else:
                item = self.tre_plugins.AppendItem(parent, i.title)
                self.tre_plugins.SetItemData(item, i)

    def load(self):
        data = loader.build_plugins(root_dir+'/menus')
        extends = glob(root_dir+'/plugins/*/menus')
        keydata = {}
        for i in data[1]:
            if isinstance(i, tuple): keydata[i[0].title] = i[1]
        for i in extends:
            plgs = loader.build_plugins(i)
            data[2].extend(plgs[2])
            for j in plgs[1]:
                if not isinstance(j, tuple): continue
                name = j[0].title
                if name in keydata: keydata[name].extend(j[1])
                else: data[1].append(j)
        root = self.tre_plugins.AddRoot('Plugins')
        self.addnode(root, data[1])

    # Virtual event handlers, overide them in your derived class
    def on_run( self, event ):
        plg = self.tre_plugins.GetItemPyData(event.GetItem())
        if hasattr(plg, 'start'):plg().start(self.app)

    def on_select( self, event ):
        plg = self.tre_plugins.GetItemData(event.GetItem())
        if plg!=None:
            self.plg = plg
            name = self.tre_plugins.GetItemText(event.GetItem())
            lang = ConfigManager.get('language')
            doc = DocumentManager.get(name, tag=lang)
            doc = doc or DocumentManager.get(name, tag='English')
            self.txt_info.set_cont(doc or 'No Document!')

    def on_source(self, event):
        ## TODO: should it be absolute path ?
        filename = self.plg.__module__.replace('.','/')+'.py'
        #print('==========', filename)
        root = os.path.split(root_dir)[0]
        filename=os.path.join(root,filename)
        #print(filename)
        EditorFrame(filename=filename).Show()


import torch
from skimage import transform
import random
import numpy as np

class Rescale(object):
    """Rescale the 3 3D images in a sample to a given size.
    Args:
        output_size (tuple or int): Desired output size of widht ad height (Lenght stays the same)
    """
    def __init__(self, output_size):
        assert isinstance(output_size, (int, tuple))
        if isinstance(output_size, int):
            self.output_size = (output_size, output_size)
        else:
            assert len(output_size) == 2
            self.output_size = output_size

    def __call__(self, sample):
        image=sample

        c,f,h, w = image.shape

        if isinstance(self.output_size, int):
            if h > w:
                new_h, new_w = self.output_size * h / w, self.output_size
            else:
                new_h, new_w = self.output_size, self.output_size * w / h
        else:
            new_h, new_w = self.output_size

        new_h, new_w = int(new_h), int(new_w)
        #Generate a resize
        frames=np.zeros((c,f,new_h, new_w))
        for c_i in range(c):
            for f_i in range(f):
                new_image=transform.resize(image[c_i,f_i,:,:],(new_h, new_w))
                frames [c_i,f_i,:,:]=new_image

        return frames

class RandomCrop(object):
    """Crop randomly the 3D image in a sample, using the same crop for all filters and frames

    Args:
        output_size (tuple or int): Desired output size of each frame of each filter. If int, square crop
            is made.
    """
    def __init__(self, output_size):
        assert isinstance(output_size, (int, tuple))
        if isinstance(output_size, int):
            self.output_size = (output_size, output_size)
        else:
            assert len(output_size) == 2
            self.output_size = output_size

    def __call__(self, sample):
        image= sample

        c,f,h, w = image.shape
        new_h, new_w = self.output_size
        top = np.random.randint(0, abs(h - new_h))
        left = np.random.randint(0, abs(w - new_w))
        frames = image[:,:,top: top + new_h,left: left + new_w]
        return frames

class Normalize(object):
    """Normalize the 3 filters

    Args:
        output_size (tuple or int): Desired output size of each frame of each filter. If int, square crop
            is made.
    """
    def __init__(self, mean, stddev):
        self.mean = mean
        self.stddev= stddev

    def __call__(self, sample):
        image= sample

        c,f,h, w = image.shape
        for c_i in range(c):
             image[c_i,:,:,:]=(image[c_i,:,:,:] - self.mean[c_i])/self.stddev[c_i]
        return image

class RandomRotate(object):
    """ Rotate 90/180/270 degrees all filters"""
    def __init__(self):

        self.degrees=list([0,0,0,90,180,270])

    def __call__ (self,sample):
        c,f,h, w = sample.shape
        choice=random.choice(self.degrees)
        if choice==90:
            frames=np.zeros((c,f,w, h))
            for c_i in range(c):
                for f_i in range(f):
                    frames[c_i,f_i,:,:]= np.rot90(sample[c_i,f_i,:,:])
            return frames
            #rotate 90 degrees
        elif choice==180:
            #rotate 180 degrees
            frames=np.zeros((c,f,h, w))
            for c_i in range(c):
                for f_i in range(f):
                    frames[c_i,f_i,:,:]= np.rot90(sample[c_i,f_i,:,:],2)
            return frames
        elif choice==270:
            frames=np.zeros((c,f,w, h))
            for c_i in range(c):
                for f_i in range(f):
                    frames[c_i,f_i,:,:]= np.rot90(sample[c_i,f_i,:,:],-1)
            return frames
        else:
            return sample
            #rotate 270 degrees

class RandomFlip(object):
    def __init__(self):
        self.direction=list(["Nothing","Nothing","LRDirection","UDDirection"])

    def __call__ (self,sample):
        c,f,h, w = sample.shape
        choice=random.choice(self.direction)
        if choice == "Nothing":
            #Do nothing
            return sample
        elif choice=="LRDirection":
            frames=np.zeros((c,f,h, w))
            for c_i in range(c):
                for f_i in range(f):
                    frames[c_i,f_i,:,:]= np.fliplr(sample[c_i,f_i,:,:])
            return frames
        elif choice=="UDDirection":
            frames=np.zeros((c,f,h,w))
            for c_i in range(c):
                for f_i in range(f):
                    frames[c_i,f_i,:,:]= np.flipud(sample[c_i,f_i,:,:])
            return frames

class RandomBrightner(object):
    """Brightness the 3 filters

    Args:
        output_size (tuple or int): Desired output size of each frame of each filter. If int, square crop
            is made.
    """
    def __init__(self,var):
        self.var = var

    def __call__(self, sample):
        image= sample
        if random.randint(0,1)==1:
            alpha = random.uniform(0.9, self.var)
            c,f,h, w = image.shape
            image=image*alpha

        return image

class ToTensor(object):
    """Convert ndarrays in sample to Tensors."""
    def __call__(self, sample):

        return torch.from_numpy(np.asarray(sample).astype(np.float32))


import matplotlib.pyplot as plt
import pandas as pd

plt.style.use("fast")

data = pd.read_csv("subplot.csv")
age = data["Age"]
python = data["Python"]
js = data["JavaScript"]
all_devs = data["All_Devs"]

fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, sharex=True)

ax1.plot(age, python, label="Python")
ax1.plot(age, js, label="JavaScript")
ax1.set_ylabel("Median Salary (USD)")
ax1.set_title("Median Salary by age")
ax1.legend()

ax2.plot(age, all_devs, label="All Developers")
ax2.set_xlabel("Age")
ax2.set_ylabel("Median Salary (USD)")
ax2.legend()

plt.tight_layout()
plt.show()

fig.savefig("subplot.png")


#simulador de dado de 1 a 6
#sem interface (incompleto)
import random
import PySimpleGUI as sg
class SimuladorDeDado:
    def __init__(self):
        self.valor_minimo = 1
        self.valor_maximo = 6
        #layout
        self.layout = [
            [sg.Text('Jogar o dado?')],
            [sg.Button('sim'),sg.Button('Não')]
        ]

    def Iniciar(self):
        #criar a janela
        self.janela = sg.Window('simulador de dado',layout=self.layout)
        #ler eventos
        self.eventos, self.valores = self.janela.Read()
        #fazer algo com os valores
        try:
            if self.eventos == 'sim' or self.eventos == 's':
                self.GerarValorDoDado()
            elif self.eventos == 'Não' or self.eventos == 'n':
                print('Agradecemos sua participação!!!')
            else:
                print('favor digitar sim,s,não ou n')
        except:
            print('Ocorreu um erro durante sua operação!')
    def GerarValorDoDado(self):
        print(random.randint(self.valor_minimo,self.valor_maximo))

simulador = SimuladorDeDado()
simulador.Iniciar()


import requests
import random
from Crypto.Cipher import AES
import base64
import codecs
import os
import json
from pypinyin import  lazy_pinyin

class Netease:
    def __init__ (self):
        self.b = '010001'
        self.c = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'
        self.d = '0CoJUm6Qyw8W8jud'
    #随机的十六位字符串

    def createSecretKey(self, size):
        return (''.join(map(lambda xx: (hex(ord(xx))[2:]), str(os.urandom(size)))))[0:16]
    #AES加密算法

    def AES_encrypt(self, text, key, iv):
        pad = 16 - len(text) % 16
        if type(text)==type(b''):
            text = str(text, encoding='utf-8')
        text = text + str(pad * chr(pad))
        encryptor = AES.new(key.encode("utf8"), AES.MODE_CBC, iv.encode("utf8"))
        encrypt_text = encryptor.encrypt(text.encode("utf8"))
        encrypt_text = base64.b64encode(encrypt_text)
        return encrypt_text
    #得到第一个加密参数

    def Getparams(self, a, SecretKey):
        #0102030405060708是偏移量，固定值
        iv = '0102030405060708'
        h_encText = self.AES_encrypt(a, self.d, iv)
        h_encText = self.AES_encrypt(h_encText,SecretKey,iv)
        return h_encText

    #得到第二个加密参数

    def GetSecKey(self, text, pubKey, modulus):
        # 因为JS做了一次逆序操作
        text = text[::-1]
        rs = int(codecs.encode(text.encode('utf-8'), 'hex_codec'), 16) ** int(pubKey, 16) % int(modulus, 16)
        return format(rs, 'x').zfill(256)

    #得到表单的两个参数

    def GetFormData(self, a):
        SecretKey = self.createSecretKey(16)
        params = self.Getparams(a, SecretKey)
        enSecKey = self.GetSecKey(SecretKey, self.b, self.c)
        data = {
            "params" : str(params, encoding='utf-8'),
            "encSecKey" : enSecKey
        }
        return data

    def searchMusic(self, search_song, headers):
        searchUrl = 'https://music.163.com/weapi/cloudsearch/get/web?csrf_token='
        musicStr = ''.join(lazy_pinyin(search_song))
        key = '{hlpretag:"",hlposttag:"</span>",s:"' + musicStr + '",type:"1",csrf_token:"",limit:"30",total:"true",offset:"0"}'
        dataStr = str({'s': musicStr, 'csrf_token': ''})
        FormData = self.GetFormData(key)
        response = requests.request('POST', searchUrl, data=FormData, headers={
            'User-agent': headers,
            'referer': 'https://music.163.com/',
            'Host': 'music.163.com',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'})
        song_writer = []
        song_id = []
        song_name = []
        song_zj = []
        song_dict = json.loads(response.text)
        # for song in song_dict['result']['songs']:
        #     print(song)
        #     song_name.append(song['name'])
        #     song_id.append(song['id'])
        #     song_ar = song['ar']
        #     if len(song_ar) == 2:
        #         song_writer.append(song_ar[0]['name'] + '_' + song_ar[1]['name'])
        #     else:
        #         song_writer.append(song_ar[0]['name'])
        #     song_zj.append(song['al']['name'])
        return song_dict['result']['songs']
if __name__ == '__main__':
    search_song = input("请输入搜索音乐的名称:")
    nete_music = Netease()
    song = nete_music.searchMusic(search_song, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36')
    print(song)


"""It contains  DoctorProfile."""

from django.contrib.auth.models import User
from django.db import models

class DoctorProfile(models.Model):
    """Defines Doctor model."""

    KIND_CHOICES = (
        ('Urologist', 'Urologist'),
        ('Gynecologist', 'Gynecologist'),
        ('Dermatologist', 'Dermatologist'),
        ('Nephrologist', 'Nephrologist'),
        ('Endocrinologist', 'Endocrinologist'),
        ('Cardiologist', 'Cardiologist'),
        ('Virologist', 'Virologist'),
        ('Surgeon', 'Surgeon'),
    )

    profile_picture = models.ImageField(
        upload_to='users',
        blank=True
    )
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
    name = models.CharField(max_length=30)
    specialty = models.CharField(max_length=20, choices=KIND_CHOICES)

    def __str__(self):
        return f"{self.name}"


'''
< 가르침 >
https://www.acmicpc.net/problem/1062
문제)
- K개의 글자만 안다.
- 남극의 단어는 "anta"으로 시작 ~ "tica"로 끝난다.
- 남극언어에 단어는 N개 밖에 없다고 가정
- 학생들이 읽을 수 있는 단어의 최댓값?
입력)
- 1     : 단어의 개수N 아는 글자K
- 2[N]  : N개의 남극단어
출력)
K개의 글자를 알때 읽을수 있는 단어 개수의 최댓값?
'''
from itertools import combinations
import sys
N, K = map(int, input().split())
first = set(['a', 'c', 'i', 'n', 't'])
words = [set(input().rstrip()[4:-4]) - first for _ in range(N)]
remain = set(chr(i) for i in range(ord('a'), ord('z') + 1)) - first

if K < 5: print(0); exit()
elif K == 26: print(N); exit()

ans = 0
for i in combinations(remain, K-5):
    readcnt = 0
    for word in words:
        # 단어 - 읽을수있는 단어 : 0이아니면 못읽음
        if word - set(i): continue
        readcnt += 1
    ans = max(readcnt, ans)

print(ans)

'''
pypy ❌
python ❌

실수 set(i) : combinations을 set해도 내부 원소는 tuple이다
'''


import arcade
import arcade.gui

import constants

class MainMenu(arcade.View):
    def __init__(self, window: arcade.Window = None, game_loop = None):
            super().__init__(window)

            self._game_loop = game_loop

            self._ui_manager = arcade.gui.UIManager()
            self._ui_manager.enable()

            self.v_box = arcade.gui.UIBoxLayout(space_between=5)

            self._create_battle_button = arcade.gui.UIFlatButton(text="Battle!", width=constants.MAIN_MENU_BUTTON_WIDTH)

            self._create_battle_button.on_click = self._create_battle

            self.v_box.add(self._create_battle_button)

            self._create_unit_button = arcade.gui.UIFlatButton(text= "Create Units [Disabled]", width=constants.MAIN_MENU_BUTTON_WIDTH)

            self._create_unit_button.on_click = self._create_unit

            self.v_box.add(self._create_unit_button)

            self._exit_button = arcade.gui.UIFlatButton(text="Exit", width=constants.MAIN_MENU_BUTTON_WIDTH)

            self._exit_button.on_click = self._exit_game

            self.v_box.add(self._exit_button)

            self._ui_manager.add(
                arcade.gui.UIAnchorWidget(
                    anchor_x="center_x",
                    anchor_y="center_y",
                    child=self.v_box)
            )

    def on_show_view(self):
        """ Called when switching to this view"""
        arcade.set_background_color(arcade.color.GRAY)

    def on_hide_view(self):
        self._ui_manager.disable()
        return super().on_hide_view()

    def on_draw(self):
        """ Draw the menu """
        self.clear()

        self._ui_manager.draw()

    def _create_battle(self, event):
        self._game_loop.create_battle()

    def _create_unit(self, event):
        pass
        #self._game_loop.create_unit()

    def _exit_game(self, event):
        self._game_loop.exit()


from setuptools import setup, find_packages

CLASSIFIERS = [
    'Development Status :: 5 - Production/Stable',
    'Intended Audience :: Developers',
    'Intended Audience :: Science/Research',
    'Programming Language :: Python',
    'Programming Language :: Python :: 3',
]

dist = setup(
    name='cf_data_mining',
    version='0.1.3',
    license='MIT License',
    description='Package providing basic data mining widgets (based on scikit-learn) for ClowdFlows >= 2',
    url='https://github.com/xflows/cf_data_mining',
    classifiers=CLASSIFIERS,
    packages=find_packages(),
    include_package_data=True,
    zip_safe=False,
    install_requires=[
        # 'cf_core',
        'scipy',
        'numpy',
        'scikit-learn',
        'pydot'
    ]
)