import numpy as np
np.random.seed(2017)

import time
import pandas as pd
import matplotlib.pyplot as plt

from sklearn import metrics
from sklearn.model_selection import train_test_split

#city_shifted = login_sub_df['city'].shift(1)
#city_changed = login_sub_df['city']!=city_shifted
#login_sub_df[city_changed]

#def get_one():
#    global trade_df, merged_login_df
#    risk_id = trade_df[trade_df.is_risk==1]['id'].unique()
#    risk_login_df = merged_login_df[merged_login_df.id.isin(risk_id)]
#    count_df = risk_login_df.groupby(risk_login_df['id']).count()
#    print('count_df is ', count_df)

#def get_city_ip_login_num():
#    global trade_df, merged_login_df
#    print('starting computing')
#    start_t = time.time()
#    trade_df_new = trade_df.copy()
#
#    ip_count_df = merged_login_df[['ip']].groupby(merged_login_df['id']).unique().count()
#    city_count_df = merged_login_df[['city']].groupby(merged_login_df['id']).unique().count()
#    end_t = time.time()
#    print('cost time is ', end_t-start_t)
#
#    trade_df_id_set = set(trade_df_new['id'])
#    merged_login_df_id_set = set(ip_count_df.index)
#    check = trade_df_id_set < merged_login_df_id_set
#    print('check is ', check)
#
#    trade_df_new['ip_num'] = ip_count_df['ip'][trade_df_new['id']].values
#    trade_df_new['city_num'] = city_count_df['city'][trade_df_new['id']].values
#    trade_df_new['city_num'] = city_count_df['city'][trade_df_new['id']].values
#    trade_df_new.to_csv('ip_city_num.csv')

#------------------------------------------------------------------------------------#
#得到本次交易最近20次登录切换城市的最小分钟数
def get_neareast_N_change_city_min_elaspe(N=20):
    global trade_df, merged_login_df
    trade_df_new = trade_df.copy()
    min_elapses = []
    count = 0
    print('starting computing')
    start_t = time.time()
    for index, row in trade_df_new.iterrows():
        print('get_neareast_N_change_city_min_elaspe compute ', count)
        count += 1
        trade_time = row['time']
        user_id = row['id']
        login_sub_df = merged_login_df[merged_login_df.id==user_id].sort_values(by='time')
        login_sub_df = login_sub_df[login_sub_df.time<=trade_time]
        login_sub_df = login_sub_df.iloc[-N:, :]
        elaspe_time = get_change_city_min_elapse(login_sub_df)
        min_elapses.append(elaspe_time)
    end_t = time.time()
    print('cost time is ', end_t-start_t)
    trade_df_new['change_city_min_elaspe'] = np.array(min_elapses)
    trade_df_new.to_csv('./features/change_city_min_elaspe.csv')

def get_change_city_min_elapse(login_df):
    min_elapse = 10.**8
    for i in range(len(login_df)-1):
        ip, city, time = login_df.iloc[i][['ip', 'city', 'time']]
        ip_next, city_next, time_next = login_df.iloc[i+1][['ip', 'city', 'time']]
        if city!=city_next:
            elapse = (time_next - time)
            elapse = np.timedelta64(elapse, 's')
            elapse = elapse.astype('float')/60.0  # 计算间隔的分钟数，转换为float类型
            min_elapse = min(min_elapse, elapse)
    return min_elapse

#------------------------------------------------------------------------------------#
#本次交易最后一次登录的device ip city所登录距离现在的时间（以分钟计算）如果从来没有登录过 则为10**8
def get_last_login_device_ip_city_time():
    global trade_df, merged_login_df
    trade_df_new = trade_df.copy()
#    trade_df_new = trade_df_new.iloc[:200]
    device_new, ip_new, city_new = [], [], []
    count = 0
    print('starting computing')
    start_t = time.time()
    for index, row in trade_df_new.iterrows():
        print('get_last_login_device_ip_city_time compute ', count)
        count += 1
        trade_time = row['time']
        user_id = row['id']
        login_sub_df = merged_login_df[merged_login_df.id==user_id].sort_values(by='time')
        login_sub_df = login_sub_df[login_sub_df.time<=trade_time]
        if len(login_sub_df)==0:  # 如果没有最后一次登录,则为10.**6
            device_new.append(10.**8)
            ip_new.append(10.**8)
            city_new.append(10.**8)
        else:
            device, ip, city = login_sub_df.iloc[-1][['device', 'ip', 'city']]
            last_login_t = login_sub_df['time'].values[-1]
            device_t, ip_t, city_t = compute_last_time(device, ip, city, last_login_t, login_sub_df.iloc[:-1])
            device_new.append(device_t)
            ip_new.append(ip_t)
            city_new.append(city_t)
    end_t = time.time()
    print('cost time is ', end_t-start_t)
    trade_df_new['device_new'] = np.array(device_new)
    trade_df_new['ip_new'] = np.array(ip_new)
    trade_df_new['city_new'] = np.array(city_new)
    trade_df_new.to_csv('./features/last_login_device_ip_city_time.csv')

def compute_last_time(device, ip, city, last_login_time, login_df):
    device_t, ip_t, city_t = None, None, None

    device_t_series = login_df['time'][login_df.device==device]
    ip_t_series = login_df['time'][login_df.ip==ip]
    city_t_series = login_df['time'][login_df.city==city]
    if len(device_t_series)>0:
        device_t = device_t_series.values[-1]
    if len(ip_t_series)>0:
        ip_t = ip_t_series.values[-1]
    if len(city_t_series)>0:
        city_t = city_t_series.values[-1]

    result_t = []
    for t in (device_t, ip_t, city_t):
        if t==None:
            time_t = 10.**8
        else:
            time_t = compute_elaspe_time(t, last_login_time)
        result_t.append(time_t)
    return result_t

#计算间隔时间，以分钟计算 会包含小数点部分 time1 time2 为 np.datetime64 类型
def compute_elaspe_time(time1, time2):
    if isinstance(time1, str):
        time1 = np.datetime64(time1)
    if isinstance(time2, str):
        time2 = np.datetime64(time2)
    if time1 > time2:
        time1, time2 = time2, time1
    elapse = (time2 - time1)
    elapse = np.timedelta64(elapse, 's')
    elapse = elapse.astype('float')/60.0  # 计算间隔的分钟数，转换为float类型
    return elapse

#------------------------------------------------------------------------------------#
#本次交易是否是当天第一次交易，是否是当天第一次登录
def get_whether_today_first_trade_login():
    global trade_df, merged_login_df
    trade_df_new = trade_df.copy()
    trade_df_new = trade_df_new.sort_values(by='time')
    first_trade_list, first_login_list = [], []
    id_newest_trade_time = {}
    count = 0
    print('starting computing')
    start_t = time.time()
    for index, row in trade_df_new.iterrows():
        print('get_whether_today_first_trade_login compute ', count)
        count += 1
        trade_time = row['time']
        user_id = row['id']
        if user_id not in id_newest_trade_time:
            first_trade_list.append(1)
        else:
            last_trade_time = id_newest_trade_time[user_id]
            if check_whether_same_day(trade_time, last_trade_time):
                first_trade_list.append(0)
            else:
                first_trade_list.append(1)
        id_newest_trade_time[user_id] = trade_time

        login_sub_df = merged_login_df[merged_login_df.id==user_id].sort_values(by='time')
        login_sub_df = login_sub_df[login_sub_df.time<=trade_time]
        if login_sub_df.shape[0]<2:
            first_login_list.append(1)
        else:
            login_t = login_sub_df['time'].values[-1]
            last_login_t = login_sub_df['time'].values[-2]
            if check_whether_same_day(login_t, last_login_t):
                first_login_list.append(0)
            else:
                first_login_list.append(1)
    end_t = time.time()
    print('cost time is ', end_t-start_t)
    trade_df_new['today_first_trade'] = np.array(first_trade_list)
    trade_df_new['today_first_login'] = np.array(first_login_list)
    trade_df_new.to_csv('./features/whether_today_first_trade_login.csv')

#time1 time2为 np.datetime64 类型
def check_whether_same_day(time1, time2):
    time1 = pd.DatetimeIndex([time1])
    time2 = pd.DatetimeIndex([time2])
    year1, month1, day1 = time1.year[0], time1.month[0], time1.day[0]
    year2, month2, day2 = time2.year[0], time2.month[0], time2.day[0]
    same_day = (year1==year2 and month1==month2 and day1==day2)
    return 1 if same_day else 0

#------------------------------------------------------------------------------------#
#本次交易距离上一次交易时间，本次登录距离上一次登录时间，本次交易与登录之间的时间差，以分钟计数
def get_last_login_trade_time_elapse():
    global trade_df, merged_login_df
    trade_df_new = trade_df.copy()
    trade_df_new = trade_df_new.sort_values(by='time')
#    trade_df_new = trade_df_new.iloc[:200]
    trade_elapse_list, login_elapse_list, trade_login_elapse_list = [], [], []
    id_newest_trade_time = {}
    count = 0
    print('starting computing')
    start_t = time.time()
    for index, row in trade_df_new.iterrows():
        print('get_last_login_trade_time_elapse compute ', count)
        count += 1
        trade_time = row['time']
        user_id = row['id']
        if user_id not in id_newest_trade_time:
            trade_elapse_list.append(10.**8)
        else:
            last_trade_time = id_newest_trade_time[user_id]
            elapse_t = compute_elaspe_time(trade_time, last_trade_time)
            trade_elapse_list.append(elapse_t)
        id_newest_trade_time[user_id] = trade_time

        login_sub_df = merged_login_df[merged_login_df.id==user_id].sort_values(by='time')
        login_sub_df = login_sub_df[login_sub_df.time<=trade_time]
        if login_sub_df.shape[0]>=1:
            login_t = login_sub_df['time'].values[-1]
            elapse_t = compute_elaspe_time(login_t, trade_time)
            trade_login_elapse_list.append(elapse_t)
            if login_sub_df.shape[0]>1:
                last_login_t = login_sub_df['time'].values[-2]
                elapse_t = compute_elaspe_time(login_t, last_login_t)
                login_elapse_list.append(elapse_t)
            else:
                login_elapse_list.append(10.**8)
        else:
            login_elapse_list.append(10.**8)
            trade_login_elapse_list.append(10.**8)
    end_t = time.time()
    print('cost time is ', end_t-start_t)
    trade_df_new['last_trade_elapse'] = np.array(trade_elapse_list)
    trade_df_new['last_login_elapse'] = np.array(login_elapse_list)
    trade_df_new['trade_login_elapse'] = np.array(trade_login_elapse_list)
    trade_df_new.to_csv('./features/last_login_trade_time_elapse.csv')

#------------------------------------------------------------------------------------#
#得本次交易登录的device ip city是否与上一次登录相同，如果没有上次登录 默认为0
def get_whether_this_trade_same_device_ip_city():
    global trade_df, merged_login_df
    trade_df_new = trade_df.copy()
    trade_df_new = trade_df_new.sort_values(by='time')
#    trade_df_new = trade_df_new.iloc[400:800]
    same_device_list, same_ip_list, same_city_list = [], [], []
    count = 0
    print('starting computing')
    start_t = time.time()
    for index, row in trade_df_new.iterrows():
        print('get_whether_this_trade_same_device_ip_city compute ', count)
        count += 1
        trade_time = row['time']
        user_id = row['id']
        login_sub_df = merged_login_df[merged_login_df.id==user_id].sort_values(by='time')
        login_sub_df = login_sub_df[login_sub_df.time<=trade_time]
        if login_sub_df.shape[0]>=2:
            device1, device2 = list(login_sub_df['device'].values[-2:])
            ip1, ip2 = list(login_sub_df['ip'].values[-2:])
            city1, city2 = list(login_sub_df['city'].values[-2:])
            same_device_list.append(1 if device1==device2 else 0)
            same_ip_list.append(1 if device1==device2 else 0)
            same_city_list.append(1 if device1==device2 else 0)
        else:
            same_device_list.append(0)
            same_ip_list.append(0)
            same_city_list.append(0)
    end_t = time.time()
    print('cost time is ', end_t-start_t)
    trade_df_new['same_device'] = np.array(same_device_list)
    trade_df_new['same_ip'] = np.array(same_ip_list)
    trade_df_new['same_city'] = np.array(same_city_list)
    trade_df_new.to_csv('./features/whether_this_trade_new_device_ip_city.csv')

