import re
import numpy as np
import sys
import getopt
import json
import math

import random
import copy

import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.distributions import Categorical

def init(filename):
    f = open(filename, 'r')

    #Get hyperperiod
    hyperperiod=int(f.readline().split()[1])
    #print(hyperperiod)

    f.readline()
    f.readline()
    f.readline()
    f.readline()

    # Calculate the amount of tasks
    num_of_tasks = 0
    while f.readline().startswith('\tTASK'):
        num_of_tasks += 1
    #print('Number of tasks =',num_of_tasks)

    # Build a communication matrix
    data = [[-1 for i in range(num_of_tasks)] for i in range(num_of_tasks)]
    line = f.readline()
    while line.startswith('\tARC'):
        line = re.sub(r'\bt\d_', '', line)
        i, j, d = [int(s) for s in line.split() if s.isdigit()]
        data[i][j] = d
        line = f.readline()
    #for line in data:
    #    print(line)

    while not f.readline().startswith('# type'):
        pass

    # Build a computation matrix
    comp_cost = {}
    line = f.readline()
    while line.startswith('\t') or line.startswith('    '):
        comp_cost.update({int(line.split()[0]):int(line.split()[1])})
        line = f.readline()
    #for key in comp_cost.keys():
    #    print(key,comp_cost[key],type(key),type(comp_cost[key]))

    return [hyperperiod, num_of_tasks, data, comp_cost]

def Get_Neighborhood(position,radius,M,N): #return a list which consists of positions around input position with radius=r
    row=int(position/N)
    col=position%N
    neighborhood=[]
    for i in range(row-radius,row+radius+1):
        if(i>=0 and i<M):
            for j in range(col-radius,col+radius+1):
                if(j>=0 and (i!=row or j!=col) and j<N):
                    neighborhood.append(i*N+j)
    return neighborhood

def Get_mapping_exe_time(PEs_task_current_solution,Tasks_position_current_solution,computation_ability,num_of_rows,execution):#传进来的computation_ability是CVB_method生成的矩阵
    num_of_tasks=len(execution)-1
    ret_execution=copy.deepcopy(execution)
    for i in Tasks_position_current_solution.keys():#首先计算是否有task被匹配到同一个PE，有的话添加惩罚，但是如果是这同一个PE上有任务图中同一条边上的两个task，意思就是它们一定不同时运行，这个时候不计算惩罚
        position=Tasks_position_current_solution[i]
        if(len(PEs_task_current_solution[position])>1):
            ret_execution[i+1]=ret_execution[i+1]*len(PEs_task_current_solution[position])

    #然后根据运行能力倍减运行时间
    for i in Tasks_position_current_solution.keys():
        position=Tasks_position_current_solution[i]
        #ret_execution[i+1]=int(ret_execution[i+1]/computation_ability[int(position/num_of_rows)][position%num_of_rows])
        ret_execution[i+1]=computation_ability[i][position]

    ret=0
    for i in ret_execution:
        ret+=i
    return ret

def Get_detailed_data(num_of_tasks,edges,comp_cost):#输出邻接矩阵,total_needSend,total_needReceive,execution
    adj_matrix=np.zeros((num_of_tasks+1,num_of_tasks+1),dtype=np.int)#task从1开始算
    for i in range(0,len(edges)):
        for j in range(0,len(edges[i])):
            if(edges[i][j]!=-1):
                adj_matrix[i+1][j+1]=comp_cost[edges[i][j]]#adj_matrix[i][j]不为0，表示task i有到task j的出边，数组的值为待传输的量

    total_needSend=np.zeros(num_of_tasks+1,dtype=np.int)#task从1开始算的
    total_needReceive=np.zeros(num_of_tasks+1,dtype=np.int)#task从1开始算的
    for i in range(1,num_of_tasks+1):
        task_i_needSend=0
        for j in range(1,num_of_tasks+1):
            task_i_needSend+=adj_matrix[i][j]
        total_needSend[i]=task_i_needSend

    for j in range(1,num_of_tasks+1):
        task_j_needReceive=0
        for i in range(1,num_of_tasks+1):
            task_j_needReceive+=adj_matrix[i][j]
        total_needReceive[j]=task_j_needReceive

    execution=np.zeros(num_of_tasks+1,dtype=np.int)#task从1开始算的
    for i in range(1,num_of_tasks+1):
        execution[i]=comp_cost[i-1]

    return adj_matrix,total_needSend,total_needReceive,execution

def find_start_task(adj_matrix,num_of_tasks):#寻找入度为0的点
    ret=[]
    in_degree=np.zeros(num_of_tasks+1,dtype=np.int)
    for i in range(1,num_of_tasks+1):
        for j in range(1,num_of_tasks+1):
            if(adj_matrix[i][j]!=0):
                in_degree[j]+=1
    for i in range(1,num_of_tasks+1):
        if(in_degree[i]==0):
            ret.append(i)
    return ret

def get_sorted_dict(dict):#将task_graph按照task的序号排序，再传入online_compute
    ret={}
    l=[]
    for i in dict.keys():
        l.append(int(i))
    l.sort()
    for i in l:
        ret.update({str(i):dict[str(i)]})
    return ret

def Get_full_route_by_XY(part_route,source_position,dest_position,num_of_rows):
    ret=copy.deepcopy(part_route)
    dest_row=int(dest_position/num_of_rows)
    dest_col=dest_position%num_of_rows
    cur_row=-1
    cur_col=-1

    if(len(part_route)==0):
        cur_row=int(source_position/num_of_rows)
        cur_col=source_position%num_of_rows
    else:#计算出数据现在走到了哪个位置
        cur_position=part_route[-1][0]
        cur_row=int(cur_position/num_of_rows)
        cur_col=cur_position%num_of_rows
        if(part_route[-1][1]=='N'):
            cur_row-=1
        elif(part_route[-1][1]=='S'):
            cur_row+=1
        elif(part_route[-1][1]=='W'):
            cur_col-=1
        elif(part_route[-1][1]=='E'):
            cur_col+=1

    while(cur_col<dest_col):
        tmp=[]
        tmp.append(cur_row*num_of_rows+cur_col)
        tmp.append('E')
        ret.append(tmp)
        cur_col+=1
    while(cur_col>dest_col):
        tmp=[]
        tmp.append(cur_row*num_of_rows+cur_col)
        tmp.append('W')
        ret.append(tmp)
        cur_col-=1

    while(cur_row<dest_row):
        tmp=[]
        tmp.append(cur_row*num_of_rows+cur_col)
        tmp.append('S')
        ret.append(tmp)
        cur_row+=1
    while(cur_row>dest_row):
        tmp=[]
        tmp.append(cur_row*num_of_rows+cur_col)
        tmp.append('N')
        ret.append(tmp)
        cur_row-=1

    return ret

def Get_reward_by_pendTimes(pendTimes):
    return 0-pendTimes

"""
#state为[state_tensor,cur_position,partRouteFromRL]，传进来的partRoute的格式是直接的路由表，没有第一位第二位的task
def check_if_Done(state,source_position,dest_position,num_of_rows,task_graph,fullRouteFromRL,task_source,task_dest,MapResult):#检查当前的state是否已经结束，结束了的话直接把end_state,reward,done=True返回
    next_state_tensor=state[0]
    next_position=-1
    next_partRoute=state[2]

    cur_row=int(state[1]/num_of_rows)
    cur_col=state[1]%num_of_rows
    dest_row=int(dest_position/num_of_rows)
    dest_col=dest_position%num_of_rows

    flag=False

    if(cur_row==dest_row or cur_col==dest_col):#结束，先更新tensor
        flag=True
        if(cur_row==dest_row and cur_col==dest_col):#考虑到一开始两个task就被map到同一个PE的情况
            next_state_tensor=state[0]
        elif(cur_row==dest_row):
            if(cur_col<dest_col):#向East走
                for i in range(cur_col,dest_col):#更新tensor
                    next_state_tensor[0][3][cur_row*num_of_rows+i]=1
            else:#向West走
                for i in range(cur_col,dest_col,-1):#更新tensor
                    next_state_tensor[0][2][cur_row*num_of_rows+i]=1
        elif(cur_col==dest_col):
            if(cur_row<dest_row):#向South走
                for i in range(cur_row,dest_row):#更新tensor
                    next_state_tensor[0][1][i*num_of_rows+cur_col]=1
            else:#向North走
                for i in range(cur_row,dest_row,-1):
                    next_state_tensor[0][0][i*num_of_rows+cur_col]=1

    if(flag==False):#没有结束
        return [],0,False
    else:#结束
        #更新position
        next_position=dest_position
        #更新partRoute，此时的partRoute就是这一条链路全部的路由表，可以直接传进onlineCompute
        next_partRoute=Get_full_route_by_XY(state[2],source_position,dest_position,num_of_rows)
        #处理参数，传进onlineCompute计算pending次数
        #首先更新taskgraph里的这条链路的路由表
        for i in range(0,len(task_graph[str(task_source)]['out_links'])):
            if(int(task_graph[str(task_source)]['out_links'][i][0])==task_dest):
                task_graph[str(task_source)]['out_links'][i][2]=next_partRoute
        #处理partRoute
        partRoute_to_onlineCompute=[]
        partRoute_to_onlineCompute.append(task_source)
        partRoute_to_onlineCompute.append(task_dest)
        for i in next_partRoute:
            partRoute_to_onlineCompute.append(i)
        task=onlineTimeline("",num_of_rows)
        #print("C_fullRoute:",fullRouteFromRL)
        #print("C_partRoute:",partRoute_to_onlineCompute)
        #print("C_computing:",task_graph)
        task.loadGraphByDict(task_graph,MapResult,fullRouteFromRL,partRoute_to_onlineCompute,len(MapResult)-1)
        pendTimes=task.computeTime()
        #print("C_pendTimes",pendTimes)

        for j in task_graph.keys():
            for k in range(0,len(task_graph[j]['out_links'])):
                task_graph[j]['out_links'][k]=task_graph[j]['out_links'][k][0:6]

        #根据pendTimes计算reward
        return [next_state_tensor,next_position,next_partRoute],Get_reward_by_pendTimes(pendTimes),True

#state为[state_tensor,cur_position,partRouteFromRL]，传进来的partRoute的格式是直接的路由表，没有第一位第二位的task
def Environment(state,action,source_position,dest_position,num_of_rows,task_graph,fullRouteFromRL,task_source,task_dest,MapResult):#用于获得next_state，reward，done，除了state和action，剩下的参数都是为了传进onlineCompute
    next_state_tensor=torch.Tensor(np.zeros((1,4,num_of_rows*num_of_rows),dtype=np.int))
    next_state_tensor.copy_(state[0])
    next_position=-1
    next_partRoute=copy.deepcopy(state[2])

    #执行action前
    cur_row=int(state[1]/num_of_rows)
    cur_col=state[1]%num_of_rows
    dest_row=int(dest_position/num_of_rows)
    dest_col=dest_position%num_of_rows

    #RL学习之前check一次，就能确保起码能走一步

    #开始执行action
    #state_tensor的四个channel,从0-3以此为N,S,W,E
    if(action==0):#沿x轴走
        if(cur_col<dest_col):
            next_state_tensor[0][3][cur_row*num_of_rows+cur_col]=1#更新tensor
            next_partRoute.append([cur_row*num_of_rows+cur_col,'E'])#更新路由表
            cur_col+=1#向East走了一步
        elif(cur_col>dest_col):
            next_state_tensor[0][2][cur_row*num_of_rows+cur_col]=1#更新tensor
            next_partRoute.append([cur_row*num_of_rows+cur_col,'W'])#更新路由表
            cur_col-=1#向West走了一步
    elif(action==1):#沿y轴走
        if(cur_row<dest_row):
            next_state_tensor[0][1][cur_row*num_of_rows+cur_col]=1#更新tensor
            next_partRoute.append([cur_row*num_of_rows+cur_col,'S'])#更新路由表
            cur_row+=1#向South走了一步
        elif(cur_row>dest_row):
            next_state_tensor[0][0][cur_row*num_of_rows+cur_col]=1#更新tensor
            next_partRoute.append([cur_row*num_of_rows+cur_col,'N'])#更新路由表
            cur_row-=1#向North走了一步

    next_position=cur_row*num_of_rows+cur_col

    ret_state,ret_reward,done=check_if_Done([next_state_tensor,next_position,next_partRoute],source_position,dest_position,num_of_rows,task_graph,fullRouteFromRL,task_source,task_dest,MapResult)

    if(done==True):
        return ret_state,ret_reward,done
    else:#没有结束，需要计算reward
        #根据XY-routing补全路由表，然后传进onlineCompute
        fullRouteByXY=Get_full_route_by_XY(next_partRoute,source_position,dest_position,num_of_rows)
        #处理参数，传进onlineCompute计算pending次数
        #首先更新taskgraph里的这条链路的路由表
        for i in range(0,len(task_graph[str(task_source)]['out_links'])):
            if(int(task_graph[str(task_source)]['out_links'][i][0])==task_dest):
                task_graph[str(task_source)]['out_links'][i][2]=fullRouteByXY
        #处理partRoute
        partRoute_to_onlineCompute=[]
        partRoute_to_onlineCompute.append(task_source)
        partRoute_to_onlineCompute.append(task_dest)
        for i in next_partRoute:
            partRoute_to_onlineCompute.append(i)
        task=onlineTimeline("",num_of_rows)
        #print("fullRoute:",fullRouteFromRL)
        #print("partRoute:",partRoute_to_onlineCompute)
        #print("computing:",task_graph)
        task.loadGraphByDict(task_graph,MapResult,fullRouteFromRL,partRoute_to_onlineCompute,len(MapResult)-1)
        pendTimes=task.computeTime()
        #print("pendTimes",pendTimes)

        for j in task_graph.keys():
            for k in range(0,len(task_graph[j]['out_links'])):
                task_graph[j]['out_links'][k]=task_graph[j]['out_links'][k][0:6]

        #根据pendTimes计算reward
        return [next_state_tensor,next_position,next_partRoute],Get_reward_by_pendTimes(pendTimes),False
"""

