# https://techdevguide.withgoogle.com/paths/foundational/find-longest-word-in-dictionary-that-subsequence-of-given-string#!
# Given a string S and a set of words D, find the longest word in D that is a subsequence of S.
# Word W is a subsequence of S if some number of characters, possibly zero, can be deleted from S to form W, without reordering the remaining characters.
# Note: D can appear in any format (list, hash table, prefix tree, etc.
# For example, given the input of S = "abppplee" and D = {"able", "ale", "apple", "bale", "kangaroo"} the correct output would be "apple"
# The words "able" and "ale" are both subsequences of S, but they are shorter than "apple".
# The word "bale" is not a subsequence of S because even though S has all the right letters, they are not in the right order.
# The word "kangaroo" is the longest word in D, but it isn't a subsequence of S.

def find_longest_word_in_dictionary_that_subsequence_of_given_string(S, D):
    indexer = {}
    result = ''
    maxLen = -1

    for i, letter in enumerate(S):
        if letter not in indexer:
            indexer[letter] = []
        indexer[letter].append(i)

    for word in D:
        prevB = -1
        for letter in word:
            if letter not in indexer:
                # if the word in D has a letter that S doesn't have,
                # it's impossible for it to be a subsequence
                break
            for position in indexer[letter]:
                if position > prevB:
                    prevB = position
                    break
            else:
                break
        else:
            # subsequence found
            if len(word) > maxLen:
                result = word
                maxLen = len(word)
    return result


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

import  matplotlib.pyplot as plt
import numpy as np
from pylab import mpl
def DrawBar(dicts):
    # print dicts
    mpl.rcParams['font.sans-serif'] = ['FangSong']  # 指定默认字体
    mpl.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题

    vls= []
    labels = []
    for key, value in dicts.items():
        labels.append(key.decode('utf8'))
        vls.append(value)

    n_groups = len(labels)
    # means_men = (20, 35, 30, 35, 27)
    # means_women = (25, 32, 34, 20, 25)

    fig, ax = plt.subplots()
    index = np.arange(n_groups)
    bar_width = 0.35

    opacity = 0.4
    rects1 = plt.bar(index, tuple(vls), bar_width, alpha=opacity, color='b', label=u'板块动态统计')
    # rects2 = plt.bar(index + bar_width, means_women, bar_width, alpha=opacity, color = 'r', label = 'Women')

    plt.xlabel(u'所属板块')
    plt.ylabel(u'新增帖子数量')
    plt.title(u'天涯论坛部分板块')
    plt.xticks(index + bar_width, labels)
    plt.ylim(0, max(vls) + 20)
    plt.legend()

    plt.tight_layout()
    plt.show()
    pass

def startMain2():
    x = np.linspace(-np.pi,np.pi,256,endpoint=True)
    c,s = np.cos(x),np.sin(x)
    plt.figure(1)
    plt.plot(x,c,color="blue",linewidth=1.0,linestyle="-",label="COS",alpha=0.5)
    plt.plot(x,s,"r*",label="SIN")
    plt.title("Cos & Sin")

    ax = plt.gca()
    ax.spines["right"].set_color("none")
    ax.spines["top"].set_color("none")
    ax.spines["left"].set_position(("data",0))
    ax.spines["bottom"].set_position(("data",0))
    ax.xaxis.set_ticks_position("bottom")
    ax.yaxis.set_ticks_position("left")
    plt.xticks([-np.pi,-np.pi / 2,0,np.pi / 2,np.pi],[r'$-\pi$',r'$-\pi/2$',r'$0$',r'$+\pi/2$',r'$+\pi$'])
    plt.yticks(np.linspace(-1,1,5,endpoint=True))

    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_fontsize(16)
        label.set_bbox(dict(facecolor="white",edgecolor="None",alpha=0.2))

    plt.legend(loc="upper left")
    plt.grid()  #

    # plt.axis([-1,1,-0.5,1])

    plt.fill_between(x,np.abs(x) <0.5,c,c>0.5,color="green",alpha=0.25)

    t = 1
    plt.plot([t,t],[0,np.cos(t)],"y",linewidth=3,linestyle="--")

    plt.annotate("cos(1)",xy=(t,np.cos(1)),xycoords="data",xytext=(+10,+30),textcoords="offset points",arrowprops=dict(arrowstyle="->",connectionstyle="arc3,rad=.2"))
    plt.show()
    return

def startMain3():
    # 散点图
    fig = plt.figure()
    fig.add_subplot(3,3,1)
    n = 128
    X = np.random.normal(0,1,n)
    Y = np.random.normal(0,1,n)
    T = np.arctan2(Y,X)
    plt.axes([0.025,0.025,0.95,0.95])
    plt.scatter(X,Y,s=75,c=T,alpha=.5)
    plt.xlim(-1.5,1.5),plt.xticks([])
    plt.ylim(-1.5,1.5),plt.yticks([])
    plt.axis()
    plt.title("Scatter")

    plt.xlabel("X")
    plt.ylabel("Y")

    plt.show()

    return

def startMain():
    # 柱狀圖
    fig = plt.figure()
    ax = fig.add_subplot(3, 3, 1)
    n = 128
    X = np.random.normal(0, 1, n)
    Y = np.random.normal(0, 1, n)
    T = np.arctan2(Y, X)
    # plt.axes([0.025, 0.025, 0.95, 0.95])
    ax.scatter(X, Y, s=75, c=T, alpha=.5)
    plt.xlim(-1.5, 1.5), plt.xticks([])
    plt.ylim(-1.5, 1.5), plt.yticks([])
    plt.axis()
    plt.title("Scatter")

    plt.xlabel("X")
    plt.ylabel("Y")

    fig.add_subplot(332)
    n = 10
    X = np.arange(n)
    Y1 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
    Y2 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)

    plt.bar(X, +Y1,facecolor="#9999ff",edgecolor="white")
    plt.bar(X, -Y2, facecolor="#9999ff", edgecolor="white")

    for x,y in zip(X,Y1):
        plt.text(x + 0.4,y + 0.05,'%.2f'% y, ha= "center",va="bottom")

    for x,y in zip(X,Y2):
        plt.text(x + 0.4, y - 0.05, '%.2f' % y, ha="center", va="top")

    # 饼图
    ax = fig.add_subplot(333)
    n = 20
    Z = np.ones(n)
    ax.pie(Z,explode=Z*.05,colors=['%f'%(i/float(n)) for i in range(n)],labels=['%.2f'%(i/float(n)) for i in range(n)])
    plt.gca().set_aspect("equal")
    plt.xticks([]),plt.yticks([])

    # 及坐标
    ax = fig.add_subplot(334,polar=True)
    n=20
    theta=np.arange(0.0,2*np.pi,2*np.pi/n)
    radii=10*np.random.rand(n)
    plt.plot(theta,radii)

    # 热图
    ax = fig.add_subplot(335)
    from matplotlib import  cm
    data = np.random.rand(3,3)
    cmap=cm.Reds
    map = plt.imshow(data,interpolation="nearest",cmap=cmap,aspect='auto',vmin=0,vmax = 1)

    # 3D图
    from  mpl_toolkits.mplot3d import  Axes3D
    ax = fig.add_subplot(336,projection="3d")
    ax.scatter(1,1,3,s=100)

    # 热力图
    fig.add_subplot(3,1,3)
    def f(x,y):
        return  (1-x/2 + x**5 + y**3)*np.exp(-x**2-y**2)

    n=256
    x=np.linspace(-3,3,n)
    y=np.linspace(-3,3,n)
    X,Y = np.meshgrid(x,y)
    plt.contourf(X,Y,f(X,Y),8,alpha=.75, cmap=plt.cm.hot)

    plt.savefig("d:/test.jpg")
    plt.show()

    return
if __name__ == "__main__":
    dicts={}
    dicts["A"] = 10
    dicts["B"] = 18
    dicts["C"] = 91
    DrawBar(dicts)


def swap(x,y):
    x = x + y
    y = x - y
    x = x - y

def BubbleSort(ar):
    size = len(ar)
    for x in range(size):
        for y in range(size-x-1):
            if ar[y] > ar[y+1]:
                temp = ar[y]
                ar[y] = ar[y+1]
                ar[y+1] = temp
    return ar

def main():
    ar = [12,77,98,45,8,5,44,36,15,50]
    BubbleSort(ar)
    print(ar)

if __name__ == '__main__':
    main()


from typing import Optional
from starlette import status
from fastapi import APIRouter

from app.core.schema import TaskPostResult
from app.core.queue import create_task

from .schema import LinearParams, HCAParams, PCAParams, KMeansParams, StatsTaskParams
from .tasks import run_linear, run_hca, run_kmeans, run_pca, run_stats

router = APIRouter()

@router.post(
    "/clustering/kmeans",
    status_code=status.HTTP_201_CREATED,
    response_model=TaskPostResult,
    summary="Метод К-средних",
    description="Кластеризация методом К-средних",
    deprecated=True
)
def kmeans(params: KMeansParams):
    res = create_task(run_kmeans, params.dict())
    return TaskPostResult(**res)

@router.post(
    "/clustering/hca",
    status_code=status.HTTP_201_CREATED,
    response_model=TaskPostResult,
    summary="Метод иерархического кластерного анализа",
    description="Кластеризация методом иерархического кластерного анализа",
    deprecated=True
)
def hca(params: HCAParams):
    res = create_task(run_hca, params.dict())
    return TaskPostResult(**res)

@router.post(
    "/regression/linear",
    status_code=status.HTTP_201_CREATED,
    response_model=TaskPostResult,
    summary="Линейная регрессия",
    description="Построение линейной регрессии",
    deprecated=True
)
def linear(params: LinearParams):
    res = create_task(run_linear, params.dict())
    return TaskPostResult(**res)

@router.post(
    "/statistics/stats",
    status_code=status.HTTP_201_CREATED,
    response_model=TaskPostResult,
    summary="Описатиельная статистика",
    description="Вычисление описательной статистики по входным данным",
    deprecated=True
)
def stats(params: StatsTaskParams):
    res = create_task(run_stats, params.dict())
    return TaskPostResult(**res)

@router.post(
    "/factor/pca",
    status_code=status.HTTP_201_CREATED,
    response_model=TaskPostResult,
    summary="Метод главных компонент",
    description="Выделение главных компонент",
    deprecated=True
)
def pca(params: PCAParams):
    res = create_task(run_pca, params.dict())
    return TaskPostResult(**res)


from tool_uni import tool2_crawler
from tool_UI import tool_ui
import tkinter as tk

