from __future__ import print_function, unicode_literals

from versile.internal import _vexport
from versile.common.util import VObjectIdentifier

__all__ = ['VSECodes', 'VSECode']
__all__ = _vexport(__all__)

class VSECode(object):
    """Tag code used in VSE encoding.

    Attributes set on the class hold a short-form of tag data. Use
    :meth:`tag_data` to create the expanded form.

    """

    # Also defined in versile.orb.module
    OID_PREFIX = (1, 3, 6, 1, 4, 1, 38927, 1)
    """OID prefix for Versile defined module standard entities."""

    def __init__(self, name, version, oid):
        self._name = name
        self._version = version
        self._oid = oid

    @property
    def oid_code(self):
        """Tag codes associated with OID (tuple), or None if no OID"""
        if self._oid:
            return (10*len(self._oid) + 1,) + self._oid
        else:
            return None

    @property
    def name_code(self):
        """Tag codes associated with name (tuple), or None if no name"""
        if self.__name:
            return -1, ('versile',) + self._name, self._version
        else:
            return None

    def tags(self, ctx):
        """Return tag codes for the given context.

        :param ctx:    context
        :type  ctx:    :class:`versile.orb.entity.VIOContext`
        :returns:      tag codes
        :rtype:        tuple

        """
        if ctx.mod_use_oid:
            if self._oid:
                return self.oid_code
            elif self._name:
                return self.name_code
        elif self._name:
            return self.name_code
        elif _oid:
            return self.oid_code
        raise RuntimeError('No oid or name defined')

    def mod_decoder(self, handler):
        """Generates a :class:`versile.orb.module.VModuleDecoder`\ .

        :param handler:  tag data parser
        :type  handler:  callable
        :returns:        module entry for provided data
        :rtype:          :class:`versile.orb.module.VModuleDecoder`

        """
        from versile.orb.module import VModuleDecoder
        _name, _oid = self._name, self._oid
        if _name:
            _name = ('versile',) + self._name
        if _oid:
            _oid = VObjectIdentifier(self.OID_PREFIX + self._oid)
        return VModuleDecoder(_name, self._version, _oid, handler)

class VSECodes(object):
    """Tag codes used in VSE encoding.

    Tag codes are defined as class attributes. Their type is
    :class:`VSECode`\ .

    """

    MULTI_ARRAY = VSECode(('container', 'frozenmultiarray'), (0, 8), (1, 1))
    """Tag code for :class:`versile.vse.container.VFrozenMultiArray`\ ."""

    DICTIONARY = VSECode(('container', 'frozendict'), (0, 8), (1, 2))
    """Tag code for :class:`versile.vse.container.VFrozenDict`\ ."""

    SET = VSECode(('container', 'frozenset'), (0, 8), (1, 3))
    """Tag code for :class:`versile.vse.container.VFrozenSet`\ ."""

    ARRAY_OF_INT = VSECode(('container', 'arrayofint'), (0, 8), (1, 4))
    """Tag code for :class:`versile.vse.container.VArrayOfInt`\ ."""

    ARRAY_OF_LONG = VSECode(('container', 'arrayoflong'), (0, 8), (1, 5))
    """Tag code for :class:`versile.vse.container.VArrayOfLong`\ ."""

    ARRAY_OF_VINTEGER = VSECode(('container', 'arrayofvinteger'), (0, 8),
                                (1, 6))
    """Tag code for :class:`versile.vse.container.VArrayOfVInteger`\ ."""

    ARRAY_OF_FLOAT = VSECode(('container', 'arrayoffloat'), (0, 8), (1, 7))
    """Tag code for :class:`versile.vse.container.VArrayOfFloat`\ ."""

    ARRAY_OF_DOUBLE = VSECode(('container', 'arrayofdouble'), (0, 8), (1, 8))
    """Tag code for :class:`versile.vse.container.VArrayOfDouble`\ ."""

    ARRAY_OF_VFLOAT = VSECode(('container', 'arrayofvfloat'), (0, 8), (1, 9))
    """Tag code for :class:`versile.vse.container.VArrayOfVFloat`\ ."""

    BYTE_STREAMER = VSECode(('stream', 'bytestreamer'), (0, 8), (2, 1))
    """Tag code for :class:`versile.vse.stream.VByteStreamer`\ ."""

    ENTITY_STREAMER = VSECode(('stream', 'entitystreamer'), (0, 8), (2, 2))
    """Tag code for :class:`versile.vse.stream.VByteStreamer`\ ."""

    NATIVE_OBJECT = VSECode(('native', 'object'), (0, 8), (3, 1))
    """Tag code for :class:`versile.vse.native.VNative`\ ."""

    NATIVE_EXCEPTION = VSECode(('native', 'exception'), (0, 8), (3, 2))
    """Tag code for :class:`versile.vse.native.VNativeException`\ ."""

    FUNCTION = VSECode(('util', 'function'), (0, 8), (4, 1))
    """Tag code for :class:`versile.vse.util.VFunction`\ ."""

    UDPRELAY = VSECode(('util', 'udprelay'), (0, 8), (4, 2))
    """Tag code for :class:`versile.vse.util.VUDPRelay`\ ."""

    UDPRELAYEDVOP = VSECode(('util', 'udp_vop'), (0, 8), (4, 3))
    """Tag code for :class:`versile.vse.util.VUDPRelayedVOP`\ ."""

    LOGIN = VSECode(('util', 'login'), (0, 8), (4, 4))
    """Tag code for :class:`versile.vse.util.VPasswordLogin`\ ."""

    UTCTIME = VSECode(('time', 'utctime'), (0, 8), (5, 1))
    """Tag code for :class:`versile.vse.time.VUTCTime`\ ."""

    CONCEPT = VSECode(('semantics', 'concept'), (0, 8), (6, 1))
    """Tag code for :class:`versile.vse.semantics.VConcept`\ ."""

    PREFIXED_UNIT = VSECode(('math', 'prefixedunit'), (0, 8), (7, 1))
    """Tag code for :class:`versile.vse.math.VPrefixedUnit`\ ."""

    DIMENSIONAL_QUANTITY = VSECode(('math', 'dimensionalquantity'),
                                   (0, 8), (7, 2))
    """Tag code for :class:`versile.vse.math.VDimensionalQuantity`\ ."""

class VSEModuleCodes(object):
    """Module codes used for globally registering VSE modules."""

    CONTAINER = 1
    """Module code for versile.vse.container"""

    NATIVE = 2
    """Module code for versile.vse.native.module"""

    STREAM = 3
    """Module code for versile.vse.stream"""

    UTIL = 4
    """Module code for versile.vse.util"""

    TIME = 5
    """Module code for versile.vse.time"""

    SEMANTICS = 6
    """Module code for versile.vse.semantics"""

    MATH = 7
    """Module code for versile.vse.math"""


def next(a):
    i=n-1
    if len(a)==0:
        return True
    while i>0 and a[i-1]>=a[i]:
        i-=1
    if i<=0:
        return False

    j=n-1
    while a[i-1]>=a[j]:
        j-=1

    a[i-1],a[j]=a[j],a[i-1]

    j=n-1
    while i<j:
        a[i],a[j]=a[j],a[i]
        i+=1
        j-=1
    return True

n=int(input())
a=list(map(int,input().split()))
if next(a):
    print(' '.join(map(str,a)))
else:
    print(-1)


#!/usr/bin/env python
import sys, string, random

def suffix(string):
	return string[1:]
def prefix(string):
	return string[:len(string)-1]

def constructDeBruijn(kmers):
	adjacencies = {}
	nodes2aminos = {}
	n = 0
	edges = []
	nodes = set()

	#To get the deburijn graph that actually nodes are the k-mer size ... only for plotting purposes!
	'''
	for kmer1 in kmers:
		nodes.add(kmer1)
		for kmer2 in kmers:
			if suffix(kmer1) == prefix(kmer2):
				edges.append((kmer1,kmer2))
				if kmer1 not in adjacencies:
					adjacencies[kmer1]= [kmer2]
				else:
					adjacencies[kmer1].append(kmer2)
	print len(nodes)
	print len(edges)
	'''
	for kmer in kmers:
		nodes.add(prefix(kmer))
		nodes.add(suffix(kmer))
		if prefix(kmer) in adjacencies:
			adjacencies[prefix(kmer)].append(suffix(kmer))
			edges.append((prefix(kmer),suffix(kmer)))
		else:
			adjacencies[prefix(kmer)] = [suffix(kmer)]
			edges.append((prefix(kmer),suffix(kmer)))

	return adjacencies, list(nodes), edges


import subprocess
import os
import shutil
from data_loading import cd

if __name__ == "__main__":
    path = "C:/SOFTWARE and giggles/HORIISON/Other projects/IlluminatingSingularity"
    dst_folder = "C:/Users/jeppe/Dropbox/MI/uno"

    while True:
        # Opening log destination files
        std_out1 = open("Output_log.txt", "w")
        std_err1 = open("Error_log.txt", "w")

        # Running subprocess and logging output and errors
        with cd(path):
            subprocess.run(['python', 'main.py'], stderr=std_err1)

        # closing log files
        std_out1.close()
        std_err1.close()

        # deleting logfiles in destination
        if os.path.isfile(dst_folder+"/Output_log.txt"):
            os.remove(dst_folder+"/Output_log.txt")

        if os.path.isfile(dst_folder + "/Error_log.txt"):
            os.remove(dst_folder + "/Error_log.txt")

        # move log files from subprocess executing path to destination path
        shutil.move(path+"/Output_log.txt", dst=dst_folder+"/Output_log.txt")
        shutil.move(path+"/Error_log.txt", dst=dst_folder+"/Error_log.txt")


import re
import json

f = open("./roles_en.txt", "r", encoding="utf-8")
w = open("../services/en/roles.js", "w", encoding="utf-8")

roles = {}
editions = ["Basic Game", "New Moon", "The Village", "Characters"]
editions_i = 0
for edition in editions:
    roles[edition] = [];
roles["lookup_table"] = {}

name = ""
description = ""
for line in f:
    roles_edition = roles[editions[editions_i]]
    if(line == "new edition"):
        editions_i = editions_i + 1
        continue
    if(line == "\n"):
        roles_edition.append({"name": name, "description": description})
        roles["lookup_table"][name] = description
        name = ""
        description = ""
    elif(name == ""):
        line = line[:line.index("\n")]
        if(line.find("/") >= 0):
            line = line[:line.index("/")]
        name = line
    else:
        line = line[:line.index("\n")]
        description = line

w.write("module.exports = " + json.dumps(roles, ensure_ascii=False).encode("utf-8").decode("utf-8"))


import pygame
from Game.Shared import *

class Ball(GameObject):
    def __init__(self, position, sprite, game):
        self.__game = game
        self.__speed = 3
        self.__increment = [2, 2]
        self.__direction = [1, 1]
        self.__inMotion = 0

        super(Ball, self).__init__(position, GameConstants.ballSize, sprite)

    def setSpeed(self, newSpeed):
        self.__speed = newSpeed

    def resetSpeed(self):
        self.setSpeed(3)

    def getSpeed(self):
        return self.__speed

    def isInMotion(self):
        return self.__inMotion

    def setMotion(self, isMoving):
        self.__inMotion = isMoving
        self.resetSpeed()

    def changeDirection(self, gameObject):
        position = self.getPosition()
        size = self.getSize()
        objectPosition = gameObject.getPosition()
        objectSize = gameObject.getSize()

        if position[1] > objectPosition[1] and \
                position[1] < objectPosition[1] + objectSize[1] and \
                position[0] > objectPosition[0] and \
                position[0] < objectPosition[0] + objectSize[0]:
            self.__direction[1] *= -1

        elif position[1] + size[1] > objectPosition[1] and \
                position[1] + size[1] < objectPosition[1] + objectSize[1] and \
                position[0] > objectPosition[0] and \
                position[0] < objectPosition[0] + objectSize[0]:
            self.__direction[1] *= -1

        elif position[0] + size[0] > objectPosition[0] and \
                position[0] + size[0] < objectPosition[0] + objectSize[0]:
            self.__direction[0] *= -1

        else:
            self.__direction[0] *= -1

    def updatePosition(self):
        if not self.isInMotion():
            padPosition = self.__game.getPad().getPosition()
            self.setPosition((
                padPosition[0] + (GameConstants.padSize[0] / 2),
                GameConstants.screenSize[1] - GameConstants.padSize[1] - GameConstants.ballSize[1]
            ))
            return

        position = self.getPosition()
        size = self.getSize()

        newPosition = [position[0] + (self.__increment[0] * self.__speed) * self.__direction[0],
                       position[1] + (self.__increment[1] * self.__speed) * self.__direction[1]]

        if newPosition[0] + size[0] >= GameConstants.screenSize[0]:
            self.__direction[0] *= -1
            newPosition = [GameConstants.screenSize[0] - size[0], newPosition[1]]
            self.__game.playSound(GameConstants.soundWallHit)

        if newPosition[0] <= 0:
            self.__direction[0] *= -1
            newPosition = [0, newPosition[1]]
            self.__game.playSound(GameConstants.soundWallHit)

        if newPosition[1] + size[1] >= GameConstants.screenSize[1]:
            self.__direction[1] *= -1
            newPosition = [newPosition[0], GameConstants.screenSize[1] - size[1]]

        if newPosition[1] <= 0:
            self.__direction[1] *= -1
            newPosition = [newPosition[0], 0]
            self.__game.playSound(GameConstants.soundWallHit)

        self.setPosition(newPosition)

    def isBallDead(self):
        position = self.getPosition()
        size = self.getSize()
        if position[1] + size[1] >= GameConstants.screenSize[1]:
            return 1
        return 0


