import torch

from hooks.transformer.vit import ViTAttHookHolder
from model import model_library
from utils import exp_starter_pack
from datasets import weird_image_net

def main():
    exp_name, args, _ = exp_starter_pack()
    model, image_size, batch_size, name = model_library[34]()
    eval = weird_image_net.eval()

    hooks = ViTAttHookHolder(model, True, True, True, True, True, True)

    """ Result: 18 """
    # images, labels = [], []
    # for i in range(1000):
    #     x, y = eval[i]
    #     images.append(x)
    #     labels.append(y)
    #
    #     xs = torch.stack(images).cuda()
    #     with torch.no_grad():
    #         _ = hooks(xs)
    #         print(f'Can be done: {len(xs)} w/o grad')

    """ Result: 10 """
    images, labels = [], []
    for i in range(1000):
        x, y = eval[i]
        images.append(x)
        labels.append(y)

        xs = torch.stack(images).cuda()
        cl = xs.clone()
        cl.requires_grad_()
        _, out = hooks(cl)
        out.sum().backward()
        print(f'Can be done: {len(xs)} w/ grad')

if __name__ == '__main__':
    main()


import json
import time
import requests
import random
import easygui

def get_like(tlp: tuple):
    return tlp[2]

input = easygui.enterbox("请输入作品ID")

aid: int = input
i = 0
all_comments = []
like_sort_mode = True
print(f'aid: {aid}')

while 1:
    i += 1
    data = requests.get(f'http://api.bilibili.com/x/v2/reply?jsonp=jsonp&pn={i}&type=1&oid={aid}').json()
    print('len: ', len(data['data']['replies']))
    if len(data['data']['replies']) < 1:
        break
    else:
        all_comments.extend(data['data']['replies'])
    time.sleep(random.uniform(0.8, 1.2))

print('oh i finished step 1')
filter_data = []

for i in range(len(all_comments)):
    msg = all_comments[i]['content']['message']
    if len(msg) >= 20 or '建议' in msg or '觉得' in msg or '看法' in msg:
        filter_data.append((all_comments[i]['member']['uname'], all_comments[i]['content']['message'], all_comments[i]['like']))

if like_sort_mode:
    filter_data = sorted(filter_data, key=get_like, reverse=True)
print('step 2')

with open(f'49_comments_output_av{aid}.txt', 'w', encoding='u8') as f:
    for i in filter_data:
        # json.dumps(filter_data, ensure_ascii=False)
        f.write(f'{i[0]}:\n{i[1]}\n')
        f.write(f'[👍 点赞数] {i[2]}')
        f.write('\n==========================================\n')


import math
import time
from typing import List

import numpy as np

from agent.agent import Agent, SOFTMAX
from agent.non_tabular.non_tabular_agent import NonTabularAgent
from agent.tabular.tabular_agent import TabularAgent
from method.actor_critic.actor_critic import get_d_ln_pi__d_theta
from method.reinforce.model.episode_vars import EpisodeVars

def reinforce_with_baseline(agent: Agent, lambda_: float,
                            alpha_weights: float, alpha_theta: float,
                            sigma: float, trial: int, trials_total: int,
                            episodes: int, trials_dir: str) -> list:
    # List of rewards across all episodes, for this one trial
    episode_returns = []

    theta = agent.init_weights()

    weights = agent.init_weights_actor_critic()

    exec_time = time.time()
    for episode in range(episodes):
        print('Episode {} / {} in Trial {} / {} (Time = {} s, '
              'Time steps = {}, {})'
            .format(episode + 1, episodes, trial + 1, trials_total,
                round(time.time() - exec_time, 2), agent.time_step, trials_dir))
        exec_time = time.time()

        agent.reset_for_new_episode(sigma=sigma)

        episode_vars_list: List[EpisodeVars] = []

        while not agent.has_terminated():
            state = agent.state

            action = agent.get_action(weights=theta,
                    action_selection_method=SOFTMAX)

            reward, state_next = agent.take_action(action)

            episode_vars_list.append(
                    EpisodeVars(state=state, state_next=state_next,
                            action=action, reward=reward)
            )

            # Episode end

        g_array = get_g_array(episode_vars_list=episode_vars_list,
                gamma=agent.gamma)

        delta_j_pi = np.zeros_like(theta)

        e_trace = agent.init_e_v()

        for t in range(len(episode_vars_list)):
            v_w, v_w_next, dv_dw = get__v_w__dv_dw(agent=agent, t=t,
                    episode_vars_list=episode_vars_list, weights=weights,
                    e_trace=e_trace)

            delta_j_pi += math.pow(agent.gamma, t) * \
                          (g_array[t] - v_w) * \
                          get_d_ln_pi__d_theta(agent=agent, theta=theta,
                                  state=episode_vars_list[t].state,
                                  action_index=agent.get_action_index(
                                          episode_vars_list[t].action)
                          )

            e_trace *= agent.gamma * lambda_ + dv_dw

            delta = episode_vars_list[t].reward + \
                    agent.gamma * v_w_next - v_w

            weights += alpha_weights * delta * e_trace

            # Time steps end

        theta += alpha_theta * delta_j_pi

        episode_returns.append(agent.returns)

        # Episode end

    return episode_returns

def get__v_w__dv_dw(agent: Agent, t: int, episode_vars_list: List[EpisodeVars],
                    weights: np.ndarray, e_trace: np.ndarray) -> tuple:
    dv_dw = np.zeros_like(e_trace)

    if isinstance(agent, TabularAgent):
        state_index = agent.get_state_index(episode_vars_list[t].state)
        state_next_index = agent.get_state_index(episode_vars_list[t].state_next)

        dv_dw[state_index] = 1

        return weights[state_index], weights[state_next_index], dv_dw

    elif isinstance(agent, NonTabularAgent):
        v_w = agent.get_q_values_vector(state=episode_vars_list[t].state,
                weights=weights)
        v_w_next = agent.get_q_values_vector(
                state=episode_vars_list[t].state_next,
                weights=weights)

        dv_dw = agent.get_phi(episode_vars_list[t].state)

        return v_w, v_w_next, dv_dw

def get_g_array(episode_vars_list: List[EpisodeVars], gamma: float) \
        -> np.ndarray:
    g_array = np.zeros(len(episode_vars_list))

    for i in range(len(g_array))[::-1]:
        g_array[i] = episode_vars_list[i].reward

        if i != len(g_array) - 1:
            g_array[i] += gamma * g_array[i + 1]

    return g_array


import datetime
import serial
import threading
import time
import sys