#------------------------------------------------------------------------------------#
#得到该id在这之前的总交易次数、总登录次数、登录过的city数、ip数、device数
def get_before_trade_login_city_ip_device_num():
    global trade_df, merged_login_df
    print('starting computing')
    start_t = time.time()
    trade_df_new = trade_df.copy()
    ip_count_df = merged_login_df[['ip']].groupby(merged_login_df['id']).unique().count()
    city_count_df = merged_login_df[['city']].groupby(merged_login_df['id']).unique().count()
    end_t = time.time()
    print('cost time is ', end_t-start_t)

    trade_df_id_set = set(trade_df_new['id'])
    merged_login_df_id_set = set(ip_count_df.index)
    check = trade_df_id_set < merged_login_df_id_set
    print('check is ', check)

    trade_df_new['last_half_year_ip_num'] = ip_count_df['ip'][trade_df_new['id']].values
    trade_df_new['last_half_year_city_num'] = city_count_df['city'][trade_df_new['id']].values
    trade_df_new.to_csv('ip_city_num.csv')

if __name__=='__main__':
    start_t = time.time()
    trade_df = pd.read_csv('./data/Risk_Detection_Qualification/t_trade.csv',
                           index_col='rowkey', dtype={'id': np.str})
    trade_test_df = pd.read_csv('./data/Risk_Detection_Qualification/t_trade_test.csv',
                                index_col='rowkey', dtype={'id': np.str})
    dateparse = lambda x: pd.datetime.strptime(x, '%Y-%m-%d %H:%M:%S')
    login_df = pd.read_csv('./data/Risk_Detection_Qualification/t_login.csv',
                           index_col='log_id',
                           dtype={'id': np.str, 'timestamp': np.str},
                           parse_dates=['time'], date_parser=dateparse)
    login_test_df = pd.read_csv('./data/Risk_Detection_Qualification/t_login_test.csv',
                                index_col='log_id',
                                dtype={'id': np.str, 'timestamp': np.str},
                                parse_dates=['time'], date_parser=dateparse)
    merged_login_df = login_df.append(login_test_df)
    end_t = time.time()
    print('load cost time: ', end_t-start_t)

#    get_neareast_N_change_city_min_elaspe()
#    get_city_ip_num()
#    get_last_login_device_ip_city_time()
#    get_whether_today_first_trade_login()
#    get_last_login_trade_time_elapse()
    get_whether_this_trade_same_device_ip_city()


import re

# lst = re.findall(r"\d+", "今天吃了2次鸡, alex吃了0次鸡, 他是砸在")
# print(lst)

# it = re.finditer(r"\d+", "今天吃了2次鸡, alex吃了0次鸡, 他是砸在")
# for item in it:
#     print(item.group())

# # 查找, 查找导第一个匹配项就返回了
# obj = re.search(r"\w+", "@@@www.baidu.com")
# print(obj.group())
#
# # # 匹配, 查找导第一个匹配项就返回了
# # 从头匹配
# obj = re.match(r"\w+", "@@@www.baidu.com")
# print(obj)

# # 把正则加载到内存
# obj = re.compile(r"\w+")
# obj.match("www.baidu.com")

# # 重点
# #
(?P<name>.*?)

# #
倚天屠龙

# s = "
倚天屠龙

少林武当
fasdfasdfsda
"
#
# it = re.finditer(r"
(?P<hahaha>.*?)
", s)
# for item in it:
#     print(item.group("hahaha"))

# 爬虫应用

# 获取到网络源代码需要的包
from urllib.request import urlopen

content = urlopen("https://www.dytt8.net/html/gndy/dyzz/20190419/58500.html").read().decode("gbk")
# print(content)