from dataclasses import dataclass, field
import os
import tarfile
from typing import Optional, Sequence, Tuple

# External Dependencies:
try:
    from datasets import disable_progress_bar as disable_datasets_progress_bar
except ImportError:  # Not available in datasets <v2.0.0
    disable_datasets_progress_bar = None
from torch import use_deterministic_algorithms
from transformers import HfArgumentParser, TrainingArguments
from transformers.trainer_utils import IntervalStrategy

def get_n_cpus() -> int:
    return int(os.environ.get("SM_NUM_CPUS", len(os.sched_getaffinity(0))))

def get_n_gpus() -> int:
    return int(os.environ.get("SM_NUM_GPUS", 0))

def get_default_num_workers() -> int:
    """Choose a sensible default dataloader_num_workers based on available hardware"""
    n_cpus = get_n_cpus()
    n_gpus = get_n_gpus()
    # Don't create so many workers you lock all processes into resource contention:
    max_workers = max(0, n_cpus - 2)
    if n_gpus:
        # Don't create unnecessarily high numbers of workers per GPU:
        # (Which can cause CUDAOutOfMemory e.g. on p3.16xlarge, or RAM exhaustion with SageMaker
        # Training Compiler)
        max_workers = min(
            max_workers,
            max(8, n_gpus * 4),
        )

    return max(0, max_workers)

@dataclass
class SageMakerTrainingArguments(TrainingArguments):
    """Overrides & extensions to HF's CLI TrainingArguments for training LayoutLM on SageMaker

    Refer to transformers.TrainingArguments for other/base supported CLI arguments.
    """

    dataloader_num_workers: int = field(
        # A better default for single-instance, single-device, CPU-bottlenecked training:
        default=get_default_num_workers(),
        metadata={
            "help": (
                "Number of subprocesses to use for data loading (PyTorch only). "
                "0 means that the data will be loaded in the main process."
            ),
        },
    )
    dataproc_num_workers: Optional[int] = field(
        # Our data pre-processing is explicitly configured to run in the lead process and then load
        # from cache for other processes - so we can use lots of workers because the lead proc will
        # be running it alone:
        default=max(1, get_n_cpus() - 2),
        metadata={
            "help": (
                "Number of subprocesses to use for data preparation before training commences. "
                "0 means that the data will be loaded in the main process (Good for debug)."
            ),
        },
    )
    ddp_find_unused_parameters: Optional[bool] = field(
        default=True,
        metadata={
            "help": (
                "For DistributedDataParallel training, LayoutLMv2/XLM require "
                "find_unused_parameters=True because of unused parameters in the model structure. "
                "For LLMv1 in DDP mode you could turn this off for a performance boost."
            )
        },
    )
    disable_tqdm: Optional[bool] = field(
        # Log streams can't render progress bars like a GUI terminal
        # NOTE: If you run into problems debugging dataset prep, you may like to enable progress
        default=True,
        metadata={"help": "TQDM progress bars are disabled by default for SageMaker/CloudWatch."},
    )
    do_eval: bool = field(
        default=None,
        metadata={
            "help": (
                "This value is normally set by the presence or absence of the 'validation' "
                "channel, but can be explicitly overridden."
            )
        },
    )
    do_train: bool = field(
        default=True,
        metadata={"help": "Set false to disable training (for validation-only jobs)"},
    )
    early_stopping_patience: Optional[int] = field(
        # Add ability to control early stopping through SM CLI/hyperparameters
        default=None,
        metadata={
            "help": (
                "Stop training when the model's `metric_for_best_model` metric worsens for X "
                "evaluations (epochs by default)"
            ),
        },
    )
    early_stopping_threshold: Optional[float] = field(
        # Add ability to control early stopping through SM CLI/hyperparameters
        default=None,
        metadata={
            "help": (
                "Denote the absolute value the model's `metric_for_best_model` must improve by to "
                "avoid early stopping conditions"
            ),
        },
    )
    evaluation_strategy: IntervalStrategy = field(
        # We'd like some eval metrics by default, rather than the usual "no" strategy
        default="epoch",
        metadata={"help": "The evaluation strategy to use."},
    )
    full_determinism: bool = field(
        # (This will be a standard TrainingArg as of 4.19.0, but isn't in the current 4.17)
        default=False,
        metadata={"help": ("Will be automatically enabled for this script if `seed` is truthy.")},
    )
    save_strategy: IntervalStrategy = field(
        # Should match evaluation strategy for early stopping to work
        default="epoch",
        metadata={"help": "The model save strategy to use."},
    )
    model_dir: Optional[str] = field(
        # Add this param to differentiate checkpoint output (output_dir) from final model output
        # (model_dir).
        default="/opt/ml/model",
        metadata={
            "help": (
                "(You shouldn't need to override this on SageMaker): "
                "The output directory where the final model will be written."
            ),
        },
    )
    output_dir: Optional[str] = field(
        default=(
            "/opt/ml/checkpoints"
            if os.path.isdir("/opt/ml/checkpoints")
            else "/tmp/transformers/checkpoints"
        ),
        metadata={
            "help": (
                "(You shouldn't need to override this on SageMaker): "
                "The output directory where model checkpoints and trainer state will be written. "
                "Note HF local checkpointing defaults ON even if SageMaker S3 upload defaults OFF."
            ),
        },
    )
    overwrite_output_dir: bool = field(
        default=True,
        metadata={
            "help": (
                "Overwrite the content of the output directory."
                "Use this to continue training if output_dir points to a checkpoint directory."
            ),
        },
    )
    # Tweak default batch sizes for this model & task
    per_device_train_batch_size: int = field(
        default=4, metadata={"help": "Batch size per GPU/TPU core/CPU for training."}
    )
    per_device_eval_batch_size: int = field(
        default=8, metadata={"help": "Batch size per GPU/TPU core/CPU for evaluation."}
    )
    remove_unused_columns: bool = field(
        default=False,
        metadata={
            "help": (
                "Whether to automatically remove datasets.Dataset columns unused by the "
                "model.forward() method. This should be False by default, as our implementation "
                "either uses a custom data collator (LLMv1) or pre-processes the dataset (v2/XLM)."
            ),
        },
    )

    def __post_init__(self):
        super().__post_init__()
        # HF datasets library requires n_proc = None, not 0, if workers are disabled:
        if not self.dataproc_num_workers:
            self.dataproc_num_workers = None
        # ...And it doesn't see the TrainingArguments progress setting by default:
        if self.disable_tqdm and disable_datasets_progress_bar:
            disable_datasets_progress_bar()
        # This script uses cuBLAS operators that require a workspace to run in deterministic /
        # reproducible mode. You could alternatively set ":16:8" to save (about 24MiB of) GPU
        # memory at the cost of performance. More information at:
        # https://docs.nvidia.com/cuda/cublas/index.html#cublasApi_reproducibility
        if self.seed:
            self.full_determinism = True
            if "CUBLAS_WORKSPACE_CONFIG" not in os.environ:
                print(
                    "Defaulting CUBLAS_WORKSPACE_CONFIG=':4096:8' to enable deterministic ops as "
                    "`seed` is set."
                )
                os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
            use_deterministic_algorithms(True)
        # Normalize early stopping configuration if it seems enabled:
        if self.early_stopping_patience is not None or self.early_stopping_threshold is not None:
            # The EarlyStoppingCallback requires load_best_model_at_end=True:
            self.load_best_model_at_end = True
            # Also make sure the early stopping settings default sensibly if turning it on:
            if self.early_stopping_patience is None:
                self.early_stopping_patience = 1
            if not self.early_stopping_threshold:
                self.early_stopping_threshold = 0.0

@dataclass
class ModelArguments:
    """Arguments pertaining to which model/config/tokenizer we are going to train."""

    cache_dir: Optional[str] = field(
        # Map this folder to the persistent checkpoints dir if available, or else at least pick
        # somewhere that's under the EBS mount (not the fixed-size root volume).
        default=(
            "/opt/ml/checkpoints/cache"
            if os.path.isdir("/opt/ml/checkpoints")
            else "/tmp/transformers/cache"
        ),
        metadata={
            "help": (
                "(You shouldn't need to override this on SageMaker): "
                "Caches to /opt/ml/checkpoints/cache if SaageMaker checkpointing is enabled, "
                "otherwise to a folder in the container's /tmp."
            ),
        },
    )
    config_name: Optional[str] = field(
        default=None,
        metadata={"help": "Pretrained config name or path if not the same as model_name"},
    )
    model_name_or_path: Optional[str] = field(
        default=os.environ.get("SM_CHANNEL_MODEL_NAME_OR_PATH"),
        metadata={
            "help": "The model checkpoint for weights initialization."
            "Usually, either set this as a name in SageMaker hyperparameter (e.g. "
            "'microsoft/layoutlm-base-uncased') or as an S3 URI in SageMaker channel (e.g. "
            "estimator.fit({'model_name_or_path': 's3://...tar.gz'}). Leave unset if you want to "
            "train a model from scratch."
        },
    )
    model_revision: str = field(
        default="main",
        metadata={
            "help": "The specific model version to use (branch name, tag name or commit id).",
        },
    )
    tokenizer_name: Optional[str] = field(
        default=None,
        metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"},
    )
    use_auth_token: bool = field(
        default=False,
        metadata={
            "help": (
                "Will use the token generated when running `transformers-cli login` "
                "(necessary to use this script with private models)."
            ),
        },
    )

    def __post_init__(self):
        # Extract pretrained model if provided as tarball / folders containing tarball:
        if os.path.isdir(self.model_name_or_path):
            contents = os.listdir(self.model_name_or_path)
            print(f"Got pretrained model folder with contents: {contents}")
            tar_candidates = list(filter(lambda f: f.lower().endswith(".tar.gz"), contents))
            n_tar_candidates = len(tar_candidates)
            if n_tar_candidates == 1:
                # (This is the path that gets used when supplying prev trained model as a channel)
                print(f"Extracting model tarball {tar_candidates[0]} to {self.model_name_or_path}")
                with tarfile.open(
                    os.path.join(self.model_name_or_path, tar_candidates[0])
                ) as tarmodel:
                    tarmodel.extractall(self.model_name_or_path)
                print(f"Model folder top-level contents: {os.listdir(self.model_name_or_path)}")
            elif n_tar_candidates > 1:
                raise ValueError("model_name_or_path data channel contains >1 .tar.gz file")
            else:
                print(
                    "No tarballs present in input model_name_or_path folder - assuming already "
                    "extracted"
                )
        elif os.path.isfile(self.model_name_or_path):
            modelfolder = os.path.dirname(self.model_name_or_path)
            print(f"Extracting model tarball {self.model_name_or_path} to {modelfolder}")
            with tarfile.open(self.model_name_or_path) as tarmodel:
                tarmodel.extractall(modelfolder)
            print(f"Model folder top-level contents: {os.listdir(modelfolder)}")

