"""
Dockenv - Run untrusted python in Docker
"""
import argparse
import os
import sys
import shutil
import tempfile
import subprocess
import logging
import traceback
import shlex
import docker

ROOT_FOLDER = os.path.abspath(os.path.dirname(__file__))
CLIENT = docker.from_env()

LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.INFO)
LOGGER.addHandler(logging.StreamHandler())

def get_posix_path(path):
    """
    If on Windows, convert a windows path to POSIX
    """
    if os.name != "nt":
        return path
    split = os.path.splitdrive(path)
    drive = split[0].replace(":", "").lower()
    posix_path = "/" + drive + split[1].replace("\\", "/")
    return posix_path

def get_venv_name(dockenv_name):
    """
    Helper function to split the user-defined virtual env name
    out of the full name that includes the dockenv tag

    :param dockenv_name: The full name to pull the virtual env name
                         out of
    """
    # Format of tage name if dockenv-**NAME**:latest
    venv_start = len("dockenv-")
    if dockenv_name.endswith(":latest"):
        venv_end = len(":latest") * -1
        return dockenv_name[venv_start:venv_end]
    return dockenv_name[venv_start:]

def local_image_exists(image_name, tagname="latest"):
    """
    Check if we already have an image stored locally.

    :param tagname: The particular tag of the image to get. defaults to 'latest'
    :returns: The Docker container object that matches the name if it
              exists locally, otherwise None.
    """
    for image in CLIENT.images.list():
        for tag in image.tags:
            if tag == f"{image_name}:{tagname}":
                return True
    return False

def get_local_container(venv_name, tagname="latest"):
    """
    Get a Docker container object, but only if we have it locally.
    The Docker API will attempt to call out to the remote registry if we
    do not have the container localy.

    However as Dockenv only operates on the local
    level, calling out to a remote site is reduntant at best, and a potential security
    risk at worst, as the name may contain sensitive information, and would open
    the user to an attack where a malicious user uploads their own container matching
    the name to the registry.

    :param venv_name: The name of the container to attempt to get.
    :param tagname: The particular tag of the image to get. defaults to 'latest'
    :returns: The Docker container object that matches the name if it
              exists locally, otherwise None.
    """
    if local_image_exists(venv_name, tagname=tagname):
        return CLIENT.containers.get(venv_name)
    return None

# pylint: disable=too-many-arguments, too-many-locals
# pylint: disable=too-many-branches, too-many-statements
def run_script(dockenv_name,
               script,
               as_module=False,
               expose_port=None,
               mount=None,
               write_filesystem=False,
               write_mount=False,
               script_args=None):
    """
    Run a script inside a virtual env. This will build the new image that includes
    the the script file. It will then run the script passing in the args

    :param dockenv_name: The full name of the image to create
    :param script_path: The path to the script file to run
    :param as_module: If True, script is not a file on disk, but a python module
                      to run with 'python -m'
    :param expose_port: A port to expose on the docker container, so the host can
                        connect to it
    :param mount: A folder to mount inside the container. This can be used to pass
                  in config files or other data to the script to read
    :param write_filesystem: If True, allow script to write to conainer's filesystem
    :param write_mount: If True, allow script to write to the mounted folder
    :param script_args: If not None, an array of arguments to pass into the script
    """
    # Check venv exists:
    if not local_image_exists(dockenv_name):
        venv_name = get_venv_name(dockenv_name)
        LOGGER.error(f"ERROR: {venv_name!r} doesn't exist")
        return

    if as_module:
        cmd = ["-m", f"{script}"]
    else:
        cmd = [f"./{os.path.split(script)[-1]}"]

    if script_args:
        cmd += script_args
    cmd_quoted = " ".join(shlex.quote(x) for x in cmd)

    expose_script = ""
    if expose_port:
        expose_script = f"echo [***] Exposed port: $(hostname -i):{expose_port} [***]"

    runner_script = f"""
    {expose_script}
    cd ./runner
    python {cmd_quoted}
    """

    with tempfile.TemporaryDirectory() as runner_dir:
        if not as_module:
            shutil.copy(script, runner_dir)
        with open(
                os.path.join(runner_dir, "run.sh"), "w",
                newline="\n") as frunner:
            frunner.write(runner_script)

        # Create new container to run, mounting our temp dir into it
        try:
            args = ["docker", "run", "-ti", "--rm"]
            # mount temp dir into container
            vol_cmd = f"{get_posix_path(runner_dir)}:/usr/src/app/runner"
            if write_filesystem:
                args += ["-v", vol_cmd]
            else:
                args += ["-v", f"{vol_cmd}:ro"]
                args += ["--read-only"]

            if expose_port:
                args += ["--expose", expose_port]

            if mount:
                src_abs = os.path.abspath(mount)
                dest_folder = os.path.split(src_abs)[-1]
                args += ["-v"]
                mount_cmd = f"{get_posix_path(src_abs)}:/usr/src/app/{dest_folder}"
                if write_mount:
                    args += [mount_cmd]
                else:
                    args += [f"{mount_cmd}:ro"]
            args += [dockenv_name]
            subprocess.check_call(args)
        except subprocess.CalledProcessError:
            # As long as docker is installed, this is just the same
            # amout of information that is printed out by the running container
            LOGGER.debug(traceback.format_exc())
            LOGGER.error("\nERROR: Script completed with error! "
                         "Use 'dockenv --verbose run' to get more info")

def build_venv(args, upgrade=False):
    """
    Create a new virtual env or upgrade an existing one.
    If new, this will build a Docker image based on the "Python:3" image,
    and our Image will be named named "dockenv-<envname>".
    If upgrading, we will build from a base image of the same name

    :param args: cli args
    :param upgrade: If True, base image will be the same dockenv image.
                    If False, base image will be "python:3"
    """
    dockenv_name = f"dockenv-{args.envname}"

    image_exists = local_image_exists(dockenv_name)
    # Check if either upgrading and image is missing,
    # or if creating new and image already exists
    if (not upgrade) and image_exists:
        LOGGER.error(f"ERROR: Virtual Env {args.envname!r} already exists! "
                     "Use 'dockenv delete' or 'dockenv run'")
        return
    if upgrade and (not image_exists):
        LOGGER.error(f"ERROR: Virtual Env {args.envname!r} doesn't exist! ")
        return

    if args.package and args.requirements:
        LOGGER.error(f"ERROR: Use only one of '--package' or '--requirements'")
        return

    # If a new env, we need to setup the user permissions
    if upgrade:
        base_script = f"""
        FROM {dockenv_name}
        USER root
        RUN python -m pip install --upgrade pip
        """
    else:
        base_script = """
        FROM python:3
        RUN python -m pip install --upgrade pip
        RUN groupadd -r dockenv && useradd -m -r -g dockenv dockenv
        """

    pip_script = ""
    if args.requirements or args.package:
        pip_script = "RUN pip install --no-cache-dir --user -r requirements.txt"
        if not args.allow_nonbinary:
            pip_script += " --only-binary=:all:"

    if args.extra_pip_arguments:
        pip_script += " " + " ".join(args.extra_pip_arguments)

    dockerfile = f"""
    {base_script}
    USER dockenv
    WORKDIR /usr/src/app
    COPY . .
    {pip_script}
    CMD [ "sh", "./runner/run.sh" ]
    """

    # Put everything inside a temp directory
    with tempfile.TemporaryDirectory() as build_dir:
        # copy required files
        with open(os.path.join(build_dir, "Dockerfile"), "w") as fdockerfile:
            fdockerfile.write(dockerfile)

        # Copy any optional files
        if args.requirements is not None:
            shutil.copy(args.requirements,
                        os.path.join(build_dir, "requirements.txt"))

        if args.package is not None:
            with open(os.path.join(build_dir, "requirements.txt"),
                      "w") as fpackage:
                fpackage.write(args.package)

        # It takes a while to build the base Python3 image if we haven't already
        if not local_image_exists("python", tagname="3"):
            LOGGER.info(
                f"[*] First time using dockenv, may take some extra time")

        # Build the container
        LOGGER.info(f"[*] building virtual env {dockenv_name!r}...")
        # NOTE: I didn't see how to get 'CLIENT.images.build'
        # to actually print what it is doing, leading this to "hang" with no output
        # Switched to calling subprocess so user gets feedback on whats going on
        if args.verbose:
            subprocess.check_call(
                ["docker", "build", "-t", dockenv_name, build_dir])
        else:
            CLIENT.images.build(tag=dockenv_name, path=build_dir)
        LOGGER.info(f"[*] built virtual env {dockenv_name!r}")

def func_new_venv(args):
    """
    Create a new virtual env. This will build a Docker image based on the
    "Python:3" image. Our Image will be named named "dockenv-<envname>"

    :param args: cli arguments
    """
    build_venv(args, upgrade=False)

def func_upgrade_venv(args):
    """
    Upgrade a virtual env, installing new packages and creating
    a new version of the virtualenv image

    :param args: cli arguments
    """
    build_venv(args, upgrade=True)

def func_run_script(args):
    """
    Run a script inside a virtual env. This will build and create a container
    based on an image named "dockenv-<envname>"

    :param args: cli arguments
    """
    # Create a new container on top of the virtual env image
    dockenv_name = f"dockenv-{args.envname}"
    run_script(
        dockenv_name,
        args.script,
        as_module=args.as_module,
        expose_port=args.port,
        mount=args.mount,
        write_mount=args.write_mount,
        write_filesystem=args.write_filesystem,
        script_args=args.arguments)

def func_run_shell(args):
    """
    Launch a shell inside a virtual env. This will build and create a container
    based on an image named "dockenv-<envname>"

    :param args: cli arguments
    """
    dockenv_name = f"dockenv-{args.envname}"
    with tempfile.TemporaryDirectory() as runner_dir:
        shell_fname = os.path.join(runner_dir, "shell.py")
        with open(shell_fname, "w", newline="\n") as fshell:
            fshell.write("import pty; pty.spawn('/bin/bash')")
        LOGGER.info(f"[*] Starting shell in {dockenv_name!r}")
        LOGGER.info(
            f"[*] NOTE: ANYTHING you do inside the container will be blown")
        LOGGER.info(f"[*] away once you quit. This is only for debugging!")
        # As this is for debugging, we'll always run with a writable filesystem
        run_script(
            dockenv_name,
            shell_fname,
            expose_port=args.port,
            mount=args.mount,
            write_mount=True,
            write_filesystem=True)

# pylint: disable=W0613
def func_list_venv(args):
    """
    List all virtual envs. This will list all images that
    match the name "dockenv-<envname>"

    :param args: cli arguments, ignored.
    """
    LOGGER.info("Dockenv virtual envs:")
    for image in CLIENT.images.list():
        for tag in image.tags:
            if tag.startswith("dockenv"):
                venv_name = get_venv_name(tag)
                LOGGER.info(f"  {venv_name}")

def func_run_freeze(args):
    """
    This will call "dockenv run" calling a special script that
    will run "pip freeze" inside the container.
    Like "dockenv run" this will create a container based on an
    image named "dockenv-<envname>"

    :param args: cli arguments
    """
    dockenv_name = f"dockenv-{args.envname}"
    with tempfile.TemporaryDirectory() as runner_dir:
        freeze_fname = os.path.join(runner_dir, "freeze.py")
        with open(freeze_fname, "w", newline="\n") as ffreeze:
            ffreeze.write(
                "import subprocess; subprocess.run('pip freeze', shell=True)")
        # Pip freeze needs write access to the container's /tmp for some reason...
        run_script(dockenv_name, freeze_fname, write_filesystem=True)

def func_delete_venv(args):
    """
    Delete a virtual environment.
    This will remove both containers and images that match the
    name "dockenv-<envname>"

    :param args: cli arguments
    """
    dockenv_name = f"dockenv-{args.envname}"

    # Check env exists:
    if not local_image_exists(dockenv_name):
        LOGGER.error(f"ERROR: Virtual Env {args.envname!r} doesn't exist")
        return

    # First force-stop any running containers that start with venv_name
    for container in CLIENT.containers.list():
        for tag in container.image.tags:
            if tag.startswith(dockenv_name):
                LOGGER.info(f"[*] deleting container {dockenv_name!r}")
                container.remove(force=True)

    # Then delete the image
    LOGGER.info(f"[*] deleting image {dockenv_name!r}")
    CLIENT.images.remove(dockenv_name, force=True)

