class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        table = {'(': 1, ')': -1, '{': 2, '}': -2, '[': 3, ']': -3}
        length = len(s)
        if length % 2 == 1:
            return False
        else:
            half_length = int(length / 2)
            center = 0
            while center < half_length:
                if table.get(s[center]) > 0 and table.get(s[center]) + table.get(s[center+1]) == 0:
                    bound = -1
                    for i in range(center-1,-1, -1):
                        if table.get(s[i]) < 0 or table.get(s[i]) + table.get(s[2*center + 1 - i]) != 0:
                            bound = i
                            break
                    s = s[0 : bound+1] + s[2 * center - bound+1 : ]
                    half_length = half_length-center+bound
                    center = 0
                else:
                    center += 1
        return not center

if __name__ == "__main__":
    solution = Solution()
    print (solution.isValid("(([]))"))


import user as u
import pyodbc
import dbprovider as udb

class UserContainer:

    def __init__(self, user_db: udb.DBProvider, soft_max_users=50):
        self.users = []
        self.userDB = user_db
        self.soft_max_users = soft_max_users

    def get_user(self, user_id):

        if len(self.users) > self.soft_max_users:
            self.remove_unused_users()

        user = [us for us in self.users if us.user_id == user_id]
        if len(user) == 0:
            user = self.userDB.get_user(user_id)

            if user is None:
                user = u.User(user_id)
                self.userDB.save_user(user)

            self.users.append(user)
        else:
            user = user[0]

        return user

    def update_user_to_bd(self, user_id):
        user = [us for us in self.users if us.user_id == user_id]
        if len(user) != 0:
            self.userDB.update_user(user[0])

    def remove_unused_users(self):
        unused_users = [us for us in self.users if us.status == u.UserState.free or us.status == u.UserState.unregister]
        for us in unused_users:
            self.users.remove(us)


from .....dirs import DIR_DATA_RAW, DIR_DATA_PROCESSED

def main(text):
    import pandas as pd
    import numpy as np

    dict_nigativ = [' не ', ' ни ']

    result = []
    for index, sample in enumerate(text):
        result.append(0)
        for i in dict_nigativ:
            if i in sample: result[index] = 1

    return result

if __name__ == '__main__': main()


#User function Template for python3

class Solution():
    def maximizeMinHeight(self, n, k, w, a):
        start=min(a)
        end=max(a)+k
        while(start<end-1):
            mid=(start+end)//2
            r=self.find(a,mid,w)
            if(r<=k):
                start=mid
            else:
                end=mid-1
        if(self.find(a,end,w)<=k):
            return end
        return start
    def find(self,arr,mid,w):
        start=0
        cost=0
        i=0
        ans=0
        tem=[0 for _ in range(len(arr))]
        while(i<len(arr)):
            if(i-start+1<=w):
                if(arr[i]+cost<mid):
                    tem[i]=(mid-arr[i]-cost)
                    ans+=tem[i]
                    cost+=tem[i]
            else:
                while((i-start+1)>w):
                    cost-=tem[start]
                    start+=1
                if(arr[i]+cost<mid):
                    tem[i]=(mid-arr[i]-cost)
                    cost+=tem[i]
                    ans+=tem[i]
            i+=1
        return ans

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

for _ in range(int(input())):
    n,k,w = map(int, input().split())
    arr = [int(i) for i in input().split()]
    print(Solution().maximizeMinHeight(n, k, w,arr))

# } Driver Code Ends


from typing import List

def twoSum(nums: List[int], target: int) -> List[int]:
    dnums = {num:i for i, num in enumerate(nums)}
    for i, num in enumerate(nums):
        complement = target - num
        if complement in dnums.keys() and i != dnums[complement]:
            return [i, dnums[complement]]

assert [0, 1] == twoSum([2, 7, 11, 15], 9)
assert [1, 2] == twoSum([3, 2, 4], 6)


from django.conf.urls import url
from arya_lib import views
#from django.conf.urls import patterns
urlpatterns = [
url(r'^$', views.login_page, name='login_page'),
url(r'check',views.check_user,name='check_status'),
url(r'issue',views.book_issue_render,name='book_issue_render'),
url(r'data_book',views.data_book,name='book_issue_data'),
url(r'search',views.book_search,name='book_search'),
url(r'home',views.main_page,name='main_page'),
url(r'book_find',views.search_in_db,name='book_find'),

]


import random
import sys
import pygame
import os # Let me import images
import json

# Constants
fps = 30
row = 16
col = 30
blockLength = 40
numMines = 99

try: # Loads .txt file
    with open('settings.txt') as txtFile:
        settings = json.load(txtFile)
    row = settings['rows']
    col = settings['columns']
    blockLength = settings['blockLength']
    numMines = settings['Mines']
except: # If there is no txt file the program will make one
    settings = {
        'rows': row,
        'columns': col,
        'blockLength': blockLength,
        'Mines': numMines
    }
    with open('settings.txt','w') as txtFile:
        json.dump(settings, txtFile)

# Sets up window
window = pygame.display.set_mode((blockLength*col, blockLength*row))

# Imports Images
coveredBlock = pygame.image.load(os.path.join('Images','block.png')).convert()
coveredBlock = pygame.transform.scale(coveredBlock, (blockLength, blockLength))

emptyBlock = pygame.image.load(os.path.join('Images','EmptyBlock.png')).convert()
emptyBlock = pygame.transform.scale(emptyBlock, (blockLength, blockLength))

flagBlock = pygame.image.load(os.path.join('Images','Flag.png')).convert()
flagBlock = pygame.transform.scale(flagBlock, (blockLength, blockLength))

mineBlock = pygame.image.load(os.path.join('Images','mine.png')).convert()
mineBlock = pygame.transform.scale(mineBlock, (blockLength, blockLength))

wrongMineBlock = pygame.image.load(os.path.join('Images','wrongMine.png')).convert()
wrongMineBlock = pygame.transform.scale(wrongMineBlock, (blockLength, blockLength))

oneBlock = pygame.image.load(os.path.join('Images','1.png')).convert()
oneBlock = pygame.transform.scale(oneBlock, (blockLength, blockLength))

twoBlock = pygame.image.load(os.path.join('Images','2.png')).convert()
twoBlock = pygame.transform.scale(twoBlock, (blockLength, blockLength))

threeBlock = pygame.image.load(os.path.join('Images','3.png')).convert()
threeBlock = pygame.transform.scale(threeBlock, (blockLength, blockLength))

fourBlock = pygame.image.load(os.path.join('Images','4.png')).convert()
fourBlock = pygame.transform.scale(fourBlock, (blockLength, blockLength))

fiveBlock = pygame.image.load(os.path.join('Images','5.png')).convert()
fiveBlock = pygame.transform.scale(fiveBlock, (blockLength, blockLength))

sixBlock = pygame.image.load(os.path.join('Images','6.png')).convert()
sixBlock = pygame.transform.scale(sixBlock, (blockLength, blockLength))

sevenBlock = pygame.image.load(os.path.join('Images','7.png')).convert()
sevenBlock = pygame.transform.scale(sevenBlock, (blockLength, blockLength))

eightBlock = pygame.image.load(os.path.join('Images','8.png')).convert()
eightBlock = pygame.transform.scale(eightBlock, (blockLength, blockLength))

class field:
    def __init__(self):
        self.openSetup() # this is to make sure self.open and self.hidden
        self.hiddenSetup() # are set up even if the methods are not called

    def openSetup(self): # Sets up visable field
        open = [['#' for i in range(col+2)] for j in range(row)]
        for i in range(0,row):
            open[i][0] = '|'
            open[i][col + 1] = '|'
        xlabel = []
        x = '|'
        for i in range(0,col+2):
            xlabel.append(x)
        open.insert(0,xlabel)
        open.insert(row + 1, xlabel)

        self.open = open

    def hiddenSetup(self): # Sets up hidden field
        # First build and empty array with borders
        hidden = [[' ' for i in range(col+2)] for j in range(row)]
        for i in range(0, row):
            hidden[i][0] = '|'
            hidden[i][col + 1] = '|'
        xlabel = []
        x = '|'
        for i in range(0, col + 2):
            xlabel.append(x)
        hidden.insert(0,xlabel)
        hidden.insert(row + 1,xlabel)

        # Then add the mines to the field
        z = 0
        while(z < numMines):
            x = random.randint(1,row)
            y = random.randint(1,col)
            if (hidden[x][y] == '*'):
                z = z - 1
            hidden[x][y] = '*'
            z = z + 1

        # Then places numbers next to the mines
        for i in range(1,row+1):
            for j in range (1, col+1):
                if (hidden[i][j] != '*'):
                    numSquare = countMines(hidden, i, j)
                    hidden[i][j] = numSquare

        self.hidden = hidden

def countMines(mines,rowInput,colInput): # Count how many mines are next to the square
    numMines = 0
    if (mines[rowInput-1][colInput-1] == '*'): # Check top left
        numMines = numMines + 1
    if (mines[rowInput-1][colInput] == '*'): # Check top
        numMines = numMines + 1
    if (mines[rowInput-1][colInput+1] == '*'): # Check Top right
        numMines = numMines + 1
    if (mines[rowInput][colInput-1] == '*'): # Check left
        numMines = numMines + 1
    if (mines[rowInput][colInput+1] == '*'): # Check right
        numMines = numMines + 1
    if (mines[rowInput+1][colInput-1] == '*'): # Check bottom left
        numMines = numMines + 1
    if (mines[rowInput+1][colInput] == '*'): # Check bottom
        numMines = numMines + 1
    if (mines[rowInput+1][colInput+1] == '*'): # Check bottom right
        numMines = numMines + 1
    return numMines

def checkEmpty(open,hidden,rowInput,colInput): # This function expands the field when a the user uncovers an empty square
    hidden[rowInput][colInput] = '!'
    check = True

    # This block of code uncovers the marked squre with '!'. It then checks adjacenct squares with '0'. then marks them with '!'. This goes on until there are no more '!'
    while (check):
        check = False
        for i in range(1,row + 1):
            for j in range(1,col + 1):
                if (hidden[i][j] == '!'):
                    open[i][j] = ' '
                    hidden[i][j] = ' '
                    check = True
                    if (hidden[i-1][j-1] != 0): # Check top left
                        open[i-1][j-1] = hidden[i-1][j-1]
                    else:
                        hidden[i-1][j-1] = '!'
                    if (hidden[i-1][j] != 0): # Check top
                        open[i-1][j] = hidden[i-1][j]
                    else:
                        hidden[i-1][j] = '!'
                    if (hidden[i-1][j+1] != 0): # Check Top right
                        open[i-1][j+1] = hidden[i-1][j+1]
                    else:
                        hidden[i-1][j+1] = '!'
                    if (hidden[i][j-1] != 0): # Check left
                        open[i][j-1] = hidden[i][j-1]
                    else:
                        hidden[i][j-1] = '!'
                    if (hidden[i][j+1] != 0): # Check right
                        open[i][j+1] = hidden[i][j+1]
                    else:
                        hidden[i][j+1] = '!'
                    if (hidden[i+1][j-1] != 0): # Check bottom left
                        open[i+1][j-1] = hidden[i+1][j-1]
                    else:
                        hidden[i+1][j-1] = '!'
                    if (hidden[i+1][j] != 0): # Check bottom
                        open[i+1][j] = hidden[i+1][j]
                    else:
                        hidden[i+1][j] = '!'
                    if (hidden[i+1][j+1] != 0): # Check bottom right
                        open[i+1][j+1] = hidden[i+1][j+1]
                    else:
                        hidden[i+1][j+1] = '!'
    return open