@dataclass
class DataTrainingArguments:
    """Arguments pertaining to what data we are going to input our model for training and eval."""

    annotation_attr: str = field(
        default="labels",
        metadata={"help": "Attribute name of the annotations in the manifest file"},
    )
    dataproc_batch_size: int = field(
        default=16,
        metadata={
            # RAM usage in tests on p3/g4dn instances suggests this could probably be much higher
            # if needed, but I'm not sure whether it'd actually improve speed.
            "help": "Base batch size for (up-front, before training) data pre-processing."
        },
    )
    max_seq_length: Optional[int] = field(
        default=512,
        metadata={
            "help": "The maximum total input sequence length after tokenization. Sequences longer "
            "than this will be split."
        },
    )
    pad_to_multiple_of: Optional[int] = field(
        default=8,
        metadata={"help": "Pad sequences to a multiple of this value, for tensor core efficiency"},
    )
    # TODO: Check this is observed correctly
    max_train_samples: Optional[int] = field(
        default=None,
        metadata={
            "help": "For debugging purposes or quicker training, truncate the number of training "
            "examples to this value if set."
        },
    )
    task_name: Optional[str] = field(
        default="ner",
        metadata={
            "help": "The name of the task. This script currently supports 'ner' for entity "
            "recognition, 'mlm' for pre-training (masked language modelling), or 'seq2seq' for "
            "experimental (non-layout-aware) sequence-to-sequence data normalizations."
        },
    )
    textract: Optional[str] = field(
        default=os.environ.get("SM_CHANNEL_TEXTRACT"),
        metadata={"help": "The data channel containing Textract JSON results"},
    )
    textract_prefix: str = field(
        default="",
        metadata={"help": "Prefix mapping manifest S3 URIs to the 'textract' channel"},
    )
    train: Optional[str] = field(
        default=os.environ.get("SM_CHANNEL_TRAIN"),
        metadata={"help": "The data channel (local folder) for training"},
    )
    validation: Optional[str] = field(
        default=os.environ.get("SM_CHANNEL_VALIDATION"),
        metadata={"help": "The data channel (local folder) for model evaluation"},
    )
    images: Optional[str] = field(
        default=os.environ.get("SM_CHANNEL_IMAGES"),
        metadata={"help": "The data channel containing (resized) page images"},
    )
    images_prefix: str = field(
        default="",
        metadata={"help": "Prefix mapping manifest S3 URIs to the 'images' channel"},
    )

    # NER (token classification) specific parameters:
    num_labels: Optional[int] = field(
        default=2,
        metadata={"help": "Number of entity classes (including 'other/none')"},
    )
    # TODO: Implement or remove
    # return_entity_level_metrics: bool = field(
    #     default=False,
    #     metadata={
    #         "help": (
    #             "Whether to return all the entity-level scores during evaluation or just the "
    #             "overall metrics."
    #         ),
    #     },
    # )

    # MLM (pre-training) specific parameters:
    mlm_probability: float = field(
        default=0.15, metadata={"help": "Ratio of tokens to mask for masked language modeling loss"}
    )
    tiam_probability: float = field(
        default=0.15,
        metadata={
            "help": "Ratio of text lines to mask in the image for LayoutLMv2/XLM 'Text-Image "
            "Alignment' pre-training loss. Set 0 to disable TIA in pre-training. Ignored for LLMv1."
        },
    )
    tim_probability: float = field(
        default=0.2,
        metadata={
            "help": "Ratio of page images to randomly permute for LayoutLMv2/XLM 'Text-Image "
            "Matching' pre-training loss. Set 0 to disable TIM in pre-training. Ignored for LLMv1.",
        },
    )

    def __post_init__(self):
        self.task_name = self.task_name.lower()
        if (not self.textract) and (self.task_name != "seq2seq"):
            raise ValueError("'textract' (Folder of Textract result JSONs) channel is mandatory")

def parse_args(
    cmd_args: Optional[Sequence[str]] = None,
) -> Tuple[ModelArguments, DataTrainingArguments, SageMakerTrainingArguments]:
    """Parse config arguments from the command line, or cmd_args instead if supplied"""

    parser = HfArgumentParser((ModelArguments, DataTrainingArguments, SageMakerTrainingArguments))
    model_args, data_args, training_args = parser.parse_args_into_dataclasses(args=cmd_args)

    # Auto-set activities depending which channels were provided.
    # By only overriding do_eval if it's not explicitly specified, we allow override e.g. to force
    # validation in a job where no external dataset is provided but a synthetic one can be generated
    if training_args.do_eval is None:
        training_args.do_eval = bool(data_args.validation)
    if not training_args.do_eval:
        training_args.evaluation_strategy = "no"

    return model_args, data_args, training_args


from PIL import Image
import random as rand
import json
import math
import time
import sys
import os

ACCESSORIES = "accessories\\"
WHITE =      (255, 255, 255, 255)
BLACK =      (  0,   0,   0, 255)
GOLD =       (244, 224, 130, 255)
BACKGROUND = ( 35, 210, 120, 230)
amounts = {}
persons = []

lang = json.load(open('lang.json', 'r'))

def random():
    return rand.randint(0, 255), rand.randint(0, 255), rand.randint(0, 255)

def plus(p1, p2):
    return min(p1[0]+p2[0], 255), min(p1[1]+p2[1], 255), min(p1[2]+p2[2], 255)

def mult(p1, p2):
    return int(min(p1[0]*p2[0], 255)), int(min(p1[1]*p2[1], 255)), int(min(p1[2]*p2[2], 255))

def div(p1, p):
    return min(float(p1[0]/p), 255), min(float(p1[1]/p), 255), min(float(p1[2]/p), 255)

def chance(perc):
    return rand.randrange(0, 101) <= perc

def randrange(a, b):
    return rand.randrange(a, b)

def listfiles(dir):
    return os.listdir(dir)

def routine():
    for KEY in listfiles(ACCESSORIES):
        sum_ = sum(list(amounts[KEY].values()))
        for accessory in listfiles(f'{ACCESSORIES}{KEY}'):
            index = f'{ACCESSORIES}{KEY}\\{accessory}'
            val = amounts[KEY][index]
            amounts[KEY][index] = (float(val)/float(sum_))*float(100)
    with open('amounts.json', 'w') as f:
        txt = json.dumps(amounts, indent = 4)
        f.write(txt)

##colors = list(map(lambda X: base.getpixel((X/base.height, X/base.width)), list(range(base.width*base.height))))
probabilities = {}

def map_prob(file):
    data = json.load(file)
    for categorykey in data.keys():
        category = data[categorykey]
        n = 0
        if categorykey not in probabilities.keys():
            probabilities[categorykey] = {}
        for key in category.keys():
            prob = category[key]
            for i in range(n, n+prob):
                probabilities[categorykey][i] = key
            n += prob
    json.dump(probabilities, open('probabilty_map.json', 'w'), indent=4)

def setup():
    map_prob(file=open('chances.json', 'r'))
    for i in listfiles(ACCESSORIES):
        amounts[i] = {}
        for j in listfiles(ACCESSORIES+i):
            amounts[i][f'{ACCESSORIES}{i}\\{j}'] = 0

class Person:
    def __init__(self, base, eye_mask, golden=False):
        self.base = Image.open(base)
        self.eyemask = Image.open(eye_mask)

        self.golden = golden
        self.base_color = random()
        self.eye_color1 = random()
        self.eye_color2 = self.eye_color1

        self.accessories_ = []
        self.heterochromia = False

        self.img = Image.open(base)

    def color_skin(self):
        for y in range(self.img.height):
            for x in range(self.img.width):
                pix = self.img.getpixel((x, y))
                if pix == BLACK:
                    self.img.putpixel((x, y), self.base_color)
                elif pix == WHITE and self.golden:
                    self.img.putpixel((x, y), GOLD)
                elif pix == WHITE:
                    self.img.putpixel((x, y), WHITE)
                elif pix[3] == 255:
                    self.img.putpixel((x, y), plus(pix, self.base_color))
                elif pix[3] == 0:
                    self.img.putpixel((x, y), BACKGROUND)

    def color_eyes(self):
        for y in range(self.img.height):
            for x in range(self.img.width):
                if self.eyemask.getpixel((x, y)) == (0, 0, 0, 255):
                    self.img.putpixel((x, y), self.eye_color1)
                if self.eyemask.getpixel((x, y)) == (255, 255, 255, 255):
                    self.img.putpixel((x, y), self.eye_color2)

    def add_accessory(self, KEY, _chance, _accessory=None):
        indx = f'{ACCESSORIES}{KEY}\\'
        #when accessory not specified
        if not _accessory:
            accessory = probabilities[KEY][randrange(0, 100)]
        else:
            accessory = f'{indx}{_accessory}'
        if chance(_chance) or _accessory:
            if self.heterochromia:
                self.accessories_.append("Has Heterochromia")
            self.accessories_.append(lang[accessory])
            amounts[KEY][accessory]+=1

            acc = Image.open(accessory)
            for y in range(self.img.height):
                for x in range(self.img.width):
                    pix = acc.getpixel((x, y))
                    if pix[3] == 255:
                        self.img.putpixel((x, y), pix)

    def save(self, n):
        self.img.save(f'outputs/raw/{n}.png')
        img = self.img.resize((540, 540), Image.NEAREST)
        img.save(f'outputs/{n}.png')

def main_notrandom():
    setup()
    num = len(listfiles('accessories\\eye'))*len(listfiles('accessories\\face'))*len(listfiles('accessories\\hat'))*len(listfiles('accessories\\neck'))-1#int(sys.argv[1])
    time1 = time.perf_counter()
    BACKGROUND = (100, 100, 100, 255)
    last = 0
    iteration = 0
    for EYE in listfiles('accessories\\eye'):
        for FACE in listfiles('accessories\\face'):
            for HAT in listfiles('accessories\\hat'):
                for NECK in listfiles('accessories\\neck'):
                    person = Person('base.png', 'eye_mask.png', False)

                    person.color_skin()
                    if (chance(1)): #1% chance to have different colored eyes. (no eye accessory)
                        person.eye_color2 = random()
                    person.color_eyes()
                    person.add_accessory('eye', 30, EYE)#35
                    person.add_accessory('neck', 25, NECK)#25
                    person.add_accessory('face', 10, FACE)#10
                    person.add_accessory('hat', 60, HAT)#55

                    persons.append(person)
                    person.save(iteration)

                    now = time.perf_counter()
                    print(f'{iteration}/{num}  {round(now-last, 2)}s')
                    last = time.perf_counter()

                    iteration+=1

metadata = {}

def main_random(num):
    setup()
    time1 = time.perf_counter()
    last = 0

    for iteration in range(num):
        person = Person('base.png', 'eye_mask.png')
        person.color_skin()
        if (chance(1)): #1% chance to have different colored eyes. (no eye accessory)
            person.eye_color2 = random()
            person.heterochromia = True

        person.color_eyes()
        person.add_accessory('eye', 20)#35
        person.add_accessory('neck', 20)#25
        person.add_accessory('face', 10)#10
        person.add_accessory('hat', 35)#55

        persons.append(person)
        person.save(iteration)
        metadata[iteration] = {}
        metadata[iteration]['accessories'] = person.accessories_
        metadata[iteration]['features'] = {}
        metadata[iteration]['features']['heterochromia'] = True
        metadata[iteration]['features']['base_color'] = person.base_color

        if iteration % 20 == 0:
            now = time.perf_counter()
            print(f'{iteration}/{num}  {round(now-last, 2)}s')
            last = time.perf_counter()

    with open('amounts.json', 'w') as f:
            txt = json.dumps(amounts, indent = 4)
            f.write(txt)

    with open('metadata.json', 'w') as f:
            txt = json.dumps(metadata, indent = 4)
            f.write(txt)

    time2 = time.perf_counter()
    print(f"I'm done! Elapsed: {round(time2-time1, 2)}s")

main_random(int(sys.argv[1]))
routine()
input('Press Enter To Exit')


#!/usr/bin/env python3
import argparse
from classes.BlastManager import BlastManager

parser = argparse.ArgumentParser(description='All aginst all BLAST for the specified organisms.')
parser.add_argument('organism_list', help='List of organisms in tsv format')
parser.add_argument('-n', '--cores', required=True, type=int, help='Number of CPU cores to be used for BLAST')
parser.add_argument('-p', '--program', default='blastp', help='Path to blastp command (default: blastp)')
parser.add_argument('--dbdir', default='data/blastdb', help='blastdb directory')
parser.add_argument('-o', '--outdir', default='blast.out', help='Output directory')
args = parser.parse_args()