class ActorCritic(nn.Module):#输入：channel*length,4是channel,N是PE 输出：1*action_space和1*1
    def __init__(self, input_channel,input_length, action_space):#input_length是PE的个数
        super(ActorCritic, self).__init__()

        self.critic = nn.Sequential(#输入为1*input_channel*input_length
            #nn.Linear(num_inputs, hidden_size),
            #nn.ReLU(),
            #nn.Linear(hidden_size, 1)#1*1
            nn.Conv1d(input_channel,1,input_length,1)#输出为 1*1*1
        )

        self.actor = nn.Sequential(
            #nn.Linear(num_inputs, hidden_size),
            #nn.ReLU(),
            #nn.Linear(hidden_size, action_space),#1*action_space
            #nn.Softmax(dim=1),
            nn.Conv1d(input_channel,1,(input_length-action_space+1),1),
            nn.Softmax(dim=2)#输出为1*1*action_space
        )

    def forward(self, x):
        value = self.critic(x)
        probs = self.actor(x)
        dist  = Categorical(probs)#.sample()得到的是1*1的二维矩阵tensor，例如[[1]]，再.numpy后可以得到正常的矩阵
        return dist, value

# Actor module, categorical actions only
class Actor(nn.Module):
    def __init__(self, input_channel, input_length, action_space):
        super().__init__()
        self.model = nn.Sequential(
            nn.Conv1d(input_channel,1,(input_length-action_space+1),1),
            nn.Softmax(dim=2)#输出为1*1*action_space
        )

    def forward(self, X):
        return self.model(X).squeeze(0).squeeze(0)

# Critic module
class Critic(nn.Module):
    def __init__(self, input_channel, input_length):
        super().__init__()
        self.model = nn.Sequential(
            nn.Conv1d(input_channel,1,input_length,1)#输出为 1*1*1
        )

    def forward(self, X):
        return self.model(X).squeeze(0).squeeze(0)

def Get_rand_computation_ability(num_of_rows):
    ret=np.random.randint(1,5,(num_of_rows,num_of_rows))
    return ret

def Get_rand_computation_ability2(num_of_rows):
    ret=np.full((num_of_rows,num_of_rows),0.5)
    for i in range(0,num_of_rows*num_of_rows):
        random_choose=np.random.randint(0,num_of_rows*num_of_rows)
        ret[int(random_choose/num_of_rows)][int(random_choose%num_of_rows)]+=0.5
    return ret

#improved版新添加的函数
#检查在used_link中，给定的时间区间是否有冲突
#返回是否有争用，哪一条link有争用，这条link在哪个时间段发生了争用
def Check_contention(used_link,link_set,start_time,end_time):
    for i in used_link:
        for j in link_set[i].timeline:
            if(end_time>j[0] and end_time<j[1]):
                return False,i,link_set[i].timeline.index(j)
            elif(start_time>j[0] and start_time<j[1]):
                return False,i,link_set[i].timeline.index(j)
            elif(start_time<j[0] and end_time>j[1]):
                return False,i,link_set[i].timeline.index(j)
            elif(start_time == j[0]):#目标区间起点与当前检测的区间起点相同，冲突
                return False,i,link_set[i].timeline.index(j)
            elif(end_time == j[1]):#目标区间终点与当前检测的区间终点相同，冲突
                return False,i,link_set[i].timeline.index(j)

    return True,-1,-1

def Get_link_index_by_route(route,num_of_rows):#根据route（如[6,"S"]）返回link的编号
    tmp_row=int(route[0]/num_of_rows)
    tmp_col=route[0]%num_of_rows
    if(route[1]=='N'):
        tmp_row-=1
        return (2*num_of_rows-1)*tmp_row+(num_of_rows-1)+tmp_col
    elif(route[1]=='S'):
        return (2*num_of_rows-1)*tmp_row+(num_of_rows-1)+tmp_col
    elif(route[1]=='W'):
        return (2*num_of_rows-1)*tmp_row+(tmp_col-1)
    elif(route[1]=='E'):
        return (2*num_of_rows-1)*tmp_row+tmp_col

def computeContention(partRoute,link_set,num_of_rows,start_time,transmission):
    used_link=[]
    for i in partRoute:
        used_link.append(Get_link_index_by_route(i,num_of_rows))
    contentious_link=[]
    contentious_timeline_index=[]
    end_time=start_time+transmission
    for i in used_link:
        for j in link_set[i].timeline:#检查这条link的时间轴
            if(end_time>j[0] and end_time<j[1]):#目标区间右侧落在了当前检测到的区间里，冲突
                contentious_link.append(i)
                contentious_timeline_index.append(link_set[i].timeline.index(j))
            elif(start_time>j[0] and start_time<j[1]):#目标区间左侧落在了当前检测到的区间里，冲突
                contentious_link.append(i)
                contentious_timeline_index.append(link_set[i].timeline.index(j))
            elif(start_time<j[0] and end_time>j[1]):#目标区间包括了当前检测到的区间，冲突
                contentious_link.append(i)
                contentious_timeline_index.append(link_set[i].timeline.index(j))
            elif(start_time == j[0]):#目标区间起点与当前检测的区间起点相同，冲突
                contentious_link.append(i)
                contentious_timeline_index.append(link_set[i].timeline.index(j))
            elif(end_time == j[1]):#目标区间终点与当前检测的区间终点相同，冲突
                contentious_link.append(i)
                contentious_timeline_index.append(link_set[i].timeline.index(j))

    if(len(contentious_link)==0):#这个route没有任何争用
        return 0
    else:#这个route有争用，需要计算参数T
        T_=0
        max_end_time=0
        for i in range(0,len(contentious_link)):#寻找争用过的link的最大结束时间
            if(link_set[ contentious_link[i] ].timeline[ contentious_timeline_index[i] ][1] > max_end_time):
                max_end_time=link_set[ contentious_link[i] ].timeline[ contentious_timeline_index[i] ][1]
        T_=max_end_time-start_time#设置T值的初始值，然后还需要检测这个T是否可以使得这条边传输
        flag_contention,link_index,timeline_index=Check_contention(used_link,link_set,start_time+T_,end_time+T_)
        while(flag_contention==False):#增加T后依然发生争用，还需要继续增大T
            T_=link_set[link_index].timeline[timeline_index][1]-start_time
            flag_contention,link_index,timeline_index=Check_contention(used_link,link_set,start_time+T_,end_time+T_)
        return T_

def Update_link_set(partRoute,link_set,num_of_rows,start_time,end_time):#在link_set上预约[start_time,end_time]的时间
    used_link=[]
    for i in partRoute:
        used_link.append(Get_link_index_by_route(i,num_of_rows))

    for i in used_link:#遍历要使用的link
        if(len(link_set[i].timeline)==0):#这条link的时间轴为空
            link_set[i].timeline.append([start_time,end_time])
        for j in range(0,len(link_set[i].timeline)):#遍历这条link的timeline
            if(end_time <= link_set[i].timeline[j][0]):#放在这个区间的左边
                link_set[i].timeline.insert(j,[start_time,end_time])
                break
            elif(start_time >= link_set[i].timeline[j][1]):#放在这个区间的右边
                if( j == (len(link_set[i].timeline)-1) ):#最后一位，append即可
                    link_set[i].timeline.append([start_time,end_time])
                else:
                    link_set[i].timeline.insert(j+1,[start_time,end_time])
                break

#这里reward返回的是-contention，需要再考虑一下
def Check_if_Done_improved(state,source_position,dest_position,link_set,num_of_rows,start_time,end_time):#state为[state_tensor,cur_position,partRoute],这里的partRoute是直接的路由表，如[[0,'S'],[4,'E']]
    next_state_tensor=torch.Tensor(np.zeros((1,4,num_of_rows*num_of_rows),dtype=np.int))
    next_state_tensor.copy_(state[0])
    cur_row=int(state[1]/num_of_rows)
    cur_col=state[1]%num_of_rows
    dest_row=int(dest_position/num_of_rows)
    dest_col=dest_position%num_of_rows

    if(cur_row==dest_row or cur_col==dest_col):#结束，需要使用XY routing补全路由，更新tensor，并且计算contention
        #更新tensor
        if(cur_row==dest_row and cur_col==dest_col):#考虑到一开始两个task就被map到同一个PE的情况
            pass
        elif(cur_row==dest_row):
            if(cur_col<dest_col):#向East走
                for i in range(cur_col,dest_col):#更新tensor
                    next_state_tensor[0][3][cur_row*num_of_rows+i]=1
            else:#向West走
                for i in range(cur_col,dest_col,-1):#更新tensor
                    next_state_tensor[0][2][cur_row*num_of_rows+i]=1
        elif(cur_col==dest_col):
            if(cur_row<dest_row):#向South走
                for i in range(cur_row,dest_row):#更新tensor
                    next_state_tensor[0][1][i*num_of_rows+cur_col]=1
            else:#向North走
                for i in range(cur_row,dest_row,-1):
                    next_state_tensor[0][0][i*num_of_rows+cur_col]=1
        #补全路由，计算contention
        full_Route=Get_full_route_by_XY(state[2],source_position,dest_position,num_of_rows)
        contention=computeContention(full_Route,link_set,num_of_rows,start_time,end_time-start_time)
        return [next_state_tensor,dest_position,full_Route],-1*contention,True
    else:
        return [],0,False