obj = re.compile(r'
.*?◎片　　名(?P<name>.*?)
.*?◎主　　演(?P<arts>.*?)

.*?<a href="(?P<url>.*?)">',re.S)

it = obj.finditer(content)
for item in it:
    print(item.group("name"))
    print(item.group("arts"))
    print(item.group("url"))

'''
# 作业
# 爬去dytt 2019新片精品  -> 把所有电影的名字, 主演, 下载链接. 放在一个json文件里

[
    {"main_people": ["井柏然", "张一山"], "name": xxxx, "url":},
    {},
    {}

]

要求: 代码合理性
'''


###############################################################################
#                                                                             #
# Copyright (C) 2004-2014 Edward d'Auvergne                                   #
#                                                                             #
# This file is part of the program relax (http://www.nmr-relax.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 3 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/>.       #
#                                                                             #
###############################################################################

# Module docstring.
"""Module for all of the reduced spectral density mapping specific user functions."""

# relax module imports.
from lib.errors import RelaxError, RelaxFuncSetupError
from pipe_control.pipes import check_pipe
import specific_analyses

def set_frq(frq=None):
    """Function for selecting which relaxation data to use in the J(w) mapping."""

    # Test if the current pipe exists.
    check_pipe()

    # Test if the pipe type is set to 'jw'.
    function_type = cdp.pipe_type
    if function_type != 'jw':
        raise RelaxFuncSetupError(specific_analyses.setup.get_string(function_type))

    # Test if the frequency has been set.
    if hasattr(cdp, 'jw_frq'):
        raise RelaxError("The frequency has already been set.")

    # Create the data structure if it doesn't exist.
    if not hasattr(cdp, 'jw_frq'):
        cdp.jw_frq = {}

    # Set the frequency.
    cdp.jw_frq = frq


# four integers: 𝑊, 𝑆, 𝐶, 𝐾
def isPossible(w,s,c,k):

    # no boat storage
    if k == 0:
        return False

    # No wolves, sheep or cabbage
    if not (s or w or c):
        return True

    # No sheep
    if not s:
        return True

    # More sheep than boat storage
    if k < s:

        # If you can carry wolves and cabbages,
        # bring sheep over 1 at a time
        if k == w+c-1:
            return True

        return False

    # sheep fit storage exactly
    if s == k:

        # more woves or cabbage, boat is impossible
        if w > k or c > k:
            return False

        return True

    # if less sheep than boat storage, carry everything at a time
    if s < k:
        return True

    return False

# s, w, c, k = input().split()
# s = int(s)
# w = int(w)
# c = int(c)
# k = int(k)

# if isPossible(s, w, c, k):
#     print('YES')
# else:
#     print('NO')

# Testing Data
# ----------------------------------------
S_set = [0,0,0,1,0,1,1, 10, 5, 10, 10]
W_set = [0,0,0,0,1,0,1, 10, 10, 5, 10]
C_set = [0,0,0,0,1,0,1, 10, 10, 10, 5]
K_set = [0,0,1,0,1,1,1, 5, 5, 5, 5]

answers= [0,0,1,0,1,1,1, 0, 0, 0, 0]

for i in range(len(S_set)):
    is_possible = isPossible(S_set[i], W_set[i], C_set[i], K_set[i])
    if(not(answers[i] == is_possible)):
        print('error at '+str(i) +': '+str(is_possible))

print('all works')


import cv2
import os
from shutil import copyfile
import json
from tqdm import tqdm

ROOT = './data/vn_food/raw'
out_folder = './data/vn_food/images'
ann_path = './data/vn_food/annotations'
my_dict = {
    'images':[],
    'annotations': [],
    'categories':[]
}

class_names = [
"Banh_mi",
"Banh_trang_tron",
"Banh_xeo",
"Bun_bo_Hue",
"Bun_dau",
"Com_tam",
"Goi_cuon",
"Pho",
"Hu_tieu",
"Xoi"]

image_id = 0
obj_id = 0

for class_id, class_name in tqdm(enumerate(class_names)):

    cat_dict = {
        'id': class_id+1,
        'name': class_name,
        'supercategory': "food"
    }

    my_dict['categories'].append(cat_dict)

    image_folder = os.path.join(ROOT, class_name)
    file_names = os.listdir(image_folder)
    image_names = [i for i in file_names if not i.endswith('txt')]
    for image_name in image_names:
        image_path = os.path.join(image_folder,image_name)
        img = cv2.imread(image_path)
        height, width, _ = img.shape

        new_image_path = os.path.join(out_folder, image_name)
        copyfile(image_path, new_image_path)
        img_dict = {
            "id": image_id,
            "file_name": image_name,
            "height": height,
            "width": width
        }

        my_dict['images'].append(img_dict)

        _, ext = os.path.splitext(image_name)
        ext_len = str.__len__(ext)
        text_name = image_path[:-ext_len]+ '.txt'
        with open(text_name, 'r') as f:
            data = f.read()
            lines = data.splitlines()
        for line in lines:
            item = line.split(' ')
            box = [float(i) for i in item[1:]]
            box[0] -= box[2]/2
            box[1] -= box[3]/2

            box[0] = int(box[0] * width)
            box[1] = int(box[1] * height)
            box[2] = int(box[2] * width)
            box[3] = int(box[3] * height)

            box_dict ={
                "id": obj_id,
                "bbox": box,
                "area": box[2]*box[3],
                "category_id": class_id+1,
                "image_id": image_id,
                "iscrowd": 0
            }

            my_dict['annotations'].append(box_dict)
            obj_id+=1
        image_id+=1

with open(f'{ann_path}/annotations.json', 'w+') as f:
    json.dump(my_dict, f)


# importing packages

import os
import csv

# loading in csv file
electionData = os.path.join("election_data.csv")
# list for the names of electees
electees = []
# counter for how many votes for each electee based on index in electees list
voteCounter = []
# list that calculates the percentages of votes for each candidadte
votePercentages = []
# counter that registers the total amount of votes
voteTotal = 0

with open(electionData, newline = "") as csvfile:
        csvread = csv.reader(csvfile, delimiter = ",")
        header = next(csvread)
        for row in csvread:
        # adds +1 to vote total
            voteTotal += 1
        # if clause that adds the electee to electee list if they havent appeared yet
            if row[2] not in electees:
                electees.append(row[2])
                index = electees.index(row[2])
                voteCounter.append(1)
        # else clause that adds a vote to electee if already in list
            else:
                index = electees.index(row[2])
                voteCounter[index] += 1

    # for statement looping through each of the votenumbers in voteCounter list that calculates the percentage for each of the candidates
        for voteNumber in voteCounter:
            percentage = (voteNumber / voteTotal) * 100
        # rounds percentage so that it's not too big of a number
            percentage = round(percentage)
        #formats percentage
            percentage = "%.3f%%" % percentage
        # appends percentages to votePercentages list
            votePercentages.append(percentage)

    # find index of highest vote count in vote counter list
        highestVoteIndex = voteCounter.index(max(voteCounter))
    # stores the winner in winner variable
        winner = electees[highestVoteIndex]

#statements to print to the console
        print("Election Results")
        print("------------------------------")
        print(f"Total Votes: {str(voteTotal)}")
        print("------------------------------")
        for i in range(len(electees)):
            print(f"{electees[i]}: {str(votePercentages[i]) } ({str(voteCounter[i])})")
        print("------------------------------")
        print(f"Winner: {winner}")
        print("------------------------------")

#will output the program to a text file
with open("output.txt", "w") as x:
   print("Election Results \n", file = x)
   print("------------------------------\n", file=x)
   print(str(f'Total Votes: {str(voteTotal)}\n'), file = x)
   print("------------------------------\n", file = x)
   for i in range(len(electees)):
        print(str(f'{electees[i]}: {str(votePercentages[i])} ({str(voteCounter[i])})'), file = x)
   print("------------------------------\n", file = x)
   print(str(f'Winner: {winner}\n'), file = x)
   print("------------------------------\n", file = x)


import typing
import inspect
import re

from copy import copy
from vbml import Pattern, Patcher
from html import unescape

from vkbottle.types.message import Message as BotMessage
from vkbottle.types.user_longpoll import Message as UserMessage
from vkbottle.types import user_longpoll
from vkbottle.utils import flatten, json

Message = typing.Union[BotMessage, UserMessage]

class Copy:
    def copy(self):
        return copy(self)

class RuleExecute:
    def __init__(self):
        self.args = []
        self.kwargs = {}

    def update(self, args: list, kwargs: dict):
        self.args.extend(args)
        self.kwargs.update(kwargs)

    def __call__(self):
        return self.args, self.kwargs

    def __repr__(self):
        return f"<{self.args}{self.kwargs}>"

class AbstractRule(Copy):
    call: typing.Callable
    getfullargspec: inspect.FullArgSpec

    def __init_subclass__(cls, **kwargs):
        cls.context: RuleExecute = RuleExecute()
        cls.watch_context = None

    def create(self, func: typing.Callable, data: dict = None):
        self.call: typing.Callable = func
        if data is not None:
            setattr(self, "data", {**getattr(self, "data", {}), **data})
        self.getfullargspec = inspect.getfullargspec(self.call)

    def resolve(self, value):
        if self.watch_context:
            context = self.watch_context.get(value)

            if isinstance(context, (list, tuple)):
                self.context.args.extend(context)
            elif isinstance(context, dict):
                self.context.kwargs.update(context)
            else:
                self.context.args.append(context)

    async def __call__(self, event):
        self.__init_subclass__()
        return await self.check(event)

    async def check(self, event) -> bool:
        ...

    def __repr__(self):
        return f"<Rule {self.__class__.__qualname__} context={self.context}>"

class Any(AbstractRule):
    async def check(self, event) -> bool:
        return True

class AbstractMessageRule(AbstractRule):
    async def check(self, message: Message) -> bool:
        ...

class UnionMixin(AbstractMessageRule):
    def __init__(self, mixin=None):
        mixin = mixin if mixin is not None else []
        if not isinstance(mixin, list):
            mixin = [mixin]
        self.data = {"mixin": mixin}

class StickerRule(UnionMixin):
    async def check(self, message: Message) -> bool:
        if message.attachments and message.attachments[0].sticker:
            if not self.data["mixin"]:
                return True
            if message.attachments[0].sticker.sticker_id in self.data["mixin"]:
                return True

class FromMe(AbstractMessageRule):
    def __init__(self, from_me: bool = True):
        self.from_me = from_me

    async def check(self, message: Message) -> bool:
        if (message.from_id == await message.api.user_id) is self.from_me:
            return True

class MessageRule(AbstractMessageRule):
    def __init__(self, message: typing.Union[str, typing.List[str]]):
        if isinstance(message, str):
            message = [message]
        self.data = {"message": message}

    async def check(self, message: Message) -> bool:
        if message.text in self.data["message"]:
            return True

class LevenshteinDisRule(AbstractMessageRule):
    def __init__(self, mixin: typing.Union[str, typing.List[str]], lev_d: float = 1):
        if isinstance(mixin, dict):
            self.watch_context = mixin
            mixin = list(mixin.keys())
        elif not isinstance(mixin, list):
            mixin = [mixin]
        self.data = {"samples": mixin}
        self.lev = lev_d

    @staticmethod
    def distance(a, b):
        n, m = len(a), len(b)
        if n > m:
            a, b = b, a
            n, m = m, n

        current_row = range(n + 1)
        for i in range(1, m + 1):
            previous_row, current_row = current_row, [i] + [0] * n
            for j in range(1, n + 1):
                add, delete, change = (
                    previous_row[j] + 1,
                    current_row[j - 1] + 1,
                    previous_row[j - 1],
                )
                if a[j - 1] != b[i - 1]:
                    change += 1
                current_row[j] = min(add, delete, change)

        return current_row[n]

    async def check(self, message: Message) -> bool:
        for sample in self.data["samples"]:
            if self.distance(message.text, sample) <= self.lev:
                self.resolve(sample)
                return True

class CommandRule(AbstractMessageRule):
    def __init__(
        self,
        message: typing.Union[str, typing.List[str]],
        prefixes: typing.Tuple[str, ...] = ("/", "!"),
    ):
        if isinstance(message, dict):
            self.watch_context = message
            message = list(message.keys())
        elif not isinstance(message, list):
            message = [message]
        self.data = {"message": message, "prefixes": prefixes}

    async def check(self, message: Message) -> bool:
        if not message.text or len(message.text) < 2:
            return
        prefix, text = message.text[0], message.text[1:]
        return prefix in self.data["prefixes"] and text in self.data["message"]

class EventRule(AbstractRule):
    def __init__(self, event: typing.Union[str, typing.List[str]]):
        if isinstance(event, str):
            event = [event]
        self.data = {"event": event}

    async def check(self, event) -> bool:
        if "data" not in self.data:
            self.data["data"] = self.getfullargspec.annotations.get(
                self.getfullargspec.args[0], dict
            )
        for e in self.data["event"]:
            if e == event:
                return True

class UserLongPollEventRule(AbstractRule):
    def __init__(self, event: typing.Union[int, typing.List[int]], *rules):
        if isinstance(event, int):
            event = [event]
        self.data = {"event": event, "rules": rules}

    async def check(self, update):
        for e in self.data["event"]:
            if e == update[0]:
                if not self.data["rules"]:
                    return True
                return tuple(self.data["rules"])

class UserMessageRule(UnionMixin):
    async def check(self, message: user_longpoll.Message):
        if message.text in self.data["mixin"]:
            return True

class ChatMessage(AbstractMessageRule):
    async def check(self, message: Message) -> bool:
        if message.peer_id > 2e9:
            return True

class PrivateMessage(AbstractMessageRule):
    async def check(self, message: Message) -> bool:
        if message.peer_id < 2e9:
            return True

class VBML(AbstractMessageRule):
    def __init__(
        self,
        pattern: typing.Union[
            str,
            Pattern,
            typing.List[typing.Union[str, Pattern]],
            typing.Dict[typing.Union[str, Pattern], typing.Union[list, dict]],
        ],
        lower: bool = None,
    ):
        if isinstance(pattern, dict):
            self.watch_context = pattern
            pattern = list(pattern)
        self._patcher = Patcher.get_current()
        patterns: typing.List[Pattern] = []
        if isinstance(pattern, Pattern):
            patterns = [pattern]
        elif isinstance(pattern, list):
            for p in pattern:
                if isinstance(p, str):
                    patterns.append(
                        self._patcher.pattern(p, flags=re.IGNORECASE if lower else None)
                    )
                else:
                    patterns.append(p)
        elif isinstance(pattern, str):
            patterns = [
                self._patcher.pattern(pattern, flags=re.IGNORECASE if lower else None)
            ]

        self.data = {"pattern": patterns}

class VBMLRule(VBML):
    async def check(self, message: Message) -> bool:
        patterns: typing.List[Pattern] = self.data["pattern"]
        message = message.text.replace("<br>", "\n")
        message = unescape(message)

        for pattern in patterns:
            if self._patcher.check(message, pattern) is not None:
                self.context.kwargs = pattern.dict()
                self.resolve(pattern)
                return True

class AttachmentRule(UnionMixin):
    async def check(self, message: Message) -> bool:
        attachments = flatten(
            [
                list(attachment.dict(skip_defaults=True).keys())
                for attachment in message.attachments
            ]
        )
        if attachments and not self.data["mixin"]:
            # ANY ATTACHMENTS
            return True
        for attachment_type in self.data.get("mixin", []):
            if attachment_type in attachments:
                return True

class ChatActionRule(AbstractMessageRule):
    def __init__(
        self, chat_action: typing.Union[str, typing.List[str]], rules: dict = None
    ):
        if isinstance(chat_action, str):
            chat_action = [chat_action]
        self.data = dict()
        self.data["chat_action"] = chat_action
        self.data["rules"] = rules or {}

    async def check(self, message: Message) -> bool:
        if message.action:
            if message.action.type in self.data["chat_action"]:
                if {
                    **message.action.dict(skip_defaults=True),
                    **self.data["rules"],
                } == message.action.dict(skip_defaults=True):
                    return True

class PayloadRule(AbstractMessageRule):
    def __init__(self, payload: dict, mode=1):
        self.data = dict()
        self.data["payload"] = payload
        self.data["mode"] = mode

    @staticmethod
    def dispatch(payload: str) -> dict:
        try:
            return json.loads(payload)
        except json.decoder.JSONDecodeError:
            return dict()

    async def check(self, message: Message) -> bool:
        if message.payload is not None:
            payload = self.dispatch(message.payload)
            if self.data["mode"] == 1:
                # EQUALITY
                if payload == self.data["payload"]:
                    return True
            elif self.data["mode"] == 2:
                # CONTAIN
                if {**payload, **self.data["payload"]} == payload:
                    return True


from typedecorator._type_error import _type_error
from typedecorator.checker import _check_constraint_validity, _verify_type_constraint, _constraint_to_string
from typedecorator import conf

def returns(return_type):
    """
    Assert that function returns value of specific type

    Example:

        @returns(int)
        def get_random_number():
            return 4  # http://xckd.com/221/

    See module documentation for more information about type signatures.

    """
    _check_constraint_validity(return_type)

    def deco(fn):
        if not conf._decorator_enabled:
            return fn

        if not hasattr(fn, '__def__site__'):
            if hasattr(fn, '__code__'):
                fc = fn.__code__
            else:
                fc = fn.func_code
            fn.__def_site__ = (fc.co_filename, fc.co_firstlineno, fn.__name__,
                               '')

        def wrapper(*args, **kwargs):
            retval = fn(*args, **kwargs)
            if conf._enabled:
                if retval is None and return_type is not type(None):
                    _type_error("non-void function didn't return a value",
                                stack=fn.__def_site__)
                elif retval is not None and return_type is type(None):
                    _type_error("void function returned a value",
                                stack=fn.__def_site__)
                elif not _verify_type_constraint(retval, return_type):
                    _type_error("function returned value %s not matching "
                                "signature %s" % (repr(retval),
                                                  _constraint_to_string(return_type)),
                                stack=fn.__def_site__)
            return retval

        wrapper.__name__ = fn.__name__
        wrapper.__doc__ = fn.__doc__
        wrapper.__return_type__ = return_type
        return wrapper

    return deco

void = returns(type(None))
void.__doc__ = """Annotate function returning nothing"""


#coding='utf-8'
import sys

from PyQt5.QtCore import QPoint
from PyQt5.QtWidgets import QApplication, QMainWindow, QPlainTextEdit,QHBoxLayout,QWidget
import sys
import os
from PyQt5 import QtGui, QtCore,QtWidgets
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QTextCursor,QTextLine,QFont

from qtUi.NewMainWindows import Ui_MainWindow
from textEdit import syntax

#-----------主弹窗-----------------------#

class MyMainWindow(QMainWindow):
    switch_window = QtCore.pyqtSignal()
    def __init__(self):
        super(MyMainWindow, self).__init__()

        layout = QHBoxLayout()
        self.centralWidget = QWidget()
        self.centralWidget.setLayout(layout)
        self.setCentralWidget(self.centralWidget)
        self.line = QTextLine()
        self.highlight = syntax.PythonHighlighter(self.textedit.document())
        self.textedit.setFont(QFont("Microsoft YaHei", 17))
        self.textedit.textChanged.connect(self.save_text)
        layout.addWidget(self.textedit)

    def save_text(self):
        list = ["baidu","test","screenshotaaa","search"]
        text = self.textedit.toPlainText()
        lines_last_words = text.split("\n")[-1].split(" ")[-1]
        self.picwindow = ImgListView(lines_last_words)
        self.picwindow.resize(130,130)
        self.picwindow.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.Tool)
        if lines_last_words in list:
            self.picwindow.show()
            popup_pos = self.textedit.cursorRect()
            x = popup_pos.x()
            y = popup_pos.height()
            pos2 = self.mapToGlobal(QPoint(1.2*x,2*y))
            self.picwindow.move(pos2)
            self.picwindow.setStyleSheet("border :1px solid black;")
            self.picwindow.raise_()

        else:
            self.picwindow.hide()
        with open('mytextfile.txt', 'w') as f:
            f.write(text)

#----------小弹窗----------#