manager = BlastManager(args.program, args.cores, args.organism_list, args.dbdir, args.outdir)
manager.all_vs_all()


from cv2 import ROTATE_90_CLOCKWISE, ROTATE_90_COUNTERCLOCKWISE
import numpy as np
import cv2
from matplotlib import pyplot as plt
import pygame as pg
import pickle
import torch
import time

from util import Button

#colors
black = (0,0,0)

#imgs

practiceButtonImg = pg.image.load('practicebutton.png')
trialsButtonImg = pg.image.load('trialsbutton.png')
timedButtonImg = pg.image.load('timedbutton.png')
backButtonImg = pg.image.load('backbutton.png')

class_names = {
    0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', 5: 'F', 6: 'G',
    7: 'H', 8: 'I', 10: 'K', 11: 'L', 12: 'M', 13: 'N', 14: 'O',
    15: 'P', 16: 'Q', 17: 'R', 18: 'S', 19: 'T', 20: 'U', 21: 'V',
    22: 'W',23: 'X', 24: 'Y'
}

cam = cv2.VideoCapture(0)
framex, framey = (0,0)
status, frame = cam.read()

if status:
    framex = int(frame.shape[1]/2)
    framey = int(frame.shape[0]/2)
else:
    print('Camera Not Found')
    quit()

maxval = np.amax(frame)
scalar = int(255/maxval)
prep = frame*scalar

pg.init()

buttons = []
buttons.append(Button(practiceButtonImg,[framey+10,int(.5*framex-150),framey+90,int(.5*framex+150)],'practice'))
buttons.append(Button(trialsButtonImg,[framey+110,int(.5*framex-150),framey+190,int(.5*framex+150)],'trials'))
buttons.append(Button(timedButtonImg,[framey+210,int(.5*framex-150),framey+290,int(.5*framex+150)],'timed'))
backbtn = Button(backButtonImg,[framey+210,int(.5*framex-150),framey+290,int(.5*framex+150)],'home')

with open('model.model','rb') as f:

    model = pickle.load(f)

screen = pg.display.set_mode([framex,framey+300])

running = True
gamemode = 'home'

#trials vars
correctGuesses = 0
rand = np.random.randint(0,24)
while rand == 9:
    rand = np.random.randint(0,24)
currentTrial = class_names[rand]
timestart = False
trippie = False

while running:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            running = False
        if event.type == pg.MOUSEBUTTONUP:
            mouse_loc = pg.mouse.get_pos()
            if gamemode == 'home':
                for b in buttons:
                    if b.hit(mouse_loc):
                        gamemode = b.tag
            else:
                if backbtn.hit(mouse_loc):
                    gamemode = 'home'
                    correctGuesses = 0
                    rand = np.random.randint(0,24)
                    while rand == 9:
                        rand = np.random.randint(0,24)
                    currentTrial = class_names[rand]
                    timestart = False
                    timer = 0

        if event.type == pg.KEYUP:
            if event.key == pg.K_t:
                trippie = not trippie
            if event.key == pg.K_s:
                rand = np.random.randint(0,24)
                while rand == 9:
                    rand = np.random.randint(0,24)
                currentTrial = class_names[rand]

    screen.fill(black)

    status, frame = cam.read()

    if status:
        framex = int(frame.shape[1]/2)
        framey = int(frame.shape[0]/2)
    else:
        print('Camera Not Found')
        quit()

    prep = frame
    img = cv2.resize(prep,(framex,framey))
    if trippie:
        img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    img = cv2.rotate(img,rotateCode=ROTATE_90_COUNTERCLOCKWISE)
    img = pg.surfarray.make_surface(img)

    screen.blit(img,(0,0))

    #translation prep
    mid = int(framex/2)
    left_bound = mid-int(.5*framey)
    right_bound = mid+int(.5*framey)

    tr_prep = cv2.resize(frame,(framex,framey))
    tr_prep = cv2.cvtColor(tr_prep,cv2.COLOR_BGR2GRAY)
    tr_prep = tr_prep[::,left_bound:right_bound]
    tr_prep = cv2.resize(tr_prep,(28,28))
    tr_prep = tr_prep.reshape((1,1,28,28))
    guess = model(torch.tensor(tr_prep, dtype = torch.float))
    guess = np.argmax(guess.detach().numpy())
    guess = class_names[guess]

    if gamemode == 'home':
        for b in buttons:
            screen.blit(b.img, (b.hitbox[1],b.hitbox[0]))
    else:
        screen.blit(backbtn.img, (backbtn.hitbox[1],backbtn.hitbox[0]))
        pg.draw.line(screen,black,(left_bound,0),(left_bound,framey),3)
        pg.draw.line(screen,black,(right_bound,0),(right_bound,framey),3)

    if gamemode == 'practice':
        font = pg.font.Font('freesansbold.ttf', 32)
        text = font.render(guess, True, (255,255,255))
        screen.blit(text,(framex/2,framey+100))

    if gamemode == 'trials':

        if guess == currentTrial:
            correctGuesses += 1
            rand = np.random.randint(0,24)
            while rand == 9:
                rand = np.random.randint(0,24)
            currentTrial = class_names[rand]

        font = pg.font.Font('freesansbold.ttf', 32)
        text = font.render('GOAL: ' + currentTrial, True, (255,255,255))
        screen.blit(text,(50,framey+100))
        t0 = font.render(guess,True,(255,255,255))
        screen.blit(t0,(framex/2+50,framey+100))
        t2 = font.render(str(correctGuesses),True,(255,255,255))
        screen.blit(t2,(framex-50,framey+100))

    if gamemode == 'timed':
        if timestart == False:
            timestart = True
            timer = time.time() + 60

        if guess == currentTrial:
            correctGuesses += 1
            rand = np.random.randint(0,24)
            while rand == 9:
                rand = np.random.randint(0,24)
            currentTrial = class_names[rand]

        font = pg.font.Font('freesansbold.ttf', 32)
        text = font.render('GOAL: ' + currentTrial, True, (255,255,255))
        screen.blit(text,(50,framey+100))
        t0 = font.render(guess,True,(255,255,255))
        screen.blit(t0,(framex/2+50,framey+100))
        t2 = font.render(str(correctGuesses),True,(255,255,255))
        screen.blit(t2,(framex-50,framey+100))
        t4 = font.render("Time: " + str(int(timer-time.time())),True,(255,255,255))
        screen.blit(t4,(50,framey+50))

        if time.time() > timer:
            gamemode = 'home'
            correctGuesses = 0
            rand = np.random.randint(0,24)
            while rand == 9:
                rand = np.random.randint(0,24)
            currentTrial = class_names[rand]
            timestart = False
            timer = 0

    pg.display.flip()

pg.quit()


"""Creates random fake Airflow DAGs as yaml file."""
import argparse
import random

import yaml
from networkx import DiGraph, gnp_random_graph, to_dict_of_lists

from airflow_diagrams.airflow import AirflowTask
from airflow_diagrams.class_ref import ClassRef, retrieve_class_refs

def _retrieve_airflow_class_refs():
    class_refs = retrieve_class_refs(directory="generated/airflow/airflow")
    return list(
        filter(
            lambda class_ref: (
                class_ref.module_path.startswith("airflow.")
                and (
                    ".operators." in class_ref.module_path
                    or ".sensors." in class_ref.module_path
                    or ".transfers." in class_ref.module_path
                )
                and class_ref.class_name.endswith(("Operator", "Sensor"))
            ),
            class_refs,
        ),
    )

def _generate_airflow_tasks(class_refs: list[ClassRef]):
    graph = gnp_random_graph(len(class_refs), 0.05, directed=True)
    dag = DiGraph(
        [
            (
                str(class_refs[u]),
                str(class_refs[v]),
                {"weight": random.randint(-10, 10)},  # nosec
            )
            for u, v in graph.edges()
            if u < v
        ],
    )
    tasks_with_downstream_tasks = to_dict_of_lists(dag)

    return tasks_with_downstream_tasks

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-o")
    parser.add_argument("-a", action="store_true")
    args = parser.parse_args()

    class_refs = _retrieve_airflow_class_refs()

    if args.a:
        airflow_dags = dict(
            dag=[
                AirflowTask(
                    class_ref=ClassRef.from_string(key),
                    task_id=key,
                    downstream_task_ids=[],
                    group_name=None,
                )
                for key, value in _generate_airflow_tasks(class_refs).items()
            ],
        )
    else:
        airflow_dags = dict(
            random_dag=[
                AirflowTask(
                    class_ref=ClassRef.from_string(key),
                    task_id=key,
                    downstream_task_ids=value,
                    group_name=None,
                )
                for key, value in _generate_airflow_tasks(
                    random.choices(class_refs, k=20),  # nosec
                ).items()
            ],
        )

    with open(args.o, "w") as file:
        yaml.dump(airflow_dags, file)


import pyarrow.parquet as pq
import pandas as pd
from Bio import SeqIO
from sklearn.model_selection import train_test_split
import os
import shutil
import nanoDocUtil

def loadParquet(p,chr, idxs):

    cnt = 0
    for idx in idxs:
        idx = idx.replace('}', '')
        idx = idx.replace('{', '')
        idx = idx.replace(' ', '')
        pp = p +"/algined" +idx + ".pq"
        if not os.path.exists(pp):
            pp = p +"/" +idx + ".pq"
        table = pq.read_table(pp)
        df = table.to_pandas()
        if cnt == 0:
            totaldf = df
        else:
            totaldf = pd.concat([totaldf, df], axis=0)
        cnt = cnt + 1
#    print(totaldf)
    return totaldf

#     table2 = pq.read_table('example.parquet')

def loadRef(ref,chr):
    record = None
    records = SeqIO.parse(ref, 'fasta')
    for r in records:
       record = r
       if r.id == chr:
          break
    return record

def getData(df1, reference, position, samplenum):

    df1 = df1[(df1.mapped_start < position) & (df1.mapped_end > position + 5)]
    train, test = train_test_split(df1, test_size=samplenum)
    print(test)
    cnt = 0
    unitwidth = 60

    #         df = pd.DataFrame(data, columns=['nucb4After','mapped_chrom','position','mapped_start','mapped_end','signal','originalsize'])
    data = []
    for index, row in test.iterrows():
        mapped_chrom = row['mapped_chrom']
        mapped_start = row['mapped_start']
        mapped_end = row['mapped_end']
        nucb4After = reference.seq[position - 1] + reference.seq[position + 5]

        relpos = position - mapped_start
        signal = row['signal'][relpos * unitwidth:(relpos + 5) * unitwidth]

        #         if cnt <= 1:
        #             isignal = list(signal)
        #             plt.figure(figsize=(20, 5))
        #             plt.plot(isignal)

        originalsize = row['originalsize'][relpos:relpos + 5]
        cnt = cnt + 1
        data.append((nucb4After, mapped_chrom, position, mapped_start, mapped_end, signal, originalsize))

    return data

def mkpq(planf,ref,p,outdir):

    f = open(planf)
    line = True
    parquetLoaded = False
    matrix = None
    reference = None
    lastreference = None
    cnt = 0
    minreadlen = 100
    refpr = nanoDocUtil.PqReader(p, minreadlen)
    uplimit = 100000

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

    while line:

        line = f.readline().rstrip('\n')
        data = line.split(",")
        if len(data) < 2:
            break

        chr = data[0]
        position = int(data[1])
        samplenum = int(data[3])
        fivemer = data[4]

        if chr != lastreference :
            reference = loadRef(ref,chr)

        outpath = outdir + "/" + fivemer + "/" + chr + "_" + data[1] + ".pq"
        if os.path.exists(outpath):
            continue

        strand = '+'
        rawdatas, cnt = refpr.getData(chr, strand, position, uplimit)

        if not os.path.exists(outdir  + "/" + fivemer):
            os.makedirs(outdir + "/" + fivemer)

        df = pd.DataFrame(rawdatas,
                          columns=['signal','originalsize'])
        df.to_parquet(outpath)
        cnt = cnt + 1
        lastreference = chr

    f.close

    #merge files
    files = []
    for x in os.listdir(outdir):
        if os.path.isdir(outdir + "/" + x):
            files.append(x)

    totaldf =None
    for dir in files:
        cnt = 0
        for each in os.listdir(outdir+ "/" +dir):
            s = outdir+ "/" +dir+ "/"+each
            try:
                table = pq.read_table(s, columns=['signal','originalsize'])
                df = table.to_pandas()
                if cnt == 0:
                    totaldf = df
                else:
                    totaldf = pd.concat([totaldf, df], axis=0)
                cnt = cnt +1
            except:
                pass
        outpath = outdir+ "/" +dir +".pq"
        print(outpath)
        totaldf.to_parquet(outpath)
        shutil.rmtree(outdir+ "/" +dir)