def tool2(root):
    root.iconify()
    new_window = tk.Toplevel(root)
    new_window.title("批量构建蛋白文件")
    new_window.geometry("800x600")

    button1 = tk.Button(new_window, text="返回主窗口",
                        command=lambda x=new_window: tool_ui.go_back(x, root))
    button1.grid(row=0, column=0)
    label1 = tk.Label(new_window, text="批量构建蛋白文件")
    label1.grid(row=0, column=1)

    # 选择目标文件
    label2 = tk.Label(new_window, text="请选择一个文件：")
    label2.grid(row=1, column=0, padx=10, pady=5)
    entry2 = tk.Entry(new_window, width=40)
    entry2.grid(row=1, column=1, padx=10, pady=5)
    button2 = tk.Button(new_window, text="选择文件",
                        command=lambda x=entry2: tool_ui.open_file_dialog(x, var))
    button2.grid(row=1, column=2)

    # 选择保存文件夹
    label3 = tk.Label(new_window, text="请选择输出位置：")
    label3.grid(row=2, column=0, padx=10, pady=5)
    entry3 = tk.Entry(new_window, width=40)
    entry3.grid(row=2, column=1, padx=10, pady=5)
    button3 = tk.Button(new_window, text="选择文件夹",
                        command=lambda x=entry3: tool_ui.open_directory_dialog(x, var))
    button3.grid(row=2, column=2)

    # headers, base_url, tool_url, set_data显示
    label5 = tk.Label(new_window, text="headers")
    label5.grid(row=4, column=0, padx=10, pady=5)
    v5 = tk.StringVar()
    v5.set('{"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"}')
    entry5 = tk.Entry(new_window, width=40, textvariable=v5, state="readonly")
    entry5.grid(row=4, column=1, padx=10, pady=5)

    label6 = tk.Label(new_window, text="base_url")
    label6.grid(row=5, column=0, padx=10, pady=5)
    v6 = tk.StringVar()
    v6.set('https://www.ddl.unimi.it')
    entry6 = tk.Entry(new_window, width=40, textvariable=v6, state="readonly")
    entry6.grid(row=5, column=1, padx=10, pady=5)

    label7 = tk.Label(new_window, text="tool_url")
    label7.grid(row=6, column=0, padx=10, pady=5)
    v7 = tk.StringVar()
    v7.set('https://www.ddl.unimi.it/vegaol/probuilder_run.php')
    entry7 = tk.Entry(new_window, width=40, textvariable=v7, state="readonly")
    entry7.grid(row=6, column=1, padx=10, pady=5)

    label8 = tk.Label(new_window, text="setting_data")
    label8.grid(row=7, column=0, padx=10, pady=5)
    v8 = tk.StringVar()
    v8.set('{"format": "PDB2","secstruct": "AlphaHelix","phi": "-135","psi": "135","omega": "180","molfile": "(binary)","molecule": " "}')
    entry8 = tk.Entry(new_window, width=40, textvariable=v8, state="readonly")
    entry8.grid(row=7, column=1, padx=10, pady=5)

    # 设置进度变量
    var = tk.StringVar()
    var.set('000')
    label4 = tk.Label(new_window, textvariable=var)
    label4.grid(row=8, column=1, padx=10, pady=10)

    # 状态说明栏
    text9 = tk.Text(new_window, width=40, height=20)
    text9.grid(row=9, column=1, padx=10, pady=10)

    # 开始按钮
    button4 = tk.Button(new_window, text="开始",
                        command=lambda dataset_url=entry2, save_url=entry3, headers=v5, base_url=v6, tool_url=v7, setting_data=v8, text=text9:
                        tool2_crawler.crawler(dataset_url, save_url, headers, base_url, tool_url, setting_data, text, var))
    button4.grid(row=8, column=0, padx=10, pady=10)

if __name__ == '__main__':
    root = tk.Tk()
    root.title("测试")
    root.geometry("700x300")

    tool2(root)

    root.mainloop()


import pygame
import os
import math
import random
import pkg_resources.py2_warn

pygame.init()  # First Step To Initiate The Game
'''
Written In Capital Because The Values Will Be Constant And It Is
A Good Practice To Write Constant Variables in capital letters
'''

# Setup Window
WIDTH = 800
HEIGHT = 500
window = pygame.display.set_mode((WIDTH, HEIGHT))
caption = pygame.display.set_caption("Hangman Game!")

