import PySimpleGUI as sg

class TelaAluno():
  def __init__(self):
    self.__window = None
    self.__alunos = []

  def init_components(self):
    layout = [
      [sg.Text('Escolha sua opção:')],
      [sg.Text('1 - Cadastrar Aluno', size=(15, 1))],
      [sg.Text('2 - Operações do Aluno', size=(20, 1))],
      [sg.Text('3 - Listar disciplinas', size=(20, 1))],
      [sg.Text('4 - Listar atividades', size=(20, 1))],
      [sg.Text('0 - Voltar', size=(20, 1))],
      [sg.Text('Escolha o desejado:', size=(15, 1)), sg.InputText()],
      [sg.Submit()]
    ]
    self.__window = sg.Window('Tela aluno').Layout(layout)

  def open(self):
    self.init_components()
    values = self.__window.Read()
    return values

  def pega_dados_aluno(self):
    self.close()
    layout = [
      [sg.Text('Escolha os dados do aluno:')],
      [sg.Text('Nome do aluno:', size=(15, 1)), sg.InputText("", key="nome")],
      [sg.Text('Idade do aluno:', size=(15, 1)), sg.InputText("", key="idade")],
      [sg.Submit()]
    ]
    self.__window = sg.Window('Adicionar aluno').Layout(layout)
    values = self.__window.Read()
    self.__window.Close()
    return values[1]

  def seleciona_aluno(self):
    self.close()
    layout = [
      [sg.Text('Escolha o aluno:', size=(20, 1))]
    ]
    for aluno in self.__alunos:
      layout.append([sg.Text(aluno, size=(20, 1))])

    layout.extend([[sg.Text('Qual opção:', size=(15, 1)), sg.InputText(key="opcao")], [sg.Submit()]])

    self.__window = sg.Window('Tela aluno').Layout(layout)
    values = self.__window.Read()
    num = values[1]
    self.__window.Close()
    self.__alunos = []
    return int(num['opcao'])

  def opcoes_aluno(self):
    layout = [
      [sg.Text('Escolha sua opção:')],
      [sg.Text('1 - Excluir aluno', size=(15, 1))],
      [sg.Text('2 - Editar aluno', size=(20, 1))],
      [sg.Text('Qual opção:', size=(15, 1)), sg.InputText(key="opcao")],
      [sg.Submit()]
    ]
    self.__window = sg.Window('Escolher operação').Layout(layout)
    values = self.__window.Read()
    num = values[1]
    self.__window.Close()
    return int(num['opcao'])

  def opcoes_editar(self):
    layout = [
      [sg.Text('O que deseja editar:')],
      [sg.Text('1 - Nome', size=(15, 1))],
      [sg.Text('2 - Idade', size=(15, 1))],
      [sg.Text('Qual opção:', size=(15, 1)), sg.InputText(key="opcao")],
      [sg.Submit()]
    ]
    self.__window = sg.Window('Escolher edição').Layout(layout)
    values = self.__window.Read()
    num = values[1]
    self.__window.Close()
    return int(num['opcao'])

  def novo_campo(self, indicador):
    if (indicador == 1):
      layout = [
        [sg.Text('Qual a nova idade:', size=(15, 1)), sg.InputText(key="opcao")],
        [sg.Submit()]
      ]
      self.__window = sg.Window('Nova idade').Layout(layout)
      values = self.__window.Read()
      valor = values[1]
      self.__window.Close()
      return valor['opcao']
    else:
      layout = [
        [sg.Text('Qual o novo nome:', size=(15, 1)), sg.InputText(key="opcao")],
        [sg.Submit()]
      ]
      self.__window = sg.Window('Novo nome').Layout(layout)
      values = self.__window.Read()
      valor = values[1]
      self.__window.Close()
      return valor['opcao']

  def lista_aluno(self, dados_aluno):
    self.__alunos.append(dados_aluno["opcao"])

  def lista_atividades(self, atividades):
    self.close()
    layout = [
      [sg.Text('Lista de atividades:', size=(20, 1))]
    ]
    for atividade in atividades:
      layout.append([sg.Text(atividade, size=(20, 1))])

    layout.append([sg.Cancel()])

    self.__window = sg.Window('Tela atividades').Layout(layout)
    self.__window.Close()
    self.open()

  def lista_disciplinas(self, disciplinas):
    self.close()
    layout = [
      [sg.Text('Lista de disciplinas:', size=(20, 1))]
    ]
    for disciplina in disciplinas:
      layout.append([sg.Text(disciplina, size=(20, 1))])

    layout.append([sg.Cancel()])

    self.__window = sg.Window('Tela disciplinas').Layout(layout)
    self.open()

  def pega_nome_aluno(self):
    self.close()
    layout = [
      [sg.Text('Qual o nome do aluno:', size=(20, 1)), sg.InputText(key="opcao")],
      [sg.Submit()]
    ]
    self.__window = sg.Window('Qual o nome').Layout(layout)
    values = self.__window.Read()
    valor = values[1]
    self.__window.Close()
    return valor['opcao']

  def close(self):
    self.__window.Close()


# _*_ coding:utf-8 _*_
# author: zizle
# last edited: 2018/10/09
"""
Qt库是最强大的GUI库之一。pyqt5的官方网站http://www.riverbankcomputing.co.uk/news。

pyqt5做为Python的一个模块，它有620多个类和6000个函数和方法。这是一个跨平台的工具包，它可以运行在所有主要的操作系统，包括UNIX，Windows，Mac OS。pyqt5是双重许可。开发者可以在GPL和商业许可之间进行选择。
pyqt5的类别分为几个模块，包括以下：
QtCore
QtGui
QtWidgets
QtMultimedia
QtBluetooth
QtNetwork
QtPositioning
Enginio
QtWebSockets
QtWebKit
QtWebKitWidgets
QtXml
QtSvg
QtSql
QtTest
QtCore:包含了核心的非GUI功能。此模块用于处理时间、文件和目录、各种数据类型、流、URL、MIME类型、线程或进程。
QtGui包含类窗口系统集成、事件处理、二维图形、基本成像、字体和文本。
qtwidgets模块包含创造经典桌面风格的用户界面提供了一套UI元素的类。
QtMultimedia包含的类来处理多媒体内容和API来访问相机和收音机的功能。
Qtbluetooth模块包含类的扫描设备和连接并与他们互动。描述模块包含了网络编程的类。这些类便于TCP和IP和UDP客户端和服务器的编码，使网络编程更容易和更便携。
Qtpositioning包含类的利用各种可能的来源，确定位置，包括卫星、Wi-Fi、或一个文本文件。
Enginio模块实现了客户端库访问Qt云服务托管的应用程序运行时。
Qtwebsockets模块包含实现WebSocket协议类。
QtWebKit包含一个基于Webkit2图书馆Web浏览器实现类。
Qtwebkitwidgets包含的类的基础webkit1一用于qtwidgets应用Web浏览器的实现。
QtXml包含与XML文件的类。这个模块为SAX和DOM API提供了实现。
QtSvg模块提供了显示SVG文件内容的类。可伸缩矢量图形（SVG）是一种描述二维图形和图形应用的语言。
QtSql模块提供操作数据库的类。
QtTest包含的功能，使pyqt5应用程序的单元测试

"""

import sys

from PyQt5.QtWidgets import QApplication, QWidget

if __name__ == '__main__':
    # 创建app对象并接受从命令行输入的参数列表
    app = QApplication(sys.argv)
    # 创建一个window。 QWidget是所有用户界面对象的基类
    window = QWidget()
    # 调整窗口大小,宽X高，单位是px
    window.resize(250, 150)
    # 移动窗口到屏幕的位置，也就是初始想显示的位置, 不指定为显示在屏幕的中间
    # window.move(100, 300)
    # 设置标题
    window.setWindowTitle('这是第一个PyQt窗口')
    # 显示
    window.show()
    # 使用系统exit()确保程序干净退出
    sys.exit(app.exec())

01-简单介绍与开始.py

01-简单介绍与开始.py


import pygame

class Moving_Block:
    """A class to manage the block."""

    def __init__(self, ai_game):
        """Initialize the block and set its starting position."""
        self.screen = ai_game.screen
        self.settings = ai_game.settings
        self.screen_rect = ai_game.screen.get_rect()

        # Load the block image and get its rect.
        self.image = pygame.image.load('images/grayblock.png')
        self.rect = self.image.get_rect()

        # Start each new block at the bottom center of the screen.
        self.rect.midbottom = self.screen_rect.midbottom

        # Store a decimal value for the block's horizontal position.
        self.x = float(self.rect.x)
        # Store a decimal value for the block's vertical position.
        self.y = float(self.rect.y)

        self.moving_block_pos = (self.x, self.y)

        # Movement flags
        self.moving_right = False
        self.moving_left = False

    def update(self):
        """Update the block's position based on movement flags."""
        # Update the block's x value, not the rect.
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.x += self.settings.block_speed
        if self.moving_left and self.rect.left > 0:
            self.x -= self.settings.block_speed

        # Update rect object from self.x.
        self.rect.x = self.x

    def blitme(self):
        """Draw the block at its current location."""
        self.screen.blit(self.image, self.rect)

    def center_block(self):
        """Center the block on the screen"""
        self.rect.midbottom = self.screen_rect.midbottom
        self.x = float(self.rect.x)


"""
E Practical: Agents
Author: Sophie Ray Morrison
"""

import random
import operator
import matplotlib.pyplot
import agentframework

def distance_between(agent_a, agent_b):
    """
    This calculates the distance between agents

    Paramaters
    ----------
    agent_a :   List containing y,x coordinates of agent a
                One of the coordinates
    agent_b :   List containing y,x coordinates of agent b
                One of the coordinates

    Returns
    ------
    distance : Number
    distance between agent_a and agent_b
    """
    return (((agent_a.x - agent_b.x)**2) +
    ((agent_a.y - agent_b.y)**2))**0.5

num_of_agents = 10
num_of_iterations = 100
agents = []

# test connection between model and agent framework
a = agentframework.Agent()
print(a.y, a.x)
# this check worked! printing random values
a.move()
print(a.y, a.x)
# also worked

# making agents
for i in range(num_of_agents):
    agents.append(agentframework.Agent())

# moving agents
for j in range(num_of_iterations):
    for i in range(num_of_agents):
        agents[i].move()

matplotlib.pyplot.xlim(0, 99)
matplotlib.pyplot.ylim(0, 99)
for i in range(num_of_agents):
    matplotlib.pyplot.scatter(agents[i].x,agents[i].y)
matplotlib.pyplot.show()

## distance calculation - not needed?
# for agents_row_a in agents:
#     for agents_row_b in agents:
#         distance = distance_between(agents_row_a, agents_row_b)

# Test to check distance is being calculated correctly
# for this, i changed agents to 2 and iterations to 1
# for agent1 in agents:
#     for agent2 in agents:
#         distance = distance_between(agent1, agent2)
#         print("Printing position of agent 1: ")
#         print(agent1.x, agent1.y)
#         print("Printing position of agent 2: ")
#         print(agent2.x, agent2.y)
#         print("Printing distance: ")
#         print(distance)

# first pair calculates distance of agent 1 from itself
# second pair calculates distance between 1st and 2nd agent
# third pair calculates distance between 2nd and 1st
# last pair calculates distance between agent 2 and agent 2

# Print distance between agents
for agent1 in agents:
    for agent2 in agents:
        distance = distance_between(agent1, agent2)
        print(distance)


from django.shortcuts import render, HttpResponse
from .models import  Customer, Order
from django.contrib.auth.models import User
from .func_utils import find_children

def find_parent(request,pk):
    # find the customer
    customer = Customer.objects.get(pk=pk)
    # get the customer parent
    parent = customer.parent
    # return it
    return HttpResponse(parent)

def find_all_children(request,pk):
    my_html = find_children(request,pk)
    return HttpResponse(my_html)

def customer_total_debt(request,pk):
    customer = Customer.objects.get(pk=pk)
    order_list = list(customer.order_set.all())
    children = list(customer.customer_set.all())
    my_html = f"
-->{customer}
"
    for single_order in list(customer.order_set.all()):
        my_html = my_html + f"
--------{single_order}__{single_order.price}$
"
    for child in children:
        order_list += list(child.order_set.all())
        my_html = my_html +f"