def pos2Index(): # Converts the posion of the mouse input to the array index
    xPos, yPos = pygame.mouse.get_pos()
    j = (xPos//blockLength) + 1
    i = (yPos//blockLength) + 1
    return i, j

def checkWrongFlag(open, hidden): # Tell the player where all the misplaced flags where on the field
    for i in range(1,row + 1):
        for j in range(1,col + 1):
            if (open[i][j] == 'f' and hidden[i][j] != '*'):
                hidden[i][j] = 'x'
    pygame.display.set_caption("\
                                \
                                \
                                \
                                \
                                \
            Game Over")
    return hidden

def checkWinCondition(open, hidden): # Checks if the player won the game
    x = 0
    run = True
    for i in range(1,row + 1):
        for j in range(1,col + 1):
            if (open[i][j] == '#' and hidden[i][j] != '*'):
                x = x + 1
    if (x == 0): # If the player won, the blocks without flags get flags on them
        for i in range(1,row + 1):
            for j in range(1,col + 1):
                if (open[i][j] == '#'):
                    open[i][j] = 'f'
        pygame.display.set_caption("\
                                    \
                                    \
                                    \
                                    \
                            You Win!")
        run = False
    return run

def placeFlag(open, numFlags, i, j): # Places or removes a flag i fth player right clicks a block
    if (open[i][j] == '#'):
        open[i][j] = 'f'
        numFlags = numFlags + 1
    elif (open[i][j] == 'f'):
        open[i][j] = '#'
        numFlags = numFlags - 1
    pygame.display.set_caption("Number of Mines: "+str(numMines-numFlags)+"\
                                                                           \
                                                            Minesweeper")
    return numFlags, open

def drawScreen(open):  # Draws the field
    window.fill("black")
    for i in range(1,row + 1):
        for j in range(1, col + 1):
            if (open[i][j] == '#'):
                window.blit(coveredBlock,(blockLength*(j - 1), blockLength*(i - 1)))
            elif (open[i][j] == ' '):
                window.blit(emptyBlock,(blockLength*(j - 1), blockLength*(i - 1)))
            elif (open[i][j] == '*'):
                window.blit(mineBlock,(blockLength*(j - 1), blockLength*(i - 1)))
            elif (open[i][j] == 'f'):
                window.blit(flagBlock,(blockLength*(j - 1), blockLength*(i - 1)))
            elif (open[i][j] == 'x'):
                window.blit(wrongMineBlock,(blockLength*(j - 1), blockLength*(i - 1)))
            elif (open[i][j] == 0):
                window.blit(emptyBlock,(blockLength*(j - 1), blockLength*(i - 1)))
            elif (open[i][j] == 1):
                window.blit(oneBlock,(blockLength*(j - 1), blockLength*(i - 1)))
            elif (open[i][j] == 2):
                window.blit(twoBlock,(blockLength*(j - 1), blockLength*(i - 1)))
            elif (open[i][j] == 3):
                window.blit(threeBlock,(blockLength*(j - 1), blockLength*(i - 1)))
            elif (open[i][j] == 4):
                window.blit(fourBlock,(blockLength*(j - 1), blockLength*(i - 1)))
            elif (open[i][j] == 5):
                window.blit(fiveBlock,(blockLength*(j - 1), blockLength*(i - 1)))
            elif (open[i][j] == 6):
                window.blit(sixBlock,(blockLength*(j - 1), blockLength*(i - 1)))
            elif (open[i][j] == 7):
                window.blit(sevenBlock,(blockLength*(j - 1), blockLength*(i - 1)))
            elif (open[i][j] == 8):
                window.blit(eightBlock,(blockLength*(j - 1), blockLength*(i - 1)))
    pygame.display.update()

def main():
    field.openSetup()
    field.hiddenSetup()
    numFlags = 0
    pygame.display.set_caption("Number of Mines: "+str(numMines-numFlags)+"\
                                                                           \
                                                            Minesweeper")
    clock = pygame.time.Clock()
    run = True
    firstBlock = True

    while (run):
        clock.tick(fps)

        for event in pygame.event.get():
            if (event.type == pygame.QUIT):
                pygame.quit()
                sys.exit()
            mousePress = pygame.mouse.get_pressed()
            if (event.type == pygame.MOUSEBUTTONDOWN and mousePress[0]):
                i, j = pos2Index()
                if (firstBlock):
                    while(field.hidden[i][j] != 0):
                        field.hiddenSetup()
                    firstBlock = False
                if (field.hidden[i][j] == '*' and field.open[i][j] != 'f'):
                    field.open = checkWrongFlag(field.open, field.hidden)
                    run = False
                elif (field.hidden[i][j] == 0 and field.open[i][j] != 'f'):
                    field.open = checkEmpty(field.open, field.hidden,i,j)
                    run = checkWinCondition(field.open, field.hidden)
                elif (field.open[i][j] != 'f'):
                    field.open[i][j] = field.hidden[i][j]
                    run = checkWinCondition(field.open, field.hidden)
            if (event.type == pygame.MOUSEBUTTONDOWN and mousePress[2]):
                i, j = pos2Index()
                numFlags, field.open = placeFlag(field.open, numFlags, i, j)

        drawScreen(field.open)

    while(run != True):
        clock.tick(fps)

        for event in pygame.event.get():
            if (event.type == pygame.QUIT):
                pygame.quit()
                sys.exit()
            if (event.type == pygame.MOUSEBUTTONDOWN):
                run = True

    main()

if (__name__ == "__main__"):
    field = field()
    main()


#Ordenar lista por BubbleSort
#Ordenar lista por SelectSort
#Ordenar lista por MergeSort

lista  = [7,6,5,4,3,2,1];
lista2 = [9,8,7,6,5,4,3,2,1,0];
def Bubblesort(lista):
    n = len(lista);
    for j in range(n):
        ordenado = False;
        for i in range(n-1):
            if (lista[i] > lista[i+1]):
                troca = lista[i];
                lista[i] , lista[i+1] =lista[i+1], lista[i] ;
                troca = lista[i+1];
                ordenado = True;
        if (ordenado == False):
            break;
    print(j, lista);

def SelectSort(lista2):
    for i in range(0,len(lista2)-1):
        valorminimo = i;
        for j  in range(i+1, len(lista2)):
            if(lista2[j] < lista2[valorminimo]):
                valorminimo = j
        if (valorminimo != i):
            lista2[i], lista2[valorminimo] = lista2[valorminimo], lista2[i];
    print(j, lista2)

###################################################################
bubble = Bubblesort(lista);
select = SelectSort(lista2);


from django.contrib import admin
from django.urls import path
from core_api.views import(
    EmployeeListAPIView,
    EmployeeCreateAPIView,
    EmployeeDetailAPIView,
    EmployeeUpdateAPIView,
    EmployeeDestroyAPIView,
    EmployeeCreateAPIView,
    error,
)

urlpatterns = [
    path('admin/', admin.site.urls),
    # LIST all employees
    path("api/list-employee/", EmployeeListAPIView.as_view(),
        name="api-employee-list"),

    # CREATE a new employee
    path("api/create-employee/", EmployeeCreateAPIView.as_view(),
        name="api-create-employee"),

    # RETRIEVE a deleted employee
    path("api/retrieve-employee/<int:pk>",
        EmployeeDetailAPIView.as_view(), name="api-retrieve-employee"),

    # UPDATE employee's data
    path("api/update-employee/<int:pk>", EmployeeUpdateAPIView.as_view(),
        name="api-update-employee"),

    # DELETE an employee
    path("api/delete-employee/<int:pk>", EmployeeDestroyAPIView.as_view(),
        name="api-delete-employee"),

    # Error Handling
    path('error/', error, name='error'),

]


# coding=UTF-8
#

'''
@File: GPT2_train_total
@Author: WeiWei
@Time: 2023/3/23
@Email: weiwei_519@outlook.com
@Software: PyCharm
'''

# project external dependency
import os
import argparse
import time
import logging
import glob
import torch
from torch import nn
from torch.nn import Identity, CrossEntropyLoss
from torch import softmax
from torch.optim import AdamW
from torch.utils.data import DataLoader
import numpy as np
import docx2txt
import pandas as pd
from tqdm import tqdm
from rouge import Rouge
from dataclasses import dataclass
from typing import Optional, Tuple
from transformers import get_scheduler
from transformers.models.gpt2 import GPT2Config, GPT2LMHeadModel, GPT2Tokenizer
from transformers.utils import ModelOutput

# Project internal dependency
from utils.dataset_util import GPT2Dataset, preprocess, postprocess

# device : GPU or CPU
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
logging.basicConfig(level=logging.INFO)
pad_token_id = 0

# 训练参数
def setup_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--device', default='cuda:0', type=str,
                        help='cuda:n——training with GPU, cpu——training with CPU')
    parser.add_argument('--pretrained_model_dir', default="../models/chatgpt-aia-chinese/gpt-aia-chinese/", type=str, help='')
    parser.add_argument('--model_output_dir', default="../models/chatgpt-aia-chinese/gpt-aia-chinese/", type=str,
                        help='')
    parser.add_argument('--doc_path', default='../datasets/company_datasets/aiacn/', type=str, help='')
    parser.add_argument('--action', default='validate', type=str,
                        help='train训练/validate测试/checkpoint继续训练/fine-tuning微调模型/prod生产运行')
    parser.add_argument('--with_pretrained_mode', default=False, type=str,
                        help='采用空模型，还是预训练模型')
    parser.add_argument('--batch_size', default=2, type=int, required=False, help='batch size')
    parser.add_argument('--epochs', default=10000, type=int, required=False, help='epochs')
    parser.add_argument('--max_length', default=200, type=int, required=False, help='max context length')
    parser.add_argument('--warmup_steps', default=2000, type=int, required=False, help='warm up steps')
    parser.add_argument('--lr', default=1.5e-4, type=float, required=False, help='learn rate')
    parser.add_argument('--max_grad_norm', default=1.0, type=float, required=False)
    parser.add_argument('--log_step', default=10, type=int, required=False, help='print log steps')
    return parser.parse_args()

def rouge(not_ignore, shift_labels, preds):
    main_rouge = Rouge()
    true_length = [w.sum() for w in not_ignore.float()]
    rouge_labels = []
    rouge_predicts = []
    for idx, tmp_len in enumerate(true_length):
        tmp_labels = shift_labels[idx][:int(tmp_len)]
        rouge_labels.append(" ".join([str(w) for w in tmp_labels.tolist()]))
        tmp_pred = preds[idx][:int(tmp_len)]
        rouge_predicts.append(" ".join([str(w) for w in tmp_pred.tolist()]))
    rouge_score = main_rouge.get_scores(rouge_predicts, rouge_labels, avg=True)
    return rouge_score

def calculate_loss_and_accuracy(outputs, labels, device):
    global pad_token_id
    logits = outputs.logits
    # Shift so that tokens < n predict n
    shift_logits = logits[..., :-1, :].contiguous()
    shift_labels = labels[..., 1:].contiguous().to(device)

    # Flatten the tokens
    loss_fct = CrossEntropyLoss(ignore_index=pad_token_id, reduction='sum')
    loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))

    _, preds = shift_logits.max(dim=-1)
    not_ignore = shift_labels.ne(pad_token_id)
    num_targets = not_ignore.long().sum().item()

    correct = (shift_labels == preds) & not_ignore
    correct = correct.float().sum()

    accuracy = correct / num_targets
    loss = loss / num_targets

    rouge_score = rouge(not_ignore, shift_labels, preds)
    return loss, accuracy, rouge_score

def collate_fn(batch):
    global pad_token_id
    input_ids = []
    input_lens_list = [len(w) for w in batch]
    max_input_len = max(input_lens_list)
    for btc_idx in range(len(batch)):
        input_len = len(batch[btc_idx])
        input_ids.append(batch[btc_idx])
        input_ids[btc_idx].extend([pad_token_id] * (max_input_len - input_len))
    # print(f"input_ids shape: {len(input_ids)}x{len(input_ids[0])}")
    # for l, input_id in enumerate(input_ids):
    #     for r, id in enumerate(input_id):
    #         if not isinstance(id, int):
    #             print(f"line:{l}, row:{r}, id:{id}")
    return torch.tensor(input_ids, dtype=torch.long)