# Button Variable
RADIUS = 20
GAP = 15
letters = []
startx = round((WIDTH - (RADIUS * 2 + GAP) * 13) / 2)
starty = 400
A = 65
for i in range(26):
    x = startx + GAP * 2 + ((RADIUS * 2 + GAP) * (i % 13))
    y = starty + ((i // 13) * (GAP + RADIUS * 2))
    letters.append([x, y, chr(A + i), True])

# FONTS
LETTER_FONT = pygame.font.SysFont('comicsans', 40)
WORD_FONT = pygame.font.SysFont('comicsans', 60)
TITLE_FONT = pygame.font.SysFont('comicsans', 70)

# Loading The Images
images = []
for i in range(7):
    image = pygame.image.load("images/hangman" + str(i) + ".png")
    images.append(image)

# GAME VARIABLES
hangman_status = 0
words = ["DEVELOPER", "HELLO", "MOTHER", "FATHER", "MONITOR", "FURIOUS", "GAMING", "PYTHON", "PYGAME", "DJANGO", "PROGRAMING", "CODING", "SPEAKERS", "ELEPHANT", "TOUCAN", "BEDSHEET", "PILLOW", "CHARGER", "MOBILE", "WALLET", "PENCIL", "GRAPHICS"]
word =random.choice(words)
guessed = []

# COLORS
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# setup Main Loop
FPS = 60
clock = pygame.time.Clock()
run = True

# FUNCTIONS
def Lost_Won_Message(message):
    pygame.time.delay(2000)
    window.fill(WHITE)
    text = WORD_FONT.render(message, 1, BLACK)
    window.blit(text, (WIDTH / 2 - text.get_width() / 2, HEIGHT / 2 - text.get_height() / 2))
    pygame.display.update()
    pygame.time.delay(5000)
    print(message)

def Draw_Letters():
    window.fill(WHITE)
    text = TITLE_FONT.render("HANGMAN IN PYGAME", 1, BLACK)
    window.blit(text, (WIDTH / 2 - text.get_width() / 2, 20))
    # Draw Word
    display_word = ""
    for letter in word:
        if letter in guessed:
            display_word += letter + " "
        else:
            display_word += "_ "
    text = WORD_FONT.render(display_word, 1, BLACK)
    window.blit(text, (400, 200))

    # DRAW LETTERS
    for letter in letters:
        x, y, ltr, visible = letter
        if visible:
            pygame.draw.circle(window, BLACK, (x, y), RADIUS, 3)
            text = LETTER_FONT.render(ltr, 1, BLACK)
            window.blit(text, (x - text.get_width()/2, y - text.get_height()/2))

    window.blit(images[hangman_status], (150, 100))
    pygame.display.update()

# The Main Loop
while run:
    clock.tick(FPS)
    Draw_Letters()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            m_x, m_y = pygame.mouse.get_pos()
            for letter in letters:
                x, y, ltr, visible = letter
                if visible:
                    distance = math.sqrt((x - m_x) ** 2 + (y - m_y) ** 2)
                    if distance < RADIUS:
                        letter[3] = False
                        guessed.append(ltr)
                        if ltr not in word:
                            hangman_status += 1
    won =True
    for letter in word:
        if letter not in guessed:
            won = False
            break

    if won:
        Lost_Won_Message("You WON !")
        break

    if hangman_status == 6:
        Lost_Won_Message("You LOST !!")
        break

pygame.quit()


from Crud_app import app
import json
from flask import jsonify, request
from Crud_app.models.usuario import Usuario
from Crud_app.business.usuario import sign_in, get_users, sign_up

@app.route('/cadastrar', methods=['POST'])
def create():
 try:
        body = request.get_json()

        usuario = sign_up(body)

        response = {
        'statusCode': 200,
        'headers': {
            'Access-Control-Allow-Headers': 'Content-Type',
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
        },
        'body': usuario
    }

        return response, 200
 except Exception as e:
        return e.args, 400

@app.route('/login', methods=['POST'])
def login():
    try:
        body = request.get_json()
        #usuario = Usuario.from_map(json_request)
        usuario = sign_in(body)
        response = {
        'statusCode': 200,
        'headers': {
            'Access-Control-Allow-Headers': 'Content-Type',
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
        },
        'body': usuario
    }

        return response, 200
    except OSError as e:
        return e.args, 404

@app.route('/usuarios', methods=['GET'])
def get_all_users():
    try:
        users = get_users()

        return users
    except Exception as e:
        return e.args, 400

@app.route('/index', methods=['GET'])
def home():
    try:
        return 'olaaaaaaaaa'
    except Exception as e:
        return e.args, 400


import pprint

class Car:
    def __init__(self, manufacturer: str, model: str, color: str,
                 fuel_consumption: float, fuel_tank_capacity: float):
        self.manufacturer: str = manufacturer
        self.__model: str = model
        self.__color: str = color
        self._fuel_consumption = fuel_consumption
        self.fuel_tank_capacity = fuel_tank_capacity
        self.__km: int = 0
        self.fuel: float = 0

    def get_model(self):
        return self.__model

    def get_color(self):
        return self.__color

    def set_color(self, new_color):
        self.__color = new_color

    def __str__(self):
        return f"{self.manufacturer} | Model: {self.__model} |  km: {self.__km} | fuel: {self.fuel}"

    def fill_tank(self, amnt: float) -> bool:
        if amnt <= 0:
            return False
        if amnt + self.fuel > self.fuel_tank_capacity:
            return False

        self.fuel += amnt
        return True

    def drive(self, kms_driven: int):
        if (self.fuel_consumption / 100) * kms_driven <= self.fuel:
            self.__km += kms_driven
            self.fuel -= (self.fuel_consumption / 100) * kms_driven
            return True
        else:
            return False

if __name__ == '__main__':
    car_mazda: Car = Car('Mazda', '3', 'white', 20, 60)
    car_toyota = Car('Toyota', 'Yaris', 'red', 25, 45)
    # ret_val = car_mazda.drive(30)
    # print(ret_val)
    print(car_mazda.__dir__())
    print(car_mazda._Car__km)
    # car_mazda._Car__km = 30
    # car_mazda.km = 60
    print(car_mazda.get_color())
    # print(car_mazda._fuel_consumption)
    # print(car_mazda.__km)


import os
import argparse
import numpy as np
import torch
from torch import nn
import torch.backends.cudnn as cudnn
import torch.distributed as dist

from scripts.helper import load_from_json, write_to_json
import utils
from eval_knn_transformer import extract_features
from transformer_models import PointTransformerSeg
from projection_models import DINOHead
from eval_knn_transformer import ReturnIndexDataset, ReturnIndexDatasetNorm

def create_data_loader(args):
    # ============ preparing data ... ============
    if args.crop_normalized:
        dataset_train = ReturnIndexDatasetNorm(args.pc_dir, args.scene_dir, args.metadata_path,
                                               max_coord=args.max_coord, num_local_crops=0, num_global_crops=0,
                                               mode='train', cat_to_idx=args.cat_to_idx, num_points=args.num_point)
        dataset_val = ReturnIndexDatasetNorm(args.pc_dir, args.scene_dir, args.metadata_path, max_coord=args.max_coord,
                                             num_local_crops=0, num_global_crops=0, mode=args.mode,
                                             cat_to_idx=args.cat_to_idx, num_points=args.num_point)
    else:
        dataset_train = ReturnIndexDataset(args.pc_dir, args.scene_dir, args.metadata_path, num_local_crops=0,
                                           num_global_crops=0, mode='train', cat_to_idx=args.cat_to_idx,
                                           num_points=args.num_point)
        dataset_val = ReturnIndexDataset(args.pc_dir, args.scene_dir, args.metadata_path, num_local_crops=0,
                                         num_global_crops=0, mode=args.mode, cat_to_idx=args.cat_to_idx,
                                         num_points=args.num_point)

    sampler = torch.utils.data.DistributedSampler(dataset_train, shuffle=False)
    data_loader_train = torch.utils.data.DataLoader(
        dataset_train,
        sampler=sampler,
        batch_size=args.batch_size_per_gpu,
        num_workers=args.num_workers,
        pin_memory=True,
        drop_last=False,
    )
    data_loader_val = torch.utils.data.DataLoader(
        dataset_val,
        batch_size=args.batch_size_per_gpu,
        num_workers=args.num_workers,
        pin_memory=True,
        drop_last=False,
    )
    print(f"Data loaded with {len(dataset_train)} train and {len(dataset_val)} val regions.")

    return data_loader_train, data_loader_val

@torch.no_grad()
def extract_features(model, data_loader, use_cuda=True):
    metric_logger = utils.MetricLogger(delimiter="  ")
    features = None
    all_labels = []
    all_file_names = []
    for data, index in metric_logger.log_every(data_loader, 10):
        all_file_names += data['file_name'].copy()
        samples = data['crops'][0]
        samples = samples.cuda(non_blocking=True)

        labels = data['labels'].long().cuda()
        all_labels.append(labels)

        index = index.cuda(non_blocking=True)

        output = model(samples)
        feats = output.mean(1)

        # init storage feature matrix
        if dist.get_rank() == 0 and features is None:
            features = torch.zeros(len(data_loader.dataset), feats.shape[-1])
            if use_cuda:
                features = features.cuda(non_blocking=True)
            print(f"Storing features into tensor of shape {features.shape}")

        # get indexes from all processes
        y_all = torch.empty(dist.get_world_size(), index.size(0), dtype=index.dtype, device=index.device)
        y_l = list(y_all.unbind(0))
        y_all_reduce = torch.distributed.all_gather(y_l, index, async_op=True)
        y_all_reduce.wait()
        index_all = torch.cat(y_l)

        # share features between processes
        feats_all = torch.empty(
            dist.get_world_size(),
            feats.size(0),
            feats.size(1),
            dtype=feats.dtype,
            device=feats.device,
        )
        output_l = list(feats_all.unbind(0))
        output_all_reduce = torch.distributed.all_gather(output_l, feats, async_op=True)
        output_all_reduce.wait()

        # update storage feature matrix
        if dist.get_rank() == 0:
            if use_cuda:
                features.index_copy_(0, index_all, torch.cat(output_l))
            else:
                features.index_copy_(0, index_all.cpu(), torch.cat(output_l).cpu())
    all_labels = torch.cat(all_labels, dim=0)

    return features, all_labels, all_file_names

def extract_features_pipeline(args, data_loader_train, data_loader_val):
    # initialize a point transformer model
    if args.classifier_type == 'supervised':
        backbone = PointTransformerSeg(args)
        head = DINOHead(in_dim=args.transformer_dim, out_dim=args.num_class, use_bn=args.use_bn_in_head,
                        norm_last_layer=args.norm_last_layer)
        classifier = utils.DINO(backbone, head, num_local_crops=0, num_global_crops=1, network_type='teacher')
        classifier = torch.nn.DataParallel(classifier).cuda()
        checkpoint = torch.load(os.path.join(args.cp_dir, args.results_folder_name, args.checkpoint))
        classifier.load_state_dict(checkpoint['model_state_dict'])
        model = classifier.module.backbone
    else:
        checkpoint_path = os.path.join(args.cp_dir, args.results_folder_name, args.checkpoint)
        backbone = PointTransformerSeg(args)
        head = DINOHead(in_dim=args.transformer_dim, out_dim=args.out_dim, use_bn=args.use_bn_in_head)
        DINO = utils.DINO(backbone, head, num_local_crops=0, num_global_crops=0, network_type='teacher')
        DINO.cuda()

        utils.load_pretrained_weights(DINO, checkpoint_path, 'teacher')
        model = DINO.backbone

    model.eval()

    # ============ extract features ... ============
    print("Extracting features for train set...")
    train_features, train_labels, train_file_names = extract_features(model, data_loader_train)
    print("Extracting features for val set...")
    test_features, test_labels, test_file_names = extract_features(model, data_loader_val)

    if utils.get_rank() == 0:
        train_features = nn.functional.normalize(train_features, dim=1, p=2)
        test_features = nn.functional.normalize(test_features, dim=1, p=2)

    features_dir = os.path.join(args.cp_dir, args.results_folder_name, args.features_dir_name)
    if not os.path.exists(features_dir):
        os.mkdir(features_dir)
    if dist.get_rank() == 0:
        torch.save(train_features.cpu(), os.path.join(features_dir, "trainfeat.pth"))
        torch.save(train_labels.cpu(), os.path.join(features_dir, "trainlabels.pth"))
        write_to_json(train_file_names, os.path.join(features_dir, "train_file_names.json"))

        torch.save(test_features.cpu(), os.path.join(features_dir, "testfeat.pth"))
        torch.save(test_labels.cpu(), os.path.join(features_dir, "testlabels.pth"))
        write_to_json(test_file_names, os.path.join(features_dir, "test_file_names.json"))

    print("Features are ready!.")

    return train_features, train_labels, train_file_names, test_features, test_labels, test_file_names

def sample_training_data(train_features, train_labels, sample_ratio):
    # map each label to the data indices of that label.
    label_to_indices = {}
    for i, label in enumerate(train_labels):
        label = label[0].item()
        if label not in label_to_indices:
            label_to_indices[label] = [i]
        else:
            label_to_indices[label].append(i)

    sampled_indices = []
    # sample from each label.
    for label, indices in label_to_indices.items():
        sample_size = int(np.ceil(sample_ratio * len(indices)))
        sample = np.random.choice(indices, sample_size, replace=False)
        sampled_indices.append(sample)
    sampled_indices = np.concatenate(sampled_indices)

    return train_features[sampled_indices, ...], train_labels[sampled_indices, ...]

@torch.no_grad()
def knn_classifier(train_features, train_labels, test_features, test_labels, k, T, num_classes):
    top1, total = 0.0, 0
    train_features = train_features.t()
    num_test_images, num_chunks = test_labels.shape[0], 100
    imgs_per_chunk = num_test_images // num_chunks
    retrieval_one_hot = torch.zeros(k, num_classes).to(train_features.device)
    all_predictions = []
    for idx in range(0, num_test_images, imgs_per_chunk):
        # get the features for test images
        features = test_features[
            idx : min((idx + imgs_per_chunk), num_test_images), :
        ]
        targets = test_labels[idx : min((idx + imgs_per_chunk), num_test_images)]
        batch_size = targets.shape[0]

        # calculate the dot product and compute top-k neighbors
        similarity = torch.mm(features, train_features)
        distances, indices = similarity.topk(k, largest=True, sorted=True)
        candidates = train_labels.view(1, -1).expand(batch_size, -1)
        retrieved_neighbors = torch.gather(candidates, 1, indices)

        retrieval_one_hot.resize_(batch_size * k, num_classes).zero_()
        retrieval_one_hot.scatter_(1, retrieved_neighbors.view(-1, 1), 1)
        distances_transform = distances.clone().div_(T).exp_()
        probs = torch.sum(
            torch.mul(
                retrieval_one_hot.view(batch_size, -1, num_classes),
                distances_transform.view(batch_size, -1, 1),
            ),
            1,
        )
        _, predictions = probs.sort(1, True)
        all_predictions.append(predictions[:, 0:1])

        # find the predictions that match the target
        correct = predictions.eq(targets.data.view(-1, 1))
        top1 = top1 + correct.narrow(1, 0, 1).sum().item()
        total += targets.size(0)

    top1 = top1 * 100.0 / total
    all_predictions = torch.cat(all_predictions, dim=0)

    return top1, all_predictions

def get_args():
    parser = argparse.ArgumentParser('Extracting Features Labels and Predicted Labels', add_help=False)
    # paths
    parser.add_argument('--dataset', default='matterport3d')
    parser.add_argument('--accepted_cats_path', default='../../data/{}/accepted_cats_top10.json')
    parser.add_argument('--pc_dir', default='../../data/{}/pc_regions')
    parser.add_argument('--scene_dir', default='../../data/{}/scenes')
    parser.add_argument('--metadata_path', default='../../data/{}/metadata_non_equal_full_top10.csv')
    parser.add_argument('--cp_dir', default='../../results/{}/LearningBased/')
    parser.add_argument('--results_folder_name', dest='results_folder_name',
                        default='region_classification_transformer_2_32_4096_non_equal_full_region_top10')
    parser.add_argument('--features_dir_name', default='features', type=str)
    parser.add_argument('--output_file_name', default='predicted_labels_knn.json', type=str)
    parser.add_argument('--checkpoint', default='CP_best.pth', type=str)

    # parameters
    parser.add_argument('--mode', dest='mode', default='val')
    parser.add_argument('--classifier_type', dest='classifier_type', default='supervised', help='supervised|dino')
    parser.add_argument('--batch_size_per_gpu', default=8, type=int, help='Per-GPU batch-size')
    parser.add_argument('--num_workers', default=4, type=int, help='Number of data loading workers per GPU.')
    parser.add_argument('--sample_train', action='store_true', default=False)
    parser.add_argument('--sample_ratio', default=1.0, type=float)
    parser.add_argument('--load_features', action='store_true', default=False)

    # transformer parameters
    parser.add_argument('--num_point', default=4096, type=int)
    parser.add_argument('--nblocks', default=2, type=int)
    parser.add_argument('--nneighbor', default=16, type=int)
    parser.add_argument('--input_dim', default=3, type=int)
    parser.add_argument('--transformer_dim', default=32, type=int)
    parser.add_argument('--crop_normalized', action='store_true', default=True)
    parser.add_argument('--max_coord', default=14.30, type=float, help='14.30 MP3D| 5.02 shapenetsem')
    parser.add_argument('--norm_last_layer', default=True, type=utils.bool_flag)
    parser.add_argument('--use_bn_in_head', default=False, type=utils.bool_flag)
    parser.add_argument('--out_dim', dest='out_dim', default=2000, type=int)

    # knn params
    parser.add_argument('--temperature', default=0.07, type=float, help='Temperature used in the voting coefficient')
    parser.add_argument('--nb_knn', default=20, type=int, help='Number of NN to use. 20 is usually working the best.')

    # the rest
    parser.add_argument("--dist_url", default="env://", type=str)
    parser.add_argument("--local_rank", default=0, type=int, help="Please ignore and do not set this argument.")

    return parser

def adjust_paths(args, exceptions):
    base_dir = os.path.dirname(os.path.abspath(__file__))
    for k, v in vars(args).items():
        if (type(v) is str) and ('/' in v) and k not in exceptions:
            v = v.format(args.dataset)
            vars(args)[k] = os.path.join(base_dir, v)

def main():
    # get the arguments
    parser = argparse.ArgumentParser('Extracting Features Labels and Predicted Labels', parents=[get_args()])
    args = parser.parse_args()
    adjust_paths(args, exceptions=['dist_url'])

    utils.init_distributed_mode(args)
    print("git:\n  {}\n".format(utils.get_sha()))
    print("\n".join("%s: %s" % (k, str(v)) for k, v in sorted(dict(vars(args)).items())))
    cudnn.benchmark = True

    # map each category to an index
    accepted_cats = load_from_json(args.accepted_cats_path)
    accepted_cats = sorted(accepted_cats)
    cat_to_idx = {cat: i for i, cat in enumerate(accepted_cats)}
    args.cat_to_idx = cat_to_idx
    args.num_class = len(cat_to_idx)

    # create data loader
    data_loader_train, data_loader_val = create_data_loader(args)

    if args.load_features:
        features_dir = os.path.join(args.cp_dir, args.results_folder_name, args.features_dir_name)
        train_features = torch.load(os.path.join(features_dir, "trainfeat.pth"))
        test_features = torch.load(os.path.join(features_dir, "testfeat.pth"))
        train_labels = torch.load(os.path.join(features_dir, "trainlabels.pth"))
        test_labels = torch.load(os.path.join(features_dir, "testlabels.pth"))
        test_file_names = load_from_json(os.path.join(features_dir, "test_file_names.json"))
    else:
        # need to extract features !
        train_features, train_labels, train_file_names, test_features, test_labels, test_file_names = \
            extract_features_pipeline(args, data_loader_train, data_loader_val)

    if utils.get_rank() == 0:
        # determine if you sample from the training data as opposed to taking it all.
        if args.sample_train:
            train_features, train_labels = sample_training_data(train_features, train_labels, args.sample_ratio)

        # apply knn and save the predicted results.
        top1, predictions = knn_classifier(train_features, train_labels, test_features, test_labels, args.nb_knn,
                                           args.temperature, args.num_class)
        print(f"{args.nb_knn}-NN classifier result: Top1: {top1}")

        # save predictions.
        file_name_to_predictions = {}
        for i, prediction in enumerate(predictions):
            file_name_to_predictions[test_file_names[i]] = prediction[0].item()
        write_to_json(file_name_to_predictions, os.path.join(args.cp_dir, args.results_folder_name,
                                                             args.features_dir_name, args.output_file_name))

        dist.barrier()

if __name__ == '__main__':
    main()


nums = {2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten'}

def tenIntheBed(num):
    lyrics = 'There were '+ num + ' in the bed and the little one said, Roll over! Roll over! So they all rolled over and one fell out'
    return lyrics

for i in range(10, 1, -1):
    print(tenIntheBed(nums[i]))
print('There were one in the bed and the little one said, Alone at last! Good night!')


#
# [282] Expression Add Operators
#
# https://leetcode.com/problems/expression-add-operators/description/
#
# algorithms
# Hard (30.66%)
# Total Accepted:    47.8K
# Total Submissions: 155.9K
# Testcase Example:  '"123"\n6'
#
# Given a string that contains only digits 0-9 and a target value, return all
# possibilities to add binary operators (not unary) +, -, or * between the
# digits so they evaluate to the target value.
#
# Example 1:
#
#
# Input: num = "123", target = 6
# Output: ["1+2+3", "1*2*3"]
#
#
# Example 2:
#
#
# Input: num = "232", target = 8
# Output: ["2*3+2", "2+3*2"]
#
# Example 3:
#
#
# Input: num = "105", target = 5
# Output: ["1*0+5","10-5"]
#
# Example 4:
#
#
# Input: num = "00", target = 0
# Output: ["0+0", "0-0", "0*0"]
#
#
# Example 5:
#
#
# Input: num = "3456237490", target = 9191
# Output: []
#
#
#
class Solution:
    def addOperators(self, num, target):
        """
        :type num: str
        :type target: int
        :rtype: List[str]
        """
        def helper(s, acc_str, bal, multi):
            # print('(', s, acc_str, bal, multi, ')')
            if len(s) == 0:
                if multi is not None:
                    bal -= multi
                if bal == 0:
                    ans.append(acc_str[1:])
            else:
                for i in range(len(s)-1, -1, -1):
                    head = s[:i]
                    tail = s[i:]
                    if len(tail) > 1 and tail[0] == '0':
                        continue
                    if multi is None:
                        helper(head, '+' + tail + acc_str, bal - int(tail), None)
                        if len(head) > 0:
                            helper(head, '-' + tail + acc_str, bal + int(tail), None)
                            helper(head, '*' + tail + acc_str, bal, int(tail))
                    else:
                        helper(head, '+' + tail + acc_str, bal - (int(tail)*multi), None)
                        if len(head) > 0:
                            helper(head, '-' + tail + acc_str, bal + (int(tail)*multi), None)
                            helper(head, '*' + tail + acc_str, bal, int(tail)*multi)

            pass

        ans = []
        helper(num, '', target, None)
        return list(ans)


# -*- coding: utf-8 -*-
"""Shared functions and classes for testing."""

import os
import shutil
import tempfile

from dfvfs.lib import definitions as dfvfs_definitions
from dfvfs.resolver import context
from dfvfs.vfs import fake_file_system

# TODO: move majority of this class to dfvfs helpers.
class FakeFileSystemBuilder(object):
  """Class that implements a fake file system builder.

  Attributes:
    file_system: the fake file system (instance of dvfvs.FakeFileSystem).
  """

  _TEST_DATA_PATH = os.path.join(os.getcwd(), u'test_data')

  def __init__(self):
    """Initializes the fake file system builder object."""
    super(FakeFileSystemBuilder, self).__init__()
    resolver_context = context.Context()
    self._file_system = fake_file_system.FakeFileSystem(
        resolver_context)

  @property
  def file_system(self):
    """The file system."""
    return self._file_system

  def _AddParentDirectories(self, path):
    """Adds the parent directories of a path to the fake file system.

    Args:
      path: a string containing the path of the file within the fake
            file system.
    """
    path_segments = self._file_system.SplitPath(path)
    for segment_index in range(len(path_segments)):
      parent_path = self._file_system.JoinPath(path_segments[:segment_index])
      if not self._file_system.FileEntryExistsByPath(parent_path):
        self._file_system.AddFileEntry(
            parent_path,
            file_entry_type=dfvfs_definitions.FILE_ENTRY_TYPE_DIRECTORY)

  def _FileEntryExistsByPath(self, path):
    """Determines if a file entry for a path exists.

    Args:
      path: the path of the file entry.

    Returns:
      Boolean indicating if the file entry exists.
    """
    return path and path in self._file_system.GetPaths()

  def _ReadTestFile(self, path_segments):
    """Reads the data from a test file.

    Args:
      path_segments: a list of strings containing the path segments
                     inside the test data directory.

    Returns:
      Binary string containing the data of the test file or None.
    """
    # Note that we need to pass the individual path segments to os.path.join
    # and not a list.
    path = os.path.join(self._TEST_DATA_PATH, *path_segments)

    file_object = open(path, 'rb')
    try:
      file_data = file_object.read()
    except IOError:
      return None
    finally:
      file_object.close()

    return file_data

  def AddFile(self, path, file_data):
    """Adds a "regular" file to the fake file system.

    Args:
      path: a string containing the path of the file within the fake
            file system.
      file_data: a binary string containing the data of the file.

    Raises:
      ValueError: if the path is already set.
    """
    if self._file_system.FileEntryExistsByPath(path):
      raise ValueError(u'Path: {0:s} already set.'.format(path))

    self._AddParentDirectories(path)
    self._file_system.AddFileEntry(path, file_data=file_data)

  def AddSymbolicLink(self, path, linked_path):
    """Adds a symbolic link to the fake file system.

    Args:
      path: a string containing the path of the symbolic link within the fake
            file system.
      linked_path: a string containing the path that is linked.

    Raises:
      ValueError: if the path is already set.
    """
    if self._file_system.FileEntryExistsByPath(path):
      raise ValueError(u'Path: {0:s} already set.'.format(path))

    self._AddParentDirectories(path)
    self._file_system.AddFileEntry(
        path, file_entry_type=dfvfs_definitions.FILE_ENTRY_TYPE_LINK,
        link_data=linked_path)

  def AddTestFile(self, path, path_segments):
    """Adds a "regular" file from the test data to the fake file system.

    Args:
      path: a string containing the path of the file within the fake
            file system.
      path_segments: a list of strings containing the path segments
                     inside the test data directory.
    """
    file_data = self._ReadTestFile(path_segments)
    self.AddFile(path, file_data)

class TempDirectory(object):
  """Class that implements a temporary directory."""

  def __init__(self):
    """Initializes the temporary directory."""
    super(TempDirectory, self).__init__()
    self.name = u''

  def __enter__(self):
    """Make this work with the 'with' statement."""
    self.name = tempfile.mkdtemp()
    return self.name

  def __exit__(self, unused_type, unused_value, unused_traceback):
    """Make this work with the 'with' statement."""
    shutil.rmtree(self.name, True)


from sys import stdin
from collections import deque

def main():
    T = int(stdin.readline().strip())
    for _ in range(T):
        n = int(stdin.readline().strip())
        t, total = 0, n**2 - 1
        board = [[int(x) for x in list(stdin.readline().strip())] for _ in range(n)]
        sizes = [[0 for _ in range(n)] for _ in range(n)]
        colours = [0, 0, 0, 0, 0, 0, 0]
        iterations = 0
        groups = {}

        #INITIATE FLOOD
        flooded = set([(0, 0)])
        to_check = deque([(0, 0)])
        while to_check:
            a, b = to_check.popleft()
            for c, d in [(a + 1, b), (a - 1, b), (a, b + 1), (a, b - 1)]:
                if 0 <= c < n and 0 <= d < n and board[a][b] == board[c][d] and (c, d) not in flooded:
                    t += 1
                    to_check.append((c, d))
                    flooded.add((c, d))

        #GROUP NUMS
        seen = [[True for _ in range(n)] for _ in range(n)]
        x = 0
        for i in range(n):
            for j in range(n):
                if seen[i][j]:
                    x += 1
                    seen[i][j] = False
                    to_check = deque([(i, j)])
                    current = []
                    count = 0
                    while to_check:
                        count += 1
                        a, b = to_check.popleft()
                        current.append((a, b))
                        for c, d in [(a + 1, b), (a - 1, b), (a, b + 1), (a, b - 1)]:
                            if 0 <= c < n and 0 <= d < n and board[a][b] == board[c][d] and seen[c][d]:
                                to_check.append((c, d))
                                seen[c][d] = False
                for a, b in current:
                    sizes[a][b] = (count, x)
                groups[x] = current

        while t < total:
            iterations += 1
            counts = [0, 0, 0, 0, 0, 0, 0]
            index_to_fill = {1:[], 2:[], 3:[], 4:[], 5:[], 6:[]}
            #FIND BIGGEST
            seen_ids = set()
            for i, j in flooded:
                for a, b in [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]:
                    if 0 <= a < n and 0 <= b < n and sizes[a][b][1] not in seen_ids and (a, b) not in flooded:
                        counts[board[a][b]] += sizes[a][b][0]
                        seen_ids.add(sizes[a][b][1])
                        index_to_fill[board[a][b]].append(sizes[a][b][1])
            to_fill = counts.index(max(counts))
            colours[to_fill] += 1
            for i in index_to_fill[to_fill]:
                for a, b in groups[i]:
                    t += 1
                    flooded.add((a, b))
        print(iterations)
        print(" ".join([str(x) for x in colours[1:]]))

if __name__ == '__main__':
    main()


from threading import Thread
import time
import pigpio as pg
import rpi_ad7606 as adc

RPi = pg.pi()
laserClockPin = 12

def main():
    laser_pins = [17,22,27,24]
    adc_pins = {'standby':13, 'convsta':19, 'reset':26, 'busy':21, '1stData':20}
    adc1 = adc.AD7606('SPI',10,'simultaneous',adc_pins)

    laserLoop = laserSim(1250,laser_pins)
    adcLoop = adcSim(adc1)

    laserLoop.start()
    adcLoop.start()
    time.sleep(10)
    laserLoop.stop()
    adcLoop.stop()
    # blah blah

def laserSim(Thread):
    def __init__(self,frequency,pins):
        super(laserSim, self).__init__()
        self._keepgoing = True
        self.frequency = frequency
        self.pins = pins

    def run(self):
        RPi.set_pull_up_down(laserClockPin,pg.PUD_DOWN)
        RPi.hardware_PWM(laserClockPin,self.frequency,500000)
        while(self._keepgoing):
            for pin in self.pins:
                RPi.write(pin,1)
                time.sleep(0.25)
                Rpi.write(pin,0)

    def stop(self):
        RPi.write(laserClockPin,0)
        for pin in self.pins:
            RPi.write(pin,0)
        self._keepgoing = False

def adcSim(Thread):
    def __init__(self,_adc):
        super(adcSim,self).__init__()
        self._keepgoing = true
        self._adc = _adc
        self._adc.ADCreset()

    def run(self):
        while(self._keepgoing):
            r = self._adc.ADCread()
            _ch = 1
            for _r in r:
                print("Channel {n}: {v:1.3f} volts.".format(n=_ch,v=_r))
                _ch += 1
            time.sleep(1)

    def stop(self):
        self._adc.ADCreset()
        self._keepgoing = False

if __name__ == '__main__':
    main()


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

'''PPO (Proximal Policy Optimization).
'''

from ..Environment import EnvX
import torch
import numpy as np

class DecisionMakerPPO():
    '''The decision maker of PPO.

    Args:
        head_mask (int): ``head_mask``.
        tail_mask (int): ``tail_mask``.
        device (torch.device): The computing device.

    Attributes:
        training (bool): If ``training=True``, ``model.train()`` will be called. If ``training=False``, ``model.eval()`` will be called.
    '''
    def __init__(self, head_mask, tail_mask, device=torch.device("cuda")):
        self.head_mask = head_mask
        self.tail_mask = tail_mask
        self.training = True
        self.device = device

    def get_model_output(self, model, input_data, action_data):
        '''Get the raw output of a model.

        Args:
            model (torch.nn.Module): The neural network model.
            input_data (torch.tensor): A batch of sequencial environment states. The shape is ``(batch_size, sequence_length, input_size)``.
            action_data (torch.tensor): A batch of sequencial actions. The shape is ``(batch_size, sequence_length)``. This tensor is generated in trainer.
        '''
        if self.training:
            model.train()
        else:
            model.eval()
        seq_num = input_data.shape[0]
        value_data = []
        info = (None, None)
        for i in range(input_data.shape[1]):
            if i == 0:
                action = torch.ones(seq_num).to(torch.int64).to(self.device)
            else:
                action = action_data[:, i-1]
            state_data = torch.nn.functional.one_hot(
                action, num_classes=3)
            state_data = state_data.reshape((
                state_data.shape[0], 1, state_data.shape[1]
            ))
            value, info = model(
                input_data[:, i:i+1, :],
                (state_data, info)
            )
            value_data.append(value)
        value_data = torch.stack(value_data, dim=1)
        return value_data

    def __call__(self, model, input_data):
        '''Get the decisions of a model.

        Args:
            model (torch.nn.Module): The neural network model.
            input_data (torch.tensor): A batch of sequencial environment states. The shape is ``(batch_size, sequence_length, input_size)``.

        Returns:
            signal_series (torch.tensor): A batch of sequencial actions.
        '''
        if self.training:
            model.train()
        else:
            model.eval()
        if len(input_data.shape) == 2:
            input_data = input_data.reshape(
                1, input_data.shape[0], input_data.shape[1]
            )
        if not isinstance(input_data, torch.Tensor):
            input_data = torch.tensor(
                input_data, dtype=torch.float).to(self.device)
        with torch.no_grad():
            seq_num = input_data.shape[0]
            signal_series = []
            action = torch.ones(seq_num).to(torch.int64).to(self.device)
            info = (None, None)
            for i in range(input_data.shape[1]):
                state_data = torch.nn.functional.one_hot(
                    action, num_classes=3)
                state_data = state_data.reshape((
                    state_data.shape[0], 1, state_data.shape[1]
                ))
                q_value, info = model(
                    input_data[:, i:i+1, :],
                    (state_data, info)
                )
                if i < self.head_mask or i >= self.tail_mask:
                    action = torch.ones(seq_num).to(
                        torch.int64).to(self.device)
                else:
                    prob = torch.nn.functional.softmax(q_value, dim=-1)
                    prob = torch.clamp(prob, min=0.001, max=0.999)
                    action = (torch.ones(seq_num) * 2).to(torch.int64).to(self.device)
                    r = torch.rand(action.shape).to(self.device)
                    action[r < prob[:, 0]+prob[:, 1]] = 1
                    action[r < prob[:, 0]] = 0
                signal_series.append(action)
            signal_series = torch.stack(signal_series, dim=1)
            return signal_series

class TrainerPPO():
    '''The trainer of PPO.

    Args:
        head_mask (int): ``head_mask``.
        tail_mask (int): ``tail_mask``.
        buy_cost_pct (float): The cost ratio when the agent chooses to buy.
        sell_cost_pct (float): The cost ratio when the agent chooses to sell.
        gamma (float): The discount factor in the reward value function.
        eps (float): The bound value in PPO.
        huber_loss_delta (float): The delta value in huber loss function.
        target_tao (float): The coefficient for target (old) model updating.
        clamp_bound (float): The upper bound on the absolute value of the gradients.
        device (torch.device): The computing device.

    Attributes:
        difficulty (float): ``difficulty``.
    '''

    def __init__(
        self,
        head_mask,
        tail_mask,
        buy_cost_pct=0.0001,
        sell_cost_pct=0.0001,
        gamma=0.99,
        eps=0.2,
        huber_loss_delta=1.0,
        target_tao=0.01,
        clamp_bound=0.01,
        device=torch.device("cuda")
    ):
        self.head_mask = head_mask
        self.tail_mask = tail_mask
        self.buy_cost_pct = buy_cost_pct
        self.sell_cost_pct = sell_cost_pct
        self.gamma = gamma
        self.eps = eps
        self.critic_loss = torch.nn.HuberLoss(delta=huber_loss_delta)
        self.target_tao = target_tao
        self.clamp_bound = clamp_bound
        self.env = EnvX()
        self.difficulty = 0.0
        self.warm_up = False
        self.device = device

    def update_target_network(self, model, model_target):
        state_dict = model.state_dict()
        target_dict = model_target.state_dict()
        for i in state_dict:
            target_dict[i] = target_dict[i]*(1-self.target_tao)\
                + state_dict[i]*self.target_tao
        model_target.load_state_dict(target_dict)

    def calculate_loss(self, model, decision_maker, input_data_batch, buy_price_batch, sell_price_batch):
        actor_model, critic_model, actor_model_old, critic_model_old = model
        # price
        mid_price_batch = (buy_price_batch + sell_price_batch) / 2
        buy_price_batch_ = mid_price_batch + \
            (buy_price_batch - mid_price_batch) * self.difficulty
        sell_price_batch_ = mid_price_batch + \
            (sell_price_batch - mid_price_batch) * self.difficulty
        # action
        action_batch = decision_maker(actor_model, input_data_batch)
        if self.warm_up:
            action_batch = torch.randint(0, 3, action_batch.shape, dtype=torch.int64).to(self.device)
        # reward
        reward_action = []
        for position_signal_series, buy_price_series, sell_price_series in zip(
                action_batch.tolist(), buy_price_batch_.tolist(), sell_price_batch_.tolist()):
            reward_series = self.env.simulate_trade(
                [i-1 for i in position_signal_series],
                buy_price_series,
                sell_price_series,
                self.buy_cost_pct*self.difficulty,
                self.sell_cost_pct*self.difficulty
            )
            reward_action.append(reward_series)
        reward_action = torch.tensor(reward_action).to(self.device)
        sum_reward = torch.sum(reward_action)
        # actor and critic value
        actor_value = decision_maker.get_model_output(
            actor_model, input_data_batch, action_batch)
        prob = torch.nn.functional.softmax(actor_value, dim=-1)
        prob = torch.clamp(prob, min=0.001, max=0.999)
        critic_value = decision_maker.get_model_output(
            critic_model, input_data_batch, action_batch)
        critic_value = critic_value.reshape(
            critic_value.shape[0], critic_value.shape[1]
        )
        # old actor and critic value
        with torch.no_grad():
            actor_value_old = decision_maker.get_model_output(
                actor_model_old, input_data_batch, action_batch)
            prob_old = torch.nn.functional.softmax(actor_value_old, dim=-1)
            prob_old = torch.clamp(prob_old, min=0.001, max=0.999)
            critic_value_old = decision_maker.get_model_output(
                critic_model_old, input_data_batch, action_batch)
            critic_value_old = critic_value_old.reshape(
                critic_value_old.shape[0], critic_value_old.shape[1]
            )
        # advance value
        with torch.no_grad():
            critic_value_target = reward_action[:, self.head_mask:self.tail_mask] + \
                self.gamma * critic_value_old[:, self.head_mask+1:self.tail_mask+1]
            baseline = critic_value_old[:, self.head_mask:self.tail_mask]
            advance = critic_value_target - baseline
        # r_t
        mask = torch.nn.functional.one_hot(
            action_batch, num_classes=3).to(self.device)
        prob_action = torch.sum(
            prob*mask, dim=2, keepdim=False)[:, self.head_mask:self.tail_mask]
        prob_action_old = torch.sum(
            prob_old*mask, dim=2, keepdim=False)[:, self.head_mask:self.tail_mask]
        r_t = prob_action/prob_action_old
        # actor_loss
        r_t_clip = torch.clamp(r_t, 1-self.eps, 1+self.eps)
        actor_loss = torch.minimum(r_t*advance, r_t_clip*advance)
        actor_loss = -torch.sum(actor_loss)
        # critic_loss
        critic_loss = self.critic_loss(
            critic_value[:, self.head_mask:self.tail_mask],
            critic_value_target
        )
        critic_loss = torch.sum(critic_loss)
        return actor_loss, critic_loss, sum_reward

    def train_epoch(self, model, data_loader, optimizer, **kwargs):
        '''Train the model for an epoch.

        Args:
            model (A list that contains 4 torch.nn.Module): The neural network model, including actor_model, critic_model, actor_model_old, critic_model_old.
            data_loader (torch.utils.data.DataLoader): The data loader which contains the ``train`` dataset.
            optimizer (A list that contains 2 torch.optim.Adam): The optimizer provided by PyTorch, including actor_optimizer, critic_optimizer.
            decision_maker (DecisionMakerPPO): The decision maker that chooses actions according to the model\'s output.

        Returns:
            sum_loss, sum_reward (float, float): The average loss and reward of each sequence in the data loader.
        '''
        decision_maker = kwargs["decision_maker"]
        decision_maker.training = True
        actor_model, critic_model, actor_model_old, critic_model_old = model
        actor_model.train()
        critic_model.train()
        actor_model_old.eval()
        critic_model_old.eval()
        actor_optimizer, critic_optimizer = optimizer
        sum_loss, sum_reward, data_amount = 0, 0, 0
        for batch_id, data_batch in enumerate(data_loader):
            # loss
            state_batch, ask_price_batch, bid_price_batch = data_batch
            state_batch = state_batch.to(self.device)
            actor_loss, critic_loss, reward = self.calculate_loss(
                model, decision_maker, state_batch, ask_price_batch, bid_price_batch
            )
            # log
            sum_loss += actor_loss.tolist() + critic_loss.tolist()
            sum_reward += reward.tolist()
            data_amount += state_batch.shape[0]
            # optimize
            actor_optimizer.zero_grad()
            actor_loss.backward()
            for param in actor_model.parameters():
                param.grad.data.clamp_(-self.clamp_bound, self.clamp_bound)
            actor_optimizer.step()
            critic_optimizer.zero_grad()
            critic_loss.backward()
            for param in critic_model.parameters():
                param.grad.data.clamp_(-self.clamp_bound, self.clamp_bound)
            critic_optimizer.step()
            # update
            self.update_target_network(actor_model, actor_model_old)
            self.update_target_network(critic_model, critic_model_old)
        sum_loss /= data_amount
        sum_reward /= data_amount
        return sum_loss, sum_reward

    def test_epoch(self, model, data_loader, **kwargs):
        '''Test the model for an epoch.

        Args:
            model (A list that contains 4 torch.nn.Module): The neural network model, including actor_model, critic_model, actor_model_old, critic_model_old.
            data_loader (torch.utils.data.DataLoader): The data loader which contains the ``test`` dataset.
            decision_maker (DecisionMakerPPO): The decision maker that chooses actions according to the model\'s output.

        Returns:
            sum_loss, sum_reward (float, float): The average loss and reward of each sequence in the data loader.
        '''
        decision_maker = kwargs["decision_maker"]
        decision_maker.training = False
        actor_model, critic_model, actor_model_old, critic_model_old = model
        actor_model.eval()
        critic_model.eval()
        actor_model_old.eval()
        critic_model_old.eval()
        sum_loss, sum_reward, data_amount = 0, 0, 0
        for batch_id, data_batch in enumerate(data_loader):
            # loss
            state_batch, ask_price_batch, bid_price_batch = data_batch
            state_batch = state_batch.to(self.device)
            actor_loss, critic_loss, reward = self.calculate_loss(
                model, decision_maker, state_batch, ask_price_batch, bid_price_batch
            )
            # log
            sum_loss += actor_loss.tolist() + critic_loss.tolist()
            sum_reward += reward.tolist()
            data_amount += state_batch.shape[0]
        sum_loss /= data_amount
        sum_reward /= data_amount
        return sum_loss, sum_reward


from math import factorial

def encode_string(string, base):
    base_36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    encoded_string = ""
    hash_sum = 0

    for character in string:
        factorial_utf8 = factorial(ord(character))
        factorial_reduction = 1/(len(string)**(1/2))
        hash_sum += int(factorial_utf8**factorial_reduction)

    while hash_sum > 0:
        encoded_string += base_36[hash_sum%base]
        hash_sum = hash_sum//base

    encoded_string = encoded_string[::-1]

    return encoded_string


import tensorflow as tf
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Model

from model.layers import _activation
from model.layers import _normalization
from model.layers import ArcMarginPenaltyLogists
from model.attention import _se_block
from model.attention import _cbam_block

def VGG(args, **kwargs):
    total_layers = int(args.backbone[-2:])
    num_layers = {
        11: [1, 1, 2, 2, 2],
        13: [2, 2, 2, 2, 2],
        16: [2, 2, 3, 3, 3],
        19: [2, 2, 4, 4, 4]
    }

    filters = [1, 2, 4, 8, 8]

    img_input = x = Input(shape=(args.img_size, args.img_size, args.img_channel), name='main_input')
    for i, layers in enumerate(num_layers[total_layers]):
        for layer in range(layers):
            x = Conv2D(64*filters[i], (3, 3), padding='same', name='block{}_conv{}'.format(i+1, layer+1))(x)
            x = _normalization(x, norm=args.norm, name='block{}_norm{}'.format(i+1, layer+1))
            if layer == layers-1:
                if args.attention == 'se':
                    x = _se_block(x, name='block{}_se{}'.format(i+1, layer+1))
                elif args.attention == 'cbam':
                    x = _cbam_block(x, name='block{}_cbam{}'.format(i+1, layer+1))

            x = _activation(x, activation=args.activation, name='block{}_acti{}'.format(i+1, layer+1))

        x = MaxPooling2D((2, 2), name='block{}_pool'.format(i+1))(x)

    x = Flatten(name='flatten')(x)
    x = Dense(4096, name='fc1')(x)
    x = _normalization(x, norm=args.norm, name='fc1_norm')
    x = _activation(x, activation=args.activation, name='fc1_acti')

    if args.embedding == 'softmax':
        x = Dense(4096, name='fc2')(x)
        x = _normalization(x, norm=args.norm, name='fc2_norm')
        x = _activation(x, activation=args.activation, name='fc2_acti')
        x = Dense(args.classes, activation='softmax' if args.classes > 1 else 'sigmoid', name='main_output')(x)

        model_input = [img_input]
        model_output = [x]

    elif args.embedding == 'arcface':
        x = Dense(args.embd_shape, name='fc2')(x)
        x = _normalization(x, norm=args.norm, name='fc2_norm')

        label = Input(shape=(args.classes,), name='arcface_input')
        x = ArcMarginPenaltyLogists(num_classes=args.classes, margin=args.margin, logist_scale=args.logist_scale, name='arcface_output')(x, label)

        model_input = [img_input, label]
        model_output = [x]

    elif args.embedding == 'dual':
        x = Dense(args.embd_shape, name='fc2')(x)
        x = _normalization(x, norm=args.norm, name='fc2_norm')

        x1 = _activation(x, activation=args.activation, name='fc2_acti')
        x1 = Dense(args.classes, activation='softmax' if args.classes > 1 else 'sigmoid', name='main_output')(x1)

        label = Input(shape=(args.classes,), name='arcface_input')
        x2 = ArcMarginPenaltyLogists(num_classes=args.classes, margin=args.margin, logist_scale=args.logist_scale, name='arcface_output')(x, label)

        model_input = [img_input, label]
        model_output = [x1, x2]

    else:
        raise ValueError()

    model = Model(model_input, model_output, name='{}_{}'.format(args.backbone, args.embedding))
    return model


import functools
import os
from amaranth import Elaboratable
from amaranth.sim import Simulator

__all__ = ["GatewareBuildError", "simulation_test"]

class GatewareBuildError(Exception):
    pass

def simulation_test(case=None, **kwargs):
    def configure_wrapper(case):
        @functools.wraps(case)
        def wrapper(self):
            if hasattr(self, "configure"):
                self.configure(self.tb, **kwargs)
            def setup_wrapper():
                if hasattr(self, "simulationSetUp"):
                    yield from self.simulationSetUp(self.tb)
                yield from case(self, self.tb)
            if isinstance(self.tb, Elaboratable):
                sim = Simulator(self.tb)
                with sim.write_vcd(vcd_file=open("test.vcd", "w")):
                    sim.add_clock(1e-8)
                    sim.add_sync_process(setup_wrapper)
                    sim.run()
        return wrapper

    if case is None:
        return configure_wrapper
    else:
        return configure_wrapper(case)


from django.shortcuts import render, redirect
from .forms import JobForm
from .models import Job

# Create your views here.
def index(request):
    jobs = Job.objects.all()
    return render(request, 'index.html', {'jobs':jobs})

def create(request):
    if request.POST:
        form = JobForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect('Home')
    else:
        form = JobForm()
    return render(request, 'create.html', {'form':form})

def show(request, job_id):
    job = Job.objects.get(uuid=job_id)
    return render(request, 'show.html', {'job':job})

def manage(request):
    jobs = Job.objects.all()
    return render(request, 'manage.html', {'jobs':jobs})

def edit(request, job_id):
    job = Job.objects.get(uuid=job_id)
    if request.POST:
        form = JobForm(request.POST, request.FILES, instance=job)
        if form.is_valid():
            form.save()
            return redirect('Home')
    else:
        form = JobForm(instance=job)
    return render(request, 'edit.html', {'job':job, 'form':form})

def delete(request, job_id):
    job = Job.objects.get(uuid=job_id)
    if request.POST:
        job.delete()
        return redirect('Manage')
    return render(request, 'manage.html', {'job':job})


#Se crea la funcion para hacer los procesos y se le ingresa los datos para realizar los procesos
def Procesos(n1,n2):
   #se multiplica la cantidad de horas por el valor de cada hora
    hora=(n1 *3000)
    #se multiplica la cantidad de minutos por el valor de cada minuto
    minutos=(n2*50)
    #se suma el valor de los minutos y las horas
    pagar=hora + minutos
    #se returna el precio total a pagar
    return(pagar)


import os
import sys
import codecs
import time
import traceback
import datetime as dt

import win32api #this is used to get the current user , the current time and the total time workded.
import win32file
import win32profile
import win32gui
import win32evtlog
import win32con
import win32evtlogutil
import winerror

FILEPATH = f'{win32profile.GetProfilesDirectory()}\\{win32api.GetUserName()}\\Desktop\\systemadmin.txt'

def check_file_exists():
    '''Checks Wether the File Exists or Not if not then creates the file using win32file'''
    if(not win32file.FindFilesW(FILEPATH)):
        win32file.CreateFile(FILEPATH, win32file.GENERIC_WRITE, win32file.FILE_SHARE_WRITE, None, 2, 0, 0)

    if(not os.path.isdir('C:\\logs')):
        os.mkdir('C:\\logs')

def write_to_file(inp):
    with open(FILEPATH,'a') as file:
        file.write(inp)

def get_date():
    wdate = win32api.GetSystemTime()
    date = f'{wdate[1]}-{wdate[3]}-{wdate[0]}'
    return date

def get_time_worked():
    time_ms = win32api.GetTickCount()
    time_min = (time_ms/(1000*60))%60
    time_hr = (time_ms/(1000*60*60))%24
    return f'{int(time_hr)} Hours and {int(time_min)} Minutes'

def window_message():
    win32gui.MessageBeep(1)
    message = f'Your Attendance Has Been Marked For Today! Updated Info stored at {FILEPATH} and System Application Logs at C:\logs'
    win32gui.MessageBox(0,message,'System Administrator',0)

def getAllEvents(server, logtypes, basePath):
    if not server:
        serverName = "localhost"
    else:
        serverName = server
    for logtype in logtypes:
        path = os.path.join(basePath, f"{serverName}_{logtype}_log")
        getEventLogs(server, logtype, path)

def getEventLogs(server, logtype, logPath):
    """
    Get the event logs from the specified machine according to the
    logtype (Example: Application) and save it to the appropriately
    named log file
    """
    logPath=f"{logPath}-{dt.date.today()}.log"
    print(f"Logging {logtype} events")
    log = codecs.open(logPath, encoding='utf-8', mode='w')
    line_break = '-' * 80

    log.write(f"\n{server} Log of {logtype} Events\n")
    log.write(f"Created: {time.ctime()}\n\n")
    log.write("\n" + line_break + "\n")
    hand = win32evtlog.OpenEventLog(server,logtype)
    total = win32evtlog.GetNumberOfEventLogRecords(hand)
    print(f"Total events in {logtype} = {total}")
    flags = win32evtlog.EVENTLOG_BACKWARDS_READ|win32evtlog.EVENTLOG_SEQUENTIAL_READ
    events = win32evtlog.ReadEventLog(hand,flags,0)
    evt_dict={win32con.EVENTLOG_AUDIT_FAILURE:'EVENTLOG_AUDIT_FAILURE',
              win32con.EVENTLOG_AUDIT_SUCCESS:'EVENTLOG_AUDIT_SUCCESS',
              win32con.EVENTLOG_INFORMATION_TYPE:'EVENTLOG_INFORMATION_TYPE',
              win32con.EVENTLOG_WARNING_TYPE:'EVENTLOG_WARNING_TYPE',
              win32con.EVENTLOG_ERROR_TYPE:'EVENTLOG_ERROR_TYPE'}

    try:
        events=1
        while events:
            events=win32evtlog.ReadEventLog(hand,flags,0)

            for ev_obj in events:
                the_time = ev_obj.TimeGenerated.Format()
                evt_id = str(winerror.HRESULT_CODE(ev_obj.EventID))
                record = ev_obj.RecordNumber
                msg = win32evtlogutil.SafeFormatMessage(ev_obj, logtype)

                source = str(ev_obj.SourceName)
                if not ev_obj.EventType in list(evt_dict.keys()):
                    evt_type = "unknown"
                else:
                    evt_type = str(evt_dict[ev_obj.EventType])
                log.write(f"Event Date/Time: {the_time}\n")
                log.write(f"Event ID / Type: {evt_id} / {evt_type}\n")
                log.write(f"Record #{record}\n")
                log.write(f"Source: {source}\n\n")
                log.write(msg)
                log.write("\n\n")
                log.write(line_break)
                log.write("\n\n")
    except:
        print(traceback.print_exc(sys.exc_info()))

    print(f"Log creation finished. Location of log is {logPath}")

def main():
    check_file_exists()
    username = win32api.GetUserName()
    machine_name = win32api.GetComputerName()
    today_date = get_date()
    worked_hours = get_time_worked()
    system_info = win32api.GetSystemInfo()

    template = f'For Day : {today_date}\nMachine Name : {machine_name} \nUser Name : {username} \nWorked Hours : {worked_hours} \nSystem Info : {system_info}'
    write_to_file(template)
    write_to_file('\n\n')

    server = None
    logTypes = ["System", "Application"]
    getAllEvents(server, logTypes, "C:\logs")
    window_message()

if  __name__ == '__main__':
    main()


# convert a base-yaml + prescription to a destination yaml
import yaml

from prescription_parser import prescriptions_to_tree
from dict_utils import dict_merge, dict_prune_nulls

def manipulate_yaml(base_tree, alteration_tree):
    """
    Merge alterations onto the base tree,
    and also take care of pruning resulting nulls
    to effect key deletions.
    """
    merged = dict_merge(alteration_tree, default=base_tree)
    pruned = dict_prune_nulls(merged)
    return pruned

if __name__ == '__main__':
    #
    # usage so far:
    #   python yaml_manipulate [src_yaml] [prescription_file] [out_file_must_not_exist]
    # e.g.:
    #   python yaml_manipulate.py example/src1.yaml example/prescr1.txt out/out1.yaml
    #
    import sys
    import os
    in_yaml_file = sys.argv[1]
    prescriptions_file = sys.argv[2]
    out_yaml_file = sys.argv[3]
    in_tree = yaml.load(open(in_yaml_file), Loader=yaml.Loader)
    pr_lines = [
        l
        for l in open(prescriptions_file).readlines()
        if l
        if l[0] != '#'
    ]
    #
    pr_tree = prescriptions_to_tree(pr_lines)
    result_tree = manipulate_yaml(in_tree, pr_tree)
    #
    dumped = yaml.dump(result_tree, default_flow_style=False)
    if os.path.isfile(out_yaml_file):
        print('** OUT FILE EXISTS, DUMPING TO STDOUT **')
        print(dumped)
    else:
        with open(out_yaml_file, 'w') as of:
            of.write('%s\n' % dumped)


from acts.base_test import BaseTestClass

class BudsTest(BaseTestClass):
    def setup_class(self):
        super().setup_class()
        self.dut = self.buds_devices[0]

    def test_make_toast(self):
        self.log.info('Battery Level: %s', self.dut.get_battery_level())
        self.log.info('Gas Gauge Current: %s', self.dut.get_gas_gauge_current())
        self.log.info('Gas Gauge Voltage: %s', self.dut.get_gas_gauge_voltage())
        self.log.info('Serial Log Dump: %s', self.dut.get_serial_log())


import json

from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.utils.decorators import method_decorator
from django.views import View

from message.models import MessageModel
from tools.chack_login import check_login, get_request_username
from topic.models import TopicModel
#10300-10399
from user.models import UserModel

class TopicViews(View):

    #自己看自己 is_self = True 显示私有文章  category = tec or no-tec 显示技术类or非技术类 无此字段 全部显示
    def make_topics_res(self, username, topic):
        res = {'code':200, 'data':{}}
        topic_list = []

        for i in topic:
            d = {}
            d['id'] = i.id
            d['title'] = i.title
            d['category'] = i.category
            d['created_time'] = i.created_time.strftime('%Y-%m-%d %H:%M')
            d['content'] = i.content
            d['introduce'] = i.introduce
            d['author'] = username
            topic_list.append(d)

        res['data']['nickname'] = username
        res['data']['topics'] = topic_list
        return res

    def make_topic_res(self, is_self, username, topic):

        #messages and reply

        messages = MessageModel.objects.filter(topic_id=topic.id).order_by('-created_time')
        msg_dic = {}
        msg_list = []
        msg_count = 0
        for message in messages:
            msg_count += 1
            parent_msg = messages.filter(id=message.parent_message)
            sender = UserModel.objects.get(id=message.publisher_id_id)
            if parent_msg:
                reply_dic = {}
                msg_dic.setdefault(message.parent_message, [])

                reply_dic['publisher'] = sender.nickname
                reply_dic['publisher_avatar'] = str(sender.avatar)
                reply_dic['created_time'] = message.created_time.strftime('%Y-%m-%d %H:%M')
                reply_dic['content'] = message.content
                reply_dic['msg_id'] = message.id
                msg_dic[message.parent_message].append(reply_dic)
            else:

                msg_list.append({
                    'id':message.id,
                    'content':message.content,
                    'publisher': sender.nickname,
                    'publisher_avatar': str(sender.avatar),
                    'created_time': message.created_time.strftime('%Y-%m-%d %H:%M'),
                    'reply': [],
                })
                # {
                #  id: 2
                #  reply [
                #  {msg_id:1, parent_id:2, }
                #  {msg_id:2, parent_id:2. }
                #  ]
                #
                # }
                #  {
                #  2 : [{msg:1,par:2},{msg:2,par:2}],
                #  3 : [],
                #  4 : [{msg:2,par:2}]
                #  }
        for i in msg_list:
            if i['id'] in msg_dic:
                i['reply'] = msg_dic[i['id']]

        res = {'code': 200, 'data': {}}

        if is_self:
            next_topic = TopicModel.objects.filter(id__gt=topic.id).first()
            last_topic = TopicModel.objects.filter(id__lt=topic.id).last()
        else:
            next_topic = TopicModel.objects.filter(id__gt=topic.id, limit='public').first()
            last_topic = TopicModel.objects.filter(id__lt=topic.id, limit='public').last()

        res['data']['nickname'] = username
        res['data']['title'] = topic.title
        res['data']['category'] = topic.category
        res['data']['created_time'] = topic.created_time.strftime('%Y-%m-%d %H:%M')
        res['data']['content'] = topic.content
        res['data']['introduce'] = topic.introduce
        res['data']['author'] = username
        res['data']['next_id'] = next_topic.id if next_topic else None
        res['data']['next_title'] = next_topic.title if next_topic else ''
        res['data']['last_id'] = last_topic.id if last_topic else None
        res['data']['last_title'] = last_topic.title if last_topic else ''
        res['data']['messages'] = msg_list
        res['data']['messages_count'] = msg_count

        return res

    def get(self, request, username):
        try:
            UserModel.objects.get(nickname=username)
        except:
            return JsonResponse({'code':301, 'error':'no author'})
        is_self = False
        category = request.GET.get('category')
        visitor = get_request_username(request)

        if visitor:
            user = UserModel.objects.get(nickname=visitor)

        if visitor == username:
            is_self = True

        #詳情頁查詢
        t_id = request.GET.get('t_id')
        if t_id:
            if is_self:
                try:
                    topic = TopicModel.objects.get(id=t_id)
                except:
                    return JsonResponse({'error': '查無此文'})
                res = self.make_topic_res(is_self, username, topic)
                return JsonResponse(res)
            else:
                try:
                    topic = TopicModel.objects.get(id=t_id, limit='public')
                except:
                    return JsonResponse({'error': '查無此文'})
                res = self.make_topic_res(is_self, username, topic)
                return JsonResponse(res)

        #技術類查詢
        if category:
            if is_self:
                topic = TopicModel.objects.filter(author=user.id, category=category)
                res = self.make_topics_res(username, topic)
                return JsonResponse(res)
            else:
                topic = TopicModel.objects.filter(author=user.id, category=category, limit='public')
                res = self.make_topics_res(username, topic)
                return JsonResponse(res)
        #全部查詢
        if is_self:
            topic = TopicModel.objects.filter(author=user.id)
            res = self.make_topics_res(username, topic)
            return JsonResponse(res)
        else:
            topic = TopicModel.objects.filter(author=user.id, limit='public')
            res = self.make_topics_res(username, topic)
            return JsonResponse(res)

        if category not in ['tec','no-tec']:
            return JsonResponse({'code': 10302})
        if category not in ['tec', 'no-tec']:
            return JsonResponse({'error': 'run'})

    @method_decorator(check_login)
    def post(self, request, username):
        username = request.user.nickname

        json_str = request.body
        json_obj = json.loads(json_str)

        content = json_obj['content']
        title = json_obj['title']
        category = json_obj['category']
        limit = json_obj['limit']
        introduce = content[:30]

        if category not in ['tec','no-tec']:
            return JsonResponse({'code':10300})

        if limit not in ['public','private']:
            return JsonResponse({'code': 10301})

        TopicModel.objects.create(title=title, content=content, category=category, limit=limit, introduce=introduce, author_id=request.user.id)

        return JsonResponse({'code':200, 'username':username})


import matplotlib.pyplot as plt
D= { 'CTTT': 60,
'Kế toán': 310,
'Kinh tế ' : 360,
'CNTT': 580,
'Cơ khí': 340,
'Thủy văn': 290 }
plt.bar(range(len(D)), D.values(), align='center')
plt.xticks(range(len(D)), D.keys())
plt.title('Các ngành tuyển sinh của Đại học Thủy Lợi')
plt.show()

Năm 2/Lập trình khoa học dữ liệu/Python/LT/Lý thuyết buổi 5/vd về biểu đồ cột.py


import socket
import time
import subprocess

UDP_IP = "127.0.0.1" #IP
UDP_PORT = 9876 #port
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT)) #bind socket with port
counter = 1
start = 0
diff = 60000 #time difference
total = 0
dsum = 0
while True:
    data, addr = sock.recvfrom(1024)
    data_split = str(data).split(',') #split data with comma (,)
    total += 1
    dsum+= int(data_split[15]) #total sum of the dOctet
    if(start == 0):
        filename = str(counter) + ".txt" #file creation
        f = open(filename,"w+")
        start = int(data_split[3])
    f.write(str(data_split[15]+"\n"))
    #file creation logic
    if(start + diff <= (int(data_split[3])) + 1):
        counter= counter + 1
        filename = str(counter) + ".txt"
        f = open(filename,"w+")
        start = int(data_split[3])


#Run in console/terminal: python recur_max.py
# or just recur_max.py
# then type in the list of integers separated by commas & press enter
def Max(list):
    if len(list) == 1:
        return list[0]
    else:
        m = Max(list[1:])
        return m if m > list[0] else list[0]

def main():
    list = eval(raw_input(" please enter a list of numbers: "))
    print("the largest number is: ", Max(list))

main()


#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

# Author: Kin Lo <kinabcd@gmail.com>
import sys
import argparse
import feedparser
from rtorrent.remotecontrol import RemoteControl

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('uri', help='request URI')

args = parser.parse_args()
rtctl = RemoteControl()
d = feedparser.parse(args.uri)
for feed in d.entries:
    print( feed.title )
    for link in feed.links:
        if link.type == 'application/x-bittorrent' :
            rtctl.load(link.href)


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

# In[6]:

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
get_ipython().run_line_magic('matplotlib', 'inline')
from tqdm import tqdm_notebook
import concurrent.futures
from multiprocessing import Pool
import copy,os,sys,psutil
from collections import Counter

# In[16]:

import numpy as np
from zac_pyutils import ExqUtils
from sklearn import metrics

# # PyTorch AllenNLP
# AllenNLP 的 pyTorch 版本需要配置文件，暂时没有搞清楚配置文件怎么写

# In[1]:

from allennlp.modules.elmo import Elmo, batch_to_ids

# In[2]:

options_file = "options.json"  # 配置文件地址
weight_file = "weights.hdf5" # 权重文件地址
# 这里的1表示产生一组线性加权的词向量。
# 如果改成2 即产生两组不同的线性加权的词向量。
elmo = Elmo(options_file, weight_file, 1, dropout=0)
# use batch_to_ids to convert sentences to character ids
sentence_lists = ["I have a dog", "How are you , today is Monday","I am fine thanks"]
character_ids = batch_to_ids(sentences_lists)
embeddings = elmo(character_ids)['elmo_representations']

# # Tensorflow_hub AllenNLP
# - [Introduction to TensorflowHub: Simple Text Embedding(Using ELMo)](https://medium.com/@joeyism/embedding-with-tensorflow-hub-in-a-simple-way-using-elmo-d1bfe0ada45c)
# - [Tensorflow+Keras做ELMo（中文翻译）](https://ai.yanxishe.com/page/TextTranslation/1597)

# In[13]:

import tensorflow as tf
import tensorflow_hub as hub
elmo = hub.Module("https://tfhub.dev/google/elmo/2", trainable=True) # trainable=True 是指使用'elmo'模式的向量结果时，ELMo的线性加和权重是可以训练的

# ## 参考自[TensorflowHub的官网](https://tfhub.dev/google/elmo/2)
#
# ### 输入
# #### 参数
# - `signature`
#     - default | 默认是分句子输入，batch_size等于句子的个数
#         > the module takes untokenized sentences as input. The input tensor is a string tensor with shape [batch_size]. The module tokenizes each string by splitting on spaces.
#     - tokens  | 使用分过词的句子作为输入
#         > With the tokens signature, the module takes tokenized sentences as input. The input tensor is a string tensor with shape [batch_size, max_length] and an int32 tensor with shape [batch_size] corresponding to the sentence length. The length input is necessary to exclude padding in the case of sentences with varying length.
#     - 上述两种方式的结果是完全一致的
# - `as_dict`
#     - 不提供参数 `as_dict`时（默认为 `False`），会使用`default`。即 `elmo(tokens, as_dict=True)['default']` = `elmo(tokens)`
# - 有句子sen0,sen1,sen2；
#   - 分开输入：分三次输入（使用'default'）得到三个句子向量emb0,emb1,emb2；
#   - 一次输入：作为一个list [sen0,sen1,sen2] 一次性输入，得到一个emb_list；
#   - 根据测试结果来看，emb_lit[0] 和 emb0 余弦相似度 0.99999+

# In[11]:

# 使用 signature = default， 输入是句子
sentences = ["the cat is on the mat", "dogs are in the fog"]
emb_opt_sentence = elmo(inputs=sentences, as_dict=True)

# 使用 signature = tokens, 输入是字典，包含 tokens 和 sequence_len 注意需要padding
def padding(tokens_inp,pad=""):
    pad_len = max([len(i) for i in tokens_inp])
    return [(i+[pad]*pad_len)[:pad_len] for i in tokens_inp]

tokens_list = ["the cat is on the mat".split(" "),"dogs are in the fog".split(" ")]
tokens_list = padding(tokens_list,"__PAD__")
tokens_len = [len(i) for i in tokens_list]
input_dict = {
    'tokens': tokens_list,
    'sequence_len' : tokens_len
}
input_dict
emb_opt_tokens = elmo(inputs=input_dict, signature='tokens', as_dict=True)

# ### 输出
# > The output dictionary contains:
# >- word_emb:
# >    - the character-based word representations
# >    - general embedding 与上下文无关，ELMo使用的是charater-based CNN + Highway
# >    - `elmo(tokens, as_dict=True)['word_emb']`
# >    - *[batch_size, max_length, 512]*
# >- lstm_outputs1:
# >    - the first LSTM hidden state
# >    - 双向LSTM中的一个
# >    - `elmo(tokens, as_dict=True)['lstm_outputs1']`
# >    - *[batch_size, max_length, 1024].*
# >- lstm_outputs2: t
# >    - the second LSTM hidden state
# >    - 双向LSTM中的一个
# >    - `elmo(tokens, as_dict=True)['lstm_outputs2']`
# >    - *[batch_size, max_length, 1024].*
# >- elmo:
# >    - the weighted sum of the 3 layers, where the weights are trainable.
# >    - 上述三个（word+2*lstm) emb的线性加权组合，针对不同任务时这里的权重（维度为3）可以进行训练，即设置的Trainable
# >    - `elmo(tokens, as_dict=True)['elmo']`
# >    - *[batch_size, max_length, 1024]*
# >- default:
# >    - a fixed mean-pooling of all contextualized word representations
# >    - 即平均后的句子向量
# >    - `elmo(tokens, as_dict=True)['default']`
# >    - *[batch_size, 1024].*
#
# 当输入采用不同方式tokens/sentences时，只有word_emb能确保五种输出结果是一致的；
#
# 不一致的原因实际上是因为句子分词后长度不同，需要用空串padding；
#
# 如果句子分词后长度都一直，则五种输出结果都一致。
#
# batch_size: elmo的inputs是多少个句子； max_length: 每个句子最多多少个词（padding）

# In[12]:

with tf.Session() as sess:
    for output in ['word_emb','lstm_outputs1','lstm_outputs2','elmo','default']:
        sess.run(tf.global_variables_initializer())
        emb = sess.run(emb_opt_sentence[output])
        emb2 = sess.run(emb_opt_tokens[output])
        print(f"signature不同的输入方式(tokens,sentences)，'{output}' 结果是一致的: ",np.all(emb == emb2))

# In[20]:

# 计算好emb
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    emb_tokens = sess.run(emb_opt_tokens['elmo'])
    emb_sentence = sess.run(emb_opt_sentence['elmo'])

# In[ ]:

print("sentences: ", sentences)
for sen_idx in [0,1]:
    print("\n>>> 使用tokens_list: ", tokens_list[sen_idx])
    consistent = np.all(emb_tokens[sen_idx] == emb_sentence[sen_idx])
    print("两种输入方式下 elmo 的一致性：",consistent)
    if not consistent:
        print("不一致时，tokens和sentence两种方式的tensor值示例：")
        emb_tokens[sen_idx].shape
        emb_tokens[sen_idx]
        emb_sentence[sen_idx]

# # 验证

# ## 句子相似度度量

# In[18]:

pad_len = 13
demoSentence_total = ["Modi was appointed Chief Minister of Gujarat in 2001",
                      "Narendra Modi was appointed Chief Minister of Gujarat in 2001",
                      "Obama was appointed Chief Minister of Gujarat in 2001"]
demoSentence_total = [" ".join(ExqUtils.padding(sen.split(" "),pad_len)) for sen in demoSentence_total]
demoSentence_total

with tf.Session() as sess:
    emb_opt = elmo(inputs=sentences, as_dict=True)
    sess.run(tf.global_variables_initializer())
    elmo(inputs=demoSentence_total, as_dict=True)['default'].shape
    emb_demo_opt = tf.reshape(elmo(inputs=demoSentence_total, as_dict=True)['default'],[3,1,1024])
    emb_demo_total = sess.run(emb_demo_opt)

# In[ ]:

emb_demo_total = emb_demo_total.reshape(3,1,1024)
metrics.pairwise.cosine_similarity(emb_demo_total[0],emb_demo_total[1])
metrics.pairwise.cosine_similarity(emb_demo_total[0],emb_demo_total[2])

# In[ ]:


"""Config flow for Palazzetti integration."""
import logging
import voluptuous as vol
from homeassistant import config_entries, exceptions
from palazzetti_sdk_local_api import Hub

from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)