--->{child}
"
        for single_order in list(child.order_set.all()):
            my_html = my_html + f"
--------{single_order}__{single_order.price}$
"

    print(order_list)

    return HttpResponse(my_html)

from django.contrib.auth.forms import UserCreationForm
from .forms import CreateUserCreationForm

def register_page(request):
    form = CreateUserCreationForm()

    if request.method == "POST":
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
    context = {"form":form}
    return render(request, "base/register.html", context)


"""Julia.py: Implementation of the Julia Set as a subclass of Fractal."""

__author__ = ['Jacob Taylor Cassady']
__email__ = ['jacobtaylorcassady@outlook.com']

from typing import List
from numpy import linspace, array, meshgrid, zeros

from Fractal import Fractal

class Julia(Fractal):
    """Julia Set fractal class.

    Args:
        Fractal (class): The Fractal base class."""
    KNOWN_CONSTANTS: List[complex] = [-0.8 + 0.156j, -0.4 + 0.5j,
                                      -0.618 + 0.0j, -0.7 + 0.27015j,
                                      -0.7269 + 0.1889j, -0.835 - 0.2321j]
    def __init__(self, width: int, height: int, xmin: float,
                 xmax:float, ymin: float, ymax: float,
                 constant: complex = -0.835 - 0.2321j) -> None:
        """Constructor for the Julia Set fractal class.

        Args:
            width (int): The width of the fractal.
            height (int): The height of the fractal.
            xmin (float): The minimum x-value for the fractal.
            xmax (float): The maximum x-value for the fractal.
            ymin (float): The minimum y-value for the fractal.
            ymax (float): The maximum y-value for the fractal.
            constant (complex, optional): The complex constant for the Julia Set. Defaults to -0.835-0.2321j."""
        x: array = linspace(xmin, xmax, width)
        y: array = linspace(ymin, ymax, height)
        X, Y = meshgrid(x, y)

        self.constant = constant
        self.z: array = X + 1j * Y
        self.fractal: array = zeros(self.z.shape, dtype=int)

    def iterate(self) -> array:
        """Performs one iteration of the Julia set algorithm.

        Returns:
            array: A 2D NumPy array representing the state of the fractal after the iteration."""
        mask: array = abs(self.z) < 2
        self.z[mask] = self.z[mask] ** 2 + self.constant
        self.fractal += mask
        return self.fractal

if __name__ == '__main__':
    # Set the desired parameters
    width, height = 1000, 1000
    xmin, xmax = -2, 2
    ymin, ymax = -2, 2
    max_iter = 100
    cmap = 'gist_stern'

    julia_test: Julia = Julia(width=width, height=height, xmin=xmin, xmax=xmax,
                              ymin=ymin, ymax=ymax, constant=Julia.KNOWN_CONSTANTS[-1])
    julia_test.animate(iterations=100, cmap=cmap, xmin=xmin, xmax=xmax,
                       ymin=ymin, ymax=ymax)


# -*- coding: utf-8 -*-
"""
Created on Sat Sep  4 15:32:08 2021

@author: iri_0
"""
#Escribí una función tirar() que devuelva una lista con cinco dados generados aleatoriamente. Escribí otra función llamada es_generala(tirada) que devuelve True si y sólo si los cinco dados de la lista tirada son iguales.

import random

def tirar():
    tirada=[]
    for i in range(5):
        tirada.append(random.randint(1,6))
    return tirada

tirada= tirar()

def es_generala(tirada):
    resultado= all(numero==tirada[0] for numero in tirada)
    print(resultado)

es_generala(tirada)

N= 100000
salio_generala_servida= [(es_generala(tirada)) for i in range (N)]
G = sum([es_generala(tirada) for i in range(N)])
prob = G/N
print(f'Tiré {N} veces, de las cuales {G} saqué generala servida.')
print(f'Podemos estimar la probabilidad de sacar generala servida mediante {prob:.6f}.')

#Ejercicio 5.2: Generala no necesariamente servida

import random
import numpy as np
from collections import Counter

def completar_tirada(seleccion):
    '''recibe los datos que se guardaron en una tirada anterior y completa tirando los que falta hasta 5'''
    tirada=seleccion
    while len(tirada) < 5:
        tirada.append(np.random.randint(1,7))
    return tirada

def seleccionar_mejores (tirada):
    '''selecciona los dados que más se repiten'''
    T=Counter(tirada)
    d= T.most_common(1)[0][0] #me de el dado que más se repite
    n= T.most_common(1)[0][1] #cuantas veces se repite

    seleccion= [d]*n #selecciono estos dados para la proxima mano
    return seleccion

# completar_tirada([3,3,3])

N=100000  #vamos a jugar N veces
generalas=0

for j in range (N):
    if j%10000==0:
        print(j)
    seleccion=[]
    for k in range(3):
        tirada= completar_tirada(seleccion)
        seleccion= seleccionar_mejores(tirada)
    if len(seleccion)==5: #opcion2= len(set(seleccion)==1)
        generalas+=1

print(f'la probabilidad de hacer generala es de: {generalas/N}')

#%% Opcion 2: Generala

import random

def tirar(n):
    tirada=[]
    for i in range(n):
        tirada.append(random.randint(1,6))
    return tirada

def es_generala(tirada):
    return len(set(tirada))==1

def repetidos(tirada):
    lista=[]
    for i in range(1,7):
        lista.append(tirada.count(i))
    for j, e in enumerate (lista):
        if e==max(lista):
            res= [j+1]*e
    return res

def mejorar_tirada(tirada):
    rep= repetidos(tirada)
    l= tirar(5-len(rep))
    t= rep + l
    return t

def jugar_generala():
    t= tirar(5)
    t= mejorar_tirada(t)
    t= mejorar_tirada(t)
    return es_generala(t)

N= 100000
G= sum([jugar_generala() for i in range(N)])
prob=G/N
print(f'Tiré {N} veces de las cuales {G} saqué generala')
print(f' la probabilidad de hacer generada es de {prob}')


import copy
import hashlib
import os
import shutil
from datetime import timedelta
from zipfile import ZipFile

from django.conf import settings

from utils.api.tests import APITestCase
from .serializers import ProblemAdminSerializer
from .models import ProblemTag
from .models import Problem
from contest.models import Contest
from contest.tests import DEFAULT_CONTEST_DATA

from .utils import parse_problem_template

DEFAULT_PROBLEM_DATA = {"_id": "A-110", "title": "test", "description": "
test
",
                        "visible": True, "tags": ["test"], "languages": ["C", "C++", "Java", "Python2"], "template": {},
                        "hint": "
test
", "lab_config": {"ttl":233, "packet_size":"32KB", "loss_rate": 0.05},
                        "vm_num": 2, "port_num": [1, 1], "code_num": 3, "share_submission": True}

class ProblemCreateTestBase(APITestCase):
    @staticmethod
    def add_problem(problem_data, created_by):
        data = copy.deepcopy(problem_data)
        total_score = 0
        data["total_score"] = total_score
        data["created_by"] = created_by
        tags = data.pop("tags")
        data["languages"] = list(data["languages"])
        problem = Problem.objects.create(**data)
        for item in tags:
            try:
                tag = ProblemTag.objects.get(name=item)
            except ProblemTag.DoesNotExist:
                tag = ProblemTag.objects.create(name=item)
            problem.tags.add(tag)
        return problem

class ProblemTagListAPITest(APITestCase):
    def test_get_tag_list(self):
        ProblemTag.objects.create(name="name1")
        ProblemTag.objects.create(name="name2")
        resp = self.client.get(self.reverse("problem_tag_list_api"))
        self.assertSuccess(resp)

class ProblemAdminAPITest(APITestCase):
    def setUp(self):
        self.url = self.reverse("problem_admin_api")
        self.create_super_admin()
        self.data = copy.deepcopy(DEFAULT_PROBLEM_DATA)

    def test_create_problem(self):
        resp = self.client.post(self.url, data=self.data)
        self.assertSuccess(resp)
        return resp

    def test_duplicate_display_id(self):
        self.test_create_problem()

        resp = self.client.post(self.url, data=self.data)
        self.assertFailed(resp, "Display ID already exists")

    def test_get_problem(self):
        self.test_create_problem()
        resp = self.client.get(self.url)
        print(resp.data)
        self.assertSuccess(resp)

    def test_get_one_problem(self):
        problem_id = self.test_create_problem().data["data"]["id"]
        resp = self.client.get(self.url + "?id=" + str(problem_id))
        self.assertSuccess(resp)

    def test_edit_problem(self):
        problem_id = self.test_create_problem().data["data"]["id"]
        data = copy.deepcopy(self.data)
        data["id"] = problem_id
        data["vm_num"] = 3
        # resp = self.client.put(self.url, data=data)
        resp = self.client.get(self.url, data)
        print("vm_num:", resp.data)
        self.assertSuccess(resp)

class ProblemAPITest(ProblemCreateTestBase):
    def setUp(self):
        self.url = self.reverse("problem_api")
        admin = self.create_admin(login=False)
        self.problem = self.add_problem(DEFAULT_PROBLEM_DATA, admin)
        self.create_user("test", "test123")

    def test_get_problem_list(self):
        resp = self.client.get(f"{self.url}?limit=10")
        print(resp.data)
        self.assertSuccess(resp)

    def test_get_one_problem(self):
        resp = self.client.get(self.url + "?problem_id=" + self.problem._id)
        print(resp.data)
        self.assertSuccess(resp)

class ContestProblemAdminTest(ProblemCreateTestBase):
    def setUp(self):
        self.url = self.reverse("contest_problem_admin_api")
        self.user = self.create_admin()
        self.problem = self.add_problem(DEFAULT_PROBLEM_DATA, self.user)
        self.contest = self.client.post(self.reverse("contest_admin_api"), data=DEFAULT_CONTEST_DATA).data["data"]
        self.data = {
            "display_id": "1000",
            "contest_id": self.contest["id"],
            "problem_id": self.problem.id,
            "lab_config": {"loss_rate": 0.1, "packet_size": "32MB"}
        }

    def test_create_contest_problem(self):
        resp = self.client.post(self.url, data=self.data)
        self.assertSuccess(resp)
        return resp.data["data"]

    def test_get_contest_problem(self):
        self.test_create_contest_problem()
        self.data["display_id"]="2000"
        self.client.post(self.url, data=self.data)
        contest_id = self.contest["id"]
        resp = self.client.get(self.url + "?contest_id=" + str(contest_id))
        print(resp.data)
        self.assertSuccess(resp)
        self.assertEqual(len(resp.data["data"]["results"]), 2)

    def test_get_one_contest_problem(self):
        contest_problem = self.test_create_contest_problem()
        contest_id = self.contest["id"]
        problem_id = contest_problem["id"]
        resp = self.client.get(f"{self.url}?contest_id={contest_id}&id={problem_id}")
        self.assertSuccess(resp)

class ContestProblemTest(ProblemCreateTestBase):
    def setUp(self):
        admin = self.create_admin()
        url = self.reverse("contest_admin_api")
        contest_data = copy.deepcopy(DEFAULT_CONTEST_DATA)
        contest_data["password"] = ""
        contest_data["start_time"] = contest_data["start_time"] + timedelta(hours=1)
        self.contest = self.client.post(url, data=contest_data).data["data"]
        self.problem = self.add_problem(DEFAULT_PROBLEM_DATA, admin)
        self.problem.contest_id = self.contest["id"]
        self.problem.save()
        self.url = self.reverse("contest_problem_api")

    def test_admin_get_contest_problem_list(self):
        contest_id = self.contest["id"]
        resp = self.client.get(self.url + "?contest_id=" + str(contest_id))
        self.assertSuccess(resp)
        self.assertEqual(len(resp.data["data"]), 1)

    def test_admin_get_one_contest_problem(self):
        contest_id = self.contest["id"]
        problem_id = self.problem._id
        resp = self.client.get("{}?contest_id={}&problem_id={}".format(self.url, contest_id, problem_id))
        print(resp.data)
        self.assertSuccess(resp)

    def test_regular_user_get_not_started_contest_problem(self):
        self.create_user("test", "test123")
        resp = self.client.get(self.url + "?contest_id=" + str(self.contest["id"]))
        contest = Contest.objects.get(id=self.contest["id"])
        print(contest.status)
        print(resp.data)
        self.assertDictEqual(resp.data, {"error": "error", "data": "Contest has not started yet."})

    def test_reguar_user_get_started_contest_problem(self):
        self.create_user("test", "test123")
        contest = Contest.objects.first()
        contest.start_time = contest.start_time - timedelta(hours=1)
        contest.save()
        resp = self.client.get(self.url + "?contest_id=" + str(self.contest["id"]))
        print(resp.data)
        self.assertSuccess(resp)