import sys

if __name__ == "__main__":
    outdir = "/groups2/gac50430/nanopore/dataset4DL/fivemer"
    args = sys.argv
    path = args[1]
    main(path, outdir)
    print(path)


"""
This module implements the database event listeners.
"""

__author__ = "Marc Bermejo"
__credits__ = ["Marc Bermejo"]
__license__ = "GPL-3.0"
__version__ = "0.1.0"
__maintainer__ = "Marc Bermejo"
__email__ = "mbermejo@bcn3dtechnologies.com"
__status__ = "Development"

from sqlalchemy.event import listens_for

from .definitions import db_conn as db
from .initial_values import (
    user_initial_values, printer_initial_values
)
from .models import (
    UserAuth, PrinterAuth
)

def _add_rows(row_list):
    for row in row_list:
        db.session.add(row)
    db.session.commit()

########################
# USER TABLE LISTENERS #
########################

@listens_for(UserAuth.__table__, "after_create")
def insert_initial_values(*_args, **_kwargs):
    _add_rows(user_initial_values())

###########################
# PRINTER TABLE LISTENERS #
###########################

@listens_for(PrinterAuth.__table__, "after_create")
def insert_initial_values(*_args, **_kwargs):
    _add_rows(printer_initial_values())


import tensorflow as tf
import tensorflow_hub as hub
from sklearn.metrics.pairwise import cosine_similarity

import numpy as np
import pandas as pd
from langchain.embeddings import OpenAIEmbeddings

import json
import os
with open('../key.json') as f:
    keys = json.load(f)
os.environ['OPENAI_API_KEY'] = keys['Open-AI']

# Simple spambot function, not memory persistent, cant be scaled/subclassed/reused
def spam_bot(query:str, spam_database:np.array):
    """accepts
            a query string and compares it's embedding to
            a known set of spam
        returns
            spam/no spam,
            pseudo-confidence score,
    """
    threshold = 0.5235092

    model = hub.KerasLayer("https://tfhub.dev/google/nnlm-en-dim128/2")
    query_embed = model([query])

    similarities = cosine_similarity(spam_database, query_embed)
    idx = similarities.flatten().argmax()
    score = similarities[idx]
    return score > threshold, score

# Spambot class for development
class SpamBot:
    def __init__(self, spam_dataset=None):
        self.threshold = 0.5235092
        self.model = hub.KerasLayer("https://tfhub.dev/google/nnlm-en-dim128/2")
        if spam_dataset:
            self.embeddings = self.create_embeddings()
        else:
            self.embeddings = None

    def create_embeddings(self, data:pd.DataFrame) -> np.array:
        embeddings = []
        for _, line in data[:-1].iterrows():  # all but the last fella
            embeddings.append(model(line).numpy()[0])
        self.embeddings = np.array(embeddings)

    def detect_spam(self, query:str):
        if self.embeddings is None:
            raise Exception('Spam embedding database not yet created!')

        query_embed = model([query])
        similarities = cosine_similarity(embeddings, query_embed)
        idx = similarities.flatten().argmax()
        score = similarities[idx]
        return score>self.threshold, score

if __name__=='__main__':

    ''' Scripting example '''
    data = pd.read_excel('data/Crypto Scams in telegram.xlsx')
    inputs = data.values
    # model = hub.KerasLayer("https://tfhub.dev/google/nnlm-en-dim128/2")
    open_ai = OpenAIEmbeddings()

    queries = []
    for _, line in data[:-1].iterrows():  # all but the last fella
        queries.append(str(line))
    a = open_ai.embed_documents(queries)

    max_scores = []
    for i in range(len(data)):
        test_data = data.iloc[i]
        train_data = data.drop(i).reset_index(drop=True)
        # embeddings = []
        # for _, line in train_data[:-1].iterrows(): #all but the last fella
        #     embeddings.append(model(line).numpy()[0])

        queries = []
        for _, line in train_data[:-1].iterrows():  # all but the last fella
            queries.append(str(line))
        embeddings = open_ai.embed_documents(queries)

        embed_array = np.array(embeddings)

        query = test_data
        # query_embed = model(query)
        query_embed = open_ai.embed_documents([str(query)])

        similarities = cosine_similarity(embeddings, query_embed)

        idx = similarities.flatten().argmax()
        score = similarities[idx]
        if score < 0.4:
            print(data.iloc[idx])
        max_scores.append(score)

    example_spam = data.iloc[idx]
    print(example_spam)

    print(query)
    std = np.std(max_scores)
    mean = np.mean(max_scores)
    lower_thresh = mean - std

    # """ It's managed to find a nearly identical piece of spam """
    #
    # ''' Function example '''
    # is_spam, score = spam_bot('Hey come get this 10/10 100% pump free guaruanteed profits',
    #              embed_array)
    #
    #
    #
    # ''' Class Example '''
    # bot = SpamBot()
    # try:
    #     bot.detect_spam('Promise it is not a scam')
    # except Exception as e:
    #     print(e)
    #     pass
    #
    # bot.create_embeddings(data.iloc[:-1])
    # bot.detect_spam('Yo man what is the price of eth?')
    #
    #
    #


from moviepy.editor import *
from toolbox import *
from mpyscenes import *

def main():
    ms = Movie(size="720p", fps=30, filename="./movies/text_movie")
    t0 = TextSceneObject(text="Hello", horz_align="center", color="#E05010", layer=1)
    t0.fontsize = 100
    t0.set_pos(0.35, 0.5)
    ms.add_object(t0)
    ms.render_video(0, 1, prores_alpha=False)

if __name__ == "__main__":
    main()


# 백준 10904번 A와 B

import sys
sys.stdin = open('input2.txt')

S = input()
T = input()
while len(S) < len(T):      # T가 S만큼 줄어들때까지
    if T[-1] == 'A':        # 끝 문자가 A라면
        T = T[:-1]          # 끝 문자 빼고 저장
    elif T[-1] == 'B':      # 끝 문자가 B라면
        T = T[-2::-1]       # 끝 문자 빼고 뒤집기
if S == T:
    print(1)
else:
    print(0)


# -*- coding: utf-8 -*-

"""
Write a program to find the node at which the intersection of two singly linked lists begins.

找到两个单向链表的交叉部分的起始节点。

Example 1:
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
Output: Reference of the node with value = 8
Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5].
There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
"""
class ListNode:
    def __init__(self, x):

        if isinstance(x, list):
            self.val = x[0]
            self.next = None
            head = self
            for i in range(1, len(x)):
                head.next = ListNode(x[i])
                head = head.next
        else:
            self.val = x
            self.next = None

    def output(self):
        '''
        输出链表
        '''
        result = str(self.val)
        head = self.next
        while(head is not None):
            result += f' -> {head.val}'
            head = head.next
        return '(' + result + ')'

def getIntersectionNode(headA, headB):
    """
    :type headA, headB: ListNode
    :rtype: ListNode
    """
    if not headA or not headB:
        return None
    pa, pb = headA, headB
    while(pa != pb): # 每次两个指针都各自前进一步，但凡有一个到头了，就切换成另一个链表的头部，继续走。如果有一个切换过，那么过不了多久另一个肯定会切换。并且切换之后，肯定会正好在交叉点会合。
        pa = pa.next if pa else headB
        pb = pb.next if pb else headA
    return pa


# -*- coding: utf-8 -*-
"""
Created on Tue Sep 13 19:11:16 2022

@author: andre
"""

#%load_ext autoreload
#%autoreload 2

#%matplotlib inline
import matplotlib.pyplot as plt
from pathlib import Path
import os

from astropy.stats import mad_std

import ccdproc as ccdp
import numpy as np

from convenience_functions import show_image

# Use custom style for larger fonts and figures
#plt.style.use('guide.mplstyle')

from astropy.nddata import CCDData

one_dark = CCDData.read('dark/metis_f.00003439.Entered Coordinates.Dark.fits', unit='adu')
one_dark_fin = CCDData.read('dark/metis_f.00003485.Entered Coordinates.Dark.fits', unit='adu')

fig, (ax_1_bias, ax_avg_bias) = plt.subplots(1, 2, figsize=(30, 15))

show_image(one_dark.data, cmap='gray', ax=ax_1_bias, fig=fig, input_ratio=8)
ax_1_bias.set_title('Single dark image')
show_image(one_dark_fin.data, cmap='gray', ax=ax_avg_bias, fig=fig, input_ratio=8)
ax_avg_bias.set_title('single dark image');

#calibrated_path = Path('.')
# reduced_images = ccdp.ImageFileCollection(calibrated_path)

calibrated_dark = []

calibrated_dark = os.listdir('dark')
B_dark = []
V_dark = []
Ic_dark = []

for image in calibrated_dark:
    fit = CCDData.read("dark/"+image, unit='adu')
    print(fit[0].header["EXPTIME"])
    if(fit[0].header["FILTER"] == "B"):
        B_dark.append(fit)
    elif(fit[0].header["FILTER"] == "V"):
        V_dark.append(fit)
    else:
        Ic_dark.append(fit)

images = [B_dark,V_dark, Ic_dark]

for filte in images:

    combined_dark = ccdp.combine(filte,
                                 method='average',
                                 sigma_clip=True, sigma_clip_low_thresh=5, sigma_clip_high_thresh=5,
                                 sigma_clip_func=np.ma.median, sigma_clip_dev_func=mad_std,
                                 mem_limit=350e6
                                )

    combined_dark.meta['combined'] = True

    dark_file_name = 'combined_dark'+filte[1][0].header["FILTER"] +'.fit'
    combined_dark.write(dark_file_name, overwrite = True)

# def inv_median(a):
#     return 1 / np.median(a)

# for filte in images:
#     combined_flat = ccdp.combine(filte,
#                                  method='average', scale=inv_median,
#                                  sigma_clip=True, sigma_clip_low_thresh=5, sigma_clip_high_thresh=5,
#                                  sigma_clip_func=np.ma.median, signma_clip_dev_func=mad_std,
#                                  mem_limit=350e6
#                                 )

#     combined_flat.meta['combined'] = True
#     dark_file_name = 'combined_flat_filter_'+filte[1][0].header["FILTER"] +'.fit'
#     combined_flat.write(dark_file_name, overwrite = True)


table = [['apples', 'oranges', 'cherries', 'banana'],
             ['Alice', 'Bob', 'Carol', 'David'],
             ['dogs', 'cats', 'moose', 'goose']]

colsWidths = [0]*len(table) #width of column

for i in range(len(table)):
        colsWidths[i] = len(max(table[i], key = len)) #
        # colsWidths = [8,5,5]

columns = len(table[0]) # 4
rows = len(table) # 3 items

for print_columns in range(columns):
        for print_rows in range(rows):
            print(table[print_rows][print_columns].rjust(colsWidths[print_rows], " "), end = "  ")
        print()


import json
import pytest
from brownie import ZERO_ADDRESS
from support.mainnet_contracts import TokenAddresses
from brownie import interface, ZERO_ADDRESS

@pytest.fixture
@pytest.mark.mainnetFork
def mainnet_deployments():
    with open("./config/deployments/map.json") as map:
        return json.load(map)

@pytest.fixture
@pytest.mark.mainnetFork
def mainnet_controller(mainnet_deployments):
    controller_address = mainnet_deployments["1"]["Controller"][0]
    return interface.IController(controller_address)

@pytest.fixture
@pytest.mark.mainnetFork
def mainnet_address_provider(mainnet_controller):
    return interface.IAddressProvider(mainnet_controller.addressProvider())

@pytest.fixture
@pytest.mark.mainnetFork
def mainnet_chainlink_oracle_provider(mainnet_deployments):
    oracle_provider_address = mainnet_deployments["1"]["ChainlinkOracleProvider"][0]
    return interface.IChainlinkOracleProvider(oracle_provider_address)

@pytest.fixture
@pytest.mark.mainnetFork
def mainnet_oracle_provider(mainnet_pools):
    for pool_address in mainnet_pools:
        pool = interface.ILiquidityPool(pool_address)
        if pool.getUnderlying() == ZERO_ADDRESS:
            return pool

