import re
import os
import time
from typing import NoReturn
import psutil
from pypresence import Presence

from linux_discord_rpc.procs import find_pid
from linux_discord_rpc.config import read_games_conf
from linux_discord_rpc.optgen import gen_options

# the main daemon interacting with discord and monitoring
# game processes
def daemon() -> NoReturn:
    games = read_games_conf()
    statefile = os.path.join(os.environ.get('XDG_RUNTIME_DIR', '/tmp'), 'linux-discord-rpc-state')
    try:
        open(statefile, 'w').close()
    except:
        print(f'statefile could not be created at {statefile}')
        exit(1)

    active = None
    rpc = None
    options = None
    firstrun = True
    restorable = False
    while True:
        if firstrun:
            firstrun = False
        else:
            time.sleep(10)

        # wait until discord actually runs
        # and set restorable
        if not find_pid('Discord'):
            print('Discord not running')
            if rpc and options:
                rpc.close()
                restorable = True
            while not find_pid('Discord'):
                time.sleep(1)

        new = None

        # first check for manual state
        with open(statefile) as f:
            state = re.sub(r'\n', r'', f.readline())
            for game in games:
                if game == state:
                    new = game

        # then auto state
        if not new:
            for game in games:
                if not games[game]['exe'] == '__noauto__':
                    if find_pid(games[game]['exe']):
                        new = game

        # running and not active yet
        if new and new != active:
            print(f'Changing status to {new}')
            if rpc:
                rpc.close()
            try:
                rpc_new = Presence(games[new]['appid'])
                # commit new -> active
                active = new
                rpc = rpc_new
                options = gen_options(games[new])
            except:
                print('Updating status failed. Keeping previous.')
                continue
            finally:
                try:
                    rpc.connect()
                    rpc.update(**options)
                except:
                    print('Couldn\'t connect to Discord. Retrying.')
                    continue

        # running and already active (restore status)
        elif new and new == active:
            if rpc and options and restorable:
                print(f'Restoring status {active}')
                try:
                    rpc.connect()
                    rpc.update(**options)
                    restorable = False
                except:
                    print('Couldn\'t connect to Discord. Retrying.')
                    continue

        # not running (clear)
        elif not new and active:
            print(f'Clearing status.')
            if rpc:
                rpc.close()
            active = rpc = options = None

        # not running and already cleared
        elif not new and not active:
            pass

if __name__ == '__main__':
    daemon()


import os

from collections import Counter
from datasets import load_dataset

HUGGINGFACE_TOKEN = os.environ.get("HUGGINGFACE_TOKEN")

oscar = load_dataset(
    "oscar-corpus/OSCAR-2201",
    "en",
    use_auth_token=HUGGINGFACE_TOKEN,
    num_proc=128,
    ignore_verifications=True,
)
# oscar.save_to_disk("/home/piktus_huggingface_co/lumi/oscar/")

oscar_ids = oscar["train"]["id"]
print("Number of Oscar IDs", len(oscar_ids))

unique_ids = Counter(oscar_ids)
print(unique_ids.most_common(10))


import re
from functools import wraps
import boto3

def create_s3_client(url, access_key, secret_key):
    return boto3.client('s3',
        aws_access_key_id=access_key,
        aws_secret_access_key=secret_key,
        endpoint_url = url
        )

def _get_status_code(resp):
    return resp['ResponseMetadata']['HTTPStatusCode']

def create_bucket(client, bucket_name):
    try:
        return client.create_bucket(Bucket=bucket_name)
    except Exception as e:
        print(e)
        return None

def clear_bucket(client, bucket_name):
    objects = list_objects(client, bucket_name)
    for obj in objects:
        key = obj['Key']
        delete_object(client, bucket_name, key)
    return objects

def delete_bucket(client, bucket_name):
    try:
        return client.delete_bucket(Bucket=bucket_name)
    except Exception as e:
        print(e)
        return None

def list_buckets(client):
    resp = client.list_buckets()
    return resp.get('Buckets', [])

def list_objects(client, bucket_name):
    resp = client.list_objects(Bucket=bucket_name)
    return resp.get('Contents', [])

def put_object(client, bucket_name, key, binary_data):
    return client.put_object(Bucket=bucket_name, Key=key, Body=binary_data)

def get_object(client, bucket_name, key):
    return client.get_object(Bucket=bucket_name, Key=key)

def get_object_body(client, bucket_name, key):
    '''
    s3://bucket_name/key => bytes
    '''
    return client.get_object(Bucket=bucket_name, Key=key)['Body'].read()

def delete_object(client, bucket_name, key):
    '''
    s3://bucket_name/key => bool
    '''
    resp = client.delete_object(Bucket=bucket_name, Key=key)
    return resp and _get_status_code(resp) < 300

def upload_file(client, bucket_name, filename):
    key = re.sub(r'\.', '', filename)
    if key[0] == '/':
        key = key[1:]
    client.upload_file(Bucket=bucket_name, Key=key, Filename=filename)
    return key

class S3Client:
    def __init__(self, url, access_key, secret_key):
        def wrap(f):
            @wraps(f)
            def wrapped(*args, **kwargs):
                return f(self.client, *args, **kwargs)
            return wrapped
        functions = [
                create_bucket,
                clear_bucket,
                delete_bucket,
                list_buckets,
                list_objects,
                put_object,
                get_object,
                get_object_body,
                delete_object,
                upload_file
                ]
        self.__dict__ = {}
        for f in functions:
            self.__dict__[f.__name__] = wrap(f)
        self.url = url
        self.access_key = access_key
        self.secret_key = secret_key
        self.client = create_s3_client(url, access_key, secret_key)

def test_s3_server(url, access_key, secret_key):
    s3 = S3Client(url, access_key, secret_key)
    bucket = 'abc'
    print(s3.create_bucket(bucket))
    print(s3.put_object(bucket, 'aaa.dat', bytes([1,2])))
    print(s3.list_buckets())
    print(s3.list_objects(bucket))
    print(s3.get_object_body(bucket, 'aaa.dat'))
    key = s3.upload_file(bucket, './s3_utils.py')
    print(s3.delete_object(bucket, key))
    print(s3.delete_bucket(bucket))
    print(s3.clear_bucket(bucket))
    print(s3.delete_bucket(bucket))

def test_minio():
    '''
    docker pull minio/minio
    docker run -p 9000:9000 minio/minio server /data
    '''
    test_s3_server('http://localhost:9000', 'minioadmin', 'minioadmin')

def test_cloudserver():
    '''
    git clone https://github.com/scality/S3.git
    cd S3
    yarn install --frozen-lock-file
    yarn start

    or

    docker pull zenko/cloudserver
    docker run -p 8000:8000 -e S3BACKEND=mem zenko/cloudserver
    '''
    test_s3_server('http://localhost:8000', 'accessKey1', 'verySecretKey1')

if __name__ == '__main__':
    test_minio()
    test_cloudserver()


FREQUENCIA = 'Frequencia'
PROBABILIDADE_UM = 'Probabilidade 1'
PROBABILIDADE_DOIS = 'Probabilidade 2'
FREQUENCIA_UM =   'Frequencia 1'
FREQUENCIA_DOIS = 'Frequencia 2'
SEMENTE = 'Semente'
MAXIMO = 'Maximo'
MEDIA = 'Media'
DESVIO_PADRAO = 'Desvio padrao'
MINIMO = 'Minimo'
IGUAL = 'Igual'
DIFERENTE_ALEATORIA = 'Diferente Aleatoria'
DIFERENTE_DETERMINISTICA = 'Diferente Deterministica'
TEMPO_UM = 'Tempo um'
TEMPO_DOIS = 'Tempo dois'
TEMPO = 'Tempo'
NORMAL = 'Normal'
EXPONENCIAL = 'Exponencial'
TRIANGULAR = 'Triangular'
ALEATORIA = 'Aleatoria'
LIMITE_FILA_UM = 'Limite da fila do servidor 1'
LIMITE_FILA_DOIS = 'Limite da fila do servidor 2'
LOCALIZACAO_RELATORIO = 'Nome ou caminho completo do relatorio'
TEMPO_DE_DURACAO = 'Tempo de duracao da simulacao'


from functools import reduce

import tensorflow as tf
from secml.array import CArray
from secml.ml import CClassifierDNN

class CClassifierKeras(CClassifierDNN):
    """CClassifierKeras, wrapper for Keras models.

        Parameters
        ----------
        model : model dtype of the specific backend
            The model to wrap.
        input_shape : tuple or None, optional
            Shape of the input for the DNN, it will
            be used for reshaping the input data to
            the expected shape.
        preprocess : CPreprocess or str or None, optional
            Preprocessing module.
        pretrained : bool, optional
            Whether or not the model is pretrained. If the
            model is pretrained, the user won't need to call
            `fit` after loading the model. Default False.
        pretrained_classes : None or CArray, optional
            List of classes labels if the model is pretrained. If
            set to None, the class labels for the pretrained model should
            be inferred at the moment of initialization of the model
            and set to CArray.arange(n_classes). Default None.
        softmax_outputs : bool, optional
            Whether or not to add a softmax layer after the
            logits. Default False.
        n_jobs : int, optional
            Number of parallel workers to use for training the classifier.
            Cannot be higher than processor's number of cores. Default is 1.

        Attributes
        ----------
        class_type : 'keras-clf'

        """
    __class_type = 'keras-clf'

    def __init__(self, model, input_shape=None, preprocess=None,
                 pretrained=False, pretrained_classes=None,
                 softmax_outputs=False, n_jobs=1):
        super(CClassifierKeras, self).__init__(
            model=model, input_shape=input_shape, preprocess=preprocess,
            pretrained=pretrained, pretrained_classes=pretrained_classes,
            softmax_outputs=softmax_outputs, n_jobs=n_jobs)

        self._cached_in = None
        self._cached_out = None
        self._tape = tf.GradientTape(persistent=True)

        if self._pretrained is True:
            self._trained = True
            if self._pretrained_classes is not None:
                self._classes = self._pretrained_classes
            else:
                self._classes = CArray.arange(
                    reduce(lambda x, y: x * y, self._model.output_shape[1:]))
            self._n_features = reduce(lambda x, y: x * y,
                                      self._model.input_shape[1:])

    @property
    def layers(self):
        """Returns list of tuples containing the layers of the model.
        Each tuple is structured as (layer_name, layer)."""

        # excluding input layer
        return [(layer.name, layer) for layer in self._model.layers[1:]]

    @property
    def layer_shapes(self):
        """Returns a dictionary containing the shapes of the output
        of each layer of the model."""

        # excluding input layer
        return {layer.name: layer.output_shape[1:]
                for layer in self._model.layers[1:]}

    @staticmethod
    def _to_tensor(x, shape=None):
        """Convert input CArray to backend-supported tensor."""
        tensor = tf.convert_to_tensor(x.tondarray(), dtype=tf.float32)
        if shape is not None:
            tensor = tf.reshape(tensor, shape)
        return tensor

    @staticmethod
    def _from_tensor(x):
        """Convert input backend-supported tensor to CArray"""
        return CArray(x.numpy())

    def _forward(self, x):
        """Forward pass on input x.
        Returns the output of the layer set in _out_layer.
        If _out_layer is None, the last layer output is returned,
        after applying softmax if softmax_outputs is True.

        Parameters
        ----------
        x : CArray
            preprocessed array, ready to be transformed by the current module.

        Returns
        -------
        CArray
            Transformed input data.
        """
        x = self._to_tensor(x, shape=(-1, *self._input_shape))

        if self._cached_x is None:
            # grad is not required
            if self._tape._recording:
                self._tape._pop_tape()
                self._tape._tape = None
            self._cached_in = None
            self._cached_out = None
            out = self._model(x)
        else:
            if self._tape._recording:
                self._tape.reset()
            else:
                self._tape.__enter__()
            self._tape.watch(x)
            out = self._model(x)
            self._cached_in = x
            self._cached_out = [out[:, i] for i in self.classes]
            self._tape.stop_recording()

        return self._from_tensor(out)

    def _backward(self, w):
        """Returns the gradient of the DNN - considering the output layer set
        in _out_layer - wrt data.

        Parameters
        ----------
        w : CArray
            Weights that are pre-multiplied to the gradient
            of the module, as in standard reverse-mode autodiff.

        Returns
        -------
        gradient : CArray
            Accumulated gradient of the module wrt input data.
        """
        if w is None:
            w = CArray.ones(shape=(self.n_classes,))

        if not self._cached_x.is_vector_like:
            raise ValueError("Tested only on vector-like arrays")
        grad = CArray.zeros(shape=self._cached_x.shape)

        # loop only over non-zero elements in w, to save computations
        for c in w.nnz_indices[1]:
            grad += w[c] * self._from_tensor(
                self._tape.gradient(self._cached_out[c], self._cached_in))
        self._tape._pop_tape()
        self._tape._tape = None
        return grad

    def save_model(self, filename):
        """
        Stores the model and optimization parameters.

        Parameters
        ----------
        filename : str
            path of the file for storing the model

        """
        pass

    def load_model(self, filename):
        """
        Restores the model and optimization parameters.
        Notes: the model class should be
        defined before loading the params.

        Parameters
        ----------
        filename : str
            path where to find the stored model

        """
        pass

    def _fit(self, x, y):
        """Private method that trains the One-Vs-All classifier.
        Must be reimplemented by subclasses.

        Parameters
        ----------
        x : CArray
            Array to be used for training with shape (n_samples, n_features).
        y : CArray or None, optional
            Array of shape (n_samples,) containing the class labels.
            Can be None if not required by the algorithm.

        Returns
        -------
        CClassifier
            Trained classifier.

        """
        pass