class MyListModel(QtCore.QAbstractListModel):
    def __init__(self, datain, parent=None, *args):
        """ datain: a list where each item is a row
        """
        QtCore.QAbstractListModel.__init__(self, parent, *args)
        self.listdata = datain
    def rowCount(self, parent=QtCore.QModelIndex()):
        return len(self.listdata)

    def data(self, index, role):
        if index.isValid() and role == QtCore.Qt.DecorationRole:
            return QtGui.QIcon(QtGui.QPixmap(self.listdata[index.row()]))
        if index.isValid() and role == QtCore.Qt.DisplayRole:
            # print(QtCore.QVariant(self.ListItemData[index.row()]['name']))
            return QtCore.QVariant(os.path.splitext(os.path.split(self.listdata[index.row()])[-1])[0])
        else:
            return QtCore.QVariant()

    def getItem(self, index):
        if index > -1 and index < len(self.ListItemData):
            return self.ListItemData[index]

class ImgListView(QtWidgets.QListView):
    """docstring for MyListView"""
    def __init__(self,imagename,parent=None):
        super(ImgListView, self).__init__(parent)
        self.Listview = QListView()
        self.setViewMode(QtWidgets.QListView.IconMode)
        self.setIconSize(QtCore.QSize(110, 110))
        self.setGridSize(QtCore.QSize(120, 120))

        crntDir = "/Users/yupeng55/Documents/PycharmProjects/qtUi/icon/"
        list_data = []
        target_file = crntDir+imagename+".jpg"
        list_data.append(target_file)
        self.List_data = list_data
        lm = MyListModel(list_data)
        self.setModel(lm)
        self.show()
        self.clicked.connect(self.onclicked)

    def onclicked(self,item):
        image_selected_path = self.List_data[item.row()]
        self.close()


import scrapy
import re

class RefModeleSpider(scrapy.Spider):
    name = 'modeles'

    def start_requests(self):

        marque = getattr(self, 'marque', None)
        marque_url = getattr(self, 'marque_url', None)

        if marque is not None:
            url = 'https://www.caradisiac.com/auto--%s/modeles' % marque

        if marque_url is not None:
            url = 'https://www.caradisiac.com%smodeles' % marque_url
            marque = setattr(self,'marque',re.search('(?<=\/auto--)(.*)(?=\/)', marque_url).group(1))

        yield scrapy.Request(url, self.parse)

    def parse(self, response):
        for ref in  response.css('div.visuModeleGalerie.col-xs-2.col-sm-4.col-md-5 a'):
            yield {
                'src':  ref.css('img').xpath('@src').get(),
                'alt': ref.css('img').xpath('@alt').get(),
                'href': 'https://www.caradisiac.com' + ref.xpath('@href').get(),
                'marque' : self.marque,
            }

class RefMarqueSpider(scrapy.Spider):
    name = 'marques'

    def start_requests(self):
        url = 'https://www.caradisiac.com/constructeurs--automobiles/'

        yield scrapy.Request(url, self.parse)

    def parse(self, response):
        for ref in  response.css('a.hidden-xs'):
            yield {
                'src':  ref.css('img').xpath('@src').get(),
                'href': ref.xpath('@href').get(),
                'marque' : ref.xpath('@title').get(),
            }


def tokenize(lines, mode="word"):
    """
    From list of strings to list of lists of tokens;
    :param lines: list of strings;
    :param mode: "words" or "char", former tokenizes to words, latter to chars;
    :return: list of lists of tokens;
    """
    if mode == "word":
        return [line.split() for line in lines]
    elif mode == "char":
        return [list(line) for line in lines]
    else:
        print(f"Unrecognised mode: {mode}")

def count_corpus(corpus):
    """
    :param corpus: list of lists of tokens (2D) or list of tokens (1D);
    :return: dictionary of <token, count> pairs;
    """
    freqs = dict()
    if len(corpus) == 0:
        return freqs
    if isinstance(corpus[0], list):
        for line in corpus:
            for token in line:
                if token not in freqs:
                    freqs[token] = 1
                else:
                    freqs[token] += 1
    else:
        for token in corpus:
            if token not in freqs:
                freqs[token] = 1
            else:
                freqs[token] += 1
    return freqs

class Vocab:
    def __init__(self, tokens=None, reserved_tokens=None, min_freq=0):
        """
        :param tokens: 1D or 2D list of tokens;
        :param reserved_tokens: list of reserved tokens other than <unk>;
        :param min_freq: take tokens that appear more than min_freq times;
        """
        if tokens is None:
            tokens = []
        if reserved_tokens is None:
            reserved_tokens = []
        token_counts = count_corpus(tokens)
        self._sorted_tokens = sorted([item for item in token_counts.items()
                                      if item[1] > min_freq], key=lambda x: x[1], reverse=True)
        self._idx_to_token = ["<unk>"] + reserved_tokens
        self.num_special_tokens = len(self._idx_to_token)
        self._token_to_idx = {self._idx_to_token[i]: i for i in range(len(self._idx_to_token))}
        for token, _ in self._sorted_tokens:
            if token not in self._token_to_idx:
                self._idx_to_token.append(token)
                self._token_to_idx[token] = len(self._idx_to_token) - 1

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

    def _convert_to_idxs(self, tokens):
        if not isinstance(tokens, (list, tuple)):
            return self._token_to_idx.get(tokens, 0)  # idx if in dict else idx_of_unk
        return [self._convert_to_idxs(token) for token in tokens]

    def __getitem__(self, tokens):
        idxs = self._convert_to_idxs(tokens)
        return idxs if isinstance(idxs, list) else [idxs]

    def _convert_to_tokens(self, idxs):
        if not isinstance(idxs, (list, tuple)):
            return self._idx_to_token[idxs] if (idxs >= 0 and idxs < len(self._idx_to_token)) else "<unk>"
        return [self._convert_to_tokens(idx) for idx in idxs]

    def convert_to_token(self, idxs):
        tokens = self._convert_to_tokens(idxs)
        return tokens if isinstance(tokens, list) else [tokens]


import requests
import json

CONFIDENCE_THRESHOLD = 0.5;

def is_wearing_mask():
    with open("config.json") as config, open("inputData/test_img.jpg", mode="rb") as test_data:
        customvision_data = json.load(config)["customvision"]
        endpoint = customvision_data["endpoint"]
        prediction_key = customvision_data["prediction_key"]
        content_type = customvision_data["content_type"]

        r = requests.post(endpoint, headers={"Prediction-Key": prediction_key, "Content-Type": content_type}, data=test_data)

        json_response_predictions = json.loads(r.text)["predictions"]

        mask_probability = 0.0
        nomask_probability = 0.0
        for prediction in json_response_predictions:
            if prediction["tagName"] == "mask":
                mask_probability = float(prediction["probability"])
            elif prediction["tagName"] == "nomask":
                nomask_probability = float(prediction["probability"])

        return mask_probability > nomask_probability


# -*- coding: utf-8 -*-
"""
Created on Wed Aug 11 18:07:42 2021

@author: Miguel
"""
import torch
import albumentations as A
from albumentations.pytorch import ToTensorV2
from tqdm import tqdm
from tqdm import tnrange, notebook, tqdm
import torch.nn as nn
import torch.optim as optim
import pandas as pd
from basededatos import LiTS
import time as t

from model import UNET, TC_UNET, SEGNET
from utils import (
    load_checkpoint,
    save_checkpoint,
    get_loaders,
    check_accuracy,
    save_predictions_as_imgs,
    generador_nombre,
    informe,
    fecha
)

# Hyperparameters etc.
LEARNING_RATE = 1e-4
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
BATCH_SIZE = 16
NUM_EPOCHS = 10
NUM_WORKERS = 2
IMAGE_HEIGHT = 448  #
IMAGE_WIDTH =  448  #
PIN_MEMORY = True
LOAD_MODEL = True

TRAIN_IMG_DIR = "/content/entrenamiento/vol"
TRAIN_MASK_DIR = "/content/entrenamiento/seg"

#VAL_IMG_DIR = "/content/drive/MyDrive/SOFTWARE_TT/datos/LiTS/batch2/vol"
#VAL_MASK_DIR = "/content/drive/MyDrive/SOFTWARE_TT/datos/LiTS/batch2/seg"

VAL_IMG_DIR = "/content/validacion/vol"
VAL_MASK_DIR = "/content/validacion/seg"

#__________guardar información sobre el entrenamiento___________________________

mode = 'SEGNET'
data = 'LiTS'
shape = (IMAGE_HEIGHT, IMAGE_WIDTH)
batch = BATCH_SIZE
ad = 'AD' # data aumentation
opti = 'ADAM'
nclass = 3

nombre = generador_nombre(mode, data, shape, batch, ad, opti, nclass)
DIR = '/content/drive/MyDrive/SOFTWARE_TT/datos/LiTS/'

CHECK_P_FILENAME = DIR + nombre + ".pth.tar"
INFO_FILENAME =  nombre

info = informe(INFO_FILENAME, LEARNING_RATE, DIR)

#info.agrega(loss, dice, acc,lr)

def train_fn(loader, model, optimizer, loss_fn, scaler,info):
    loop = notebook.tqdm(loader, desc = '=> Entrenando', leave = False)

    for batch_idx, (data, targets) in enumerate(loop):

        data = data.to(device=DEVICE)
        targets = targets.to(device=DEVICE)
        # forward
        with torch.cuda.amp.autocast():

            predictions = model(data)

            loss = loss_fn(predictions.float(), targets)

            info.agrega(loss.detach().cpu().numpy(), (0,0), (0,0))
        # backward
        optimizer.zero_grad()
        scaler.scale(loss).backward()
        scaler.step(optimizer)
        scaler.update()

        # update tqdm loop
        loop.set_postfix(loss=loss.item())

def main():
    train_transform = A.Compose(
        [
            A.Resize(height=IMAGE_HEIGHT, width=IMAGE_WIDTH),
            #A.Rotate(limit=35, p=1.0),
            A.HorizontalFlip(p=0.5),
            A.VerticalFlip(p=0.1),
            A.Normalize(
                mean=[0.0],  # modificar para que sean mas canales de entrada
                std=[1.0],    # modificar para que sean mas canales de entrada
                max_pixel_value=255.0,
            ),
            ToTensorV2(),
        ],
    )
    val_transforms = A.Compose(
        [
            A.Resize(height=IMAGE_HEIGHT, width=IMAGE_WIDTH),
            A.Normalize(
                mean=[0.0],
                std=[1.0],
                max_pixel_value=255.0,
            ),
            ToTensorV2(),
        ],
    )
    if mode == "UNET":
      model = UNET(in_channels=3, out_channels=3).to(DEVICE)
    elif mode == "TC_UNET":
      model = TC_UNET(in_channels=3, out_channels=3).to(DEVICE)
    elif mode == "SEGNET":
      model = SEGNET(in_channels=3, out_channels=3).to(DEVICE)

    #loss_fn = nn.BCEWithLogitsLoss()
    loss_fn = nn.CrossEntropyLoss()

    optimizer = optim.Adam(model.parameters(), lr=LEARNING_RATE)
    #optimizer = optim.SDG(model.parameters(), lr=LEARNING_RATE)
    train_loader, val_loader = get_loaders(
        TRAIN_IMG_DIR,
        TRAIN_MASK_DIR,
        VAL_IMG_DIR,
        VAL_MASK_DIR,
        BATCH_SIZE,
        train_transform,
        val_transforms,
        NUM_WORKERS,
        PIN_MEMORY,
    )

    #if LOAD_MODEL:
    #  load_checkpoint(torch.load(CHECK_P_FILENAME), model)
    info.load_checkpoint(model)
    #check_accuracy(val_loader, model, device=DEVICE)

    scaler = torch.cuda.amp.GradScaler()

    for epoch in notebook.tqdm(range(NUM_EPOCHS), desc = ' General'):

      info.it = epoch
      train_fn(train_loader, model, optimizer, loss_fn, scaler,info)

      # save model

      info.checkpoint(model, optimizer)

      # check accuracy
    check_accuracy(val_loader, model, info, device=DEVICE)

      # print some examples to a folder
      #save_predictions_as_imgs(
      #    val_loader, model, folder="saved_images/", device=DEVICE
      #)
if __name__ == "__main__":
    main()


from random import choice