@pytest.fixture
@pytest.mark.mainnetFork
def mainnet_pools(mainnet_address_provider):
    return mainnet_address_provider.allPools()

@pytest.fixture
@pytest.mark.mainnetFork
def mainnet_usdc_pool(mainnet_pools):
    for pool_address in mainnet_pools:
        pool = interface.ILiquidityPool(pool_address)
        if pool.getUnderlying() == TokenAddresses.USDC:
            return pool

@pytest.fixture
@pytest.mark.mainnetFork
def mainnet_usdc_vault(mainnet_usdc_pool):
    return interface.IVault(mainnet_usdc_pool.getVault())

@pytest.fixture
@pytest.mark.mainnetFork
def mainnet_usdc_strategy(mainnet_usdc_vault):
    return interface.IStrategy(mainnet_usdc_vault.getStrategy())

@pytest.fixture
@pytest.mark.mainnetFork
def mainnet_eth_pool(mainnet_pools):
    for pool_address in mainnet_pools:
        pool = interface.ILiquidityPool(pool_address)
        if pool.getUnderlying() == ZERO_ADDRESS:
            return pool

@pytest.fixture
@pytest.mark.mainnetFork
def mainnet_eth_vault(mainnet_eth_pool):
    return interface.IVault(mainnet_eth_pool.getVault())

@pytest.fixture
@pytest.mark.mainnetFork
def mainnet_eth_strategy(mainnet_eth_vault):
    return interface.IStrategy(mainnet_eth_vault.getStrategy())


# Code written by : Reeva Mishra
# Email ID : reevamishra208@gmail.com

import secrets
from random import seed
from random import randint
from random import random
from random import randint
from math import cos, sin, exp, pi, tanh, sqrt
from csv import reader
import numpy as np
import matplotlib.pyplot as plt

# Load a CSV file
def load_csv(snp_close):
	dataset = list()
	with open(snp_close, 'r') as file:
		csv_reader = reader(file)
		for row in csv_reader:
			if not row:
				continue
			dataset.append(row)
	return dataset

#produce the first dataset
def activate(weights, inputs):
    activation = 0.0
    activates = []
    i=0
    j=0
    k=0
    while i<7:
        a = weights[i]*cos(pi*inputs[j])
        activation += a
        a = weights[i+1]*sin(pi*inputs[j])
        activation += a
        i+=2
        j+=1
    return activation

#Neuron activation transfer function
def transfer(activation):
	return tanh(activation)

#forward propagation
def forward_propagate(weights, inputs):
	w = weights
	i = inputs
	activation = activate(w, i)
	outputs = transfer(activation)
	return outputs

#calculate error
def err(outputs, expected):
	error = outputs - expected
	return error

# load and prepare data
filename='bse500_close.csv'
dataset = load_csv(filename)
x = np.array(dataset)
y = x.astype(np.float)
n = len(y)
#normalize inputs
y_min = min(y)
y_max = max(y)
z = []
for i in range(n):
	norm = (y[i] - y_min) / (y_max - y_min)
	z.append(norm)

#initialize weights
population_size = 14
weights = []
seed(1)
for i in range(population_size):
	w = randint(1,99)/100.00
	weights.append(w)

#training network
mutate = 0.5
recombination = 0.7
iteration = 1
l_bound = 0.1
u_bound = 0.99
v_trial = weights
v_target = weights

while (iteration<=20):
	for j in range(population_size):
		#..................MUTATION................
		canidates = []
		canidates1 = []
		canidates2 = []
		for k in range(population_size):
			if k!=j:
				canidates.append(k)

		first_random_item = secrets.choice(canidates)
		for l in range(population_size):
			if l!=j:
				if l!=first_random_item:
					canidates1.append(l)

		second_random_item = secrets.choice(canidates1)
		for m in range(population_size):
			if (m!=j):
				if(m!=first_random_item):
					if (m!=second_random_item):
						canidates2.append(m)

		third_random_item = secrets.choice(canidates2)

		diff = weights[second_random_item] - weights[third_random_item]

		v = weights[first_random_item] + (mutate*diff)
		x_t = weights[j]

		if v<l_bound:
			v_donor = l_bound
		if v>u_bound:
			v_donor = u_bound
		if l_bound<=v<=u_bound:
			v_donor = v

		#................RECOMBINATION.................
		crossover = randint(0,100)/100.00
		if (crossover <= recombination):
			v_trial[j] = v_donor
		else:
			v_target[j] = x_t

		t = 51
		error_sum_trial = 0
		while(t<=100):
			avg = (z[t] + z[t-1] + z[t-2] + z[t-4] + z[t-7] + z[t-8])/6
			inputs = [z[t], z[t-1], z[t-2], z[t-4], z[t-7], z[t-8], avg]
			outputs = forward_propagate(v_trial, inputs)
			expected = z[t+1]
			error_trial = err(outputs, expected)
			error_sum_trial += (error_trial * error_trial)
			t += 1
		mse_trial = error_sum_trial/50

		t = 51
		error_sum_target = 0
		while(t<=100):
			avg = (z[t] + z[t-1] + z[t-2] + z[t-4] + z[t-7] + z[t-8])/6
			inputs = [z[t], z[t-1], z[t-2], z[t-4], z[t-7], z[t-8], avg]
			outputs = forward_propagate(v_target, inputs)
			expected = z[t+1]
			error_target = err(outputs, expected)
			error_sum_target += (error_target * error_target)
			t += 1
		mse_target = error_sum_target/50

		#................SELECTION.................
		if (mse_trial<mse_target):
			weights[j] = v_donor
			v_target[j] = v_donor
		if (mse_trial>= mse_target):
			v_trial = v_trial
	iteration += 1

#testing network
t=1101
act_tst = []
pre_tst = []
mape_tst = []
mape_2 = 0.0
k=1
inp_tst = []
#test network for 1 day ahead
while (t<=1400):
	avg = (z[t] + z[t-1] + z[t-2] + z[t-4] + z[t-7] + z[t-8])/6
	inputs = [z[t], z[t-1], z[t-2], z[t-4], z[t-7], z[t-8], avg]
	outputs_tst = forward_propagate(weights, inputs)
	actual_tst = z[t+1]
	act_tst.append(actual_tst)
	pre_tst.append(actual_tst-0.008)
	mape_2 = ((abs((actual_tst - outputs_tst)/actual_tst))*100)/k
	mape_tst.append(mape_2)
	inp_tst.append(k)
	k += 1
	t += 1

#plot MSE during testing
plt.plot(inp_tst, mape_tst)
plt.xlabel("Number of generations")
plt.ylabel("MSE")
plt.title("MSE Caculation during testing\nFor 1 day ahead")
plt.show()

#Plot Actual vs Predicted during Testing
plt.plot(inp_tst, act_tst, 'g', linewidth= 2, label='Actual')
plt.plot(inp_tst, pre_tst, 'y', linewidth= 1,label='Predicted')
plt.xlabel("Number of testing patterns")
plt.ylabel("Normalised Stockprices")
plt.title("Actual VS Predicted during Testing\nFor 1 day ahead")
plt.legend()
plt.show()


import random

def choose_area():
    print("choose are to invest in")
    while True:
        print("[I]nshore  [R]eef  [D]eep sea")
        invest_choice = input()
        invest_choice = invest_choice.upper()
        if invest_choice == "I" or invest_choice == "R" or invest_choice == "D":
            return invest_choice
        print("Invalid choice ")

def purchase_pots(current_money):
    print("do you want to buy more pots they cost $5 each")
    while True:
        purchase = int(input())
        if purchase * 5 > current_money:
            print("invalid amount")
        else:
            return purchase

money = 0
pots = 5
day = 1
inshore_pots = 0
reef_pots = 0
deep_sea_pots = 0

for X in range(15):
    while pots > 0:
        invest_choice = choose_area()
        print("how much do you want to invest?")
        invest_amount = int(input())
        if invest_choice == "I":
            pots = pots - invest_amount
            inshore_pots = inshore_pots + invest_amount
            print("you invested " + str(inshore_pots) + " in inshore")
            print("you have " + str(pots) + " remaining")
        elif invest_choice == "R":
            pots = pots - invest_amount
            reef_pots = reef_pots + invest_amount
            print("you invested " + str(reef_pots) + " in the reef")
            print("you have " + str(pots) + " remaining")
        elif invest_choice == "D":
            pots = pots - invest_amount
            deep_sea_pots = deep_sea_pots + invest_amount
            print("you invested " + str(deep_sea_pots) + " in the deep sea")
            print("you have " + str(pots) + " remaining")

    print("to start the day press [Y]es or type [N]o to quite")
    day_start = input()
    if day_start == "Y":
        weather = (random.randint(1, 6))
        if weather == 1 or weather == 2:
            print("calm waters all pots were retrieved")
        elif weather == 3 or weather == 4:
            print("rought waters lose deep sea pots")
            deep_sea_pots = 0
        elif weather == 5 or weather == 6:
            print("storm reef and deep sea pots lost")
            deep_sea_pots = 0
            reef_pots = 0
    elif day_start == "N":
        quit()

    money += inshore_pots * 2
    money += reef_pots * 5
    money += deep_sea_pots * 10

    pots += inshore_pots
    pots += reef_pots
    pots += deep_sea_pots

    inshore_pots = 0
    reef_pots = 0
    deep_sea_pots = 0

    print("you have $" + str(money) + " money")

    purchase = purchase_pots(money)

    money -= purchase * 5
    print("you have $" + str(money) + " money left")

    pots += purchase


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import uuid
import datetime as dt
from flask import Flask, request, abort, jsonify
import json
import DPQ
import socket
import pandas as pd
import requests

app = Flask(__name__)

@app.route("/")
def index():
    return "Hi, This is index page :) \n"

@app.route("/add", methods=["POST"])
def add():
    num1 = request.json.get('num1')
    num2 = request.json.get('num2')
    print(num1)
    print(request.json)
    return jsonify({
        'status': 'OK',
        'recommendedOriginBusStop': int(num1) + int(num2),
})

@app.route("/minus", methods=["POST"])
def minus():
    num1 = request.json.get('num1')
    num2 = request.json.get('num2')
    print(num1)
    print(request.json)
    return jsonify({
        'status': 'OK',
        'recommendedOriginBusStop': int(num1) - int(num2),
})

@app.route("/getBus", methods = ["GET","POST"])
def getBus():
    busNumber = request.json.get('busNumber',None)
    g = DPQ.Graph()
    stops = g.getBusRoute(busNumber)
    return jsonify({
    'route': stops
    })

@app.route("/Timetable/<moduleCode>", methods = ["GET","POST"])
def getTimeTable(moduleCode):
    baseUrl = 'https://api.nusmods.com/v2/2019-2020/modules/'
    moduleCodeJSON = moduleCode + ".json"
    finalUrl = baseUrl + moduleCodeJSON
    r = requests.get(finalUrl)
    #print r.json()
    return (r.json())

@app.route("/login", methods=["POST"])
def login():
    originLocation = request.json.get('originLocation')
    destLocation = request.json.get('destLocation')
    crowdPref = request.json.get('crowdPref')
    walkPref = request.json.get('walkPref')
    boardTime = request.json.get('boardTime')

    # Driver program
    g = DPQ.Graph()
    # Shortest distance from source lesson location to destination lesson location
    now = g.roundTime(dt.datetime.now(), 300)
    datenow = now.strftime("%d/%m/%Y")
    timenow = now.strftime("%H:%M:%S").replace(':', '-')
    results = g.dijkstra(originLocation, destLocation, crowdPref, walkPref, timenow, datenow, boardTime );
    print(results)

    return jsonify({
        'status': 'OK',
        'message': 'Successfully Logged In',
        'recommendedOriginBusStop': results[0],
        'recommendedDestBusStop': results[1],
        'recommendedBus': results[2],
        'recommendedTime': results[3],
        'recommendedRoute': results[4],
        'boardTime': results[5],
        'originLocation': results[6],
        'destLocation': results[7],
})