DATA_SCHEMA = vol.Schema({"host": str})
STEP_USER_DATA_SCHEMA = vol.Schema({"host": str})

async def validate_input(_user_host):
    """chech if user host is a Palazzetti Hub IP"""

    myhub = Hub(_user_host)
    myconfig = {"hub_id": myhub.hub_id, "host": _user_host}

    # connect to get Hub data
    await myhub.async_update(deep=True)
    if myhub.online:
        myconfig["hub_isbiocc"] = myhub.hub_isbiocc
        myconfig["label"] = myhub.label
        return myconfig

    raise CannotConnect

class DomainConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
    """Handle a config flow for Palazzetti."""

    VERSION = 1
    CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL

    async def async_step_user(self, user_input=None):
        """Handle the initial step."""
        if user_input is None:
            return self.async_show_form(
                step_id="user", data_schema=STEP_USER_DATA_SCHEMA
            )

        errors = {}

        try:
            self.host = user_input["host"]

            # validate host address
            info = await validate_input(self.host)

            # check if already registered
            check_exists = await self.async_set_unique_id(info["hub_id"].lower())

            if check_exists:
                return self.async_abort(reason="host_exists")

            # create config_entry
            return self.async_create_entry(
                title=f"{info['label']} ({self.host})",
                data=info,
            )

        except CannotConnect:
            _LOGGER.error("Error connecting to the ConnBox at %s", self.host)
            errors["base"] = "cannot_connect"

        except InvalidSN:
            _LOGGER.error("Error validating SN to the ConnBox at %s", self.host)
            errors["base"] = "invalid_sn"

        except Exception:  # pylint: disable=broad-except
            _LOGGER.exception(
                "Unknown error connecting to the ConnBox at %s", self.host
            )
            errors["base"] = "unknown"

        return self.async_show_form(
            step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
        )