class Y3Module(threading.Thread):
    """Wi-SUN Module BP35A1(ROHM) 通信クラス"""
    def __init__(self):
        """コンストラクタ"""
        super().__init__()
        self.Y3_UDP_ECHONET_PORT = 3610 # ECHONET UDPポート
        self.Y3_UDP_PANA_PORT = 716     # PANAポート
        self.Y3_TCP_ECHONET_PORT = 3610 # TCPポート

        self.msg_list_queue = []        # 受信データ用リスト
        self.term_flag = False          # run()の終了フラグ

        self.uart_hdl = None            # UART
        self.uart_dev = None
        self.uart_baud = 9600

        self.search = {                 # write()用, UART送信後の受信待ちデータ
            'search_words': [],             # UART送信後の受信待ちデータリスト
            'ignore_intermidiate': False,   # 途中の受信データを無視する
            'found_word_list': [],          # 受け取った受信待ちデータリスト
            'start_time': None,             # UART送信時のtime
            'timeout': 0}                   # 設定タイムアウト時間[s]

        self.msg_list_lock = threading.Lock()   # msg_listの排他制御用

    def set_opt(self, flag):
        """ERXUDP, ERXTCPのフォーマット設定
            flag: True: ASCII
                  False: Binary
        """
        current = self.get_opt()
        if flag and not current:        # 変更無しの場合はモジュールに書き込まない（FLASHへの書き込み制限）
            self.write(b'WOPT 01\r\n', ['OK 01'])
        elif not flag and current:
            self.write(b'WOPT 00\r\n', ['OK 00'])
        return True

    def get_opt(self):
        """ERXUDP, ERXTCPのフォーマット取得
            retern True: ASCII
                   False: Binary
        """
        res = self.write(b'ROPT\r\n', ['OK'])
        return True if res[0]['MESSAGE'][0] == '01' else False

    def set_echoback_off(self):
        """エコーバックを停止"""
        self.write(b'SKSREG SFE 0\r\n', ['OK'], ignore = True)

    def set_channel(self, ch):
        """Wi-SUNチャンネル設定"""
        bc = '{:02X}'.format(ch).encode()
        self.write(b'SKSREG S02 ' + bc + b'\r\n', ['OK'])

    def set_pairing_id(self, pairid):
        """ペアリングID設定"""
        self.write(b'SKSREG S0A ' + pairid.encode() + b'\r\n', ['OK'])

    def set_pan_id(self, pan):
        """PAN ID設定"""
        bp = '{:04X}'.format(pan).encode()
        self.write(b'SKSREG S03 ' + bp + b'\r\n', ['OK'])

    def set_accept_beacon(self, flag):
        """ビーコンリクエストへの反応
            flag True:  応答する
                 False: 応答しない
        """
        bf = b'1' if flag else b'0'
        self.write(b'SKSREG S15 ' + bf + b'\r\n', ['OK'])

    def get_tx_limit(self):
        """送信制限フラグ取得"""
        res = self.write(b'SKSREG SFB\r\n' , ['ESREG', 'OK'])
        result = True if res[0]['VAL'][0] == '1' else False
        return result

    def set_password(self, password):
        """パスワード設定"""
        length = len(password)
        if length < 1 or length > 32:
            result = False
        else:
            bp = '{:X} {}'.format(length, password).encode()
            self.write(b'SKSETPWD ' + bp + b'\r\n', ['OK'])
            result = True
        return result

    def set_routeb_id(self, rbid):
        """ルートB ID設定"""
        if len(rbid) != 32:
            result = False
        else:
            self.write(b'SKSETRBID ' + rbid.encode() + b'\r\n', ['OK'])
            result = True
        return result

    def start_paa(self):
        """PAA開始"""
        self.write(b'SKSTART\r\n', ['OK'])

    def start_pac(self, ip6):
        """PaC開始"""
        res = self.write(b'SKJOIN ' + ip6.encode() + b'\r\n', [['EVENT 24', 'EVENT 25', 'FAIL ER10']],
                         ignore = True, timeout = 10)
        try:
            result = True if res[0]['COMMAND'] == 'EVENT 25' else False
            return result
        except:     # IndexErrorが発生するときのための暫定処理。要検討
            result = False

    def restart_pac(self):
        """PaCをリスタート"""
        res = self.write(b'SKREJOIN\r\n', [['EVENT 24', 'EVENT 25', 'FAIL ER10']], ignore = True, timeout = 10)
        try:
            result = True if res[0]['COMMAND'] == 'EVENT 25' else False
            return result
        except:     # IndexErrorが発生するときのための暫定処理。要検討
            result = False

    def pac_terminate(self):
        """PANAセッションを終了する"""
        res = self.write(b'SKTERM\r\n', [['OK', 'FAIL ER10']], ignore = True, timeout = 10)
        if res[0]['COMMAND'] == 'OK':
            return True
        else:
            return False

    def get_ip6(self, add):
        """IP6アドレス習得"""
        res = self.write(b'SKLL64 ' + add.encode() + b'\r\n', ['UNKNOWN'])
        return res[0]['MESSAGE'][0]

    def tcp_connect(self, ip6, rport, lport):
        """TCPコネクション開始"""
        br = ' {:04X}'.format(rport).encode()
        bl = ' {:04X}'.format(lport).encode()
        res = self.write(b'SKCONNECT ' + ip6.encode() + br + bl + b'\r\n', ['ETCP'])
        return res[0]

    def tcp_disconnect(self, handle):
        """TCPコネクション停止"""
        res = self.write(b'SKCLOSE ' + str(handle).encode() + b'\r\n', ['ETCP'])
        return res[0]['STATUS'] == 3

    def tcp_send(self, handle, message):
        """TCPで送信"""
        len_bt =' {:04X} '.format(len(message)).encode()
        res = self.write(b'SKSEND ' + str(handle).encode() + len_bt + message, ['ETCP'])
        return res[0]['STATUS'] == 5

    def udp_send(self, handle, ip6, security, port, message):
        """UDPで送信"""
        sec_bt = b' 1' if security else b' 0'
        len_bt = ' {:04X} '.format(len(message)).encode()
        port_bt = ' {:04X}'.format(port).encode()
        res = self.write(b'SKSENDTO ' + str(handle).encode() + b' ' + ip6.encode() + port_bt +
                         sec_bt + len_bt + message, ['EVENT 21', 'OK'])

        if res[0]['PARAM'] == '01':
            sys.stdout.write('[Error]: UDP transmission.\n')
            if self.get_tx_limit():
                sys.stdout.write('[Error]: TX limit.\n')
            return False
        else:
            return True     # 送信成功

    def ed_scan(self, duration = 4):
        """EDスキャン"""
        bd = '{:X}'.format(duration).encode()
        self.write(b'SKSCAN 0 FFFFFFFF ' + bd + b'\r\n', [['EEDSCAN'], ['OK']])
        res = []
        while True:
            if self.get_queue_size():
                msg = self.dequeue_message()
                res = msg['MESSAGE']
                break
            else:
                time.sleep(0.01)

        lqi_list = []
        for i in range(0, len(res), 2):
            lqi_list.append([int(res[i + 1], base=16), int(res[i], base=16)])  # [[LQI, channel], [LQI, channel],....]
            lqi_list.sort()  # LQIでソート
        return [lqi_list[0][1], lqi_list[0][0]]  # LQI最小チャンネル [channel, LQImin]

    def active_scan(self, duration = 6):
        """アクティブスキャン"""
        bd = '{:X}'.format(duration).encode()
        self.write(b'SKSCAN 2 FFFFFFFF ' + bd + b'\r\n')
        scan_end = False
        channel_list = []
        channel = {}

        try:
            while not scan_end:
                if self.get_queue_size():
                    msg_list = self.dequeue_message()
                    if msg_list['COMMAND'] == 'EVENT 20':
                        pass    # beacon 受信
                    elif msg_list['COMMAND'] == 'EPANDESC':
                        channel = {}
                    elif msg_list['COMMAND'] == 'ACTIVESCAN':
                        if 'Channel' in msg_list:
                            channel['Channel'] = msg_list['Channel']
                        elif 'Channel Page' in msg_list:
                            channel['Channel Page'] = msg_list['Channel Page']
                        elif 'Pan ID' in msg_list:
                            channel['Pan ID'] = msg_list['Pan ID']
                        elif 'Addr' in msg_list:
                            channel['Addr'] = msg_list['Addr']
                        elif 'LQI' in msg_list:
                            channel['LQI'] = msg_list['LQI']
                        elif 'PairID' in msg_list:
                            channel['PairID'] = msg_list['PairID']
                            channel_list.append(channel)
                    elif msg_list['COMMAND'] == 'EVENT 22':
                        scan_end = True
                else:
                    time.sleep(0.01)
        except KeyboardInterrupt:
            channel_list = False   # スキャンキャンセル

        return channel_list

    @staticmethod
    def parse_message(msg):
        """受信メッセージのパーサー"""
        msg_list = {}

        if msg.startswith('Channel Page'):
            msg_list['COMMAND'] = 'ACTIVESCAN'
            cols = msg.split(':')
            msg_list['Channel Page'] = int(cols[1], base=16)
            return msg_list

        if msg.startswith('Channel'):
            msg_list['COMMAND'] = 'ACTIVESCAN'
            cols = msg.split(':')
            msg_list['Channel'] = int(cols[1], base=16)
            return msg_list

        if msg.startswith('Pan ID'):
            msg_list['COMMAND'] = 'ACTIVESCAN'
            cols = msg.split(':')
            msg_list['Pan ID'] = int(cols[1], base=16)
            return msg_list

        if msg.startswith('Addr'):
            msg_list['COMMAND'] = 'ACTIVESCAN'
            cols = msg.split(':')
            msg_list['Addr'] = cols[1]
            return msg_list

        if msg.startswith('LQI'):
            msg_list['COMMAND'] = 'ACTIVESCAN'
            cols = msg.split(':')
            msg_list['LQI'] = int(cols[1], base=16)
            return msg_list

        if msg.startswith('PairID'):
            msg_list['COMMAND'] = 'ACTIVESCAN'
            cols = msg.split(':')
            msg_list['PairID'] = cols[1]
            return msg_list

        cols = msg.split()

        if cols[0] == 'OK':
            msg_list['COMMAND'] = cols[0]
            if len(cols) > 1:
                msg_list['MESSAGE'] = cols[1:len(cols)]
            return msg_list

        if cols[0] == 'EVENT':
            msg_list['COMMAND'] = cols[0] + ' ' + cols[1]
            msg_list['SENDER'] = cols[2]
            if len(cols) == 4:
                msg_list['PARAM'] = cols[3]

            return msg_list

        if cols[0] == 'ERXUDP':  # UDP
            msg_list['COMMAND'] = cols[0]
            msg_list['SENDER'] = cols[1]
            msg_list['DEST'] = cols[2]
            msg_list['RPORT'] = int(cols[3], base=16)
            msg_list['LPORT'] = int(cols[4], base=16)
            msg_list['SENDERLLA'] = cols[5]
            msg_list['SECURED'] = int(cols[6], base=16)
            msg_list['DATALEN'] = int(cols[7], base=16)
            msg_list['DATA'] = cols[8]
            return msg_list

        if cols[0] == 'ERXTCP':
            msg_list['COMMAND'] = cols[0]
            msg_list['SENDER'] = cols[1]
            msg_list['RPORT'] = int(cols[2], base=16)
            msg_list['LPORT'] = int(cols[3], base=16)
            msg_list['DATALEN'] = int(cols[4], base=16)
            msg_list['DATA'] = cols[5]
            return msg_list

        if cols[0] == 'ETCP':
            msg_list['COMMAND'] = cols[0]
            msg_list['STATUS'] = int(cols[1], base=16)
            msg_list['HANDLE'] = int(cols[2], base=16)
            if msg_list['STATUS'] == 1:
                msg_list['IPADDR'] = cols[3]
                msg_list['RPORT'] = int(cols[4], base=16)
                msg_list['LPORT'] = int(cols[5], base=16)
            return msg_list

        if cols[0] == 'ESREG':
            msg_list['COMMAND'] = cols[0]
            msg_list['VAL'] = cols[1]
            return msg_list

        if cols[0] == 'EPANDESC':
            msg_list['COMMAND'] = 'EPANDESC'
            return msg_list

        if cols[0] == 'EEDSCAN':
            msg_list['COMMAND'] = 'EEDSCAN'
            return msg_list

        # ローカルエコー停止前のローカルエコー対策: 'SKSREG SFE 0'
        if cols[0] == 'SKSREG':
            msg_list['COMMAND'] = 'SKSREG'
            msg_list['REG'] = cols[1]
            msg_list['VAL'] = cols[2]
            return msg_list

        # その他
        msg_list['COMMAND'] = 'UNKNOWN'  # unknown message
        msg_list['MESSAGE'] = cols
        #pprint(msg_list)    # debug
        return msg_list

    def enqueue_message(self, msg_list):
        """メッセージをリストに追加"""
        self.msg_list_lock.acquire()
        self.msg_list_queue.append(msg_list)
        self.msg_list_lock.release()

    def dequeue_message(self):
        """メッセージをリストから取り出す"""
        self.msg_list_lock.acquire()

        if self.msg_list_queue:
            result = self.msg_list_queue.pop(0)
        else:
            result = False

        self.msg_list_lock.release()

        return result

    def get_queue_size(self):
        """リスト内のメッセージ数"""
        return len(self.msg_list_queue)

    def uart_open(self, dev, baud, timeout):
        """UARTオープン"""
        try:
            self.uart_hdl = serial.Serial(dev, baud, timeout=timeout)
            self.uart_dev = dev
            self.uart_baud = baud
            return True
        except OSError as msg:
            sys.stdout.write('[Error]: {}\n'.format(msg))
            return False

    def uart_close(self):
        """UARTクローズ"""
        try:
            self.uart_hdl.close()
        except OSError as msg:
            sys.stdout.write('[Error]: {}\n'.format(msg))

    def write(self, send_msg, search_words = [], ignore = False, timeout = 0):
        """UART書き込み & 受信待ち
            send_msg: 送信データ: bytes
            search_word: 受信待ちコマンド
                (例) ['word1', 'word2', ['word31', 'word32']]: 'word1 -> 'word2' -> 'word31' or 'word32'
            ignore: 途中の受信データを無視する
            timeout: タイムアウト時間[s]
        """
        try:
            self.uart_hdl.write(send_msg)
            if search_words:
                self.search['found_word_list'] = []
                self.search['ignore_intermidiate'] = ignore
                self.search['start_time'] = time.time()
                self.search['timeout'] = timeout
                self.search['search_words'] = search_words    # run()で監視しているので、一番最後に設定する

                while self.search['search_words'] != []:
                    time.sleep(0.01)
                return self.search['found_word_list']

        except OSError as msg:
            sys.stdout.write('[Error]: {}\n'.format(msg))
            return False

    def read(self):
        """1行読み込み（文字列)"""
        try:
            res = self.uart_hdl.readline().decode().strip()
            #print('read:'+res)   # debug
            return res
        except OSError as msg:
            sys.stdout.write('[Error]: {}\n'.format(msg))
            return False

    def run(self):
        """UART受信用スレッド"""
        while not self.term_flag:
            msg = self.read()
            if msg:
                msg_list = self.parse_message(msg)

                # debug: UDP(PANA)の受信
                if msg_list['COMMAND'] == 'ERXUDP' and msg_list['LPORT'] == self.Y3_UDP_PANA_PORT:
                    #sys.stdout.write('[Note]: PANA message received.\n')
                    pass

                elif self.search['search_words']:     # サーチ中である
                    # サーチワードを受信した。
                    search_words = self.search['search_words'][0]
                    if isinstance(search_words, list):
                        for word in search_words:
                            if msg_list['COMMAND'].startswith(word):
                                self.search['found_word_list'].append(msg_list)
                                self.search['search_words'].pop(0)
                                break
                    elif msg_list['COMMAND'].startswith(search_words):
                        self.search['found_word_list'].append(msg_list)
                        self.search['search_words'].pop(0)

                    elif self.search['ignore_intermidiate']:
                        pass    # 途中の受信データを破棄

                    else:    # サーチワードではなかった
                        self.enqueue_message(msg_list)

                else:   # サーチ中ではない
                    self.enqueue_message(msg_list)

            elif self.search['timeout']:  # read()がタイムアウト，write()でタイムアウトが設定されている
                if time.time() - self.search['start_time'] > self.search['timeout']:
                    self.search['found_word_list'] = []
                    self.search['search_words'] = []
                    self.search['timeout'] = 0

    def terminate(self):
        """run()の停止"""
        self.term_flag = True
        self.join()