class AddProblemFromPublicProblemAPITest(ProblemCreateTestBase):
    def setUp(self):
        admin = self.create_admin()
        url = self.reverse("contest_admin_api")
        contest_data = copy.deepcopy(DEFAULT_CONTEST_DATA)
        contest_data["password"] = ""
        contest_data["start_time"] = contest_data["start_time"] + timedelta(hours=1)
        self.contest = self.client.post(url, data=contest_data).data["data"]
        self.problem = self.add_problem(DEFAULT_PROBLEM_DATA, admin)
        self.url = self.reverse("add_contest_problem_from_public_api")
        self.data = {
            "display_id": "1000",
            "contest_id": self.contest["id"],
            "problem_id": self.problem.id,
            "lab_config": {"loss_rate": 0.1, "packet_size": "32MB"}
        }

    def test_add_contest_problem(self):
        resp = self.client.post(self.url, data=self.data)
        for problem in Problem.objects.all():
            print(ProblemAdminSerializer(problem).data)
        self.assertSuccess(resp)
        self.assertTrue(Problem.objects.all().exists())
        self.assertTrue(Problem.objects.filter(contest_id=self.contest["id"]).exists())

class ParseProblemTemplateTest(APITestCase):
    def test_parse(self):
        template_str = """
//PREPEND BEGIN
aaa
//PREPEND END

//TEMPLATE BEGIN
bbb
//TEMPLATE END

//APPEND BEGIN
ccc
//APPEND END
"""

        ret = parse_problem_template(template_str)
        self.assertEqual(ret["prepend"], "aaa\n")
        self.assertEqual(ret["template"], "bbb\n")
        self.assertEqual(ret["append"], "ccc\n")

    def test_parse1(self):
        template_str = """
//PREPEND BEGIN
aaa
//PREPEND END

//APPEND BEGIN
ccc
//APPEND END
//APPEND BEGIN
ddd
//APPEND END
"""

        ret = parse_problem_template(template_str)
        self.assertEqual(ret["prepend"], "aaa\n")
        self.assertEqual(ret["template"], "")
        self.assertEqual(ret["append"], "ccc\n")


# Note: Download gist as 'emojis.json'
# https://gist.github.com/oliveratgithub/0bf11a9aff0d6da7b46f1490f86a71eb

import json

raw_emojis = []
emotes = []
discord_emote = []

get_discord_emote = {}

print("Grabbing emojis...")
with open("emojis.json", encoding='utf-8') as emojis:
    data = json.load(emojis)
    for emoji in data['emojis']:
        # print(emoji['name'] + ": " + emoji['shortname'])
        raw_emojis.append(emoji['emoji'])
        emotes.append(emoji['shortname'])
        discord_emote.append(emoji['shortname'])

        get_discord_emote[emoji['name']] = emoji['shortname']
print("Created emote list!")

print(discord_emote)


from django.urls import path
from . import views

app_name = 'shelves'

urlpatterns = [
    path('', views.get_shelves_list, name='get_shelves_list'),
    path('add_new_shelf/', views.add_new_shelf, name='add_new_shelf'),
    path('modify_the_shelf/<int:shelf_id>/', views.modify_the_shelf, name='modify_the_shelf'),
    path('get_shelf_sensors/<int:shelf_id>/', views.get_shelf_sensors, name='get_shelf_sensors'),
    path('get_shelf_relays/<int:shelf_id>/', views.get_shelf_relays, name='get_shelf_relays'),
]


from picamera.array import PiRGBArray
from picamera import PiCamera
import cv2
import time
import io

camera = PiCamera()
camera.awb_mode='off'
camera.awb_gains=(1.0,1.2)
camera.resolution =(640 ,480)
camera.iso = 400
camera.framerate = 10
rawCapture = PiRGBArray(camera,size=(640,480))
#file
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('./video_car_12.h264',fourcc,10,(640 ,480))
#warm up
time.sleep(1)
for frame in camera.capture_continuous(rawCapture,format="bgr",use_video_port=True):
    image = frame.array
    cv2.imshow("Frame",image)
    key=cv2.waitKey(1) & 0xFF
    out.write(image)
    rawCapture.truncate(0)
    if key == ord("q"):
        break


import pygame

class Piece:
    def __init__(self, row, col, color):
        self.PADDING = 10

        self.row = row
        self.col = col
        self.color = color
        self.king = False
        self.x = 0
        self.y = 0
        self.set_xy()

    def set_xy(self):
        self.x = 100 * self.col + 100 // 2
        self.y = 100 * self.row + 100 // 2

    def draw_piece(self, Window):
        radius = 100//2 - self.PADDING
        pygame.draw.circle(Window, self.color, (self.x, self.y), radius)
        if self.king:
            pygame.draw.circle(Window, (150, 0, 255), (self.x, self.y), radius-25)

    def move_piece(self, row, col):
        self.row = row
        self.col = col
        self.set_xy()

    def make_king(self):
        self.king = True


# Importamos librerias
import cv2

# Guardamos en variables los pesos, la arquitectura y las clases del modelo previamente entrenado
model = 'frozen_inference_graph.pb'
config = 'ssd_mobilenet_v2_coco_2018_03_29.pbtxt'
clases = 'coco_labels.txt'

# Extraemos las etiquetas del archivo
with open(clases) as cl:
    labels = cl.read().split("\n")
print(labels)

# Leemos el modelo con TensorFlow
net = cv2.dnn.readNetFromTensorflow(model, config)

# Creamos una función de detección
def object_detect(net, img):
    # Dimensiones
    dim = 300

    # Preprocesamos nuestra imagen
    blob = cv2.dnn.blobFromImage(img, 1.0, size = (dim, dim), mean = (0,0,0), swapRB = True, crop = False)

    # Pasamos nuestra imagen preprocesada a la red
    net.setInput(blob)

    # Extraemos los objetos detectados
    objetos = net.forward()

    return objetos

# Creamos una función de mostrar texto
def text(img, text, x, y):
    # Extraemos el tamaño del texto
    sizetext = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 1)
    # Extraemos el tamaño
    dim = sizetext[0]
    baseline = sizetext[1]

    # Creamos un rectangulo negro con el tamaño apropiado
    cv2.rectangle(img, (x, y-dim[1] - baseline), (x + dim[0], y + baseline), (0,0,0), cv2.FILLED)
    # Mostramos el texto
    cv2.putText(img, text, (x, y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,255,0), 1)

# Creamos una función de mostrar objetos
def dibujar_objetos(img, objects, umbral = 0.5):

    # Extraemos info
    filas = img.shape[0]
    colum = img.shape[1]

    # Para los objetos detectados
    for i in range(objects.shape[2]):
        # Buscamos su clase y confianza de detección
        clase = int(objects[0, 0, i, 1])
        puntaje = float(objects[0, 0, i, 2])

        # Extraemos sus coordenadas y las normalizamos a pixeles
        x = int(objects[0, 0, i, 3] * colum)
        y = int(objects[0, 0, i, 4] * filas)
        w = int(objects[0, 0, i, 5] * colum - x)
        h = int(objects[0, 0, i, 6] * filas - x)

        # Revisamos si superamos el umbral
        if puntaje > umbral:
            # Mostramos la clase
            text(img, "{}".format(labels[clase]), x, y)
            cv2.rectangle(img, (x, y), (x + w, y + h), (0,255,0), 2)

# Creamos la Video Captura
cap = cv2.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)

# Creamos un ciclo para ejecutar nuestros Frames
while True:
    # Leemos los fotogramas
    ret, frame = cap.read()

    # Realizamos las detecciones
    detect = object_detect(net, frame)

    # Mostramos las detecciones
    dibujar_objetos(frame, detect)

    # Mostramos los Frames
    cv2.imshow("VIDEO CAPTURA", frame)

    # Cerramos con lectura de teclado
    t = cv2.waitKey(1)
    if t == 27:
        break

# Liberamos la VideoCaptura
cap.release()
# Cerramos la ventana
cv2.destroyAllWindows()


import datetime, pymongo, os, glob

#path = '/home/cyber/Desktop/files/*'
path = '//share_ip/computers/username/*.*'
def conn_db():
	myclient = pymongo.MongoClient("mongodb://localhost:27017/")
	#db = myclient.test
	upload_db = myclient.my_db
	create_data = upload_db.user_data
	return create_data

def close_conn():
	myclient = pymongo.MongoClient("mongodb://localhost:27017/")
	myclient.close()

def create_db():
	create_data = conn_db()
	users = new_users_list()
	insert_data = create_data.insert_many(users)
	return create_db

def create_users_list():
	users = []
	for filename in glob.glob(os.path.join(path)):
		with open(filename, "r") as file:
			for line in file:
				s=line.split()
				if len(s) != 3:
					users.append({'comp_name':s[0].lower(), 'u_name':s[1].lower(), 's_name':None, 'ip':None})
				else :
					users.append({'comp_name':s[0].lower(), 'u_name':s[1].lower(), 's_name':None, 'ip':s[2][:-1]})
	return users

def new_users_list():
	new_users = create_users_list()
	for s in new_users:
		if '.' in s['u_name']:
			a=s['u_name'].split('.')
			s.update(u_name=a[0], s_name=a[1])
		else:
			s.update(s_name=None)
	return new_users

def drop_user_data():
	create_data = conn_db()
	create_data.drop()

def check():
	create_data = conn_db()
	db_id = create_data.find_one()
	if db_id == None:
		create_db()
	gen_time = create_data.find_one()['_id'].generation_time
	return gen_time

def user_search():
	try:
		user_data = conn_db()
		while True:
			user_input = input('Enter username for search: ').lower().split(' ')
			first = user_input[0]
			if 'close' in user_input:
				print('\nProgram closed...')
				break
			elif first == 'c':
				comp = input('Enter compname for search:').strip().lower()
				comp_find = {'comp_name':{'$regex':"^"+comp}}
				query_handle(comp_find)
				if len(user_input) == 1:
					user_find = {'u_name':{'$regex':"^"+user_input[0]}}
				else:
					user_find = {'u_name':{'$regex':"^"+user_input[0]},'s_name':{'$regex':"^"+user_input[1]}}
				query_handle(user_find)
	except KeyboardInterrupt:
		print('\nProgram closed...')

def query_handle(user_needs):
	user_data = conn_db()
	for y in user_data.find(user_needs):
		name = y.get('u_name')
		sname = y.get('s_name')
		if sname == None:
			sname = ' '
		comp = y.get('comp_name')
		ip = y.get('ip')
		print('*'*55)
		print((name+'.'+sname).ljust(20),comp.center(20),ip)
	print('='*55)

def test():
	day = str(datetime.date.today())
	gen_time = check()
	if day not in str(gen_time):
		print('dropping')
		drop_user_data()
		create_db()
	user_search()
	close_conn()
test()


#workermodel
import random

class Worker:

    def __init__(self, wid, classification, properties):
    # do not change input parameters - it is used for initialize a worker
        self.wid = wid
        self.classification = classification
        self.properties = properties
        self.task = None # it will be assigned by task assignment strategy

    def execute(self, worker_manager, task_manager, data):
    # must have this function
    # this function returns the actual execution time
        for du in self.task.data_units:
            for i in range(len(du.data_row) - 1):
                n1 = du.data_row[i]
                n2 = du.data_row[i+1]
                steps = int(n1.get_distance(n2) / 5)
                for j in range(steps):
                    lat = n1.lat + (n2.lat-n1.lat)/steps*j
                    lng = n1.lng + (n2.lng-n1.lng)/steps*j
                    trees = data.tf.find_trees(lat, lng)
                    for tree in trees:
                        if random.random() < self.properties["accuracy"]: # it can be set by worker properties on web
                            data.res.append(tree)
        return self.properties["execution_time"]

    def submit(self):
    # must have this function
        if self.task is None:
            return
        for du in self.task.data_units:
            du.add_judgement(self, self.task, du)