#这里reward返回的是-contention，需要再考虑一下
def Environment_improved(state,action,source_position,dest_position,link_set,num_of_rows,start_time,end_time):
    next_state_tensor=torch.Tensor(np.zeros((1,4,num_of_rows*num_of_rows),dtype=np.int))
    next_state_tensor.copy_(state[0])
    next_position=-1
    next_partRoute=copy.deepcopy(state[2])

    #执行action前
    cur_row=int(state[1]/num_of_rows)
    cur_col=state[1]%num_of_rows
    dest_row=int(dest_position/num_of_rows)
    dest_col=dest_position%num_of_rows

    #RL学习之前check一次，就能确保起码能走一步

    #开始执行action
    #state_tensor的四个channel,从0-3以此为N,S,W,E
    if(action==0):#沿x轴走
        if(cur_col<dest_col):
            next_state_tensor[0][3][cur_row*num_of_rows+cur_col]=1#更新tensor
            next_partRoute.append([cur_row*num_of_rows+cur_col,'E'])#更新路由表
            cur_col+=1#向East走了一步
        elif(cur_col>dest_col):
            next_state_tensor[0][2][cur_row*num_of_rows+cur_col]=1#更新tensor
            next_partRoute.append([cur_row*num_of_rows+cur_col,'W'])#更新路由表
            cur_col-=1#向West走了一步
    elif(action==1):#沿y轴走
        if(cur_row<dest_row):
            next_state_tensor[0][1][cur_row*num_of_rows+cur_col]=1#更新tensor
            next_partRoute.append([cur_row*num_of_rows+cur_col,'S'])#更新路由表
            cur_row+=1#向South走了一步
        elif(cur_row>dest_row):
            next_state_tensor[0][0][cur_row*num_of_rows+cur_col]=1#更新tensor
            next_partRoute.append([cur_row*num_of_rows+cur_col,'N'])#更新路由表
            cur_row-=1#向North走了一步

    next_position=cur_row*num_of_rows+cur_col

    ret_state,ret_reward,done=Check_if_Done_improved([next_state_tensor,next_position,next_partRoute],source_position,dest_position,link_set,num_of_rows,start_time,end_time)

    if(done==True):
        return ret_state,ret_reward,done
    else:
        contention=computeContention(next_partRoute,link_set,num_of_rows,start_time,end_time-start_time)
        return [next_state_tensor,next_position,next_partRoute],-1*contention,False

def CVB_method(execution,V_machine,num_of_rows):#execution里的task编号从0开始，没有多余的数据
    num_of_tasks=len(execution)
    mean_task=np.mean(execution)
    std_task=np.std(execution,ddof=1)
    V_task=std_task/mean_task
    #print("V_task=",V_task)
    #print("mean_task=",mean_task)
    alpha_task=1/(V_task*V_task)
    alpha_machine=1/(V_machine*V_machine)
    beta_task=mean_task/alpha_task
    #print("alpha_task=",alpha_task)
    #print("beta_task=",beta_task)
    beta_machine=[]
    q=[]
    e=np.zeros(shape=(num_of_tasks,num_of_rows*num_of_rows),dtype=np.int)
    for i in range(0,num_of_tasks):
        #q.append(np.random.gamma(shape=alpha_task,scale=beta_task))
        q.append(execution[i])
        #print("i=",i,"q[i]=",q[i])
        beta_machine.append(q[i]/alpha_machine)
        #print("i=",i,"beta_machine[i]=",beta_machine[i])
        for j in range(0,num_of_rows*num_of_rows):
            e[i][j]=int(np.random.gamma(shape=alpha_machine,scale=beta_machine[i]))
    return e

def read_NoC(NoC_file_name):
    ret=[]
    f=open(NoC_file_name)
    for line in f:
        tmp=[]
        for i in line[1:-2].split(','):
            tmp.append(int(i))
        ret.append(tmp)
    return ret

def init_from_json(input_json_file):
    task_graph={}
    with open(input_json_file,"r") as f:
        task_graph=json.load(f)
    num_of_tasks=len(task_graph)

    adj_matrix=np.zeros((num_of_tasks+1,num_of_tasks+1),dtype=np.int)#task从1开始算
    total_needSend=np.zeros(num_of_tasks+1,dtype=np.int)#task从1开始算的
    total_needReceive=np.zeros(num_of_tasks+1,dtype=np.int)#task从1开始算的
    execution=np.zeros(num_of_tasks+1,dtype=np.int)#task从1开始算的

    for i in task_graph.keys():#json里的task的序号是从0开始的
        total_needSend[int(i)+1]=task_graph[i]['total_needSend']
        total_needReceive[int(i)+1]=task_graph[i]['total_needReceive']
        execution[int(i)+1]=task_graph[i]['exe_time']
        for j in task_graph[i]['out_links']:
            adj_matrix[int(i)+1][int(j[0][0])+1]=int(j[0][1])

    return adj_matrix,total_needSend,total_needReceive,execution,num_of_tasks

def Get_weight(total_needSend,execution,c=4):
    exe_sum=0
    send_sum=0
    z=1
    num_of_tasks=len(execution)-1
    for i in range(1,num_of_tasks+1):
        exe_sum+=execution[i]
        send_sum+=total_needSend[i]

    map_w=exe_sum/z
    route_w=send_sum/c
    a=map_w/(map_w+route_w)
    b=route_w/(map_w+route_w)
    return a,b

if __name__ == '__main__':
    hyperperiod,num_of_tasks,edges,comp_cost=init('./task graph/N12_autocor.tgff')
    adj_matrix,total_needSend,total_needReceive,execution=Get_detailed_data(num_of_tasks,edges,comp_cost)
    #adj_matrix,total_needSend,total_needReceive,execution=init_from_json('./AIRfile/Autocor_Mesh8x8_AIR1_basic.json')

    print(Get_weight(total_needSend,execution,c=6))

    #print(execution[1:])
    #print(CVB_method(execution=execution[1:],V_machine=0.5,num_of_rows=4))


import logging
import sys
import threading
import os
import click

from anime_downloader.sites import get_anime_class, ALL_ANIME_SITES
from anime_downloader import util
from anime_downloader.__version__ import __version__

logger = logging.getLogger(__name__)

echo = click.echo
sitenames = [v[1] for v in ALL_ANIME_SITES]

class SiteThread(threading.Thread):
    def __init__(self, site, *args, **kwargs):
        self.site = site
        self.exception = None
        super().__init__(*args, **kwargs)

    def run(self):
        try:
            ani = get_anime_class(self.site)

            # this should be more dynamic
            sr = ani.search('naruto')[0]

            anime = ani(sr.url)

            stream_url = anime[0].source().stream_url
        except Exception as e:
            self.exception = e

@click.command()
@click.argument('test_query', default='naruto')
def command(test_query):
    """Test all sites to see which ones are working and which ones aren't. Test naruto as a default."""
    util.print_info(__version__)
    logger = logging.getLogger("anime_downloader")
    logger.setLevel(logging.ERROR)

    threads = []

    for site in sitenames:
        t = SiteThread(site, daemon=True)
        t.start()
        threads.append(t)

    for thread in threads:
        if os.name == 'nt':
            p, f = 'Works: ', "Doesn't work: "  # Emojis doesn't work in cmd
        else:
            p, f = '✅ ', '❌ '
        thread.join(timeout=10)
        if not thread.is_alive():
            if not thread.exception:
                # echo(click.style('Works ', fg='green') + site)
                echo(click.style(p, fg='green') + thread.site)
            else:
                logging.debug('Error occurred during testing')
                logging.debug(thread.exception)
                echo(click.style(f, fg='red') + thread.site)
        else:
            logging.debug('timeout during testing')
            echo(click.style(f, fg='red') + thread.site)


import cv2
import os
import numpy as np

subjects = ["", "Vishal", "Elvis Presley", "Test case 3", "Test_case 4"]

def detect_face(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    face_cascade = cv2.CascadeClassifier('opencv-files/lbpcascade_frontalface.xml')
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);

    if (len(faces) == 0):
        return None, None
    return faces

def prepare_training_data(data_folder_path):

    dirs = os.listdir(data_folder_path)
     #list to hold all subject faces
    faces = []
    #list to hold labels for all subjects
    labels = []

    for dir_name in dirs:
        if not dir_name.startswith("s"):
            continue;

        label = int(dir_name.replace("s", ""))
        subject_dir_path = data_folder_path + "/" + dir_name

        subject_images_names = os.listdir(subject_dir_path)

        for image_name in subject_images_names:
            if image_name.startswith("."):
                continue;

            image_path = subject_dir_path + "/" + image_name
            image = cv2.imread(image_path)
            cv2.imshow("Training on image...", cv2.resize(image, (400, 500)))
            cv2.waitKey(100)

            #detect face
            faces1 = detect_face(image)
            gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

            #for now ignoring the faces that are not detected
            if faces1[0] is not None:
                (x, y, w, h) = faces1[0]
                face = gray[y:y+w, x:x+h]
                rect = faces1[0]
                faces.append(face)
                labels.append(label)

    cv2.destroyAllWindows()
    cv2.waitKey(1)
    cv2.destroyAllWindows()

    return faces, labels

print("Preparing data...")
faces, labels = prepare_training_data("training-data")
print("Data prepared")

#print total faces and labels
print("Total faces: ", len(faces))
print("Total labels: ", len(labels))

face_recognizer = cv2.face.LBPHFaceRecognizer_create()
face_recognizer.train(faces, np.array(labels))

def draw_rectangle(img, rect):
    (x, y, w, h) = rect
    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)

#function to draw text on give image starting from passed (x, y) coordinates.
def draw_text(img, text, x, y):
    cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 255, 0), 2)

def predict(test_img):
    #make a copy of the original image
    img = test_img.copy()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces1 = detect_face(img)

    for i in faces1:
        (x, y, w, h) = i
        face = gray[y:y+w, x:x+h]
        rect = i
        #predict the image using our face recognizer
        label, confidence = face_recognizer.predict(face)
        label_text = subjects[label]

        #draw a rectangle around face detected
        draw_rectangle(img, rect)
        #draw name of predicted person
        draw_text(img, label_text, rect[0], rect[1]-5)

    return img

print("Predicting images...")

#load test images
test_img1 = cv2.imread("test-data/test1.jpg")
test_img2 = cv2.imread("test-data/test2.jpg")
test_img3 = cv2.imread("test-data/test3.jpg")
#test_img4 = cv2.imread("test-data/test4.jpg")
#test_img5 = cv2.imread("test-data/test5.jpg")

predicted_img1 = predict(test_img1)
predicted_img2 = predict(test_img2)
predicted_img3 = predict(test_img3)
#predicted_img4 = predict(test_img4)
#predicted_img5 = predict(test_img5)
print("Prediction complete")

cv2.imshow(subjects[1], cv2.resize(predicted_img1, (400,500)))
cv2.imshow(subjects[2], cv2.resize(predicted_img2, (400, 500)))
cv2.imshow(subjects[3], cv2.resize(predicted_img3, (400, 500)))
#cv2.imshow(subjects[4], cv2.resize(predicted_img4, (400, 500)))
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)
cv2.destroyAllWindows()


import sys
# #Test open excel
#
# value = '1003837'
# set = ['1003837', '1004969', '1004321', '1006065', '3006675']
#
# x = set.index(value)
# print(x)
#
# h = 169
# w = 76
# x = round(w/((h/100)**2), 1)
# print(x)
# a = 'hi'
# try:
#     a = float(a)
# except:
#     pass
# print(a)

str = 'C:\\Users\\sjg\\Desktop\\PCTL\\Source\\Excel Template input (standard 25072017).xls'
List = str.rsplit('.')
print(List)


class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        from collections import defaultdict
        c_chars = defaultdict(int)
        l, r, res = 0, 0, 0
        contract = False
        while r < len(s):
            # Expand
            if s[r] in c_chars and c_chars[s[r]] > 0: # check if contract is needed
                contract = True
            c_chars[s[r]] += 1
            r += 1

            # Contract
            if contract:
                d_char = s[r-1] # Char to remove
                while s[l] != d_char and l < r:
                    c_chars[s[l]] -= 1
                    l += 1
                c_chars[s[l]] -= 1
                l += 1
                contract = False

            res = max(res, r-l)
        return res

leetcode/3. Longest Substring Without Repeating Characters/soln.py


import numpy as np
import pandas as pd
import tensorflow as tf

def predict(model, x):
    return model.predict(np.array([x]))