def func_export_venv(args):
    """
    Exports a virtual environment to a .tar file

    :param args: cli arguments
    """
    dockenv_name = f"dockenv-{args.envname}"

    # Check env exists:
    if not local_image_exists(dockenv_name):
        LOGGER.error(f"ERROR: Virtual Env {args.envname!r} doesn't exist")
        return

    image = CLIENT.images.get(dockenv_name)
    LOGGER.info(f"Exporting env {args.envname!r}, this can take 5-10 minutes")
    with open(args.filename, "wb") as fimage:
        for chunk in image.save(chunk_size=209715, named=True):
            fimage.write(chunk)
    LOGGER.info(f"Exported env {args.envname!r} to {args.filename!r}")

def func_import_venv(args):
    """
    Imports a virtual environment from a saved a .tar file

    :param args: cli arguments
    """
    LOGGER.info(
        f"Attempting to import from {args.filename!r}, this can take 5 minutes"
    )
    with open(args.filename, "rb") as fimage:
        image = CLIENT.images.load(fimage.read())[0]
    # Get the new env name
    for tag in image.tags:
        if tag.startswith("dockenv"):
            LOGGER.info(f"Imported env {get_venv_name(tag)!r}")
            break
    else:
        # Wasn't a dockenv image, remove it an error out
        CLIENT.images.remove(image.id)
        LOGGER.error(
            "Imported Docker image from {args.filename!r} wasn'y a dockenv env"
        )

def main():
    """
    Main entry function
    """
    parser = argparse.ArgumentParser(description="Run python inside docker")
    parser.add_argument(
        "-v", "--verbose", action="store_true", help="print verbose output")
    subparsers = parser.add_subparsers(help="options")

    # --- New virtual Env ---
    new_parser = subparsers.add_parser(
        "new", help="create a new virtual environment")
    new_parser.add_argument("envname", help="name of the virtualenv to create")
    new_parser.add_argument(
        "--requirements", "-r", help="requirements.txt file to install first")
    new_parser.add_argument(
        "--package", "-p", help="name of a packge to install from pip first")
    new_parser.add_argument(
        "-anb",
        "--allow-nonbinary",
        action="store_true",
        dest="allow_nonbinary",
        help="If not set, pip will be run with '--only-binary=:all:'")
    new_parser.add_argument(
        "extra_pip_arguments",
        nargs=argparse.REMAINDER,
        help="after envname, any extra arguments to pass to pip")
    new_parser.set_defaults(func=func_new_venv)

    # --- Upgrade virtual Env ---
    upgrade_parser = subparsers.add_parser(
        "upgrade", help="upgrade an existing virtual environment")
    upgrade_parser.add_argument(
        "envname", help="name of the virtualenv to upgrade")
    upgrade_parser.add_argument(
        "--requirements",
        "-r",
        help="requirements.txt file to install into virtualenv")
    upgrade_parser.add_argument(
        "--package", "-p", help="name of a packge to install into virtualenv")
    upgrade_parser.add_argument(
        "-anb",
        "--allow-nonbinary",
        action="store_true",
        dest="allow_nonbinary",
        help="If not set, pip will be run with '--only-binary=:all:'")
    upgrade_parser.add_argument(
        "extra_pip_arguments",
        nargs=argparse.REMAINDER,
        help="after envname, any extra arguments to pass to pip")
    upgrade_parser.set_defaults(func=func_upgrade_venv)

    # --- Run Script ---
    run_parser = subparsers.add_parser(
        "run", help="run script inside an existing virtual env")
    run_parser.add_argument(
        "envname", help="name of the virtualenv to run script in")
    run_parser.add_argument("script", help="path to script to run")
    run_parser.add_argument(
        "-am",
        "--as-module",
        action="store_true",
        dest="as_module",
        help=("If set, script is not a file on disk, "
              "but a name of a python module"))
    run_parser.add_argument(
        "-e",
        "--expose-port",
        type=int,
        dest="port",
        help="Expose a network port on the container")
    run_parser.add_argument(
        "-m",
        "--mount",
        help=("Mount a folder inside the container "
              "to '/usr/src/app/<folder_name>'"))
    run_parser.add_argument(
        "-wm",
        "--writeable-mount",
        action="store_true",
        dest="write_mount",
        help="Allow script to write data into the mount")
    run_parser.add_argument(
        "-wf",
        "--writeable-fs",
        action="store_true",
        dest="write_filesystem",
        help="Allow script to write data anywhere in the env's own filesystem")
    run_parser.add_argument(
        "arguments",
        nargs=argparse.REMAINDER,
        help="arguments to pass into script")
    run_parser.set_defaults(func=func_run_script)

    # --- List Virtual Envs ---
    list_parser = subparsers.add_parser(
        "list", help="list all virtual environments")
    list_parser.set_defaults(func=func_list_venv)

    # --- List packages inside a Virtual Env ---
    freeze_parser = subparsers.add_parser(
        "freeze", help="list packages inside an environment")
    freeze_parser.add_argument(
        "envname", help="name of the virtualenv to list packges in")
    freeze_parser.set_defaults(func=func_run_freeze)

    # --- Delete Virtual Env ---
    del_parser = subparsers.add_parser(
        "delete", aliases=['del'], help="delete a virtual environment")
    del_parser.add_argument("envname", help="name of the virtualenv to enter")
    del_parser.set_defaults(func=func_delete_venv)

    # --- Export Virtual Env ---
    export_parser = subparsers.add_parser(
        "export", help="Exports a virtual environment into a .tar file")
    export_parser.add_argument(
        "envname", help="name of the virtualenv to export")
    export_parser.add_argument(
        "filename", help="file to save the virtualenv to")
    export_parser.set_defaults(func=func_export_venv)

    # --- Import Virtual Env ---
    import_parser = subparsers.add_parser(
        "import", help="imports a virtual environment from a .tar file")
    import_parser.add_argument(
        "filename", help="file to save the virtualenv to")
    import_parser.set_defaults(func=func_import_venv)

    # --- Debug Shell Script ---
    shell_parser = subparsers.add_parser(
        "shell",
        help="FOR ADVANCED USERS ONLY! Start a debug shell inside the env. "
        "Anything you do in this shell will be blown away once you quit")
    shell_parser.add_argument(
        "envname", help="name of the virtualenv to start the shell in")
    shell_parser.add_argument(
        "-e",
        "--expose-port",
        type=int,
        dest="port",
        help="Expose a network port on the container")
    shell_parser.add_argument(
        "-m",
        "--mount",
        help="Mount a folder into the working directory of the container")
    shell_parser.set_defaults(func=func_run_shell)

    if len(sys.argv) == 1:
        parser.print_help()
    else:
        args = parser.parse_args()
        if args.verbose:
            LOGGER.setLevel(logging.DEBUG)
        args.func(args)

if __name__ == "__main__":
    main()


import re
import os
import sys

class ExtentionException(Exception):
    pass

class EmptyLabelException(Exception):
    pass

class Segment:
    """
    a unit of speech (i.e. phoneme, mora)
    """
    def __init__(self, tStart, tEnd, label):
        self.tStart = tStart
        self.tEnd = tEnd
        self.label = label

    def __add__(self, other):
        return Segment(self.tStart, other.tEnd, self.label + other.label)

    def can_follow(self, other):
        """
        return True if Segment self can follow Segment other in one mora,
        otherwise return False
        example: (other, self)
             True: ('s', 'a'), ('sh', 'i'), ('ky', 'o:'), ('t', 's')
             False: ('a', 'q'), ('a', 's'), ('u', 'e'), ('s', 'ha')
        """
        vowels = ['a', 'i', 'u', 'e', 'o', 'a:', 'i:', 'u:', 'e:', 'o:']
        consonants = ['w', 'r', 't', 'y', 'p', 's', 'd', 'f', 'g', 'h', 'j',
                      'k', 'z', 'c', 'b', 'n', 'm']
        only_consonants = lambda x: all([c in consonants for c in x])
        if only_consonants(other.label) and self.label in vowels:
            return True
        if only_consonants(other.label) and only_consonants(self.label):
            return True
        return False

    def to_textgrid_lines(self, segmentIndex):
        label = '' if self.label in ['silB', 'silE'] else self.label
        return [f'        intervals [{segmentIndex}]:',
                f'            xmin = {self.tStart} ',
                f'            xmax = {self.tEnd} ',
                f'            text = "{label}" ']

def openjtalk2julius(p3):
    if p3 in ['A','I','U',"E", "O"]:
        return p3.lower()
    if p3 == 'cl':
        return 'q'
    if p3 == 'pau':
        return 'sp'
    return p3

def read_lab(filename):
    """
    read label file (.lab) generated by Julius segmentation kit and
    return SegmentationLabel object
    """
    try:
        if not re.search(r'\.lab$', filename):
            raise ExtentionException("read_lab supports only .lab")
    except ExtentionException as e:
        print(e)
        return None

    with open(filename, 'r') as f:
        labeldata = [line.split() for line in f if line != '']
        segments = [Segment(tStart=float(line[0])/10e6, tEnd=float(line[1])/10e6,
                            label=openjtalk2julius(re.search(r"\-(.*?)\+", line[2]).group(1))) for line in labeldata]
        return SegmentationLabel(segments)

class SegmentationLabel:
    """
    list of segments
    """
    def __init__(self, segments, separatedByMora=False):
        self.segments = segments
        self.separatedByMora = separatedByMora

    def by_moras(self):
        """
        return new SegmentationLabel object whose segment are moras
        """
        if self.separatedByMora == True:
            return self

        moraSegments = []
        curMoraSegment = None
        for segment in self.segments:
            if curMoraSegment is None:
                curMoraSegment = segment
            elif segment.can_follow(curMoraSegment):
                curMoraSegment += segment
            else:
                moraSegments.append(curMoraSegment)
                curMoraSegment = segment
        if curMoraSegment:
            moraSegments.append(curMoraSegment)
        return SegmentationLabel(moraSegments, separatedByMora=True)

    def _textgrid_headers(self):
        segmentKind = 'mora' if self.separatedByMora else 'phones'
        return ['File type = "ooTextFile"',
                'Object class = "TextGrid"',
                ' ',
                'xmin = 0 ',
               f'xmax = {self.segments[-1].tEnd} ',
                'tiers? <exists> ',
                'size = 1 ',
                'item []: ',
                '    item [1]: ',
                '        class = "IntervalTier" ',
               f'        name = "{segmentKind}" ',
                '        xmin = 0 ',
               f'        xmax = {self.segments[-1].tEnd} ',
               f'        intervals: size = {len(self.segments)} ']

    def to_textgrid(self, textgridFileName):
        """
        save to .TextGrid file, which is available for Praat
        """
        try:
            if not self.segments:
                raise EmptyLabelException(f'warning: no label data found in '
                                          f'{textgridFileName}')
        except EmptyLabelException as e:
            print(e)
            return

        textgridLines = self._textgrid_headers()
        for i, segment in enumerate(self.segments):
            textgridLines.extend(segment.to_textgrid_lines(i + 1))
        with open(textgridFileName, 'w') as f:
            f.write('\n'.join(textgridLines))

if __name__ == '__main__':
    args = sys.argv
    if len(args) >= 2:
        mainDirectory = args[1]
    else:
        mainDirectory = os.curdir

    answer = None
    while not answer in ['y', 'Y', 'n', 'N']:
        answer = input('change segmentation unit to mora?'\
                       ' (default:phoneme) y/n:')
        choosesMora = answer in ['y', 'Y']

    for dirPath, dirNames, fileNames in os.walk(mainDirectory):
        labFileNames = [n for n in fileNames if re.search(r'\.lab$', n)]

        for labFileName in labFileNames:
            label = read_lab(os.path.join(dirPath, labFileName))
            if choosesMora:
                label = label.by_moras()
            textgridFileName = re.sub(r"\.lab$", ".TextGrid", labFileName)
            label.to_textgrid(os.path.join(dirPath, textgridFileName))