#taskmodel
class Task:

    def __init__(self, tid, data_units):
    # do not change input parameters - it is used for initialize a task
        self.tid = tid
        self.data_units = data_units
        self.properties = {} # configurable

#workerslt
def worker_seletion(worker):
# do not change input parameters - it returns if a worker is qualified (true)
    return True

#taskgen
def task_generation(data, n_data_rows):
# do not change input parameters - it returns data_units for creating a new task
    data_units = data.random_selection(n_data_rows)
    return data_units
#taskassign

def assign(worker_manager, task_manager, data):
    worker = wm.return_upcoming_worker()
    task = tm.return_upcoming_task()
    return (worker, task)

#others

import xml.dom.minidom
import matplotlib.pyplot as plt
import math

class Tree:
    def __init__(self, id, category, lat, lng):
        self.category = category
        self.lat = lat
        self.lng = lng
        self.id = id

class TreeFinder:

    def __init__(self, filename, initial_trees = [], grid_size = 5):
        self.tree_finder = []
        self.trees = initial_trees
        self.count = 0
        self.max_lat = -1e99
        self.min_lat = 1e99
        self.max_lng = -1e99
        self.min_lng = 1e99
        self.grid_size = grid_size
        tid = len(self.trees)
        if filename is not None and filename != "":
            for line in open(filename,'r'):
                temp = line.split(';')
                tree = Tree(tid, temp[0],float(temp[1]),float(temp[2]))
                tid += 1
                self.trees.append(tree)
        for tree in self.trees:
            self.max_lat = max(self.max_lat, tree.lat)
            self.min_lat = min(self.min_lat, tree.lat)
            self.max_lng = max(self.max_lng, tree.lng)
            self.min_lng = min(self.min_lng, tree.lng)
        w = LatLng(self.min_lat, self.min_lng).get_xy(LatLng(self.min_lat, self.max_lng)).x
        h = LatLng(self.min_lat, self.min_lng).get_xy(LatLng(self.max_lat, self.min_lng)).y
        self.n_lat = int(h / grid_size) + 1
        self.n_lng = int(w / grid_size) + 1
        self.k_lat = int((self.n_lat-1)/(self.max_lat-self.min_lat))
        self.k_lng = int((self.n_lng-1)/(self.max_lng-self.min_lng))
        for i in range(self.n_lat):
            self.tree_finder.append([])
            for j in range(self.n_lng):
                self.tree_finder[i].append([])
        self.tree_vis = [False for tree in self.trees]
        for tree in self.trees:
            self.add_tree(tree)

    def add_tree(self, tree):
        i = int((tree.lat - self.min_lat) * self.k_lat)
        j = int((tree.lng - self.min_lng) * self.k_lng)
        self.tree_finder[i][j].append(tree)
        self.count += 1

    def find_trees(self, lat, lng, radius = 5):
        trees = []
        grid_range = int((radius-0.1)/self.grid_size) + 1
        i = int((lat - self.min_lat) * self.k_lat)
        j = int((lng - self.min_lng) * self.k_lng)
        for p in range(i-grid_range,i+grid_range+1):
            for q in range(j-grid_range,j+grid_range+1):
                if 0 <= p and p < self.n_lat and 0<=q and q < self.n_lng:
                    for tree in self.tree_finder[p][q]:
                        if LatLng(lat,lng).get_distance(LatLng(tree.lat,tree.lng)) < radius:
                            trees.append(tree)
        return trees

class Cartesian:

    def __init__(self, x, y):
        self.x = x
        self.y = y

class LatLng:

    def __init__(self, lat, lng):
        self.lat = lat
        self.lng = lng

    def get_latlng(self, x, y):
        return LatLng(self.lat + y/111300.0, self.lng + x/111300.0/math.cos(self.lat/180.0*math.pi))

    def get_xy(self, latlng):
        return Cartesian((latlng.lng - self.lng) * 111300.0 * math.cos(self.lat/180.0*math.pi), (latlng.lat - self.lat) * 111300.0)

    def get_distance(self,latlng):
        p = self.get_xy(latlng)
        return math.sqrt(p.x*p.x + p.y*p.y)

class RoadNetwork:

    def __init__(self, xml_filename):
        DOMTree = xml.dom.minidom.parse(xml_filename)
        self.roads = []
        root = DOMTree.documentElement
        xml_roads = root.getElementsByTagName("road")
        i = 0
        for xml_road in xml_roads:
            coords = []
            nodes = xml_road.getElementsByTagName("node")
            for node in nodes:
                coords.append(LatLng(float(node.getAttribute('lat')),float(node.getAttribute('lng'))) )
            self.roads.append(coords)
            i += 1

    def plot_map(self):
        plt.switch_backend('Agg')   # run matplotlib at the backend
        for road in self.roads:
            ndx = []
            ndy = []
            for node in road:
                ndy.append(node.lat)
                ndx.append(node.lng)
            plt.plot(ndx, ndy, lw = 0.5, c = [0.3,0.3,0.3])

def init(instance, worker_manager, task_manager, data):
    rn = RoadNetwork("project/example/road_network.xml")
    tf = TreeFinder("project/example/trees.csv")
    for r in rn.roads:
        data.add_data(r)
    data.rn = rn
    data.tf = tf
    data.res = []

def output(instance, worker_manager, task_manager, data):
    dict = {}
    n = 0
    for du in data.data_units:
        n += len(du.judgements)
    dict["cost"] = n * data.price
    dict["num_of_workers"] = len(worker_manager.workers)
    return dict

def final(instance, worker_manager, task_manager, data):
    plt.figure()
    data.rn.plot_map()
    px = []
    py = []
    for tree in data.res:
        px.append(tree.lng)
        py.append(tree.lat)
    plt.scatter(px,py,c=[0.3,0.8,0.3],marker='o',s=1)
    plt.savefig(instance.fig_location("tree_map"))


from flask import Flask, jsonify
from flask import Flask, request, jsonify
from sklearn.externals import joblib
import sys
import traceback
import pandas as pd
import numpy as np
app = Flask(__name__)
#@app.route('/predict', methods=['POST'])
@app.route('/predict', methods= ['POST'])

def predict():
     json_ = request.get_json(force= True)
     #print(json_)
     data_df = pd.DataFrame(json_)
     result = xgb_model.predict(data_df)

     # send back to browser
     output = result.tolist()  #use .tolist to jsonify the list
     #int(result[0]) #{'results': int(result[0])}  #return 1 prediction at a time

     # return data
     return jsonify(results=output)

if __name__ == '__main__':
    try:
        port = int(sys.argv[1]) # This is for a command-line argument
    except:
        port = 12345 # If you don't provide any port then the port will be set to 12345
    xgb_model = joblib.load('test_finalized_random_forest_iris_model.pkl') # Load "model.pkl"
    print ('Model loaded')
    model_columns = joblib.load('model_columns.pkl') # Load "model_columns.pkl"
    print ('Model columns loaded')
    app.run(port=port, debug=True)


from collections import Counter
from string import punctuation, whitespace
import sys
import re
from pathlib import Path

def most_common_str(s, n=None):
    # str.maketrans(arg1, arg2, arg3) function creates a rule for translation
    # str is string class, arg1 => string to be replaced, arg2 => string replaced with, arg3 => remove the string
    # string_obj.translate() performs the actual translation
    # punctuation refers to the any of this group of characters !"#$%&'()*+, -./:;<=>?@[\]^_`{|}~
    words = s.lower().translate(str.maketrans('', '', punctuation)).split()
    print(words[:10])
    return Counter(words).most_common(n)

def most_common_re(s, n=None):
    # [^{}{}] - Negate all the characters within the pattern
    # ^ - Negate character, rest of the characters is selected
    # Counter function is applied on the result
    return Counter(re.findall(rf'[^{punctuation}{whitespace}]+',
                              s.lower())).most_common(n)

def most_common_iter(s, n=None):
    # Remove the punctuation in all the words, passing through each character of a word
    # apply join function on the remaining words after removing punctuation character
    return Counter(''.join(c for c in w if c not in punctuation)
                   for w in s.lower().split()).most_common(n)

if __name__ == "__main__":
    try:
        file = sys.argv[1]
    except IndexError:
        p_path = Path.cwd().parent
        f_path = p_path/'data_files/harry.txt'
        file = f_path

    with open(file, encoding='utf8') as f:
        common_words = most_common_str(f.read(), n=20)

    for word, count in common_words:
        print('{:<4} {}'.format(count, word))

    with open(file, encoding='utf8') as f:
        print(list(most_common_re(f.read(), n=10)))


from datetime import datetime, timedelta
from flask import request, Blueprint, current_app
from api.schema.user import UserSchema
from api.model.user import User
import jwt
from api.common.error_handling import ObjectNotFound, ObjectAlreadyExists, BadRequest
from marshmallow import ValidationError

user_bp = Blueprint('user_bp', __name__)
user_schema = UserSchema()

@user_bp.route("/users/login", methods=['POST'])
def login():
    user_json = request.get_json()
    try:
        user_schema.load(user_json)
    except ValidationError as e:
        raise BadRequest('Invalid data to login')
    user = User.login(user_json['email'], user_json['password'])
    if user:
        try:
            token = jwt.encode(
                {"user_id": user.id,
                 "exp": datetime.utcnow() + timedelta(hours=1)},
                current_app.config["SECRET_KEY"],
                algorithm="HS256"
            )
            return {
                "token": token,
                "message": "Successfully fetched auth token"
            }
        except Exception as e:
            return {
                "error": "Something went wrong",
                "message": str(e)
            }, 500
    else:
        raise ObjectNotFound('User not found')

@user_bp.route("/users", methods=['POST'])
def add_user():
    try:
        user = user_schema.load(request.get_json())
    except ValidationError as e:
        raise BadRequest('Invalid data to create a user')
    if user.create(user):
        return {'message': 'User created successfully. '
                           'Use your email and password at /users/login to login to the API'}, 201
    else:
        raise ObjectAlreadyExists('User already exists')


from .abc import MemberBase, ChannelBase, DBAPIBase, ServerBase
from .permissions import ServerPermissions, Permission, ChannelPermissions
from .channel import TextChannel
from .payload import Payload
from typing import Sequence

class Server(ServerBase):
    database: DBAPIBase = None

    def __init__(self,
                 sid: int,
                 name: str,
                 icon: str = None,
                 default_permissions: int = Permission.DEFAULT_SERVER_PERMISSIONS):
        self.id = sid
        self.name = name
        if icon is None:
            icon = f"https://via.placeholder.com/350x350?text={self.name}"
        self.icon = icon
        self.default_permissions = ServerPermissions(default_permissions)
        self.owner = None
        self.channels = {}

    async def load_channels(self,):
        if Server.database is not None:
            channel_data = await Server.database.query(
                Server.database.queries["SELECT_JOIN"].format(
                    columns="channel.id, channel.name, channel.type",
                    table1="server_channels",
                    table2="channel ON channel.id=server_channels.channel_id",
                    where="server_channels.server_id=$1::bigint"
                ),
                (self.id,)
            )
            for i in channel_data:
                channel = None
                if i["type"] == "text":
                    channel = TextChannel(i["id"], i["name"], self)
                if channel is not None:
                    self.channels[i["id"]] = channel

    async def load_client_members(self, member_ids: Sequence[int] = None) -> Sequence[MemberBase]:
        if Server.database is not None:
            server_members_data = []
            if member_ids:
                server_members_data = await Server.database.query(
                    Server.database.queries["SELECT_WHERE"].format(
                        table="server_members",
                        where="server_id=$1::bigint AND member_id IN ({})".format(
                            ",".join(member_ids)
                        )
                    ),
                    (self.id,)
                )
            else:
                server_members_data = await Server.database.query(
                    Server.database.queries["SELECT_WHERE"].format(
                        table="server_members",
                        where="server_id=$1::bigint"
                    ),
                    (self.id,)
                )
            members_ids = [x["member_id"] for x in server_members_data]
            members = []
            for x in members_ids:
                if x in Server.database.clients["id"]:
                    members.append(Server.database.clients["id"][x]["member"])
            return members

    @property
    def gateway_format(self,):
        result = {
            "id": self.id,
            "name": self.name,
            "icon": self.icon,
        }
        return result

    async def broadcast(self, payload: Payload) -> None:
        members = await self.load_client_members()
        permissions = {}
        print(payload)
        print(type(payload))
        if payload.payload_type == "message":
            required = ChannelPermissions.VIEW_CHANNEL
            channel = payload.data["channel"]
            default = channel["default_permissions"]
            permissions["channel"] = channel
            permissions["required"] = required
            permissions["default"] = (default & required) == required
        for member in members:
            if payload.payload_type == "message":
                default = permissions["default"]
                channel = permissions["channel"]
                required = permissions["required"]
                user_permissions = member.permissions["channel"].get(channel["id"], 0)
                user_permissions = (user_permissions & required) == required
                if not default and not user_permissions:
                    continue
            await member.send_via_client(payload)

    async def multicast(self, payload: Payload, member_ids: Sequence[int] = None) -> None:
        members = await self.load_client_members(member_ids)
        for member in members:
            await member.send_via_client(payload)

    async def create_channel(self, name, channel_type) -> ChannelBase:
        channel = await Server.database.create_server_channel(name, channel_type, self)
        payload = Payload("channel", channel.gateway_format, "new")
        await self.broadcast(payload)