if __name__ == '__main__':
    # 2159
    X = pd.read_csv('radio_train/input_data.csv').to_numpy()
    Y = pd.read_csv('radio_train/target_data.csv').to_numpy() + 0.2

    X_train = X[::2]
    Y_train = Y[::2]

    X_valid = X[1::2]
    Y_valid = Y[1::2]

    n = X_train.shape[0]
    dim_no = X_train.shape[1]
    class_no = Y_train.shape[1]

    model = tf.keras.models.load_model("model/", compile=True)
    model.summary()
    good = 0
    bad = 0
    error_interval = 0
    for i in range(len(X_valid)):
        x = X_valid[i]
        y = Y_valid[i]
        y_predict = predict(model, x)
        if any((y_predict + error_interval > y)[0]) and any((y_predict - error_interval < y)[0]):
            good += 1
        else:
            bad += 1
            print(f'pred: {y_predict} real: {y}')
    print(f'good {good}')
    print(f'bad {bad}')
    current_accuracy = good / (bad + good)
    print(f'{current_accuracy}')


# -*- coding: UTF-8 -*-
# Copyright 2013-2015 Luc Saffre
# License: BSD (see file COPYING for details)
"""Utilities, choicelists, mixins used by :mod:`lino_xl.lib.polls`.

"""

from lino.api import dd
from django.utils.translation import ugettext_lazy as _

class PollStates(dd.Workflow):
    """
    The list of possible states of a :class:`Poll`. Default is:

    .. django2rst::

       rt.show(polls.PollStates)

    """
    verbose_name_plural = _("Poll States")
    required_roles = dd.required(dd.SiteStaff)

add = PollStates.add_item
add('10', _("Draft"), 'draft')
add('20', _("Published"), 'published')
add('30', _("Closed"), 'closed')

PollStates.published.add_transition(
    _("Publish"), required_states='draft')
PollStates.closed.add_transition(
    _("Close"), required_states='draft published')
PollStates.draft.add_transition(
    _("Reopen"), required_states='published closed')

class ResponseStates(dd.Workflow):
    """
    The list of possible states of a :class:`Poll`. Default is:

    .. django2rst::

       rt.show(polls.ResponseStates)

    """
    verbose_name_plural = _("Response States")
    required_roles = dd.required(dd.SiteStaff)

add = ResponseStates.add_item
add('10', _("Draft"), 'draft', editable=True)
add('20', _("Registered"), 'registered', editable=False)

ResponseStates.registered.add_transition(
    _("Register"), required_states='draft')
ResponseStates.draft.add_transition(
    _("Deregister"), required_states="registered")


from django.conf.urls import url,include
from .views import *

urlpatterns = [
    url(r'^add/$', add_to_cart, name='add_to_cart'),
    url(r'^view_cart', view_cart, name='view_cart'),
    url(r'^remove/(\d+)$', remove_from_cart, name='remove_from_cart'),
    url(r'^update/(\d+)$', update_quantity, name='update_quantity'),
    url(r'^coupon/apply$',apply_coupon,name="apply_coupon"),
     url(r'^addc/$', add_to_cart_product_list, name='add_to_cart_product_list'),

    ]


import unittest

import bs4
import requests

DEV_URL = 'http://localhost:8000'

class FunctionalTests(unittest.TestCase):
    def test_server_is_running(self):
        text = 'django'
        req = requests.get(DEV_URL)
        page = bs4.BeautifulSoup(req.content, 'html.parser')
        self.assertIn(text, page.title.string.lower())

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


from util.filehelper import file_to_array

def calculate_fuel_needed(mass: int) -> int:
    """
    Fuel required to launch a given module is based on its mass.
    Specifically, to find the fuel required for a module, take its mass, divide
    by three, round down, and subtract 2.
    """
    return int(mass / 3) - 2

def calculate_fuel_for_fuel(mass: int) -> int:
    """
    So, for each module mass, calculate its fuel and add it to the total.
    Then, treat the fuel amount you just calculated as the input mass and
    repeat the process,
    continuing until a fuel requirement is zero or negative.
    """
    fuel_needed = calculate_fuel_needed(mass)
    if fuel_needed <= 0:
        return 0
    else:
        return fuel_needed + calculate_fuel_for_fuel(fuel_needed)

def day01_01():
    path = "./puzzles/01/puzzle.txt"
    fuel_list = [calculate_fuel_needed(mass) for mass in file_to_array(path)]
    fuel_needed = sum(fuel_list)
    print(f"fuel needed: {fuel_needed}")

def day01_02():
    path = "./puzzles/01/puzzle02.txt"
    fuel_list = [calculate_fuel_for_fuel(mass) for mass in file_to_array(path)]
    fuel_needed = sum(fuel_list)
    print(f"fuel needed for fuel: {fuel_needed}")

if __name__ == "__main__":
    day01_01()
    day01_02()


def separator():
    print("-------------------")

# The lambda operator or lambda function is a way to create small anonymous
# functions, i.e. functions without a name.
# These functions are throw-away functions, i.e. they are just needed where they
# have been created. Lambda functions are mainly used in combination with the
# functions filter(), map() and reduce().
sum_of_two = lambda x, y: x + y
print(sum_of_two(3, 7))

separator()

# map(func, seq)
# The first argument func is the name of a function and the second a sequence
# (e.g. a list) seq. map() applies the function func to all the elements of the
# sequence seq.
C = [39.2, 36.5, 37.3, 38, 37.8]
F = list(map(lambda x: (float(9)/5)*x + 32, C))
print(F)

C = list(map(lambda x: (float(5)/9)*(x-32), F))
print(C)

# map() can be applied to more than one list. The lists have to have the same
# length. map() will apply its lambda function to the elements of the argument
# lists, i.e. it first applies to the elements with the 0th index, then to the
# elements with the 1st index until the n-th index is reached
a = [1, 2, 3, 4]
b = [17, 12, 11, 12]
c = [-1, -4, 5, 9]

print(list(map(lambda x,y:x+y, a,b)))
print(list(map(lambda x,y,z: x+y+z, a,b,c)))
print(list(map(lambda x,y,z : 2.5*x + 2*y - z, a,b,c)))

separator()

# filter(function, sequence)
# offers an elegant way to filter out all the elements of a sequence "sequence",
# for which the function function returns True. i.e. an item will be produced by
# the iterator result of filter(function, sequence) if item is included in the
# sequence "sequence" and if function(item) returns True.
fibonacci = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
odd_numbers = list(filter(lambda x: x % 2, fibonacci))
print(odd_numbers)

separator()

# reduce(func, seq)
# continually applies the function func() to the sequence seq. It returns a
# single value.
import functools
red = functools.reduce(lambda x, y: x+y, [47, 11, 42, 13])
print(red)

# Calculating the sum of the numbers from 1 to 100:
print(functools.reduce(lambda x, y: x+y, range(1, 101)))


"""
给定一个非空且只包含非负数的整数数组 nums，数组的度的定义是指数组里任一元素出现频数的最大值。

你的任务是在 nums 中找到与 nums 拥有相同大小的度的最短连续子数组，返回其长度。

示例 1：

输入：[1, 2, 2, 3, 1]
输出：2
解释：
输入数组的度是2，因为元素1和2的出现频数最大，均为2.
连续子数组里面拥有相同度的有如下所示:
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
最短连续子数组[2, 2]的长度为2，所以返回2.
示例 2：

输入：[1,2,2,3,1,4,2]
输出：6

提示：

nums.length 在1到 50,000 区间范围内。
nums[i] 是一个在 0 到 49,999 范围内的整数。
通过次数38,237提交次数66,082

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/degree-of-an-array
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
"""
from typing import *
from collections import Counter

class Solution:

    def findShortestSubArray(self, nums: List[int]) -> int:
        def check(counter, freq):
            _, cur_freq = counter.most_common(1)[0]
            return cur_freq >= freq

        counter = Counter(nums)
        _, freq = counter.most_common(1)[0]

        left, right = 0, 0
        num_counter, length = Counter(), len(nums)
        while right < len(nums):
            n = nums[right]
            right += 1
            num_counter[n] += 1
            flag = False
            while check(num_counter, freq):
                flag = True
                num_counter[nums[left]] -= 1
                left += 1
            if flag:
                tmp = right - left + 1
                length = tmp if tmp < length else length
        return length

if __name__ == '__main__':
    s = Solution()
    print(s.findShortestSubArray([1,2,2,3,1,4,2]))


# Данный пример позволяет задавать параметры сервопривода.
                                        # $ Строки со знаком $ являются необязательными.
from pyiArduinoI2Cexpander import *     #   Подключаем библиотеку для работы с расширителем выводов.
from time import sleep                  #   Импортируем функцию ожидания из модуля времени
ext = pyiArduinoI2Cexpander(0x08)       #   Объявляем объект ext для работы с функциями модуля pyiArduinoI2Cexpander, указывая адрес модуля на шине I2C.
                                        #
ext.pinMode(0, OUTPUT, SERVO)           # $ Конфигурируем вывод 0 на работу в качестве выхода для сервопривода.
ext.pinMode(1, OUTPUT, SERVO)           # $ Конфигурируем вывод 1 на работу в качестве выхода для сервопривода.
ext.pinMode(2, OUTPUT, SERVO)           # $ Конфигурируем вывод 2 на работу в качестве выхода для сервопривода.
ext.pinMode(3, OUTPUT, SERVO)           # $ Конфигурируем вывод 3 на работу в качестве выхода для сервопривода.
ext.servoAttach(0,  500, 2500,          #   Определяем параметры серпопривода,
                0,  180)                #   с углом поворота 180°, подключённого к выводу 0.
ext.servoAttach(1,  900, 2100,          #   Определяем параметры серпопривода,
                0,   90)                #   с углом поворота 90°,  подключённого к выводу 1.
ext.servoAttach(2, 1320, 1600,          #   Определяем параметры серпопривода,
                -100, +100)             #   постоянного вращения,  подключённого к выводу 2.
ext.servoAttach(3, 1100, 1700,          #   Определяем параметры серпопривода,
                -100, +100)             #   постоянного вращения,  подключённого к выводу 3.
print("Управляем сервоприводами. "      #
      "Нажмите ctrl+c для выхода.")     #   Назначение аргументов функции servoAttach() см. внизу скетча.
                                        #
while True:                             #
  ext.servoWrite(0,   0)                #   Поворачиваем сервопривод в минимальный угол.
  ext.servoWrite(1,   0)                #   Поворачиваем сервопривод в минимальный угол.
  ext.servoWrite(2, -60)                #   Заставляем медленно вращаться сервопривод против часовой стрелки.
  ext.servoWrite(3, -60)                #   Заставляем медленно вращаться сервопривод против часовой стрелки.
  sleep(.5)                             #   Ждём пол секунды.
  ext.servoWrite(0,  90)                #   Поворачиваем сервопривод в среднее положение.
  ext.servoWrite(1,  45)                #   Поворачиваем сервопривод в среднее положение.
  ext.servoWrite(2,   0)                #   Останавливаем сервопривод постоянного вращения.
  ext.servoWrite(3,   0)                #   Останавливаем сервопривод постоянного вращения.
  sleep(.5)                             #   Ждём пол секунды.
  ext.servoWrite(0, 180)                #   Поворачиваем сервопривод в максимальный угол.
  ext.servoWrite(1,  90)                #   Поворачиваем сервопривод в максимальный угол.
  ext.servoWrite(2, +60)                #   Заставляем медленно вращаться сервопривод по часовой стрелке.
  ext.servoWrite(3, +60)                #   Заставляем медленно вращаться сервопривод по часовой стрелке.
  sleep(.5)                             #   Ждём пол секунды.
                                        #
#  ПРИМЕЧАНИЕ:
#  Для проверки работы скетча подключите сервоприводы к выводам.
#  Параметры сервоприводов задаются из их datasheet.
#  Функция servoAttach() принимает до 5 аргументов:
#  1 - номер вывода к которому подключён сервопривод.
#  2 - минимальная  ширина импульсов ШИМ в микросекундах.
#  3 - максимальная ширина импульсов ШИМ в микросекундах.
#  4 - угол или значение соответствующее минимальной  ширине имп.
#  5 - угол или значение соответствующее максимальной ширине имп.
#  ПРИМЕР:
#  Сервопривод с углом поворота в 45° подключён к 3 выводу модуля.
#  При 0° ширина ШИМ = 600мкс, при 45° ширина ШИМ = 2300мкс.
#  ext.servoAttach(3, 600, 2300, 0, 45)
#  ext.servoWrite(3, угол от 0 до 45° )