class CannotConnect(exceptions.HomeAssistantError):
    """Error to indicate we cannot connect."""

class InvalidSN(exceptions.HomeAssistantError):
    """Error indicating embedded SN is not valid"""


# stlib
import csv

# third party
import pandas

# custom modules

def load_data_plain_open()-> list:
    """ Loads a CSV and returns it's data
    inside a list.

    If you can see this requires data requires a lot
    of cleaning because each row of the csv is still
    comma sepparated, and still contains newlines, and
    maybe will contain all sorts of funny things.

    Returns:
        list: A list containing the data of the csv file
    """
    with open(file='./weather_data.csv', mode="r") as f:
        data = f.readlines()
    return data

def load_data_with_csv() -> list:
    """ Loads CSV and returns it's data inside a list.

    Returns:
        list: A list containing the data of the csv file
    """
    with open(file="./weather_data.csv", mode="r") as f:
        data_reader = csv.reader(f)
        data = []
        for row in data_reader:
            data.append(row)
    return data

def get_temperatures_list() -> list:
    """ Returns a list of temperatures when reading
    weather data.

    CSV is the in-built library, still not great. If we
    had more data, it's going to be very painful to work with

    Returns:
        list: A list containing the temperatures in the weather data
    """
    with open(file="./weather_data.csv", mode="r") as f:
        data_reader = csv.reader(f)
        temperatures = []
        for row in data_reader:
            if row[1] != "temp":
                temperatures.append(int(row[1]))
        return temperatures

def load_data_with_pandas() -> list:
    """ Loads CSV data using pandas

    Returns:
        list: A list containing the weather data
    """
    data = pandas.read_csv("./weather_data.csv")
    return data

if __name__ == '__main__':
    print("____Plain open____")
    data = load_data_plain_open()
    print(type(data))
    print(data)
    print()

    print("____CSV____")
    data = load_data_with_csv()
    print(type(data))
    print(data)
    temperatures_list = get_temperatures_list()
    print(temperatures_list)
    print()

    print("____Pandas____")
    data = load_data_with_pandas()
    print(type(data))
    print(data)
    print(type(data['temp']))
    print(data['temp'])
    print("Data frame to dict")
    data_dict = data.to_dict()
    print(type(data_dict))
    print(data_dict)
    print("Series to list")
    data_temp_list = data['temp'].to_list()
    print(type(data_temp_list))
    print(data_temp_list)