def data_loader(args, doc_path, tokenizer, shuffle):

    # 初始训练，基于原始doc格式文本数据集进行GPT模型训练
    texts = []
    if args.action == 'train' or args.action == 'checkpoint':
        doc_path = '../datasets/company_datasets/aiacn/'
        files = os.listdir(doc_path)
        for file in files:
            doc_content = docx2txt.process(doc_path + file)
            # doc_content = "".join(doc_content.split())   # 去掉空格
            # 对于超出content_length限制的文本，需要进行拆分处理。
            if len(doc_content) <= args.text_length:
                texts.append(doc_content)
            else:
                r = 0
                for i in range(len(doc_content) // args.text_length):
                    texts.append(doc_content[i * args.text_length:(i + 1) * args.text_length])
                    r += 1
                texts.append(doc_content[r * args.text_length:len(doc_content)])
        input_ids = []
        for text in tqdm(texts):
            input_id = tokenizer.encode(text)
            input_ids.append(input_id)
    elif args.action == 'fine-tuning':
        doc_path = '../datasets/company_datasets/aiacn/Prompt_Finetuning.xlsx'
        content = pd.read_excel(doc_path)
        print(content.head(5))
        text_pairs = content.iloc[:, [0, 1]]
        source_texts = content.iloc[:, 0].values.tolist()
        target_texts = content.iloc[:, 1].values.tolist()
        paraphs = [src + tgt for src, tgt in zip(source_texts, target_texts)]

    dataset = GPT2Dataset(input_ids, tokenizer)
    dataloader = DataLoader(dataset=dataset,
                            batch_size=args.batch_size,
                            shuffle=shuffle,
                            collate_fn=collate_fn)

    return dataloader

def train(args, model, dataloader):
    # 开始模型训练
    pre = time.time()
    num_training_steps = args.epochs * len(dataloader)
    optimizer = AdamW(model.parameters(), lr=args.lr)
    lr_scheduler = get_scheduler(
        name="linear",
        optimizer=optimizer,
        num_warmup_steps=args.warmup_steps,
        num_training_steps=num_training_steps
    )
    model.train()
    batch_steps = 0
    for epoch in range(args.epochs):
        for batch in dataloader:
            batch_steps += 1
            inputs = {"input_ids": batch.to(device)}
            outputs = model(**inputs, labels=batch.to(device))
            # loss = outputs.loss
            loss, acc, rouge_score = calculate_loss_and_accuracy(outputs, batch.to(device), device)
            loss.backward()
            torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)
            optimizer.step()
            lr_scheduler.step()
            optimizer.zero_grad()
            if batch_steps % args.log_step == 0:
                print("train epoch {}/{}, batch {}/{}, loss {}, accuracy {}, rouge-1 {}, rouge-2 {}, rouge-l {}".format(
                    epoch, args.epochs,
                    batch_steps,
                    num_training_steps,
                    loss, acc,
                    rouge_score["rouge-1"]['f'],
                    rouge_score["rouge-2"]["f"],
                    rouge_score["rouge-l"]["f"]))

        model_to_save = model.module if hasattr(model, 'module') else model
        model_to_save.save_pretrained(args.model_output_dir)
    # torch.save(model, os.path.join(args.final_model_path, 'gpt2_WenAn.pth'))

    print('训练时间：', time.time() - pre)

def evaluate(dataloader, args):
    model, _ = load_model(args)
    model.to(device)
    model.eval()
    loss_list, acc_list, rouge_1_list, rouge_2_list, rouge_l_list = [], [], [], [], []
    batch_steps = 0
    with torch.no_grad():
        for batch in dataloader:
            batch_steps += 1
            inputs = {"input_ids": batch.to(device)}
            outputs = model(**inputs, labels=batch.to(device))
            loss, acc, rouge_score = calculate_loss_and_accuracy(outputs, batch.to(device), device)
            loss_list.append(float(loss))
            acc_list.append(float(acc))
            rouge_1_list.append(float(rouge_score["rouge-1"]['f']))
            rouge_2_list.append(float(rouge_score["rouge-2"]['f']))
            rouge_l_list.append(float(rouge_score["rouge-l"]['f']))
            print("eval batch {}/{}, loss {}, accuracy {}, rouge-1 {}, rouge-2 {}, rouge-l {}".format(
                batch_steps,
                len(dataloader),
                loss, acc,
                rouge_score["rouge-1"]['f'],
                rouge_score["rouge-2"]["f"],
                rouge_score["rouge-l"]["f"]))
    print("loss: {},".format(np.mean(loss_list)),
          "accuracy: {}.".format(np.mean(acc_list)),
          "rouge-1: {},".format(np.mean(rouge_1_list)),
          "rouge-2: {},".format(np.mean(rouge_2_list)),
          "rouge-l: {}".format(np.mean(rouge_l_list)))

def load_model(args):
    # 加载预训练模型，若不存在则初始化空模型
    checkpoint = glob.glob(os.path.join(args.model_output_dir, 'checkpoint-*'))  # 按照目前trainer的训练输出，只会存在一个checkpoint
    if len(checkpoint) > 0 and args.action == 'checkpoint':
        # 从checkpoint断点继续训练
        checkpoint = (checkpoint[0]).replace("\\", "/")
        tokenizer = GPT2Tokenizer.from_pretrained(checkpoint)
        config = GPT2Config.from_pretrained(checkpoint)
        model = GPT2LMHeadModel.from_pretrained(checkpoint)
    elif args.action == 'fine-tuning':
        # 对GPT2模型进行调优
        tokenizer = GPT2Tokenizer.from_pretrained(args.model_output_dir)
        config = GPT2Config.from_pretrained(args.model_output_dir)
        model = GPT2LMHeadModel.from_pretrained(args.model_output_dir)
    else:
        if args.with_pretrained_mode:
            # 加载预训练模型
            tokenizer = GPT2Tokenizer.from_pretrained(args.pretrained_model_dir)
            config = GPT2Config.from_pretrained(args.pretrained_model_dir)
            model = GPT2LMHeadModel.from_pretrained(args.pretrained_model_dir, config=config)
        else:
            # 加载空模型
            tokenizer = GPT2Tokenizer.from_pretrained(args.pretrained_model_dir)
            config = GPT2Config.from_pretrained(args.pretrained_model_dir)
            model = GPT2LMHeadModel(config=config)

    model.to(args.device)
    model_size = sum(t.numel() for t in model.parameters())
    print(f"model size: {model_size / 1000 ** 2:.1f}M parameters")
    # gpu_tracker.track()
    return config, tokenizer, model

def model_train(args):
    # 加载预训练模型或空模型
    config, tokenizer, model = load_model(args=args)
    global pad_token_id
    pad_token_id = tokenizer.eos_token_id
    train_dataloader = data_loader(args, args.doc_path, tokenizer=tokenizer, shuffle=True)
    eval_dataloader = data_loader(args, args.doc_path, tokenizer=tokenizer, shuffle=False)

    print('dataset''s shape = {0}'.format(len(train_dataloader)))

    train(
        args=args,
        model=model,
        dataloader=train_dataloader
    )

@dataclass
class CausalLMOutputWithCrossAttentions(ModelOutput):
    loss: Optional[torch.FloatTensor] = None
    logits: torch.FloatTensor = None
    past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None
    hidden_states: Optional[Tuple[torch.FloatTensor]] = None
    attentions: Optional[Tuple[torch.FloatTensor]] = None
    cross_attentions: Optional[Tuple[torch.FloatTensor]] = None
    value: Optional[torch.FloatTensor] = None

# 模型测试
def infer_answer(input_text, tokenizer, model, do_sample=False, return_seqs=1):
    input_ids = []
    input_ids.extend(tokenizer.encode(input_text))
    input_ids = input_ids[:-1]
    for i in range(args.max_length):
        inputs = {"input_ids": torch.tensor([input_ids]).to(device)}
        outputs = model(**inputs)
        logits = outputs.logits
        last_token_id = int(torch.argmax(logits[0][-1]))
        last_token = tokenizer.convert_ids_to_tokens(last_token_id)
        input_text += tokenizer.convert_tokens_to_string(last_token)
        input_ids.append(last_token_id)
    return input_text