#!/usr/bin/python3.5
import discord
import asyncio
import json
import random
import time
import math

DAB_EMOJI = "<:ledabbinganimegirl:256497986979233792>"
GIVE_SYNTAX = """`$give` syntax: `$give [amount] [@person]` or `$give [@person] [amount]`.
Give dabs to another person.
Make sure you have enough dabs. {}""".format(DAB_EMOJI)
BR_SYNTAX = """`$br` syntax: `$br [number]`.
Bet dabs on a roll of 1 to 100.
Make sure you have enough dabs. {}""".format(DAB_EMOJI)
BF_SYNTAX = """`$bf` syntax: `$bf [amount] [heads or tails]`.
Bet dabs on a coin flip with a 1.8* payout.
h or t works as well as head or tail.
Make sure you have enough dabs. {}""".format(DAB_EMOJI)

def pretty_table(l):
    maxs = [max(map(lambda x:len(str(x[i])), l)) for i in range(len(l[0]))]
    for row in range(len(l)):
        for col in range(len(l[row])):
            l[row][col] = str(l[row][col]) + (" " * (maxs[col] - len(str(l[row][col]))))
    l = list(map(lambda x: [""] + x + [""], l))
    dashes = list(map(lambda x: "━"*x, maxs))
    top = "┏" + "┳".join(dashes) + "┓"
    mid = "┣" + "╋".join(dashes) + "┫"
    bot = "┗" + "┻".join(dashes) + "┛"
    out = ""
    out += top + "\n"
    for i in l:
        out += "┃".join(i) + "\n"
        if i != l[-1]:
            out += mid + "\n"
    out += bot
    return out

def mappu(old, new):
    # translates a num range to another
    # old value to be translated goes mappu((HERE, 0,100),(0,849725982795))
    if len(old) == 3 and len(new) == 2:
        old_v = old[0]
        old_mi = min(old[1:3])
        old_ma = max(old[1:3])
        new_mi = min(new)
        new_ma = max(new)

        return ((old_v - old_mi) / (old_ma - old_mi)) * (new_ma - new_mi) + new_mi

class Client(discord.Client):
    def __init__(self):
        super().__init__()
        self.shitpost_count = 0
        self.shitpost_target = random.randint(50, 200)
        self.dab_count = 0
        self.dab_target = random.randint(20, 100)
        self.dab_announce = None
        self.prices = {
            "roles": {
                'Radio Mod': 1000},
            "items": {
                'droll x 10': 100}
        }
        self.niceroles = None
        self.exec_out = None

        with open("user_data.json") as json_file:
            self.user_data = json.load(json_file)

    @asyncio.coroutine
    def on_ready(self):
        print('Logged in as')
        print(client.user.name)
        print(client.user.id)
        print('------')
        nice_server = self.get_server("182829461786460161")
        self.niceroles = [role.name for role in nice_server.roles]

    @asyncio.coroutine
    def on_message(self, message):
        if message.author == self.user:
            return
        if message.content == "":
            return
        if message.channel.id in ["183205223609663488", "183214846534352897"]:
            return
        if "182951530591158272" in [role.id for role in message.author.roles]:
            return

        split = message.content.split()

        if split[0] == "$$":
            yield from self.check_user(message.channel, message.author)
            if message.mentions:
                yield from self.check_user(message.channel, message.mentions[0])
                if self.user_data.get(message.mentions[0].id) is None:
                    return
                member = message.mentions[0]
            else:
                member = message.author
            money = self.user_data[member.id]["money"]
            level = self.user_data[member.id]["level"]
            rolls = self.user_data[member.id]["rolls"]
            fmt = [member.nick or member.name, money, DAB_EMOJI, level, rolls]
            msg = "{0} has {1} dabs. {2}\n{0} is level {3} and has {4} rolls left today.".format(*fmt)
            yield from self.send_message(message.channel, msg)

        if split[0] == "$give":
            yield from self.check_user(message.channel, message.author)
            if len(split) == 3 and message.mentions:
                yield from self.check_user(message.channel, message.mentions[0])
                if self.user_data.get(message.mentions[0].id) is None:
                    return
                if split[1].isdigit():
                    amount = int(split[1])
                elif split[2].isdigit():
                    amount = int(split[2])
                else:
                    return
                if self.user_data[message.author.id]["money"] >= amount:
                    self.user_data[message.author.id]["money"] -= amount
                    yield from self.give_money(amount, message.mentions[0])
                    name1 = message.author.nick or message.author.name
                    name2 = message.mentions[0].nick or message.mentions[0].name
                    msg = "{} gave {} dabs {} to {}.".format(name1, str(amount), DAB_EMOJI, name2)

                else:
                    msg = "You don't have enough dabs. {}".format(DAB_EMOJI)
                yield from self.send_message(message.channel, msg)
                return
            yield from self.send_message(message.channel, GIVE_SYNTAX)

        if split[0] == "$lb":
            lb = []
            if len(split) > 1 and split[1].isdigit():
                num = int(split[1])
            else:
                num = 10
            for user_id in self.user_data:
                if isinstance(self.user_data[user_id]["money"], str):
                    continue
                member = message.server.get_member(user_id)
                if member is None:
                    continue
                lb.append([member.nick or member.name, self.user_data[user_id]["money"]])
            lb.sort(key=lambda x: x[1], reverse=True)
            lb = "```" + pretty_table(lb[:num]) + "```"
            yield from self.send_message(message.channel, lb)
            return

        if split[0] == "$br":
            yield from self.check_user(message.channel, message.author)
            if len(split) >= 2 and split[1].isdigit():
                amount = int(split[1])
                if amount <= self.user_data[message.author.id]["money"]:
                    self.user_data[message.author.id]["money"] -= amount
                    num = random.randint(1, 100)
                    if num < 66:
                        suffix = "no dabs. "
                    elif num < 90:
                        suffix = "{} dabs! ".format(amount*2)
                        self.user_data[message.author.id]["money"] += amount*2
                    elif num < 100:
                        suffix = "{} dabs for getting above 90! ".format(int(amount*3.5))
                        self.user_data[message.author.id]["money"] += int(amount*3.5)
                    elif num == 100:
                        suffix = "{} dabs! PERFECT ROLL! ".format(amount*10)
                        self.user_data[message.author.id]["money"] += amount*10
                    else:
                        suffix = "Something went wrong."
                    prefix = "{} rolled {} and won ".format(message.author.nick or message.author.name, num)
                    msg = prefix + suffix + DAB_EMOJI
                    yield from self.send_message(message.channel, msg)
                else:
                    yield from self.send_message(message.channel, "You don't have enough dabs. {}".format(DAB_EMOJI))
            else:
                yield from self.send_message(message.channel, BR_SYNTAX)
            return

        if split[0] == "$bf":
            yield from self.check_user(message.channel, message.author)
            words = ["t", "h", "head", "tail", "heads", "tails"]
            if len(split) >= 3 and\
                ((split[1].isdigit() and split[2] in words) or
                    (split[2].isdigit() and split[1] in words)):
                if split[1].isdigit():
                    amount = int(split[1])
                    guess = split[2][0]
                else:
                    amount = int(split[2])
                    guess = split[1][0]
                if amount < 3:
                    yield from self.send_message(message.channel, "Minimal bet is 3.")
                    return
                if amount <= self.user_data[message.author.id]["money"]:
                    self.user_data[message.author.id]["money"] -= amount
                    if random.choice([True, False]):
                        with open("makotocoinhead.png", "rb") as heads:
                            yield from self.send_file(message.channel, heads, content="Coin flip: heads.")
                        success = guess == "h"
                    else:
                        with open("makotocointails.png", "rb") as tails:
                            yield from self.send_file(message.channel, tails, content="Coin flip: tails.")
                        success = guess == "t"
                    if success:
                        self.user_data[message.author.id]["money"] += int(amount*1.8)
                        msg = "Congratz you win {} dabs! {}".format(int(amount*1.8), DAB_EMOJI)
                    else:
                        msg = "You win nothing."
                    yield from self.send_message(message.channel, msg)
                else:
                    yield from self.send_message(message.channel, "You don't have enough dabs. {}".format(DAB_EMOJI))
            else:
                yield from self.send_message(message.channel, BF_SYNTAX)
            return

        if split[0] == "$del":
            if len(split) == 2 and message.mentions and message.author.id == "147790355776012289":
                if self.user_data.get(message.mentions[0].id) is not None:
                    del self.user_data[message.mentions[0].id]
                    msg = "{} deleted from database.".format(message.mentions[0].nick or message.mentions[0].name)
                else:
                    msg = "{} not found in database.".format(message.mentions[0].nick or message.mentions[0].name)
                yield from self.send_message(message.channel, msg)
            return

        if split[0] == "$exec":
            if message.author.id == "147790355776012289":
                cmd = " ".join(split[1:])
                cmd = cmd.strip("`")
                cmd = cmd.strip()
                try:
                    self.exec_out = None
                    exec(cmd)
                    if self.exec_out is not None:
                        yield from self.send_message(message.channel, str(self.exec_out))
                except Exception as e:
                    yield from self.send_message(message.channel, "```{}```".format(str(e)))
            return

        if split[0] == "$drool":
            yield from self.check_user(message.channel, message.author)

            if message.channel.id == "182829461786460161" or message.channel.id == "223361230172061696":
                yield from self.send_message(message.channel,
                                             "You were TOLD my MARCH to no drool in #general or #oc_channel!")
                return

            if self.user_data[message.author.id]["rolls"] > 0:
                drools = 1

                if len(split) > 1 and split[1].isdigit():
                    drools = int(split[1])
                elif len(split) > 1 and split[1].lower() == "all":
                    drools = self.user_data[message.author.id]["rolls"]

                if self.user_data[message.author.id]["rolls"] >= drools:
                    self.user_data[message.author.id]["rolls"] -= drools
                    wins_shortened = {
                        0: "No dubs :(", 1: "*dubs*", 2: "**trips**", 3: "***quads***",
                        4: "quints!", 5: "***WHAT TO HECK, SEXTUPLES***",
                        6: "***WOWE! LOWEST ROL POSSIBLE!!***", 7: "***WOWE! HIHGEST ROL POSSIBLE!!***\n"
                    }
                    wins = {
                        0: 'No dubs :frowning:', 1: "Dubs! Nice! Check'em!", 2: 'Trips?! Whoa, check those bad boys!',
                        3: 'Quads?! No way! Witnessed!', 4: "PENTS?! HOT DAMN! Now that's #rare.",
                        5: 'TOO MANY REPEATING DIGITS. GET THIS PERSON A MEDAL!', 6: 'LOWEST', 7: "HIGHEST"
                    }
                    numdict = {
                        "1": "one", "2": "two", "3": "three", "4": "four", "5": "five",
                        "6": "six", "7": "seven", "8": "eight", "9": "nine", "0": "zero"
                    }
                    prize_dict = {0: 1, 1: 10, 2: 100, 3: 250, 4: 500, 5: 750}
                    level_mod = math.log(self.user_data[message.author.id]["level"]) + 1

                    if drools > 1:
                        finalprize, out = 0, ""
                        for i in range(0, drools):
                            roll = str(random.randint(0, 1000000))
                            if roll in (0, 1000000):
                                out += wins_shortened[mappu((int(roll), 0, 1000000), (6, 7))]
                                finalprize += int(level_mod * 1000)
                                break
                            else:
                                win = 0

                                for i2 in range(-1, -len(roll), -1):
                                    if roll[i2] == roll[i2-1]:
                                        win += 1
                                    else:
                                        break

                                out += "{2} roll {0} {1}\n".format(str(i)+'. ',
                                                                   wins_shortened[win],
                                                                   ''.join([":{}:".format(numdict[i]) for i in roll]))
                                finalprize += prize_dict[win] * level_mod
                        finalprize = int(finalprize)
                        out += "You won {} dabs {} total!".format(finalprize, DAB_EMOJI)
                        self.user_data[message.author.id]["money"] += finalprize
                        yield from self.send_message(message.channel, out)

                    else:
                        roll = str(random.randint(0, 1000000))
                        win = 0

                        for i in range(-1, -len(roll), -1):
                            if roll[i] == roll[i-1]:
                                win += 1
                            else:
                                break

                        yield from self.send_message(message.channel, ''.join([":{}:".format(numdict[i]) for i in roll]))
                        if roll in ("0", "1000000"):
                            msg = "WOW! {} POSSIBLE ROLL! What does this mean?".format(win[mappu((int(roll), 0, 1000000), (0, 7))])
                            prize = int(level_mod * 1000)
                        else:
                            msg = wins[win]
                            prize = int(prize_dict[win] * level_mod)

                        msg += "\n"+"{} wins {} dabs. {}".format(message.author.nick or message.author.name,
                                                                 prize,
                                                                 DAB_EMOJI)
                        yield from self.send_message(message.channel, msg)
                        self.user_data[message.author.id]["money"] += prize
            else:
                yield from self.send_message(message.channel, "No daily dub rolls left. Try again tomorrow.")
            return

        if split[0] == "$level":
            yield from self.check_user(message.channel, message.author)
            level = self.user_data[message.author.id]["level"]
            if len(split) >= 2 and split[1] == "all":
                cost = int((level ** 2) / 11)
                money = self.user_data[message.author.id]["money"]
                if cost > money:
                    yield from self.send_message(message.channel, "You can't afford to level up at all right now.")
                    return
                templevel = 1
                while cost < money:
                    templevel += 1
                    cost += int(((level + templevel) ** 2) / 11)
                cost -= int(((level + templevel) ** 2) / 11)
                templevel -= 1
                yield from self.send_message(message.channel, "You can level up {} times and it would cost {} dabs.".format(templevel, cost))
                yield from self.send_message(message.channel, "Would you like to buy the level? `y/n`")
                check = lambda x: x.content.lower()[0] == "y" or x.content.lower()[0] == "n"
                response = yield from self.wait_for_message(timeout=10, channel=message.channel, author=message.author,
                                                            check=check)
                if response is not None and response.content.lower()[0] == "y":
                    self.user_data[message.author.id]["money"] -= cost
                    self.user_data[message.author.id]["level"] += templevel
                    level = self.user_data[message.author.id]["level"]
                    yield from self.send_message(message.channel, "You are now level {}!".format(level))
                    yield from self.update_json()

            else:
                cost = int((level ** 2) / 11)
                yield from self.send_message(message.channel, "You are level {}, to level up it costs {} dabs.".format(level, cost))
                if cost > self.user_data[message.author.id]["money"]:
                    yield from self.send_message(message.channel, "You can't afford it right now. :(")
                else:
                    yield from self.send_message(message.channel, "Would you like to buy the level? `y/n`")
                    check = lambda x: x.content.lower()[0] == "y" or x.content.lower()[0] == "n"
                    response = yield from self.wait_for_message(timeout=10, channel=message.channel,
                                                                author=message.author, check=check)
                    if response is not None and response.content.lower()[0] == "y":
                        self.user_data[message.author.id]["money"] -= cost
                        self.user_data[message.author.id]["level"] += 1
                        level = self.user_data[message.author.id]["level"]
                        yield from self.send_message(message.channel, "You are now level {}!".format(level))
                        yield from self.update_json()
            return

        yield from self.update_vars(message)

    @asyncio.coroutine
    def check_user(self, channel, member):
        if member == self.user:
            return

        if "182951530591158272" in [role.id for role in member.roles]:
            return

        if self.user_data.get(member.id) is None:
            yield from self.send_message(channel, "{} not found in database, 100 free dabs! {}".format(member.nick or member.name, DAB_EMOJI))
            self.user_data[member.id] = {"money": 100, "rolls": 10, "level": 1, "claimed": -1}

        if isinstance(self.user_data[member.id]["money"], str):
            return

        if self.user_data[member.id].get("record") is None:
            self.user_data[member.id]["record"] = self.user_data[member.id]["money"]

        if self.user_data[member.id]["record"] > self.user_data[member.id]["money"]:
            self.user_data[member.id]["record"] = self.user_data[member.id]["money"]

        if self.user_data[member.id]["claimed"] != time.gmtime().tm_yday:
            self.user_data[member.id]["claimed"] = time.gmtime().tm_yday
            level_mod = math.log(self.user_data[member.id]["level"])
            self.user_data[member.id]["rolls"] += int(level_mod * 10)
            yield from self.send_message(channel, "{} has {} daily rolls for dubs left.".format(member.nick or member.name, self.user_data[member.id]["rolls"]))

        yield from self.update_json()

    @asyncio.coroutine
    def give_money(self, amount, member):
        self.user_data[member.id]["money"] += amount
        yield from self.update_json()

    @asyncio.coroutine
    def update_vars(self, message):
        self.shitpost_count += 1
        print("shitpost counter: {}\nshitpost target: {}".format(self.shitpost_count, self.shitpost_target))
        print("---")
        if "dab" in message.content.lower():
            print("dab message, skipping")
        else:
            self.dab_count += 1
            print("dab counter: {}\ndab target: {}".format(self.dab_count, self.dab_target))
        print("------")
        if self.shitpost_count == self.shitpost_target:
            yield from self.send_message(message.channel, '^ interesting.')
            self.shitpost_count = 0
            self.shitpost_target = random.randint(10, 100)
        elif self.shitpost_count > self.shitpost_target:
            self.shitpost_count = 0
            self.shitpost_target = random.randint(10, 100)

        if self.dab_count == self.dab_target:
            dabs = random.randint(1, 10)
            with open("bca.jpg", "rb") as dabpic:
                msg = "{} dabs have appeared! DAB TO GET EM!".format(dabs)
                self.dab_announce = yield from self.send_file(message.channel, dabpic, content=msg)
            self.dab_count = 0
            self.dab_target = random.randint(20, 100)
            check = lambda x: "dab" in x.content.lower() and x.author != self.user
            claim = yield from self.wait_for_message(channel=message.channel, check=check)
            self.check_user(claim.channel, claim.author)
            msg = "{} has claimed {} dabs!".format(claim.author.nick or claim.author.name, dabs)
            award = yield from self.send_message(message.channel, msg)
            self.user_data[claim.author.id]["money"] += dabs
            if self.dab_announce is not None:
                yield from self.delete_message(self.dab_announce)
            yield from asyncio.sleep(10)
            yield from self.delete_message(award)
        elif self.dab_count > self.dab_target:
            self.dab_count = 0
            self.dab_target = random.randint(20, 100)

    @asyncio.coroutine
    def update_json(self):
        with open("user_data.json", "w") as json_file:
            json.dump(self.user_data, json_file)