# vanilla: 5633, 5633, 5633
# Does "" refer to "" ? :625, 365, 634
# "" = "" ? 6346 6346 6346
#  = ? 6346 5865 6346
#  = ? & discriminator output layer 3653 4807 3653
# A = B and B = A 6346 6346 6346
# A = B and B = A, valid random. 6346 6346 6346
# A=B[sep] or [sep]B=A 6346 6442 6346
# cand 6346 6346 6346 # 6346 from firts epoch
# label smooth 6346 6346 6346 # validation loss drop from to 7x or 8x

# large test
# dev 67 -> test 66.4

if c.wsc_ijiri:
  from _utils.wsc_trick import *
  equal_token_id = hf_tokenizer.convert_tokens_to_ids(['='])[0]
  def wsc_preprocess(examples, augment=False):
    new_examples = {'inp_ids':[], 'attn_mask':[], 'token_type_ids':[], 'label':[]}
    for i in range(len(examples['span1_text'])):
      # clean up the candidate
      the_candidate = examples['span1_text'][i]
      the_candidate = the_candidate.replace('\n',' ')
      if the_candidate.endswith('.') or the_candidate.endswith(','):
        the_candidate = the_candidate[:-1]
      # clean up pronoun
      pronoun = examples['span2_text'][i].rstrip('.,"')
      # find other pronounce
      if augment:
        cand_spans = filter_noun_chunks(
                  extended_noun_chunks(spnlp(examples['text'][i])),
                  exclude_pronouns=True,
                  exclude_query=the_candidate,
                  exact_match=False,
              )
        other_candidates = [str(span) for span in cand_spans]
      else:
        other_candidates = []

      for candidate, label in zip([the_candidate, *other_candidates], [examples['label'][i]] + [0]*len(other_candidates)):
        # random choose
        select = random.randint(0,1)
        # 1
        if select==0 or augment:
          query = f'{pronoun} = {candidate} ?'
          sample = hf_tokenizer.encode_plus(query, examples['text'][i])
          new_examples['inp_ids'].append(sample['input_ids'])
          new_examples['attn_mask'].append(sample['attention_mask'])
          new_examples['token_type_ids'].append(sample['token_type_ids'])
          new_examples['label'].append(label)
        # 2
        if select==1 or augment:
          query = f'{candidate} = {pronoun} ?'
          sample = hf_tokenizer.encode_plus(examples['text'][i], query)
          new_examples['inp_ids'].append(sample['input_ids'])
          new_examples['attn_mask'].append(sample['attention_mask'])
          new_examples['token_type_ids'].append(sample['token_type_ids'])
          new_examples['label'].append(label)
    return new_examples

  wsc = datasets.load_dataset('super_glue', 'wsc', cache_dir='./datasets')
  #bk()
  glue_dsets['wnli'] = wsc.my_map(wsc_preprocess, batched=True,
                                  remove_columns=wsc['train'].column_names,
                                  fn_kwargs={'train':{'augment': True}},
                                  cache_file_names="ijiri_{split}.arrow")
  hf_dsets = HF_Datasets(glue_dsets['wnli'], hf_toker=hf_tokenizer, n_inp=3,
                cols={'inp_ids':TensorText, 'attn_mask':noop, 'token_type_ids':noop, 'label':TensorCategory})
  glue_dls['wnli'] = hf_dsets.dataloaders(bs=32, shuffle_train=True, num_workers=c.num_workers,
                                        cache_name="dl_ijiri_{split}.json")
  LOSS_FUNC['wnli'] = LabelSmoothingCrossEntropyFlat()

# if c.wsc_ijiri:
#   equal_token_id = hf_tokenizer.convert_tokens_to_ids(['='])[0]
#   def wsc_preprocess(example):
#     # clean up the candidate
#     candidate = example['span1_text']
#     candidate = candidate.replace('\n',' ')
#     if candidate.endswith('.') or candidate.endswith(','):
#       candidate = candidate[:-1]
#     # clean up pronoun
#     pronoun = example['span2_text'].rstrip('.,"')
#     # create the query
#     query = f'{pronoun} = {candidate} ?'
#     # tokenizer
#     sample = hf_tokenizer.encode_plus(example['text'], query)
#     # search for pronoun span
#     start_idx = -(sample['input_ids'][::-1].index(equal_token_id)-1)
#     assert start_idx < -1
#     # create example
#     example['inp_ids'] = sample['input_ids']
#     example['attn_mask'] = sample['attention_mask']
#     example['token_type_ids'] = sample['token_type_ids']
#     example['pronoun_span'] = [start_idx, -1]
#     return example
#   wsc = datasets.load_dataset('super_glue', 'wsc', cache_dir='./datasets')
#   glue_dsets['wnli'] = wsc.my_map(wsc_preprocess,
#                                   cache_file_names="ijiri_{split}.arrow")
#   hf_dsets = HF_Datasets(glue_dsets['wnli'], hf_toker=hf_tokenizer, n_inp=4,
#                 cols={'inp_ids':TensorText, 'attn_mask':noop, 'token_type_ids':noop, 'pronoun_span':noop, 'label':TensorCategory})
#   glue_dls['wnli'] = hf_dsets.dataloaders(bs=32, shuffle_train=True, num_workers=c.num_workers,
#                                         cache_name="dl_ijiri_{split}.json")
#   class IjiriModel(nn.Module):
#     def __init__(self, discriminator):
#       super().__init__()
#       self.discriminator = discriminator
#     def forward(self, input_ids, attention_mask, token_type_ids, pronoun_span):
#       logits = self.discriminator(input_ids=input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids)[0] # (B,L)
#       scores = []
#       for i, (start,end) in enumerate(pronoun_span.tolist()):
#         scores.append(logits[i,start:end].mean())
#       return torch.stack(scores).float()

#   class IjiriLoss():
#     def __init__(self):
#       self.criterion = nn.BCEWithLogitsLoss()
#     def __call__(self, scores, y):
#       return self.criterion(scores.float(), y.float())
#     def decodes(self, scores):
#       return scores <= 0

#   def wsc_ijiri_accuracy(scores, targs):
#     predicts = scores <= 0
#     return (predicts == targs).float().mean()

#   LOSS_FUNC['wnli'] = IjiriLoss()
#   METRICS['wnli'] = [wsc_ijiri_accuracy]

# elif task == 'wnli' and c.wsc_ijiri:
#     model = IjiriModel(discriminator)

# or (task == 'wnli' and c.wsc_ijiri)


class Vehiculo:

    def vehiculo(self, color, ruedas, puertas):
        self.color = color
        self.ruedas = ruedas
        self.puertas = puertas
        print('El vehiculo es de color {}, tiene {} ruedas y {} puertas'.format(self.color, self.ruedas, self.puertas))

class Coche(Vehiculo):
    Velocidad = '100 Km/h'
    cilindrada = '0'

c = Coche()

c.vehiculo('negro','4','4')
print('Alcanza una velocidad de',c.Velocidad)
print('Posee una cilindrada de',c.cilindrada)


import numpy as np
import gpflow
import matplotlib.pyplot as plt
import matplotlib.pylab as pl
from mpl_toolkits.mplot3d import axes3d, Axes3D
from BoManifolds.Riemannian_utils.sphere_utils import logmap
from BoManifolds.kernel_utils.kernels_sphere_tf import SphereGaussianKernel, SphereLaplaceKernel

from BoManifolds.plot_utils.manifold_plots import plot_sphere

plt.rcParams['text.usetex'] = True  # use Latex font for plots
plt.rcParams['text.latex.preamble'] = [r'\usepackage{bm}']
"""
This example shows the use of different kernels for the hypershere manifold S^n , used for Gaussian process regression.
The tested function corresponds to a Gaussian distribution with a mean defined on the sphere and a covariance defined on
the tangent space of the mean. Training data are generated "far" from the mean. The trained Gaussian process is then
used to determine the value of the function from test data sampled around the mean of the test function.
The kernels used are:
    - Manifold-RBF kernel (geometry-aware)
    - Laplace kernel (geometry-aware)
    - Euclidean kernel (classical geometry-unaware)
This example works with GPflow version = 0.5 (used by GPflowOpt).

Authors: Noemie Jaquier and Leonel Rozo, 2019
License: MIT
Contact: noemie.jaquier@idiap.ch, leonel.rozo@de.bosch.com
"""

def test_function(x, mu_test_function):

    # Parameters
    sigma_test_fct = np.array([[0.6, 0.2, 0], [0.2, 0.3, -0.01], [0, -0.01, 0.2]])
    inv_sigma_test_fct = np.linalg.inv(sigma_test_fct)
    det_sigma_test_fct = np.linalg.det(sigma_test_fct)

    # Function value
    x_proj = logmap(x, mu_test_function)
    return np.exp(- 0.5 * np.dot(x_proj.T, np.dot(inv_sigma_test_fct, x_proj))) / np.sqrt(
        (2 * np.pi) ** dim * det_sigma_test_fct)

def plot_gaussian_process_prediction(figure_handle, mu, test_data, mean_est, mu_test_fct, title):
    ax = Axes3D(figure_handle)

    # Make the panes transparent
    ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
    ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
    ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))

    # Make the grid lines transparent
    ax.xaxis._axinfo["grid"]['color'] = (1, 1, 1, 0)
    ax.yaxis._axinfo["grid"]['color'] = (1, 1, 1, 0)
    ax.zaxis._axinfo["grid"]['color'] = (1, 1, 1, 0)

    # Remove axis
    ax._axis3don = False

    # Initial view
    # ax.view_init(elev=10, azim=-20.)  # (default: elev=30, azim=-60)
    ax.view_init(elev=10, azim=30.)  # (default: elev=30, azim=-60)

    # Plot sphere
    plot_sphere(ax, alpha=0.4)

    # Plot training data on the manifold
    plt_scale_fact = test_function(mu_test_fct, mu_test_fct)[0, 0]
    nb_data_test = test_data.shape[0]
    for n in range(nb_data_test):
        ax.scatter(test_data[n, 0], test_data[n, 1], test_data[n, 2], c=pl.cm.inferno(mean_est[n] / plt_scale_fact))

    # Plot mean of Gaussian test function
    ax.scatter(mu[0], mu[1], mu[2], c='g', marker='D')

    plt.title(title, size=25)