if __name__ == '__main__':
    args = setup_args()
    if args.action == 'train' or args.action == 'checkpoint' or args.action == 'fine-tuning':
        model_train(args)
    elif args.action == 'validate':
        # 加载预训练模型：
        tokenizer = GPT2Tokenizer.from_pretrained(args.model_output_dir)
        model = GPT2LMHeadModel.from_pretrained(args.model_output_dir)
        model.to(device)
        cont = True
        while cont:
            sep = 100
            input_text = str(input("请输入/Please input： "))

            if input_text == "exit":
                cont = False
            else:
                # output_text = infer_answer(input_text,
                #                            tokenizer=tokenizer,
                #                            model=model,
                #                            do_sample=False,
                #                            return_seqs=1,
                #                            )
                output_text = infer_answer(input_text=input_text, tokenizer=tokenizer, model=model)
                if isinstance(output_text, list):
                    for text in output_text:
                        idx = 0
                        for i in range(len(text) // sep):
                            print(text[i * sep:(i + 1) * sep])
                            idx = i + 1
                        print(text[idx * sep:-1])
                else:
                    idx = 0
                    for i in range(len(output_text) // sep):
                        print(output_text[i * sep:(i + 1) * sep])
                        idx = i + 1
                    print(output_text[idx * sep:-1])
                # print(output_text)


# app.py
# Serves all the servers

from flask import Flask
import os, time
from random import randint
import requests

app = Flask(__name__)

@app.route('/')
def sample():
    time.sleep(randint(3,10))
    return "This is the {} application. Serving on {}.".format(os.environ["APP"], os.environ["ENDPOINT"])

@app.route('/healthcheck')
def healthcheck():
    return "OK"

if __name__ == '__main__':
    app.run(host="0.0.0.0", debug=True)


"C-lime error messages"

__all__ = [
    "check_status",
]

from . import lib

MESSAGES = {
    lib.LIME_ERR_LAST_NOT_WRITTEN: "Last not written error",
    lib.LIME_ERR_PARAM: "Parameter error",
    lib.LIME_ERR_HEADER_NEXT: "Error with next record",
    lib.LIME_ERR_WRITE: "Writing error",
    lib.LIME_ERR_READ: "Reading error",
    lib.LIME_ERR_SEEK: "Seeking error",
    lib.LIME_ERR_MBME: "MB/ME flags incorrect",
    lib.LIME_ERR_CLOSE: "Closing error",
    # Not errors
    lib.LIME_LAST_REC_WRITTEN: "Not an error: last record written",
    lib.LIME_EOR: "Not an error: end of record",
    lib.LIME_EOF: "Not an error: end of file",
}

def check_status(status):
    "Checks the status integer returned by c-lime and raise error if needed"
    if status == 0:
        return

    raise RuntimeError(MESSAGES.get(status, "Unknown error"))


"""
	Collect all Medians from CCDB per genus and choose the value according to:
	merge counts of duplicate accepted,
	merge counts of duplicate unresolved,
	filter out unresolved when accepted exists
"""

__author__ = 'MichalDrori'

DEBUG_FLAG = 0

import argparse
import csv
from collections import Counter
import sqlite3
#from taxonome.taxa.file_csv import load_taxa
#from taxonome.taxa.name_selector import NameSelector
#from taxonome.tracker import CSVTaxaTracker, _flatten_name, CSVListMatches
import unicodedata

import pickle
import sys
import os
from collections import namedtuple

# Running line:
#python /groups/itay_mayrose/michaldrori/Pipeline_Files/CCDB/make_ccdb_counts_files_for_chromevol.py Pipeline_Files/Alignment_TaxonList/Taxon_list_origin.txt

CCDB_DATABASE = '/groups/itay_mayrose/michaldrori/MD_ChromEvol/CCDB_1.46.db'

def new_90prec_max(last_dist_str):
#example 24=0.270000_16=0.440000_8=0.100000_20=0.120000_28=0.010000_40=0.010000_12=0.030000_32=0.010000_23=0.010000

	if DEBUG_FLAG == 1: print("new_90prec_max------------------------------")
	count_prec_dict=dict()
	split_data = last_dist_str.strip().split('_')
	if DEBUG_FLAG == 1: print('pppppppppppppppppppppppppp')
	if DEBUG_FLAG == 1: print(split_data)
	for item in split_data:
		if DEBUG_FLAG == 1: print('ppppppppppppppppppppppp')
		if DEBUG_FLAG == 1: print(item)
		count = int(item.split('=')[0])
		prec = item.split('=')[1]
		count_prec_dict[count]=prec

	count_max_sort = sorted(count_prec_dict.keys(),reverse=True)
	if DEBUG_FLAG == 1: print(count_max_sort)
	sum_prec=0.0000
	for count in count_max_sort:
		sum_prec+=float(count_prec_dict[count])
		if DEBUG_FLAG == 1: print(sum_prec)
		if DEBUG_FLAG == 1: print(count)
		if sum_prec > 0.1:
			return count
		else:
			print("Still....checking")

	return New_max

def break_doubles(species_dist_dict):

	clean_list=[]
	for item in species_dist_dict:
		if not item:
			continue
		elif ',' in item:
			# Split counts in case more than one:
			for count_item in item.split(','):
				clean_list.append(count_item.strip())
		else:
			clean_list.append(item)

	return clean_list

def check_max_value(dist_dict):
	if DEBUG_FLAG == 1: print(dist_dict.keys())
	max_count = max(dist_dict.keys())
	if dist_dict[max_count] >= 0.1:
		return max_count
	else:
		del dist_dict[max_count]
		new_max_count = max(dist_dict.keys())
		return new_max_count

#max,dist = calc_distribution(species_dist_dict)
def calc_distribution(species_dist_dict,path):

	#split data which include list of counts for one count
	min_f = open(path + '_min', 'w')
	max_f = open(path + '_max', 'w')
	dist_f = open(path + '_dist', 'w')

	lines_max=''
	lines_min=''
	lines_dist=''
	for specie in species_dist_dict.keys():
		specie_ = escape_organism_name(specie)
		lines_dist += '>%s\n'%specie_.replace(' ','_')
		numberOfcounts = 0
		Sum_counts = 0
		max_count = -1
		min_count = 999999
		list_single_data = break_doubles(species_dist_dict[specie])
		dist_counts = Counter(list_single_data)
		for count in list_single_data:
			if not count:
				continue
			else:
				Sum_counts+=int(count)
				if int(count) > max_count:
					max_count = int(count)
				if int(count) < min_count:
					min_count = int(count)
				numberOfcounts+=1
		dist_str=''
		dist_dict = dict()
		max_prec=-1
		max_item='null'
		sum_prec=0
		for item in dist_counts.keys():
			dist_str+=str(item)
			prectg = round(float(dist_counts[item])/float(numberOfcounts),2)
			sum_prec+=prectg
			if prectg > max_prec:
				max_prec = prectg
				max_item = item
			dist_str += "=%f_" % prectg
			dist_dict[int(item)]=prectg
		if float(sum_prec) == float(1):
			print('Sum is 1')
		else:
			diff_prec = float(1)-sum_prec
			if DEBUG_FLAG == 1: print("diff_prec %f" %diff_prec)
			if DEBUG_FLAG == 1: print("max_prec Before %f" % max_prec)
			max_prec+=diff_prec
			if DEBUG_FLAG == 1: print("max_prec After %f" % max_prec)
			dist_str = ''
			for item in dist_counts.keys():
				dist_str+=str(item)
				prectg = round(float(dist_counts[item])/float(numberOfcounts),2)
				if item == max_item:
					prectg=max_prec
				dist_str += "=%f_" % prectg
				dist_dict[int(item)] = prectg
		#check max value to be at least 0.1 of dist, otherwise replace with next value:
		#if dist_dict.keys():
		#	final_max = check_max_value(dist_dict)
		#else:
		#	final_max = max_count
		last_dist_str = dist_str[:-1]
		if last_dist_str:
			New_max = new_90prec_max(last_dist_str)
		else:
			New_max=max_count
		#lines_max += '%s\n'%str(final_max)
		if New_max == -1:
			print("%s, No maximum found" %specie_)
		else:
			lines_max += '>%s\n' % specie_.replace(' ', '_')
			lines_max += '%s\n'%str(New_max)
		if min_count == 999999:
			print("%s, No minimum found" % specie_)
		else:
			lines_min += '>%s\n' % specie_.replace(' ', '_')
			lines_min += '%s\n'%str(min_count)
		lines_dist += '%s\n'%str(last_dist_str)

	max_f.write(lines_max)
	min_f.write(lines_min)
	dist_f.write(lines_dist)

	return
#----------------------------------------------------------------------------------
def escape_organism_name(organism_name):
	if organism_name is None:
		return None

	escaped_organism = organism_name
	#Added space after � to be aligned with the name in the plant list:
	escaped_organism = escaped_organism.replace("×", "x")
	escaped_organism = escaped_organism.replace(" ", "_")
	escaped_organism = escaped_organism.replace(",", "_")
	escaped_organism = escaped_organism.replace("-", "_")
	escaped_organism = escaped_organism.replace("'", "_")
	escaped_organism = escaped_organism.replace("/", "_")
	escaped_organism = escaped_organism.replace("&", "AND")
	escaped_organism = unicodedata.normalize('NFKD', escaped_organism).encode('ascii','ignore')
	escaped_organism = str(escaped_organism, encoding='ascii')

	return escaped_organism
#--------------------------------------------------------------------------------------
def get_species_list_for_genus (genus,db_curser):
	if DEBUG_FLAG == 1: print("##" + genus + "\n")
	if DEBUG_FLAG == 1: print("-----------------------------\n")
	species_names=[]
	species_list_unique=[]
	query_genus_species_db = "SELECT resolved_name_full FROM resolved_chrom_counts WHERE genus like '%s'" % genus
	db_curser.execute(query_genus_species_db)
	rows_genus_db = db_curser.fetchall()
	for line in rows_genus_db:
		species_names.append(line[0])
	species_list_unique=set(species_names)
	if DEBUG_FLAG == 1: print(species_list_unique)
	return species_list_unique
#--------------------------------------------------------------------------------------
def get_counts_for_species (genus,db_curser):

	query_species_counts_db = "SELECT id,genus,resolved_name_full,resolved_name,median,status FROM resolved_chrom_counts WHERE resolved_name like '%s%%'" % genus
	db_curser.execute(query_species_counts_db)
	rows_genus_db = db_curser.fetchall()

	return rows_genus_db
#--------------------------------------------------------------------------------------

def get_ploidy_data_all(ploidy_file,file_handle):
	ploidy_dict = {}
	chromCount_dict = {}
	with open(ploidy_file,'r') as f:
		reader = csv.DictReader(f)
		indx=0
		for taxon_line in reader:
			species_name=taxon_line['Taxon']
			if DEBUG_FLAG == 1: print(">>>>>>>>>>>>>>>>>>>>>>>>>>>")
			if DEBUG_FLAG == 1: print(species_name)
			chromCount_dict[species_name] = taxon_line['Chromosome count']
			ploidy_dict[species_name] = taxon_line['Ploidy inference']
			indx+=1
			if DEBUG_FLAG == 1:
				file_handle.write("%s,%s,%s\n" % (species_name,chromCount_dict[species_name],ploidy_dict[species_name]))
		return (ploidy_dict,chromCount_dict)

def recalc_median(species_name,db_curser):
	list_parsed_n=[]
	query_species_parsed_n_db = "SELECT parsed_n FROM resolved_chrom_counts WHERE resolved_name LIKE '%s'" % species_name
	db_curser.execute(query_species_parsed_n_db)
	rows_genus_db = db_curser.fetchall()
	for line in rows_genus_db:
		line_list=list(line)
		if "None" in str(line_list):
			continue
		else:
			for item in line_list:
				parsed_n = item.replace(" ","")
				parsed_n = parsed_n.split(",")
				if '' in parsed_n:
					continue
				else:
					for parsed_val in parsed_n:
						list_parsed_n.append(int(parsed_val))
	if not list_parsed_n:
		new_median=0
	else:
		new_median = int(statistics.median(list_parsed_n))
	return new_median

#D:\Michal\scripts\make_ccdb_counts_files_for_chromevol.py

if __name__ == "__main__":

	#Count_type = 'Min' #options are: median, max, dist (distribution)
	genera_list_var = sys.argv[1]
	Output_ForCounts = sys.argv[2] #'/groups/itay_mayrose/michaldrori/MD_ChromEvol/Counts_ccdb'
	File_or_Name = sys.argv[3] # 0 -> File with a list of genera or 1 -> list of genera in string 'AAA,BBB,CCC'
	#species_list=[]
	out_file = "Counts.txt"
	#out_handle = open(out_file,mode="w",encoding='utf8',newline='')
	#writer = csv.writer(out_handle,delimiter=',')
	#writer.writerow(['genus','resolved_name_full','resolved_name','median','status'])
	#with open('/groups/itay_mayrose/michaldrori/Pipeline_Files/Alignment_TaxonList/Angio_Missing.txt','r') as f:
	if File_or_Name == '0':
		with open(genera_list_var,'r') as f:
			genera_list = [g.strip() for g in f.readlines()]
	elif File_or_Name == '1':
		genera_list= genera_list_var.split(',')

	file_out=open(Output_ForCounts + "/COUNTS_DATA_LOG.txt",mode="w",encoding='utf8',newline='')
	file_LessThan5Counts=open(Output_ForCounts + "/Genera_withLessThan5Counts.txt",mode="w",encoding='utf8',newline='')
	for genus in genera_list:
		species_dist_dict=dict()
		count_counts=0
		print(genus)
		counts_file = Output_ForCounts + '/' + genus + '.counts'
		counts_file=open(counts_file,'w')
		species_names=[]
		species_median_dict={}
		species_status_dict={}
		species_parsed_n_dict={}
		conn_db = sqlite3.connect(CCDB_DATABASE)
		db_curser = conn_db.cursor()
		query_genus_species_db = "SELECT id,genus,resolved_name_full,resolved_name,parsed_n,median,status from resolved_chrom_counts WHERE genus is '%s'" % genus
		#query_genus_species_db = "SELECT resolved_name FROM resolved_chrom_counts WHERE genus is '%s'" % genus
		db_curser.execute(query_genus_species_db)
		rows_genus_db = db_curser.fetchall()
		for line in rows_genus_db:
			specie_name = line[3]
			parsed_n = line[4]
			median_val = line[5]
			status = line[6]
			#Check for duplicate species names:
			if specie_name in species_names:
				if DEBUG_FLAG == 1: print(specie_name)
				if DEBUG_FLAG == 1: print(parsed_n)
				if DEBUG_FLAG == 1: print(status)
				#create data arrayes for max/dist calc
				#if status: # == 'Accepted':
				if specie_name in species_dist_dict.keys():
					species_dist_dict[specie_name].append(parsed_n)
				else:
					species_dist_dict[specie_name]=[parsed_n]
				#Check if same median value:
				if median_val == species_median_dict[specie_name]:
					#No need to add data
					continue
				#Check if different median and different status:
				else:
					#Check if Different status and take Accepted median:
					if status != species_median_dict[specie_name]:
						#Update values according to Accepted specie:
						if status == 'Accepted':
							#First remove the existing value if exists:
							if specie_name in species_names:
								file_out.write("%s is being removed, Accepted median was found\n" %specie_name)
								species_names.remove(specie_name)
								del species_median_dict[specie_name]
								del species_status_dict[specie_name]
								del species_parsed_n_dict[specie_name]
							species_names.append(specie_name)
							species_median_dict[specie_name]=median_val
							species_status_dict[specie_name]=status
							species_parsed_n_dict[specie_name]=parsed_n
							if specie_name in species_dist_dict.keys():
								species_dist_dict[specie_name].append(parsed_n)
							else:
								species_dist_dict[specie_name] = [parsed_n]
						else:
							continue
					else:
						#Different values to same status:
						file_out.write('## Need to re-calc median for %s\n' % specie_name)
						continue
			#Specie name not in list:
			else:
				species_names.append(specie_name)
				species_median_dict[specie_name]=median_val
				species_status_dict[specie_name]=status
				species_parsed_n_dict[specie_name]=parsed_n
				if specie_name in species_dist_dict.keys():
					species_dist_dict[specie_name].append(parsed_n)
				else:
					species_dist_dict[specie_name]=[parsed_n]

		if DEBUG_FLAG == 1: print("DDDDDDDDDDDDDD - > Dist by name:")
		if DEBUG_FLAG == 1: print(species_dist_dict)
		#Calc dist and max for each
		calc_distribution(species_dist_dict,Output_ForCounts + '/' + genus + '.counts')

		#species_list_unique=set(species_names)
		#file_out.write(str(species_list_unique))
		#file_out.write(str(len(species_list_unique)))
		for specie in species_names:
			if DEBUG_FLAG == 1: print(specie)
			if DEBUG_FLAG == 1: print("%s,%s\n" %(genus,specie))
			if DEBUG_FLAG == 1: file_out.write("%s,%s,%s,%s,%s\n" % (genus, specie,species_median_dict[specie],species_parsed_n_dict[specie],species_status_dict[specie]))
			specie_=escape_organism_name(specie)
			#specie_=specie_x.replace(' ','_')
			if not species_median_dict[specie]:
				file_out.write('### None value for  %s' % specie_name)
				continue
			else:
				counts_file.write('>%s\n' % specie_)
				counts_file.write('%s\n' % species_median_dict[specie])
				count_counts+=1
		#Check number of counts:
		if count_counts < 5:
			file_LessThan5Counts.write(genus+'\n')


from django.urls import path

from . import views

urlpatterns = [

    path('', views.hotelhome),
    path('book/', views.book, name='book'),
    path('payment/', views.payment, name='payment'),
    path('confirm/', views.confirm, name='confirm'),
    path('validate/', views.validate, name='validate'),
    path('rooms/', views.room, name='rooms'),
    path('room/<slug:type1>/', views.roomdetail, name='roomdetail'),
    path('checkin/', views.checkin, name='checkin'),
    path('addcheckin/', views.addcheckin, name='addcheckin'),
    path('checkout/', views.checkout, name='checkout'),

]


__author__ = 'Matteo'

import os.path, os,glob, time, csv, re
#print "last modified: %s" % time.ctime(os.path.getmtime(file))
#print "created: %s" % time.ctime(os.path.getctime(file))

class photo:
    def __init__(self,file):
        self.ctime=os.path.getctime(file)
        self.mtime=os.path.getmtime(file)
        self.time=self.mtime
        self.long_name=file
        if file.find('bd_bokeh') >0:
            self.bk='prom'
        elif file.find('Night View 3') >0:
            self.bk='NYC'
        elif file.find('plane - Copy') >0:
            self.bk='C17'
        elif file.find('crest') >0:
            self.bk='crest'
        else:
            self.bk='UNKNOW'
        try:
            self.name=re.match('DSC\d+',file).group(0)
        except AttributeError:
            self.name='UNKNOWN'

    def __str__(self):
        return self.name+' made on '+str(time.ctime(self.time))+' with bk '+self.bk

class album:
    def __init__(self,path):
        self.data=[]
        os.chdir(path)
        for file in sorted(glob.glob("*.jpg")):
            f=photo(file)
            #print(f)
            self.append(f)

    def __add__(self, other):
        self.data.append(other)

    def append(self,other):
        self.data.append(other)

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

    def csv(self,rowhead):
        return [[rowhead, x.name,x.time,x.bk] for x in self.data]

soldball=album("D:\\High Res")
print(len(soldball))
unsoldball=album("D:\\choose_raw")
print(len(unsoldball))

with open('D:\\data.csv', 'w') as csvfile:
    data = csv.writer(csvfile)
    data.writerows(unsoldball.csv('unsold'))
    data.writerows(soldball.csv('sold'))


import os
from flask import Flask, request, redirect, url_for, render_template, send_from_directory
import pandas as pd
from werkzeug.utils import secure_filename
from PyPDF2 import PdfFileReader, PdfFileWriter

UPLOAD_FOLDER = os.path.dirname(os.path.abspath(__file__)) + '/uploads/'
DOWNLOAD_FOLDER = os.path.dirname(os.path.abspath(__file__)) + '/downloads/'
ALLOWED_EXTENSIONS = {'pdf', 'txt','jpeg','csv'}

app = Flask(__name__, static_url_path="/static")
DIR_PATH = os.path.dirname(os.path.realpath(__file__))
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['DOWNLOAD_FOLDER'] = DOWNLOAD_FOLDER
# limit upload size upto 8mb
app.config['MAX_CONTENT_LENGTH'] = 8 * 1024 * 1024

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route("/index1")
def home():
    return render_template("index1.html")

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        print(request.files['file'])
        print(allowed_file(request.files['file'].filename))
        if 'file' not in request.files:
            print('No file attached in request')
            return redirect(request.url)
        file = request.files['file']
        if file.filename == '':
            print('No file selected')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            extension_type = request.files['file'].filename.split('.')[1]
            if extension_type == 'pdf':
              process_file(os.path.join(app.config['UPLOAD_FOLDER'], filename), filename)
              print('Uploaded folder and filename is {0},{1}'.format(app.config['UPLOAD_FOLDER'], filename))
              return redirect(url_for('uploaded_file', filename=filename))
            elif extension_type == 'csv':
              input_file = pd.read_csv(filepath_or_buffer = os.path.join(app.config['UPLOAD_FOLDER'], filename))

              table = filename.split('.')[0]  # Get name of the table

              # Get all "keys" inside "values" key of dictionary (column names)
              columns = ', '.join(list(input_file.columns))

              # Get all "values" inside "values" key of dictionary (insert values)
              values = ', '.join(dictionary["values"].values())

              # Generate INSERT query
              print(f"INSERT INTO {table} ({columns}) VALUES ({values})" + "\n")

              # Generate QUERY for each dictionary inside data list
              for query in data:
                generate_insert_query(query)
              # input_file.to_csv(app.config['DOWNLOAD_FOLDER'] + filename)
              # output_stream = open(app.config['DOWNLOAD_FOLDER'] + filename, 'wb')
              # output.write(output_stream)
              return render_template("tables.html", column_names=input_file.columns.values, row_data=list(input_file.head().values.tolist()),
                            zip=zip)
    return render_template('index1.html')

@app.route("/analyse")
def analyse():
  return "in analyse"
def process_file(path, filename):
    remove_watermark(path, filename)
    # with open(path, 'a') as f:
    #    f.write("\nAdded processed content")

def remove_watermark(path, filename):
    input_file = PdfFileReader(open(path, 'rb'))
    output = PdfFileWriter()
    for page_number in range(input_file.getNumPages()):
        page = input_file.getPage(page_number)
        page.mediaBox.lowerLeft = (page.mediaBox.getLowerLeft_x(), 20)
        output.addPage(page)
    output_stream = open(app.config['DOWNLOAD_FOLDER'] + filename, 'wb')
    output.write(output_stream)

@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['DOWNLOAD_FOLDER'], filename, as_attachment=True)

if __name__ == '__main__':
    port = int(os.environ.get("PORT", 5000))
    app.run()


from django.urls import path, include

from .views import ArticleList, ArticleDetail

app_name = "api/v1"

urlpatterns = [
    path("article/", ArticleList.as_view(), name="listcreate"),
    path("article/<int:pk>/", ArticleDetail.as_view(), name="detailcreate"),
]


#! /usr/bin/env python
# !-*- codding:utf-8 -*-
import sys
import cv2
import numpy as np

path1, path2 = sys.argv[-2:]

img1 = cv2.imread(path1)
h1, w1 = img1.shape[:2]
img2 = cv2.imread(path2)
h2, w2 = img2.shape[:2]

newImg = np.zeros((h1+h2, w1, 3), dtype = np.uint8)
newImg[0:h1, :] = img1
newImg[h1:, :] = img2

cv2.imwrite('merge.jpg', newImg)

# cv2.namedWindow('image', 0)
# cv2.imshow('image', newImg)
# cv2.waitKey(0) & 0xFF
# cv2.destroyAllWindows()


# rasa 20180212
# 使用ES python api插入geojson面数据（map.geojson）

import json
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
import hashlib
import requests
import sys
import time
import re
import jieba
from wordcloud import WordCloud

def demo(es, key_words):
    count=0
    allcnt=0
    for key_word in key_words:
        try:
            str = key_word["publisher"]+key_word["developer"]+key_word["tags"]+key_word["info"]
            pins = []
            mytext=" ".join(jieba.cut(str))
            wordcloud = WordCloud(font_path='msyh.ttc').generate(mytext)
            a = wordcloud.layout_
            for i in a:
                pin = {'name':"",'value':0}
                pin['name']=i[0][0]
                pin['value'] = i[1]
                pins.append(pin)
            print(pins)
            item = {'game_name':key_word["game_name"],'cipin':pins}
            es.updateDocument(item)
            print("插入成功")
            # time.sleep(2)
        except Exception as e:
            print(e)
            # print(key_word)
            continue

class ArticlePipelines():
    # 初始化
    def __init__(self):
        # elasticsearch的index
        self.index = "trygame"
        # elasticsearch的type
        self.type = "_doc"
        # elasticsearch的ip加端口
        self.es = Elasticsearch(hosts="39.98.125.235:9200")

    # 更新文档
    def updateDocument(self, item):
        result = self.checkDocumentExists(item)
        if result == False:
            return
        # origin_tag = self.es.get(self.index, self.myhash(item["game_name"]))['_source']['tags']
        parm = {
            "doc": {
                "cipin": item['cipin']
            }
        }
        try:
            self.es.update(index=self.index, id=self.myhash(item['game_name']), body=parm)
        except:
            pass

    # 检查文档是否存在
    def checkDocumentExists(self, item):
        try:
            print(self.myhash(item["game_name"]))
            self.es.get(self.index, self.myhash(item["game_name"]))
            return True
        except:
            return False

    def myhash(self, name):
        md5 = hashlib.md5()
        name_pre = name
        md5.update(name_pre.encode('utf-8'))
        return md5.hexdigest()

if __name__ == '__main__':
    # es = Elasticsearch(hosts=["127.0.0.1:9200"], http_auth=('elastic','changeme'),timeout=5000)
    # es = ResourcePipelines()
    es = ArticlePipelines()
    key_words = []

    with open("./data/3dmnew.json", encoding="UTF-8") as f:
        for line in f.readlines():
            resource = json.loads(line)
            key_words.append(resource)

    print(key_words)
    demo(es,key_words)
    # print(type(data))
    # createDocument(item)

    # pass


import math
class Solution:
    def expect(self , n ):
        rst = 0

        def c(n, k):
            # return tmp(n, k) / math.factorial(k)
            return math.factorial(n) / (math.factorial(n - k) * math.factorial(k))

        def tmp(n, k):
            rst = 1
            tmp = n
            while tmp >= n - k:
                rst *= tmp
                tmp -= 1
            return rst

        for k in range(n):
            a = c(n + k, k) * (0.5 ** (n + k)) * (n - k)
            rst += a

        return rst

if __name__ == '__main__':
    print(Solution().expect(2))


#Problem Link: https://codeforces.com/problemset/problem/1703/E

t=int(input())
while t>0:
    n=int(input())
    arr=[[x for x in input()] for i in range(n)]
    count=0
    m=n-1
    ans=[]
    for i in range((n+1)//2):
        for j in range(n//2):
            nowi=i
            nowj=j
            oldnowj=nowj
            sum_ = int(arr[nowi][nowj])
            nowj=n-nowi-1
            nowi=oldnowj
            sum_+=int(arr[nowi][nowj])
            oldnowj=nowj
            nowj=n-nowi-1
            nowi=oldnowj
            sum_+=int(arr[nowi][nowj])
            oldnowj=nowj
            nowj=n-nowi-1
            nowi=oldnowj
            sum_+=int(arr[nowi][nowj])
            count+=min(sum_, 4-sum_)
    print(count)

    t=t-1


from WebScrapping.search import searchWord
import csv
import pandas as pd
import pickle
import time

with open('save1.p', 'rb') as f:
    data_1 = pickle.load(f)

with open('save2.p', 'rb') as f:
    data_2 = pickle.load(f)

xl = pd.read_excel('sentence.xlsx', header=None)

p_alternative = []
for row in xl.iterrows():
    number = row[1][2]
    p_alternative.append(number)
    print()

try:
    with open('memo-sentence.p', 'rb') as f:
        memo = pickle.load(f)
except FileNotFoundError:
    memo = {}

result = []

try:
    for i in range(1001):
        print('sentence number', i+1)
        alternative1 = data_1[i]
        alternative2 = data_2[i]
        # print(alternative1, alternative2)

        if i not in memo:
            time.sleep(5)
            sentences1 = searchWord(alternative1)
            time.sleep(60)
            sentences2 = searchWord(alternative2)
            time.sleep(30)
            memo[i] = {
                'sentences1': sentences1,
                'sentences2': sentences2
            }

        else:
            sentences1 = memo[i]['sentences1']
            sentences2 = memo[i]['sentences2']

        if(len(sentences1) > len(sentences2)):
            a = 1
            print('Plausible alternative is 1')
        elif len(sentences1) < len(sentences2):
            a = 2
            print('Plausible alternative is 2')
        else:
            a = 3
            print('Equal')
        result.append(a)
        memo[i]['result'] = a
    pickle.dump ( a, open("save3.p", "wb"))
    pickle.dump(memo, open('memo-sentence.p', "wb"))
    pickle.dump(p_alternative, open("correct_alt.p", "wb"))
except Exception:
    print(Exception)
    pickle.dump(a, open("save3.p", "wb"))
    pickle.dump(memo, open('memo-sentence.p', "wb"))
    pickle.dump(p_alternative, open("correct_alt.p", "wb"))
except KeyboardInterrupt:
    pickle.dump(a, open("save3.p", "wb"))
    pickle.dump(memo, open('memo-sentence.p', "wb"))
    pickle.dump(p_alternative, open("correct_alt.p", "wb"))
# for i in sentences:
#     print(i, end='\n\n')


import collections
def is_palindrome(s1):
    low, high = 0, len(s1)-1
    while low < high:
        if s1[low] != s1[high]:
            return False
        low+=1
        high-=1
    return True

def make_palindrome(str):
    low, high = 0, len(str)-1
    while low<high:
        if str[low]==str[high]:
            low+=1
            high-=1
        else:
            if(is_palindrome(str[low+1:high+1])):
                return low
            elif(is_palindrome(str[low:high])):
                return high
            return -1
    return -2

def main():
    # value = "abcbea"
    # value = "abcba"
    value = "abcd"
    idx = make_palindrome(value)
    if idx == -1:
        print("Not possible")
    elif idx == -2:
        print("already palindorome")
    else:
        print("remove char at "+str(idx))

def can_make_palindrome(s):
    return sum(v %2 for v in collections.Counter(s).values()) <= 1

main()

print(collections.Counter("Neha").values())
print(can_make_palindrome("nneehhaaa "))


"""Collect data from source API."""
import json
import re
import time
from pathlib import Path
from typing import Union

import pandas as pd
import requests

def get_query(
    query: str = "", country: str = "", page: int = 1
) -> Union[pd.DataFrame, None]:
    """
    This function takes in the query(species), country with cnt: at the start,
    page as page number. This will output a dataframe from the reqeust of the
    xeno canto api
    ---
    Args: query:str,country:str, page:str
    ---
    Returns: Dataframe consisting of the data retrieved from the api
    """
    try:
        response = requests.get(
            f"https://xeno-canto.org/api/2/recordings?query={query}{country}&page={page}",
            timeout=5,
        )
        if response.status_code != 200:
            print(f"Request failed with status code {response.status_code}")
            return None
        data = response.json()
        if "recordings" not in data:
            print("No recordings found")
            return None
        recordings = data["recordings"]
        df = pd.json_normalize(recordings)
        time.sleep(1)

        json_obj = json.loads(response.text)
        num_recordings = json_obj["numRecordings"]
        num_species = json_obj["numSpecies"]
        page = json_obj["page"]
        num_pages = json_obj["numPages"]

        print("Number of recordings: ", num_recordings),
        print("Number of species: ", num_species)
        print("Page: ", page)
        print("Number of pages: ", num_pages)
        return df
    except Exception as e:
        print(f"An error occurred: {e}")
        return None

def get_queries(queries: list, country: str = "", page: int = 1) -> pd.DataFrame:
    concat_df = None
    for query in queries:
        query_df = get_query(query, country, page)
        if query_df is not None:
            if concat_df is None:
                concat_df = query_df
            else:
                concat_df = pd.concat([concat_df, query_df])
    return None

def download_files(df):
    """
    Downloads audio files from URLs specified in a Pandas DataFrame,
    saves them to a local 'data' folder, and appends the file path as
    a new column to the DataFrame.
    ---
    Args:
        df: Pandas DataFrame with 'file' and 'file-name' columns specifying
            the URLs and file names of the audio files to be downloaded.
    ---
    Returns:
        df: Pandas DataFrame with an additional 'file-path' column that
            contains the local file paths of the downloaded audio files.
    """
    for _, row in df.iterrows():
        url = row["file"]
        file_name = row["file-name"]
        response = requests.get(url)
        with open(row["file-path"], "wb") as f:
            f.write(response.content)
        print(f"File '{file_name}' downloaded to 'data' folder.")

    return df

def clean_file_name(file_name: str) -> str:
    """
    Replace any characters that are not alphanumeric, underscore,
    or period with an underscore
    ---
    Args: file name
    ---
    Returns : cleaned file name
    """
    return re.sub(r"[^\w\.]", "_", file_name)

def run_standalone() -> None:
    print("Getting query for Pycnonotus+goiavier")
    df_temp2 = get_query(query="Pycnonotus+goiavier")
    print("Getting query for Eudynamys scolopaceus")
    df_temp3 = get_query(query="Eudynamys scolopaceus")

    page = [1, 2, 3]
    df_int = pd.DataFrame()
    print("Getting query for Tringa+totanus")
    for i in page:
        df_temp = get_query(query="Tringa+totanus", page=i)
        df_int = pd.concat([df_int, df_temp])
    print("Concatenating the queries")
    df_int = pd.concat([df_int, df_temp2, df_temp3])
    print("Dropping missing files")
    df_int.drop(df_int.loc[df_int["file"] == ""].index, inplace=True)
    print("Cleaning file name.")
    df_int["file-name"] = df_int["file-name"].apply(clean_file_name)
    df_int["file-path"] = df_int["file-name"].apply(
        lambda x: "/".join(["data", "audio", x])
    )

    data_path = Path("./data/audio/")
    data_path.mkdir(parents=True, exist_ok=True)
    print("Downloading Audio Files")
    download_files(df_int)
    print("Audio files download complete")
    file_path = Path("./data/csv/")
    file_path.mkdir(parents=True, exist_ok=True)
    csv_path = file_path.joinpath("my_data.csv")
    print("Saving meta data")
    df_int.to_csv(csv_path, index=False)
    print("Meta data saved.")
    print("Data collection process completed")

    return None

    # Sample code
    # -----------
    # python -m src.data_module.data_collector

if __name__ == "__main__":
    run_standalone()


# ~*~ coding: utf-8 ~*~
import socket
import hashlib
import simplejson as json

class sendInfo:
	def __init__(self, ip, port):
		self.address = (ip, int(port))

	def send(self, id, version, info):
		s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		try:
			s.connect(self.address)
		except: return 1
		st = json.dumps(info)
		h = hashlib.md5()
		h.update(st)
#		print id, type(id)
		if(id == 'unknown'):
			id = socket.gethostbyaddr(socket.gethostname())[0]
		mes = 'Hello.\nID: ' + str(id.encode('utf-8')) + '\nVersion: ' + str(version) + '\nHash: ' + h.hexdigest() + '\n\n' + st
		self.sendParts(s, mes)
		try:
			dat1 = s.recv(1024)
		except:
			return 1
		if(dat1[0:6] != 'Hello.'):
			s.close()
			return 1
		if(dat1[7:15] == 'Update: '):
			link = dat1[14:]
			s.close()
			if(link != ''):
				self.deblink = link
				return 2
			else: return 1
		elif(dat1[7:] == 'Get info again.'):
			self.sendParts(s, mes)
			dat2 = s.recv(1024)
			if(dat2 == 'Ok'):
				s.close()
				return 0
			else:
				s.close()
				return 1
		elif(dat1[7:] == 'Ok'):
			s.close()
			return 0
		else:
			s.close()
			return 1

	def sendParts(self, s, st):
		lrang = len(range(0, len(st), 1024))-1
		for i in range(0, len(st), 1024):
			if(i < lrang):
				s.send(st[i:(1024+i)])
			else:
				s.send(st[i:])
				if(len(st[i:]) == 1024):
					s.send('END')

	def getDeblink(self):
		return self.deblink

	def ready(self):
		s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		try:
			s.connect(self.address)
		except: return 1
		s.send("Are you ready?")
		dat = s.recv(1024)
		if(dat == 'Yes.'): return 0
		else: return 1


import random
from random import randint
from PyQt5 import uic
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from FunctionsHolder import *
from PyQt5.QtMultimedia import *
import sys
import pygame
soundEffect = '487195__nicknamelarry__typewriter-cartoon.wav'

################################################################################
################################################################################
#openning and linking the pyQt GUI to pycharm and then openning up the demographic csv file I created so that I can write in the particpant info

app = QApplication([])
window = uic.loadUi("GamblingTaskGUI.ui")
fileDemo = open('DemographicInfo.csv','a') #append because I created the titles on a different module

def nextPage(): #creating one function to call on whenever I want to turn to the next page put before everything because this is the first function called
    window.stackedWidget.setCurrentIndex(window.stackedWidget.currentIndex() + 1)

###################################################################################
###################################################################################
#below is a list of all the variables that will need to be created outside of the functions below

CSVFile = open('ParticipantInfo.csv','a')
window.student = ''
window.trialNumber = 0
window.doorNumber = []
window.moneyWon = 0
window.feedBack = ''
window.response = []
window.sentenceBuild = ''
window.n = 0
animatedSentence = "You have won: " #called animated sentence because it will be animated to have a typewriter effect

###################################################################################
###################################################################################

window.btnConsent.clicked.connect(nextPage)  #Only turning the page if the participant consents

###################################################################################
###################################################################################
#Part of the code used to sort out proporitons of win/loss results shown
#The function is called from the FunctionsHolder Page and it will randomize the results so that an almost equal proportion is shown for number of trials
#but in a random order so that participant does not know that the feedback is predetermined

window.preDeterminedResults = pseudorandomAssignment(4, 0.5) #0.5 because for when there is a large number of trials we want half to show positve
#and half to show negative reedback

###################################################################################
###################################################################################
#Create a function for list of items that need to be hidden at the beginning of the experiment so that
#I can use this function to hide the labels for every trial

def HiddenLabels():
    window.lblQuestion.hide()
    window.lblInstructions.hide()
    window.btnNo.hide()
    window.btnYes.hide()
    window.lblLose.hide()
    window.lblWin.hide()
    trials()

###################################################################################
###################################################################################
## These timers will be used to show stimuli for a certain period of time and to shift the page automatically
## after a certain amount of time has passed
#reason for large number of timers is because there are different functions called at the same time with different starting points that
#are required to make the program run in a certain order

NumberTimer = QTimer()
QuestionLabelTimer = QTimer()
HidePlusSignTimer = QTimer()
PlusIntervalTimer = QTimer()
HideFirstSignTimer = QTimer()
RewardTimer = QTimer()
PageTurnTimer = QTimer()

###################################################################################
###################################################################################
#This function will be used for the first fixation mark before the first trial. After 1 second the page will turn and the participant will no longer
#see the fixation mark. That is why both next page is timed to the timer as well as trials which will kick in other functions that are needed
#for subsequent parts of the experiment

def HideFirstSign():
    HideFirstSignTimer.timeout.connect(nextPage)
    HideFirstSignTimer.timeout.connect(trials)
    HideFirstSignTimer.start(1000)
    HideFirstSignTimer.setSingleShot(True)

###################################################################################
###################################################################################
#The following functions follow a set of logic similar to the HideFirstSign however slightly more complicated.
#I am using timers to turn the next page after a certain interval of time because I only
#want some things to be shown on the screen for a short amount of time. Therefore, within trials I have a pseudoRandom
#number being chosen and that becomes the trial condition. I set that trial condition to the door number and I change the
#text within the GUI to whatever the new trial number that was chosen.
#Then I set two timers within trials() that will call 2 separate functions at different times creating an interval of time between the pages
#that I want to turn i.e I connect HideNumber after 1 second and then showQuestionLabel at 2500 to create a time interval of the pages to be 1500
#using this way there will also be a small gap of blank space where the number is hidden as well so there is nothing on the screen
#The functions are put in order of when they are called below so that the system can be followed easier

def trials():
    if window.trialNumber != 0:
        window.stackedWidget.setCurrentIndex(4) #the first trial of the experiment calls upon trial a little differently so in order to move on
        #to other trials after the first one (0) we need to set the current index to 4 everytime trials() is called and it is not the first trial

    window.lblNumber.show()
    trialCondition = randint(1, 3) #we want the numbers 1,2 or 3, to show up on the screen randomly picked
    window.doorNumber.append(trialCondition)
    window.lblNumber.setText(str(window.doorNumber[-1]))
    window.doorNumberWrite = trialCondition

    #so that we don't have to have multiple labels within the GUI this way we can just set
    #one label and continue to set the text

    NumberTimer.timeout.connect(HideNumber)
    NumberTimer.start(1000)
    NumberTimer.setSingleShot(True)

    QuestionLabelTimer.timeout.connect(showQuestionLabel)
    QuestionLabelTimer.start(2500)  # use 2500 because we want the label to show 1500ms after the number is hidden
    QuestionLabelTimer.setSingleShot(True)

def HideNumber(): # just used to hide the number on the screen while the other labels are being shown
    window.lblNumber.hide()

def showQuestionLabel(): #this will show the labels that I want to ask the participant whether or not they believe that they will win this trial
    window.lblQuestion.show()
    window.lblInstructions.show()
    window.btnNo.show()
    window.btnYes.show()

def HidePlusSign(): #same pupose as the HideNumber(): function
    window.lblPlus.hide()
    HiddenLabels()

def yes():
    window.response.append('yes')
def no():
    window.response.append('no')

window.btnNo.clicked.connect(nextPage)
window.btnYes.clicked.connect(nextPage)
window.btnNo.clicked.connect(no)
window.btnYes.clicked.connect(yes)

button(window.pg1,HideFirstSign,HiddenLabels,nextPage,trials) #button called from FunctionsHolder called button because it makes any key
#on the keyboard into a button to turn the next page and call on the functions within the ().

def plusInterval(): #called plus interval because this function is in charge of creating the interval of time where the plus sign is shown
    #between each trial
    window.stackedWidget.setCurrentIndex(7) #page that the plus sign is on
    window.lblPlus.show()
    HidePlusSignTimer.timeout.connect(HidePlusSign)
    HidePlusSignTimer.start(1000) #this is 1000 because we want the screen to change after exactly 1 second of looking at fixation mark before each trial
    HidePlusSignTimer.setSingleShot(True) #all of the setSingleShots is so that the functions are not called repeatedly since some of them turn the page

def counter(): #called counter because it is the portion of the code that will count how many trials there are
    nextPage()
    window.trialNumber = window.trialNumber + 1 # adding to keep track of which trial number participant is on
    results(window)
    if window.trialNumber >1 and window.trialNumber <=3:
        #######writing CSV File in this function so that it writes before each trial

        CSVFile.write(str(window.trialNumber-1) + ',' + str(window.response[0]) + ',' + str(window.doorNumber[0])
                      + ',' + str(window.feedBack) + ',' + str(window.response[1]) + '\n')
        del window.response[0]
        del window.doorNumber[:3]

    if window.trialNumber <= 2: #you would change this number depending on the number of trials you wanted to do
        #to make the experiment go by quickly I'm only running 3 total trials 0 would be the first one
        PlusIntervalTimer.timeout.connect(plusInterval)
        PlusIntervalTimer.start(1000)
        PlusIntervalTimer.setSingleShot(True)
    else:
        NumberTimer.timeout.connect(FinalTrial)
        NumberTimer.start(1000)
        NumberTimer.setSingleShot(True)

def FinalTrial():
    window.stackedWidget.setCurrentIndex(8)
    playTyperWriterSound() #calling sound function to playtypewriter sound at the same time as the animation
    RewardTimer.timeout.connect(typewriterAnimation)
    RewardTimer.start(350) #This was time with the sound
    PageTurnTimer.timeout.connect(nextPage)
    PageTurnTimer.start(6000) #giving the typewriter time to write out the results
    PageTurnTimer.setSingleShot(True)

###################################################################################
###################################################################################
#Just a typewriter effect that will write out how much money the participant has won

def typewriterAnimation():
    if window.n < len(animatedSentence):
        window.sentenceBuild = window.sentenceBuild + animatedSentence[window.n]
        window.n+=1
        window.lblReward.setText(window.sentenceBuild)
    else:
        window.lblReward.setText(window.sentenceBuild + str(window.moneyWon))
    NumberTimer.stop() #we have to stop the timer here in the animation so that the typewriter sound effect is not replayed indefintely

###################################################################################
###################################################################################
##########using door function to create clickable doors for the experiment / function called from FunctionHolder

Door('Door1', 20, window.pg3, counter)
Door('Door2', 150, window.pg3, counter)
Door('Door3', 280, window.pg3, counter)
Door('Door4', 410, window.pg3, counter)

lblColor(window.pg8,"You're Done!",180,40,190,61) #calling my blinking label from Functions Holder to create a blinking label at the end

#Demographic Information Collection Portion of Code Below
################################################################################
################################################################################
#linking the demographics page to information that will be put into the CSV file
#calling on other demographic check functions so that the page doesn't move unless the participant answers all the question
# writing out information into CSV file for demographics
window.lblError.hide() #have to hide the label when the experiment begins and only show it if people have not filled out all portions
                        #of the demographic page

def CheckingAllDemographics():
    window.gender = ''
    window.age = window.ageBox.value()
    window.age = window.ageBox.value()
    window.education = window.educationBox.currentText()
    window.study = window.majorBox.text()
    window.name = window.nameBox.text()
    if window.btnFemale.isChecked():
        window.gender = 'Female'
    elif window.btnMale.isChecked():
        window.gender = 'Male'
    elif window.btnOther.isChecked():
        window.gender = 'other' + window.otherBox.text()
    #functions used to check the demographics is imported from functionsHolder
    if nameCheck(window) == True and ageCheck(window) and GenderCheck(window) == True and educationCheck(window) == True and studyCheck(window) == True:
        nextPage() #all of the demographic checking and information is imported from FunctionsHolder
        writeDemographicInfo(fileDemo,window)
window.btnDemo.clicked.connect(CheckingAllDemographics)

################################################################################
################################################################################
#End of Experiment

def windowClose():
    window.close() #function to call when the last button is clicked

window.btnFinish.clicked.connect(windowClose)

window.show()
app.exec()


import os
import discord
from dotenv import load_dotenv
import message_process as mc
from multiprocessing import Process

def getIntents():
    intents = discord.Intents()
    intents.messages = True
    intents.message_content = True
    intents.guilds = True
    intents.typing = True
    intents.reactions = True
    intents.auto_moderation = True
    return intents

def getTOKEN():
    if os.path.isfile(".env"):
        load_dotenv()
        token = os.getenv('DISCORD_TOKEN')
    else:
        print('Cannot find environment file\n')
        token = input('Please enter your bot token!\n')
        saveToken = input('Do you want to save this token?\n [Y/N] \n')
        if saveToken.upper() == 'Y':
            f = open(".env", "w")
            f.write(f'DISCORD_TOKEN={token}')
            f.close()
    return token

client = discord.Client(intents=getIntents())

@client.event
async def on_ready():
    for guild in client.guilds:
        print(f'{client.user.name} has connected to {guild.name}!')

@client.event
async def on_message(message):
    if not message.author.bot:
        reply = mc.Msg(message)
        if reply.type:
            await message.edit(suppress=True)
            reply.getVideo()
            if reply.replyVideo:
                rMsg = await message.channel.send(content=reply.replyVideo, reference=message)
                await rMsg.edit(suppress=False)

try:
    client.run(getTOKEN())
except:
    print('Error: The connection to the server cannot be established, please check your Token!')


from pynput.keyboard import Key, Controller
import time

keyboard = Controller()

# Press and release space
keyboard.press(Key.cmd)
keyboard.release(Key.cmd)
time.sleep(0.01)
keyboard.type("cmd")
time.sleep(0.05)
keyboard.press(Key.enter)
time.sleep(0.05)
keyboard.release(Key.enter)
###

time.sleep(0.05)
keyboard.type("color")


# 基础类型
Base_Type = ["int", "char", "float", "double"]
# 特殊符号
Special_Symbol = ["(", ")", "[", "]", "*", "const", "volatile", ","]
Compara_Array = Base_Type + Special_Symbol

Input_String = ""
Input_Array = []
Array_Point = 0
# 数据结构堆栈
Stack_Array = []
Stack_Point = 0
# 专门用于处理参数的堆栈
Function_Parameter_Stack_Array = []

# 读取变量的声明，并将其分解为后续程序可执行的列表结构
def declaration_analysis(string):
    global Input_String
    global Input_Array
    compara_succeess_flag = 0
    Input_String = str(string)

    # 去除内部空格" "
    Input_String = Input_String.strip(" ")

    if Input_String == "":
        return

    # 分割声明中的各种符号
    for i in Compara_Array:
        # 如果匹配成功，则删除匹配中的字符串并且加入数组中
        if Input_String.startswith(i):
            # 加入数组
            Input_Array.append(i)
            # 删除匹配字符串
            Input_String = Input_String.lstrip(i)
            # 表示匹配成功
            compara_succeess_flag = 1
            break

    # 匹配不成功,表明为未知变量
    if compara_succeess_flag == 0:
        true_item = len(Input_String)
        for i in Compara_Array:
            item = Input_String.find(i)
            if item >= 0:
                true_item = min(true_item, item)

        Input_Array.append(Input_String[:true_item])
        Input_String = Input_String.lstrip(Input_String[:true_item])

    # 递归调用
    declaration_analysis(Input_String)

    return

def find_the_right_parenthesis(item):
    global Input_Array
    paranthesis_number = 1
    point = item + 1
    while True:
        if Input_Array[point] == "(":
            paranthesis_number += 1
        elif Input_Array[point] == ")":
            paranthesis_number -= 1
            if paranthesis_number == 0:
                break

        point += 1

    return point

def value_right_dealing():
    # TODO 需要改为临时变量，并且这些数据有参数传递尽量，方便进行迭代
    global Array_Point
    global Stack_Array
    global Stack_Point
    # 将输入列表导入数据到stack并且停止在第一个未知数据
    for Array_Point in range(len(Input_Array)):
        Stack_Array.append(Input_Array[Array_Point])

        if Input_Array[Array_Point] in Compara_Array:
            pass
        else:
            print("Find the unknown value:", Input_Array[Array_Point])
            Stack_Point = Array_Point
            break

    # while True:
    if Input_Array[Array_Point + 1] == "(":
        print("The next value is: '('")
        right_parenthesis = find_the_right_parenthesis(Array_Point + 1)
        print("The left and right parenthesis position:%d, %d" % (Array_Point + 1, right_parenthesis))
        temp = Input_Array[(Array_Point + 2):right_parenthesis]
        print(temp)
    elif Input_Array[Array_Point + 1] == "[":
        if Input_Array[Array_Point + 2] != "]":
            print("该变量是一个长度为:", Input_Array[Array_Point + 2], "的数组")
            Array_Point += 3
        else:
            print("该变量是一个长度未知的数组")
            Array_Point += 2
    else:
        pass

if __name__ == "__main__":
    # declaration_analysis("const char *(* c[10])(int **p, char * (*fp)(void b))")
    declaration_analysis("const char * (func(char* time, void *p))")
    print(Input_Array)
    value_right_dealing()
    while True:
        pass


# -*- coding: utf-8 -*-
from copy import copy

from swift.common.exceptions import DiskFileDeviceUnavailable
from swift.common.swob import Request, HTTPInsufficientStorage, HTTPCreated
from swift.common.utils import get_logger
from swift.common.request_helpers import split_and_validate_path, is_user_meta
from swift.obj.diskfile import DiskFileManager

from swiftlifecyclemanagement.common.lifecycle import GLACIER_FLAG_META

class TruncateMiddleware(object):
    def __init__(self, app, conf, *args, **kwargs):
        self.app = app
        self.conf = conf
        self.logger = get_logger(self.conf, log_route='truncate')
        self._diskfile_mgr = DiskFileManager(conf, self.logger)

    def truncate(self, env):
        req = Request(env)
        try:
            disk_file = self.get_diskfile(self.device, self.partition,
                                          self.account, self.container,
                                          self.obj)
        except DiskFileDeviceUnavailable:
            return HTTPInsufficientStorage(drive=self.device,
                                           request=Request(copy(env)))

        # object flow 상, 임시 데이터를 삭제 후 DiskFileWrite 의 put을 하게 되면,
        # _finalize_put을 호출하게 된다. 이 때, metadata에 설정된 X-Timestamp 값으로
        # object 파일명을 생성하고, 임시 파일로 대체한다.
        # 따라서 별 다른 truncate를 할 필요가 없다.

        ori_meta = disk_file.read_metadata()
        metadata = {
            'X-Timestamp': ori_meta['X-Timestamp'],
            'Content-Type': ori_meta['Content-Type'],
            'ETag': 'd41d8cd98f00b204e9800998ecf8427e',
            'Content-Length': 0,
            'X-Object-Meta-Glacier': True,
            'X-Object-Meta-S3-Content-Length': ori_meta['Content-Length'],
            'X-Object-Meta-S3-ETag': ori_meta['ETag']
        }
        # 원본 Object Metatdata 도 저장
        metadata.update(val for val in ori_meta.iteritems()
                        if is_user_meta('object', val[0]))

        # Object Restore 정보가 있으면 해당 정보 지움.
        # 이 경우는 restored object expiration 임.
        if 'X-Object-Meta-S3-Restore' in metadata:
            del metadata['X-Object-Meta-S3-Restore']

        with disk_file.create(size=0) as writer:
            writer.put(metadata)

        return HTTPCreated(request=req, etag=ori_meta['ETag'])

    def get_diskfile(self, device, partition, account, container, obj,
                     **kwargs):
        return self._diskfile_mgr.get_diskfile(device, partition, account,
                                               container, obj, **kwargs)

    def __call__(self, env, start_response):
        req = Request(copy(env))
        method = req.method

        if (method == 'PUT' or method == 'POST') and \
           GLACIER_FLAG_META in req.headers:
            self.device, self.partition, self.account, self.container, \
                self.obj = split_and_validate_path(req, 5, 5, True)
            return self.truncate(env)(env, start_response)

        return self.app(env, start_response)

def filter_factory(global_conf, **local_conf):
    """Standard filter factory to use the middleware with paste.deploy"""
    conf = global_conf.copy()
    conf.update(local_conf)

    def truncate_filter(app):
        return TruncateMiddleware(app, conf)

    return truncate_filter


1
2
3

import numpy as np
import pandas as pd
import datetime as dt
from scipy import signal
from tqdm import tqdm

print('株価データ読み込み開始')
df_365d2 = pd.read_excel('365price2.xlsx', header=[0, 1], index_col=0) #Excelから株価情報を取得
df_365d = df_365d2["Adj Close"]
df_365dv = df_365d2["Volume"]
vcp_t2 = []
vcp_t3more = []
ticker_list = list(df_365d.columns.values)

df_180d = df_365d[-180:]

print('株価データ読み込み終了')

#近似値計算--------------------------------------------------------------------------------
print('近似値計算開始')

df_180d['date_no.'] = list(range(len(df_180d),0,-1))
df_app = df_180d.copy()

x = np.array(df_app['date_no.'])

for ticker in tqdm(ticker_list):
    y = np.array(df_app[ticker])
    coe = np.polyfit(x, y, 10) #10次関数に近似
    y3 = np.poly1d(coe)(x)
    df_app[ticker] = y3

for ticker in tqdm(ticker_list):
    maxid = signal.argrelmax(np.array(df_app.loc[:,ticker])) #極大値のindex取得
    minid = signal.argrelmin(np.array(df_app.loc[:,ticker])) #極小値のindex取得

    app_maxlist = df_app.index[maxid[0]] #極大値の日付取得
    app_minlist = df_app.index[minid[0]] #極小値の日付取得

    applist = pd.concat([df_app.loc[app_maxlist,ticker], df_app.loc[app_minlist,ticker]]).sort_index() #極大値と極小値を1つのリストにしてソート

    t_depth = []
    maxima_list = list(df_app.loc[app_maxlist,ticker])

    if len(applist) >= 4: #極値が4以上（2T以上）
        if applist[0] > applist[1]: #最初の極値が極大値の時
            if len(applist) % 2 == 1: #極値の数が奇数の時
                for i in range(0, len(applist)-1, 2):
                    t_depth.append(( applist[i + 1] - applist[i] ) * -100 / applist[i])
            else:
                for i in range(0, len(applist), 2):
                    t_depth.append(( applist[i + 1] - applist[i] ) * -100 / applist[i])

        else:
            if len(applist) % 2 == 0:
                for i in range(1, len(applist)-1, 2):
                    t_depth.append(( applist[i + 1] - applist[i] ) * -100 / applist[i])
            else:
                for i in range(1, len(applist), 2):
                    t_depth.append(( applist[i + 1] - applist[i] ) * -100 / applist[i])

        if (1 < len(t_depth) <= 2) and (t_depth[-1] < 5) and (maxima_list[-1] * 0.9 <= maxima_list[-2] <= maxima_list[-1] * 1.1):
            if t_depth[-1] < t_depth[-2]:
                vcp_t2.append(ticker)
        elif (len(t_depth) > 2) and (t_depth[-1] < 10) and (maxima_list[-1] * 0.9 <= maxima_list[-2] <= maxima_list[-1] * 1.1):
            if t_depth[-1] < t_depth[-2] <= t_depth[-3]:
                vcp_t3more.append(ticker)
            elif (t_depth[-1] < t_depth[-2]) and (t_depth[-2] > t_depth[-3]):
                vcp_t2.append(ticker)
print('近似値計算終了')

#出来高判定--------------------------------------------------------------------------------------
print('出来高判定開始')

df_365dv.loc['50ma',:] = list(df_365dv.rolling(50).mean().iloc[-1,:])
v_underave_list = df_365dv.columns[df_365dv.iloc[-1,:] > df_365dv.iloc[-2,:]].tolist() #直近出来高が50日平均未満

print('出来高判定終了')

print('VCP出力')
tt_vcpt2 = list(set(vcp_t2) & set(v_underave_list))
tt_vcpt3more = list(set(vcp_t3more) & set(v_underave_list))
tt_vcpt2.sort()
tt_vcpt3more.sort()
print(tt_vcpt2)
print(tt_vcpt3more)


"""

    http server 2.0

    python

    HTTPServer v2.0
    功能：
    1. 接收客户端(浏览器)请求
    2. 解析客户端发送的请求 index.html
    3. 根据请求组织数据内容 请求：请求头，请求行，空行，请求体
    4. 讲数据内容组成HTTP响应格式返回给浏览器
        响应：响应头，响应行，空行，响应体

    升级：
    1. 使用IO多路复用(select)，可以满足同时处理多个客户端发来的请求
    2. 做基本的请求解析，根据具体的请求返回具体的内容
    3. 通过类接口的形式进行功能(server)封装

    HTTPServer v2.0
    功能：
    1. 接收客户端(浏览器)请求
    2. 解析客户端发送的请求 index.html
    3. 根据请求组织数据内容 请求：请求头，请求行，空行，请求体
    4. 讲数据内容组成HTTP响应格式返回给浏览器
        响应：响应头，响应行，空行，响应体

    升级：
    1. 使用IO多路复用(select)，可以满足同时处理多个客户端发来的请求
    2. 做基本的请求解析，根据具体的请求返回具体的内容
    3. 通过类接口的形式进行功能(server)封装
"""
from socket import *
from select import *

# 建立套接字 -》 绑定地址 -》 监听 -》 accept -》 处理请求
class HTTPServer:
    def __init__(self, host, port, dir):
        self.host = host
        self.port = port
        self.dir = dir
        self.address = (host, port)
        # IO多路复用列表
        self.rlist = []
        self.wlist = []
        self.xlist = []
        # 创建套接字对象
        self.create_socket()
        # 绑定地址
        self.bind()

    # 绑定地址
    def bind(self):
        self.sockfd.bind(self.address)

    # 创建套接字
    def create_socket(self):
        self.sockfd = socket()
        # 可重复使用
        self.sockfd.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)

    # 启动服务
    def serve_forever(self):
        self.sockfd.listen(5)  # 监听
        print("Listen the port {}".format(self.port))
        # IO多路复用接收客户端的请求
        self.rlist.append(self.sockfd)
        while True:  # 使用select循环处理客户端发来的请求
            rs, ws, xs = select(self.rlist, self.wlist, self.xlist)
            for r in rs:
                if r is self.sockfd:  # r为服务端的套接字
                    connfd, addr = r.accept()
                    print("Connect from {}".format(addr))
                    self.rlist.append(connfd)
                else:  # r为客户端与服务端的连接套接字
                    # 处理客户端发来的请求
                    self.handle(r)

    # 服务端处理客户端发来的请求
    def handle(self, connfd):
        # 接收http请求
        data = connfd.recv(1024)
        if not data:  # 客户端断开连接的处理
            self.rlist.remove(connfd)  # 从select的rlist中删除连接
            connfd.close()
            return
        # 将字节串进行切割，提取请求内容
        request_line = data.splitlines()[0]  # b'GET /index.html HTTP/1.1'
        info = request_line.decode().split(" ")[1]  # /index.html
        print(connfd.getpeername(), ":", info)  # 打印客户端的地址信息
        # 21:03上课～
        # 根据具体的请求内容进行数据处理
        # /index.html: 表示客户端要访问页面资源
        if info == "/" or info[-5:] == ".html":
            self.get_html(connfd, info)  # 处理页面请求
        else:
            self.get_data(connfd, info)  # 处理其他数据请求

    # 处理客户端的获取网页请求
    def get_html(self, connfd, info):
        # 获取网站首页信息 index.html
        if info == "/":  # http://127.0.0.1:12306/，表示客户端要访问首页
            filename = self.dir + "/index.html"  # ./static/index.html
        # 获取其他页面信息
        else:  # /hahahah.html
            filename = self.dir + info  # 例如：./static/new.html
            print("filename: ", filename)
        try:
            f = open(filename, "r")
        except Exception as e:
            # 如果要访问的资源不存在，则返回404
            response = "HTTP/1.1 404 Not Found\r\n"  # 响应头
            response += "Content-Type: text/html\r\n"  # 响应行
            response += "\r\n"
            response += "
Page not found, sorry...
"
            connfd.send(response.encode())
        else:
            # 组织HTTP Response给浏览器  响应头，响应行，空行，响应体
            response = "HTTP/1.1 200 OK\r\n"  # 响应头
            response += "Content-Type: text/html\r\n"  # 响应行
            response += "\r\n"  # 空行
            response += f.read()  # 响应体
            connfd.send(response.encode())

    # 处理其他请求  /index.html   username password  登录按钮
    # 127.0.0.1:12306/register
    def get_data(self, connfd, info):
        response = "HTTP/1.1 200 OK\r\n"  # 响应头
        response += "Content-Type: text/html\r\n"  # 响应行
        response += "\r\n"  # 空行
        response += "
zan bu chu li ~ waiting for httpserverv3.0 ~
"  # 响应体
        connfd.send(response.encode())
# 休息到21：03，15分钟～下节课开始正则表达式～
if __name__ == '__main__':
    HOST = "127.0.0.1"
    PORT = 12306
    DIR = "./static"  # 存储网页地址
    server = HTTPServer(HOST, PORT, DIR)  # 实例化对象
    server.serve_forever()  # 启动服务


from numpy import *
from os import listdir

"""
Custom class for handwriting recognization, from which we can get values and labels.

"""
class Handwriting_dataset(object):

    def __init__(self, dir_name):
        file_list = listdir(dir_name)
        self.size = len(file_list)

        dataset, labels = [], []
        for i in range(self.size):
            # file name: 'label_count.txt'. For example, '0_2.txt' means the second sample of digit 0.
            file_name = file_list[i]

            img_vec = Handwriting_dataset.img2vector(self, dir_name + file_name)
            dataset.append(img_vec)
            label = (file_name.split('.')[0]).split('_')[0]
            labels.append(label)

        self.dataset = array(dataset)
        self.labels = labels

    # Every img file consists of 32 rows and 32 columns, with element 0 or 1.
    # Now, convert it into a 1*1024 vector.
    def img2vector(self, file_name):
        img_vector = zeros((1024))
        fr = open(file_name)
        lines = fr.readlines()
        # copy the element row by row.
        for row in range(32):
            line = lines[row]
            for column in range(32):
                img_vector[32 * row + column] = int(line[column])
        return array(img_vector)

if __name__ == '__main__':

    training_set = Handwriting_dataset('handwriting_digits/trainingDigits/')
    test_set = Handwriting_dataset('handwriting_digits/testDigits/')

    print('training set size: %d' % training_set.size)
    print('test set size: %d' % test_set.size)