class RandomWalkRefactoring():

	def __init__(self, number_points=3000):
		self.number_points = number_points
		self.x_value = [0]
		self.y_value = [0]

	def get_x_step(self):
		x_direction = choice([1, -1])
		x_distance = choice([0, 1, 2, 3, 4, 5])
		x_step = x_direction * x_distance
		return x_step

	def get_y_step(self):
		y_direction = choice([1, -1])
		y_distance = choice([0, 1, 2, 3, 4, 5])
		y_step = y_direction * y_distance
		return y_step

	def walk(self):
		while len(self.x_value) < self.number_points:
			x_step = self.get_x_step()
			y_step = self.get_y_step()

			if x_step and y_step == 0:
				continue

			next_step_x = self.x_value[-1] + x_step
			next_step_y = self.y_value[-1] + y_step

			self.x_value.append(next_step_x)
			self.y_value.append(next_step_y)


import os, os.path
import random
import string
import simplejson
# import cherrypy
import json

import sys
sys.path.append("..")
from .jsonClass import *

#MEDIA_DIR = os.path.join(os.path.abspath("."), u"media")

#class StringGenerator(object):
#     @cherrypy.expose
#     def index(self):
#         #return open('public/test.html')
#         return open('../../frontend/GridInterface/samples/gridSkeleton.html')

#     @cherrypy.expose
#     def generate(self, length=8):
#         some_string = ''.join(random.sample(string.hexdigits, int(length)))
#         cherrypy.session['mystring'] = some_string
#         return some_string

      # @cherrypy.expose
def jsonCodeGenerator(jsonObj):

        #cherrypy.response.headers['Content-Type'] = 'application/json'
        # cherrypy.response.headers["Access-Control-Allow-Origin"] = "http://localhost"
        print("====================================")
        print("Code Generator called...........")
        print("JSONObj : "+jsonObj)

        # #return "Hello Raman.."
        data=simplejson.dumps(dict(title='Hello, Raman'))
        print(data)
        print("=============== RAMAN =====================")
        circCreator, numOfInputLines = CreateCircuit(jsonObj)
        print("======== Going to call APPLY ==============")
        output=circCreator.Apply()
        print("############ OUTPUT #############")
        print(output)
        print("#################################")
        #return data
        return OutputToJson(output,numOfInputLines)

        '''
        This function will convert the output of the circuit to the JSON format
        which will be sent back to the frontend for plotting
        '''
def OutputToJson(output,numOfInputLines):
        yList=output[:,0]
        yStringList=[abs(item)[0]**2 for item in yList]
        xList=[bin(val) for val in range(2**numOfInputLines)]
        jsonData={"xdata":xList,"ydata":yStringList}
        # print("++++++++++++++++++++++++++++++")
        # print(xList)
        # print(yStringList)
        jsonConvObj=json.dumps(jsonData)
        print(jsonConvObj)
        return jsonConvObj

def Apply():
        return circCreator.Apply()

#Python code to call QuTiP function
def CreateCircuit(gateJson):
        numOfInputLines=FindNumOfInputLines(gateJson)
        numOfInputLines=numOfInputLines+1
        print("====== numOfInputLines : "+str(numOfInputLines)+" ============")
        circCreator=CircuitCreator(gateJson,numOfInputLines,False)
        return circCreator, numOfInputLines
        #print(self.circCreator.OperatorMatrix())
        # self.circCreator.DumpCircuitImage()

def FindNumOfInputLines(gateJson):
        json_dict=json.loads(gateJson)
        ctl_bits_List=[]
        tgt_bits_List=[]
        for gate_dict in json_dict["instructions"]:
            print("@@@@@@@@@@@@@@@@@@")
            print(gate_dict)
            print("@@@@@@@@@@@@@@@@@@")
            if(gate_dict["ctl_bits"]=="None"):
                gate_dict["ctl_bits"]=None

            if(gate_dict["ctl_bits"]!=None):
                gate_dict["ctl_bits"]=list(map(int,gate_dict["ctl_bits"].split(",")))
                #print("@@@@@@@@ Comparison with None failed @@@@@@@@")
            gate_dict["tgt_bits"]=list(map(int,gate_dict["tgt_bits"].split(",")))

            print(gate_dict["ctl_bits"])
            print(gate_dict["tgt_bits"])
            print("@@@@@@@@@@@@@@@@@@")

            if(gate_dict["ctl_bits"]!=None):
                for ctlBit in gate_dict["ctl_bits"]:
                    ctl_bits_List.append(ctlBit)
            for tgtBit in gate_dict["tgt_bits"]:
                    tgt_bits_List.append(tgtBit)

        maxCtl=0
        if(len(ctl_bits_List)>0):
            maxCtl=max(ctl_bits_List)
        maxTgt=max(tgt_bits_List)
        if(maxCtl > maxTgt):
            return maxCtl
        else:
            return maxTgt

            #return 4
            '''
            print("***************************************")
            print(lists.ctl_bits_List)
            print("***************************************")
            maxCtl=max(lists.ctl_bits_List)
            maxTgt=max(lists.tgt_bits_List)
            if(maxCtl > maxTgt):
                return maxCtl
            else:
                return maxTgt
            '''
        # @cherrypy.expose
        # def display(self):
        #     return cherrypy.session['mystring']

    # if __name__ == '__main__':
    #      conf = {

    #          '/': {
    #              'tools.sessions.on': True,
    #              'tools.staticdir.root': os.path.abspath(os.getcwd())
    #          },
    #          '/static': {
    #              'tools.staticdir.on': True,
    #              #'tools.staticdir.dir': './public'
    #              #'tools.staticdir.dir': '/home/rsehgal/barcQ/frontend'
    #              'tools.staticdir.dir': '../../frontend'
    #          },
    #          '/images': {
    #              'tools.staticdir.on': True,
    #              #'tools.staticdir.dir': './public'
    #              #'tools.staticdir.dir': '/home/rsehgal/barcQ/frontend/images'
    #              'tools.staticdir.dir': '../../frontend/images'
    #          },
    #          '/css': {
    #              'tools.staticdir.on': True,
    #              #'tools.staticdir.dir': './public'
    #              #'tools.staticdir.dir': '/home/rsehgal/barcQ/frontend/css'
    #              'tools.staticdir.dir': '../../frontend/css'
    #          },
    #          '/js': {
    #              'tools.staticdir.on': True,
    #              #'tools.staticdir.dir': './public'
    #              #'tools.staticdir.dir': '/home/rsehgal/barcQ/frontend/js'
    #              'tools.staticdir.dir': '../../frontend/js'
    #          },
    #          '/jsonschemas': {
    #              'tools.staticdir.on': True,
    #              #'tools.staticdir.dir': './public'
    #              #'tools.staticdir.dir': '/home/rsehgal/barcQ/frontend/jsonschemas'
    #              'tools.staticdir.dir': '../../frontend/jsonschemas'
    #          }
    #      }
    #      cherrypy.config.update({'server.socket_host': 'localhost','server.socket_port':8000})
    #      cherrypy.quickstart(StringGenerator(), '/', conf)


# 2019-01-22
# updated 2019-03-05
# sarahfong

########
# Purpose
########

# calculate TFBS density for each roadmap enhancer/ core|derived segment of enhancer
# calculate TFBS diversity for each roadmap enhancer / core|derived segment of enhancer

########
# load modules
########

import glob
import numpy as np
import os
import pandas as pd
import sys

import datetime
last_run = datetime.datetime.now()
print("last run", datetime.datetime.now())

########
# get sample_id
########

sid_in = sys.argv[1]

#sid_list = UBERON0000996,UBERON0001736,CL0002334,CL0002334,UBERON0002331,UBERON0000995 # input is a string
#sid = "E071"
#CL0000312_x_ucsc_tfbs.bed

sid_list = sid_in.split(",")
count = 0

# get all of the temp files
#temp_30000_E061_tfbs_count_density.bed
temp_file_list = glob.glob("/dors/capra_lab/projects/enhancer_ages/roadmap_encode/data/hg19_roadmap_samples_enh_age/tfbs/temp*.bed")
temp_dict = {}
for temp in temp_file_list:
    sid = (temp.split("/")[-1]).split("_")[2]
    enh_id_number = (temp.split("/")[-1]).split("_")[1]
    temp_dict[sid]= enh_id_number

########
# get the TFBS data
# MIDPEAK (30bp) ChIP peaks
########

tfbs_file = "/dors/capra_lab/data/encode/midpeaks_wgEncodeRegTfbsClusteredV3.bed"

########
# measure density
########