if __name__ == "__main__":
    np.random.seed(1234)

    # Define the test function mean
    mu_test_fct = np.array([1 / np.sqrt(2), 1 / np.sqrt(2), 0])

    # Generate random data on the sphere
    nb_data = 20
    dim = 3

    mean = np.array([1, 0, 0])
    mean = mean / np.linalg.norm(mean)
    fact_cov = 0.1
    cov = fact_cov * np.eye(dim)

    data = np.random.multivariate_normal(mean, cov, nb_data)
    x_man = data / np.linalg.norm(data, axis=1)[:, None]

    y_train = np.zeros((nb_data,1))
    for n in range(nb_data):
        y_train[n] = test_function(x_man[n], mu_test_fct)

    # Generate test data on the sphere
    nb_data_test = 10

    mean_test = mu_test_fct
    mean_test = mean_test / np.linalg.norm(mean)
    fact_cov = 0.1
    cov_test = fact_cov * np.eye(dim)

    data = np.random.multivariate_normal(mean_test, cov_test, nb_data_test)
    x_man_test = data / np.linalg.norm(data, axis=1)[:, None]

    y_test = np.zeros((nb_data_test, 1))
    for n in range(nb_data_test):
        y_test[n] = test_function(x_man_test[n], mu_test_fct)

    # Plot training data - 3D figure
    fig = plt.figure(figsize=(5, 5))
    plot_gaussian_process_prediction(fig, mu_test_fct, x_man, y_train, mu_test_fct, r'Training data')

    # Plot true test data - 3D figure
    fig = plt.figure(figsize=(5, 5))
    plot_gaussian_process_prediction(fig, mu_test_fct, x_man_test, y_test, mu_test_fct, r'Test data (ground truth)')

    # ### Gaussian kernel
    # Define the kernel
    k_gauss = SphereGaussianKernel(input_dim=dim, active_dims=range(dim), beta_min=7.0, beta=10.0, variance=1.)
    # Kernel computation
    K1 = k_gauss.compute_K_symm(x_man)
    K12 = k_gauss.compute_K(x_man, x_man_test)
    K2 = k_gauss.compute_K_symm(x_man_test)
    # GPR model
    m_gauss = gpflow.gpr.GPR(x_man, y_train, kern=k_gauss, mean_function=None)
    # Optimization of the model parameters
    m_gauss.optimize()
    # Compute posterior samples
    # Does not always work due to Cholesky decomposition used in gpflow
    # nb_samples_post = 10
    # posterior_samples = m.predict_f_samples(y_man_test.T, nb_samples_post)
    # Prediction
    mean_est_gauss, cov_est_gauss = m_gauss.predict_f_full_cov(x_man_test)
    # mean, cov = m.predict_y(x_new)  # includes noise variance (seems not to be included in predict_f functions
    var_est_gauss = np.diag(cov_est_gauss[0])[:, None]
    # Error computation
    error_gauss = np.sqrt(np.sum((y_test - mean_est_gauss) ** 2) / nb_data_test)
    print('Estimation error (Manifold-RBF kernel) = ', error_gauss)
    # Plot test data
    fig = plt.figure(figsize=(5, 5))
    plot_gaussian_process_prediction(fig, mu_test_fct, x_man_test, mean_est_gauss, mu_test_fct, r'Manifold-RBF kernel')

    # ### Laplace kernel
    # Define the kernel
    k_laplace = SphereLaplaceKernel(input_dim=dim, active_dims=range(dim), beta=10.0, variance=1.)
    # Kernel computation
    K1 = k_laplace.compute_K_symm(x_man)
    K12 = k_laplace.compute_K(x_man, x_man_test)
    K2 = k_laplace.compute_K_symm(x_man_test)
    # GPR model
    m_laplace = gpflow.gpr.GPR(x_man, y_train, kern=k_laplace, mean_function=None)
    # Optimization of the model parameters
    m_laplace.optimize()
    # Compute posterior samples
    # Does not always work due to Cholesky decomposition used in gpflow
    # nb_samples_post = 10
    # posterior_samples = m.predict_f_samples(y_man_test.T, nb_samples_post)
    # Prediction
    mean_est_laplace, cov_est_laplace = m_laplace.predict_f_full_cov(x_man_test)
    # mean, cov = m.predict_y(x_new)  # includes noise variance (seems not to be included in predict_f functions
    var_est_laplace = np.diag(cov_est_laplace[0])[:, None]
    # Error computation
    error_laplace = np.sqrt(np.sum((y_test - mean_est_laplace) ** 2) / nb_data_test)
    print('Estimation error (Laplace kernel) = ', error_laplace)
    # Plot test data
    fig = plt.figure(figsize=(5, 5))
    plot_gaussian_process_prediction(fig, mu_test_fct, x_man_test, mean_est_laplace, mu_test_fct, r'Laplace kernel')

    # ### Euclidean RBF
    # Define the kernel
    k_eucl = gpflow.kernels.RBF(input_dim=dim, ARD=False)
    # Kernel computation
    K1 = k_eucl.compute_K_symm(x_man)
    K12 = k_eucl.compute_K(x_man, x_man_test)
    K2 = k_eucl.compute_K_symm(x_man_test)
    # GPR model
    m_eucl = gpflow.gpr.GPR(x_man, y_train, kern=k_eucl, mean_function=None)
    # Optimization of the model parameters
    m_eucl.optimize()
    # Compute posterior samples
    # Does not always work due to Cholesky decomposition used in gpflow
    # nb_samples_post = 10
    # posterior_samples = m.predict_f_samples(y_man_test.T, nb_samples_post)
    # Prediction
    mean_est_eucl, cov_est_eucl = m_eucl.predict_f_full_cov(x_man_test)
    # mean, cov = m_eucl.predict_y(x_new)  # includes noise variance (seems not to be included in predict_f functions
    var_est_eucl = np.diag(cov_est_eucl[0])[:, None]
    # Error computation
    error_eucl = np.sqrt(np.sum((y_test - mean_est_eucl) ** 2) / nb_data_test)
    print('Estimation error (Euclidean-RBF kernel) = ', error_eucl)
    # Plot test data
    fig = plt.figure(figsize=(5, 5))
    plot_gaussian_process_prediction(fig, mu_test_fct, x_man_test, mean_est_eucl, mu_test_fct, r'Euclidean-RBF kernel')

    plt.show()


# coding: utf-8

"""
  Test module for the entity.files module
"""

import unittest
from lxml import etree
import reader.smreader as smreader

class TestFinders(unittest.TestCase):
    """ TestCase for all the functions in the reader.smreader module """

    def shortDescription(self):
        return None

    def test_function_abs_path(self):
        """ test for the absolute path function """
        # Only testing function_abs_path is enough because it will call
        # file_abs_path which itself calls last_checkpoint_abs_path
        self.assertEqual(
            smreader.function_abs_path("test_source.c", "my_func"),
            "/sourcemonitor_metrics/project/checkpoints/checkpoint[last()]" +
            "/files/file[@file_name='test_source.c']" +
            "/function_metrics/function[@name='my_func']"
        )

    def test_create_file_finder(self):
        """ test for the function that create XPath file finder """
        file_finder = smreader.create_file_finder("test_source.c")

        self.assertIsInstance(file_finder, etree.XPath)
        self.assertEqual(
            str(file_finder),
            "/sourcemonitor_metrics/project/checkpoints/checkpoint[last()]" +
            "/files/file[@file_name='test_source.c']"
        )

    def test_all_file_pathfinder(self):
        """ test for the function that create XPath function finder """
        path_finder = smreader.all_file_pathfinder()

        self.assertIsInstance(path_finder, etree.XPath)
        self.assertEqual(
            str(path_finder),
            "/sourcemonitor_metrics/project/checkpoints/checkpoint[last()]" +
            "/files"
        )

    def test_create_function_finder(self):
        """ test for the function that create XPath function finder """
        func_finder = smreader.create_function_finder(
            "test_source.c", "my_func"
        )

        self.assertIsInstance(func_finder, etree.XPath)
        self.assertEqual(
            str(func_finder),
            "/sourcemonitor_metrics/project/checkpoints/checkpoint[last()]" +
            "/files/file[@file_name='test_source.c']/" +
            "function_metrics/function[@name='my_func']"
        )


from math import sqrt

MOD = pow(10,9)+7
MAX= 10e+9

class Point:
    def __init__(self,x,y,distOrigin,distEnd):
        self.x = x
        self.y = y
        self.distOrigin = distOrigin
        self.distEnd = distEnd

test = int(input())

def distance(a,b,c,d):
    return sqrt( ((a-c)*(a-c)) + ((b-d)*(b-d)) )

for _ in range(test):
    x,y = map(int,input().split())
    origin = Point(x,y,MAX,MAX)
    n,m,k = map(int,input().split())
    Set1 = []
    Set2 = []
    Set3 = []
    s1 = [int(x) for x in input().split()]
    s2 = [int(x) for x in input().split()]
    s3 = [int(x) for x in input().split()]

    for i in range(0,2*n-1,2):
        Set1.append(Point(s1[i],s1[i+1],MAX,MAX))
    for i in range(0,2*m-1,2):
        Set2.append(Point(s2[i],s2[i+1],MAX,MAX))
    for i in range(0,2*k-1,2):
        Set3.append(Point(s3[i],s3[i+1],MAX,MAX))

    for i in Set1:
        minDistance = 10e+9
        for j in Set3:
            dist =  distance(i.x,i.y,j.x,j.y)
            if dist<minDistance:
                minDistance = dist
                i.distEnd = minDistance

    nearestSet2 = {}
    for i in Set2:
        minDistance = 10e+9
        for j in Set3:
            dist =  distance(i.x,i.y,j.x,j.y)
            if dist<minDistance:
                minDistance = dist
                i.distEnd = minDistance

    distSet1FromStart = {}
    for i in Set1:
        i.distOrigin = distance(x,y,i.x,i.y)

    distSet2FromStart = {}
    for i in Set2:
        i.distOrigin = distance(x,y,i.x,i.y)

    #print(nearestSet1)
    mini = 10e+9
    for i in Set1:
        for j in Set2:
            distBetweenPoints = distance(i.x,i.y,j.x,j.y)
            dist1 = i.distOrigin + distBetweenPoints
            dist2 = j.distOrigin + distBetweenPoints
            setC1 = dist1 + j.distEnd
            setC2 = dist2 + i.distEnd
            if setC1<mini:
                mini = setC1
            if setC2<mini:
                mini = setC2

    print ('%.7f'%mini)


import os
import re
import argparse
import random

def crossval_files(prefix, numfolds):
    cvfiles = []
    for i in range(numfolds):
        trainfile = '{}train{}.types'.format(prefix, i)
        testfile = '{}test{}.types'.format(prefix, i)
        cvfiles.append((trainfile, testfile))
    return cvfiles

def reduced_file(file):
    match = re.match('(.*?)(((train|test)[0-9]+)?.types)', file)
    return match.group(1) + '_reduced' + match.group(2)

def read_lines(file):
    with open(file, 'r') as f:
        lines = f.readlines()
    return lines

def write_reduced_lines(file, lines, factor):
    random.shuffle(lines)
    reduced = lines[:int(len(lines)/factor)]
    with open(file, 'w') as f:
        f.write(''.join(reduced))

def parse_args(argv=None):
    parser = argparse.ArgumentParser()
    parser.add_argument('-p', '--prefix', required=True)
    parser.add_argument('-n', '--numfolds', type=int, default=3)
    parser.add_argument('-a', '--allfolds', default=False, action='store_true')
    parser.add_argument('-f', '--factor', required=True, type=float)
    parser.add_argument('-s', '--random_seed', type=int, default=0)
    return parser.parse_args(argv)

if __name__ == '__main__':
    args = parse_args()
    random.seed(args.random_seed)
    cvfiles = crossval_files(args.prefix, args.numfolds)
    for i, (trainfile, testfile) in enumerate(cvfiles):
        train = read_lines(trainfile)
        reduced_trainfile = reduced_file(trainfile)
        write_reduced_lines(reduced_trainfile, train, args.factor)
        print(reduced_trainfile)
        test = read_lines(testfile)
        reduced_testfile = reduced_file(testfile)
        write_reduced_lines(reduced_testfile, test, args.factor)
        print(reduced_testfile)
    if args.allfolds:
        allfile = '{}.types'.format(args.prefix)
        all = read_lines(allfile)
        reduced_allfile = reduced_file(allfile)
        write_reduced_lines(reduced_allfile, all, args.factor)
        print(reduced_allfile)


import tkinter as tk
from tkinter import ttk

def calculate_points():
    red_points = 0
    green_points = 0

    # Red points calculations
    for role, value in autofill_vars.items():
        if value.get():
            red_points += autofill_points[role]

    for role, value in champ_experience_vars.items():
        if value.get():
            red_points += 2

    for role, var in bad_recent_vars.items():
        red_points += 1 if var.get() else 0

    for role, var in very_bad_recent_vars.items():
        red_points += 3 if var.get() else 0

    if bad_mental_var.get():
        red_points += 2

    if comp_diff_sad_var.get():
        red_points += 1

    # Green points calculations
    for role, value in good_winrate_vars.items():
        if value.get():
            green_points += 2

    for role, var in good_recent_vars.items():
        green_points += 1 if var.get() else 0

    for role, var in zoggy_recent_vars.items():
        green_points += 2 if var.get() else 0

    for role, value in bad_winrate_vars.items():
        if value.get():
            red_points += 2

    if comp_diff_happy_var.get():
        green_points += 1

    net_points = red_points - green_points

    if net_points < 0:
        result_var.set("It's Free! [" + str(net_points) + "]")
    elif net_points == 0:
        result_var.set("It’s Free? [" + str(net_points) + "]")
    elif 1 <= net_points <= 3:
        result_var.set("Winnable [" + str(net_points) + "]")
    elif net_points == 4:
        result_var.set("Medium Fun [" + str(net_points) + "]")
    elif net_points == 5:
        result_var.set("MonkaS [" + str(net_points) + "]")
    else:
        result_var.set("DODGE!!! [" + str(net_points) + "]")