import os
import sys
import signal
import socket
import datetime

import ftrack_api

from openpype_modules.ftrack.ftrack_server.ftrack_server import FtrackServer
from openpype_modules.ftrack.ftrack_server.lib import (
    SocketSession,
    ProcessEventHub,
    TOPIC_STATUS_SERVER
)
from openpype.modules import ModulesManager

from openpype.lib import (
    Logger,
    get_openpype_version,
    get_build_version
)

subprocess_started = datetime.datetime.now()

class SessionFactory:
    session = None

def send_status(event):
    subprocess_id = event["data"].get("subprocess_id")
    if not subprocess_id:
        return

    if subprocess_id != os.environ["FTRACK_EVENT_SUB_ID"]:
        return

    session = SessionFactory.session
    if not session:
        return

    new_event_data = {
        "subprocess_id": subprocess_id,
        "source": "processor",
        "status_info": [
            ["created_at", subprocess_started.strftime("%Y.%m.%d %H:%M:%S")],
            ["OpenPype version", get_openpype_version() or "N/A"],
            ["OpenPype build version", get_build_version() or "N/A"]
        ]
    }

    new_event = ftrack_api.event.base.Event(
        topic="openpype.event.server.status.result",
        data=new_event_data
    )

    session.event_hub.publish(new_event)

def register(session):
    '''Registers the event, subscribing the discover and launch topics.'''
    session.event_hub.subscribe(
        "topic={}".format(TOPIC_STATUS_SERVER), send_status
    )

def main(args):
    log = Logger.get_logger("Event processor")

    port = int(args[-1])
    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Connect the socket to the port where the server is listening
    server_address = ("localhost", port)
    log.debug("Processor connected to {} port {}".format(*server_address))
    sock.connect(server_address)

    sock.sendall(b"CreatedProcess")

    returncode = 0
    try:
        session = SocketSession(
            auto_connect_event_hub=True, sock=sock, Eventhub=ProcessEventHub
        )
        register(session)
        SessionFactory.session = session

        manager = ModulesManager()
        ftrack_module = manager.modules_by_name["ftrack"]
        server = FtrackServer(
            ftrack_module.server_event_handlers_paths
        )
        log.debug("Launched Ftrack Event processor")
        server.run_server(session)

    except Exception:
        returncode = 1
        log.error("Event server crashed. See traceback below", exc_info=True)

    finally:
        log.debug("First closing socket")
        sock.close()
        return returncode

if __name__ == "__main__":
    # Register interupt signal
    def signal_handler(sig, frame):
        print("You pressed Ctrl+C. Process ended.")
        sys.exit(0)

    signal.signal(signal.SIGINT, signal_handler)
    signal.signal(signal.SIGTERM, signal_handler)

    sys.exit(main(sys.argv))


from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import *
from .forms import TrialFiltersForm

# Create your views here.
def homepage(request):
    clinical_trials = ContentTypeClinicalTrials.objects.filter(field_id_number_value__icontains="RPCEC").order_by(
        '-vid')
    rpcec = DatosInternosRpcec.objects.all()[0].cod_rpcec

    i = 1
    trial_list = []
    while i <= rpcec:
        j = i
        aux = 'RPCEC' + str(j).zfill(8)
        clinical_trial = clinical_trials.filter(field_id_number_value=aux)[0]
        trial_list.append(clinical_trial)
        i += 1

    context = {'trial_registrered': len(trial_list)}
    trial_anticipated = 0
    trial_actual = 0

    trial_year = {'trial_january': 0, 'trial_february': 0, 'trial_march': 0, 'trial_april': 0, 'trial_may': 0,
                  'trial_june': 0, 'trial_july': 0, 'trial_august': 0, 'trial_september': 0, 'trial_october': 0,
                  'trial_november': 0, 'trial_december': 0}

    for trial in trial_list:
        # Formatting date of registration
        if int(trial.field_id_number_value[5:]) <= 163:
            date_registration = str(trial.field_date_register_value[-2:]) + '/' + str(
                trial.field_date_register_value[5:7]) + '/' + str(trial.field_date_register_value[0:4])

        else:
            date_registration = str(trial.field_date_register_value)

        if date_registration[-4:] == '2021':
            if date_registration[-7:-5] == '01':
                trial_year['trial_january'] = trial_year['trial_january'] + 1

            if date_registration[-7:-5] == '02':
                trial_year['trial_february'] = trial_year['trial_february'] + 1

            if date_registration[-7:-5] == '03':
                trial_year['trial_march'] = trial_year['trial_march'] + 1

            if date_registration[-7:-5] == '04':
                trial_year['trial_april'] = trial_year['trial_april'] + 1

            if date_registration[-7:-5] == '05':
                trial_year['trial_may'] = trial_year['trial_may'] + 1

            if date_registration[-7:-5] == '06':
                trial_year['trial_june'] = trial_year['trial_june'] + 1

            if date_registration[-7:-5] == '07':
                trial_year['trial_july'] = trial_year['trial_july'] + 1

            if date_registration[-7:-5] == '08':
                trial_year['trial_august'] = trial_year['trial_august'] + 1

            if date_registration[-7:-5] == '09':
                trial_year['trial_september'] = trial_year['trial_september'] + 1

            if date_registration[-7:-5] == '10':
                trial_year['trial_october'] = trial_year['trial_october'] + 1

            if date_registration[-7:-5] == '11':
                trial_year['trial_november'] = trial_year['trial_november'] + 1

            if date_registration[-7:-5] == '12':
                trial_year['trial_december'] = trial_year['trial_december'] + 1

        # Formatting date enrolment
        date_enrolment = str(trial.field_date_first_enrollment_value)[8:10] + '/' + str(
            trial.field_date_first_enrollment_value)[5:7] + '/' + str(trial.field_date_first_enrollment_value)[
                                                                  0:4]
        # Setting type of enrolment
        if date_enrolment[-4:] < date_registration[-4:]:
            trial_actual = trial_actual + 1
        else:
            if date_enrolment[-4:] > date_registration[-4:]:
                trial_anticipated = trial_anticipated + 1
            else:
                if date_enrolment[-7:-5] < date_registration[-7:-5]:
                    trial_actual = trial_actual + 1
                else:
                    if date_enrolment[-7:-5] > date_registration[-7:-5]:
                        trial_anticipated = trial_anticipated + 1
                    else:
                        if date_enrolment[:2] >= date_registration[:2]:
                            trial_anticipated = trial_anticipated + 1
                        else:
                            trial_actual = trial_actual + 1

    context['trial_anticipated'] = trial_anticipated
    context['trial_actual'] = trial_actual
    context['trial_year'] = list(trial_year.values())

    return render(request, 'index.html', context)

class ClinicalTrialListView(ListView):
    model = ContentTypeClinicalTrials
    template_name = 'clinical_trial/clinical_trial_list.html'
    context_object_name = 'ClinicalTrials'

    def get_queryset(self):
        clinical_trials = ContentTypeClinicalTrials.objects.filter(field_id_number_value__icontains="RPCEC").order_by(
            '-vid')
        rpcec = DatosInternosRpcec.objects.all()[0].cod_rpcec

        i = 1
        trial_list = []

        recruitment_status = self.request.GET.get('recruitment_status_field')
        if recruitment_status:
            clinical_trials = clinical_trials.filter(field_recruitment_status_value=recruitment_status)

        phase = self.request.GET.get('phase_field')
        if phase:
            clinical_trials = clinical_trials.filter(field_phase_value=phase)

        covid_trial = self.request.GET.get('covid_trial_field')
        if covid_trial == "1":
            clinical_trials = clinical_trials.filter(field_scientific_title_value__icontains='COVID-19')

        while i <= rpcec:
            j = i
            aux = 'RPCEC' + str(j).zfill(8)
            try:
                clinical_trial = clinical_trials.filter(field_id_number_value=aux)[0]
                trial_list.append(clinical_trial)
            except:
                pass
            i += 1

        return trial_list

    def get_context_data(self, **kwargs):
        context = super(ClinicalTrialListView, self).get_context_data(**kwargs)
        context['form'] = TrialFiltersForm(initial={
            'recruitment_status_field': self.request.GET.get('recruitment_status_field'),
            'phase_field': self.request.GET.get('phase_field'),
            'covid_trial_field': self.request.GET.get('covid_trial_field'),
        })
        return context