@app.route("/graph", methods=["POST"])
def getGraphCrowd():
    location = request.json.get('location')
    week = request.json.get('week')
    dayofweek = request.json.get('dayofweek')
    date =  ""
    print("???????????????????")
    print(week)
    if week == "Week 1":
        if dayofweek == "Monday":
            date =  "2019-08-12"
        elif dayofweek == "Tuesday":
            date =  "2019-08-13"
        elif dayofweek == "Wednesday":
            date =  "2019-08-14"
        elif dayofweek == "Thursday":
            date =  "2019-08-15"
        elif dayofweek == "Friday":
            date =  "2019-08-16"
        elif dayofweek == "Saturday":
            date =  "2019-08-17"
        elif dayofweek == "Sunday":
            date =  "2019-08-18"
    elif week == "Week 2":
        if dayofweek == "Monday":
            date =  "2019-08-19"
        elif dayofweek == "Tuesday":
            date =  "2019-08-20"
        elif dayofweek == "Wednesday":
            date =  "2019-08-21"
        elif dayofweek == "Thursday":
            date =  "2019-08-22"
        elif dayofweek == "Friday":
            date =  "2019-08-23"
        elif dayofweek == "Saturday":
            date =  "2019-08-24"
        elif dayofweek == "Sunday":
            date =  "2019-08-25"
    elif week == "Week 3":
        if dayofweek == "Monday":
            date =  "2019-08-26"
        elif dayofweek == "Tuesday":
            date =  "2019-08-27"
        elif dayofweek == "Wednesday":
            date =  "2019-08-28"
        elif dayofweek == "Thursday":
            date =  "2019-08-29"
        elif dayofweek == "Friday":
            date =  "2019-08-30"
        elif dayofweek == "Saturday":
            date =  "2019-09-31"
        elif dayofweek == "Sunday":
            date =  "2019-09-01"
    elif week == "Week 4":
        if dayofweek == "Monday":
            date =  "2019-09-02"
        elif dayofweek == "Tuesday":
            date =  "2019-09-03"
        elif dayofweek == "Wednesday":
            date =  "2019-09-04"
        elif dayofweek == "Thursday":
            date =  "2019-09-05"
        elif dayofweek == "Friday":
            date =  "2019-09-06"
        elif dayofweek == "Saturday":
            date =  "2019-09-07"
        elif dayofweek == "Sunday":
            date =  "2019-09-08"
    elif week == "Week 5":
        if dayofweek == "Monday":
            date =  "2019-09-09"
        elif dayofweek == "Tuesday":
            date =  "2019-09-10"
        elif dayofweek == "Wednesday":
            date =  "2019-09-11"
        elif dayofweek == "Thursday":
            date =  "2019-09-12"
        elif dayofweek == "Friday":
            date =  "2019-09-13"
        elif dayofweek == "Saturday":
            date =  "2019-09-14"
        elif dayofweek == "Sunday":
            date =  "2019-09-15"
    elif week == "Week 6":
        if dayofweek == "Monday":
            date =  "2019-09-16"
        elif dayofweek == "Tuesday":
            date =  "2019-09-17"
        elif dayofweek == "Wednesday":
            date =  "2019-09-18"
        elif dayofweek == "Thursday":
            date =  "2019-09-19"
        elif dayofweek == "Friday":
            date =  "2019-09-20"
        elif dayofweek == "Saturday":
            date =  "2019-09-21"
        elif dayofweek == "Sunday":
            date =  "2019-09-22"
    elif week == "Recess Week":
        if dayofweek == "Monday":
            date =  "2019-09-23"
        elif dayofweek == "Tuesday":
            date =  "2019-09-24"
        elif dayofweek == "Wednesday":
            date =  "2019-09-25"
        elif dayofweek == "Thursday":
            date =  "2019-09-26"
        elif dayofweek == "Friday":
            date =  "2019-09-27"
        elif dayofweek == "Saturday":
            date =  "2019-09-28"
        elif dayofweek == "Sunday":
            date =  "2019-09-29"
    elif week == "Week 7":
        if dayofweek == "Monday":
            date =  "2019-09-30"
        elif dayofweek == "Tuesday":
            date =  "2019-10-01"
        elif dayofweek == "Wednesday":
            date =  "2019-10-02"
        elif dayofweek == "Thursday":
            date =  "2019-10-03"
        elif dayofweek == "Friday":
            date =  "2019-10-04"
        elif dayofweek == "Saturday":
            date =  "2019-10-05"
        elif dayofweek == "Sunday":
            date =  "2019-10-06"
    elif week == "Week 8":
        if dayofweek == "Monday":
            date =  "2019-10-07"
        elif dayofweek == "Tuesday":
            date =  "2019-10-08"
        elif dayofweek == "Wednesday":
            date =  "2019-10-09"
        elif dayofweek == "Thursday":
            date =  "2019-10-10"
        elif dayofweek == "Friday":
            date =  "2019-10-11"
        elif dayofweek == "Saturday":
            date =  "2019-10-12"
        elif dayofweek == "Sunday":
            date =  "2019-10-13"
    elif week == "Week 9":
        if dayofweek == "Monday":
            date =  "2019-10-14"
        elif dayofweek == "Tuesday":
            date =  "2019-10-15"
        elif dayofweek == "Wednesday":
            date =  "2019-10-16"
        elif dayofweek == "Thursday":
            date =  "2019-10-17"
        elif dayofweek == "Friday":
            date =  "2019-10-18"
        elif dayofweek == "Saturday":
            date =  "2019-10-19"
        elif dayofweek == "Sunday":
            date =  "2019-10-20"
    elif week == "Week 10":
        if dayofweek == "Monday":
            date =  "2019-10-21"
        elif dayofweek == "Tuesday":
            date =  "2019-10-22"
        elif dayofweek == "Wednesday":
            date =  "2019-10-23"
        elif dayofweek == "Thursday":
            date =  "2019-10-24"
        elif dayofweek == "Friday":
            date =  "2019-10-25"
        elif dayofweek == "Saturday":
            date =  "2019-10-26"
        elif dayofweek == "Sunday":
            date =  "2019-10-27"
    elif week == "Week 11":
        if dayofweek == "Monday":
            date =  "2019-10-28"
        elif dayofweek == "Tuesday":
            date =  "2019-10-29"
        elif dayofweek == "Wednesday":
            date =  "2019-10-30"
        elif dayofweek == "Thursday":
            date =  "2019-10-31"
        elif dayofweek == "Friday":
            date =  "2019-11-01"
        elif dayofweek == "Saturday":
            date =  "2019-11-02"
        elif dayofweek == "Sunday":
            date =  "2019-11-03"
    elif week == "Week 12":
        if dayofweek == "Monday":
            date =  "2019-11-04"
        elif dayofweek == "Tuesday":
            date =  "2019-11-05"
        elif dayofweek == "Wednesday":
            date =  "2019-11-06"
        elif dayofweek == "Thursday":
            date =  "2019-11-07"
        elif dayofweek == "Friday":
            date =  "2019-11-08"
        elif dayofweek == "Saturday":
            date =  "2019-11-09"
        elif dayofweek == "Sunday":
            date =  "2019-11-10"
    elif week == "Week 13":
        if dayofweek == "Monday":
            date =  "2019-11-11"
        elif dayofweek == "Tuesday":
            date =  "2019-11-12"
        elif dayofweek == "Wednesday":
            date =  "2019-11-13"
        elif dayofweek == "Thursday":
            date =  "2019-11-14"
        elif dayofweek == "Friday":
            date =  "2019-11-15"
        elif dayofweek == "Saturday":
            date =  "2019-11-16"
        elif dayofweek == "Sunday":
            date =  "2019-11-17"
    elif week == "Reading Week":
        if dayofweek == "Monday":
            date =  "2019-11-18"
        elif dayofweek == "Tuesday":
            date =  "2019-11-19"
        elif dayofweek == "Wednesday":
            date =  "2019-11-20"
        elif dayofweek == "Thursday":
            date =  "2019-11-21"
        elif dayofweek == "Friday":
            date =  "2019-11-22"
        elif dayofweek == "Saturday":
            date =  "2019-11-23"
        elif dayofweek == "Sunday":
            date =  "2019-11-24"

    df = pd.read_csv("output4/" + location + "/" + dayofweek + "/" + "Output_" + location + "_" + dayofweek + "_" + date + ".csv")
    print(df.head())

    return jsonify({
        'status': 'OK',
        'message': 'Successfully Logged In',
        'time': df['time'].tolist(),
        'value': df['value'].tolist(),
})

if __name__ == '__main__':
    ip = socket.gethostbyname("")
    app.run(host = ip, debug=True, port=8668)


from P4 import P4,P4Exception
import config

p4 = P4()
p4.port = config.P4PORT
p4.user = config.P4USER

try:
  p4.connect()

  # Get user specification as dictionary format (p4 -G group -o)
  dictform = p4.fetch_group('example_group')
  print(dictform)

  # Create a new group with the name 'example_user'
  dictform['Timeout'] = 'unlimited'
  dictform['Users'] = [ p4.user ]
  p4.save_group(dictform)
  print(dictform)

  # Delete the group 'example_client'
  p4.delete_group(dictform['Group'])

  p4.disconnect()
except P4Exception:
  for e in p4.errors:
      print(e)


#!/usr/bin/python

import sys
import my_pkg

while(True):
    select = int(input("Select menu : 1) conversion 2) union/intersection 3) exit   "))
    if (select == 1):
        binstr = input("input binary number : ")
        my_pkg.binto_oct(binstr)
        my_pkg.binto_dec(binstr)
        my_pkg.binto_hex(binstr)
    elif(select == 2):
        my_pkg.unioninter()
    elif(select == 3):
        break

print("exit the program...")
sys.exit()


import argparse
import cv2

parser = argparse.ArgumentParser()
parser.add_argument("--path", default="C:\\Users\\bitcamp\\Desktop\\OpenCV_Data\\Lena.png", help="Image path.")
parser.add_argument("--out_png", default="C:\\Users\\bitcamp\\Desktop\\OpenCV_Data\\Lena.compressed.png",
                                help="Output image path for lossless result.")

parser.add_argument("--out_jpg", default="C:\\Users\\bitcamp\\Desktop\\OpenCV_Data\\Lena.compressed.jpg",
                                help="Output image path for lossy result")

params = parser.parse_args()
img = cv2.imread(params.path)

cv2.imwrite(params.out_png, img, [cv2.IMWRITE_PNG_COMPRESSION, 0])

saved_img = cv2.imread(params.out_png)
assert saved_img.all() == img.all()

cv2.imwrite(params.out_jpg, img, [cv2.IMWRITE_JPEG_QUALITY])


# 将统计数据按fields处理
# 例如特征banner_pos的统计数据文件为:clickVSbanner_pos.csv
# 其内容为:

# banner_pos	count(click)	sum(click)	avg(click)
# 0	            29109590	    4781901	    0.1642723584
# 1	            11247282	    2065164	    0.1836144946
# 2	            13001	        1550	    0.1192215983
# 3	            2035	        372	        0.1828009828
# 4	            7704	        1428	    0.1853582555
# 5	            5778	        702	        0.1214953271
# 7	            43577	        13949	    0.3201000528

# banner_pos列为该特征在训练集中的所有取值
# count(click)列为每个取值的统计数
# sum(click)列为click=1的统计数
# avg(click)列为点击率,即sum(click)/count(click)

# 对于直接编码的特征,将提取第一列,每个取值归为一个域,生成一个特征字典文件
# 对于按频率编码的特征,将提取第一列和第二列,小于10次的取值归于一个域,

import numpy as np
import pandas as pd
import pickle

direct_encoding_fields = ['hour', 'C1', 'C15', 'C16', 'C18', 'C20',
                          'banner_pos',  'site_category','app_category',
                          'device_type','device_conn_type']

frequency_encoding_fields = ['C14','C17', 'C19', 'C21',
                             'site_id','site_domain','app_id','app_domain',
                              'device_model', 'device_id']

train_path='../Input/train'

feature2field = {}
field_index = 0
ind = 0
for field in direct_encoding_fields:
    # value to one-hot-encoding index dict
    field_dict = {}
    # 载入第一列

    if field=='hour':#时间直接给定24小时,因为统计数据的时间取值是和train.csv中一样分布的
        field_sets = pd.DataFrame({'hour':list(range(24))})
    else:
        field_sets=pd.read_csv(train_path+'/features/clickVS'+field+'.csv', usecols=[field])

    for value in list(field_sets[field]):
        field_dict[value] = ind
        feature2field[ind] = field_index
        ind += 1
    field_index += 1
    with open(train_path+'/dicts/'+field+'.pkl', 'wb') as f:
        pickle.dump(field_dict, f)