def reset():
    for var in [*autofill_vars.values(), *champ_experience_vars.values(), *bad_winrate_vars.values(), bad_mental_var, comp_diff_happy_var, comp_diff_sad_var, *good_winrate_vars.values(), *good_recent_vars.values(), *zoggy_recent_vars.values(), *bad_recent_vars.values(), *very_bad_recent_vars.values()]:
        var.set(False)
    result_var.set("")  # Clear the result_var text

root = tk.Tk()
root.title("Dodge Point Calculator")

autofill_points = {
    'Top': 1,
    'Jungler': 3,
    'Mid': 3,
    'ADC': 2,
    'Support': 2
}

autofill_vars = {}
champ_experience_vars = {}
good_winrate_vars = {}
bad_winrate_vars = {}  # Added bad_winrate
good_recent_vars = {}
zoggy_recent_vars = {}
bad_recent_vars = {}
very_bad_recent_vars = {}

roles = ["Top", "Jungler", "Mid", "ADC", "Support"]
grid_positions = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1)]

for idx, role in enumerate(roles):
    row, col = grid_positions[idx]
    frame = ttk.LabelFrame(root, text=role)
    frame.grid(row=row, column=col, padx=10, pady=5, sticky='w')

    good_winrate_var = tk.BooleanVar()
    ttk.Checkbutton(frame, text="Good winrate", variable=good_winrate_var).pack(anchor='w', padx=5, pady=2)
    good_winrate_vars[role] = good_winrate_var

    good_recent_var = tk.BooleanVar()
    ttk.Checkbutton(frame, text="Good Recent", variable=good_recent_var).pack(anchor='w', padx=5, pady=2)
    good_recent_vars[role] = good_recent_var

    zoggy_recent_var = tk.BooleanVar()
    ttk.Checkbutton(frame, text="ZOGGY Recent", variable=zoggy_recent_var).pack(anchor='w', padx=5, pady=2)
    zoggy_recent_vars[role] = zoggy_recent_var

    autofill_var = tk.BooleanVar()
    ttk.Checkbutton(frame, text="Autofill", variable=autofill_var).pack(anchor='w', padx=5, pady=2)
    autofill_vars[role] = autofill_var

    champ_experience_var = tk.BooleanVar()
    ttk.Checkbutton(frame, text="1-2nd time champ", variable=champ_experience_var).pack(anchor='w', padx=5, pady=2)
    champ_experience_vars[role] = champ_experience_var

    bad_winrate_var = tk.BooleanVar()  # Added bad_winrate
    ttk.Checkbutton(frame, text="Bad winrate", variable=bad_winrate_var).pack(anchor='w', padx=5, pady=2)
    bad_winrate_vars[role] = bad_winrate_var

    bad_recent_var = tk.BooleanVar()
    ttk.Checkbutton(frame, text="Bad Recent", variable=bad_recent_var).pack(anchor='w', padx=5, pady=2)
    bad_recent_vars[role] = bad_recent_var

    very_bad_recent_var = tk.BooleanVar()
    ttk.Checkbutton(frame, text="Very Bad Recent", variable=very_bad_recent_var).pack(anchor='w', padx=5, pady=2)
    very_bad_recent_vars[role] = very_bad_recent_var

# Additional Checkboxes
additional_frame = ttk.LabelFrame(root, text="Additional Points")
additional_frame.grid(row=1, column=2, padx=10, pady=5, sticky='w')

bad_mental_var = tk.BooleanVar()
ttk.Checkbutton(additional_frame, text="Bad Mental", variable=bad_mental_var).pack(anchor='w', padx=5, pady=2)

comp_diff_sad_var = tk.BooleanVar()
ttk.Checkbutton(additional_frame, text="Comp Diff Lame", variable=comp_diff_sad_var).pack(anchor='w', padx=5, pady=2)

comp_diff_happy_var = tk.BooleanVar()
ttk.Checkbutton(additional_frame, text="Comp Diff Jipie", variable=comp_diff_happy_var).pack(anchor='w', padx=5, pady=2)

calculate_btn = ttk.Button(root, text="Calculate Points", command=calculate_points)
calculate_btn.grid(row=2, column=0, pady=10)

reset_btn = ttk.Button(root, text="Reset", command=reset)
reset_btn.grid(row=2, column=1, pady=10)

result_var = tk.StringVar()
result_label = ttk.Label(root, textvariable=result_var, font=("Arial", 14))
result_label.grid(row=2, column=2, columnspan=2, pady=10)

root.mainloop()


# CONSTANTS
from SymbolTable import SymbolTable
from Number import Number
from Lexer import Lexer
from Parser import Parser
from Interpreter import Interpreter
from Context import Context

global_symbol_table = SymbolTable()
global_symbol_table.set("NULL", Number(0))
global_symbol_table.set("FALSE", Number(0))
global_symbol_table.set("TRUE", Number(1))

# RUN
def run(file_name, text):
    lexer = Lexer(file_name, text)
    tokens, error = lexer.create_tokens()
    if error:
        return None, error
    # GENERATE AST
    parser = Parser(tokens)
    ast = parser.parse()

    # Create Instance of Interpreter
    if ast.error:
        return None, ast.error

    interpreter = Interpreter()
    context = Context('<program>')
    context.symbol_table = global_symbol_table
    result = interpreter.visitNode(ast.node, context)

    return result.value, result.error


# calculates time dependent concordanane index

import abc
from collections import defaultdict
from typing import Tuple
from easydict import EasyDict
import numpy as np
from sksurv.metrics import concordance_index_ipcw
from src.utils.utils import df_to_event_time_array
from src.utils.data_class import Data

class EvaluatorBase:
    """
    Base class that for caluculating time dependent concordance index

    Attributes:
        - model_name (str): name of model evaluated on
        - model: class from utils.model
        - offset: number columns to skip from the left of risk calculation
        - df_train_all: dataframe with features and events & durations trained on
        - x_eval: features data to evaluate on. either validation or test set
        - df_y_eval: dataframe of events and durations to evaluate on.
        - times: duration indexes
        - horionzs: quantiles
        - metric_dict: (defaultdict): stores calculated metrics
    """
    def __init__(self, data: Data, model, config: EasyDict, test_set: bool=True):
        '''
        Args:
            - data: class from utils.Data
            - model: class from utils.models
            - config: configuration file from utils.configurations
            - test_set (bool):
                - True: evaluate on test set
                - False: evaluate on validation set
        '''
        self.model_name = config.model
        self.model = model
        self.offset = model.eval_offset
        self.df_train_all = data.df.loc[data.df_train.index]

        if test_set:
            self.x_eval = data.df_test if self.model_name.startswith('survtrace') else data.x_test
            self.df_y_eval = data.df_y_test
        # use validation set
        else:
            self.x_eval = data.df_val if self.model_name.startswith('survtrace') else data.x_val
            self.df_y_eval = data.df_y_val

        self.times = config['duration_index'][1:-1]
        self.horizons = config['horizons']
        self.metric_dict = defaultdict(list)

    def _make_event_time_array(self, event_var_name: str) -> Tuple[np.array, np.array]:
        '''
        helper function to convert training data and evaluation data

        Args:
            - event_var_name (str): variable name of event

        Returns:
            - tuple of event time arrays
            - Tuple[0]: training data
            - Tuple[1]: evaluation data
        '''
        def helper(df):
            return df_to_event_time_array(df, event_var_name=event_var_name)

        return helper(self.df_train_all), helper(self.df_y_eval)

    def _calc_concordance_index_ipcw_base(self, risk, event_var_name: str, event_dict_label: str='', event_print_label: str=''):
        '''
        Calculates time dependent concordance index.

        Use sksurv.concordance_index_ipcw.

        Args:
            - risk: array of models risk predicts for each time horizon.
            - event_var_name (str): variable name of event.
            - event_dict_label (str):
            - event_print_label (str):

        Return:
            Nothing.
            Adds each metric to attribute metric_dict.
        '''
        et_train, et_test = self._make_event_time_array(event_var_name)

        cis = []
        for i, _ in enumerate(self.times):
            cis.append(
                concordance_index_ipcw(et_train, et_test, estimate=risk[:, i+self.offset], tau=self.times[i])[0]
                )
            self.metric_dict[f'{self.horizons[i]}_ipcw{event_dict_label}'] = cis[i]

        for horizon in enumerate(self.horizons):
            print(f"{event_print_label}For {horizon[1]} quantile,")
            print("TD Concordance Index - IPCW:", cis[horizon[0]])

    @abc.abstractclassmethod
    def _calc_risk(self):
        '''
        Calcuates risk based on provided trained model.
        '''
        pass

    @abc.abstractclassmethod
    def calc_concordance_index_ipcw(self):
        '''
        Calculates time dependent concordance index.
        '''
        pass

    @abc.abstractclassmethod
    def eval(self):
        '''
        Calculates time dependent concordance index and return metrics.
        '''
        pass

class EvaluatorSingle(EvaluatorBase):
    """
    Caluculating time dependent concordance index on single events.

    Child of EvaluatorBase.
    """
    def __init__(self, data: Data, model, config: EasyDict, test_set: bool=True):
        '''
        Args:
            - data: class from utils.Data
            - model: class from utils.models
            - config: configuration file from utils.configurations
            - test_set (bool):
                - True: evaluate on test set
                - False: evaluate on validation set
        '''
        super().__init__(data, model, config, test_set)

    # TODO: move to baselinse.models
    #   ideally it would be nice to have a generic interface for calculating risk in utils.models
    #   like the train method. This may requrie additional refactor of evaluators and models to make that work
    # Note: self.model.model.{function}:
    #   - first model references a class from utils.models
    #   - second model references a model from pycox, sksurv, DSM
    def _calc_risk(self):
        '''
        Calculates risk based on trained model provided.

        Returns:
            Array of risk per time horizon.
        '''
        if self.model_name == 'DSM':
            return 1 - self.model.model.predict_survival(self.x_eval.astype('float64'), self.times.tolist())
        elif self.model_name == 'RSF':
            return 1 - self.model.model.predict_survival_function(self.x_eval)
        elif self.model_name == 'CPH':
            surv = self.model.model.predict_survival_function(self.x_eval)
            surv = np.array([f.y for f in surv])
            return 1 - surv
        elif self.model_name == 'DeepSurv':
            _ = self.model.model.compute_baseline_hazards()
            return 1 - self.model.model.predict_surv(self.x_eval)
        elif self.model_name.startswith('survtrace'):
            return 1 - self.model.model.predict_surv(self.x_eval, batch_size=None).cpu()
        else:
            return 1 - self.model.model.predict_surv(self.x_eval)

    def calc_concordance_index_ipcw(self, event_var_name: str='event'):
        '''
        Calculates time dependent concordance index.

        Args:
            - event_var_name (str): variable name of event.
                - Defualt = 'event'

        Return:
            Nothing.
            Adds each metric to attribute metric_dict.
        '''
        risk = self._calc_risk()
        self._calc_concordance_index_ipcw_base(risk, event_var_name)

    def eval(self):
        '''
        Calculates time dependent concordance index and return metrics.
        '''
        self.calc_concordance_index_ipcw()
        return self.metric_dict