class ClinicalTrialDetailView(DetailView):
    model = ContentTypeClinicalTrials
    template_name = 'clinical_trial/clinical_trial_detail.html'
    context_object_name = 'trial'

    def get_context_data(self, **kwargs):
        context = super(ClinicalTrialDetailView, self).get_context_data(**kwargs)

        context['object'].secondary_sponsors = ContentFieldSecSponsor.objects.filter(nid=context['object'].nid,
                                                                                vid=context['object'].vid)

        sec_ids = ContentFieldIdentifying.objects.filter(nid=context['object'].nid, vid=context['object'].vid)

        issuing_authorities = ContentFieldAuthorityIdentifying.objects.filter(nid=context['object'].nid,
                                                                              vid=context['object'].vid)
        secondary_ids = []
        for i in range(0, len(sec_ids)):
            secondary_id = {'sec_id': sec_ids[i].field_identifying_value,
                            'issuing_authorities': issuing_authorities[i].field_authority_identifying_value}
            secondary_ids.append(secondary_id)

        context['object'].secondary_ids = secondary_ids

        context['object'].principal_investigator_phone = ContentFieldInvestigatorPhone.objects.filter(
            nid=context['object'].nid,
            vid=context['object'].vid)

        context['object'].principal_investigator_email = ContentFieldEmailInvestigator.objects.filter(
            nid=context['object'].nid,
            vid=context['object'].vid)

        context['object'].countries = ContentFieldCountries.objects.filter(
            nid=context['object'].nid, vid=context['object'].vid)

        context['object'].clinical_sites = ContentFieldParticipatingSc.objects.filter(
            nid=context['object'].nid, vid=context['object'].vid)

        context['object'].ethics_committes = ContentFieldEthicsCommittes.objects.filter(
            nid=context['object'].nid, vid=context['object'].vid)

        context['object'].hc_codes = ContentFieldHcCode.objects.filter(
            nid=context['object'].nid, vid=context['object'].vid)

        context['object'].hc_keywords = ContentFieldHcKeyword.objects.filter(
            nid=context['object'].nid, vid=context['object'].vid)

        context['object'].i_codes = ContentFieldInterCode.objects.filter(
            nid=context['object'].nid, vid=context['object'].vid)

        context['object'].field_phone_public_value = ContentFieldPublicPhone.objects.filter(
            nid=context['object'].nid, vid=context['object'].vid)

        context['object'].field_email_public_value = ContentFieldEmailPublic.objects.filter(
            nid=context['object'].nid, vid=context['object'].vid)

        context['object'].field_phone_scientific_value = ContentFieldScientificPhone.objects.filter(
            nid=context['object'].nid, vid=context['object'].vid)

        context['object'].field_phone_scientific_value = ContentFieldEmailScientific.objects.filter(
            nid=context['object'].nid, vid=context['object'].vid)

        if context['object'].field_study_date_value:
            context['object'].field_study_date_value = str(context['object'].field_study_date_value[8:10]) + '/' + str(
                context['object'].field_study_date_value[5:7]) + '/' + str(
                context['object'].field_study_date_value[0:4])

        if context['object'].field_results_date_value:
            context['object'].field_results_date_value = str(
                context['object'].field_results_date_value[8:10]) + '/' + str(
                context['object'].field_results_date_value[5:7]) + '/' + str(
                context['object'].field_results_date_value[0:4])

        if context['object'].field_first_publication_value:
            context['object'].field_first_publication_value = str(
                context['object'].field_first_publication_value[8:10]) + '/' + str(
                context['object'].field_first_publication_value[5:7]) + '/' + str(
                context['object'].field_first_publication_value[0:4])

        ethics_reviews = ContentFieldResearchCommittee.objects.filter(nid=context['object'].nid,
                                                                      vid=context['object'].vid)

        ethics_committee = []
        for i in range(0, len(ethics_reviews)):
            ethic_committee = {}

            ethic_committee['name'] = ethics_reviews[i].field_research_committee_value
            ethics_status = ContentFieldEvaluationStatus.objects.get(nid=context['object'].nid,
                                                                     vid=context['object'].vid,
                                                                     delta=ethics_reviews[i].delta)
            ethic_committee['status'] = ethics_status.field_evaluation_status_value

            approval_date = ContentFieldDateCommittee.objects.get(nid=context['object'].nid, vid=context['object'].vid,
                                                                  delta=ethics_reviews[i].delta)
            if approval_date.field_date_committee_value:
                ethic_committee['approval_date'] = approval_date.field_date_committee_value[
                                                   8:10] + '/' + approval_date.field_date_committee_value[
                                                                 5:7] + '/' + approval_date.field_date_committee_value[
                                                                              0:4]
            else:
                ethic_committee['approval_date'] = None

            contact_address = ContentFieldZipCommittee.objects.get(nid=context['object'].nid, vid=context['object'].vid,
                                                                   delta=ethics_reviews[i].delta)
            ethic_committee['contact_address'] = contact_address.field_zip_committee_value

            try:
                contact_phone = ContentFieldCommmitteFhone.objects.get(nid=context['object'].nid,
                                                                       vid=context['object'].vid,
                                                                       delta=ethics_reviews[i].delta)
            except ContentFieldCommmitteFhone.DoesNotExist:
                contact_phone.field_commmitte_fhone_value = '-'

            ethic_committee['contact_phone'] = contact_phone.field_commmitte_fhone_value

            try:
                contact_email = ContentFieldCommmitteMail.objects.get(nid=context['object'].nid,
                                                                      vid=context['object'].vid,
                                                                      delta=ethics_reviews[i].delta)
            except ContentFieldCommmitteMail.DoesNotExist:
                contact_email.field_commmitte_mail_value = '-'

            ethic_committee['contact_email'] = contact_email.field_commmitte_mail_value

            ethics_committee.append(ethic_committee)

        context['object'].ethics_committee = ethics_committee
        return context


"""Define models for pages."""
import os

from django.conf import settings
from django.db import models
from django.template.loaders.app_directories import get_app_template_dirs
from django.utils.translation import ugettext_lazy as _
from wagtail.admin.edit_handlers import FieldPanel
from wagtail.core.models import Page as WagtailPage

def get_templates_choices():
    """
    Return a list of templates as tuples usable as choices.

    Every file within a "pages" template directory that does not start
    with a dot (.) is included. Example, given the following files:
    - an_app/templates/pages/template.html
    - an_app/templates/pages/subdirectory/template.js
    - different_app/templates/pages/template.html

    The returned tuple would be:
    ```
    (
        ("pages/template.html", "pages/template.html"),
        ("pages/subdirectory/template.js", "pages/subdirectory/template.js"),
    )
    ```
    """
    template_dir_list = []
    for template_dir in get_app_template_dirs("templates"):
        if settings.BASE_DIR in template_dir:
            template_dir_list.append(template_dir)

    templates = set()
    for template_dir in template_dir_list + settings.TEMPLATES[0]["DIRS"]:
        full_path = os.path.join(template_dir, "pages")
        for base_dir, dirnames, filenames in os.walk(full_path):
            rel_dir = os.path.relpath(base_dir, template_dir)
            for filename in filenames:
                if not filename.startswith("."):
                    templates.add(os.path.join(rel_dir, filename))

    template_list = []
    for template in templates:
        template_list.append((template, template))

    return template_list

class Page(WagtailPage):
    """Define a page that uses a user selected template."""

    TEMPLATE_HELP_TEXT = _("Template to be used to render the page.")
    custom_template = models.CharField(
        max_length=255,
        blank=True,
        help_text=TEMPLATE_HELP_TEXT,
        verbose_name=_("Template"),
    )
    settings_panels = WagtailPage.settings_panels + [FieldPanel("custom_template")]

    def __init__(self, *args, **kwargs):
        """Set template based on custom_template property."""
        super().__init__(*args, **kwargs)

        self._meta.get_field("custom_template").choices = [
            ("", _("Default (%(template)s)") % {"template": self.template}),
            [_("Template override"), get_templates_choices()],
        ]

        if self.custom_template:
            self.template = self.custom_template


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

# # Importing Libraries

# These are just a few examples of popular Python libraries. You can import any other library using the same import statement followed by the library name or alias:

# NumPy: for numerical operations and array manipulation
#
# Pandas: for data manipulation and analysis
#
# Matplotlib: for creating visualizations
#
# Scikit-learn: for machine learning algorithms

# In[1]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
get_ipython().run_line_magic('matplotlib', 'inline')

# # Reading Dataset

# In[2]:

from tensorflow.keras.datasets import cifar10

# # Splitting Dataset

# Dataset splitting is an important step in machine learning and data analysis. It involves dividing a dataset into two or more subsets to train and evaluate a model effectively. The most common type of dataset splitting is into training and testing subsets.

# Train-Test Split: This is the most basic type of split, where the dataset is divided into a training set and a testing set. The training set is used to train the machine learning model, while the testing set is used to evaluate its performance. The split is typically done using a fixed ratio, such as 80% for training and 20% for testing.

# In[3]:

(x_train, y_train), (x_test, y_test) = cifar10.load_data()

# In[5]:

x_train.shape, y_train.shape, x_test.shape, y_test.shape

# In[6]:

plt.imshow(x_train[20])

# In[7]:

x_train[20]

# In[8]:

x_train.min(), x_train.max()

# In[9]:

x_train = x_train / 255
x_test = x_test / 255

# In[10]:

x_train.shape, x_test.shape

# # Converting To Categorical

# Converting data to categorical format is often necessary for certain machine learning tasks, especially when dealing with categorical or nominal variables. This conversion is typically performed to represent the categorical variables as numeric values that can be processed by machine learning algorithms.

# In[11]:

from tensorflow.keras.utils import to_categorical

# In[12]:

y_train_categorical = to_categorical(y_train)
y_test_categorical = to_categorical(y_test)

# In[13]:

y_train_categorical

# In[14]:

y_test_categorical

# # Buliding Model

# Creating a deep learning model involves defining the architecture and structure of the neural network, specifying the layers, and configuring the parameters for training.

# In[15]:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten

# In[16]:

from tensorflow.keras.callbacks import EarlyStopping

# In[17]:

from sklearn.metrics import classification_report, confusion_matrix

# In[18]:

model = Sequential()
model.add(Conv2D(filters = 32, input_shape = (32, 32, 3), kernel_size = (4, 4), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))

model.add(Conv2D(filters = 32, input_shape = (32, 32, 3), kernel_size = (4, 4), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))

model.add(Flatten())

model.add(Dense(256, activation = 'relu'))

model.add(Dense(10, activation = 'softmax'))

# In[19]:

model.summary()

# # Compiling Model

# Compiling the model in deep learning involves configuring essential components that define how the model will be trained.

# In[20]:

model.compile(optimizer = 'rmsprop', loss = 'categorical_crossentropy', metrics = ['accuracy'])

# In[21]:

early_stopping = EarlyStopping(monitor = 'val_loss', patience = 3)

# # Training Model

# Training the model in deep learning involves the process of iteratively updating the model's parameters (weights and biases) based on the provided training data to minimize the loss function and improve the model's performance

# In[23]:

model.fit(x_train, y_train_categorical,
          validation_data = (x_test, y_test_categorical),
          epochs = 20,
          callbacks = [early_stopping])

# In[24]:

model.save('CNN_model_for_CIFAR10.h5')

# In[25]:

loss = pd.DataFrame(model.history.history)

# In[26]:

loss.head()

# In[27]:

loss.plot()

# In[28]:

loss[['loss', 'val_loss']].plot()

# In[29]:

loss[['accuracy', 'val_accuracy']].plot()

# In[34]:

prediction = model.predict(x_test)
prediction_classes = prediction.argmax(axis = 1)

# # Error Analysis

# Error analysis is an important step in evaluating and improving the performance of a machine learning model. It involves analyzing the errors made by the model during predictions to gain insights into the patterns, sources, and potential improvements.

# In[35]:

print(classification_report(y_test, prediction_classes))

# In[37]:

print(confusion_matrix(prediction_classes, y_test))

# In[41]:

plt.figure(figsize = (12, 6))
sns.heatmap(confusion_matrix(prediction_classes, y_test),
            annot = True,
            cmap = 'coolwarm')

# # Prediction

# In[57]:

prediction = x_train[5]

# In[58]:

plt.imshow(prediction)

# In[59]:

predictions = model.predict(prediction.reshape(1, 32, 32, 3))
predicted_class = np.argmax(predictions)

print("Predicted class:", predicted_class)

CNN on CIFAR10 Dataset/CNN on CIFAR10 Dataset.py


# @Author  : Willpower-chen
# @blog: http://www.cnblogs.com/willpower-chen/
from utils.response import BaseResponse
from .base import BaseServiceList
from repository import models
from utils.pager import PageInfo

class Business(BaseServiceList):
	def __init__(self):
		condition_config = [
			{'name': 'name', 'text': '业务线', 'condition_type': 'input'},
			# {'name': 'name', 'text': '业务联系人', 'condition_type': 'input'},
			{'name': 'manager__name', 'text': '系统管理员', 'condition_type': 'input'},
		]
		table_config = [
			{
				'q': 'id',  # 用于数据库查询的字段，即Model.Tb.objects.filter(*[])
				'title': "ID",  # 前段表格中显示的标题
				'display': 1,  # 是否在前段显示，0表示在前端不显示, 1表示在前端隐藏, 2表示在前段显示
				'text': {'content': "{id}", 'kwargs': {'id': '@id'}},
				'attr': {}  # 自定义属性
			},
			{
				'q': 'name',
				'title': "业务线",
				'display': 1,
				'text': {'content': "{n}", 'kwargs': {'n': '@name'}},
				'attr': {'edit-enable':'true','edit-type':'input'}  # 自定义属性
			},
			# {
			# 	'q': 'contact__name',
			# 	'title': "业务联系人",
			# 	'display': 1,
			# 	'text': {'content': "{n}", 'kwargs': {'n': '@contact__name'}},
			# 	'attr': {'edit-enable': 'true', 'edit-type': 'input'}  # 自定义属性
			# },
			{
				'q': 'manager__name',
				'title': "系统管理员",
				'display': 1,
				'text': {'content': "{n}", 'kwargs': {'n': '@manager__name'}},
				'attr': {'edit-enable': 'true', 'edit-type': 'input'}  # 自定义属性
			},
			{
				'q': 'manager_id',
				'title': "系统管理员ID",
				'display': 0,
				'text': {'content': "{n}", 'kwargs': {'n': '@manager_id'}},
				'attr': {}  # 自定义属性
			},
			{
				'q': None,
				'title': "选项",
				'display': 1,
				'text': {
					'content': "查看详细",
					'kwargs': {'nid': '@id'}},
				'attr': {}
			},
		]
		extra_select = {}
		super(Business, self).__init__(condition_config, table_config, extra_select)

	def fetch_business(self, request):
		response = BaseResponse()
		try:
			ret = {}
			conditions = self.assets_condition(request)

			asset_count = models.BusinessUnit.objects.filter(conditions).count()
			page_info = PageInfo(request.GET.get('pager', None), asset_count)

			asset_list = models.BusinessUnit.objects.filter(conditions).extra(select=self.extra_select).values(
				*self.values_list)[page_info.start:page_info.end]

			ret['table_config'] = self.table_config
			ret['condition_config'] = self.condition_config
			ret['data_list'] = list(asset_list)
			ret['page_info'] = {
				"page_str": page_info.pager(),
				"page_start": page_info.start,
			}
			ret['global_dict'] = {}
			response.data = ret
			response.message = '获取成功'
		except Exception as e:
			response.status = False
			response.message = str(e)

		return response