import os
import sys
from info_nce import InfoNCE
infonce_loss_fct = InfoNCE(temperature=0.1, reduction='mean', negative_mode='paired')

import torchmetrics
from torchmetrics.text.bert import BERTScore
from torchmetrics import BLEUScore
from pprint import pprint

import evaluate
m_bert = evaluate.load("bertscore")
m_bleu = evaluate.load("bleu")

# file access and data structures
import json
import csv
import pandas as pd
from typing import Dict, List
import marisa_trie

# math/stats and operations
import numpy as np
from statistics import mean
import math
import random
import copy
from tqdm import tqdm

# ML libs
import nltk
nltk.download('punkt')
from nltk.tokenize import sent_tokenize

import torch
import torch.nn.functional as F
from torch import nn

from sklearn.model_selection import train_test_split
from transformers import (
    AdamW,
    get_linear_schedule_with_warmup,
    TextDataset,
    DataCollatorForLanguageModeling,
    pipeline,
    Trainer,
    TrainingArguments,
    BartForConditionalGeneration,
    BartTokenizer,
    BartTokenizerFast,
    RobertaTokenizer,
    RobertaForSequenceClassification,
    T5ForConditionalGeneration,
    T5Tokenizer,
    T5TokenizerFast,

)

# set device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")  # Uses GPU if available

def set_seed(seed = 42):
    """
    Set the random seed for reproducibility
    """
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed_all(seed)

class MarisaTrie(object):
    def __init__(
        self,
        sequences: List[List[int]] = [],
        cache_fist_branch=True,
        max_token_id=256001,
    ):

        self.int2char = [chr(i) for i in range(min(max_token_id, 55000))] + (
            [chr(i) for i in range(65000, max_token_id + 10000)]
            if max_token_id >= 55000
            else []
        )
        self.char2int = {self.int2char[i]: i for i in range(max_token_id)}

        self.cache_fist_branch = cache_fist_branch
        if self.cache_fist_branch:
            self.zero_iter = list({sequence[0] for sequence in sequences})
            assert len(self.zero_iter) == 1
            self.first_iter = list({sequence[1] for sequence in sequences})

        self.trie = marisa_trie.Trie(
            "".join([self.int2char[i] for i in sequence]) for sequence in sequences
        )

    def get(self, prefix_sequence: List[int]):
        if self.cache_fist_branch and len(prefix_sequence) == 0:
            return self.zero_iter
        elif (
            self.cache_fist_branch
            and len(prefix_sequence) == 1
            and self.zero_iter == prefix_sequence
        ):
            return self.first_iter
        else:
            key = "".join([self.int2char[i] for i in prefix_sequence])
            return list(
                {
                    self.char2int[e[len(key)]]
                    for e in self.trie.keys(key)
                    if len(e) > len(key)
                }
            )

    def __iter__(self):
        for sequence in self.trie.iterkeys():
            yield [self.char2int[e] for e in sequence]

    def __len__(self):
        return len(self.trie)

    def __getitem__(self, value):
        return self.get(value)

def shift_tokens_right(input_ids, pad_token_id, decoder_start_token_id):
    """
    Shift input ids one token to the right.
    """
    shifted_input_ids = input_ids.new_zeros(input_ids.shape)
    shifted_input_ids[:, 1:] = input_ids[:, :-1].clone()
    shifted_input_ids[:, 0] = decoder_start_token_id

    if pad_token_id is None:
        raise ValueError("self.model.config.pad_token_id has to be defined.")
    # replace possible -100 values in labels by `pad_token_id`
    shifted_input_ids.masked_fill_(shifted_input_ids == -100, pad_token_id)

    return shifted_input_ids

def save_model(Generator, gen_optimizer, i):
    SAVE_FILE = 'Multimodal-Graph-Script-Learning/model' + MODEL_NAME + '_' + str(i) + 'epochs.tar'
    print("saving model to:", SAVE_FILE)
    torch.save({
            'generator_state_dict': Generator.state_dict(),
            'generator_optimizer_state_dict': gen_optimizer.state_dict(),
            },
           SAVE_FILE)

def gen_train_1_batch(Generator, gen_optimizer, tokenizer, task_prefix, batch_input_sequences, batch_output_sequences, mini_batch_size=2, accum_iter = 16):

    max_length = 1024
    loss_fn = nn.BCEWithLogitsLoss()

    for i in range(accum_iter):
        # get mini_batch
        start = i*mini_batch_size
        end = (i+1)*mini_batch_size
        input_sequences = batch_input_sequences[start:end]
        output_sequences = batch_output_sequences[start:end]

        # encode input data
        source_encoding = tokenizer(
            # [task_prefix + sequence for sequence in input_sequences],
            input_sequences,        # do not use task prefix
            padding="longest",
            max_length=max_length,
            truncation=True,
            return_tensors="pt",
        )
        input_ids, attention_mask = source_encoding.input_ids, source_encoding.attention_mask

        # encode target output
        target_encoding = tokenizer(
            [sequence for sequence in output_sequences],
            padding="longest",
            max_length=max_length,
            truncation=True,
            return_tensors="pt",
        )
        labels = target_encoding.input_ids

        # move everything to gpu
        input_ids=input_ids.cuda()
        attention_mask=attention_mask.cuda()
        labels=labels.cuda()

        # forward pass
        out_with_labels = Generator(
            input_ids=input_ids,
            attention_mask=attention_mask,
            labels=labels,
            output_hidden_states=True,
        )
        generator_loss_on_ground_truth = out_with_labels.loss

        loss = generator_loss_on_ground_truth / accum_iter

        loss.backward()

        if USE_WANDB == True:
            wandb.log({"generator_loss": loss})

    gen_optimizer.step()
    gen_optimizer.zero_grad()

def contrastive_train_1_batch(Generator, gen_optimizer, tokenizer, task_prefix, batch_input_sequences, batch_output_sequences, batch_contrastive_sequences, mini_batch_size=2, accum_iter = 16):

    max_input_length = 1024
    max_output_length = 512
    loss_fn = nn.BCEWithLogitsLoss()

    for i in range(accum_iter):
        # get mini_batch
        start = i*mini_batch_size
        end = (i+1)*mini_batch_size
        input_sequences = batch_input_sequences[start:end]
        output_sequences = batch_output_sequences[start:end]
        contrastive_sequences = batch_contrastive_sequences[start:end]

        # ----------------------------------------------InfoNCE contrastive loss----------------------------------------------

        # infonce_loss = torch.zeros([1], device='cuda')

        for i in range(mini_batch_size):
            # torch.cuda.empty_cache()        # add this line to solve cuda out of memory
            all_neg_tgt_ids = []
            for neg_example in contrastive_sequences[i]:
                contrastive_encoding = tokenizer(
                    neg_example,
                    padding="longest",
                    max_length=max_output_length,
                    truncation=True,
                    return_tensors="pt",
                )
                all_neg_tgt_ids.append(contrastive_encoding.input_ids)

            # just for the fun of it can I use the other true example as the query
            query_example = output_sequences[i]
            query_tgt_ids = tokenizer(
                query_example,
                padding="longest",
                max_length=max_output_length,
                truncation=True,
                return_tensors="pt",
            ).input_ids

            pos_example = output_sequences[1-i]
            pos_tgt_ids = tokenizer(
                pos_example,
                padding="longest",
                max_length=max_output_length,
                truncation=True,
                return_tensors="pt",
            ).input_ids

            source_encoding = tokenizer(
                input_sequences[i],
                padding="longest",
                max_length=max_input_length,
                truncation=True,
                return_tensors="pt",
            )
            input_ids, attention_mask = source_encoding.input_ids, source_encoding.attention_mask

            # convert to decoder ids by shifting tokens right
            query_decoder_input_ids = shift_tokens_right(query_tgt_ids, Generator.config.pad_token_id, Generator.config.decoder_start_token_id)
            pos_decoder_input_ids = shift_tokens_right(pos_tgt_ids, Generator.config.pad_token_id, Generator.config.decoder_start_token_id)
            all_neg_decoder_input_ids = [shift_tokens_right(neg_tgt_ids, Generator.config.pad_token_id, Generator.config.decoder_start_token_id) for neg_tgt_ids in all_neg_tgt_ids]

            # move everything to cuda
            input_ids=input_ids.cuda()
            attention_mask=attention_mask.cuda()
            pos_decoder_input_ids = pos_decoder_input_ids.cuda()
            query_decoder_input_ids = query_decoder_input_ids.cuda()
            for i in range(len(all_neg_decoder_input_ids)):
                all_neg_decoder_input_ids[i] = all_neg_decoder_input_ids[i].cuda()

            # generate the three types of outputs
            # note that outputs[1][-1] gives us the last of decoder_hidden_states
            # for now I am using free form generation to generate anchor embeddings
            # freeform_outputs = Generator(input_ids, attention_mask=attention_mask, use_cache=False, output_hidden_states=True)
            # query = torch.mean(freeform_outputs[1][-1], dim=1)

            # try query outputs with the other representation
            query_outputs = Generator(input_ids, attention_mask=attention_mask, decoder_input_ids=query_decoder_input_ids, use_cache=False, output_hidden_states=True)
            query_embeddings = torch.mean(query_outputs[1][-1], dim=1)

            # here query_outputs[1][-1] is the last decoder hiden layer
            # print("query_outputs:",query_outputs)
            # print("query_outputs[1] len:", len(query_outputs[1]))
            # print("query_outputs[1][-1].size():", query_outputs[1][-1].size())
            # print("query_embeddings:", query_embeddings.size())

            pos_outputs = Generator(input_ids, attention_mask=attention_mask, decoder_input_ids=pos_decoder_input_ids, use_cache=False, output_hidden_states=True)
            positive_embeddings = torch.mean(pos_outputs[1][-1], dim=1)

            all_neg_outputs = [Generator(input_ids, attention_mask=attention_mask, decoder_input_ids=neg_decoder_input_ids, use_cache=False, output_hidden_states=True) for neg_decoder_input_ids in all_neg_decoder_input_ids]
            all_neg_outputs = [torch.mean(neg_outputs[1][-1], dim=1) for neg_outputs in all_neg_outputs]
            negative_embeddings = torch.stack(all_neg_outputs, dim=1)

            infonce_loss_fct = InfoNCE(temperature=0.1, reduction='mean', negative_mode='paired')
            infonce_loss = 0.1 * (infonce_loss_fct(query_embeddings, positive_embeddings, negative_embeddings)/mini_batch_size) # account for minibatch size

            loss = infonce_loss / accum_iter
            loss.backward()

            if USE_WANDB == True:
                wandb.log({"infonce_loss": infonce_loss})

    gen_optimizer.step()
    gen_optimizer.zero_grad()