import numpy as np
import matplotlib.pyplot as plt
from scipy.special import gamma
dimentions = np.arange(51)
radiuses = np.arange(1,2.05,0.05)
def volume(r, n):
    return np.pi**(n/2)*r**n/gamma(1+n/2)
volumes = []
for ind, n in enumerate(dimentions):
    volumes.append([])
    for r in radiuses:
        volumes[ind].append(volume(r,n))
from matplotlib.ticker import LinearLocator
X = dimentions
xlen = len(X)
Y = radiuses
ylen = len(Y)
X, Y = np.meshgrid(X, Y)
Z = np.transpose(np.array(volumes))
plt.figure()
CS = plt.contour(X, Y, Z,levels=[0.1,1,10,100,1000,10000])
plt.clabel(CS, fontsize=10)
plt.title('Counter plot of N-Sphere HyperVolume')
plt.ylabel('radius')
plt.xlabel('n')
labels = ['0.1', '1','10','100',
           '1000', '10000']
for i in range(len(labels)):
    CS.collections[i].set_label(labels[i])

plt.legend()
plt.savefig('N_sphere_vol_py')


import random, os, pickle

players = {}

if(os.path.exists('~/Docs/python/playersdict')):
    openfile = input("Do you want to open the existing file? (y/n) [y] ")
    if(openfile != 'n'):
        filetoload = open("playersdict","rb")
        players = pickle.load(filetoload)
        filetoload.close()

def printplayers():
    print("Current players are: ")
    for player in players.keys():
        print(player)

printplayers()

def diceroll(numberofsides):
    return random.randint(1,int(numberofsides))

def getbittoadd():
    dicea = diceroll(12)
    diceb = diceroll(4)
    return round(dicea/diceb)

def createplayer(name):
    strength = 10
    skill = 10
    skill += getbittoadd()
    strength += getbittoadd()
    player = {'name':name, 'skill':skill, 'strength':strength}
    return player

while(input("Add another player? (y/n) [n] ") == 'y'):
    name = input("Enter the player's name: ")
    players[name] = createplayer(name)
    printplayers()

printplayers()
print(players)

filetosave = open('playersdict','wb')
pickle.dump(players,filetosave)
filetosave.close()


import sys

import toml
from schema import Or, Schema

config_schema = Schema(
    {
        "bot": {
            "token": str,
            "debug": Or(True, False),
            "production": Or(True, False),
        },
        "logs": {
            "level": Or(
                "DEBUG",
                "INFO",
                "WARNING",
                "ERROR",
                "CRITICAL",
            ),
            "sentry": str,
        },
        "database": {"uri": str, "name": str},
        "global": {"name": str, "guild_id": int},
    },
    ignore_extra_keys=True,
)

# Config Loader
try:
    config = toml.load("config.toml")
except FileNotFoundError:
    sys.exit()

# Validate Config
config_schema.validate(config)


import random

# check if its a matrix
def checkRec(matrix):
    length = len(matrix)
    for i in matrix:
        if len(i) != len(matrix[0]):
            return False, length, len(matrix[0])
        else:
            pass
    return True, length, len(matrix[0])

# show a matrix in a better way
def matrixShow(matrix):
    if_rec, len, wid = checkRec(matrix)
    if if_rec == True:
        for m in range(len):
            for n in range(wid):
                print(matrix[m][n], end = ' ')
            print('')
    else:
        return 0

# creare matrix
def matrixCreate(m, n, id = int(0), rand = False, randlist = [int(0),int(1)]):
    n_list = []
    matrix = []
    if rand == True:
        for i in range(m):
            n_list = []
            for j in range(n):
                n_list.append(random.choice(randlist))
            matrix.append(n_list)
    else:
        for i in range(n):
            n_list.append(id)
        for i in range(m):
            matrix.append(list(n_list))
    return matrix

def combinMat(mat_1, mat_2, m = 0, n = 0):
    mymat_1 = []
    for i in mat_1:
        mymat_1.append(i.copy())
    mat_1_check = checkRec(mat_1)
    mat_2_check = checkRec(mat_2)
    if mat_1_check[0] == False or mat_2_check[0] == False:
        return False, mat_1
    for m_i in range(len(mat_2)):
        for n_i in range(len(mat_2[0])):
            mymat_1[m_i + m][n_i + n] = mat_2[m_i][n_i]
    return mymat_1

def partSelect(matrix, ma, na, mb, nb):
    new_mat = []
    for m in range(abs(mb - ma)):
        new_list = []
        for n in range(abs(nb - na)):
            new_list.append(matrix[m + ma][n + na])
        new_mat.append(new_list)
    return new_mat


import sys
import os
sys.path.append(os.path.realpath(__file__))
from multiDict import addtwodimdict

input_files = sys.argv[1:]

def deal_hla(afile):
    type1 = 'NULL'
    type2 = 'NULL'

    base_name = os.path.basename(afile)
    sample, gene_name, out, txt  = base_name.split(".")
    with open(afile, "r") as f:
        for line in f:
            if line.startswith("IMGT/HLA does not exists"):
                return sample, gene_name, 'NULL', 'NULL'

            line = line.rstrip("\n")
            if line.startswith("HLA gene"):
                a,name = line.split(":")
                name = name.lstrip()
                continue
            if line.startswith("[Type 1]"):
                c = line.split("\t")
                type1 = c[1]
                continue
            if line.startswith("[Type 2]"):
                c = line.split("\t")
                type2 = c[1]
                continue
    return sample, name, type1, type2

hla_types = ["HLA-A","HLA-B","HLA-C","HLA-E","HLA-F","HLA-G","MICA","MICB","HLA-DMA","HLA-DMB",
        "HLA-DOA","HLA-DOB","HLA-DPA1","HLA-DPB1","HLA-DQA1","HLA-DQB1","HLA-DRA","HLA-DRB1",
        "HLA-DRB5","TAP1","TAP2"]

results = {}
for each_file in input_files:
    if each_file.endswith("out.txt"):
        sample, gene_name, type1, type2 = deal_hla(each_file)
        addtwodimdict(results, gene_name, 'type1', type1)
        addtwodimdict(results, gene_name, 'type2', type2)

print("HLA\t{}".format(sample))
for each in hla_types:
    if each in results:
        print("{}-1\t{}".format(each, results[each].get("type1",'NULL')))
        print("{}-2\t{}".format(each, results[each].get("type2",'NULL')))


from discord.ext import commands
import re
import discord
import asyncio
from datetime import datetime
import unicodedata
import aiohttp