#!/usr/bin/env python

# 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.

__author__ = "John Wieczorek"
__contributors__ = "Aaron Steele, John Wieczorek"
__copyright__ = "Copyright 2018 vertnet.org"
__version__ = "util.py 2018-04-07T20:26-03:00"

import json
import logging

UTIL_VERSION=__version__

ADD_TO_DOWNLOAD_RESULTS = ['url', 'citation', 'gbifdatasetid', 'gbifpublisherid', 'email',
    'contact', 'pubdate', 'lastindexed', 'migrator', 'hasmedia', 'hastissue',
    'wascaptive', 'isfossil', 'isarch', 'vntype', 'haslength']

OMIT_FROM_DOWNLOADS = ['location', 'record', 'verbatim_record', 'count', 'icode',
    'title', 'orgname', 'emlrights', 'networks', 'hashid', 'rank', 'haslicense',
    'mappable', 'source_url', 'hastypestatus', 'language', 'iptrecordid',
    'ownerinstitutioncode', 'organismquantity', 'organismquantitytype', 'parenteventid',
    'samplesizevalue', 'samplesizeunit', 'pointradiusspatialfit', 'footprintspatialfit',
    'taxonid', 'acceptednameusageid', 'parentnameusageid', 'originalnameusageid',
    'nameaccordingtoid', 'taxonconceptid', 'parentnameusage', 'nameaccordingto',
    'nomenclaturalstatus']

TRANSLATE_HEADER = {'pubdate':'dataset_pubdate', 'url':'dataset_url',
    'title':'dataset_title', 'contact':'dataset_contact', 'email':'dataset_contact_email',
    'citation':'dataset_citation', 'dctype':'type', 'migrator':'migrator_version'}

DWC_RECLEVEL = ['DCType', 'Modified', 'Language', 'License', 'RightsHolder',
    'AccessRights', 'BibliographicCitation', 'References', 'InstitutionID',
    'CollectionID', 'DatasetID', 'InstitutionCode', 'CollectionCode', 'DatasetName',
    'OwnerInstitutionCode', 'BasisOfRecord', 'InformationWithheld',
    'DataGeneralizations', 'DynamicProperties']

DWC_OCC = ['OccurrenceID', 'CatalogNumber', 'RecordNumber', 'RecordedBy',
    'IndividualCount', 'OrganismQuantity', 'OrganismQuantityType', 'Sex', 'LifeStage',
    'ReproductiveCondition', 'Behavior', 'EstablishmentMeans', 'OccurrenceStatus',
    'Preparations', 'Disposition', 'AssociatedMedia', 'AssociatedReferences',
    'AssociatedSequences', 'AssociatedTaxa', 'OtherCatalogNumbers', 'OccurrenceRemarks']

DWC_ORGANISM = ['OrganismID', 'OrganismName', 'OrganismScope', 'AssociatedOccurrences',
    'AssociatedOrganisms', 'PreviousIdentifications', 'OrganismRemarks']

DWC_SAMPLE = ['MaterialSampleID']

DWC_EVENT = ['EventID', 'parentEventID', 'FieldNumber', 'EventDate', 'EventTime',
    'StartDayOfYear', 'EndDayOfYear', 'Year', 'Month', 'Day', 'VerbatimEventDate',
    'Habitat', 'SamplingProtocol', 'SampleSizeValue', 'SampleSizeUnit', 'SamplingEffort',
    'FieldNotes', 'EventRemarks']

DWC_LOCATION = ['LocationID', 'HigherGeographyID', 'HigherGeography', 'Continent',
    'WaterBody', 'IslandGroup', 'Island', 'Country', 'CountryCode', 'StateProvince',
    'County', 'Municipality', 'Locality', 'VerbatimLocality', 'MinimumElevationInMeters',
    'MaximumElevationInMeters', 'VerbatimElevation', 'MinimumDepthInMeters',
    'MaximumDepthInMeters', 'VerbatimDepth', 'MinimumDistanceAboveSurfaceInMeters',
    'MaximumDistanceAboveSurfaceInMeters', 'LocationAccordingTo', 'LocationRemarks',
    'DecimalLatitude', 'DecimalLongitude', 'GeodeticDatum',
    'CoordinateUncertaintyInMeters', 'CoordinatePrecision', 'PointRadiusSpatialFit',
    'VerbatimCoordinates', 'VerbatimLatitude', 'VerbatimLongitude',
    'VerbatimCoordinateSystem', 'VerbatimSRS', 'FootprintWKT', 'FootprintSRS',
    'FootprintSpatialFit', 'GeoreferencedBy', 'GeoreferencedDate',
    'GeoreferenceProtocol', 'GeoreferenceSources', 'GeoreferenceVerificationStatus',
    'GeoreferenceRemarks']

DWC_GEO = ['GeologicalContextID', 'EarliestEonOrLowestEonothem',
    'LatestEonOrHighestEonothem', 'EarliestEraOrLowestErathem',
    'LatestEraOrHighestErathem', 'EarliestPeriodOrLowestSystem',
    'LatestPeriodOrHighestSystem', 'EarliestEpochOrLowestSeries',
    'LatestEpochOrHighestSeries', 'EarliestAgeOrLowestStage',
    'LatestAgeOrHighestStage', 'LowestBiostratigraphicZone',
    'HighestBiostratigraphicZone', 'LithostratigraphicTerms', 'Group', 'Formation',
    'Member', 'Bed']

DWC_ID = ['IdentificationID', 'IdentificationQualifier', 'TypeStatus', 'IdentifiedBy',
    'DateIdentified', 'IdentificationReferences', 'IdentificationVerificationStatus',
    'IdentificationRemarks']

DWC_TAXON = ['TaxonID', 'ScientificNameID', 'AcceptedNameUsageID', 'ParentNameUsageID',
    'OriginalNameUsageID', 'NameAccordingToID', 'NamePublishedInID', 'TaxonConceptID',
    'ScientificName', 'AcceptedNameUsage', 'ParentNameUsage', 'OriginalNameUsage',
    'NameAccordingTo', 'NamePublishedIn', 'NamePublishedInYear', 'HigherClassification',
    'Kingdom', 'Phylum', 'Class', 'Order', 'Family', 'Genus', 'Subgenus',
    'SpecificEpithet', 'InfraspecificEpithet', 'TaxonRank', 'VerbatimTaxonRank',
    'ScientificNameAuthorship', 'VernacularName', 'NomenclaturalCode', 'TaxonomicStatus',
    'NomenclaturalStatus', 'TaxonRemarks']

VN_TRAIT = ['LengthInMM', 'LengthType', 'LengthUnitsInferred', 'MassInG', 'MassUnitsInferred',
    'UnderivedLifeStage', 'UnderivedSex']

DWC_ALL = DWC_RECLEVEL + DWC_OCC + DWC_ORGANISM + DWC_SAMPLE + DWC_EVENT + DWC_LOCATION \
    + DWC_GEO + DWC_ID + DWC_TAXON + VN_TRAIT

DWC_ALL_LOWER = [x.lower() for x in DWC_ALL]
DWC_HEADER_LIST = DWC_ALL_LOWER + ADD_TO_DOWNLOAD_RESULTS
DWC_HEADER = '\t'.join(DWC_HEADER_LIST)

def format_json(json):
    return json.replace('""{','{').replace('}""','}').replace('""','"')

def download_field_list():
    """Create a list of the fields in a download. These are the original field names."""
    field_list = []
    for f in DWC_HEADER_LIST:
        field_list.append(f)
    for f in OMIT_FROM_DOWNLOADS:
        try:
            field_list.remove(f)
        except:
            pass
#    logging.debug('%s: FIELD_LIST: %s' % (__version__, field_list))
    return field_list

def download_header():
    """Create a list of the fields in a download. These are the original field names."""
    fieldlist=download_field_list()
    translated = []
    for f in fieldlist:
        if f in TRANSLATE_HEADER:
            translated.append(TRANSLATE_HEADER[f])
        else:
            translated.append(f)
    return '\t'.join(translated)

def classify(record):
    result = dict(meta=record, reclevel={}, occ={}, organism={}, sample={}, event={},
        loc={}, geo={}, id={}, taxon={})

    for term in DWC_RECLEVEL:
        if record.has_key(term.lower()):
            result['reclevel'][term] = record[term.lower()]

    for term in DWC_OCC:
        if record.has_key(term.lower()):
            result['occ'][term] = record[term.lower()]

    for term in DWC_ORGANISM:
        if record.has_key(term.lower()):
            result['organism'][term] = record[term.lower()]

    for term in DWC_SAMPLE:
        if record.has_key(term.lower()):
            result['sample'][term] = record[term.lower()]

    for term in DWC_EVENT:
        if record.has_key(term.lower()):
            result['event'][term] = record[term.lower()]

    for term in DWC_LOCATION:
        if record.has_key(term.lower()):
            result['loc'][term] = record[term.lower()]

    for term in DWC_GEO:
        if record.has_key(term.lower()):
            result['geo'][term] = record[term.lower()]

    for term in DWC_ID:
        if record.has_key(term.lower()):
            result['id'][term] = record[term.lower()]

    for term in DWC_TAXON:
        if record.has_key(term.lower()):
            result['taxon'][term] = record[term.lower()]

    return result

def search_resource_counts(recs, old_res_counts=None):
    # Build dictionary of resources with their record counts
    res_counts = {}
    gbifdatasetid = None
    for rec in recs:
        if 'gbifdatasetid' in rec:
            gbifdatasetid=rec['gbifdatasetid']
        else:
            gbifdatasetid=rec[TRANSLATE_HEADER['gbifdatasetid']]
        if gbifdatasetid not in res_counts:
            res_counts[gbifdatasetid] = 1
        else:
            res_counts[gbifdatasetid] += 1
    return res_counts


from pbr import version as pbr_version

HSM_VENDOR = "Intel"
HSM_PRODUCT = "Intel"
HSM_PACKAGE = None

loaded = False
version_info = pbr_version.VersionInfo('hsm')
version_string = version_info.version_string