def train_one_epoch(Generator, gen_optimizer, tokenizer, task_prefix, GROUNDED_CONTRASTIVE_DATA, GROUNDED_DATA, WIKIHOW_INPUT,
                    mini_batch_size: int = 2, accum_iter: int = 16, train_steps: int = 400, num_epoch=0):

    '''this function implements the training loop for one epoch'''

    equivalent_batch_size = mini_batch_size * accum_iter

    Generator.train()

    # shuffle training data on every epoch
    c = list(zip(GROUNDED_DATA, WIKIHOW_INPUT))
    random.shuffle(c)
    GROUNDED_DATA, WIKIHOW_INPUT = zip(*c)

    # for each input sequence, find indices of all other same input sequences and randomly choose one
    # add these two sequences into the batch
    for i in tqdm(range(math.ceil(len(WIKIHOW_INPUT)/32))):
        input_sequences = []
        output_sequences = []
        contrastive_sequences = []
        for j in range(16):
            cur_index = i*16 + j        # the current index
            input = WIKIHOW_INPUT[cur_index]
            index_pos_list = [ i for i in range(len(WIKIHOW_INPUT)) if WIKIHOW_INPUT[i] == input ]  # find all indexes with the same input sequence as the current index
            other_index = random.choice(index_pos_list)     # randomly select one of them
            input_sequences += [WIKIHOW_INPUT[cur_index], WIKIHOW_INPUT[other_index]]
            output_sequences += [GROUNDED_DATA[cur_index], GROUNDED_DATA[other_index]]
            contrastive_sequences += [GROUNDED_CONTRASTIVE_DATA[cur_index], GROUNDED_CONTRASTIVE_DATA[other_index]]

        gen_train_1_batch(Generator,gen_optimizer, tokenizer, task_prefix, input_sequences, output_sequences, mini_batch_size, accum_iter)
        contrastive_train_1_batch(Generator,gen_optimizer, tokenizer, task_prefix, input_sequences, output_sequences, contrastive_sequences, mini_batch_size, accum_iter)

def train_eval_loop(Generator, tokenizer, SEP_GROUNDED_CONTRASTIVE_DATA, NEW_GROUNDED_DATA, WIKIHOW_INPUT_DATA, WIKIHOW_REFERENCE_GROUP, mini_batch_size: int = 2, accum_iter: int = 16,train_steps: int = 400, EPOCHS: int = 5):

    torch.cuda.empty_cache()
    # choose task prefix
    task_prefix = "reorder: "

    val_indices = [WIKIHOW_INPUT_DATA.index(i) for i in [*set(WIKIHOW_INPUT_DATA)]]
    WIKIHOW_INPUT_VAL = [WIKIHOW_INPUT_DATA[i] for i in val_indices]
    GROUNDED_DATA_VAL = [NEW_GROUNDED_DATA[i] for i in val_indices]
    WIKIHOW_REFERENCE_GROUP_VAL = [WIKIHOW_REFERENCE_GROUP[i] for i in val_indices]

    # print(len(GROUNDED_DATA_VAL))
    # print(len(GROUNDED_DATA_TRAIN))
    # len(GROUNDED_DATA_TRAIN) == 795

    # if we want to split the dataset with similar samples on the same side

    # move the Generator and Discriminator to cuda
    Generator = Generator.cuda()

    # initialize gen optimizer with lr = e-4/e-5
    gen_optimizer = torch.optim.Adam(Generator.parameters(), lr=2e-5)
    dis_optimizer = torch.optim.Adam(Discriminator.parameters(), lr=2e-5)

    # check performence once before any training
    # evaluate(Generator, Discriminator, tokenizer, task_prefix, GROUNDED_DATA_VAL, WIKIHOW_INPUT_VAL, WIKIHOW_REFERENCE_GROUP_VAL)

    best_score = 0

    for i in range(EPOCHS):
        print("Epoch", i+1, ':')
        # train 1 epoch

        train_one_epoch(Generator, gen_optimizer, tokenizer, task_prefix, SEP_GROUNDED_CONTRASTIVE_DATA, NEW_GROUNDED_DATA, WIKIHOW_INPUT_DATA, mini_batch_size = mini_batch_size, accum_iter = accum_iter, train_steps = train_steps, num_epoch=i)

        # if i % 10 == 0:
        # new_score = evaluate(Generator, Discriminator, tokenizer, task_prefix, GROUNDED_DATA_VAL, WIKIHOW_INPUT_VAL, WIKIHOW_REFERENCE_GROUP_VAL)

        # if new_score > 0.3 and new_score > best_score:
        #     best_score = new_score
        #     save_model(Generator, gen_optimizer, Discriminator, dis_optimizer)
        #     with open('/content/drive/MyDrive/BLENDER/contrastive_learning/model/' + MODEL_NAME + '.txt', 'a') as f:
        #         f.write('current saved best performace is: ' + str(new_score) + '\n')
        if i % 5 == 0:
            save_model(Generator, gen_optimizer, i)

    # print out the results
    # test(Generator, tokenizer, task_prefix, GROUNDED_DATA_VAL, WIKIHOW_INPUT_VAL, WIKIHOW_REFERENCE_GROUP_VAL)

    return Generator, gen_optimizer

def main():

    # set random seed
    set_seed(42)

    # load model
    Generator = BartForConditionalGeneration.from_pretrained("facebook/bart-base")
    bart_tokenizer = BartTokenizer.from_pretrained("facebook/bart-base")
    bart_tokenizer.add_tokens("<->")
    Generator.resize_token_embeddings(len(bart_tokenizer))
    checkpoint = torch.load(MODEL_CHECKPOINT, map_location=torch.device('cpu'))
    Generator.load_state_dict(checkpoint['generator_state_dict'])

    # load data
    with open("Multimodal-Graph-Script-Learning/data/crosstask_wikihow_reference_step_libraries.json", "r") as fp:
        WIKIHOW_REFERENCE_GROUP = json.load(fp)
    with open("Multimodal-Graph-Script-Learning/data/crosstask_wikihow_input_sequences.json", "r") as fp:
        WIKIHOW_INPUT_DATA = json.load(fp)
    with open("Multimodal-Graph-Script-Learning/data/crosstask_wikihow_output_sequences.json", "r") as fp:
        SEP_GROUNDED_DATA = json.load(fp)
    with open("Multimodal-Graph-Script-Learning/data/crosstask_wikihow_contrastive_sequences.json" + CONTRASTIVE_METHOD, "r") as fp:
        SEP_GROUNDED_CONTRASTIVE_DATA = json.load(fp)

    for group in WIKIHOW_REFERENCE_GROUP:
        for i in range(len(group)):
            group[i] = group[i] + ' '

    val_dict = {}
    for i in range(len(WIKIHOW_INPUT_DATA)):
        input = WIKIHOW_INPUT_DATA[i]
        if input not in val_dict or val_dict[input] == None:
            val_dict[input] = [SEP_GROUNDED_DATA[i]]
        else:
            val_dict[input].append(SEP_GROUNDED_DATA[i])

    Generator, gen_optimizer, Discriminator, dis_optimizer = train_eval_loop(Generator, Discriminator, bart_tokenizer, SEP_GROUNDED_CONTRASTIVE_DATA, SEP_GROUNDED_DATA, WIKIHOW_INPUT_DATA, WIKIHOW_REFERENCE_GROUP, mini_batch_size = 2, accum_iter = 16, train_steps = 400, EPOCHS = 200)

if __name__ == '__main__':
    main()


from magic_manager.apis.v1.users import UserView, UserUpdate
import unittest
from unittest import mock
import flask

class TestUsers(unittest.TestCase):

    @mock.patch('magic_manager.service.v1.user_service.get_all_users')
    def test_get(self,mock_user_service):
        mock_user_service.get_all_users.return_value = {}
        actual = UserView().get()
        self.assertEqual(actual[1], 200)

    @mock.patch('flask.request')
    @mock.patch('magic_manager.service.v1.user_service.add_user')
    def test_post(self, mock_flask, mock_user_service):
        mock_user_service.add_user.return_value = {}
        flask.request.json = {}
        actual = UserView().post()
        self.assertEqual(actual[1], 201)

    @mock.patch('flask.request')
    @mock.patch('magic_manager.service.v1.user_service.update_user')
    def test_put(self, mock_flask, mock_user_service):
        mock_user_service.update_user.return_value = {}
        flask.request.json = {}
        actual = UserUpdate().put(id)
        self.assertEqual(actual[1], 201)

    @mock.patch('magic_manager.service.v1.user_service.delete_user')
    def test_delete(self, mock_user_service):
        mock_user_service.delete_user.return_value = {}
        actual = UserUpdate().delete(id)
        self.assertEqual(actual[1], 200)


import argparse
import logging

import chardet
import pypinyin
import os
import random
from datetime import datetime

import numpy as np
import torch
import transformers
from torch.nn import DataParallel
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm

from tokenizations.bpe_tokenizer import get_encoder

# 不带声调的(style=pypinyin.NORMAL)
def pinyin(word):
    s = ''
    for i in pypinyin.pinyin(word, style=pypinyin.NORMAL):
        s += ''.join(i)
    return s

# 带声调的(默认)
def yinjie(word):
    s = ''
    # heteronym=True开启多音字
    for i in pypinyin.pinyin(word, heteronym=True):
        s = s + ''.join(i) + " "
    return s

class logger:
    def __init__(self, path, clevel=logging.DEBUG, Flevel=logging.DEBUG):
        self.logger = logging.getLogger(path)
        self.logger.setLevel(logging.DEBUG)
        fmt = logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s', '%Y-%m-%d %H:%M:%S')
        # 设置CMD日志
        sh = logging.StreamHandler()
        sh.setFormatter(fmt)
        sh.setLevel(clevel)
        # 设置文件日志
        fh = logging.FileHandler(path, encoding='utf-8')
        fh.setFormatter(fmt)
        fh.setLevel(Flevel)
        self.logger.addHandler(sh)
        self.logger.addHandler(fh)

    def debug(self, message):
        self.logger.debug(message)

    def info(self, message):
        self.logger.info(message)

    def war(self, message):
        self.logger.warn(message)

    def error(self, message):
        self.logger.error(message)

    def cri(self, message):
        self.logger.critical(message)

logg = logger('/templat.log', logging.INFO, logging.INFO)

def log(x):
    logg.info(x)

def checkchar(lines):
    # 原文里面的字符串
    chars1 = []
    # small里面的字符串
    chars2 = []
    smalllines = []
    for ch in lines[0]:
        chars1.append(ch)
    chars1 = set(chars1)

    with open('cache/vocab_small.txt', 'r', encoding='utf8') as f:
        small = f.readlines()
        small = [line.replace('\n', ' [SEP] ') for line in small]  # 用[SEP]表示换行, 段落之间使用SEP表示段落结束
        small = ''.join(small)
        smalllines.append(small)

    all_len = len(smalllines)
    for ch in smalllines[0]:
        chars2.append(ch)
    chars2 = set(chars2)

    aa = chars1 - chars2
    bb = chars2 - chars1
    print(len(aa))
    print(len(bb))
    writevocab(chars1)

# 生成词典
def writevocab(chars1):
    with open('cache/vocab_cidian.txt', 'w', encoding='utf8') as f:
        for ch in chars1:
            if len(ch) == 0:
                continue
            if ch.startswith('\\ue'):
                continue
            f.write(ch)
            f.write('\n')
    print('writevocab finish')

def build_files(filename, data_path, tokenized_data_path, num_pieces, full_tokenizer, min_length):
    lines = []
    with open(filename, 'rb') as f:
        text = f.read()
    char = chardet.detect(text)
    print(char['encoding'])
    encoding = char['encoding']
    if 'GB2312' == char['encoding']:
        encoding = 'gb18030'
    try:
        with open(filename, 'r', encoding=encoding) as f:
            nodel = f.readlines()
            nodel = [line.replace('\n', ' [SEP] ') for line in nodel]  # 用[SEP]表示换行, 段落之间使用SEP表示段落结束
            nodel = ''.join(nodel)
            lines.append(nodel)
            print('reading ok')
    except UnicodeDecodeError as e:
        # os.remove(os.path.join(data_path, filename))
        print('except:', e)
    all_len = len(lines)
    # checkchar(lines)
    if not os.path.exists(tokenized_data_path):
        os.mkdir(tokenized_data_path)

    for i in tqdm(range(num_pieces)):
        print('开始处理:{}'.format(i))
        parse(i, all_len, lines, tokenized_data_path, num_pieces, full_tokenizer)

    print('finish')