class EvaluatorCompeting(EvaluatorBase):
    """
    Caluculating time dependent concordance index on competing events.

    Child of EvaluatorBase.

    Attributes:
        - num_event (int): number of competing events
    """
    def __init__(self, data: Data, model, config: EasyDict, test_set: bool=True):
        '''
        Args:
            - data: class from utils.Data
            - model: class from utils.models
            - config: configuration file from utils.configurations
            - test_set (bool):
                - True: evaluate on test set
                - False: evaluate on validation set
        '''
        super().__init__(data, model, config, test_set=test_set)
        self.num_event = config.num_event

    # TODO: move to baselinse.models
    #   ideally it would be nice to have a generic interface for calculating risk in utils.models
    #   like the train method. This may requrie additional refactor of evaluators and models to make that work
    # Note: self.model.model.{function}:
    #   - first model references a class from utils.models
    #   - second model references a model from pycox, sksurv, DSM
    def _calc_risk(self, event_idx):
        '''
        Calculates risk based on trained model provided.

        Returns:
            Array of risk per time horizon.
        '''
        if self.model_name == 'DSM':
            return 1 - self.model.model.predict_survival(self.x_eval.astype('float64'), self.times.tolist(), risk=event_idx+1)
        elif self.model_name == 'DeepHit':
            return self.model.model.predict_cif(self.x_eval)[event_idx, :, :].transpose()
        elif self.model_name.startswith('survtrace'):
            return 1 - self.model.model.predict_surv(self.x_eval, batch_size=10000, event=event_idx).cpu()
        else:
            raise('Model not implemented')

    def calc_concordance_index_ipcw(self, event_idx: int, event_var_name: str):
        '''
        Calculates time dependent concordance index.

        Args:
            - event_idx: index of competing event.
            - event_var_name (str): variable name of event.

        Return:
            Nothing.
            Adds each metric to attribute metric_dict.
        '''
        risk = self._calc_risk(event_idx)
        event_dict_label = f'_{event_idx}'
        event_print_label = f'Event: {event_idx} '
        self._calc_concordance_index_ipcw_base(risk, event_var_name, event_dict_label, event_print_label)

    def eval(self):
        '''
        Calculates time dependent concordance index for each competing event and return metrics.
        '''
        for event_idx in range(self.num_event):
            event_var_name = f'event_{event_idx}'
            self.calc_concordance_index_ipcw(event_idx, event_var_name)
        return self.metric_dict


def remove_repetidos(lista):
    l = []
    for i in lista:
        if i not in l:
            l.append(i)
    l.sort()
    return l

#def numeric_compare(x, y):
 #   return x - y
#sorted([5, 2, 4, 1, 3], cmp=numeric_compare)
#[1, 2, 3, 4, 5]

#def reverse_numeric(x, y):
 #   return y - x
#sorted([5, 2, 4, 1, 3], cmp=reverse_numeric)
#[5, 4, 3, 2, 1]


from keras_tuner import GridSearch
from keras_tuner.engine import tuner_utils
import copy

class NoCheckGridSearch(GridSearch):

    def __init__(
        self,
        hypermodel=None,
        objective=None,
        max_trials=None,
        seed=None,
        hyperparameters=None,
        tune_new_entries=True,
        allow_new_entries=True,
        max_retries_per_trial=0,
        max_consecutive_failed_trials=3,
        **kwargs,
    ):
        super().__init__(hypermodel,
                         objective,
                         max_trials,
                         seed,
                         hyperparameters,
                         tune_new_entries,
                         allow_new_entries,
                         max_retries_per_trial,
                         max_consecutive_failed_trials,
                         **kwargs)

    def run_trial(self, trial, *args, **kwargs):

        callbacks = kwargs.pop("callbacks", [])

        # Run the training process multiple times.
        histories = []
        for execution in range(self.executions_per_trial):
            copied_kwargs = copy.copy(kwargs)
            #callbacks = self._deepcopy_callbacks(original_callbacks)
            self._configure_tensorboard_dir(callbacks, trial, execution)
            callbacks.append(tuner_utils.TunerCallback(self, trial))

            copied_kwargs["callbacks"] = callbacks
            obj_value = self._build_and_fit_model(trial, *args, **copied_kwargs)

            histories.append(obj_value)
        return histories


# 큐
# https://www.acmicpc.net/problem/10845

import sys

class queue:
	def __init__(self):
		self.li = list()
		self.cnt = 0

	def push(self,x):
		self.cnt = self.cnt + 1
		self.li.append(x)
		return

	def pop(self):
		if self.cnt == 0:
			print(-1)
			return -1

		print(self.li[0])
		self.cnt = self.cnt-1
		self.li = self.li[1:]
		return self.li

	def front(self):
		if self.cnt == 0:
			print(-1)
			return -1
		else:
			print(self.li[0])
			return self.li[0]

	def back(self):
		if self.cnt == 0:
			print(-1)
			return -1
		else:
			print(self.li[self.cnt-1])
			return self.li[self.cnt-1]

	def empty(self):
		if self.cnt == 0:
			print(1)
			return 1
		else:
			print(0)
			return 0

	def size(self):
		print(self.cnt)
		return self.cnt

queue = queue()

leng1 = int(sys.stdin.readline().strip())
for i in range(leng1):
	leng2 = sys.stdin.readline().strip()
	if "push" in leng2:
		push_input = int(leng2.split(' ')[1])
		queue.push(push_input)
	elif "pop" in leng2:
		queue.pop()
	elif "front" in leng2:
		queue.front()
	elif "back" in leng2:
		queue.back()
	elif "empty" in leng2:
		queue.empty()
	elif "size" in leng2:
		queue.size()


import base64
import subprocess
import time
from functools import partial
from urllib.parse import quote

from Crypto.PublicKey import RSA

from examples.useful_python.examples.spiders.utils.decoder import USE_RSA

subprocess.Popen = partial(subprocess.Popen, encoding="utf-8")
import execjs
import requests

YUANRENXUE_COM_MATCH_ = {
    "User-Agent": "yuanrenxue.project",
    "Referer": "https://match.yuanrenxue.com/match/6"
}

url_template = "https://match.yuanrenxue.com/api/match/6?&page={page}&m={m}&q={q}"
headers = YUANRENXUE_COM_MATCH_
proxies = {
    'http': 'http://127.0.0.1:8888',
    'https': 'http://127.0.0.1:8888',
}
session = requests.session()
session.headers = headers
session.cookies.update({
    "sessionid": "1v7ytuna1sh2b1lised7yltga6u1v0nj",
})

def encode0(data):
    rsa = USE_RSA()
    rsa.pubkey = RSA.import_key(open("pub.pem").read())
    # print(pubkey)
    # rsa.readPem("server-private.pem", "pubkey")
    ret = rsa.rsaEncrypt(data)
    ret = base64.b64encode(ret).decode()
    print(ret)
    return ret

def encode2(data):
    from Crypto.PublicKey import RSA
    from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
    # from Crypto.Cipher import PKCS1_OAEP as Cipher_pkcs1_v1_5
    import base64

    # 加密
    message = data
    rsakey = RSA.importKey(open("pub.pem").read())
    cipher = Cipher_pkcs1_v1_5.new(rsakey)  # 创建用于执行pkcs1_v1_5加密或解密的密码
    cipher_text = base64.b64encode(cipher.encrypt(message.encode('utf-8')))
    print(cipher_text.decode('utf-8'))
    return cipher_text.decode('utf-8')

def encode3(t, time):
    # 通过sdk加密
    f = open('sdk.js', 'r', encoding="utf-8")
    sdk_1 = f.read()
    compiled = execjs.compile(sdk_1)
    ret = compiled.call('z', int(t), int(time))
    print(ret)
    return ret

total_q = ''

def encode1(window_o):
    global total_q
    t1 = int(int(time.time()) * 1000)
    # t1 = 1644461166000
    data = f'{window_o}|{t1}'
    print(data)
    # m = encode2(data)
    m = encode3(t1, window_o)
    total_q = str(window_o) + '-' + str(t1) + "|"
    return m, total_q

total = 0
for i in range(1, 6):
    m, total_q = encode1(i)
    # q_m = parse(m)
    # q_m = quote(m, safe='')

    url = url_template.format(page=i, m=quote(m), q=quote(total_q))
    ret = session.get(url=url, headers=headers, proxies=proxies, verify=False)
    print(ret.text)
    json_content = ret.json()
    data = json_content.get("data")
    for item in data:
        total += item.get("value") + item.get("value") * 8 + item.get("value") * 15

    # result = encode1()
    print(session.cookies)
    time.sleep(2)

    print(ret)
print(total)


from django.urls import path
from .views import EquationHome,Detect,Blob,EquationImage,DetectI,FileUploadView,test

urlpatterns = [
    path('EqHome',EquationHome),
    path('EqI',EquationImage),
    path('detect',Detect),
    path('detectI',DetectI),
    path('<str:a>',Blob),
    path('ui', FileUploadView.as_view()),
    path('uij', test),

]


#
# (c) 2008 Mandriva, http://www.mandriva.com/
#
# $Id$
#
# This file is part of Pulse 2, http://pulse2.mandriva.org
#
# Pulse 2 is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Pulse 2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pulse 2; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.

"""
xmppmaster querymanager
give informations to the dyngroup plugin to be able to build dyngroups
on glpi and xmppmaster informations
"""

import logging
from mmc.plugins.glpi.database import Glpi
from mmc.plugins.glpi.config import GlpiQueryManagerConfig

from pulse2.utils import unique

from pulse2.database.xmppmaster import XmppMasterDatabase

#from mmc.plugins.xmppmaster.config import xmppMasterConfig

def activate():
    conf = GlpiQueryManagerConfig("glpi")
    return conf.activate

def queryPossibilities():
    ret = {}
    ret['OU user'] = ['list', getAllOuuser]
    ret['OU Machine'] = ['list', getAllOumachine]
    logging.getLogger().info('queryPossibilities %s' %
                             (str(ret)))
    return ret

def queryGroups():
    # Assign criterions to categories
    ret = []
    # Identification cat
    ret.append(['ActifDirectory',
                [['OU user', 'ex OU User'],
                 ['OU Machine', 'ex OU Machine']
                 ]])
    return ret

def extendedPossibilities():
    """
    xmpp plugin has no extended possibilities
    """
    return {}

def query(ctx, criterion, value):
    logging.getLogger().info(ctx)
    logging.getLogger().info(criterion)
    logging.getLogger().info(value)
    machines = []

    if criterion == 'OU user':
        machines = [x.name for x in Glpi().getMachineByHostname(ctx, value)]
    elif criterion == 'OU Machine':
        machines = [x.name for x in Glpi().getMachineByContact(ctx, value)]
    return [machines, True]

def getAllOuuser(ctx, value=''):
    return unique([x.ouuser for x in XmppMasterDatabase().getAllOUuser(ctx, value)])

def getAllOumachine(ctx, value=''):
    return unique([x.oumachine for x in XmppMasterDatabase().getAllOUmachine(ctx, value)])


first_name='varun'
last_name='saagar'

#In 3.6 below syntax can be used
fullname=f'{first_name} {last_name}'
message = f'Hello , {fullname.title()}!' #title is a method that makes the formating all first words to upper cares
print(f'Hello , {fullname.title()}')

#But in 3.5 older versions

fullname='{}'.format(first_name,last_name).title() # What will happen if we use single bracs to store two parentheses.
print(fullname)

#f-string is for format , python formats the string by replacing the name of any variable in braces its value


import time
import numpy as np

from imagingtester import (
    MINIMUM_PIXEL_VALUE,
    MAXIMUM_PIXEL_VALUE,
    N_RUNS,
    DTYPE,
    create_arrays,
    SIZES_SUBSET,
    TEST_PARALLEL_NUMBA,
)
from numba_test_utils import (
    NumbaImplementation,
    create_vectorise_add_arrays,
    create_vectorise_background_correction,
    LIB_NAME,
)
from numpy_scipy_imaging_filters import numpy_background_correction
from write_and_read_results import (
    write_results_to_file,
    ADD_ARRAYS,
    BACKGROUND_CORRECTION,
    ARRAY_SIZES,
)

mode = "parallel"

if not TEST_PARALLEL_NUMBA:
    exit()

add_arrays = create_vectorise_add_arrays(mode)
background_correction = create_vectorise_background_correction(mode)

class NumbaParallelImplementation(NumbaImplementation):
    def __init__(self, size, dtype):
        super().__init__(size, dtype)
        self.warm_up()

    def warm_up(self):
        """
        Give the functions a chance to compile.
        """
        warm_up_arrays = create_arrays((1, 1, 1), self.dtype)
        add_arrays(*warm_up_arrays[:2])
        background_correction(*warm_up_arrays, MINIMUM_PIXEL_VALUE, MAXIMUM_PIXEL_VALUE)

    def get_time(self):
        return time.time()

    def timed_imaging_operation(self, runs, alg, alg_name, n_arrs_needed):

        operation_time = 0

        # Repeat the operation
        for _ in range(runs):
            operation_time += self.time_function(
                lambda: alg(*self.cpu_arrays[:n_arrs_needed])
            )

        self.print_operation_times(
            total_time=operation_time, operation_name=alg_name, runs=runs
        )

        return operation_time / runs