class NameChanger:
    def __init__(self, bot):
        self.bot = bot

    @commands.command(name="check")
    @commands.has_permissions(manage_nicknames=True)
    @commands.guild_only()
    async def nickname_checker(self, ctx, output="channel"):
        """This will check if someone has special characters in their nickname and will either give you a list of all
        users who have special characters in their name or will directly change them.
        Example: っ, !, ◔
        """
        def check(reaction, user):
            return user == ctx.message.author and str(reaction.emoji) == '👍'

        server = ctx.message.guild
        membercount = server.member_count
        check_change = False
        # checks if the bot has the ability to change nicknames, if yes asks the user if he/she wants to get the list
        # or wants the nicknames directly changed
        if ctx.message.guild.me.guild_permissions.manage_nicknames:
            msg = await ctx.send("Would you like me to change all nicknames which contain special characters? If yes"
                                 " please add the `👍` emote as reaction.\nIf you don't want me to do it you don't have"
                                 " to do anything")
            await msg.add_reaction("👍")
            try:
                await self.bot.wait_for('reaction_add', timeout=15.0, check=check)
                check_change = True
            except asyncio.TimeoutError:
                pass
        else:
            check_change = False
        if check_change:
            text = f"""Okay. I will now check {membercount} members if their nicknames don't
contain special characters."""
            if membercount > 100:
                text = text + f" This might take a while."
            await ctx.send(text)
            counter = 0
            changed_users = ""
            could_be_changed = ""
            time_before = datetime.utcnow()
            for member in server.members:
                m_nick = member.display_name
                changed_nick = self.nickname_maker(m_nick)
                if m_nick != changed_nick:
                    # checks if a user can change his nickname if he is able to his nick will not be changed
                    try:
                        await member.edit(reason=f"Old name ({m_nick}) contained special characters.",
                                          nick=changed_nick)
                        changed_users = changed_users + f"{member.name}: {m_nick} was changed to {changed_nick}\n"
                    except commands.MissingPermissions:
                        could_be_changed += f"{member.name}: {m_nick} could be changed to {changed_nick}\n"
                    counter += 1

            if counter == 0:
                link = await self.mystbin(stringx="No users with special characters in their nicknames could be found.")
            else:
                link = await self.mystbin(stringx=f"Found {counter} members:\n{changed_users}\n\nThe nicknames of foll"
                                                  f"owing users couldn't be changed:\n{could_be_changed}")
            time_after = datetime.utcnow()
            difference = time_after - time_before
            difference = round(difference.total_seconds() / 60.0, 1)
            embed = discord.Embed(
                color=discord.Color.green(),
                title="Successfully finished!",
                description=f"Changed {counter} nicknames in {difference} minutes. Here is a list with all changes: "
                            f"{link}",
                timestamp=datetime.utcnow()
            )
            embed.set_author(name=f"{ctx.message.author}", icon_url=f"{ctx.message.author.avatar_url}")
            if output == "channel":
                await ctx.send(embed=embed)
            else:
                if output == "channel":
                    await ctx.send(embed=embed)
                else:
                    await ctx.message.author(embed=embed)
        else:
            # only returns the list
            await ctx.send("Okay. I will create a file now with all users who have special characters in their name.")
            changed_users = ""
            counter_ = 0
            time_before = datetime.utcnow()
            for member in server.members:
                m_nick = member.display_name
                changed_nick = self.nickname_maker(m_nick)
                if m_nick != changed_nick:
                    counter_ += 1
                    changed_users += f"{member.name}: {m_nick} could be changed to {changed_nick}\n"
            time_after = datetime.utcnow()
            difference = time_after - time_before
            difference = round(difference.total_seconds() / 60.0, 1)
            if changed_users == "":
                link = await self.mystbin(stringx="No users with special characters in their nicknames could be found.")
            else:
                link = await self.mystbin(stringx=f"Found {counter_} members:\n{changed_users}")
            embed = discord.Embed(
                color=discord.Color.green(),
                timestamp=datetime.utcnow(),
                title="Successfully finished!",
                description=f"Found {counter_} members with special characters in their nickname in "
                            f"{difference} minutes.\nHere is the link to the list: {link}",
            )
            embed.set_author(name=f"{ctx.message.author}", icon_url=f"{ctx.message.author.avatar_url}")

    @staticmethod
    def strip_accents(text):
        try:
            text = unicodedata.normalize('NFD', text)
            text = text.encode('ascii', 'ignore')
            text = text.decode("utf-8")
        except Exception as e:
            print(e)
            pass
        return str(text)

    def nickname_maker(self, old_nick):
        old_nick = self.strip_accents(old_nick)
        changed_nick = re.sub('[^a-zA-Z0-9 \n.]', '', old_nick)
        if len(changed_nick.replace(" ", "")) <= 1:
            changed_nick = "Request a new nickname"
        return changed_nick

    @nickname_checker.error
    async def nickname_error(self, ctx, error):
        # error handler
        if isinstance(error, commands.MissingPermissions):
            return
        elif isinstance(error, commands.NoPrivateMessage):
            return
        elif isinstance(error, commands.CommandError):
            embed = discord.Embed(
                color=discord.Color.red(),
                title="Something didn't go quite right...",
                description="I will report this error."
            )
            embed.set_author(name=f"{ctx.message.author}", icon_url=f"{ctx.message.author.avatar_url}")
            await ctx.send(embed=embed)
            print(error)

    @commands.command(name="setnick")
    @commands.guild_only()
    @commands.has_permissions(manage_nicknames=True)
    @commands.bot_has_permissions(manage_nicknames=True)
    async def setnick_cmd(self, ctx, user: discord.Member=None, *, nickname: str=None):
        """Changes the nickname of a specified user to a specified nickname."""
        if nickname is None or user is None and len(nickname) >= 2:
            embed = discord.Embed(
                color=discord.Color.red(),
                title="An error occurred.",
                description=f"Please follow the format: `{ctx.prefix}setnick {'user'} {'new nickname'}`.\n"
                            f"If you followed the format please make sure that the new nickname is at least two chara"
                            f"cters long."
            )
            return await ctx.send(embed=embed)
        await user.edit(nick=nickname, reason=f"Nickname edit by {ctx.message.author.name} ({ctx.message.author.id})")
        embed = discord.Embed(
            color=discord.Color.green(),
            title=f"Successfully changed {user}'s nickname.",
            description=f"New nickname: {nickname}\nOld nickname: {user.display_name}"
        )
        await ctx.send(embed=embed)

    @setnick_cmd.error
    async def setnick_error(self, ctx, error):
        # error handler
        if isinstance(error, commands.CheckFailure):
            return
        elif isinstance(error, commands.NoPrivateMessage):
            return
        elif isinstance(error, commands.BotMissingPermissions):
            embed = discord.Embed(
                color=discord.Color.red(),
                title="I am missing a necessary permission.",
                description="Please make sure I have the manage nicknames permission."
            )
            await ctx.send(embed=embed)
        elif isinstance(error, commands.CommandError):
            embed = discord.Embed(
                color=discord.Color.red(),
                title="Something didn't go quite right...",
                description="I will report this error."
            )
            embed.set_author(name=f"{ctx.message.author}", icon_url=f"{ctx.message.author.avatar_url}")
            await ctx.send(embed=embed)
            print(error)

    @staticmethod
    async def mystbin(stringx):
        async with aiohttp.ClientSession() as session:
            async with session.post("http://mystb.in/documents", data=stringx.encode('utf-8')) as post:
                post = await post.json()
        return f"http://mystb.in/{post['key']}.txt"

def setup(bot):
    bot.add_cog(NameChanger(bot))


import os
import subprocess
import re
from src.ratslap import RatSlap
from src.db_helper import DBHelper

class MetaKeyError(Exception):
    pass

class UndefinedSymbolError(Exception):
    pass

def get_shell_script(file_name):
    script_dir = os.path.dirname(__file__)
    return os.path.join(script_dir, "..", "shell_scripts", file_name)

class Shortcut(str):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.keys = []

    @property
    def valid(self):
        if len(self.keys) == 0:
            return False
        return bool(re.match(r"^((s)|(m*[bk]?))$", "".join(i.symbol_type[0] for i in self.keys)))

    @property
    def string(self):
        return " + ".join(i.repr for i in self.keys) + (" +" if self.all_modifiers() else "")

    def has_any_type(self, type_):
        return any((i.is_type(type_) for i in self.keys))

    def all_modifiers(self):
        return all(i.is_type("modifier") for i in self.keys) and len(self.keys) > 0

    def __iadd__(self, other):
        self.keys.insert(0, other)
        return self

    def __str__(self):
        if self.valid:
            return self.string
        else:
            return f"{self.string} is not a valid combo."

    def __repr__(self):
        return "+".join(i.repr for i in self.keys) + (" +" if self.all_modifiers() else "")

class Event:
    all_symbols = {
        'modifiers': {'Shift_L': 'LeftShift', 'Alt_L': 'LeftAlt', 'Super_L': 'Super_L',
                      'Control_L': 'LeftCtrl', 'Control_R': 'RightCtrl', 'Shift_R': 'RightShift',
                      'Alt_R': 'RightAlt', 'Super_R': 'Super_R'},
        'buttons': {'Button 1': 'Button1', 'Button 3': 'Button2', 'Button 2': 'Button3',
                    'Button 8': 'Button6', 'Button 9': 'Button7', 'Button 10': 'Button8',
                    'Button 11': 'Button9', 'Button 12': 'Button10', 'Button 13': 'Button11'},
        'specials': {'DPIUp': 'DPIUp', 'DPIDown': 'DPIDown', 'DPICycle': 'DPICycle',
                     'ModeSwitch': 'ModeSwitch', 'DPIShift': 'DPIShift', 'DPIDefault': 'DPIDefault'},
        'keys': {'Return': 'Enter', 'Escape': 'Escape', 'BackSpace': 'Backspace', 'Tab': 'Tab',
                 'space': 'Space', 'equal': '=', 'bracketleft': '[', 'bracketright': ']', 'backslash': '\\',
                 'semicolon': ';', 'minus': '-', 'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'e', 'f': 'F',
                 'g': 'G', 'h': 'H', 'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L', 'm': 'M', 'n': 'N', 'o': 'O',
                 'p': 'P', 'q': 'Q', 'r': 'R', 's': 'S', 't': 'T', 'u': 'U', 'v': 'V', 'w': 'W', 'x': 'X',
                 'y': 'Y', 'z': 'Z', 'A': 'A', 'B': 'B', 'C': 'C', 'D': 'D', 'E': 'E', 'F': 'F', 'G': 'G',
                 'H': 'H', 'I': 'I', 'J': 'J', 'K': 'K', 'L': 'L', 'M': 'M', 'N': 'N', 'O': 'O', 'P': 'P',
                 'Q': 'Q', 'R': 'R', 'S': 'S', 'T': 'T', 'U': 'U', 'V': 'V', 'W': 'W', 'X': 'X', 'Y': 'Y',
                 'Z': 'Z', '0': '0', '1': '1', '2': '2', '3': '3', '4': '4', '5': '5', '6': '6', '7': '7',
                 '8': '8', '9': '9', 'F1': 'F1', 'F2': 'F2', 'F3': 'F3', 'F4': 'F4', 'F5': 'F5', 'F6': 'F6',
                 'F7': 'F7', 'F8': 'F8', 'F9': 'F9', 'F10': 'F10', 'F11': 'F11', 'F12': 'F12',
                 'apostrophe': "'", 'grave': '`', 'comma': ',', 'period': '.', 'slash': '/', 'Pause': 'Pause',
                 'KP_Insert': 'Insert', 'Home': 'Home', 'Delete': 'Delete', 'Right': 'Right', 'Left': 'Left',
                 'Down': 'Down', 'Print': 'PrintScreen', 'KP_Prior': 'PageUp', 'KP_Next': 'PageDown',
                 'KP_Home': 'Home', 'KP_End': 'End', 'Up': 'Up', 'Num_Lock': 'NumLock', 'Insert': 'Insert',
                 'Scroll_Lock': 'ScrollLock', 'KP_Divide': 'Num/', 'KP_Multiply': 'Num*',
                 'KP_Subtract': 'Num-', 'KP_Add': 'Num+', 'KP_Enter': 'NumEnter', 'KP_0': 'Num0',
                 'KP_1': 'Num1', 'KP_2': 'Num2', 'KP_3': 'Num3', 'KP_4': 'Num4', 'KP_5': 'Num5',
                 'KP_6': 'Num6', 'KP_7': 'Num7', 'KP_8': 'Num8', 'KP_9': 'Num9', 'KP_Decimal': 'Num.',
                 'Menu': 'Menu'}}

    def __init__(self, event_type, symbol, keycode=None):
        self.type = event_type
        self.symbol = symbol
        self.keycode = keycode
        self.name = self.repr = self.symbol_type = self.dual = ""
        self.keymap = []
        self.exclude_list = []
        self.find_name()
        self.find_type()
        self.find_dual()

        if keycode and self.symbol not in self.keymap:
            print(f"You pressed a key which is not in standard english keyboard layout: {self.symbol}\n"
                  f"Ratslap can't handle non-english characters so it will be considered as '{self.repr}'\n\n")

    def find_name(self):
        if self.keycode:
            self.keymap = self.find_keymap()
            try:
                self.name = self.keymap[0]
                if self.symbol in self.keymap:
                    self.name = self.symbol

            except IndexError:
                raise UndefinedSymbolError(f"symbol not defined, '{self.symbol}'")
        else:
            self.name = f"Button {self.symbol}"

    def find_type(self):
        found = False
        while not found:
            for type_ in ["modifier", "button", "special", "key"]:
                if self.is_type(type_):
                    self.symbol_type = type_
                    self.repr = self.all_symbols[f"{type_}s"][self.name]
                    found = True
                    break
            else:
                self.exclude_list.append(self.name)
                self.find_name()

    def find_dual(self):
        if self.is_type("modifier"):
            self.dual = self.repr.replace("Left", "Right").replace("_L", "_R")
            if self.dual == self.repr:
                self.dual = self.repr.replace("Right", "Left").replace("_R", "_L")
        else:
            self.dual = None

    def is_type(self, type_):
        return self.name in self.all_symbols[f"{type_}s"]

    def find_keymap(self):
        keymap = filter(lambda x: "NoSymbol" not in x,
                        subprocess.run([get_shell_script("find_keymap.sh"), str(self.keycode)],
                                       capture_output=True).stdout.decode("utf-8").strip().split())
        return [key for key in keymap if key not in self.exclude_list]

    def set_new_name(self, new_name):
        self.name = new_name

    def set_new_repr(self, new_repr):
        self.repr = new_repr

    def __repr__(self):
        return f"{self.type} {self.repr} {self.symbol_type}"

    def __str__(self):
        return self.__repr__()