if __name__ == "__main__":
    with open("token") as f:
        client = Client()
        client.run(f.read())


#!/usr/bin/python
# _*_ coding:utf-8 _*_
# 音乐爬虫

if __name__ == "__main__":
    # num = 245610
    # print(int(num/2000))
    # print(int(num/30000))
    import requests
    from bs4 import BeautifulSoup
    file = open("music.html","r")
    html_doc = file.read()
    # print(html_doc)

    soup = BeautifulSoup(html_doc, 'html.parser')
    # print(soup.prettify())
    content1 = soup.find(id="content1")

    li = content1.find_all("li")

    #目标音乐站
    url = "http://96.ierge.cn/"
    musicid = []
    musicname = []
    musicurl = []
    for x in li:
        mtitle = x.a.get("title")
        musicname.append(mtitle)
        ids = x.input.get("title")
        musicid.append(ids)
        n1 = int(int(ids)/30000)
        n2 = int(int(ids)/2000)
        xurl = url + "/" + str(n1) + "/" + str(n2) + "/" + ids+".mp3"
        musicurl.append(xurl)
        # print x.input.get("title")
        print(xurl)
        print(mtitle)

        # url = 'http://96.ierge.cn/7/109/218490.mp3'
        r = requests.get(xurl)
        with open("music/"+mtitle+".mp3", "wb") as code:
            code.write(r.content)
        print(" 下载完成。。。。")


import sys
from math import sqrt
from random import randint
def sustava(s, k):
    vysledok = 0
    for i in range(0, len(s)):
        vysledok = vysledok * k + int(s[i])
    return vysledok

def najdiDelitela(c):
    for i in range(2, min(int(sqrt(c)+1), 10000)):
        if (c%i == 0):
            return i
    return -1

def skontroluj(s):
    zoznam = []
    for i in range(2, 11):
        c = sustava(s, i)
        #print('c', c, 'i', i)
        delitel = najdiDelitela(c)
        if (delitel == -1):
            return None
        else:
            zoznam.append(delitel)
    return zoznam

def generuj(N):
    nah_cislo = '1'
    for i in range(1, N-1):
        nah_cislo +=str(randint(0, 1))
    return nah_cislo + '1'

T = int(input())
line = input().split(' ')
N = int(line[0])
J = int(line[1])
d = {}
while (len(d) < J):
    #print(len(d))
    cislo = generuj(N)
    delitele = skontroluj(cislo)
    if (delitele):
        d[cislo] = delitele

print("Case #1:")
for key, value in d.items():
    print(key, end = '')
    for x in value:
        print(' ' + str(x), end = '')
    print()


import json
import slugify
import csv

out = {
    "spo": {
        "id":"spo",
        "short_name":'SPO',
        'name': "Strana Práv Občanů",
        "friendly_name": 'spo',
        "vote":{}
    },
    "svobodni": {
        "id":"svobodni",
        "short_name":'Svobodní',
        'name': "Strana svobodných občanů",
        "friendly_name": 'svobodni',
        "vote":{}
    },
    "pirati": {
        "id":"pirati",
        "short_name":'Piráti',
        'name': "Česká pirátská strana",
        "friendly_name": 'pirati',
        "vote":{}
    },
    "zeleni": {
        "id":"zeleni",
        "short_name":'Zelení',
        'name': "Strana zelených",
        "friendly_name": 'zeleni',
        "vote":{}
    },
}
with open("answers2.csv") as infile:
    csvr = csv.reader(infile)
    for row in csvr:
        out['spo']['vote'][row[5]] = int(row[1])
        out['svobodni']['vote'][row[5]] = int(row[2])
        out['pirati']['vote'][row[5]] = int(row[3])
        out['zeleni']['vote'][row[5]] = int(row[4])

with open("answers2.json", "w") as outfile:
    json.dump(out,outfile)


M = [list(input()) for _ in range(10)]

def dfs(m, x, y):
    for dx, dy in ((-1, 0), (1, 0), (0, -1), (0, 1)):
        if 0 <= x + dx < 10 and 0 <= y + dy < 10:
            if m[y + dy][x + dx] == 'o':
                m[y + dy][x + dx] = 'x'
                dfs(m, x + dx, y + dy)

def island(m, x, y):
    dfs(m, x, y)
    return all(all(x == 'x' for x in l) for l in m)

def solve():
    for y in range(10):
        for x in range(10):
            if M[y][x] == 'x':
                M2 = [l[:] for l in M]
                if island(M2, x, y):
                    return True

print('YES' if solve() else 'NO')


from __future__ import annotations

import math
from typing import TYPE_CHECKING

import cairocffi
import xcffib.xproto

from libqtile.backend import base

if TYPE_CHECKING:
    from typing import Optional

    from libqtile.backend.base import Internal
    from libqtile.core.manager import Qtile