practice_array = np.ones(shape=(5, 5, 5)).astype(DTYPE)
parallel_result = add_arrays(practice_array, practice_array)
assert np.all(parallel_result == 2)

# Checking the two background corrections get the same result
np_data, np_dark, np_flat = [
    np.random.uniform(low=0.0, high=20, size=(5, 5, 5)) for _ in range(3)
]

numba_data = np_data.copy()
numba_dark = np_dark.copy()
numba_flat = np_flat.copy()

background_correction(
    numba_data, numba_dark, numba_flat, MINIMUM_PIXEL_VALUE, MAXIMUM_PIXEL_VALUE
)
numpy_background_correction(
    np_data, np_dark, np_flat, MINIMUM_PIXEL_VALUE, MAXIMUM_PIXEL_VALUE
)
assert np.allclose(np_data, numba_data)

add_arrays_results = []
background_correction_results = []

def background_correction_fixed_clips(dark, data, flat):
    return background_correction(
        data, dark, flat, MINIMUM_PIXEL_VALUE, MAXIMUM_PIXEL_VALUE
    )

for size in ARRAY_SIZES[:SIZES_SUBSET]:

    imaging_obj = NumbaParallelImplementation(size, DTYPE)

    avg_add = imaging_obj.timed_imaging_operation(N_RUNS, add_arrays, "adding", 2)
    avg_bc = imaging_obj.timed_imaging_operation(
        N_RUNS, background_correction_fixed_clips, "background correction", 3
    )

    add_arrays_results.append(avg_add)
    background_correction_results.append(avg_bc)

write_results_to_file([LIB_NAME, mode], ADD_ARRAYS, add_arrays_results)
write_results_to_file(
    [LIB_NAME, mode], BACKGROUND_CORRECTION, background_correction_results
)


from collections import namedtuple