class EventList(list):
    def __init__(self, mouse_profile, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.mouse_profile = {i: list(map(str.strip,
                                          filter(None, j.split(" +")))) for i, j in mouse_profile.items()}

    def append(self, event):
        if not self.__contains__(event):
            super(EventList, self).append(event)

    def get_events(self, geometry=""):
        process = subprocess.run([get_shell_script("xev_parser.sh"), geometry], capture_output=True)
        event_table = map(str.split, (process.stdout.decode("utf-8").splitlines()))
        for row in event_table:
            try:
                self.append(Event(*row))
            except UndefinedSymbolError as e:
                print(e.args[0].capitalize(), ", skipping...", sep="")

    def __contains__(self, item):
        return item in [i.repr for i in self]

    def remove(self, shortcut_list):
        self[:] = [event for event in self if event.repr not in shortcut_list]

    def create_shortcut_from_events(self):
        shortcut = Shortcut()

        xev_transitions_for_buttons = {'left': '1', 'right': '3', 'middle': '2', 'g4': '8', 'g5': '9',
                                       'g6': '10', 'g7': '11', 'g8': '12', 'g9': '13'}

        while True:
            try:
                event = self[-1]  # type: Event
            except IndexError:
                break
            else:
                if event.type == "ButtonPress":
                    best_button = self.get_best_button(self.mouse_profile, event)
                    e = Event("ButtonPress", xev_transitions_for_buttons[best_button['name']])
                    self.remove(best_button["shortcut"])
                    shortcut += e
                else:
                    self.remove([event.repr])
                    shortcut += event
        return shortcut

    def get_best_button(self, mode, event):
        possible_buttons = {k: v for k, v in mode.items() if event.repr in v}
        r = {'name': '', 'shortcut': []}
        for button_name, shortcut in possible_buttons.items():
            if all(keyname in self for keyname in shortcut) and len(shortcut) > len(r['shortcut']):
                r['name'], r['shortcut'] = button_name, shortcut
        return r

if __name__ == '__main__':
    with DBHelper("settings.db") as conn:
        path = conn.select("file_paths", ("path",), program_name="ratslap").fetchone()[0]

    event_list = EventList(RatSlap(path).parse_mode(3))
    event_list.get_events()
    print(event_list.create_shortcut_from_events())


#http://practice.geeksforgeeks.org/problems/implement-strstr/1

def strstr(str, substr):
    index = -1
    for i in range(len(str)):
        if str[i:i+len(substr)] == substr:
            index = i
            break
    return index

if __name__=='__main__':
    n = int(input())
    for i in range(n):
        str,substr = input().strip().split()
        print(strstr(str, substr))


import tushare as ts

# codes = ['sh', 'sz', 'cyb', '000100', '002958']
codes = ['sh', 'sz', 'cyb', '300003']

def auto_get_tushare_data():
    data = ts.get_realtime_quotes(codes)
    # print(len(data))
    sh_price = round(float(data['price'].values[0]), 2)  # 上证指数 当前价格
    sh_pre_close = round(float(data['pre_close'].values[0]), 2)  # 上证指数 昨日收盘价
    sz_price = round(float(data['price'].values[1]), 2)  # 深证指数 当前价格
    sz_pre_close = round(float(data['pre_close'].values[1]), 2)  # 深证指数 昨日收盘价
    cyb_price = round(float(data['price'].values[2]), 2)  # 创业版指数 当前价格
    cyb_pre_close = round(float(data['pre_close'].values[2]), 2)  # 创业版指数 昨日收盘价
    sh = round(sh_price - sh_pre_close, 2)
    sz = round(sz_price - sz_pre_close, 2)
    cyb = round(cyb_price - cyb_pre_close, 2)
    sh_percent = round(sh / sh_pre_close * 100, 2)
    sz_percent = round(sz / sz_pre_close * 100, 2)
    cyb_percent = round(cyb / cyb_pre_close * 100, 2)
    # print('(', sh_price, sh, sh_percent, ')', '(', sz_price, sz, sz_percent, ')', '(', cyb_price, cyb, cyb_percent, ')')
    # print('(%.2f %.2f %.2f) (%.2f %.2f %.2f) (%.2f %.2f %.2f)'%(sh_price, sh, sh_percent, sz_price, sz, sz_percent, cyb_price, cyb, cyb_percent))
    result = ''
    result += (
        f'上证指数:\n当前价格:{sh_price}\n昨日收盘:{sh_pre_close}\n今日较昨日浮动:{sh};浮动百分比:{sh_percent}%'
    )
    result += (
        f'深证指数:\n当前价格:{sz_price}\n昨日收盘:{sz_pre_close}\n今日较昨日浮动:{sz};浮动百分比:{sz_percent}%'
    )
    result += f'创业版指数:\n当前价格:{cyb_price}\n昨日收盘:{cyb_pre_close}\n今日较昨日浮动:{cyb};浮动百分比:{cyb_percent}%'
    # print(result)
    # print('\n')
    # 从第4个开始遍历就是自己关注的股票代码
    for i in range(3, len(data)):
        price = float(data['price'].values[i])
        pre_close = float(data['pre_close'].values[i])
        # print(data.name.values[i], data.open.values[i], data.price.values[i], round((price-pre_close)/pre_close*100, 2))
        result += f'{data.name.values[i]}:\n当前价格:{price}\n昨日收盘:{pre_close}\n今日较昨日浮动:{round(price-pre_close,2)};浮动百分比:{round((price-pre_close)/pre_close*100, 2)}%'
        # print(f'{data.name.values[i]}:\n当前价格:{price}\n昨日收盘:{pre_close}\n今日较昨日浮动:{round(price-pre_close,2)};浮动百分比:{round((price-pre_close)/pre_close*100, 2)}%')
    # time.sleep(3)
    # print(result)
    return result

if __name__ == "__main__":
    result = auto_get_tushare_data()
    print(result)


import subprocess as sp
import os
#from configparser import *
#import daytime
import sys
import csv

with open ('firewallRules.csv','r') as csvfile:
    data = csv.DictReader(csvfile, delimiter=",")
    for values in data:
        firewall_rule_name = values['Firewall_rule_name']
        action = values['firewall_action']
        port = values['firewall_port']
        fw_proto = values['firewall_protocol']
        network = values['network']

fw_command = "gcloud compute firewall-rules create %s --%s %s:%s --network %s" %(firewall_rule_name, action, fw_proto, port, network)
os.system(fw_command)


#! /usr/bin/env python
import os
import sys
from tempfile import NamedTemporaryFile
import unittest

try:
    import mock
except:
    from unittest import mock

try:
    from cStringIO import StringIO
except:
    from io import StringIO

try:
    from ConfigParser import SafeConfigParser
except:
    from configparser import SafeConfigParser

from contextlib import contextmanager

try:
    from configutil import Config, ConfigError, \
        MissingSection, SectionError, ConfigArgumentError
except:
    from configutil.configutil import Config, ConfigError, \
        MissingSection, SectionError, ConfigArgumentError

@contextmanager
def capture(command, *args, **kwargs):
    out, sys.stdout = sys.stdout, StringIO()
    err, sys.stderr = sys.stderr, StringIO()
    try:
        command(*args, **kwargs)
        sys.stdout.seek(0)
        sys.stderr.seek(0)
        yield (sys.stdout.read().strip(), sys.stderr.read().strip())
    finally:
        sys.stdout = out
        sys.stderr = err

class TestConfigutil(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.config_path = NamedTemporaryFile().name

        parser = SafeConfigParser()
        parser.add_section('section0')
        parser.set('section0', 'arg0a', '1234.123')
        parser.set('section0', 'arg0b', 'True')
        parser.set('section0', 'arg0c', 'False')
        parser.add_section('section1')
        parser.set('section1', 'arg1a', 'configstring1a')
        parser.set('section1', 'arg1b', 'configstring1b')
        parser.set('section1', 'arg1c', '1000')

        with open(cls.config_path, 'w') as file:
            parser.write(file)

        cls.missing_path = NamedTemporaryFile().name

        parser = SafeConfigParser()
        parser.add_section('section0')
        parser.set('section0', 'arg0a', '1234.123')
        parser.add_section('section1')
        parser.set('section1', 'arg1c', '1000')

        with open(cls.missing_path, 'w') as file:
            parser.write(file)

    def setup_config(self, path):
        config = Config()

        if type(path) == list:
            config.add_paths(path)
        else:
            config.add_path(path)

        section = config.add_section('section0')
        section.add_argument('arg0a', 'a float', float)
        section.add_argument('arg0b', 'a boolean', bool)
        section.add_argument('arg0c', 'a boolean', bool)

        section = config.add_section('section1')
        section.add_argument('arg1a', 'a string', str)
        section.add_argument('arg1b', 'a string', str)
        section.add_argument('arg1c', 'an int', int)

        return config

    def test_error_creation(self):
        error = ConfigError('arg_name')
        self.assertEquals(error.arg, 'arg_name')
        self.assertEquals(error.message, 'Missing configuration argument "arg_name".')

        error = MissingSection('section_name')
        self.assertEquals(error.arg, 'section_name')
        self.assertEquals(error.message, 'Missing section "section_name" in configuration.')

        error = SectionError('section_name')
        self.assertEquals(error.arg, 'section_name')
        self.assertEquals(error.message, 'Section "section_name" does not exist in configuration.')

        error = ConfigArgumentError('arg_name')
        self.assertEquals(error.arg, 'arg_name')
        self.assertEquals(error.message, '')

    def test_config_error(self):
        config = Config()
        with self.assertRaises(AttributeError):
            config.parse()

    def test_config_get_section(self):
        config = self.setup_config([self.config_path])
        section = config.add_section('section2')
        self.assertEquals(section, config.get_section('section2'))
        self.assertEquals(section.__repr__(),
            '<ConfigSection(name=section2, required=True, arguments=[])>')
        section.add_argument('arg_name', 'arg_help', float)

        if sys.version_info >= (3, 0):
            self.assertEquals(section.arguments[0].__repr__(),
                '<ConfigArgument(name=arg_name, help=arg_help, type=<class \'float\'>, choices=None)>')
        else:
            self.assertEquals(section.arguments[0].__repr__(),
                '<ConfigArgument(name=arg_name, help=arg_help, type=<type \'float\'>, choices=None)>')

        with self.assertRaises(SectionError):
            config.get_section('section3')
        with self.assertRaises(MissingSection):
            args = config.parse()

    def test_config(self):
        sys.argv = [sys.argv[0]]
        config = self.setup_config([self.config_path])
        args = config.parse()
        self.assertEqual(args, config.arguments)
        self.assertEqual(args.section0.arg0a, 1234.123)
        self.assertEqual(args.section0.arg0b, True)
        self.assertEqual(args.section0.arg0c, False)
        self.assertEqual(args.section1.arg1a, 'configstring1a')
        self.assertEqual(args.section1.arg1b, 'configstring1b')
        self.assertEqual(args.section1.arg1c, 1000)
        self.assertEqual(args.command, None)

    def test_config_with_argv(self):
        sys.argv = [sys.argv[0]]
        sys.argv.extend(['--arg0b', 'false', '--arg1b', 'argstring1b',
            '--arg1c', '2000'])
        config = self.setup_config(self.config_path)
        args = config.parse()
        self.assertEqual(args, config.arguments)
        self.assertEqual(args.section0.arg0a, 1234.123)
        self.assertEqual(args.section0.arg0b, False)
        self.assertEqual(args.section0.arg0c, False)
        self.assertEqual(args.section1.arg1a, 'configstring1a')
        self.assertEqual(args.section1.arg1b, 'argstring1b')
        self.assertEqual(args.section1.arg1c, 2000)
        self.assertEqual(args.command, None)

    def test_config_with_commands(self):
        sys.argv = [sys.argv[0]]
        sys.argv.extend((['command1', '--arg0a', '1.1',
            '--arg1a', 'argstring1a']))
        config = self.setup_config(self.config_path)
        config.add_command('command0', 'command0 help')
        config.add_command('command1', 'command1 help')
        config.add_command('command2', 'command2 help')
        args = config.parse()
        self.assertEqual(args, config.arguments)
        self.assertEqual(args.section0.arg0a, 1.1)
        self.assertEqual(args.section0.arg0b, True)
        self.assertEqual(args.section0.arg0c, False)
        self.assertEqual(args.section1.arg1a, 'argstring1a')
        self.assertEqual(args.section1.arg1b, 'configstring1b')
        self.assertEqual(args.section1.arg1c, 1000)
        self.assertTrue(args.command, 'command1')

    def test_config_with_multiple_commands(self):
        sys.argv = [sys.argv[0]]
        sys.argv.extend((['command1', 'command0', '--arg0a', '1.1',
            '--arg1a', 'argstring1a']))
        config = self.setup_config(self.config_path)
        config.add_command('command0', 'command0 help')
        config.add_command('command1', 'command1 help')
        expected_output = 'usage: {cmd} [-h]  ...\n' \
            '{cmd}: error: unrecognized arguments: command0'.format(cmd=sys.argv[0])
        with self.assertRaises(SystemExit) as cm:
            with capture(config.parse) as out, err:
                self.assertEqual(expected_output, err)
        self.assertEqual(cm.exception.code, 2)

    def test_config_with_unknown_command(self):
        sys.argv = [sys.argv[0]]
        sys.argv.extend((['command2', '--arg0a', '1.1',
            '--arg1a', 'argstring1a']))
        config = self.setup_config(self.config_path)
        config.add_command('command0', 'command0 help')
        config.add_command('command1', 'command1 help')
        expected_output = 'usage: {cmd} [-h]  ...\n' \
            '{cmd}: error: argument : invalid choice: \'command2\'' \
            '(choose from \'command0\', \'command1\')'.format(cmd=sys.argv[0])
        with self.assertRaises(SystemExit) as cm:
            with capture(config.parse) as out, err:
                self.assertEqual(expected_output, err)
        self.assertEqual(cm.exception.code, 2)

    def test_config_with_config_arg(self):
        sys.argv = [sys.argv[0]]
        sys.argv.extend(['--config', self.config_path])
        config = self.setup_config(self.config_path)
        section = config.get_section('section0')
        section.add_argument('arg0d', 'a boolean', bool)
        with self.assertRaises(ConfigError) as cm:
            config.parse()

    @mock.patch('configutil.configutil.getenv')
    def test_config_with_envs(self, mock_env):
        sys.argv = [sys.argv[0]]
        envs = {'arg0b': 'True', 'arg0c': 'False',
            'arg1a': 'argstring1a', 'arg1b': 'argstring1b'}
        mock_env.side_effect = lambda k: envs.get(k, None)

        config = self.setup_config(self.missing_path)
        args = config.parse()

        self.assertEqual(mock_env.call_count, 4)
        self.assertEqual(args, config.arguments)
        self.assertEqual(args.section0.arg0a, 1234.123)
        self.assertEqual(args.section0.arg0b, True)
        self.assertEqual(args.section0.arg0c, False)
        self.assertEqual(args.section1.arg1a, 'argstring1a')
        self.assertEqual(args.section1.arg1b, 'argstring1b')
        self.assertEqual(args.section1.arg1c, 1000)
        self.assertEqual(args.command, None)

    @mock.patch('configutil.configutil.getenv')
    def test_config_with_command_envs(self, mock_env):
        sys.argv = [sys.argv[0]]
        sys.argv.extend((['command0', '--arg0a', '1.1',
            '--arg1a', 'argstring1a']))
        envs = {'arg0b': 'True', 'arg0c': 'False',
            'arg1a': 'envstring1a', 'arg1b': 'envstring1b'}
        mock_env.side_effect = lambda k: envs.get(k, None)

        config = self.setup_config(self.missing_path)
        config.add_command('command0', 'command0 help')
        config.add_command('command1', 'command1 help')
        config.add_command('command2', 'command2 help')

        args = config.parse()

        self.assertEqual(mock_env.call_count, 3)
        self.assertEqual(args, config.arguments)
        self.assertEqual(args.section0.arg0a, 1.1)
        self.assertEqual(args.section0.arg0b, True)
        self.assertEqual(args.section0.arg0c, False)
        self.assertEqual(args.section1.arg1a, 'argstring1a')
        self.assertEqual(args.section1.arg1b, 'envstring1b')
        self.assertEqual(args.section1.arg1c, 1000)
        self.assertEqual(args.command, 'command0')
        self.assertIsNotNone(config.__repr__())

    @mock.patch('configutil.configutil.getenv')
    def test_config_with_command_envs_error(self, mock_env):
        sys.argv = [sys.argv[0]]
        sys.argv.extend((['command0', '--arg0a', '1.1',
            '--arg1a', 'argstring1a']))
        envs = {'arg0b': 'True', 'arg0c': 'False',
            'arg1a': 'envstring1a'}
        mock_env.side_effect = lambda k: envs.get(k, None)

        config = self.setup_config(self.missing_path)
        config.add_command('command0', 'command0 help')
        config.add_command('command1', 'command1 help')
        config.add_command('command2', 'command2 help')

        with self.assertRaises(ConfigError):
            args = config.parse()

    def test_config_help(self):
        sys.argv = [sys.argv[0]]
        sys.argv.extend((['--help']))

        config = self.setup_config(self.config_path)
        config.add_command('command0', 'command0 help')
        config.add_command('command1', 'command1 help')
        config.add_command('command2', 'command2 help')

        expected_output = 'usage: {cmd} [-h]  ...\n' \
        '\n' \
        'optional arguments:\n' \
        '  -h, --help  show this help message and exit\n' \
        '\n' \
        'available commands:\n' \
        '              command help\n' \
        'command0  command0 help\n' \
        'command1  command1 help\n' \
        'command2  command2 help\n'.format(cmd=sys.argv[0])

        with self.assertRaises(SystemExit) as cm:
            with capture(config.parse) as out, err:
                self.assertEqual(expected_output, err)
        self.assertEqual(cm.exception.code, 0)

    def test_command_help(self):
        sys.argv = [sys.argv[0]]
        sys.argv.extend((['command1', '--help']))

        config = self.setup_config(self.config_path)
        config.add_command('command0', 'command0 help')
        config.add_command('command1', 'command1 help')
        config.add_command('command2', 'command2 help')

        expected_output = 'usage: {cmd} command1 [-h] [--config CONFIG] [--arg1a ARG1A]\n' \
        '                            [--arg1b ARG1B] [--arg1c ARG1C] [--arg0a ARG0A]\n' \
        '                            [--arg0b ARG0B] [--arg0c ARG0C]\n' \
        '\n' \
        'optional arguments:\n' \
        '  -h, --help       show this help message and exit\n' \
        '  --config CONFIG  configuration file path\n' \
        '  --arg1a ARG1A    a string\n' \
        '  --arg1b ARG1B    a string\n' \
        '  --arg1c ARG1C    an int\n' \
        '  --arg0a ARG0A    a float\n' \
        '  --arg0b ARG0B    a boolean\n' \
        '  --arg0c ARG0C    a boolean\n'.format(cmd=sys.argv[0])

        with self.assertRaises(SystemExit) as cm:
            with capture(config.parse) as out, err:
                self.assertEqual(expected_output, err)
        self.assertEqual(cm.exception.code, 0)

if __name__ == '__main__':
    unittest.main()


from django.contrib import admin
from django.urls import path, include
from django.urls import re_path as url
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
from rest_framework import permissions
from metric import views

schema_view = get_schema_view(
    openapi.Info(
        title="Snippets API",
        default_version='v1',
        description="Test description",
        terms_of_service="https://www.google.com/policies/terms/",
        contact=openapi.Contact(email="contact@snippets.local"),
        license=openapi.License(name="BSD License"),
    ),
    public=True,
    permission_classes=[permissions.AllowAny],
)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/metric/', views.metric_api, name="metric_api"),
]

# swagger
urlpatterns += [
    url(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),  # noqa E501
    url(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),  # noqa E501
    url(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),  # noqa E501
]


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
from ListNode import ListNode

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        val_list = []
        if head == None:
            return head
        while head.next:
            val_list.append(head.val)
            head = head.next
        # print(val_list[::-1])
        cur = head
        for one in val_list[::-1]:
            cur.next = ListNode(one)
            cur = cur.next
        cur.next = None
        return head

if __name__ == '__main__':
    head = ListNode([1, 2, 3, 4, 5])
    print(Solution().reverseList(head))


#!/usr/bin/env python3
import sys
import os
import socket
import json
import yaml

host_list = ('drive.google.com', 'mail.google.com', 'google.com')
host_for_tests = {}
host_for_tests2 = {}

for host_site in host_list: # выполним первую проверку и заполним словать с элементами сайт:IP
    ip_site = socket.gethostbyname(host_site)
    host_for_tests [host_site] = ip_site
    host_for_tests2 [host_site] = [ip_site, 'default']

with open ('host_for_tests.json','w') as json_log:
    json_log.write (json.dumps(host_for_tests2, indent=2))

with open ('host_for_tests.yaml','w') as yaml_log:
    yaml_log.write (yaml.dump(host_for_tests2, indent=2, explicit_start=True, explicit_end=True))

print ('Будем мониторить данные сайты и их дефолтные IP полученные в результе первой проверки:' + '\n' + str(host_for_tests))

#print (type(list_ip))
print ('\n'+'\n'+'Погнали, проверяем доступность сайтов в цикле')
while True: # запустим бесконечный цикл проверок внутри которого будем проверять сайты из словаря
    for host_site, ip_site in host_for_tests.items():  # цикл проверки по словарю в котором элементы состоят из сайт:IP
        ip_current = socket.gethostbyname(host_site)
        #ip_current = '192.168.1.1' # ручной "стоп-кран" чтоб не гонять долго цикл
        if ip_current == ip_site:
            print (host_site + ':  текущий IP - ' +ip_current + ', IP из первой проверки - '+ ip_site )
        else:
            print ('Для '+host_site + ' IP не сходятся, было - ' + ip_site + ', стало - '+ ip_current)

            #print (f'[ERROR] {host_site} IP mismatch: {ip_site} {ip_current}')
            print ('\n')
            my_error_to_stdout = sys.stdout
            my_error_to_stdout.write(f'[ERROR] {host_site} IP mismatch: {ip_site} {ip_current}')
            host_for_tests = {host_site: ip_current}
            host_for_tests2 [host_site] = [ip_current, 'new']

            #print ('\n')
            #print (host_for_tests2)

            #перезапишем файлики с обновленным словарем
            # в идеале конечно нужно было:
		#	1. загрузить в словарь данные из json и yaml файлов;
		#	2. обновить по ключу сбойный IP или даже удалить элемент из словаря со сбойным IP и добавить обратно новый
		#	3. записать словать обратно в 2 файла
		#	4. но т.к. мы все равно записываем словарь целиком, то думаю можно и так, заново записав словарь поверх текущих данных.
		#	5. значения в словаре представлены ввиде списка, так что бы наглядно было видно какой именно IP изменился.
            with open ('host_for_tests.json','w') as json_log:
                 json_log.write (json.dumps(host_for_tests2, indent=2))

            with open ('host_for_tests.yaml','w') as yaml_log:
                 yaml_log.write (yaml.dump(host_for_tests2, indent=2, explicit_start=True, explicit_end=True))

            exit ()


__all__ = ["create_loader", "create_data_loader"]

from .create_datasets import create_dataset
from .create_transforms import create_transform

def create_loader(
    dataset,
    batch_size,
    split="val",
    operations=None,
    input_columns=None,
    output_columns=None,
    num_parallel_workers=4,
    max_rowsize=16,
    python_multiprocessing=False,
):
    drop_remainder = True if split == "train" else False
    dataset = dataset.project(columns=input_columns)

    if operations:
        dataset = dataset.batch(
            batch_size=batch_size,
            drop_remainder=drop_remainder,
            num_parallel_workers=num_parallel_workers,
            max_rowsize=max_rowsize,
            per_batch_map=operations,
            input_columns=input_columns,
            output_columns=output_columns,
            python_multiprocessing=python_multiprocessing,
        )
    else:
        dataset = dataset.batch(
            batch_size=batch_size,
            drop_remainder=drop_remainder,
            python_multiprocessing=python_multiprocessing,
        )
    return dataset

def create_data_loader(model_name, cfg, pipeline_cfg, split, batch_size):
    cfg_dict = cfg.cfg_dict
    shuffle = split == "train"

    if pipeline_cfg is None or isinstance(pipeline_cfg, list):
        transforms_pipeline_type = model_name
    else:
        transforms_pipeline_type = pipeline_cfg.name
        pipeline_cfg = pipeline_cfg.pipeline

    operations, input_columns, output_columns = create_transform(
        model_name=transforms_pipeline_type, split=split, pipeline=pipeline_cfg, **cfg_dict
    )

    if cfg_dict.get("transform_while_batching", True):
        dataset = create_dataset(
            name=cfg.dataset_name,
            root=cfg.input_path,
            split=split,
            shuffle=shuffle,
            nframes=cfg.get("num_frames", None),
            **cfg_dict
        )

        loader = create_loader(
            dataset=dataset,
            batch_size=batch_size,
            operations=operations,
            input_columns=input_columns,
            output_columns=output_columns,
            split=split,
            num_parallel_workers=cfg_dict.get("num_parallel_workers", 4),
            max_rowsize=cfg_dict.get("max_rowsize", 16),
            python_multiprocessing=cfg_dict.get("python_multiprocessing", False),
        )
    else:
        dataset = create_dataset(
            name=cfg.dataset_name,
            root=cfg.input_path,
            split=split,
            shuffle=shuffle,
            nframes=cfg.get("num_frames", None),
            transforms=operations,
            column_names=output_columns,
            **cfg_dict
        )

        loader = create_loader(
            dataset=dataset,
            batch_size=batch_size,
            input_columns=output_columns,
            split=split,
        )
    return loader


from ggrc import db
from ggrc.models.types import JsonType
from ggrc.models.mixins import (
    Base, Titled, Described, Timeboxed, Stateful, WithContact
    )

class CycleTaskGroupObjectTask(
    WithContact, Stateful, Timeboxed, Described, Titled, Base, db.Model):
  __tablename__ = 'cycle_task_group_object_tasks'
  _title_uniqueness = False

  VALID_STATES = (None, 'InProgress', 'Assigned', 'Finished', 'Declined', 'Verified')

  cycle_id = db.Column(
      db.Integer, db.ForeignKey('cycles.id'), nullable=False)
  cycle_task_group_id = db.Column(
      db.Integer, db.ForeignKey('cycle_task_groups.id'), nullable=False)
  cycle_task_group_object_id = db.Column(
      db.Integer, db.ForeignKey('cycle_task_group_objects.id'), nullable=True)
  task_group_task_id = db.Column(
      db.Integer, db.ForeignKey('task_group_tasks.id'), nullable=False)
  task_group_task = db.relationship(
    "TaskGroupTask",
    foreign_keys="CycleTaskGroupObjectTask.task_group_task_id"
    )
  task_type = db.Column(
      db.String(length=250), nullable=False)
  response_options = db.Column(
      JsonType(), nullable=False, default='[]')
  selected_response_options = db.Column(
      JsonType(), nullable=False, default='[]')

  sort_index = db.Column(
      db.String(length=250), default="", nullable=False)

  _publish_attrs = [
      'cycle',
      'cycle_task_group_object',
      'cycle_task_group',
      'task_group_task',
      'cycle_task_entries',
      'sort_index',
      'task_type',
      'response_options',
      'selected_response_options'
      ]


from dataclasses import dataclass, field
from typing import Iterable, List, Optional

from dataclasses_json import LetterCase, dataclass_json

from ..core import AttributeDef, Cardinality, EntityDef, ObjectId, TypeCategory, RelationshipEndDef, RelationshipDef
from ..data_dictionary import (BusinessDataset, BusinessDatasetAttributes,
                               BusinessDatasetAttributesBase,
                               BusinessDatasetAttributesDefaultsBase,
                               BusinessDatasetBase,
                               BusinessDatasetDefaultsBase)

dashboard_super_type = ["m4i_dataset"]

dashboard_attributes_def = [
    AttributeDef(
        name="updatedAt",
        type_name="string"
    ),
    AttributeDef(
        name="version",
        type_name="string"
    ),
    AttributeDef(
        name="creator",
        type_name="array<m4i_person>",
        cardinality=Cardinality.SET
    ),
    AttributeDef(
        name="elasticCluster",
        type_name="array<m4i_elastic_cluster>",
        cardinality=Cardinality.SET
    ),
    AttributeDef(
        name="kibanaSpace",
        type_name="array<m4i_kibana_space>",
        cardinality=Cardinality.SET
    ),
]

dashboard_def = EntityDef(
    category=TypeCategory.ENTITY,
    name="m4i_dashboard",
    description="A type definition for a generic Dashboard in the context of models4insight.com",
    type_version="1.0",
    super_types=dashboard_super_type,
    attribute_defs=dashboard_attributes_def
)
end_1_kibana_dashboard = RelationshipEndDef(
    type="m4i_kibana_space",
    name="dashboards",
)
end_2_kibana_dashboard = RelationshipEndDef(
    type="m4i_dashboard",
    name="kibanaSpace",
)

m4i_kibana_dashboard_rel_def = RelationshipDef(
    end_def1=end_1_kibana_dashboard,
    end_def2=end_2_kibana_dashboard,
    name="m4i_kibana_space_dashboard_assignment",
    category=TypeCategory.RELATIONSHIP
)

end_1_person_dashboard = RelationshipEndDef(
    type="m4i_person",
    name="dashboards",
)
end_2_person_dashboard = RelationshipEndDef(
    type="m4i_dashboard",
    name="creator",
)

m4i_person_dashboard_rel_def = RelationshipDef(
    end_def1=end_1_person_dashboard,
    end_def2=end_2_person_dashboard,
    name="m4i_person_dashboard_assignment",
    category=TypeCategory.RELATIONSHIP
)

end_1_elastic_cluster_dashboard = RelationshipEndDef(
    type="m4i_elastic_cluster",
    name="dashboards",
)
end_2_elastic_cluster_dashboard = RelationshipEndDef(
    type="m4i_dashboard",
    name="elasticCluster",
)

m4i_elastic_cluster_dashboard_rel_def = RelationshipDef(
    end_def1=end_1_elastic_cluster_dashboard,
    end_def2=end_2_elastic_cluster_dashboard,
    name="m4i_elasticCluster_dashboard_assignment",
    category=TypeCategory.RELATIONSHIP
)

@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass
class DashboardAttributesBase(BusinessDatasetAttributesBase):
    pass

# END DashboardAttributesBase

@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass
class DashboardAttributesDefaultsBase(BusinessDatasetAttributesDefaultsBase):
    updated_at: Optional[str] = None
    version: Optional[str] = None
    elastic_cluster: List[ObjectId] = field(default_factory=list)
    kibana_space: List[ObjectId] = field(default_factory=list)
    creator: List[ObjectId] = field(default_factory=list)

# END DashboardAttributesBase

@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass
class DashboardAttributes(BusinessDatasetAttributes,
                          DashboardAttributesDefaultsBase, DashboardAttributesBase):
    pass

# END DashboardAttributes

@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass
class DashboardBase(BusinessDatasetBase):
    attributes: DashboardAttributes

# END DashboardBase

@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass
class DashboardDefaultsBase(BusinessDatasetDefaultsBase):
    pass

# END DashboardDefaultsBase

@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass
class Dashboard(
    BusinessDataset,
    DashboardDefaultsBase,
    DashboardBase,
):
    type_name: str = "m4i_dashboard"

    @classmethod
    def get_type_def(cls):
        return dashboard_def

    def get_referred_entities(self) -> Iterable[ObjectId]:
        """
        Returns the following references for this Dashboard:
        * List of Kibana Space
        * List of Elastic Cluster
        * List of Atlas Person
        """

        references = [
            *super().get_referred_entities(),
            *self.attributes.kibana_space,
            *self.attributes.elastic_cluster,

        ]

        if self.attributes.creator is not None:
            references = [*references, *self.attributes.creator]
        # END IF

        return filter(None, references)
    # END get_referred_entities

# END Dashboard


import datetime
import os
import matplotlib
import numpy as np
import pandas as pd
matplotlib.use('agg')
import matplotlib.pyplot as plt

"""
Evolution d'une espèce avec le temps
"""

def one_species_evolution_function(data: pd.DataFrame, species: str, with_cf: bool, location: str, user_id: str) -> None:
    # dossier d'export
    directory = "files/one_species_evolution_" + user_id
    if not os.path.exists(directory):
        os.makedirs(directory)

    year = data.iloc[-1]["Date"].year
    month = data.iloc[-1]["Date"].month
    last_date = datetime.datetime(year=year, month=month, day=28)

    # Filtrer espèce
    data = data.loc[data["Espèce"] == species]

    # Filtrer lieu
    if location != "Tous les lieux":
        data = data.loc[data["Lieu"] == location]

    # Si sans cf
    if not with_cf:
        data = data.loc[data["cf"] != "cf."]

    # Enregistrer le fichier excel
    writer = pd.ExcelWriter(directory + '/one_species_evolution.xlsx', engine='xlsxwriter', datetime_format='dd.mm.yyyyy', date_format='dd.mm.yyyyy')

    # Création du graphique cumulation de l'espèce
    total_obs = cumulative_observations(data, last_date)
    total_obs.to_excel(writer, sheet_name='Toutes les observations')
    plt.figure()
    plt.plot(total_obs.index, total_obs["Observations cumulées"],
             label="Observations totales")

    plt.ylabel("Nombre d'observations")
    plt.title("Evolution du nombre d'observations de " + species)
    plt.ylim(ymin=0)
    plt.legend(loc="upper left")
    plt.savefig(directory + "/observations_cumulees_par_mois.svg", bbox_inches='tight')

    # Observations par troncs
    plt.figure()
    for trunk in data["Tronc"].dropna().unique():
        trunk_obs = data.loc[(data["Tronc"] == trunk)]
        trunk_obs = cumulative_observations(trunk_obs, last_date)
        if len(trunk_obs) > 0:
            plt.plot(trunk_obs.index, trunk_obs["Observations cumulées"],
                     label="Tronc " + str(trunk))
            trunk_obs.to_excel(writer, sheet_name="Observations sur tronc " + trunk)

    plt.ylabel("Nombre d'observations")
    plt.title("Evolution du nombre d'observations de " + species + " sur troncs")
    plt.ylim(ymin=0)
    plt.legend(loc="upper left")
    plt.savefig(directory + "/observations_cumulees_par_mois_troncs.svg", bbox_inches='tight')

    # Observations par groupes troncs
    plt.figure()
    for trunk_group in data["Groupe troncs"].dropna().unique():
        trunk_group_obs = data.loc[(data["Groupe troncs"] == trunk_group)]
        trunk_group_obs = cumulative_observations(trunk_group_obs, last_date)
        if len(trunk_group_obs) > 0:
            plt.plot(trunk_group_obs.index, trunk_group_obs["Observations cumulées"],
                     label="Groupe de troncs " + str(trunk_group))
            trunk_group_obs.to_excel(writer, sheet_name="Observations groupe tronc " + trunk_group)

    plt.ylabel("Nombre d'observations")
    plt.title("Evolution du nombre d'observations de " + species + " sur groupes de troncs")
    plt.ylim(ymin=0)
    plt.legend(loc="upper left")
    plt.savefig(directory + "/observations_cumulees_par_mois_groupe_troncs.svg", bbox_inches='tight')

    # Observations par espèces de troncs
    plt.figure()
    for trunk_species in data["Espèce du tronc"].dropna().unique():
        trunk_species_obs = data.loc[(data["Espèce du tronc"] == trunk_species)]
        trunk_species_obs = cumulative_observations(trunk_species_obs, last_date)
        if len(trunk_species_obs) > 0:
            plt.plot(trunk_species_obs.index, trunk_species_obs["Observations cumulées"],
                     label=str(trunk_species))
            trunk_species_obs.to_excel(writer, sheet_name="Observations sur " + trunk_species)

    plt.ylabel("Nombre d'observations")
    plt.title("Evolution du nombre d'observations de " + species + " sur espèces de troncs")
    plt.ylim(ymin=0)
    plt.legend(loc="upper left")
    plt.savefig(directory + "/observations_cumulees_par_mois_especes_troncs.svg", bbox_inches='tight')

    writer.save()

def cumulative_observations(data: pd.DataFrame, last_date) -> pd.DataFrame:
    count = data[["Date", "Espèce"]].groupby(pd.Grouper(key='Date', freq='1M')).count()
    observations = pd.DataFrame([], )
    observations["Observations"] = count["Espèce"]
    observations["Observations cumulées"] = observations.cumsum()
    observations_last_date = observations.iloc[-1].name

    # Ajouter une ligne de la dernière date du dataframe total si absente
    if len(observations) > 0:
        if observations_last_date < last_date:
            new_row = observations.iloc[-1:]
            new_row = new_row.rename(index={observations_last_date: last_date})
            new_row['Observations'] = 0
            observations = pd.concat([observations, new_row])

    return observations


import math

class Graph:
    no_of_vertices: int
    nodes = list()

    def __init__(self, no_of_vertices: int):
        self.no_of_vertices = no_of_vertices
        for x in range(no_of_vertices):
            self.nodes.append({"adj_nodes": list(), "color": "white", "d": math.inf, "p": math.nan})

    def add_edge(self, src: int, dst: int):
        self.nodes[src]["adj_nodes"].append(dst)
    #
    # def get_adj_nodes(self, x:int):
    #     return self.nodes[x]
    #
    # def __str__(self):
    #     return str(self.nodes)

if __name__ == '__main__':
    g = Graph(5)

    g.add_edge(0, 1)
    g.add_edge(0, 4)

    g.add_edge(1, 0)
    g.add_edge(1, 2)
    g.add_edge(1, 4)

    g.add_edge(2, 1)

    g.add_edge(3, 4)

    g.add_edge(4, 0)
    g.add_edge(4, 1)
    g.add_edge(4, 3)

    src_vertex = 0
    g.nodes[0]["color"] = "gray"
    g.nodes[0]["d"] = 0
    g.nodes[0]["p"] = math.nan

    q = [src_vertex]

    while len(q) != 0:
        u = q[0]
        q.remove(u)
        for v in g.nodes[u]["adj_nodes"]:
            if g.nodes[v]["color"] == "white":
                g.nodes[v]["color"] = "gray"
                g.nodes[v]["d"] = g.nodes[u]["d"] + 1
                g.nodes[v]["p"] = u
                q.append(v)
        g.nodes[u]["color"] = "black"

    x = 2
    while x is not math.nan:
        print(x)
        x = g.nodes[x]["p"]


# -*- coding: utf-8 -*-
# __author__ = 'qiuping'

"""组间差异两样品比较检验分析"""

from biocluster.workflow import Workflow
import os
from mbio.packages.meta.common_function import get_save_pdf_status,get_name
from mbio.packages.meta.save_params import save_params

class TwoSampleWorkflow(Workflow):
    def __init__(self, wsheet_object):
        self._sheet = wsheet_object
        super(TwoSampleWorkflow, self).__init__(wsheet_object)
        options = [
            {"name": "otu_file", "type": "infile", 'format': "meta.otu.otu_table"},
            {"name": "type", "type": "string", "default": "two.side"},
            {"name": "test", "type": "string"},
            {"name": "level", "type": "int"},
            {"name": "correction", "type": "string", "default": "none"},
            {"name": "ci", "type": "float", "default": 0.05},
            {"name": "sample1", "type": "string"},
            {"name": "sample2", "type": "string"},
            {"name": "methor", "type": "string"},
            {"name": "coverage", "type": "float"},
            {"name": "params", "type": "string"},
            {"name": "update_info", "type": "string"},
            {"name": "main_id", "type": "string"},
        ]
        self.add_option(options)
        self.set_options(self._sheet.options())
        self.two_sample = self.add_tool("statistical.metastat")

    def run_two_sample(self):
        if self.option("test") == "chi":
            options = {
                "chi_input": self.option("otu_file"),
                "chi_sample1": self.option("sample1"),
                "chi_sample2": self.option("sample2"),
                "chi_correction": self.option("correction"),
                "test": self.option("test"),
                "chi_methor": self.option("methor"),
                "chi_coverage": self.option("coverage"),
                "cpu": 16
            }
        else:
            options = {
                "fisher_input": self.option("otu_file"),
                "fisher_ci": self.option("ci"),
                "fisher_sample1": self.option("sample1"),
                "fisher_sample2": self.option("sample2"),
                "fisher_correction": self.option("correction"),
                "test": self.option("test"),
                "fisher_type": self.option("type"),
                "fisher_methor": self.option("methor"),
                "fisher_coverage": self.option("coverage"),
                "cpu": 16
            }
        self.two_sample.set_options(options)
        self.output_dir = self.two_sample.output_dir
        self.two_sample.on("end", self.set_db)
        self.two_sample.run()

    def end(self):
        if self.pdf_status:
            if self.pdf_status:
                pdf_outs = self.figsave.output_dir
                os.system("cp -r {}/* {}/".format(pdf_outs, self.output_dir))
        save_params(self.output_dir, self.id)
        result_dir = self.add_upload_dir(self.output_dir)
        result_dir.add_relpath_rules([
            [".", "", "物种差异两样本比较结果目录", 0, "110143"],
            [r"./两样本比较差异检验柱形图.pdf", "pdf", "两样本比较中具有显著差异的丰度排行前15的物种差异检验柱形图", 0, ""]
        ])
        result_dir.add_regexp_rules([
            [r".*_result\.xls", "xls", "物种组间差异显著性比较结果表，包括均值，标准差，p值", 0, "110144"],
            [r".*_CI\.xls", "xls", "组间差异显著性比较两组，两样本比较的置信区间值以及效果量", 0, "110145"]
        ])
        super(TwoSampleWorkflow, self).end()

    def set_db(self):
        api_two_sample = self.api.stat_test
        params = eval(self.option("params"))
        two_sample_path = self.output_dir + '/' + self.option("test") + '_result.xls'
        ci_path = self.output_dir + '/' + self.option("test") + '_CI.xls'
        if not os.path.isfile(two_sample_path):
            self.logger.error("找不到报告文件:{}".format(two_sample_path))
            self.set_error("找不到报告文件", code="12704201")
        if not os.path.isfile(ci_path):
            self.logger.error("找不到报告文件:{}".format(ci_path))
            self.set_error("找不到报告文件", code="12704201")
        api_two_sample.add_species_difference_check_detail(statfile=two_sample_path, cifiles=[ci_path], table_id=self.option('main_id'), level=self.option("level"), check_type='two_sample', params=self.option("params"), group_id=None, from_otu_table=params["otu_id"], major=False, posthoc=None)
        api_two_sample.update_species_difference_check(self.option('main_id'), two_sample_path, ci_path, 'twosample')
        #self.end()
        self.save_pdf()

    def save_pdf(self):
        self.pdf_status = get_save_pdf_status("_".join(self.sheet.id.split('_')[:2]))
        if self.pdf_status:
            name = get_name(self.option("main_id"), "sg_species_difference_check")
            self.figsave = self.add_tool("meta.fig_save")
            self.figsave.on('end', self.end)
            self.figsave.set_options({
                "task_id": "_".join(self.sheet.id.split('_')[:2]),
                "table_id": self.option("main_id"),
                "table_name": name,
                "project": "meta",
                "submit_loc": "species_difference_two_specimen",
                "interaction": 1,
                "main_table": "sg_species_difference_check",
            })
            self.figsave.run()
        else:
            self.end()

    def run(self):
        self.run_two_sample()
        super(TwoSampleWorkflow, self).run()


# coding:utf-8

from __future__ import unicode_literals

from django.db import models
from mongoengine import *

class Tourist(Document):
    id = IntField()           # 站点id
    status = StringField()    # 状态 默认为1 在抓取， 不在抓取0
    tourist_id = IntField()   # 景点id
    task_id = IntField()      # 任务id
    task_name = StringField() # 任务名称
    crawler_name = StringField() #爬虫名字
    crawler_url = StringField() # 爬去的url
    last_time = DateTimeField() # 上次抓取时间
    crawler_grades = IntField() # 抓去等级
    crawler_status = StringField() # 抓取状态 1 在抓取，-1 停止
    lock_status = StringField()  # 是否锁定
    lock_time = IntField() # 锁定时间
    # test_key = StringField(required=True)
    # test_value = StringField(max_length=50)


import sys
# Luhn.py 896588081210001114

# Main function to call other functions.
def main():
    # Get arguements from the command line
    argList = sys.argv
    # Input is passed to validate
    # Valid input passed to split
    # Split input passed to luhn
    # Luhn returns the check digit
    num = luhn(split(validate(argList[1])))
    print("The calculated check digit is: " + str(num))
    print("Add the check digit to the end and re-run the script to check if it is valid (should equal 0)")

# Function to check input is correct
def validate(argStr):
    # Try block
    try:
        # Cast str to int
        argInt = int(argStr)
    # Catch block
    except:
        print("Only enter numerical characters only")
    return argInt

def split(argInt):
    luhnList = list(map(int, str(argInt)))
    return luhnList

def luhn(luhnList):
    counter = 0
    for element in luhnList:
        # If an odd element from the list
        if(counter % 2 ==0) != True: # Odd
            # Multiply by 2
            luhnList[counter] *= 2
            if len(str(luhnList[counter])) > 1:
                # Split the number into its digits
                luhnItem = list(map(int, str(luhnList[counter])))
                # Sum the digits
                luhnList[counter] = luhnItem[0] + luhnItem[1]
        # Increment counter
        counter += 1
    # Add all the digits together
    luhnTotal = sum(luhnList)
    # Multiply by 9
    luhnTotal *= 9
    # Div by 10 and present the remainder(the check digit)
    luhnTotal %= 10
    return luhnTotal

# If the .py is executed directly it will run as a script, otherwise allows
# other .py files to call its functions.
if __name__ == "__main__":
   main()


# -*- coding: utf-8 -*-
"""
@author: Hin C
"""
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pd.read_csv("SPXVIXInput90to18.csv", index_col='Date', )
df.index = pd.to_datetime(df.index)

SPXdata = df['SPX'].values
VIXdata = df['VIX'].values #####   delete this if you are testing other time-series ###############

plt.close('all')
plt.rcParams.update({'font.size': 10})   #### word size in plots
plt.figure(1)
plt.plot(df.index, SPXdata)
result_list = []
VIXfiltered = []
###################   Input Parameters     ###############################################
wind_size = 1000    # the size of rolling-window
bufferPeriod = 75   # restrict the recovery date should be at least 3 months later
err = 5             # A minor discrepancy between starting value of MDD and recovery value
MDD = 20            # the smallest extent of MDD (in %) to be shown
##########################################################################################
k=0   # position of the rollilng window
m = 0 # the m-th MDD found

while k <= len(SPXdata) - wind_size :
    print("window position= %s " %k)

    xs_roll = SPXdata[k:k+wind_size]

    runningmax = np.maximum.accumulate(xs_roll)
    diff = runningmax - xs_roll

    if np.count_nonzero(diff) <2 :
        print("List is empty")
        pass
    else:
        i = np.argmax(diff) # locate the min point
        j = np.argmax(xs_roll[:i]) # locate the max point

        drawdown = (xs_roll[i] - xs_roll[j])/xs_roll[j] *100

        #print(drawdown)
        if abs(drawdown) < MDD:
            pass
        else:
            print("*************************the %s-th MDD*************************" %(m+1))

            MDDstart =  df.index[k+j]
            MDDend   =  df.index[k+i]
            duration = (MDDend - MDDstart).days

            restofseries = SPXdata[k+j+bufferPeriod:-1]  # finding the recov. date 3 month later
            ### allows a minor discrepancy between starting value of MDD and recovery value
            RecoverList = np.where((restofseries> xs_roll[j]- err) & (restofseries < xs_roll[j]+ err ))
            temp = np.array(RecoverList)[0,:]
            if temp.size == 0:   # checking if no recovery date is found?
            ## most probably because it is the last window in the whole time-series
                resulttemp = [MDDstart, xs_roll[j], MDDend, xs_roll[i], duration, drawdown, 0 ,0, 0  ]
            else:
                rec_index = temp.min() # locate the 1st recovery point
                recoveredDate=  df.index[k+j+bufferPeriod+rec_index]
                recov_point = restofseries[rec_index]
                recov_days = (recoveredDate - MDDstart).days
                resulttemp = [MDDstart, xs_roll[j], MDDend, xs_roll[i], duration, drawdown,recoveredDate,recov_days, recov_point ]

            result_list.append(resulttemp)
            m += 1
    k += int(wind_size/4)  # distance to next window inspection
##########################################################################################

#####################  self-design algorithm to filter out many unwanted results #####################
unfiltered = [[1, 1 ] + result_list[0]]   # concatenate

# make 2 new parameters in the front of list
for rowIndex in range(1,len(result_list)):
    StartdaysDifference = (result_list[rowIndex][0] - result_list[rowIndex-1][0]).days
    EnddaysDifference = (result_list[rowIndex][2] - result_list[rowIndex-1][2]).days
    temp_list= [StartdaysDifference,EnddaysDifference]+ result_list[rowIndex]
    unfiltered.append(temp_list)

####      filter all the unwanted MDDs     ####
firstfilter=[]
for row in range(0,len(unfiltered)):
    # eliminate the repeated results,
    # DD with duration less than 10days,
    # recovery period is less than 30 days,
    # recov is earlier than the trough
    if (unfiltered[row][0] == 0 and unfiltered[row][1] == 0) or unfiltered[row][6] < 10 or unfiltered[row][9]<30 or unfiltered[row][4]> unfiltered[row][8]:
        pass
    else:
        firstfilter.append(unfiltered[row])

secondfilter = []
for row in range(0,len(firstfilter)-1):
    if (firstfilter[row][2] == firstfilter[row+1][2] and firstfilter[row][4]  < firstfilter[row+1][4]):
        pass
    else:
        secondfilter.append(firstfilter[row])

secondfilter.append(firstfilter[row+1])
####################################################################

for rowIndex in range(len(secondfilter)):
    [dayDiff,dayDiff2, MDDstart, high, MDDend,low, duration, DDPercent, recoveredDate, recov_days , recov_point] = secondfilter[rowIndex]
    plt.plot([MDDstart, MDDend], [high, low], 'ro--')

    MDDyear, MDDmonth, MDDdate = str(MDDstart.year),  str(MDDstart.month), str(MDDstart.day)
    DDendYear, DDendMonth, DDendDate = str(MDDend.year), str(MDDend.month), str(MDDend.day)

    height_MDD = (-1)**(rowIndex)*(200+ (rowIndex%3)*200 ) # a stupid way to avoid overlapping, need to revise later

    plt.annotate( '%s-%s-%s \n  %s-%s-%s \n %d DD days \n (%.2f %%)' % (MDDyear,MDDmonth,MDDdate, DDendYear,DDendMonth,DDendDate,duration, DDPercent),
             xy=(MDDstart, high), xytext=(MDDstart, high+height_MDD),color='black', arrowprops=dict(facecolor='red', alpha=0.3),)

    if recoveredDate !=0:
        plt.plot([MDDstart, recoveredDate], [ high , recov_point], linestyle = '--',  label='%s-%s-%s (%d RecovDays)'% (str(recoveredDate.year), str(recoveredDate.month), str(recoveredDate.day),recov_days))

plt.title("All Maximum DrawDown of SPX larger than %s %% between 1990 and 2018" % MDD)
plt.legend(loc='best')

###############   delete this if you are testing other time-series ###############
plt.figure(2)
plt.plot(df.index, VIXdata)
for row in range(len(secondfilter)):
    start = secondfilter[row][2]
    VIXstart = df.loc[start]['VIX']
    end = secondfilter[row][4]
    VIXend = df.loc[end]['VIX']
    delta = (VIXend-VIXstart)
    VIXfiltered.append([start, VIXstart,end, VIXend, delta])
    duration = (end - start).days
    plt.plot([start, end], [VIXstart, VIXend], 'ro--')
    MDDyear, MDDmonth, MDDdate = str(start.year),  str(start.month), str(start.day)
    DDendYear, DDendMonth, DDendDate = str(end.year), str(end.month), str(end.day)
    #height_MDD = (-1)**(row)*(20 + (row%3)*5)
    height_MDD = (20 + (row%3)*10)
    plt.annotate( '%s-%s-%s \n  %s-%s-%s \n %d DD days \n (%.2f %%)' % (MDDyear,MDDmonth,MDDdate, DDendYear,DDendMonth,DDendDate,duration, delta),
             xy=(start, VIXstart), xytext=(start, VIXstart+height_MDD),color='black', arrowprops=dict(facecolor='red', alpha=0.3),)

plt.title('Change of VIX during SPX MDD > %s %% between 1990 and 2018' % MDD )

###############   delete this if you are testing other time-series ###############


from collections import defaultdict
t = int(input())

for i in range(t):
    n =  int(input())
    d = defaultdict(int)
    line = input().split()
    arr = []
    for i in range(n):
        arr.append(int(line[i]))

    x = abs(arr[0] - arr[1])
    d[x] += 1
    for i in range(2,n):
        x = abs(arr[0]-arr[i])/i
        d[x] += 1

    keys = list(d.keys())
    diff = 0
    if(len(keys)>2):
        diff = arr[2]-arr[1]

    else:
        max = 0
        for i in range(len(keys)):
            if(d[keys[i]]>max):
                max = d[keys[i]]
                diff = int(keys[i])

    ans = []
    for i in range(1,len(arr)-1):
        x = abs(arr[i] - arr[i-1])
        y = abs(arr[i] - arr[i+1])

        if(x!=diff and y!=diff):
            ans.append(i)

    if(ans==[]):
        x = abs(arr[0]-arr[1])
        y = abs(arr[-1]-arr[-2])

        if(x!=diff):
            ans.append(0)

        elif(y!=diff):
            ans.append(-1)

    if(ans==[]):
        print(*arr)
    else:
        pos = ans[0]

        if(pos == 0):
            arr[pos] = arr[pos+1]-diff

        else:
            arr[pos] = arr[pos-1]+diff

        print(*arr)


import threading
import pickle
import socket
from . import Game
import time

class Room(threading.Thread):
    def __init__(self, id):
        super().__init__()

        self.id = id
        self.users = []

        self.slots = 2
        self.free_slots = 2

        self.ball = Game.Ball()

        self.running = True

    def run(self):

        while self.running:

            if len(self.users) == 2:

                for user in self.users:
                    if not user.running:
                        self.close()
                        continue

                time.sleep(0.1)

                self.ball.movement(self.users[0], self.users[1])

                data0 = {'STATUS': 'POSITION',
                         'DATA': {"ENEMY": {"x": self.users[0].x, 'y': self.users[0].y,
                                            'USERNAME': self.users[0].username},
                                  "BALL": {'x': self.ball.x, 'y': self.ball.y}}}
                data0 = pickle.dumps(data0)

                data1 = {'STATUS': 'POSITION',
                         'DATA': {"ENEMY": {"x": self.users[1].x, 'y': self.users[1].y,
                                            'USERNAME': self.users[1].username},
                                  "BALL": {'x': self.ball.x, 'y': self.ball.y}}}
                data1 = pickle.dumps(data1)

                try:
                    self.users[0].client.send(data1)
                    self.users[1].client.send(data0)
                except socket.error as error:
                    # self.running = False
                    pass

    def close(self):
        msg = {'STATUS': 'CLOSE'}
        msg = pickle.dumps(msg)

        for user in self.users:
            try:
                user.client.send(msg)
            except:
                pass
            user.running = False
            user.client.close()

        self.running = False
        self.join()

class RoomController(threading.Thread):
    def __init__(self, new_clients):
        super().__init__()

        self.running = True
        self.last_room_id = 0

        self.new_clients = new_clients
        self.rooms = []

    def run(self):

        while self.running:

            if not self.new_clients.empty():

                user = self.new_clients.get()

                target_room = None

                for room in self.rooms:
                    if room.running:
                        if room.free_slots > 0:
                            target_room = room

                if target_room is None:
                    target_room = Room(self.last_room_id)
                    self.last_room_id += 1
                    self.rooms.append(target_room)
                    target_room.start()

                data = {"STATUS": "START", 'DATA': {'USER_NR': len(target_room.users), 'ROOM_NR': target_room.id}}
                data = pickle.dumps(data)

                user.client.send(data)

                target_room.users.append(user)
                target_room.free_slots -= 1


#User function Template for python3

class Solution:
    def commonElements (self,a, b, c, n1, n2, n3):
        ans=[]
        i,j,k=0,0,0
        while i<n1 and j<n2 and k<n3:
            if a[i]==b[j] and a[i]==c[k]:       # a[i]=b[j]=c[k]
                if not ans or ans[-1]!=a[i]:    # ans list not empty or last inserted element is
                    ans.append(a[i])            # not equal to present element (to stop redundancy)
                i+=1
                j+=1
                k+=1
            elif a[i]<=b[j] and a[i]<=c[k]:
                i+=1
            elif b[j]<=a[i] and b[j]<=c[k]:
                j+=1
            else:
                k+=1
        return ans

#{
 # Driver Code Starts
#Initial Template for Python 3

t = int (input ())
for tc in range (t):
    n1, n2, n3 = list(map(int,input().split()))
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    C = list(map(int,input().split()))

    ob = Solution()

    res = ob.commonElements (A, B, C, n1, n2, n3)

    if len (res) == 0:
        print (-1)
    else:
        for i in range (len (res)):
            print (res[i], end=" ")
        print ()

# } Driver Code Ends

'''

Given three arrays sorted in increasing order. Find the elements that are common in all three arrays.
Note: can you take care of the duplicates without using any additional Data Structure?

Example 1:

Input:
n1 = 6; A = {1, 5, 10, 20, 40, 80}
n2 = 5; B = {6, 7, 20, 80, 100}
n3 = 8; C = {3, 4, 15, 20, 30, 70, 80, 120}
Output: 20 80
Explanation: 20 and 80 are the only
common elements in A, B and C.

'''


import os
from osrf.app import Application
import osrf.log
import pairtree

class Storage(Application):

    Application.register_method(
        api_name='psu.stewardship.storage.store',
        method='store',
        argc=3,
        stream=False
        )
    def store(self, request, base_location, identifier, source):
        f = pairtree.PairtreeStorageFactory()
        tree_location = os.path.join(base_location, "stewardship")
        pairstore = f.get_store(store_dir=tree_location, uri_base="ark://")
        pairobj = pairstore.create_object(identifier)
        pairobj.add_directory(source)
        request.respond(pairobj.location)

Application.register_app(Storage())


import math

def solution(r1, r2):
    answer = 0
    # 1사분면만 탐색하면서 점 개수 더하고 4 곱하기
    # x축 기준 1부터 r2까지 탐색하면서 큰 원 y값 - 작은 원 y값 사이에 있는 격자점 체크
    for i in range(1, r2 + 1) :
        upper_bound = math.floor((r2**2 - i**2)**0.5)
        lower_bound = math.ceil(max(0,r1**2 - i**2)**0.5)
        answer += upper_bound - lower_bound + 1
    return answer * 4

두 원 사이의 정수 쌍 - 김현우.py


from rest_framework import serializers
from apps.db.video.models import Video

class ListSerializer(serializers.HyperlinkedModelSerializer):
    url = serializers.HyperlinkedIdentityField(view_name="v1:video-detail")
    class Meta:
        model = Video
        fields = ['id', 'url']

class DetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = Video
        fields = ['id', 'video']