class Drawer(base.Drawer):
    """A helper class for drawing to Internal windows.

    The underlying surface here is an XCBSurface backed by a pixmap. We draw to the
    pixmap starting at offset 0, 0, and when the time comes to display to the window (on
    draw()), we copy the appropriate portion of the pixmap onto the window. In the event
    that our drawing area is resized, we invalidate the underlying surface and pixmap
    and recreate them when we need them again with the new geometry.
    """
    def __init__(self, qtile: Qtile, win: Internal, width: int, height: int):
        base.Drawer.__init__(self, qtile, win, width, height)
        self._xcb_surface = None
        self._pixmap = None
        self._gc = None

    def finalize(self):
        self._free_xcb_surface()
        self._free_pixmap()
        self._free_gc()
        base.Drawer.finalize(self)

    @property
    def width(self):
        return self._width

    @width.setter
    def width(self, width):
        if width > self._width:
            self._free_xcb_surface()
            self._free_pixmap()
        self._width = width

    @property
    def height(self):
        return self._height

    @height.setter
    def height(self, height):
        if height > self._height:
            self._free_xcb_surface()
            self._free_pixmap()
        self._height = height

    @property
    def pixmap(self):
        if self._pixmap is None:
            # draw here since the only use case of this function is in the
            # systray widget which expects a filled pixmap.
            self.draw()
        return self._pixmap

    def _create_gc(self):
        gc = self.qtile.core.conn.conn.generate_id()
        self.qtile.core.conn.conn.core.CreateGC(
            gc,
            self._win.wid,
            xcffib.xproto.GC.Foreground | xcffib.xproto.GC.Background,
            [
                self.qtile.core.conn.default_screen.black_pixel,
                self.qtile.core.conn.default_screen.white_pixel,
            ],
        )
        return gc

    def _free_gc(self):
        if self._gc is not None:
            self.qtile.core.conn.conn.core.FreeGC(self._gc)
            self._gc = None

    def _create_xcb_surface(self):
        surface = cairocffi.XCBSurface(
            self.qtile.core.conn.conn,
            self._pixmap,
            self._find_root_visual(),
            self.width,
            self.height,
        )
        return surface

    def _free_xcb_surface(self):
        if self._xcb_surface is not None:
            self._xcb_surface.finish()
            self._xcb_surface = None

    def _create_pixmap(self):
        pixmap = self.qtile.core.conn.conn.generate_id()
        self.qtile.core.conn.conn.core.CreatePixmap(
            self.qtile.core.conn.default_screen.root_depth,
            pixmap,
            self._win.wid,
            self.width,
            self.height,
        )
        return pixmap

    def _free_pixmap(self):
        if self._pixmap is not None:
            self.qtile.core.conn.conn.core.FreePixmap(self._pixmap)
            self._pixmap = None

    def paint_to(self, drawer):
        # If XCBSurface has been invalidated, we need to draw now to create it
        if self._xcb_surface is None:
            self.draw()
        drawer.ctx.set_source_surface(self._xcb_surface)
        drawer.ctx.paint()

    def _paint(self):
        # Only attempt to run RecordingSurface's operations if it actually has
        # some. self.surface.ink_extents() computes the bounds of the current
        # list of operations. If any of the bounds are not 0.0 then the surface
        # has operations and we should paint them.
        if any(not math.isclose(0.0, i) for i in self.surface.ink_extents()):
            # Paint RecordingSurface operations to the XCBSurface
            ctx = cairocffi.Context(self._xcb_surface)
            ctx.set_source_surface(self.surface, 0, 0)
            ctx.paint()

            # Clear RecordingSurface of operations
            self._reset_surface()

    def draw(
        self,
        offsetx: int = 0,
        offsety: int = 0,
        width: Optional[int] = None,
        height: Optional[int] = None,
    ):
        # If this is our first draw, create the gc
        if self._gc is None:
            self._gc = self._create_gc()

        # If the Drawer has been resized/invalidated we need to recreate these
        if self._xcb_surface is None:
            self._pixmap = self._create_pixmap()
            self._xcb_surface = self._create_xcb_surface()

        # paint stored operations(if any) to XCBSurface
        self._paint()

        # Finally, copy XCBSurface's underlying pixmap to the window.
        self.qtile.core.conn.conn.core.CopyArea(  # type: ignore
            self._pixmap,
            self._win.wid,
            self._gc,
            0, 0,  # srcx, srcy
            offsetx, offsety,  # dstx, dsty
            self.width if width is None else width,
            self.height if height is None else height
        )

    def _find_root_visual(self):
        for i in self.qtile.core.conn.default_screen.allowed_depths:
            for v in i.visuals:
                if v.visual_id == self.qtile.core.conn.default_screen.root_visual:
                    return v


CH = ['chr' + str(x) for x in range(1,23)]

ouFile = open('GEUVADIS_465LCLs_StopgainFrameshift_Sample2Mut', 'w')
D = {}
for ch in CH:
    inFile = open('GEUVADIS.%s.PH1PH2_465.IMPFRQFILT_BIALLELIC_PH.annotv2.genotypes.avi.annot.hg19_StopgainFrameshift.txt'%ch)
    for line in inFile:
        line = line.strip()
        fields = line.split('\t')
        for item in fields[13:]:
            sample = item.split(':')[0]
            D.setdefault(sample, [])
            D[sample].append([fields[0], fields[1], fields[6]])
    inFile.close()

for k in sorted(D.keys()):
    for item in D[k]:
        ouFile.write(k + '\t' + '\t'.join(item) + '\n')
ouFile.close()


#Tossaporn Tree Saengja
from graphics import *

inp = input("Enter the file (ex. 11.txt): ")

infile = open(inp, "r")
n = eval(infile.readline().strip())
# print(n)

win = GraphWin("Score Chart",300,n*50)
win.setCoords(0,0,6,n+1)

i = n
for c in infile:
	a = c.split()
	# print(a)
	t = Text(Point(1,i),a[0])
	t.draw(win)
	r = Rectangle(Point(2,i-0.15),Point(2+3.5*eval(a[1])/100,i+0.15))
	r.draw(win)
	i -= 1

win.getMouse()
infile.close()


import os
import random
import time

import praw
import requests

def main():
    reddit = login_reddit()
    url = 'https://www.reddit.com/r/TooAfraidToAsk/comments/gv2d5a/whats_wrong_with_saying_all_lives_matter/'
    generate_md_thread(url, reddit, 5)

def comment_loop(subname, reddit, list_of_words):
    print(reddit.auth.limits)
    for comment in reddit.subreddit(subname).stream.comments():
        if any(word in comment.body.lower() for word in list_of_words):
            print(comment.body)

def generate_random(reddit):
    submissions = reddit.subreddit('learnpython').hot(limit=50)
    submission_id = random.sample(list(submissions), 1)
    submission = reddit.submission(id=submission_id[0].id)
    print(submission.author)
    print(submission.selftext)

def picture(reddit):
    sub_list = ['funny', 'earthporn', 'pics']
    submission = reddit.subreddit(random.choice(sub_list)).random()
    if (submission.score < 100):
        print('Not enough updoots')
        print("This has a score of: {}".format(submission.score))
        print("From: {}".format(submission.subreddit))
    else:
        print("This has a score of: {}".format(submission.score))
        print("From: {}".format(submission.subreddit))

def generate_markdown(url, reddit):
    submission = reddit.submission(url=url)
    submitter = submission.author.name
    title = submission.author.name
    print(title)
    submission_body = submission.selftext
    filename = ("{}.txt".format(submission.id))
    submission.comments.replace_more(limit=None)
    f = open(filename, 'w+', encoding='utf-8')
    f.write(">{}\n\n".format(submission_body))
    f.write('Question|Answer\n:--|:--\n')
    for comment in submission.comments.list():
        for second_level_comment in comment.replies:
            if second_level_comment.author == submitter:
                question = comment.body.splitlines()
                answer = second_level_comment.body.splitlines()
                question = list(filter(None, question))
                answer = list(filter(None, answer))
                num_quest = len(question)
                num_answer= len(answer)
                if num_quest > num_answer:
                    for i in range(num_answer):
                        string_insert = ("[{0}]({1})|{2}\n".format(question[i], comment.permalink, answer[i]))
                        f.write(string_insert)
                    for j in range(num_answer, num_quest):
                        string_insert ="{0}| Q answered above \n".format(question[j])
                        f.write(string_insert)
                elif num_quest == num_answer:
                    for i in range(num_answer):
                        string_insert = ("[{0}]({1})|{2}\n".format(question[i], comment.permalink, answer[i]))
                        f.write(string_insert)
                elif num_quest < num_answer:
                    for i in range(num_quest):
                        string_insert = ("[{0}]({1})|{2}\n".format(question[i], comment.permalink, answer[i]))
                        f.write(string_insert)
                    for j in range(num_quest, num_answer):
                        string_insert = (" Answer to Q above |{0}\n".format(answer[j]))
                        f.write(string_insert)

    f.write("{}".format(url))
    f.close()