def parse(i, all_len, lines, tokenized_data_path, num_pieces, full_tokenizer):
    sublinesNew = lines[all_len // num_pieces * i: all_len // num_pieces * (i + 1)]
    if i == num_pieces - 1:
        sublinesNew.extend(lines[all_len // num_pieces * (i + 1):])  # 把尾部例子添加到最后一个piece
    sublines = []
    for line in sublinesNew:
        new = full_tokenizer.tokenize(line)
        new = full_tokenizer.convert_tokens_to_ids(new)
        sublines.append(new)
    full_line = []
    for subline in sublines:
        full_line.append(full_tokenizer.convert_tokens_to_ids('[MASK]'))  # 文章开头添加MASK表示文章开始
        full_line.extend(subline)
        full_line.append(full_tokenizer.convert_tokens_to_ids('[CLS]'))  # 文章之间添加CLS表示文章结束
    with open(tokenized_data_path + 'tokenized_train_{}.txt'.format(i), 'w', encoding='utf8') as f:
        for id in full_line:
            f.write(str(id) + ' ')
    print('write-finish：{}'.format(i))

def createfilepath(filepath):
    if not os.path.exists(filepath):
        os.mkdir(filepath)

def createpyfromtemplate(templatename, nodelname):
    newfilename = templatename.replace("template", nodelname)
    alllines = []
    try:
        with open('template/' + templatename, 'r', encoding='utf-8') as f:
            nodel = f.readlines()
            nodel = [line.replace('template', nodelname) for line in nodel]
            nodel = ''.join(nodel)
            alllines.append(nodel)
            print('reading ok')
        with open(nodelname + '/' + newfilename, 'w', encoding='utf8') as f:
            for line in alllines:
                f.write(line)
    except UnicodeDecodeError as e:
        print('except:', e)

def main():
    log('start')
    filename = '全唐诗.txt'
    nodel = filename.split('.')[0]
    nodelname = pinyin(nodel)
    # 新建文件夹
    createfilepath(nodelname)
    createfilepath(nodelname + '/data_' + nodelname)
    createfilepath(nodelname + '/data_' + nodelname+'/tokenized')
    createfilepath(nodelname + '/generated_' + nodelname)
    createfilepath(nodelname + '/model_' + nodelname)
    createfilepath(nodelname + '/model_' + nodelname + '/final_model')
    log('files build')

    # 根据模板创建需要的py文件和bat脚本
    createpyfromtemplate('start-parse-template.bat', nodelname)
    createpyfromtemplate('start-train-template.bat', nodelname)
    createpyfromtemplate('generate_template.py', nodelname)
    createpyfromtemplate('train-template.py', nodelname)

    log('py and bat build')

    # return
    # 根据指定的小说生成训练数据集
    tokenizer_path = 'cache/vocab_small.txt'
    raw_data_path = nodelname + '/data_' + nodelname
    tokenized_data_path = raw_data_path + '/tokenized/'

    from tokenizations import tokenization_bert
    full_tokenizer = tokenization_bert.BertTokenizer(vocab_file=tokenizer_path)
    full_tokenizer.max_len = 999999

    log('building datas')

    build_files(filename=filename, data_path=raw_data_path, tokenized_data_path=tokenized_data_path, num_pieces=1,
                full_tokenizer=full_tokenizer, min_length=128)
    log('files built')

if __name__ == '__main__':
    main()


from flask import Flask, jsonify
import requests

app = Flask(__name__)

@app.route('/random_quote')
def get_random_quote():
    api_url = 'https://api.quotable.io/random'
    response = requests.get(api_url)

    if response.status_code == 200:
        quote = response.json()
        return jsonify({'quote': quote['content'], 'author': quote['author']})
    else:
        return jsonify({'error': 'Failed to retrieve a random quote.'}), 500

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000)


from collections import UserString
from io import BytesIO
from unittest import TestCase

from translations import AttributiveTranslations

empty_mo_file = BytesIO(b'\x95\x04\x12\xde'  # magic number, same as gettext.GNUTranslations.LE_MAGIC.to_bytes(4, 'big')
                        b'\x00\x00\x00\x01'  # version
                        b'\x00\x00\x00\x00'  # msgcount
                        b'\x00\x00\x00\x00'  # masteridx
                        b'\x00\x00\x00\x00')  # transidx

class AttributiveTranslationsTest(TestCase):
    def test_value_returned_is_userstring(self):
        value_returned = AttributiveTranslations(empty_mo_file).gettext('foo')
        self.assertIsInstance(value_returned, UserString)

    def test_is_attributive(self):
        t = AttributiveTranslations()
        t._catalog = {'attribute\x04foo': 'bar'}

        foo = t.gettext('foo')
        self.assertEqual('bar', foo.attribute)

    def test_fallbacks(self):
        t = AttributiveTranslations()
        t._catalog = {'user': 'użytkownik'}

        message = t.gettext('user')
        result = message.samplecontext
        self.assertEqual('użytkownik', result)


import numpy as np
import cv2
from utils.function import *
from matplotlib import pyplot as plt
import matplotlib

# Parameters
print("Input image you want to check (0, 1, 2):")
case = int(input())
#case = 0
if case == 0:
    img1 = cv2.imread("curule/im0.png")
    img2 = cv2.imread("curule/im1.png")
    k1 = np.array([[1758.23, 0, 977.42], [0, 1758.23, 552.15], [0, 0, 1]])
    k2 = np.array([[1758.23, 0, 977.42], [0, 1758.23, 552.15], [0, 0, 1]])
    doffs = 0
    baseline = 88.39
    width = 1920
    height = 1080
    ndisp = 220
    vmin = 55
    vmax = 195
elif case == 1:
    img1 = cv2.imread("octagon/im0.png")
   # img1 = cv2.resize(img1, (320, 240), interpolation=cv2.INTER_AREA)

    img2 = cv2.imread("octagon/im1.png")
   # img2 = cv2.resize(img2, (320, 240), interpolation=cv2.INTER_AREA)

    k1 = np.array([[1742.11, 0, 804.90], [0, 1742.11, 541.22], [0, 0, 1]])
    k2 = np.array([[1742.11, 0, 804.90], [0, 1742.11, 541.22], [0, 0, 1]])
    doffs = 0
    baseline = 221.76
    width = 1920
#    width = 320
    height = 1080
  #  height = 240
    ndisp = 100
    vmin = 29
    vmax = 61
else:
    img1 = cv2.imread("pendulum/im0.png")
    img2 = cv2.imread("pendulum/im1.png")
    k1 = np.array([[1729.05, 0, -364.24], [0, 1729.05, 552.22], [0, 0, 1]])
    k2 = np.array([[1729.05, 0, -364.24], [0, 1729.05, 552.22], [0, 0, 1]])
    doffs = 0
    baseline = 537.75
    width = 1920
    height = 1080
    ndisp = 180
    vmin = 25
    vmax = 150

## Step 1. Calibration ##
## Find E and F, also find R and T ##
# Initiate SIFT detector
sift = cv2.SIFT_create()

# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1, None)
kp2, des2 = sift.detectAndCompute(img2, None)
# BFMatcher with default params
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)
# Apply ratio test
good = []
Left_points = []
Right_points = []
for m, n in matches:
    if m.distance < 0.25 * n.distance:
        good.append([m])

match_img = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good, None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
p1, p2 = Get_Features_Array(good, kp1, kp2)
F = Estimate_F(p1, p2)
print("F:", F)

E = np.dot(np.dot(k1.T, F), k1)
u, s, v = np.linalg.svd(E)
E = np.dot(u, np.matrix([[1, 0, 0], [0, 1, 0], [0, 0, 0]]), v)
print("E:", E)
t, C, R, _ = decompose_R_T(E)

print("R:", R)
print("T:", C)

print("t:", t)

## Step 2. Rectification ##
## Plot Eplolar line##
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

sift = cv2.SIFT_create()

kp1, des1 = sift.detectAndCompute(img1, None)
kp2, des2 = sift.detectAndCompute(img2, None)

FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)

flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1, des2, k=2)

good = []
pts1 = []
pts2 = []

for y, (m, n) in enumerate(matches):
    if m.distance < 0.25 * n.distance:
        good.append(m)
        pts1.append(kp1[m.queryIdx].pt)
        pts2.append(kp2[m.trainIdx].pt)

pts1 = np.int32(pts1)
pts2 = np.int32(pts2)
F, mask = cv2.findFundamentalMat(pts1, pts2, cv2.FM_LMEDS)
print(F)
# We select only inlier points
pts1 = pts1[mask.ravel() == 1]
pts2 = pts2[mask.ravel() == 1]

lines1 = cv2.computeCorrespondEpilines(pts1.reshape(-1, 1, 2), 2, F).reshape(-1, 3)
img5, img6 = drawlines(img1, img2, lines1, pts1, pts2)

lines2 = cv2.computeCorrespondEpilines(pts2.reshape(-1, 1, 2), 1, F).reshape(-1, 3)
img3, img4 = drawlines(img2, img1, lines2, pts2, pts1)

cv2.imwrite("computeCorrespondEpilines1.png", img5)
cv2.imwrite("computeCorrespondEpilines2.png", img3)

_, H1, H2 = cv2.stereoRectifyUncalibrated(np.float32(p1), np.float32(p2), F, imgSize=(width, height))
print(" H1: ", H1)
print(" H2: ", H2)

img1_rectified = cv2.warpPerspective(img1, H1, (width, height))
img2_rectified = cv2.warpPerspective(img2, H2, (width, height))
# Show the warp image
cv2.imwrite("Rectified_image1.png", img1_rectified)
cv2.imwrite("Rectified_image2.png", img2_rectified)

## Step 3. Disparity ##

disparity = correspondence(img1_rectified, img2_rectified, height, width, window=15, x_window=5, y_window=5)
disparity_original = disparity.copy()

d = np.max(disparity) - np.min(disparity)
for y in range(disparity.shape[0]):
    for x in range(disparity.shape[1]):
        disparity[y][x] = int((disparity[y][x] * 255) / d)

plt.figure(1)
plt.title('Disparity Grays')
plt.imshow(disparity, cmap='gray')
plt.figure(2)
plt.title('Disparity Heat')
plt.imshow(disparity, cmap='hot')

## Step 3. Depth ##

depth_image = np.zeros((height, width))
depth_value = np.zeros((height, width))
for y in range(height):
    for x in range(width):
        depth_image[y][x] = baseline * k1[0][0] / (disparity_original[y][x] + 1)
plt.figure(3)
plt.title('Depth Map Grays')
plt.imshow(depth_image, cmap='gray')
plt.figure(4)
plt.title('Depth Map Heat')
plt.imshow(depth_image, cmap='hot')

plt.show()


# -- coding: utf-8 --
# @Time : 2022/5/19 10:12
# @Author : Liu Hui
# @Email : 1073811240@qq.com
# @File : 15-事件转发-案例1.py
# @Software: PyCharm

# qtt
# 0. 导入需要的包和模块
from PyQt5 import QtCore
from PyQt5.Qt import *
import sys

class Mylabel(QLabel):
    def enterEvent(self, a0: QtCore.QEvent) -> None:
        print("鼠标进入")
        self.setText("欢迎光临")

    def leaveEvent(self, a0: QtCore.QEvent) -> None:
        print("鼠标离开")
        self.setText("谢谢惠顾")

# 1. 创建一个应用程序对象
app = QApplication(sys.argv)

# 2. 控件的操作
# 2.1 创建控件
window = QWidget()

# 2.2 设置控件
window.setWindowTitle("鼠标操作的案例1")
window.resize(500, 500)

label = Mylabel(window)
label.resize(200, 200)
label.move(100, 100)
label.setStyleSheet("background-color: cyan")

# 2.3 展示控件
window.show()

# 3. 应用程序的执行，进入到消息循环。
sys.exit(app.exec_())


#!/usr/bin/python3
# coding=utf-8

from gi.repository import Peas, GObject, RB
import os