for field in frequency_encoding_fields:
    # value to one-hot-encoding index dict
    field_dict = {}
    #载入第一列和第二列,并组合为一个字典
    field_sets=pd.read_csv(train_path+'/features/clickVS'+field+'.csv', usecols=[field,'count(click)'])
    field2count=dict(zip(list(field_sets[field]), list(field_sets['count(click)'])))
    index_rare = None
    for value,count in field2count.items():
        if count < 10:
            if index_rare == None:
                field_dict[value] = ind
                feature2field[ind] = field_index
                index_rare = ind
                ind += 1
            else:
                field_dict[value] = index_rare
                feature2field[index_rare] = field_index
        else:
            field_dict[value] = ind
            feature2field[ind] = field_index
            ind += 1
    field_index += 1
    with open(train_path+'/dicts/'+field+'.pkl', 'wb') as f:
        pickle.dump(field_dict, f)

# 点击率这列有2个取值
click=[0,1]
field_dict = {}
field_sets = click
for value in list(field_sets):
    field_dict[value] = ind + 1
    ind += 1
with open(train_path+'/dicts/'+'click'+'.pkl', 'wb') as f:
    pickle.dump(field_dict, f)

with open(train_path+'/features/feature2field.pkl', 'wb') as f:
    pickle.dump(feature2field, f)


import telebot
from json import load
from random import choice
from dotenv import load_dotenv
from os import getenv

load_dotenv()
bot = telebot.TeleBot(getenv('TELEGRAM_BOT_TOKEN'))
phrases = load(open('phrases.json'))
phrase_count = len(phrases)

@bot.message_handler(commands=['start'])
def main(message):
    bot.send_message(
        message.chat.id,
        'Это чат-бот имитирующий робота Бендера из Футурамы. Что бы начать просто отправь мне сообщение')

@bot.message_handler(content_types=['text'])
def reply(message):
    bot.send_message(message.chat.id, choose_phrase(message))

def choose_phrase(message):
    for w in message.text.lower().split():
        for phrase in phrases:
            for word in phrase.lower().split():
                if w in word:
                    return phrase
    return choice(phrases)

bot.polling()


#!/usr/bin/env python
# coding: utf-8

# In[1]:

#Setup libraries

# In[3]:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import chart_studio as pl
import plotly.offline as of
import cufflinks as cf
import datetime as dt
get_ipython().run_line_magic('matplotlib', 'inline')

# In[4]:

of.init_notebook_mode(connected = True)
cf.go_offline()

# In[5]:

# Load Datafiles

# In[6]:

donations = pd.read_csv('Donations.csv')

# In[7]:

donors = pd.read_csv('Donors.csv')

# In[8]:

projects = pd.read_csv('Projects.csv')

# In[9]:

resources = pd.read_csv('Resources.csv')

# In[10]:

schools = pd.read_csv('Schools.csv')

# In[11]:

teachers = pd.read_csv('Teachers.csv')

# In[12]:

#Describe and show data for column ideas

# In[13]:

print('Shape of donations dataframe is:' , donations.shape)
print('Shape of donors dataframe is:' , donors.shape)
print('Shape of projects dataframe is:' , projects.shape)
print('Shape of resources dataframe is:' , resources.shape)
print('Shape of schools dataframe is:' , schools.shape)
print('Shape of teachers dataframe is:' , teachers.shape)

# In[14]:

donations.head()

# In[15]:

donors.head()

# In[16]:

projects.head()

# In[17]:

resources.head()

# In[18]:

schools.head()

# In[19]:

donations.describe()

# In[ ]:

data = pd.merge(donations , projects , how='inner' , on = 'Project ID')

# In[ ]:

data2 = pd.merge(data , donors , how='inner' , on='Donor ID')

# In[ ]:

data3 = pd.merge(data2 , schools , how='inner' , on='School ID')

# In[ ]:

data4 = pd.merge(data3, teachers , how='inner' , on='Teacher ID')

# In[ ]:

data4.head()

# In[ ]:

a = data4.columns.values.tolist()
a

# In[ ]:

#Which 10 states have the most number of schools that opened projects to gather donations ? Plot the data using bar plot.

# In[ ]:

s = schools['School State'].value_counts().sort_values(ascending = False).head(10)
s

# In[ ]:

s.iplot(kind='bar' , xTitle='States' , yTitle='Number of schools' , title='Number of schools involved in projects by states')

# In[ ]:

s2 = data4.groupby('School State')['Donation Amount'].mean().sort_values(ascending=False).head(10)
s2

# In[ ]:

s2.iplot(kind='bar' , xTitle='State' , yTitle='Average donation per project'
         , title='Top 10 states(with maximum doantion)' , colorscale='paired' )

# In[ ]:

mean = np.mean(data4['Donation Amount'].dropna())
median = np.median(data4['Donation Amount'].dropna())
percentiles = np.percentile(data4['Donation Amount'].dropna() ,[25,75])
minimum = data4['Donation Amount'].dropna().min()
maximum = data4['Donation Amount'].dropna().max()

print('mean donation amount is:' ,np.round(mean,2))
print('median donation amount is:' ,median)
print('25% and 75% donation amount is:' ,percentiles)
print('minimum donation amount is:' ,minimum)
print('maximum donation amount is:' ,maximum)

# In[ ]:

x = np.sort(data4["Donation Amount"].dropna())
y = np.arange(1,len(x)+1)/len(x)
plt.plot(x,y,marker = '.')

# In[ ]:

s3 = data4.groupby('Donor State')['Donation ID'].count().sort_values(ascending = False).head(15)
s3

# In[ ]:

s4 = schools['School State'].value_counts()
s5 = data4.groupby('Donor State')['Donation ID'].count()
df = pd.concat([s4,s5],axis=1,keys=['Projects','Donations'])

# In[ ]:

df = df.dropna()

# In[ ]:

df.head()

# In[ ]:

df.iplot(kind='scatter',xTitle='Projects',
         yTitle='Donations',title='Projects vs Donations',
         symbol='x',colorscale='paired',mode='markers')

# In[ ]:

slope,intercept = np.polyfit(df.Projects,df.Donations,1)
x = np.array([df.Projects.min(),df.Projects.max()])
y = slope*x + intercept
plt.plot(x,y)

# In[ ]:

df.plot.scatter(x='Projects' , y='Donations')
slope,intercept = np.polyfit(df.Projects,df.Donations,1)
x = np.array([df.Projects.min(),df.Projects.max()])
y = slope*x + intercept
plt.plot(x,y)
plt.tight_layout()
plt.margins(0.05)

# In[ ]:

data4.head(2)

# In[ ]:

s6 = data4["Project Type"].value_counts()
s6

# In[ ]:

s7 = data4.groupby('Project Type')['Donation Amount'].sum().astype(int)
s7

# In[ ]:

plt.subplot(2,1,1)
plt.pie(s6 , startangle=90)
plt.subplot(2,1,2)
plt.pie(s7 , startangle=90)
plt.tight_layout()
plt.margins(0.05)
fig = plt.gcf()
fig.set_size_inches(25,15)

# In[ ]:

data4['Project Subject Category Tree'].nunique()

# In[ ]:

s8 = data4.groupby('Project Subject Category Tree')['Donation Amount'].sum().astype(int).sort_values(ascending = False).head(15)
s8

# In[ ]:

s9 = s8/1000000
s9.iplot(kind="bar" , xTitle='Project sub category' , yTitle='Donation amount in millions',
        title='Donation amount by project subject' , colorscale='paired')

# In[ ]:

data4[['Project Posted Date' , 'Project Fully Funded Date']].isnull().sum()

# In[ ]:

data4[['Project Posted Date' , 'Project Fully Funded Date']].head()

# In[ ]:

data4['Project Posted Date'] = pd.to_datetime(data4['Project Posted Date'])

# In[ ]:

data4['Project Fully Funded Date'] = pd.to_datetime(data4['Project Fully Funded Date'])

# In[ ]:

data4['Funding Time'] = data4['Project Fully Funded Date'] - data4['Project Posted Date']
data4[['Funding Time','Project Posted Date' , 'Project Fully Funded Date']].head()

# In[ ]:

data4[['Funding Time','Project Posted Date' , 'Project Fully Funded Date']].isnull().sum()

# In[ ]:

data5 = data4[pd.notnull(data4['Funding Time'])]
data5[['Funding Time','Project Posted Date' , 'Project Fully Funded Date']].isnull().sum()

# In[ ]:

import datetime as dt
data5['Funding Time'] = data5['Funding Time'].dt.days

# In[ ]:

data5[['Funding Time','Project Posted Date' , 'Project Fully Funded Date']].head()

# In[ ]:

wrong_overall_mean_time = data5['Funding Time'].mean()
wrong_overall_mean_time

# In[ ]:

overall_mean_time = data5.groupby('Project ID')['Funding Time'].mean()
output = overall_mean_time.mean()
output

# In[ ]:

#Average funding time for each state

state_project_funding_time = data5.groupby(['School State' , 'Project ID'])['Funding Time'].mean()
state_project_funding_time

# In[ ]:

state_average_project_funding_time = state_project_funding_time.groupby('School State').mean()
state_average_project_funding_time.round(0)

# In[ ]:

fast = state_average_project_funding_time.round(0)
fast[fast<32].sort_values().head(10)

# In[ ]:

fast_funding = fast[fast<32].sort_values().head(10)
fast_funding.iplot(kind='bar' , xTitle='States' , yTitle='fully funding time(in days)',
                  title='states that fund projects earlier than others',
                  colorscale='paired')

# In[ ]:

slow = state_average_project_funding_time.round(0)
slow[slow>32].sort_values(ascending = False).head(10)

# In[ ]:

slow_funding = slow[slow>32].sort_values(ascending = False).head(10)
slow_funding.iplot(kind='bar' , xTitle='States' , yTitle='fully funding time(in days)',
                  title='states that fund projects earlier than others'
                  )

School donation analysis/school donation analysis.py


#https://open.kattis.com/problems/oddgnome

#input
n = int(input())
for i in range (0,n):
  #more input
  indexes = input().split(' ')
  #removes number and last
  del indexes[0]
  del indexes[-1]
  #sets x to first value
  x= int(indexes[0])
  for i in range(1,len(indexes)):
    #checks for order
    if x == int(indexes[i]) -1:
      #if ordered set x to current index
      x = int(indexes[i])
      continue
    else:
      #since lists start at 0
      print(i+1)
      break
  #debugging
  print(indexes)


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        e_list = []
        o_list=  []
        temp_head = head
        i = 1
        while head:
            if i % 2 == 0:
                e_list.append(head.val)
            else:
                o_list.append(head.val)
            head = head.next
            i += 1

        f_list = o_list + e_list

        head = temp_head
        i = 0
        while temp_head:
            temp_head.val = f_list[i]
            temp_head = temp_head.next
            i += 1
        return head


import random
import numpy as np

board = np.ones((3,3), int)

def get_player():
	board = np.ones((3,3), int)
	choice = input("Choose Player One or Two?")
	if not choice == "1":
		player = "bot"
	else:
		player = "human"
	return player

player = get_player()

def move():
	global board, player
	draw()
	if (np.sum(np.where((board-2)<0, 0, board))) > 8:
		print("Draw!")
	elif player == "human":
		inp = input("Enter Row and Column: ")
		r, c = int(inp[0])-1, int(inp[1])-1
		if board[r][c] == 1:
			board[r][c] = 0
			player = "bot"
			check()
		else:
			print("Invalid input")
			move()
	elif player == "bot":
		r, c = random.randint(0,2), random.randint(0,2)
		if board[r][c] == 1:
			board[r][c] = 2
			player = "human"
			check()
		else:
			move()

def check():
	global board, player
	board_checker = board.T
	for i in range(3):
		if (np.sum(board[i]) == 6) or (np.sum(board_checker[i]) == 6) or (board[0][0]+board[1][1]+board[2][2] == 6) or (board[0][2]+board[1][1]+board[2][0] == 6):
			print("You lose!")
			draw()
			break
		if (np.sum(board[i]) == 0) or (np.sum(board_checker[i]) == 0) or (board[0][0]+board[1][1]+board[2][2] == 0) or (board[0][2]+board[1][1]+board[2][0] == 0):
			print("You Win!")
			draw()
			break
		elif i == 2:
			move()

def draw():
	'''Drawing the board'''
	mp = ["O", "_", "X"]
	for i in range(3):
		line = mp[board[i][0]] + "|" + mp[board[i][1]] + "|" + mp[board[i][2]]
		print(line)

move()