def generate_html_ama(url, reddit):
    reddit = reddit
    submission = reddit.submission(url=url)
    submitter = submission.author.name
    title = submission.title
    submission_body = submission.selftext
    filename = ("{}.txt".format(title))
    submission.comments.replace_more(limit=None, threshold=10)
    f = open(os.path.join('reddit', filename), 'w+', encoding='utf-8')
    f.write("{}\n".format(submission_body))
    f.write('
')
    for comment in submission.comments.list():
        for second_level_comment in comment.replies:
            if second_level_comment.author == submitter:
                question = comment
                string_insert = ("
Question: {0}\n

Answer: {1}\n
".format(question.body,second_level_comment.body))
                f.write(string_insert)
    f.write("
")
    f.close()

def generate_html_thread(url, reddit):
    reddit = reddit
    submission = reddit.submission(url=url)
    title = submission.id + "_html"
    submission_body = submission.selftext
    filename = ("{}.txt".format(title))
    submission.comments.replace_more(limit=None)
    source = ("https://www.reddit.com/{}".format(submission.permalink))
    f = open(filename, 'w', encoding='utf-8')
    f.write("{}\n".format(submission_body))
    f.write("{}\n".format(source))
    f.write('
')
    for comment in submission.comments.list():
        if comment.score > 10 and comment.depth == 0:
            string_insert = ("
/u/{0}\n

Answer: {1}\n
".format(comment.author,comment.body))
            f.write(string_insert)
    f.write("
")
    f.close()

def time_scrape_sub(subname, reddit, search_term):
    subreddit = reddit.subreddit(subname)
    f = open(subname, 'w+', encoding='utf-8')
    f.write("User|Title|Score|Date\n")
    f.write(":--|:--|:--|:--\n")
    for submission in subreddit.submissions(start= None, end= 1515326411):
        if search_term in submission.title.lower():
            date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(submission.created_utc))
            f.write("{0}| [{1}]({2})|{3}|{4}\n".format(submission.author, submission.title, submission.permalink, submission.score, date))
        else:
            continue
    f.close()

def time_scrape_com(subname, reddit, search_term):
    subreddit = reddit.subreddit(subname)
    print(reddit.auth.limits)
    f = open(subname, 'w+', encoding='utf-8')
    f.write("User|Comment|Score|Date\n")
    f.write(":--|:--|:--|:--\n")
    for submission in subreddit.submissions(start= 1509408000, end= 1514678400):
        submission.comments.replace_more(limit=None)
        for comment in submission.comments.list():
            if comment.author == "amos-counter-bot" or comment.author == "sg_amos_yee_counter" or comment.author == "rsg-retrivr":
                continue
            elif search_term in comment.body.lower():
                date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(submission.created_utc))
                answer = comment.body.splitlines()
                comment_text = ' '.join(answer)
                comment_date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(comment.created_utc))
                comment_text.strip('|')
                f.write("{0}| [{1}]({2})|{3}|{4}\n".format(comment.author, comment_text, comment.permalink, comment.score, comment_date))
            else:
                continue

def generate_md_thread(url, reddit, limit):
    reddit = reddit
    print(reddit.auth.limits)
    submission = reddit.submission(url=url)
    print(reddit.auth.limits)
    title = submission.title
    #submission_body = submission.selftext
    filename = ("{}.txt".format(submission.id))
    submission.comments.replace_more(limit = limit, threshold = 1)
    print(reddit.auth.limits)
    source = ("https://www.reddit.com{}".format(submission.permalink))
    f = open(filename, 'w', encoding='utf-8')
    f.write("[Source]({})\n\n".format(source))
    f.write('User| Answer| \n:--|:--\n')
    for comment in submission.comments.list():
        if comment.score > 500 and comment.depth == 0:
            answer = comment.body.splitlines()
            string_insert = ("[{0}](https://www.reddit.com{1})| {2} upvotes\n".format(comment.author, comment.permalink, comment.score))
            f.write(string_insert)
            for line in answer:
                if len(line)>0:
                    string_insert = (" | {}\n".format(line))
                    f.write(string_insert)
    f.close()
def generate_md_thread_yt(url, reddit, limit):
    reddit = reddit
    print(reddit.auth.limits)
    submission = reddit.submission(url=url)
    print(reddit.auth.limits)
    title = submission.title
    #submission_body = submission.selftext
    filename = ("{}.txt".format(submission.id))
    submission.comments.replace_more(limit = limit, threshold = 1)
    print(reddit.auth.limits)
    source = ("https://www.reddit.com{}".format(submission.permalink))
    f = open(filename, 'w', encoding='utf-8')
    f.write("[Source]({})\n\n".format(source))
    f.write('User| Answer| \n:--|:--\n')
    for comment in submission.comments.list():
        if comment.score > 500 and comment.depth == 0:
            answer = comment.body.splitlines()
            string_insert = ("[{0}](https://www.reddit.com{1})| {2} upvotes\n".format(comment.author, comment.permalink, comment.score))
            f.write(string_insert)
            for line in answer:
                if len(line)>0:
                    string_insert = (" | [{0}](https://www.youtube.com/results?search_query={1})\n".format(line, line.replace(" ", "%2") ))
                    f.write(string_insert)
    f.close()
def answers_scrape(subname , reddit):
    subreddit = reddit.subreddit(subname)
    i = 0
    print(reddit.auth.limits)
    filename = subname
    f = open(filename, 'w+', encoding='utf-8')
    f.write('Question | Answer| \n:--|:--\n')
    #submission_body = submission.selftext
    for submission in subreddit.top('week'):
        title = submission.title
        if(submission.comments.list()[0].author.name.lower() == "automoderator"):
            top_comment = submission.comments.list()[1]
        else:
            top_comment = (submission.comments.list())[0]
        print(top_comment)
        f.write("[{0}]({1}) | {2} - Score {3}\n".format(title, submission.permalink, top_comment.author, top_comment.score))
        for line in top_comment.body.splitlines():
            f.write(" | {}\n".format(line))
        i += 1
        if i > 5:
            break
        else:
            continue

def login_reddit():
    reddit = praw.Reddit('bot1', user_agent='<Python> Test bot')
    return reddit

def process_submissions(submission):
    download_image(submission.url, format(submission.title))
    print("done")

def download_image(image_url, local_file):
    response = requests.get(image_url)
    if response.status_code == 200:
        print('Downloading %s...' % (local_file))
    with open(local_file, 'wb') as fo:
        for chunk in response.iter_content(4096):
            fo.write(chunk)

def generate_html_ama_test(url, reddit):
    reddit = reddit
    submission = reddit.submission(url=url)
    submitter = submission.author.name
    title = submission.title
    submission_body = submission.selftext
    filename = ("{}.txt".format(title))
    submission.comments.replace_more(limit= None, threshold= 1)
    f = open(filename, 'w', encoding='utf-8')
    f.write("{}\n".format(submission_body))
    f.write('
')
    for comment in submission.comments.list():
        if comment.author == submitter and comment.depth== 1:
            question = comment.parent()
            string_insert = ("
Question: {0}\n

Answer: {1}\n
".format(question.body,comment.body))
            f.write(string_insert)
    f.write("
")
    f.close()

if __name__ == '__main__':
    main()


import nltk
import requests
import json

def do_pos_tagging(text):
    url = 'http://localhost:9000/?properties={"annotators":"pos,openie","outputFormat":"json"}'
    response = requests.post(url, data=text)
    return response.json()

def extract_named_entities(text):
    text = do_pos_tagging(text)
    return text['sentences'][0]['tokens']

def remove_dbspotlight_annotations(spotlight_result, pos_tagged_result):
    dup = pos_tagged_result.copy()
    for idx, p in enumerate(pos_tagged_result):
        if p['pos'] in ('NN', 'NNS'):
            for s in spotlight_result['annotated_text']['location_data']:
                if int(p['characterOffsetBegin']) == int(s['start']) or int(p['characterOffsetEnd']) == int(s['end']):
                    dup.remove(p)
        else:
            dup.remove(p)
    return dup


from glimmer.api import PluginParserBase, register_plugin, CONFIG, ParserExceptions, cyberspace

from urllib import parse

class Plugin(PluginParserBase):
    protocols = ["shodan"]

    def rule_check(self, module_path):
        return self.protocol_check(module_path)

    def get_data(self, module_path):
        parsed = parse.urlparse(module_path)
        config = CONFIG.base.configuration
        key = parsed.username if parsed.username else config.shodan.get(
            "key", "")
        if not key:
            raise ParserExceptions.CyberSpace.APIKeyError("Missing key")
        params = parse.parse_qs(parsed.query)
        self.client = cyberspace.ShodanClient(key)
        if "q" not in params:
            raise ParserExceptions.CyberSpace.Base("Missing query params: q")
        query_str = params["q"][0]
        max_page = int(
            params["max_page"][0]) if "max_page" in params else 1
        limit = params["limit"][0] if "limit" in params else 1
        fields = "ip,port,domains"
        data = []
        for ip, port, domains in self.client.query(query_str, max_page, limit, fields):
            domain = domains[0] if len(domains) > 0 else ""
            data.append(
                cyberspace.get_url_by_ip_port_domain(ip, port, domain))
        return data

    def destruct(self):
        if (hasattr(self, "client")):
            del self.client

register_plugin(Plugin)


from unittest import TestCase

from hestia.units import to_percentage

class ToPercentageTest(TestCase):
    """A test case for the `to_percentage`."""
    def test_works_as_expected_for_valid_values(self):
        test_data = [
            (0, '0%'),
            (0.25, '25%'),
            (-0.25, '-25%'),
            (12, '1200%'),
            (0.123, '12.3%'),
            (0.12345, '12.35%'),
            (0.12001, '12%'),
            (0.12101, '12.1%'),
            ('0', '0%'),
            ('0.25', '25%'),
        ]
        for value, expected in test_data:
            result = to_percentage(value)
            self.assertEqual(result, expected)

    def test_raises_value_error_for_invalid_types(self):
        with self.assertRaises(ValueError):
            to_percentage('foo')


#
# V-Ray For Blender
#
# http://chaosgroup.com
#
# Author: Andrei Izrantcev
# E-Mail: andrei.izrantcev@chaosgroup.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# All Rights Reserved. V-Ray(R) is a registered trademark of Chaos Software.
#

import bpy
import mathutils

from vb30.lib import ExportUtils

TYPE = 'BRDF'
ID   = 'BRDFSampled'
NAME = 'Sampled'
DESC = ""

PluginParams = (
    {
        'attr' : 'color',
        'desc' : "",
        'type' : 'COLOR',
        'default' : (1, 1, 1),
    },
    {
        'attr' : 'color_tex',
        'name' : "Color",
        'desc' : "",
        'type' : 'TEXTURE',
        'default' : (0.0, 0.0, 0.0),
    },
    {
        'attr' : 'color_tex_mult',
        'desc' : "",
        'type' : 'FLOAT',
        'default' : 1,
    },
    {
        'attr' : 'transparency',
        'desc' : "",
        'type' : 'COLOR',
        'default' : (0, 0, 0),
    },
    {
        'attr' : 'transparency_tex',
        'name' : "Transparency",
        'desc' : "",
        'type' : 'TEXTURE',
        'default' : (0.0, 0.0, 0.0),
    },
    {
        'attr' : 'transparency_tex_mult',
        'desc' : "",
        'type' : 'FLOAT',
        'default' : 1,
    },
    {
        'attr' : 'cutoff',
        'desc' : "",
        'type' : 'FLOAT',
        'default' : 0.01,
    },
    {
        'attr' : 'subdivs',
        'desc' : "",
        'type' : 'INT',
        'default' : 8,
    },
    {
        'attr' : 'back_side',
        'desc' : "",
        'type' : 'BOOL',
        'default' : False,
    },
    {
        'attr' : 'brdf_bitmap',
        'desc' : "",
        'type' : 'PLUGIN',
        'default' : "",
    },
    {
        'attr' : 'brdf_nsamples_d_theta',
        'desc' : "Number of d_theta samples",
        'type' : 'INT',
        'default' : 0,
    },
    {
        'attr' : 'brdf_nsamples_d_phi',
        'desc' : "Number of d_phi samples (1 means isotropic BRDF)",
        'type' : 'INT',
        'default' : 1,
    },
    {
        'attr' : 'brdf_importance_sampling_on',
        'name' : "Importance Sampling",
        'desc' : "true to use importance sampling for the reflections",
        'type' : 'BOOL',
        'default' : True,
    },
    {
        'attr' : 'brdf_importance_sampling_resolution',
        'name' : "Resolution",
        'desc' : "Resolution for the resampling of the BRDF used for importance sampling of reflections",
        'type' : 'INT',
        'default' : 32,
    },
    {
        'attr' : 'brdf_importance_sampling_view_terms',
        'name' : "View Terms",
        'desc' : "Number of terms to decompose the view-dependent portion of the resampling matrix",
        'type' : 'INT',
        'default' : 4,
    },
    {
        'attr' : 'brdf_importance_sampling_half_terms',
        'name' : "Half Terms",
        'desc' : "Number of terms to decompose the half-angle portion of the resampling matrix",
        'type' : 'INT',
        'default' : 2,
    },
)

PluginWidget = """
{ "widgets": [
    {   "layout" : "ROW",
        "attrs" : [
            { "name" : "subdivs" },
            { "name" : "cutoff" }
        ]
    },

    {   "layout" : "ROW",
        "align" : true,
        "attrs" : [
            { "name" : "brdf_nsamples_d_theta", "label" : "Θ Samples" },
            { "name" : "brdf_nsamples_d_phi", "label" : "Φ Samples" }
        ]
    },

    {   "layout" : "SPLIT",
        "splits" : [
            {   "layout" : "COLUMN",
                "align" : true,
                "attrs" : [
                    { "name" : "brdf_importance_sampling_on" },
                    { "name" : "brdf_importance_sampling_resolution", "active" : { "prop" : "brdf_importance_sampling_on" } }
                ]
            },
            {   "layout" : "COLUMN",
                "align" : true,
                "active" : { "prop" : "brdf_importance_sampling_on" },
                "attrs" : [
                    { "name" : "brdf_importance_sampling_view_terms" },
                    { "name" : "brdf_importance_sampling_half_terms" }
                ]
            }
        ]
    },

    {   "layout" : "ROW",
        "attrs" : [
            { "name" : "back_side" }
        ]
    }
]}
"""

def writeDatablock(bus, pluginModule, pluginName, propGroup, overrideParams):
    overrideParams.update({
        'color' : mathutils.Color((0.0, 0.0, 0.0)),
        'color_tex_mult' : 1.0,
        'transparency' : mathutils.Color((0.0, 0.0, 0.0)),
        'transparency_tex_mult' : 1.0,
    })

    return ExportUtils.WritePluginCustom(bus, pluginModule, pluginName, propGroup, overrideParams)


# coding=utf-8

import hashlib

class CryptHelper:

    def __init__(self):
        print("created")

    @staticmethod
    def getMD5Hash(source):
        m = hashlib.md5()
        m.update(source.encode("utf-8"))
        return m.hexdigest()

if __name__=="__main__":
    print("main")
    dst = "hello world, i say to him: love shanghai: by WEBMD"
    md5Hash = CryptHelper.getMD5Hash(dst)
    print (md5Hash)
    print("exit")