for sid in sid_list:

    # get the genome build and sort files correctly
    if len(sid)<5:
        build = "hg19"
    else:
        build = "hg38"

    path = "/dors/capra_lab/projects/enhancer_ages/roadmap_encode/data/%s_roadmap_samples_enh_age/breaks/" % build

    core_breaks_file = "%s%s_core_breaks.bed"% (path, sid)
    print(core_breaks_file)

    # out_path
    ucsc_path = "/dors/capra_lab/projects/enhancer_ages/roadmap_encode/data/%s_roadmap_samples_enh_age/tfbs/ucsc_tfbs/" % build
    out_path = "/dors/capra_lab/projects/enhancer_ages/roadmap_encode/data/%s_roadmap_samples_enh_age/tfbs/" % build

    # check the ucsc path to see if sample has been intersected:

    ucsc_list = glob.glob("%s*x_ucsc_tfbs_midpeak.bed" % ucsc_path)
    ucsc_done = []
    for file in ucsc_list:
        ucsc_sid = (file.split("/")[-1]).split("_")[0]
        ucsc_done.append(ucsc_sid)
    if sid not in ucsc_done:
        print("intersecting %s with ucsc tfbs first" % sid)
        # make the out_path directory
        mkdir_cmd = "mkdir %s" %out_path
        #os.system(mkdir_cmd)

        ########
        # PART 1 - sort bed file
        ########

        sort_cmd = "sort -k5,5 -k2,2 -k3,3 -k6,6 -k7,7 %s >t && mv t %s" % (core_breaks_file, core_breaks_file)
        #os.system(sort_cmd)

        ########
        # PART 2 - Bed intersect file with TFBS
        ########

        outfile_wao = "%s%s_x_ucsc_tfbs_midpeak.bed"% (ucsc_path, sid)

        bed_cmd = "bedtools intersect -a %s -b %s -wao > %s" % (core_breaks_file, tfbs_file, outfile_wao)
        print(bed_cmd)
        #os.system(bed_cmd)
    else:
        outfile_wao = "%s%s_x_ucsc_tfbs_midpeak.bed"% (ucsc_path, sid)
    ########
    # open the SID file
    ########

    enh = pd.read_csv(outfile_wao, header= -1, sep= '\t')

    ########
    # reformat file to get columns/data you want
    ########

    print("the number enh segments that did not overlap TFBS" ,\
          len(enh[20].loc[enh[20]==0]))

    #select the columns you want for analyzing break density
    enh = enh[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 20]]

    # rename the columns
    enh.columns = ["chr_syn",
                   "start_syn",
                   "end_syn",
                   "enh_id",
                   "chr_enh",
                   "start_enh",
                   "end_enh",
                   "seg_index",
                   "core_remodeling",
                   "core",
                   "mrca",
                   "chr_tf",
                   "start_tf",
                   "end_tf",
                   "tf",
                   "len_tfbs_overlap"]

    # assign a unique syn id
    enh["syn_id"] = enh["chr_syn"] + ":" + enh["start_syn"].map(str) + "-" + enh["end_syn"].map(str)

    # assign unique tf_id
    enh["tf_id"] = enh["chr_tf"] + ":" + enh["start_tf"].map(str) + "-" + enh["end_tf"].map(str)

    print("the number of unique enh_ids that overlap TFBS", len(enh["enh_id"].unique()))

    enh_tf_list = enh["enh_id"].unique() # make a list of unique enhancer ids

    if sid in temp_dict.keys():
        chop_id_list = int(temp_dict[sid])
        print("remove enh_ids that have already been done", chop_id_list)
        enh_tf_list = enh_tf_list[(chop_id_list):]
        val = chop_id_list + 1
    else:
        enh_tf_list = enh_tf_list
        val = 0

    # transform the dtypes for quantitative values
    enh[["start_enh",
         "end_enh",
         "start_tf",
         "start_syn",
         "end_syn",
         "end_tf",
         "seg_index",
         "core_remodeling",
         "core","len_tfbs_overlap"]] = enh[["start_enh",
         "end_enh",
         "start_tf",
         "start_syn",
         "end_syn",
         "end_tf",
         "seg_index",
         "core_remodeling",
         "core","len_tfbs_overlap"]].astype(int)

    enh["mrca"] = enh["mrca"].astype(float)

    ########
    # calculate TFBS density
    ########

    # create a dictionary to collect sub dataframes for enhancer tfbs density
    enh_tf_den = {}

    for test_enh in enh_tf_list:
        test = enh.loc[enh["enh_id"]==test_enh]
        # create a base dataframe to add information to the dictionary
        dict_df = test[[
            'syn_id',
            'enh_id',
            'chr_enh',
            'start_enh',
            'end_enh',
            'seg_index',
            'core_remodeling',
            'core',
            'mrca']].drop_duplicates()

        ########
        # Whole enhancer
        # TFBS density = non-redundant tf_id/ len(enh)
        # non-redundant because some TFBS peaks span syntenic block breaks.
        ########

        tf_count = len(test["tf"].loc[(test["len_tfbs_overlap"]>0)])

        dict_df["enh_tf_count"] = tf_count

        # calculate density - abs TFBS count / syntenic segment
        dict_df["enh_tf_den"] = dict_df["enh_tf_count"]/(dict_df["end_enh"] - dict_df["start_enh"])

        # count unique tfbs found in the enhancer
        enh_tf_unique = len(test["tf"].loc[(test["len_tfbs_overlap"]>0)].unique())

        dict_df["enh_tf_unq_count"] = enh_tf_unique

        # calculate diversity - unique TFBS count / syntenic segment
        dict_df["enh_tf_unq_den"] = dict_df["enh_tf_unq_count"]/(dict_df["end_enh"] - dict_df["start_enh"])

        # make a list of unique tfbs
        tfbs = np.array_str(test["tf"].unique())
        dict_df["tfs"] = tfbs

        ########
        # Syntenic block density
        # TFBS density = sum(tf_id)/ len(syn_block)
        ########

        # create dataframe to collect enh information.
        syn_dict={}

        # if tfbs duplicates sit on different syntenic block, this will count each separately
        for syn_id in test["syn_id"].unique():
            syn_test = test.loc[test["syn_id"] == syn_id]

            # abs count of TFBS
            syn_tf = syn_test[['syn_id','start_syn','end_syn']].drop_duplicates()
            syn_tf_count = len(syn_test["tf"].loc[(syn_test["len_tfbs_overlap"]>0)])
            syn_tf["syn_tf_count"]= syn_tf_count

            # abs TFBS count / syntenic segment
            syn_tf["syn_tf_den"] = syn_tf["syn_tf_count"]/(syn_tf["end_syn"] - syn_tf["start_syn"])

            # count unique tfbs found in the enhancer
            syn_tf_unique = len(syn_test["tf"].loc[(syn_test["len_tfbs_overlap"]>0)].unique())
            syn_tf["syn_tf_unq_count"]= syn_tf_unique

            # calculate TFBS diversity - unique TFBS count / syntenic segment
            syn_tf["syn_tf_unq_den"] = syn_tf["syn_tf_unq_count"]/(syn_tf["end_syn"] - syn_tf["start_syn"])

            # make a list of unique tfbs
            tfbs = np.array_str(syn_test["tf"].unique())
            syn_tf["syn_tfs"]= tfbs

            syn_tf = syn_tf[[ 'syn_id',
                     'start_syn',
                     'end_syn',
                     'syn_tf_count',
                     'syn_tf_den',
                     'syn_tf_unq_count',
                     'syn_tf_unq_den',
                     'syn_tfs']].drop_duplicates() # get rid of duplicate records

            syn_dict[syn_id] = syn_tf # save syntenic segment information to dictionary

            syn_temp = pd.concat(syn_dict.values()) # concatenate all the syntenic segments into 1 df
            temp = pd.merge(dict_df, syn_temp, on = "syn_id") # add this to the enhancer tf density dictionary

            enh_tf_den[test_enh] = temp
        if val % 2000 == 0:
            print(val)
            test_df = pd.concat(enh_tf_den.values())
            test_df.to_csv("%stemp_%s_%s_tfbs_count_density.bed" % (out_path, val, sid), sep= '\t', header = True, index = False)
            enh_tf_den = {}

        val +=1

    ########
    # concatenate all the enhancer TFBS information
    ########
    test_df = pd.concat(enh_tf_den.values())

    # write the processed data to a file
    test_df.to_csv("%stemp_%s_%s_tfbs_count_density.bed" % (out_path, val, sid), sep= '\t', header = True, index = False)

    ########
    # concatenate all the enhancer TFBS information
    # remove all temp files
    ########
    concat_cmd = "cat %s%s_tfbs_count_density_*.bed >%s%s_tfbs_count_density.bed" % (out_path, sid, out_path, sid)
    os.system(concat_cmd)

    rm_cmd = "rm %s%s_tfbs_count_density_temp*.bed" % (out_path, sid)
    os.system(rm_cmd)

    os.system(rm_cmd)
    del test_df
    del enh_tf_den
    del enh


import json
import locale
import os
import platform
import random
import sys
import time
from datetime import datetime, timedelta

import discord
import psutil
from discord.ext.commands import Bot

# 수정 금지
intents = discord.Intents().all()

with open('../bot/config.json', encoding='UTF8') as json_file:
    json_data = json.load(json_file)

    token = json_data["token"]
    prefix = json_data["prefix"]
    botname = json_data["bot_name"]
    utc_gmt = json_data["utc_gmt"]

client = Bot(command_prefix=prefix, help_command=None, intents=intents)

rp = discord.Activity(type=discord.ActivityType.listening, name=f"{botname} BY-zz0#1446　　　　\n "
                                                                "오픈소스 : https://url.kr/3qcdvf　")

locale.setlocale(locale.LC_ALL, 'ko_KR.UTF-8')

@client.event
# 봇 시작시 실행하는 스크립트
async def on_ready():
    for i in os.listdir("Cogs"):
        if i.endswith(".py"):
            client.load_extension(f"Cogs.{i[:-3]}")

    with open('../bot/config.json', encoding='UTF8') as json_file:
        json_data = json.load(json_file)

        botver = json_data["version"]
        wc = json_data["welcome_channel_name"]
        bc = json_data["bye_channel_name"]
        botname = json_data["bot_name"]
        nc = json_data["notice_channel_name"]
        lac = json_data["local_announcement_channel_name"]
        bac = json_data["bot_announcement_channel_name"]
        clg = json_data["chat_log"]
        glg = json_data["guilds_log_name"]
        main = json_data["main_guild"]

    print('------')
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print(str(len(client.guilds)) + "개의 서버에서 봇이 동작중입니다.")
    print('------')
    print("Python Version :", sys.version, "|Recommend : 3.9.0|")
    print("Discord.py Version :", discord.__version__, "|Recommend : 1.7.2|")
    print(f"[{botname}] Bot Version :", botver)
    print('------')
    print('welcome channel name :', wc)
    print('bye channel name :', bc)
    print('notice channel name :', nc)
    print('local announcement channel name :', lac)
    print('bot announcement channel name :', bac)
    print('chat log channel name :', clg)
    print('guild(server) log channel name :', glg)
    print('main guild(server) id :', main)
    print('------')
    for i in client.commands:
        print("Command : '" + str(i) + "' is loaded!")
    print('------')
    print("Event : loaded!")
    print('------')
    await client.change_presence(status=discord.Status.online, activity=rp)

    global start_date
    start_date = datetime.utcnow()

# 명령어 'shutdown' [시작]

@client.command()
async def shutdown(ctx):
    with open('../bot/config.json', encoding='UTF8') as json_file:
        json_data = json.load(json_file)

        ownerid = json_data["owner_id"]
        botname = json_data["bot_name"]

    mp = discord.Embed(title="권한 부족 이벤트 발생!", color=0xfc7f03)
    mp.add_field(name="자세한 내용", value=f"당신은 ID : `{ownerid}` 가 아니여서 해당 명령어 사용이 거부되었습니다. 자세한 내용은 관리자에게 문의해주세요.",
                 inline=True)
    mp.set_footer(text="BY - zz0#1446")
    if f"{ctx.message.author.id}" != f"{ownerid}":
        await ctx.send(embed=mp)
    else:
        before = time.monotonic()
        sd = await ctx.send(":arrows_counterclockwise: 종료 준비중...")
        ping = (time.monotonic() - before) * 1000

        uptime = datetime.utcnow() - start_date

        hours, remainder = divmod(int(uptime.total_seconds()), 3600)
        minutes, seconds = divmod(remainder, 60)
        days, hours = divmod(hours, 24)
        # 해당 서버에서 embed 출력
        shutdownembed = discord.Embed(title=":white_check_mark: 봇이 종료되었습니다",
                                      description=f"`{botname}` 봇을 성공적으로"
                                                  f" 종료하였습니다.", color=0x00ff00)
        shutdownembed.add_field(name="처리자", value=f"{ctx.message.author} {ctx.message.author.mention}",
                                inline=False)
        shutdownembed.add_field(name="자세한 정보", value=f"**작동 환경 : {platform.platform()} {platform.machine()}"
                                                     f"\n현재 CPU 사용량 : {psutil.cpu_percent()}%"
                                                     f"\n현재 램 사용량 : {psutil.virtual_memory().percent}% (_Null_MB)"
                                                     f"\n업타임 : {days}일 {hours}시간 {minutes}분 {seconds}초"
                                                     f"\n핑"
                                                     f"\n:table_tennis: 퐁!"
                                                     f"\n{int(ping)}ms, "
                                                     f"{round(client.latency, 1)}ms**")
        shutdownembed.set_footer(text="BY - zz0#1446")
        await sd.edit(content="", embed=shutdownembed)
        await client.close()

@shutdown.error
async def shutdown_error(ctx, error):
    print(error)
    await client.on_command_error(ctx, error)

# 명령어 'shutdown' [끝]

# 입장 [시작]

@client.event
async def on_member_join(member):
    with open('../bot/config.json', encoding='UTF8') as json_file:
        json_data = json.load(json_file)

        wc = json_data["welcome_channel_name"]
        botname = json_data["bot_name"]

    # 서버내 설정된 채널 찿기
    channel = discord.utils.get(member.guild.text_channels, name=f"{wc}")
    if channel is None:
        channel = await member.guild.create_text_channel(f"{wc}")
        await channel.send("해당 채널이 없어 자동으로 생성되었습니다.")

    # Embed 호출
    welcomech = discord.Embed(title=f"ID : {member.id}",
                              description=f"{member.display_name} | {member} | {member.mention}님이 ['{member.guild.name}"
                                          f"'] 에 입장하셨습니다!", timestamp=datetime.utcnow(), color=0x00ff00)
    welcomech.set_author(name=f"{member} 님이 서버에서 입장하셨습니다!", icon_url=member.avatar_url_as(static_format="png"))
    welcomech.add_field(name="자세한 정보",
                        value=f"계정을 가입한 시간 : "
                              f"`{(member.created_at + timedelta(hours=utc_gmt)).strftime('%Y년 %m월 %d일 %A %p %I시 %M분 %S초')}`"

                              f"\n서버에 가입한 시간 : "
                              f"`{(member.joined_at + timedelta(hours=utc_gmt)).strftime('%Y년 %m월 %d일 %A %p %I시 %M분 %S초')}`",
                        inline=False)
    welcomech.set_footer(
        text="서버 인원 : " + str(channel.guild.member_count) + f"명 [BY - zz0#1446, {botname}]")
    await channel.send(embed=welcomech)

# 입장 [끝]

# 퇴장 [시작]