import argparse
from core.main import Main

def _get_args():
    parser = argparse.ArgumentParser(
        description='Calculates distances between a dataset of audio files',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    parser.add_argument("in_path",
                        action="store",
                        help="Name of database")
    args = parser.parse_args()
    return args.in_path

if __name__ == '__main__':
    print('######################################')

    db_name = _get_args()
    if not db_name.endswith('.sqlite'):
        print('Provide a proper database file name. (for eg: db.sqlite)')
    else:
        main_obj = Main(db_name)
        main_obj.calc_feature_new()
        main_obj.show_similarity()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import sys
import pandas as pd
import config

def word2id(content_data, label_data):
    '''content_data的word2id'''
    word_list = []
    for idx, sentence in enumerate(content_data):
        for word in sentence:
            if word not in word_list:
                word_list.append(word)
    ids = range(len(word_list))
    word2id = pd.Series(ids, index=word_list).to_dict()
    for sentence_idx, sentence in enumerate(content_data):
        for word_idx, word in enumerate(sentence):
            if word in word2id.keys():
                content_data[sentence_idx][word_idx] = word2id[word] + 1
    '''label_data的word2id'''
    label_list = []
    for idx, sentence in enumerate(label_data):
        for word in sentence:
            if word not in label_list:
                label_list.append(word)
    ids = range(len(label_list))
    label2id = pd.Series(ids, index=label_list).to_dict()
    for sentence_idx, sentence in enumerate(label_data):
        for word_idx, word in enumerate(sentence):
            if word in label2id.keys():
                #之后需添加开始/结尾标识，分别为1和2
                label_data[sentence_idx][word_idx] = label2id[word] + 1
    return content_data, label_data

def data_prepare():
    content_data = np.load(r'C:\Users\lenovo\Desktop\Josie\自学\哥哥的\论文实验_transformer实现多分类\data\symptoms.npy',
                           allow_pickle=True)
    label_data = np.load(r'C:\Users\lenovo\Desktop\Josie\自学\哥哥的\论文实验_transformer实现多分类\data\herbs.npy',
                         allow_pickle=True)
    content_data, label_data = word2id(content_data, label_data)
    def max_len(content_data):
        max_len = 0
        for sentence_idx, sentence in enumerate(content_data):
            if max_len < len(sentence):
                max_len = len(sentence)
        return max_len
    '''用max_len补齐content_data'''
    for sentence in content_data:
        if len(sentence) < max_len(content_data):
            sentence += [0] * (max_len(content_data) - len(sentence))
    '''label_data也不一样长，同理需补齐label_data(begin:1, end:2, 剩余:0)
    算loss值时需是三维[batch_size, max_len, num_dim]'''
    new_label_data = []
    for sentence in label_data:
        #补齐开始/结尾标识
        new_label_data.append([1] + sentence + [2])
    # label_data = new_label_data
    for sentence in label_data:
        if len(sentence) < config.num_category:
            sentence += [0] * (config.num_category - len(sentence))
    return list(content_data), list(label_data)


from telethon.errors.rpcerrorlist import PhoneCodeInvalidError, PhoneCodeExpiredError
from Header import CreateSimList, ClientConnect
from colorama import Fore

sim_cards = CreateSimList()  # reads the sim cards from the list
i = 0

while i < len(sim_cards):
    try:
        ClientConnect(sim_cards[i])
        i += 1

    except PhoneCodeInvalidError:
        print(Fore.RED + "Code Invalid! Try Again")

    except PhoneCodeExpiredError:
        print(Fore.RED + "Code expired after 5 minutes! Try Again")


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 10 20:45:03 2018

@author: patrickmcfarlane

scoreboard.py contains the ScoreBoard class that
enables API calls for two score board endpoints
"""

from .utils import api_call, parse_api_call
from datetime import datetime

class ScoreBoard:
    """ The ScoreBoard class contains all resources needed to use the
    score board API calls. `stats.nba.com <https://stats.nba.com>`_
    has the following score board API endpoints:

        - **scoreboard**: Live scores across the league, with series metadata \
        and standings information.
        - **scoreboardv2**: Live scores across the league, with series metadata \
        and standings information. Also, scoreboardv2 has ticket, win \
        probability, and team leader information.

    The ScoreBoard class has the following required parameters:

        @param **headers** (*dict*): Dictionary of request header information
            required by the API. Specifically, the API requires you to declare
            the 'User-Agent' key of the request header dictionary. More information
            can be found `here <https://stackoverflow.com/questions/46781563/
            how-to-obtain-a-json-response-from-the-stats-nba-com-api>`_ and
            an example request header dictionary can be found in the __init__.py
            file in the tests folder of this module.

        @param **league_id** (*str*): LeagueID in the API. String of a \
            two-digit number corresponding to the league. '00' is the NBA, \
            '10' is the WNBA, '01' is the ABA, and '20' is the G-League.

        @param **game_date** (*str*): GameDate in the API. String of a \
            date formatted as 'MM/DD/YYYY' representing the date for \
            which data is desired.

        @param **day_offset** (*str*): DayOffset in the API. String of an \
            integer representing days from or before the date given in \
            **game_date** for which data is desired. Positive values \
            indicate days into the future, zero represents the current \
            day, and negative values indicate days into the past.

    Attributes:

        **api_resp** (*dict*): JSON object of the API response. The API \
            response has three keys. The 'resource' key describes the \
            type of response returned (the endpoint in this instance). \
            The 'parameters' key describes the parameters provided in \
            the API call. The 'resultSets' key contains the data returned \
            in the API call.

        **data** (*dict*): A dictionary of response names. Each response \
            name is a key to a list of dictionaries containing the \
            corresponding data.
    """

    cur_day = datetime.today().day
    cur_month = datetime.today().month
    cur_year = str(datetime.today().year)
    if cur_day < 10:
        cur_day = '0' + str(cur_day)
    else:
        cur_day = str(cur_day)

    if cur_month < 10:
        cur_month = '0' + str(cur_month)
    else:
        cur_month = str(cur_month)

    current_date = cur_month + '/' + \
                   cur_day + '/' + \
                   cur_year

    def __init__(self, headers, endpoint='scoreboard',
                 league_id='00', game_date=current_date,
                 day_offset='0'):

        # Controlling the parameters depending on the endpoint
        params = {'LeagueID': league_id,
                  'GameDate': game_date,
                  'DayOffset': day_offset}

        self.api_resp = api_call(endpoint=endpoint,
                                 params=params,
                                 headers=headers)

        # Storing the API response in a dictionary called data
        # The results can come in different formats, namely a
        # dictionary keyed by either resultSets or resultSet
        self.data = parse_api_call(self.api_resp)


import os
import sys
import logging
from src import protocols

__version__ = 0.1

# Set up logger.
logger = logging.getLogger(__name__)

class View():
    """
       Presentation class for the application.
    """

    def __init__(self, prompt: str = 'Press enter to return to main menu.',
                 *args, **kwargs) -> None:
        '''
           Initializes the OptionsMenu.
        '''

        super().__init__(*args, **kwargs)
        self.prompt = prompt
        self.controller_service = self.noActions

    def refresh_screen(self):
        '''
           Refreshes screen for windows and linux.
        '''
        if sys.platform == 'win32' or 'cygwin':
            os.system('cls')
        else:
            os.system('clear')

    def bind_provider(self, call: protocols.ControllerPresent) -> None:
        '''
           Binds the service provider.
        '''

        logger.info(f'{self.__class__} binded to\
                     selection method {call.__name__}')
        self.controller_service = call

    def noActions(self, *args, **kwargs) -> str:
        logger.info(f'{self.__class__} tried to run but no method binded')
        print("No method binded")
        return ''

    def mainloop(self):
        '''
           Prints options and returns user input.
        '''

        logger.info(f'{self.__class__} running.')
        # Refreshes screen.
        self.refresh_screen()

        # Prompts user for choice of option.
        print()
        input(self.controller_service() + '\n\n' + self.prompt)

        logger.info(f'{self.__class__} closing.')

if __name__  == '__main__':
    menu = View()
    menu.bind_provider(lambda: f'This is a presentation')
    menu.mainloop()


from django.shortcuts import render, redirect
from .forms import MyRegisterForm
from django.contrib import messages
from .models import RegisterForm

# Create your views here.
def home(request):
    newdata = RegisterForm.objects.all()
    if (newdata != ''):
        return render(request,"home.html", {'data':newdata})
    else:
        return render(request, 'home.html')

def register(request):
    if request.method == 'POST':
        form = MyRegisterForm(request.POST)
        if form.is_valid():
            try:
                form.save()
                messages.success(request,'Registration completed successfully!')
                return redirect ("home")
            except:
                pass
    else:
        form = MyRegisterForm()
    return render(request, "register.html", {'form':form})

def update(request, id):
    data = RegisterForm.objects.get(id =id)
    if request.method == 'POST':
        name = request.POST['name']
        age = request.POST['age']
        event = request.POST['event']
        contact = request.POST['contact']
        email = request.POST['email']

        data.Name = name
        data.Age = age
        data.Event_Name = event
        data.Contact_Number = contact
        data.Email_ID = email
        data.save()
        messages.success(request,'Registration updated successfully!')
        return redirect('home')

    return render(request, "update.html", {'data':data})

def delete(request, id):
    data = RegisterForm.objects.get(id =id)
    data.delete()
    messages.error(request,'Deleted the record successfully!')
    return redirect('home')


from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals

import collections
import errno
import logging
import multiprocessing
import threading
import traceback

from gslib.utils import constants
from gslib.utils import system_util
from six.moves import queue as Queue

# pylint: disable=g-import-not-at-top
try:
  # This module doesn't necessarily exist on Windows.
  import resource
  _HAS_RESOURCE_MODULE = True
except ImportError as e:
  _HAS_RESOURCE_MODULE = False

# Maximum time to wait (join) on the SeekAheadThread after the ProducerThread
# completes, in seconds.
SEEK_AHEAD_JOIN_TIMEOUT = 60

# Timeout for puts/gets to the global status queue, in seconds.
STATUS_QUEUE_OP_TIMEOUT = 5

# Maximum time to wait (join) on the UIThread after the Apply
# completes, in seconds.
UI_THREAD_JOIN_TIMEOUT = 60

ZERO_TASKS_TO_DO_ARGUMENT = ('There were no', 'tasks to do')

# Multiprocessing manager used to coordinate across all processes. This
# attribute is only present if multiprocessing is available, which can be
# determined by calling CheckMultiprocessingAvailableAndInit().
global top_level_manager  # pylint: disable=global-at-module-level

# Cache the values from this check such that they're available to all callers
# without needing to run all the checks again (some of these, such as calling
# multiprocessing.Manager(), are expensive operations).
_cached_multiprocessing_is_available = None
_cached_multiprocessing_check_stack_trace = None
_cached_multiprocessing_is_available_message = None

# This must be defined at the module level for pickling across processes.
MultiprocessingIsAvailableResult = collections.namedtuple(
    'MultiprocessingIsAvailableResult', ['is_available', 'stack_trace'])

# Explicitly set start method to 'fork' since this isn't always the default
# in later versions of Python.
try:
  multiprocessing_context = multiprocessing.get_context('fork')
except (AttributeError, ValueError):
  multiprocessing_context = multiprocessing

class AtomicDict(object):
  """Thread-safe (and optionally process-safe) dictionary protected by a lock.

  If a multiprocessing.Manager is supplied on init, the dictionary is
  both process and thread safe. Otherwise, it is only thread-safe.
  """

  def __init__(self, manager=None):
    """Initializes the dict.

    Args:
      manager: (multiprocessing.Manager or None) Manager instance (required for
          cross-process safety), or none if cross-process safety is not needed.
    """
    if manager:
      self.lock = manager.Lock()
      self.dict = manager.dict()
    else:
      self.lock = threading.Lock()
      self.dict = {}

  def __getitem__(self, key):
    with self.lock:
      return self.dict[key]

  def __setitem__(self, key, value):
    with self.lock:
      self.dict[key] = value

  # pylint: disable=invalid-name
  def get(self, key, default_value=None):
    with self.lock:
      return self.dict.get(key, default_value)

  def delete(self, key):
    with self.lock:
      del self.dict[key]

  def values(self):
    with self.lock:
      return self.dict.values()

  def Increment(self, key, inc, default_value=0):
    """Atomically updates the stored value associated with the given key.

    Performs the atomic equivalent of
    dict[key] = dict.get(key, default_value) + inc.

    Args:
      key: lookup key for the value of the first operand of the "+" operation.
      inc: Second operand of the "+" operation.
      default_value: Default value if there is no existing value for the key.

    Returns:
      Incremented value.
    """
    with self.lock:
      val = self.dict.get(key, default_value) + inc
      self.dict[key] = val
      return val

class ProcessAndThreadSafeInt(object):
  """This class implements a process and thread-safe integer.

  It is backed either by a multiprocessing Value of type 'i' or an internal
  threading lock.  This simplifies the calling pattern for
  global variables that could be a Multiprocessing.Value or an integer.
  Without this class, callers need to write code like this:

  global variable_name
  if isinstance(variable_name, int):
    return variable_name
  else:
    return variable_name.value
  """

  def __init__(self, multiprocessing_is_available):
    self.multiprocessing_is_available = multiprocessing_is_available
    if self.multiprocessing_is_available:
      # Lock is implicit in multiprocessing.Value
      self.value = multiprocessing_context.Value('i', 0)
    else:
      self.lock = threading.Lock()
      self.value = 0

  def Reset(self, reset_value=0):
    if self.multiprocessing_is_available:
      self.value.value = reset_value
    else:
      with self.lock:
        self.value = reset_value

  def Increment(self):
    if self.multiprocessing_is_available:
      self.value.value += 1
    else:
      with self.lock:
        self.value += 1

  def Decrement(self):
    if self.multiprocessing_is_available:
      self.value.value -= 1
    else:
      with self.lock:
        self.value -= 1

  def GetValue(self):
    if self.multiprocessing_is_available:
      return self.value.value
    else:
      with self.lock:
        return self.value

def _IncreaseSoftLimitForResource(resource_name, fallback_value):
  """Sets a new soft limit for the maximum number of open files.

  The soft limit is used for this process (and its children), but the
  hard limit is set by the system and cannot be exceeded.

  We will first try to set the soft limit to the hard limit's value; if that
  fails, we will try to set the soft limit to the fallback_value iff this would
  increase the soft limit.

  Args:
    resource_name: Name of the resource to increase the soft limit for.
    fallback_value: Fallback value to be used if we couldn't set the
                    soft value to the hard value (e.g., if the hard value
                    is "unlimited").

  Returns:
    Current soft limit for the resource (after any changes we were able to
    make), or -1 if the resource doesn't exist.
  """

  # Get the value of the resource.
  try:
    (soft_limit, hard_limit) = resource.getrlimit(resource_name)
  except (resource.error, ValueError):
    # The resource wasn't present, so we can't do anything here.
    return -1

  # Try to set the value of the soft limit to the value of the hard limit.
  if hard_limit > soft_limit:  # Some OS's report 0 for "unlimited".
    try:
      resource.setrlimit(resource_name, (hard_limit, hard_limit))
      return hard_limit
    except (resource.error, ValueError):
      # We'll ignore this and try the fallback value.
      pass

  # Try to set the value of the soft limit to the fallback value.
  if soft_limit < fallback_value:
    try:
      resource.setrlimit(resource_name, (fallback_value, hard_limit))
      return fallback_value
    except (resource.error, ValueError):
      # We couldn't change the soft limit, so just report the current
      # value of the soft limit.
      return soft_limit
  else:
    return soft_limit

def ShouldProhibitMultiprocessing():
  """Determines if the OS doesn't support multiprocessing.

  There are two cases we currently know about:
    - Multiple processes are not supported on Windows.
    - If an error is encountered while using multiple processes on Alpine Linux
      gsutil hangs. For this case it's possible we could do more work to find
      the root cause but after a fruitless initial attempt we decided instead
      to fall back on multi-threading w/o multiprocesing.

  Returns:
    (bool indicator if multiprocessing should be prohibited, OS name)
  """
  if system_util.IS_WINDOWS:
    # Issues have been observed while trying to use multi-processing in Windows
    return (True, 'Windows')
  if system_util.IS_OSX:
    # macOS does not contain /etc/os-release, used in this method. This
    # shortcuts raising an exception and returning 'Unknown' as an OS.
    return (False, 'macOS')
  try:
    with open('/etc/os-release', 'r') as f:
      # look for line that contains 'NAME=' both PRETTY_NAME and NAME should
      # be acceptable to try to find if alpine linux is being used.
      for line in f.read().splitlines():
        if 'NAME=' in line:
          os_name = line.split('=')[1].strip('"')
          return ('alpine linux' in os_name.lower(), os_name)
      # Unable to determine OS. NAME line not found in /etc/os-release file.
      return (False, 'Unknown')
  except IOError as e:
    if e.errno == errno.ENOENT:
      logging.debug('Unable to open /etc/os-release to determine whether OS '
                    'supports multiprocessing: errno=%d, message=%s' %
                    (e.errno, str(e)))
      return (False, 'Unknown')
    else:
      raise
  except Exception as exc:
    logging.debug('Something went wrong while trying to determine '
                  'multiprocessing capabilities.\nMessage: {0}'.format(
                      str(exc)))
    return (False, 'Unknown')

def CheckMultiprocessingAvailableAndInit(logger=None):
  """Checks if multiprocessing is available, and if so performs initialization.

  There are some environments in which there is no way to use multiprocessing
  logic that's built into Python (e.g., if /dev/shm is not available, then
  we can't create semaphores). This simply tries out a few things that will be
  needed to make sure the environment can support the pieces of the
  multiprocessing module that we need.

  See gslib.command.InitializeMultiprocessingVariables for
  an explanation of why this is necessary.

  Args:
    logger: (logging.Logger) Logger to use for debug output.

  Returns:
    (MultiprocessingIsAvailableResult) A namedtuple with the following attrs:
      - multiprocessing_is_available: True iff the multiprocessing module is
            available for use.
      - stack_trace: The stack trace generated by the call we tried that
            failed.
  """
  # pylint: disable=global-variable-undefined
  global _cached_multiprocessing_is_available
  global _cached_multiprocessing_check_stack_trace
  global _cached_multiprocessing_is_available_message
  if _cached_multiprocessing_is_available is not None:
    if logger:
      logger.debug(_cached_multiprocessing_check_stack_trace)
      logger.warn(_cached_multiprocessing_is_available_message)
    return MultiprocessingIsAvailableResult(
        is_available=_cached_multiprocessing_is_available,
        stack_trace=_cached_multiprocessing_check_stack_trace)

  should_prohibit_multiprocessing, os_name = ShouldProhibitMultiprocessing()
  if should_prohibit_multiprocessing:
    message = """
Multiple processes are not supported on %s. Operations requesting
parallelism will be executed with multiple threads in a single process only.
""" % os_name
    if logger:
      logger.warn(message)
    return MultiprocessingIsAvailableResult(is_available=False,
                                            stack_trace=None)

  stack_trace = None
  multiprocessing_is_available = True
  message = """
You have requested multiple processes for an operation, but the
required functionality of Python\'s multiprocessing module is not available.
Operations requesting parallelism will be executed with multiple threads in a
single process only.
"""
  try:
    # Fails if /dev/shm (or some equivalent thereof) is not available for use
    # (e.g., there's no implementation, or we can't write to it, etc.).
    try:
      multiprocessing_context.Value('i', 0)
    except:
      message += """
Please ensure that you have write access to both /dev/shm and /run/shm.
"""
      raise  # We'll handle this in one place below.

    global top_level_manager  # pylint: disable=global-variable-undefined
    top_level_manager = multiprocessing_context.Manager()

    # Check that the max number of open files is reasonable. Always check this
    # after we're sure that the basic multiprocessing functionality is
    # available, since this won't matter unless that's true.
    limit = -1
    if _HAS_RESOURCE_MODULE:
      # Try to set this with both resource names - RLIMIT_NOFILE for most Unix
      # platforms, and RLIMIT_OFILE for BSD. Ignore AttributeError because the
      # "resource" module is not guaranteed to know about these names.
      try:
        limit = max(
            limit,
            _IncreaseSoftLimitForResource(
                resource.RLIMIT_NOFILE,
                constants.MIN_ACCEPTABLE_OPEN_FILES_LIMIT))
      except AttributeError:
        pass

      try:
        limit = max(
            limit,
            _IncreaseSoftLimitForResource(
                resource.RLIMIT_OFILE,
                constants.MIN_ACCEPTABLE_OPEN_FILES_LIMIT))
      except AttributeError:
        pass

    if limit < constants.MIN_ACCEPTABLE_OPEN_FILES_LIMIT:
      message += ("""
Your max number of open files, %s, is too low to allow safe multiprocessing.
On Linux you can fix this by adding something like "ulimit -n 10000" to your
~/.bashrc or equivalent file and opening a new terminal.

On macOS, you may also need to run a command like this once (in addition to the
above instructions), which might require a restart of your system to take
effect:
  launchctl limit maxfiles 10000

Alternatively, edit /etc/launchd.conf with something like:
  limit maxfiles 10000 10000

""" % limit)
      raise Exception('Max number of open files, %s, is too low.' % limit)
  except:  # pylint: disable=bare-except
    stack_trace = traceback.format_exc()
    multiprocessing_is_available = False
    if logger is not None:
      logger.debug(stack_trace)
      logger.warn(message)

  # Set the cached values so that we never need to do this check again.
  _cached_multiprocessing_is_available = multiprocessing_is_available
  _cached_multiprocessing_check_stack_trace = stack_trace
  _cached_multiprocessing_is_available_message = message
  return MultiprocessingIsAvailableResult(
      is_available=_cached_multiprocessing_is_available,
      stack_trace=_cached_multiprocessing_check_stack_trace)

def CreateLock():
  """Returns either a multiprocessing lock or a threading lock.

  Use Multiprocessing lock iff we have access to the parts of the
  multiprocessing module that are necessary to enable parallelism in operations.

  Returns:
    Multiprocessing or threading lock.
  """
  if CheckMultiprocessingAvailableAndInit().is_available:
    return top_level_manager.Lock()
  else:
    return threading.Lock()

# Pylint gets confused by the mixed lower and upper-case method names in
# AtomicDict.
# pylint: disable=invalid-name
def PutToQueueWithTimeout(queue, msg, timeout=STATUS_QUEUE_OP_TIMEOUT):
  """Puts an item to the status queue.

  If the queue is full, this function will timeout periodically and repeat
  until success. This avoids deadlock during shutdown by never making a fully
  blocking call to the queue, since Python signal handlers cannot execute
  in between instructions of the Python interpreter (see
  https://docs.python.org/2/library/signal.html for details).

  Args:
    queue: Queue class (typically the global status queue)
    msg: message to post to the queue.
    timeout: (optional) amount of time to wait before repeating put request.
  """
  put_success = False
  while not put_success:
    try:
      queue.put(msg, timeout=timeout)
      put_success = True
    except Queue.Full:
      pass

# pylint: enable=invalid-name


import os
import json
import argparse

## 使用之前需要从作者打包的results集合中，把该数据集内所有的txt文件单独复制到archive_path里去
parser = argparse.ArgumentParser(description='transfer the format of raw results from pytracking to pysot')
parser.add_argument('--dataset', type=str,default='OTB100',
        help='datasets')
parser.add_argument('--archive_path', type=str,default='/home/xiao/pythoncode/4.pytracking/pytracking/pytracking/util_scripts/unpack/atom/default_003',
        help='the path of raw results in pytracking style')
parser.add_argument('--saved_path', type=str,default='/home/xiao/pythoncode/4.pytracking/pytracking/pytracking/util_scripts/unpack/atom/tmp',
        help='the path to save raw results in pysot style')

args = parser.parse_args()

tmp=['Jogging-2.txt', 'Human4-2.txt', 'Skating2-1.txt', 'Jogging-1.txt', 'Skating2-2.txt']

videos=os.listdir(args.archive_path)
videos=[os.path.join(args.archive_path,i) for i in videos]
trajs=[]
for v in videos:
    with open(v,'r') as f:
        traj=[list(map(float,x.strip().split('\t'))) for x in f.readlines()]
        trajs.append(traj)
video_files=os.listdir(args.archive_path)
saved_path=os.path.join(args.saved_path,args.dataset)
if not os.path.isdir(saved_path):
    os.makedirs(saved_path)
for n,i in enumerate(video_files):
    with open(os.path.join(saved_path,i), 'w') as f:
        for x in trajs[n]:
            f.write(','.join([str(i) for i in x]) + '\n')


import time
import hazelcast

if __name__ == "__main__":

    def write_pessimistic_lock():
        print("Starting pessimistic write")
        # Connect to Hazelcast cluster.
        client = hazelcast.HazelcastClient(
        cluster_members=[
        "127.0.0.1:5701",
        "127.0.0.1:5702",
        "127.0.0.1:5703"
    ],
    lifecycle_listeners=[
        lambda state: print("New event appeared in lifecycle: ", state),
    ])

        lock_map = client.get_map("lock-map")
        key = "test_val"
        lock_map.put_if_absent(key, 1)

        for i in range(100):
            lock_map.lock(key).result()
            try:
                value = lock_map.get(key).result()
                #print("Value var type: ", type(value))
                value += 1
                lock_map.put(key, value).result()
            finally:
                lock_map.unlock(key)

        print("Finalized with result: ", lock_map.get(key).result())

    write_pessimistic_lock()


"""
FILE: skeleton_parser.py
------------------
Author: Firas Abuzaid (fabuzaid@stanford.edu)
Author: Perth Charernwattanagul (puch@stanford.edu)
Modified: 04/21/2014

Skeleton parser for CS564 programming project 1. Has useful imports and
functions for parsing, including:

1) Directory handling -- the parser takes a list of eBay json files
and opens each file inside of a loop. You just need to fill in the rest.
2) Dollar value conversions -- the json files store dollar value amounts in
a string like $3,453.23 -- we provide a function to convert it to a string
like XXXXX.xx.
3) Date/time conversions -- the json files store dates/ times in the form
Mon-DD-YY HH:MM:SS -- we wrote a function (transformDttm) that converts to the
for YYYY-MM-DD HH:MM:SS, which will sort chronologically in SQL.