metro_areas = [
('Tokyo','JP',36.933,(35.689722,139.691667)),
('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889)),
('Mexico City', 'MX', 20.142, (19.433333, -99.133333)),
('New York-Newark', 'US', 20.104, (40.808611, -74.020386)),
('Sao Paulo', 'BR', 19.649, (-23.547778, -46.635833)),
]

print('{:15} | {:^9} | {:^9}'.format('', 'lat.', 'long.'))
fmt = '{:15} | {:9.4f} | {:9.4f}'

for name, cc, pop, (latitude, longitude) in metro_areas:
    if longitude <= 0:
        print(fmt.format(name, latitude, longitude))

City = namedtuple('City', 'name country population coordinates')
LatLong = namedtuple('LatLong', 'lat long')
print(City._fields)
tokyo = City('Tokyo', 'JP', 36.933, (35.689722, 139.691667))
tokyo_d = City._make(tokyo)
print(tokyo)
print(tokyo_d._asdict())


import scrapy
from jdproject.items import JdprojectItem

class JDImageSpider(scrapy.Spider):
    name = 'jdimage'

    start_urls = [
        'https://www.jd.com'
    ]

    def parse(self, response):
        item = JdprojectItem()
        item['name'] = response.css('.cate_menu_item a::text').extract()
        item['url'] = response.css('.cate_menu_item a::attr(href)').extract()
        print(item)


import urllib2
import re
import requests
import HTMLParser
from bs4 import BeautifulSoup
from core import base
from core.db import Base, session_scope
from core.auth import Auth
from core.utils import Whitelist, split_address
from sqlalchemy import Column, Integer, String

class TitleRegex(Base):
    __tablename__ = 'title_regex'

    id = Column(Integer, primary_key=True)
    bot_id = Column(String)
    regex = Column(String)

    @classmethod
    def list_regexes(cls, bot_id):
        with session_scope() as session:
            return session.query(cls.id, cls.regex).filter(cls.bot_id == bot_id).all()

    @classmethod
    def add(cls, bot_id, regex):
        re.compile(regex)
        with session_scope() as session:
            treg = cls(bot_id=bot_id, regex=regex)
            session.add(treg)
            session.commit()
            return treg.id

    @classmethod
    def delete(cls, bot_id, id):
        with session_scope() as session:
            treg = session.query(cls).filter(cls.bot_id == bot_id, cls.id == id).first()
            if treg:
                session.delete(treg)
                return True
            return False

h = HTMLParser.HTMLParser()

class Titles(base.baseClass):
    test = 2
    def __init__(self, *args, **kwargs):
        super(Titles, self).__init__(*args, **kwargs)
        self.set_regex_list()

    def set_regex_list(self):
        self.regexps = []
        for _, regex in TitleRegex.list_regexes(self.irc.id):
            self.regexps.append(re.compile(regex))

    def get_title(self, url):
        r = requests.get(url)
        soup = BeautifulSoup(r.content)
        title = h.unescape(soup.title.string)
        title = ' '.join(title.splitlines())
        return r, url, title

    def on_privmsg(self, address, target, text):
        if target.startswith('#') and 'http' in text:
            nickname, username, hostname = split_address(address)
            user = self.irc.network.get_user_by_nickname(nickname)
            if user and user.networkauth and Whitelist.has_permission(self.irc.id, user.networkauth, 'block_titles'):
                return
            for regex in self.regexps:
                titlesearch = regex.search(text)
                if titlesearch is not None:
                    link = titlesearch.group(1)
                    try:
                        self.irc.logger.info('Trying to get title from URL: %s', link)
                        r, url, title = self.get_title(link)
                        if r.status_code != 200:
                            self.irc.logger.error('Titles failed for URL: %s - %s', link, r.status_code)
                            raise urllib2.HTTPError
                    except (urllib2.HTTPError, AttributeError) as e:
                        self.irc.logger.exception(e)
                        self.irc.msg(target, 'Unable to retrieve title')
                    else:
                        if len(title) > 200:
                            title = title[0:197] + '...'
                        self.irc.msg(target, u'Title: {}'.format(title))
                    break
        elif target == self.irc.nickname and text.startswith('titles'):
            nickname = address.split('!')[0]
            params = text.split()
            if params[0] != 'titles':
                return
            user = Auth.get_user_by_hostmask(self.irc.id, address)
            if user is None or 'w' not in user.flags:
                self.irc.notice(nickname, 'Insufficient privileges')
            elif len(params) == 1:
                self.irc.notice(nickname, 'Available commands: add del list')
            elif params[1] == 'add':
                if len(params) != 3:
                    self.irc.notice(nickname, 'Usage: titles add <regex>')
                else:
                    try:
                        rid = TitleRegex.add(self.irc.id, ' '.join(params[2:]))
                    except re.error as e:
                        self.irc.notice(nickname, 'Invalid regexp: {}'.format(e))
                    else:
                        self.irc.notice(nickname, 'Done. Regex id: {}'.format(rid))
                    self.set_regex_list()
            elif params[1] == 'del':
                if len(params) != 3:
                    self.irc.notice(nickname, 'Usage: titles del <id>')
                else:
                    if TitleRegex.delete(self.irc.id, params[2]):
                        self.irc.notice(nickname, 'Done')
                    else:
                        self.irc.notice(nickname, 'Unable to delete regex')
            elif params[1] == 'list':
                regexps = TitleRegex.list_regexes(self.irc.id)
                self.irc.notice(nickname, 'Id Regex')
                for x in regexps:
                    self.irc.notice(nickname, '%s %s' % x)
                self.irc.notice(nickname, 'End of list')


import sys
import os
import time
import json
import datetime
import logging

from onedrive import onedrive_simple_sdk
from image_processing import process_image
from datetime import timezone
from video_processing import video_creation_datetime

############## Classes - Data Objects ####################
class PhotoInfo:
    filename = ""
    month = ""
    year = ""
    status = ""

############ Functions ################

def get_extension(filename):
    basename = os.path.basename(filename)  # os independent
    ext = basename.split('.')[-1]
    return ext.lower()

def read_configuration():
    with open('config.json') as json_file:
        data = json.load(json_file)
        return data

def read_credentials(filename):
    with open(filename) as json_file:
        data = json.load(json_file)
        return data

#################################################################################################################################
#################################################################################################################################
#################################################################################################################################
#################################################################################################################################

## Recursive function to transvese the folder structure
def list_onedrive_folder(onedrive_origin_client, source_path, config):
    logging.info("process_onedrive_camera_roll(): Starting to process the onedrive files.")

    result = onedrive_origin_client.listfiles(source_path)

    if(result["status"]):

        item_list = result["itemlist"]

        for item in item_list:
            if(item["type"] == "folder"):
                logging.info("process_onedrive_camera_roll(): Processing folder: %s", item["name"])
                new_path = source_path + "/" +  item["name"]
                list_onedrive_folder(onedrive_origin_client, new_path, config)

            elif(item["type"] == "file"):
                print(source_path + "/" + item["name"])

    else:
        logging.error("Error listing files: %s", result["message"])

######### MAIN PART ##########
logging.basicConfig(format='%(asctime)s [%(levelname)s] [%(name)s] %(message)s', level=logging.ERROR)

logging.info("Listing all files in the directory structure")

### Variables for global processing
destination_credentials_file = ""
destination_path = ""

## Read configuration file
config = read_configuration()

origins = config["origins"]

#### Cycle through all the origins and process the files
for origin in origins:
    profile_name = origin["profile_name"]
    origin_credentials_file = origin["credentials"]
    source_path = origin["source_path"]
    profile_destination_folder = origin["destination_folder"]

    origin_credentials = read_credentials(origin_credentials_file)

    ## Connecting to the origin
    origin_client = onedrive_simple_sdk(origin_credentials["clientID"], origin_credentials["clientSecret"], origin_credentials["refreshToken"])
    logging.info("Connected to origin client profile: %s", profile_name)

    list_onedrive_folder(origin_client, source_path, config)

logging.info("Completed listing all files.")


import os
from typing import List, Optional
import dataclasses
from official.core import config_definitions as cfg
from official.core import exp_factory
from official.modeling import hyperparams
from official.modeling import optimization
from official.vision.beta.configs import backbones
from official.vision.beta.configs import common
from official.vision.beta.configs import decoders

# pylint: disable=missing-class-docstring
@dataclasses.dataclass
class TfExampleDecoder(hyperparams.Config):
  regenerate_source_id: bool = False

@dataclasses.dataclass
class TfExampleDecoderLabelMap(hyperparams.Config):
  regenerate_source_id: bool = False
  label_map: str = ''

@dataclasses.dataclass
class DataDecoder(hyperparams.OneOfConfig):
  type: Optional[str] = 'simple_decoder'
  simple_decoder: TfExampleDecoder = TfExampleDecoder()
  label_map_decoder: TfExampleDecoderLabelMap = TfExampleDecoderLabelMap()

@dataclasses.dataclass
class Parser(hyperparams.Config):
  num_channels: int = 3
  match_threshold: float = 0.5
  unmatched_threshold: float = 0.5
  aug_rand_hflip: bool = False
  aug_scale_min: float = 1.0
  aug_scale_max: float = 1.0
  skip_crowd_during_training: bool = True
  max_num_instances: int = 100

@dataclasses.dataclass
class DataConfig(cfg.DataConfig):
  """Input config for training."""
  input_path: str = ''
  global_batch_size: int = 0
  is_training: bool = False
  dtype: str = 'bfloat16'
  decoder: DataDecoder = DataDecoder()
  parser: Parser = Parser()
  shuffle_buffer_size: int = 10000

@dataclasses.dataclass
class Anchor(hyperparams.Config):
  num_scales: int = 3
  aspect_ratios: List[float] = dataclasses.field(
      default_factory=lambda: [0.5, 1.0, 2.0])
  anchor_size: float = 4.0

@dataclasses.dataclass
class Losses(hyperparams.Config):
  focal_loss_alpha: float = 0.25
  focal_loss_gamma: float = 1.5
  huber_loss_delta: float = 0.1
  box_loss_weight: int = 50
  l2_weight_decay: float = 0.0

@dataclasses.dataclass
class RetinaNetHead(hyperparams.Config):
  num_convs: int = 4
  num_filters: int = 256
  use_separable_conv: bool = False

@dataclasses.dataclass
class DetectionGenerator(hyperparams.Config):
  pre_nms_top_k: int = 5000
  pre_nms_score_threshold: float = 0.05
  nms_iou_threshold: float = 0.5
  max_num_detections: int = 100
  use_batched_nms: bool = False

@dataclasses.dataclass
class RetinaNet(hyperparams.Config):
  num_classes: int = 0
  input_size: List[int] = dataclasses.field(default_factory=list)
  min_level: int = 3
  max_level: int = 7
  anchor: Anchor = Anchor()
  backbone: backbones.Backbone = backbones.Backbone(
      type='resnet', resnet=backbones.ResNet())
  decoder: decoders.Decoder = decoders.Decoder(
      type='fpn', fpn=decoders.FPN())
  head: RetinaNetHead = RetinaNetHead()
  detection_generator: DetectionGenerator = DetectionGenerator()
  norm_activation: common.NormActivation = common.NormActivation()

@dataclasses.dataclass
class RetinaNetTask(cfg.TaskConfig):
  model: RetinaNet = RetinaNet()
  train_data: DataConfig = DataConfig(is_training=True)
  validation_data: DataConfig = DataConfig(is_training=False)
  losses: Losses = Losses()
  init_checkpoint: Optional[str] = None
  init_checkpoint_modules: str = 'all'  # all or backbone
  annotation_file: Optional[str] = None
  gradient_clip_norm: float = 0.0
  per_category_metrics: bool = False

@exp_factory.register_config_factory('retinanet')
def retinanet() -> cfg.ExperimentConfig:
  """RetinaNet general config."""
  return cfg.ExperimentConfig(
      task=RetinaNetTask(),
      restrictions=[
          'task.train_data.is_training != None',
          'task.validation_data.is_training != None'
      ])

COCO_INPUT_PATH_BASE = 'coco'
COCO_TRIAN_EXAMPLES = 118287
COCO_VAL_EXAMPLES = 5000

@exp_factory.register_config_factory('retinanet_resnetfpn_coco')
def retinanet_resnetfpn_coco() -> cfg.ExperimentConfig:
  """COCO object detection with RetinaNet."""
  train_batch_size = 256
  eval_batch_size = 8
  steps_per_epoch = COCO_TRIAN_EXAMPLES // train_batch_size

  config = cfg.ExperimentConfig(
      runtime=cfg.RuntimeConfig(mixed_precision_dtype='bfloat16'),
      task=RetinaNetTask(
          init_checkpoint='gs://cloud-tpu-checkpoints/vision-2.0/resnet50_imagenet/ckpt-28080',
          init_checkpoint_modules='backbone',
          annotation_file=os.path.join(COCO_INPUT_PATH_BASE,
                                       'instances_val2017.json'),
          model=RetinaNet(
              num_classes=91,
              input_size=[640, 640, 3],
              min_level=3,
              max_level=7),
          losses=Losses(l2_weight_decay=1e-4),
          train_data=DataConfig(
              input_path=os.path.join(COCO_INPUT_PATH_BASE, 'train*'),
              is_training=True,
              global_batch_size=train_batch_size,
              parser=Parser(
                  aug_rand_hflip=True, aug_scale_min=0.5, aug_scale_max=2.0)),
          validation_data=DataConfig(
              input_path=os.path.join(COCO_INPUT_PATH_BASE, 'val*'),
              is_training=False,
              global_batch_size=eval_batch_size)),
      trainer=cfg.TrainerConfig(
          train_steps=72 * steps_per_epoch,
          validation_steps=COCO_VAL_EXAMPLES // eval_batch_size,
          validation_interval=steps_per_epoch,
          steps_per_loop=steps_per_epoch,
          summary_interval=steps_per_epoch,
          checkpoint_interval=steps_per_epoch,
          optimizer_config=optimization.OptimizationConfig({
              'optimizer': {
                  'type': 'sgd',
                  'sgd': {
                      'momentum': 0.9
                  }
              },
              'learning_rate': {
                  'type': 'stepwise',
                  'stepwise': {
                      'boundaries': [
                          57 * steps_per_epoch, 67 * steps_per_epoch
                      ],
                      'values': [
                          0.32 * train_batch_size / 256.0,
                          0.032 * train_batch_size / 256.0,
                          0.0032 * train_batch_size / 256.0
                      ],
                  }
              },
              'warmup': {
                  'type': 'linear',
                  'linear': {
                      'warmup_steps': 500,
                      'warmup_learning_rate': 0.0067
                  }
              }
          })),
      restrictions=[
          'task.train_data.is_training != None',
          'task.validation_data.is_training != None'
      ])

  return config

@exp_factory.register_config_factory('retinanet_spinenet_coco')
def retinanet_spinenet_coco() -> cfg.ExperimentConfig:
  """COCO object detection with RetinaNet using SpineNet backbone."""
  train_batch_size = 256
  eval_batch_size = 8
  steps_per_epoch = COCO_TRIAN_EXAMPLES // train_batch_size
  input_size = 640

  config = cfg.ExperimentConfig(
      runtime=cfg.RuntimeConfig(mixed_precision_dtype='float32'),
      task=RetinaNetTask(
          annotation_file=os.path.join(COCO_INPUT_PATH_BASE,
                                       'instances_val2017.json'),
          model=RetinaNet(
              backbone=backbones.Backbone(
                  type='spinenet',
                  spinenet=backbones.SpineNet(
                      model_id='49', stochastic_depth_drop_rate=0.2)),
              decoder=decoders.Decoder(
                  type='identity', identity=decoders.Identity()),
              anchor=Anchor(anchor_size=3),
              norm_activation=common.NormActivation(
                  use_sync_bn=True, activation='swish'),
              num_classes=91,
              input_size=[input_size, input_size, 3],
              min_level=3,
              max_level=7),
          losses=Losses(l2_weight_decay=4e-5),
          train_data=DataConfig(
              input_path=os.path.join(COCO_INPUT_PATH_BASE, 'train*'),
              is_training=True,
              global_batch_size=train_batch_size,
              parser=Parser(
                  aug_rand_hflip=True, aug_scale_min=0.1, aug_scale_max=2.0)),
          validation_data=DataConfig(
              input_path=os.path.join(COCO_INPUT_PATH_BASE, 'val*'),
              is_training=False,
              global_batch_size=eval_batch_size)),
      trainer=cfg.TrainerConfig(
          train_steps=500 * steps_per_epoch,
          validation_steps=COCO_VAL_EXAMPLES // eval_batch_size,
          validation_interval=steps_per_epoch,
          steps_per_loop=steps_per_epoch,
          summary_interval=steps_per_epoch,
          checkpoint_interval=steps_per_epoch,
          optimizer_config=optimization.OptimizationConfig({
              'optimizer': {
                  'type': 'sgd',
                  'sgd': {
                      'momentum': 0.9
                  }
              },
              'learning_rate': {
                  'type': 'stepwise',
                  'stepwise': {
                      'boundaries': [
                          475 * steps_per_epoch, 490 * steps_per_epoch
                      ],
                      'values': [
                          0.32 * train_batch_size / 256.0,
                          0.032 * train_batch_size / 256.0,
                          0.0032 * train_batch_size / 256.0
                      ],
                  }
              },
              'warmup': {
                  'type': 'linear',
                  'linear': {
                      'warmup_steps': 2000,
                      'warmup_learning_rate': 0.0067
                  }
              }
          })),
      restrictions=[
          'task.train_data.is_training != None',
          'task.validation_data.is_training != None'
      ])

  return config


#!/usr/bin/env python3
class Solution:
	@staticmethod
	def findDuplicate(nums: list[int]) -> int:
		slow = 0
		fast = 0
		while True:
			slow = nums[slow]
			fast = nums[nums[fast]]
			if slow == fast:
				break

		slow2 = 0
		while True:
			slow = nums[slow]
			slow2 = nums[slow2]
			if slow == slow2:
				break
		return slow

l_list = [1,3,4,2,2]
l_res = Solution.findDuplicate(l_list)
print('{}: result: {}'.format(l_list, l_res))

l_list = [3,1,3,3,2]
l_res = Solution.findDuplicate(l_list)
print('{}: result: {}'.format(l_list, l_res))


inp = [l.split() for l in open('dec21.txt')]
#inp = [l.split() for l in open('dec21ex.txt')]
#print(inp)
match = []
for l in inp:
    inputpattern = l[0].split("/")
    outputpattern = l[2].split("/")
    match.append([inputpattern, outputpattern])

#print (match)

#print("123"[-1])

def makenumber(s):
    i = res = 0
    while len(s) > 0:
        if s[-1] == "#": res += 2**i
        i += 1; s = s[:-1]
    return res

#print(makenumber("#..."))

def rotate(m):
    #print("rotating", m)
    res = set()
    if len(m)==4:
        for i in range(4):
            res.add(makenumber(m))
            m = m[2] + m[0] + m[3] + m[1]
            #print(i, len(m), m)
    if len(m) == 9:
        for i in range(4):
            res.add(makenumber(m))
            m = m[6] + m[3] + m[0] + m[7] + m[4] + m[1] + m[8] + m[5] + m[2]
            #print(i, len(m), m)
        #print("first   ", m)
        m = m[2] + m[1] + m[0] + m[5] + m[4] + m[3] + m[8] + m[7] + m[6]
        #print("reversed", m)
        for i in range(4):
            res.add(makenumber(m))
            m = m[6] + m[3] + m[0] + m[7] + m[4] + m[1] + m[8] + m[5] + m[2]
            #print(i, len(m), m)
        #print(sorted(res))
    return res

def makestr(m):
    res = ""
    if len(m) == 2:
        return m[0] + m[1]
    elif len(m) == 3:
        return m[0] + m[1] + m[2]

def rotations(m):
    res = rotate(makestr(m))
    return res

#print(match)
rs = []
rules2 = dict()
rules3 = dict()

for r in match:
    if len(r[0]) == 3:
        for x in rotations(r[0]):
            rs.append(x)
            rules3[x] = r[1]
    if len(r[0]) == 2:
        for x in rotations(r[0]):
            rs.append(x)
            rules2[x] = r[1]

#print(rules2)
#print(rules3)

def printmatrix(matrix):
    c = 0
    print("MATRIX")
    for m in matrix:
#        print(m)
        for i in m:
            if i == "#": c+=1
    print("--- Count: {} ---".format(c))

matrix = [".#.", "..#", "###"]

def makematrix(chunks, newblock):
    #print("new", len(newblock))
    res = []
    #print("chunks", chunks)
    for y in range(chunks):
        for l in range(len(newblock[0])):
            s = ""
            for x in range(chunks):
                #print("Want: {} Have {} (x/y: {}/{}  {})"\
                #      .format(y*chunks + x, len(newblock), x, y, chunks))
                b = newblock[y*chunks + x]
                s += b[l]
#                print("b", b)
            res.append(s)
    return res

printmatrix(matrix)
for i in range(18):
    if len(matrix) % 2 == 0:
        print(i, "-- 2 => 3  Size: {} Chunks: {}".format(len(matrix), len(matrix) // 2))

        chunks = len(matrix) // 2
        newblock = []
        oldblock = []
        for y in range(chunks):
            for x in range(chunks):
                block = [matrix[y * 2 + 0][x * 2:(x + 1) * 2],
                         matrix[y * 2 + 1][x * 2:(x + 1) * 2]]
                oldblock.append(block)
#                print("b2: ", block)
                n = makenumber(makestr(block))
                r = rules2[n]
                newblock.append(r)
        matrix = makematrix(chunks, newblock)
        print("Old blocks", oldblock)
        print("New blocks", newblock)
    elif len(matrix) % 3 == 0:
        print(i, "-- 3 => 4  Size: {} Chunks: {}".format(len(matrix), len(matrix) // 3))
        chunks = len(matrix) // 3
        newblock = []
        for y in range(chunks):
            for c in range(chunks):
                block = [matrix[y * 3 + 0][c * 3:(c + 1) * 3],
                         matrix[y * 3 + 1][c * 3:(c + 1) * 3],
                         matrix[y * 3 + 2][c * 3:(c + 1) * 3]]
                #print("b: ", block)
                n = makenumber(makestr(block))
                r = rules3[n]
                newblock.append(r)
        matrix = makematrix(chunks, newblock)
    printmatrix(matrix)

printmatrix(matrix)