@client.event
async def on_member_remove(member):
    with open('../bot/config.json', encoding='UTF8') as json_file:
        json_data = json.load(json_file)

        bc = json_data["bye_channel_name"]
        botname = json_data["bot_name"]

    # 서버내 설정된 채널 찿기
    channel = discord.utils.get(member.guild.text_channels, name=f"{bc}")
    if channel is None:
        channel = await member.guild.create_text_channel(f"{bc}")
        await channel.send("해당 채널이 없어 자동으로 생성되었습니다.")

    # Embed 호출
    byech = discord.Embed(title=f"ID : {member.id}",
                          description=f"{member.display_name} | {member} | {member.mention}님이 ['{member.guild.name}'] "
                                      f"에서 퇴장하셨습니다!", timestamp=datetime.utcnow(), color=0xff0000)
    byech.set_author(name=f"{member} 님이 서버에서 퇴장하셨습니다!", icon_url=member.avatar_url_as(static_format="png"))
    byech.add_field(name="자세한 정보",
                    value=f"계정을 가입한 시간 : "
                          f"`{(member.created_at + timedelta(hours=utc_gmt)).strftime('%Y년 %m월 %d일 %A %p %I시 %M분 %S초')}`"
                          f"\n서버에 가입한 시간 : "
                          f"`{(member.joined_at + timedelta(hours=utc_gmt)).strftime('%Y년 %m월 %d일 %A %p %I시 %M분 %S초')}`"

                          f"\n서버를 떠난 시간 : `" + (datetime.utcnow() + timedelta(hours=utc_gmt)).strftime("%Y년 %m월 %d일 %A %p %I시 %M분 %S초") + "`",
                    inline=False)
    byech.set_footer(text="서버 인원 : " + str(channel.guild.member_count) + f"명 [BY - zz0#1446, {botname}]")
    await channel.send(embed=byech)

# 퇴장 [끝]

# 봇 서버 입장 로그 [시작]

@client.event
async def on_guild_join(guild):
    with open('../bot/config.json', encoding='UTF8') as json_file:
        json_data = json.load(json_file)

        glg = json_data["guilds_log_name"]
        main = json_data["main_guild"]

    # 서버내 설정된 채널 찿기
    channel = discord.utils.get(client.get_guild(int(main)).text_channels, name=f"{glg}")
    if channel is None:
        overwrites = {
            client.get_guild(int(main)).default_role: discord.PermissionOverwrite(read_messages=False)
        }
        channel = await client.get_guild(int(main)).create_text_channel(f"{glg}",
                                                                        overwrites=overwrites)
        await channel.send("해당 채널이 없어 자동으로 생성되었습니다.")

    user_count = int()
    bot_count = int()
    users = list()
    bots = list()

    getrandomchannel = random.choice(guild.text_channels)
    invite = await getrandomchannel.create_invite(max_age=0, max_uses=1, unique=True)

    for i in guild.members:
        if not i.bot:
            user_count += 1
            users.append(str(i.name) + "#" + str(int(i.discriminator)))
        else:
            bot_count += 1
            bots.append(str(i.name) + "#" + str(int(i.discriminator)))

    jinfo = discord.Embed(timestamp=datetime.utcnow(), color=0x80ff00)
    jinfo.set_author(name=f"서버 '{guild.name}'가 봇을 추가하였습니다. [서버 정보]", icon_url=guild.icon_url)
    jinfo.add_field(name="서버 이름", value=f"{guild.name}", inline=True)
    jinfo.add_field(name="서버 ID", value=f"{guild.id}", inline=True)
    jinfo.add_field(name="서버 위치", value=f"{guild.region}", inline=True)
    jinfo.add_field(name="서버 소유자", value=f"{guild.owner}", inline=True)
    jinfo.add_field(name="서버 소유자 ID", value=f"{guild.owner_id}", inline=True)
    jinfo.add_field(name="서버내 채널 수", value=f"{len(guild.channels)}개", inline=True)
    jinfo.add_field(name="서버내 멤버 수", value=f"{user_count}명", inline=True)
    jinfo.add_field(name="서버내 봇 수", value=f"{bot_count}개", inline=True)
    jinfo.add_field(name="서버내 역할 수", value=f"{len(guild.roles)}개", inline=True)
    jinfo.add_field(name="서버가 생성된 시간",
                    value=f"{(guild.created_at + timedelta(hours=utc_gmt)).strftime('%Y년 %m월 %d일 %A %p %I시 %M분 %S초')}",
                    inline=False)
    jinfo.add_field(name="초대 링크", value=f"{invite}", inline=False)
    jinfo.add_field(name="멤버 리스트", value=f"{', '.join(users)}", inline=True)
    jinfo.add_field(name="봇 리스트", value=f"{', '.join(bots)}", inline=True)
    jinfo.set_footer(text=f"이용중인 서버수 {str(len(client.guilds))}개")

    await channel.send(embed=jinfo)

# 봇 서버 입장 로그 [끝]

# 봇 서버 퇴장 로그 [시작]

@client.event
async def on_guild_remove(guild):
    with open('../bot/config.json', encoding='UTF8') as json_file:
        json_data = json.load(json_file)

        glg = json_data["guilds_log_name"]
        main = json_data["main_guild"]

    # 서버내 설정된 채널 찿기
    channel = discord.utils.get(client.get_guild(int(main)).text_channels, name=f"{glg}")
    if channel is None:
        overwrites = {
            client.get_guild(int(main)).default_role: discord.PermissionOverwrite(read_messages=False)
        }
        channel = await client.get_guild(int(main)).create_text_channel(f"{glg}",
                                                                        overwrites=overwrites)
        await channel.send("해당 채널이 없어 자동으로 생성되었습니다.")

    rinfo = discord.Embed(timestamp=datetime.utcnow(), color=0x80ff00)
    rinfo.set_author(name=f"서버 '{guild.name}'가 봇을 제거하였습니다.", icon_url=guild.icon_url)
    rinfo.set_footer(text=f"이용중인 서버수 {str(len(client.guilds))}개")

    await channel.send(embed=rinfo)

# 봇 서버 입장 로그 [끝]

client.run(token)


from flask import Flask, request
from flask_cors import CORS
from json import dumps
from backend_functions import add_number, todays_data, compare_lengths
from database import get_data
from get_data_file import update_info
import sys
'''
default code
'''

def defaultHandler(err):
    response = err.get_response()
    print('response', err, err.get_response())
    response.data = dumps({
        "code": err.code,
        "name": "System Error",
        "message": err.get_description(),
    })
    response.content_type = 'application/json'
    return response

APP = Flask(__name__)
CORS(APP)

@APP.route("/join", methods=['POST'])
def post_add_number():
    info = request.get_json()

    return add_number(info['number'], info['postcode'])

@APP.route("/info", methods=['GET'])
def get_information():

    return dumps(get_data())

@APP.route("/update", methods=['POST'])
def update_information():
    #update_info()
    compare_lengths()
    return {}

if __name__ == '__main__':

    APP.run(port=(int(sys.argv[1]) if len(sys.argv) == 2 else 8080))


# basic NOMADS OpenDAP extraction (and plotting script)
from mpl_toolkits.basemap import Basemap
import numpy as np
import netCDF4
import datetime
import cPickle as pickle
#import matplotlib.pyplot as plt
#import matplotlib.animation as animation
#from mpl_toolkits.axes_grid1 import make_axes_locatable

##Multi-grid wave model: US West Coast (including Hawaiian coast) 4 arc-min grid
#Longitude:
#195.00000000000E to 244.00024500000E
# (736 points, avg. res. 0.067 deg)
#Latitude:
#15.00000000000N to 50.00017500000N
# (526 points, avg. res. 0.067 deg)
#Time:
#06Z09AUG2017 to 18Z16AUG2017
# (61 points, avg. res. 0.125 days)
#Variables:
#(total of 16)
#dirpwsfc = ** surface primary wave direction [deg]
#htsgwsfc = ** surface significant height of combined wind waves and swell [m]
#perpwsfc = ** surface primary wave mean period [s]
#swdir_1 = ** 1 in sequence direction of swell waves [deg]
#swdir_2 = ** 2 in sequence direction of swell waves [deg]
#swell_1 = ** 1 in sequence significant height of swell waves [m]
#swell_2 = ** 2 in sequence significant height of swell waves [m]
#swper_1 = ** 1 in sequence mean period of swell waves [s]
#swper_2 = ** 2 in sequence mean period of swell waves [s]
#ugrdsfc = ** surface u-component of wind [m/s]
#vgrdsfc = ** surface v-component of wind [m/s]
#wdirsfc = ** surface wind direction (from which blowing) [deg]
#windsfc = ** surface wind speed [m/s]
#wvdirsfc = ** surface direction of wind waves [deg]
#wvhgtsfc = ** surface significant height of wind waves [m]
#wvpersfc = ** surface mean period of wind waves [s]

##mydate='20170807'
mydate = datetime.datetime.now().strftime('%Y%m%d')

url1='http://nomads.ncep.noaa.gov:9090/dods/wave/mww3/'+ \
    mydate+'/multi_1.wc_4m'+mydate+'_00z'

file = netCDF4.Dataset(url1)
keys = file.variables.keys()

dat = {}
for key in keys:
   if (key != 'time') and (key != 'lat') and (key != 'lon'):
      pass
   else:
      dat[str(key)] = file.variables[key][:]

# constrain geographic location
ind_lat = np.where((dat['lat']>38) & (dat['lat']<42))[0]
ind_lon = np.where((dat['lon']>233) & (dat['lon']<237))[0]

for key in file.variables.keys():
   if (key != 'time') and (key != 'lat') and (key != 'lon'):
      print(str(key))
      dat[str(key)] = file.variables[key][:,ind_lat,ind_lon]
   else:
      pass
file.close()

dat['lon'] = dat['lon'][ind_lon]
dat['lat'] = dat['lat'][ind_lat]

dat['epochtime'] = []
for k in range(len(dat['time'])):
   tmp = datetime.timedelta(days=dat['time'][k] -366)+datetime.datetime(1,1,1)
   tmp = tmp.replace(year=tmp.year + 1)
   dat['epochtime'].append(tmp)

dat['datestring'] = []
for k in range(len(dat['time'])):
   dat['datestring'].append(dat['epochtime'][k].strftime('%a_%b_%d_%H_%M_%S_%Y'))

for key in keys:
    if (key != 'time') and (key != 'lat') and (key != 'lon'):
       filename = key+'_'+dat['datestring'][0]+'_'+dat['datestring'][-1]+'.pkl'
       with open(filename, 'wb') as fid:
          pickle.dump((dat[key], dat['epochtime'], dat['lat'], dat['lon']), fid, protocol=pickle.HIGHEST_PROTOCOL)

###============================================
#def ani_frames(dat, key):

#    fig = plt.figure()
#    ax1 = fig.add_subplot(111)
#    #ax1.get_xaxis().set_visible(False)
#    #ax1.get_yaxis().set_visible(False)

#    im = plt.imshow(dat[key][1,:,:],extent=[dat['lon'].min(), dat['lon'].max(), dat['lat'].min(), dat['lat'].max()])

#    divider = make_axes_locatable(ax1)
#    cax = divider.append_axes("right", size="5%")#, pad=0.05)
#    cb=plt.colorbar(im, cax=cax)
#    cb.ax.tick_params(labelsize=5)

#    def init():
#       im.set_data([[]])
#       return im

#    def update_img(i):
#        tmp = dat[key][i,:,:]
#        im.set_data(tmp)
#        ax1.imshow(tmp)
#        return im

#    ani = animation.FuncAnimation(fig,update_img, frames=len(dat['time']), interval=1000, init_func = init, save_count=len(dat['time']))
#    if animation.writers.is_available('ffmpeg'):
#       print('using ffmpeg to compile video')
#       writer = animation.writers['ffmpeg'](fps=1)
#    else:
#       print('using avconv to compile video')
#       writer = animation.writers['avconv'](fps=1)

#    ani.save('test.mp4',writer=writer,dpi=600)
#    del fig
#    print("Done!")
#    return ani

#lon = dat['lon']
#lat = dat['lat']

#key = 'htsgwsfc'
#ani_frames(dat, key)

## set up the figure
#plt.figure()

#m=Basemap(projection='mill',lat_ts=10,llcrnrlon=lon.min(), \
#  urcrnrlon=lon.max(),llcrnrlat=lat.min(),urcrnrlat=lat.max(), \
#  resolution='i')

## convert the lat/lon values to x/y projections.
#x, y = m(*np.meshgrid(lon,lat))