class NowPlaying(GObject.Object, Peas.Activatable):

    __gtype_name = 'NowPlaying'
    object = GObject.property(type=GObject.Object)

    def do_activate(self):
        """
        Called when the plugin is activated
        """
        self.shell = self.object
        self.player = self.shell.props.shell_player
        self.player.connect("playing-song-changed", self.on_playing_changed)

    def on_playing_changed(self, two, tree):
        playing_entry = self.player.get_playing_entry()
        if playing_entry is None:
            return
        title  = 'Title: ' + playing_entry.get_string(RB.RhythmDBPropType.TITLE)
        artist = 'Artist: ' + playing_entry.get_string(RB.RhythmDBPropType.ARTIST)
        datafile = open('/home/user/djShow/NowPlaying.txt', 'w')
        datafile.write(title + '\n' + artist)
        datafile.close()

    def do_deactivate(self):
        """
        Called when plugin is deactivated
        """


import argparse
import logging

logging.basicConfig(level=logging.INFO)
logging.info('This will be logged')

base_string = argparse.ArgumentParser()
base_string.add_argument("string", help="string to test", type=str)
base_string = base_string.parse_args()
print("The string is : " + base_string.string)

list_ASCII = []
for character in base_string.string:
    list_ASCII.append(ord(character))

# Display list
print(list(base_string.string))

# Display list format Ascii
print(list_ASCII)

# List of characters converted to Binary
list_bin = []
for i in list_ASCII:
    list_bin.append(bin(int(i))[2:].zfill(8))

# Display list format Binary
print(list_bin)

# String of raw binary characters
character_bin = ''.join(list_bin)
print(character_bin)

# n = length of one block
n = 6

# list formatted with 6 digits in each block
list_bin_formatted = [character_bin[i:i + n] for i in range(0, len(character_bin), n)]
print(list_bin_formatted)

# list with 6 digits on the last element
lastElement = list_bin_formatted[-1].ljust(6, "0")
list_bin_formatted[-1] = lastElement
print(list_bin_formatted)


import jwt
import requests
from django.contrib.auth.models import User
from django.db.models import F, Sum
from rest_framework import serializers
from rest_framework.status import HTTP_200_OK
from django.utils.translation import gettext_lazy as _
from core.models import Device, GetStartedBot
from rest_framework.authtoken.models import Token
from core.models.user import UserTransaction
from utils.helper import get_apple_user, UserColorGenerator

class UpdateDeviceSerializer(serializers.Serializer):  # noqa
    deviceId = serializers.CharField(required=True, allow_blank=False)
    platform = serializers.CharField(required=True)
    language = serializers.CharField(required=True)
    oneSignal = serializers.CharField(required=False, allow_blank=True)
    appVersion = serializers.CharField(required=True, allow_blank=False)
    appBuild = serializers.CharField(required=True, allow_blank=False)
    latitude = serializers.CharField(required=False, allow_blank=True)
    longitude = serializers.CharField(required=False, allow_blank=True)

    def save(self, **kwargs):
        data = self.validated_data
        request = self.context.get("request")
        device_id = data.get("deviceId", "")
        device_object, device_created = Device.objects.update_or_create(
            device_id=device_id
        )
        GetStartedBot.objects.update_or_create(device=device_object)
        device_object.platform = data.get("platform", "")
        device_object.language = data.get("language", "")
        device_object.onesignal = data.get("oneSignal", "")
        device_object.app_version = data.get("appVersion", "")
        device_object.app_build = data.get("appBuild", "")
        device_object.sessions = F("sessions") + 1

        if request.user.is_authenticated:
            device_object.user = request.user

        device_object.save()
        return request.user

class TokenSerializer(serializers.StringRelatedField):  # noqa
    class Meta:
        model = Token
        fields = ["key"]

class UserSerializer(serializers.ModelSerializer):
    token = TokenSerializer(source="auth_token")
    firstName = serializers.CharField(source="first_name")
    lastName = serializers.CharField(source="last_name")
    userId = serializers.IntegerField(source="id")
    userColor = serializers.SerializerMethodField()
    nickName = serializers.SerializerMethodField()
    balance = serializers.SerializerMethodField()
    claimGift = serializers.SerializerMethodField()
    userPlan = serializers.SerializerMethodField()

    class Meta:
        model = User
        fields = [
            "userId",
            "firstName",
            "lastName",
            "email",
            "token",
            "userColor",
            "nickName",
            "balance",
            "claimGift",
            "userPlan",
        ]

    @staticmethod
    def get_userPlan(obj):
        user_plan = obj.plans.filter(is_active=True)
        if user_plan.exists():
            user_plan = user_plan.first()
            return {
                "isActive": user_plan.is_active,
                "planId": user_plan.plan.id,
                "expireDate": user_plan.expire_in,
            }
        return {"isActive": False}

    @staticmethod
    def get_claimGift(obj):
        return obj.transactions.filter(is_gift=True).exists()

    @staticmethod
    def get_balance(obj):
        debit_subquery = obj.transactions.filter(
            is_credit=False,
        ).aggregate(debit=Sum("amount"))

        credit_subquery = obj.transactions.filter(
            is_credit=True,
        ).aggregate(credit=Sum("amount"))

        credit = credit_subquery["credit"] or 0
        debit = debit_subquery["debit"] or 0

        balance = credit - debit

        return {
            "debit": debit,
            "credit": credit,
            "balance": balance,
        }

    @staticmethod
    def get_nickName(obj, is_shortcut=True):
        # TODO: Need to find a good idea for the nickname in case there is more than 1 device
        nickname = (
            GetStartedBot.objects.filter(device__user=obj.id)
            .values_list("nickname", flat=True)
            .order_by("-created")
            .first()
        )
        if nickname:
            return nickname[0] if is_shortcut else nickname
        return None

    @staticmethod
    def get_userColor(obj):
        return UserColorGenerator.generate_user_color(obj.id)

class VisitorLoginSerializer(serializers.Serializer):  # noqa
    deviceId = serializers.CharField(allow_blank=False, required=True)

    @staticmethod
    def _check_or_retrieve(user_id):
        username = "d-%s" % user_id
        if not username or username == "":
            raise serializers.ValidationError({"auth": _("Device id is missing")})
        try:
            user = User.objects.get(username=username)
            return user
        except User.DoesNotExist:
            return False

    @staticmethod
    def _create_user_as_visitor(user_id, email, first_name, last_name):
        username = "d-%s" % user_id
        user_object = User()
        user_object.is_active = True
        user_object.username = username
        user_object.email = email
        user_object.first_name = first_name
        user_object.last_name = last_name
        user_object.save()
        return user_object

    @staticmethod
    def add_free_credits(user):
        if not UserTransaction.objects.filter(user=user, is_free=True).exists():
            UserTransaction.objects.create_free_transaction(user=user)

    def save(self, **kwargs):
        device_id = self.validated_data.get("deviceId")
        user_id = device_id
        email = device_id + "@chatgx.com"
        first_name = ""
        last_name = ""

        user = self._check_or_retrieve(user_id=user_id)

        if not user:
            user = self._create_user_as_visitor(
                user_id=user_id, email=email, first_name=first_name, last_name=last_name
            )
        user.is_active = True
        user.save()
        self.add_free_credits(user=user)
        Token.objects.update_or_create(user=user)

        if device_id:
            Device.objects.filter(device_id=device_id).update(user=user)

        return UserSerializer(user).data

class GoogleLoginSerializer(serializers.Serializer):  # noqa
    accessToken = serializers.CharField(allow_blank=False, required=True)
    deviceId = serializers.CharField(allow_blank=False, required=True)

    @staticmethod
    def _request_google_user(token):
        google_url = (
            f"https://www.googleapis.com/oauth2/v3/userinfo?access_token={token}"
        )
        google_request = requests.get(google_url)
        if google_request.status_code == HTTP_200_OK:
            return google_request.json()
        else:
            raise serializers.ValidationError(
                {"auth": [_("Unable to fetch google info data")]}
            )

    @staticmethod
    def add_free_credits(user):
        if not UserTransaction.objects.filter(user=user, is_free=True).exists():
            UserTransaction.objects.create_free_transaction(user=user)

    @staticmethod
    def _check_or_retrieve(user_id):
        username = "google-%s" % user_id
        if not username or username == "":
            raise serializers.ValidationError({"auth": _("Google id is missing")})
        try:
            user = User.objects.get(username=username)
            return user
        except User.DoesNotExist:
            return False

    @staticmethod
    def _create_user(user_id, email, first_name, last_name):
        username = "google-%s" % user_id
        user_object = User()
        user_object.is_active = True
        user_object.username = username
        user_object.email = email
        user_object.first_name = first_name
        user_object.last_name = last_name
        user_object.save()
        return user_object

    def save(self, **kwargs):
        access_token = self.validated_data.get("accessToken")
        device_id = self.validated_data.get("deviceId")
        google_data = self._request_google_user(access_token)
        user_id = google_data.get("sub")
        email = google_data.get("email")
        first_name = google_data.get("given_name", "")
        last_name = google_data.get("family_name", "")

        user = self._check_or_retrieve(user_id=user_id)

        if not user:
            user = self._create_user(
                user_id=user_id, email=email, first_name=first_name, last_name=last_name
            )
        user.is_active = True
        user.save()
        self.add_free_credits(user=user)
        Token.objects.update_or_create(user=user)

        if device_id:
            Device.objects.filter(device_id=device_id).update(user=user)

        return UserSerializer(user).data

class AppleLoginSerializer(serializers.Serializer):  # noqa
    accessToken = serializers.CharField(allow_blank=False, required=True)
    deviceId = serializers.CharField(allow_blank=False, required=True)

    @staticmethod
    def _get_apple_user(access_token):
        access_token_url = "https://appleid.apple.com/auth/token"

        client_id, client_secret = get_apple_user()
        headers = {"content-type": "application/x-www-form-urlencoded"}

        data = {
            "client_id": client_id,
            "client_secret": client_secret,
            "code": access_token,
            "grant_type": "authorization_code",
        }
        res = requests.post(access_token_url, data=data, headers=headers)
        response_dict = res.json()
        id_token = response_dict.get("id_token", None)
        if id_token:
            decoded = jwt.decode(id_token, options={"verify_signature": False})
            return decoded
        else:
            raise serializers.ValidationError(
                {"auth": [_("Unable to fetch apple info data")]}
            )

    @staticmethod
    def _check_or_retrieve(user_id):
        username = "apple-%s" % user_id
        if not username or username == "":
            raise serializers.ValidationError({"auth": _("Apple id is missing")})
        try:
            user = User.objects.get(username=username)
            return user
        except User.DoesNotExist:
            return False

    @staticmethod
    def _create_user(user_id, email, first_name, last_name):
        username = "apple-%s" % user_id
        user_object = User()
        user_object.is_active = True
        user_object.username = username
        user_object.email = email
        user_object.first_name = first_name
        user_object.last_name = last_name
        user_object.save()
        return user_object

    def save(self, **kwargs):
        access_token = self.validated_data.get("accessToken")
        apple_user_data = self._get_apple_user(access_token=access_token)
        device_id = self.validated_data.get("deviceId")

        email = apple_user_data.get("email")
        user_id = str(apple_user_data.get("sub"))

        user = self._check_or_retrieve(user_id=user_id)

        if not user:
            user = self._create_user(
                user_id=user_id, email=email, first_name="Apple", last_name=user_id
            )
        GoogleLoginSerializer.add_free_credits(user=user)
        Token.objects.update_or_create(user=user)

        user.is_active = True
        user.save()

        if device_id:
            Device.objects.filter(device_id=device_id).update(user=user)

        return UserSerializer(user).data

class SubscriptionSerializer(serializers.Serializer):  # noqa
    receipt = serializers.CharField(required=True, allow_null=False)


from django.conf.urls import patterns, url

from workup.apps.forum.views import TopicList
from workup.apps.blog_extension.views import BlogPostList
from workup.apps.comments_extension.views.generic import CommentList

urlpatterns = patterns(
    "",
    url("^users/(?P<username>.*)/topics/$",
        TopicList.as_view(
            template_name='accounts/account_profile_topics.html'),
        {"by_score": False},
        name="topic_list_user"),
    url("^users/(?P<username>.*)/blogs/$",
        BlogPostList.as_view(
            template_name='accounts/account_profile_blogs.html'),
        name="blog_list_user"),
    url("^users/(?P<username>.*)/comments/$",
        CommentList.as_view(
            template_name='accounts/account_profile_comments.html'),
        {"by_score": False},
        name="comment_list_user"),
)