Your job is to implement the parseJson function, which is invoked on each file by
the main function. We create the initial Python dictionary object of items for
you; the rest is up to you!
Happy parsing!
"""

import sys
from json import loads
from re import sub
import os
import sqlite3

columnSeparator = "|"

# Dictionary of months used for date transformation
MONTHS = {'Jan':'01','Feb':'02','Mar':'03','Apr':'04','May':'05','Jun':'06',\
        'Jul':'07','Aug':'08','Sep':'09','Oct':'10','Nov':'11','Dec':'12'}

"""
Returns true if a file ends in .json
"""
def isJson(f):
    return len(f) > 5 and f[-5:] == '.json'

"""
Converts month to a number, e.g. 'Dec' to '12'
"""
def transformMonth(mon):
    if mon in MONTHS:
        return MONTHS[mon]
    else:
        return mon

"""
Transforms a timestamp from Mon-DD-YY HH:MM:SS to YYYY-MM-DD HH:MM:SS
"""
def transformDttm(dttm):
    dttm = dttm.strip().split(' ')
    dt = dttm[0].split('-')
    date = '20' + dt[2] + '-'
    date += transformMonth(dt[0]) + '-' + dt[1]
    return date + ' ' + dttm[1]

"""
Transform a dollar value amount from a string like $3,453.23 to XXXXX.xx
"""

def transformDollar(money):
    if money == None or len(money) == 0:
        return money
    return sub(r'[^\d.]', '', money)

def is_existing_user_id(user_id, conn):
    cursor = conn.execute("SELECT count(*) from temp_user_id WHERE user_id = %s;" % (user_id,) )
    for x in cursor:
        if x[0] == 0:
            return False
    return True
def add_to_existing_user_id(user_id, conn):
    conn.execute("INSERT INTO temp_user_id (user_id) VALUES(%s);" % (user_id,))

"""
Parses a single json file. Currently, there's a loop that iterates over each
item in the data set. Your job is to extend this functionality to create all
of the necessary SQL tables for your database.
"""
def parseJson(json_file, conn):
    dat_file_dir = "dat_files/"
    if not os.path.exists(dat_file_dir):
        os.makedirs(dat_file_dir)
    with open(json_file, 'r') as f, \
        open(dat_file_dir + 'items.dat', 'a+') as item_dat, \
        open(dat_file_dir + 'users.dat', 'a+') as users_dat, \
        open(dat_file_dir + 'bids.dat', 'a+') as bid_dat, \
        open(dat_file_dir + 'categories.dat', 'a+') as cat_dat:

        items = loads(f.read())['Items'] # creates a Python dictionary of Items for the supplied json file

        for item in items:

            # Item parsing
            item_id = item['ItemID']

            item_name = item['Name']
            if item_name is None:
                item_name = '"NULL"'
            item_name = '"' + item_name.replace('"', '""') + '"'

            item_description = item['Description']
            if item_description is None:
                item_description = '"NULL"'
            item_description = '"' + item_description.replace('"', '""')  + '"'

            categories_tmp = item['Category']
            categories = set(categories_tmp)
            for category in categories:
                category = '"' + category.replace('"', '""') + '"'
                cat_dat.write(csv_line((item_id, category)))

            current_bid = transformDollar(item['Currently'])
            if current_bid is None:
                current_bid = '"NULL"'
            #current_bid = '"' + current_bid.replace('"', '""') + '"'

            first_bid = transformDollar(item['First_Bid'])
            if first_bid is None:
                first_bid = '"NULL"'
            #first_bid = '"' + first_bid.replace('"', '""') + '"'

            location = item['Location']
            if location is None:
                location = '"NULL"'
            location = '"' + location.replace('"', '""') + '"'

            country = item['Country']
            if country is None:
                country = '"NULL"'
            country = '"' + country.replace('"', '""') + '"'

            bid_started = transformDttm(item['Started'])
            if bid_started is None:
                bid_started = '"NULL"'
            bid_started = '"' + bid_started.replace('"', '""') + '"'

            bid_ends = transformDttm(item['Ends'])
            if bid_ends is None:
                bid_ends = '"NULL"'
            bid_ends = '"' + bid_ends.replace('"', '""') + '"'

            seller_id = item['Seller']['UserID']
            seller_id = '"' + seller_id.replace('"', '""') + '"'

            seller_rating = item['Seller']['Rating']
            if seller_rating == None:
                seller_rating = '"NULL"'
            seller_rating = '"' + seller_rating.replace('"', '""') + '"'

            if is_existing_user_id(seller_id, conn) is False:
                users_dat.write(csv_line((seller_id, seller_rating, location, country)))
                add_to_existing_user_id(seller_id, conn)

            num_bids = 0
            if item['Bids'] is not None:
                num_bids = len(item['Bids'])
            item_dat.write(csv_line((item_id, item_name, current_bid, first_bid, bid_started, bid_ends, str(num_bids), item_description, seller_id)))

            # Bid parsing
            bids = item['Bids']
            if bids is not None:
                for bid in bids:
                    bid_data = bid['Bid']
                    bidder = bid_data['Bidder']

                    bidder_id = bidder['UserID']
                    bidder_id = '"' + bidder_id.replace('"', '""') + '"'

                    bidder_rating = bidder['Rating']
                    if bidder_rating is None:
                        bidder_rating = 'NULL'
                    bidder_rating = '"' + bidder_rating.replace('"', '""') + '"'

                    bidder_location = None
                    if 'Location' in bidder:
                        bidder_location = bidder['Location']
                    if bidder_location is None:
                        bidder_location = 'NULL'
                    bidder_location = '"' + bidder_location.replace('"', '""') + '"'

                    bidder_country = None
                    if 'Country' in bidder:
                        bidder_country = bidder['Country']
                    if bidder_country is None:
                        bidder_country = '"NULL"'
                    bidder_country = '"' + bidder_country.replace('"', '""') + '"'

                    bid_time = transformDttm(bid_data['Time'])
                    if bid_time is None:
                        bid_time = '"NULL"'
                    bid_time = '"' + bid_time.replace('"', '""') + '"'

                    bid_amount = transformDollar(bid_data['Amount'])
                    if bid_amount is None:
                        bid_amount = '"NULL"'
                    #bid_amount = '"' + bid_amount.replace('"', '""') + '"'

                    if is_existing_user_id(bidder_id, conn) is False:
                        add_to_existing_user_id(bidder_id, conn)
                        users_dat.write(csv_line((bidder_id, bidder_rating, bidder_location, bidder_country)))
                    bid_dat.write(csv_line((item_id, bidder_id, bid_amount, bid_time)))

            """
            TODO: traverse the items dictionary to extract information from the
            given `json_file' and generate the necessary .dat files to generate
            the SQL tables based on your relation design
            """

        f.close()
        item_dat.close()
        users_dat.close()
        bid_dat.close()
        cat_dat.close()

# Aggregate data into a csv row
def csv_line(input_list):
    return columnSeparator.join(input_list) + '\n'

"""
Loops through each json files provided on the command line and passes each file
to the parser
"""
def main(argv, conn):
    if len(argv) < 2:
        print('Usage: python skeleton_json_parser.py <path to json files>', file=sys.stderr)
        sys.exit(1)
    # loops over all .json files in the argument
    for f in argv[1:]:
        if isJson(f):
            parseJson(f, conn)
            print("Success parsing " + f)

def create_tmp_db(db):
    conn = sqlite3.connect(db)
    #conn.execute("DROP TABLE IF EXISTS temp_user_id")
    conn.execute("CREATE TABLE IF NOT EXISTS temp_user_id(user_id TEXT)")
    return conn

if __name__ == '__main__':
    conn = create_tmp_db("user_tmp.db")
    main(sys.argv, conn)
    conn.commit()
    conn.close()