## plot the field using the fast pcolormesh routine
## set the colormap to jet.
#xm, ym = m(235.88, 40.11)
#m.pcolormesh(x,y,dat[key][1,:,:],shading='flat',cmap=plt.cm.jet)
#m.colorbar(location='right')
#m.plot(xm, ym, 'kp')

## Add a coastline and axis values.

#m.drawcoastlines()
#m.fillcontinents()
#m.drawmapboundary()
#m.drawparallels(np.arange(-90.,120.,2.),labels=[1,0,0,0])
#m.drawmeridians(np.arange(-180.,180.,2.),labels=[0,0,0,1])

## Add a colorbar and title, and then show the plot.

#plt.axis('tight')

#plt.title('NWW3 Significant Wave Height from NOMADS')
#plt.show()

##lat  = file.variables['lat'][:]
##lon  = file.variables['lon'][:]
###data = file.variables['htsgwsfc'][1,:,:]
##data = file.variables['htsgwsfc'][1,ind_lat,ind_lon]


# Written by Eric Martin for COMP9021

from random import seed, randrange
from collections import Counter

def give_values_to_letters(for_seed):
    seed(for_seed)
    return [randrange(1, 10) for _ in range(26)]

def capture_words():
    words = {}
    with open('dictionary.txt') as dictionary:
        for word in dictionary:
            word = word[: -1]
            words[word] = Counter(word)
    return words

# word and letters are both meant to be strings of nothing but
# uppercase letters, values, a list returned by
# give_values_to_letters(). Returns:
# - -1 if word is not in dictionary.txt
# - 0 if word is in dictionary.txt but cannot be built from letters
# - the value of word according to values otherwise.
def can_be_built_from_with_value(word, letters, values):
    try:
        word_letters = capture_words()[word]
    except KeyError:
        return -1
    values = {chr(ord('A') + i): values[i] for i in range(26)}
    available_letters = Counter(letters)
    try:
        if all(word_letters[c] <= available_letters[c] for c in word_letters):
            return sum(values[c] for c in word)
        else:
            raise KeyError
    except KeyError:
        return 0

# letters is meant to be a string of nothing but uppercase letters.
# Returns the list of words in dictionary.txt that can be built
# from letters and whose value according to values is maximal.
# Longer words come before shorter words.
# For a given length, words are lexicographically ordered.
def most_valuable_solutions(letters, values):
    words = capture_words()
    values = {chr(ord('A') + i): values[i] for i in range(26)}
    available_letters = Counter(letters)
    solutions = []
    best_value = 0
    for word in words:
        word_letters = words[word]
        try:
            if all(word_letters[c] <=\
                          available_letters[c] for c in word_letters
                  ):
                value = sum(values[c] for c in word)
                if value > best_value:
                    best_value = value
                    solutions = [word]
                elif value == best_value:
                    solutions.append(word)
        except KeyError:
            continue
    solutions.sort(key=lambda x: -len(x))
    return solutions


"""
Tests the SQL requests.
"""

import sqlite3
import os
import pytest
import pandas as pd
import sqlalchemy

@pytest.fixture
def db_conn():
    catalogue_path = os.path.join(
        os.path.dirname(__file__), '../problem_statement')

    db = sqlalchemy.create_engine('sqlite://', echo=True)
    pd.read_csv(f'{catalogue_path}/transactions.csv', parse_dates=['date']) \
        .to_sql('transactions', db)
    pd.read_csv(f'{catalogue_path}/product_nomenclature.csv') \
        .to_sql('product_nomenclature', db)
    return db

def test_query_1(db_conn):
    with open(os.path.join(os.path.dirname(__file__), 'query_1.sql')) as f:
        query_1 = f.read()
    df = pd.read_sql_query(query_1, db_conn)

    expected_df = pd.read_csv(
        os.path.join(os.path.dirname(__file__), 'expected_query_1_result.csv'))
    pd.testing.assert_frame_equal(expected_df, df)

def test_query_2(db_conn):
    with open(os.path.join(os.path.dirname(__file__), 'query_2.sql')) as f:
        query_2 = f.read()
    df = pd.read_sql_query(query_2, db_conn)

    expected_df = pd.read_csv(
        os.path.join(os.path.dirname(__file__), 'expected_query_2_result.csv'))
    pd.testing.assert_frame_equal(expected_df, df)


# -*- encoding: utf-8 -*-
# Copyright 2016 Vinzor Co.,Ltd.
#
# comment
#
# 16-7-9 qinjinghui : Init

import os
import sys
import re

# Fix path problem
basedir = os.path.abspath(os.path.dirname(__file__))
os.chdir(basedir)
sys.path.append(basedir)
sys.path.append(os.path.join(basedir, '..'))

# Import environment variables
env_file_path = os.path.join(basedir, '.env')
if os.path.exists(env_file_path):
    print('Importing environment from .env...')
    env = {}
    for line in open(env_file_path):
        line = line.strip()
        # Skip comments
        if re.match('^\s*#', line):
            continue
        try:
            idx = line.index('=')
        except:
            continue
        if idx+1 == len(line):
            continue
        env[line[:idx]] = line[idx+1:]
    os.environ.update(env)

from app import app, db
from app.models import User, Role
from flask.ext.script import Manager, Shell
from flask.ext.migrate import Migrate, MigrateCommand

#app = create_app(os.getenv('FLASK_CONFIG') or 'default')

manager = Manager(app)
migrate = Migrate(app, db)

def make_shell_context():
    return dict(app=app, db=db, User=User, Role=Role)

manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)

@app.teardown_request
def teardown_request(exec):
    print('***** teardown request *****')

@manager.command
def deploy():
    """Run deployment tasks."""
    import datetime
    from flask.ext.migrate import upgrade
    from app.models import Role, User

    # migrate database to latest revision
    upgrade()

    # init data
    Role.insert_default_roles()
    User.insert_default_users()

    app.logger.info("Deploy finished at %s." % datetime.datetime.now())

@manager.command
def test():
    """Run the unit tests."""
    import unittest
    tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)

@manager.command
def runserver():
    app.run(host='0.0.0.0', port=8001)

if __name__ == '__main__':
    manager.run()


# -*- coding: utf-8 -*-
#    Copyright 2013 Mirantis, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import copy

import jsonschema
from jsonschema import exceptions
from oslo_serialization import jsonutils
import six

from nailgun.api.v1.validators.json_schema import base_types
from nailgun import errors
from nailgun import objects
from nailgun.utils import restrictions

class BasicValidator(object):

    single_schema = None
    collection_schema = None

    @classmethod
    def validate_json(cls, data):
        # todo(ikutukov): this method not only validation json but also
        # returning parsed data
        if data:
            try:
                res = jsonutils.loads(data)
            except Exception:
                raise errors.JsonDecodeError(
                    "Invalid json received",
                    log_message=True
                )
        else:
            raise errors.InvalidData(
                "Empty request received",
                log_message=True
            )
        return res

    @classmethod
    def validate_request(cls, req, resource_type,
                         single_schema=None,
                         collection_schema=None):
        json_req = cls.validate_json(req)

        use_schema = {
            "single": single_schema or cls.single_schema,
            "collection": collection_schema or cls.collection_schema
        }.get(resource_type)

        try:
            jsonschema.validate(json_req, use_schema)
        except exceptions.ValidationError as exc:
            if len(exc.path) > 0:
                raise errors.JsonValidationError(
                    # NOTE(ikutukov): here was a exc.path.pop(). It was buggy
                    # because JSONSchema error path could contain integers
                    # and joining integers as string is not a good idea in
                    # python. So some schema error messages were not working
                    # properly and give 500 error code except 400.
                    ": ".join([six.text_type(exc.path), exc.message])
                )
            raise errors.JsonValidationError(exc.message)

    @classmethod
    def validate(cls, data):
        return cls.validate_json(data)

    @classmethod
    def validate_schema(cls, data, schema):
        """Validate a given data with a given schema.

        :param data:   a data to validate represented as a dict
        :param schema: a schema to validate represented as a dict;
                       must be in JSON Schema Draft 4 format.
        """
        try:
            checker = jsonschema.FormatChecker()
            jsonschema.validate(data, schema, format_checker=checker)
        except Exception as exc:
            # We need to cast a given exception to the string since it's the
            # only way to print readable validation error. Unfortunately,
            # jsonschema has no base class for exceptions, so we don't know
            # about internal attributes with error description.
            raise errors.InvalidData(str(exc))

    @classmethod
    def validate_release(cls, data=None, cluster=None, graph_type=None):
        """Validate if deployment tasks are present in db

        :param data: data
        :param cluster: Cluster instance
        :param graph_type: deployment graph type
        :raises NoDeploymentTasks:
        """
        # TODO(akostrikov) https://bugs.launchpad.net/fuel/+bug/1561485
        if (cluster and objects.Release.is_granular_enabled(cluster.release)
                and not objects.Cluster.get_deployment_tasks(
                cluster, graph_type)):
            raise errors.NoDeploymentTasks(
                "There are no deployment tasks for graph type '{}'. "
                "Checked cluster (ID={}), its plugins and release (ID={})."
                "".format(graph_type, cluster.id, cluster.release.id))

    @classmethod
    def validate_ids_list(cls, data):
        """Validate list of integer identifiers.

        :param data: ids list to be validated and converted
        :type data: iterable of strings
        :returns: converted and verified data
        :rtype: list of integers
        """
        try:
            ret = [int(d) for d in data]
        except ValueError:
            raise errors.InvalidData('Comma-separated numbers list expected',
                                     log_message=True)

        cls.validate_schema(ret, base_types.IDS_ARRAY)

        return ret

class BaseDefferedTaskValidator(BasicValidator):

    @classmethod
    def validate(cls, cluster):
        pass

class BasicAttributesValidator(BasicValidator):

    @classmethod
    def validate(cls, data):
        attrs = cls.validate_json(data)

        cls.validate_attributes(attrs)

        return attrs

    @classmethod
    def validate_attributes(cls, data, models=None, force=False):
        """Validate attributes.

        :param data: attributes
        :type data: dict
        :param models: models which are used in
                       restrictions conditions
        :type models: dict
        :param force: don't check restrictions
        :type force: bool
        """
        for attrs in six.itervalues(data):
            if not isinstance(attrs, dict):
                continue
            for attr_name, attr in six.iteritems(attrs):
                cls.validate_attribute(attr_name, attr)

        # If settings are present restrictions can be checked
        if models and not force:
            restrict_err = restrictions.AttributesRestriction.check_data(
                models, data)
            if restrict_err:
                raise errors.InvalidData(
                    "Some restrictions didn't pass verification: {}"
                    .format(restrict_err))
        return data

    @classmethod
    def validate_attribute(cls, attr_name, attr):
        """Validates a single attribute from settings.yaml.

        Dict is of this form::

          description: <description>
          label: <label>
          restrictions:
            - <restriction>
            - <restriction>
            - ...
          type: <type>
          value: <value>
          weight: <weight>
          regex:
            error: <error message>
            source: <regexp source>

        We validate that 'value' corresponds to 'type' according to
        attribute_type_schemas mapping in json_schema/cluster.py.
        If regex is present, we additionally check that the provided string
        value matches the regexp.

        :param attr_name: Name of the attribute being checked
        :param attr: attribute value
        :return: attribute or raise InvalidData exception
        """

        if not isinstance(attr, dict):
            return attr

        if 'type' not in attr and 'value' not in attr:
            return attr

        schema = copy.deepcopy(base_types.ATTRIBUTE_SCHEMA)
        type_ = attr.get('type')
        if type_:
            value_schema = base_types.ATTRIBUTE_TYPE_SCHEMAS.get(type_)
            if value_schema:
                schema['properties'].update(value_schema)

        try:
            cls.validate_schema(attr, schema)
        except errors.JsonValidationError as e:
            raise errors.JsonValidationError(
                '[{0}] {1}'.format(attr_name, e.message))

        # Validate regexp only if some value is present
        # Otherwise regexp might be invalid
        if attr['value']:
            regex_err = restrictions.AttributesRestriction.validate_regex(attr)
            if regex_err is not None:
                raise errors.JsonValidationError(
                    '[{0}] {1}'.format(attr_name, regex_err))