# -*- coding: utf8 -*-
from .Config import Config
from .Downloader import Downloader
from ..utils.utils import *
from ..utils.ColorString import ColorString
from ..core.OptiFine import OptiFine
from ..core.Fabric import Fabric

import os
import uuid
import datetime
import shutil

class Client:

    def __init__(self, config):
        self.config = config
        self.downloader = Downloader(self.config)

    def start(self):
        if not self.config.is_client:
            return

        if self.config.isPure :
            self.downloader.downloadGameJSON()
            self.downloader.downloadClient()
            self.downloader.downloadAssetIndex()
            self.downloader.downloadAssetObjects()
            self.downloader.donwloadLibraries()
            self.writeLauncherProfilesJSON()
        elif self.config.isForge:
            self.config.getForgeInfo()
            if not os.path.exists(self.config.game_version_client_jar_file_path()) or not os.path.exists(self.config.game_version_forge_json_file_path()):
                self.extractForgeClient()
        else:
            ColorString.warn('Not Known Client!!!!')
            return

        user = self.config.username
        resolution = ('960', '540')

        if is_sigint_up():
            return

        cmd = self.gameArguments(user,resolution)
        backgroundCmd =  cmd + ' &'
        if platformType() == 'windows':
            backgroundCmd = 'start ' + cmd
            backgroundCmd = cmd

        os.chdir(self.config.game_version_client_dir())
        os.system(backgroundCmd)
        print(ColorString.confirm('Start Client Successfully!!!'))
        self.formatOutputClientCmd(backgroundCmd)

    '''如果是调试模式，则输出命令行指令内容'''
    def formatOutputClientCmd(self, cmd):
        if self.config.debug and len(cmd) > 0:
            for e in cmd.split(' '):
                if 'jar' in e:
                    for c in e.split(self.config.java_class_path_list_separator()):
                        print(c)
                else:
                    print(e)

    '''提取java类相关启动参数'''
    def javaClassPathList(self):

        javaClassPathList = []

        libs = self.config.game_version_json_obj().get('libraries')
        total = len(libs)

        index = 0
        for lib in libs:
            downloads = lib.get('downloads')

            rules = lib.get('rules')
            isContinue = False
            if None != rules:
                for rule in rules:
                    if None != rule:
                        if rule.get('action') == 'disallow':
                            if rule.get('os').get('name') == platformType():
                                isContinue

                        if rule.get('action') == 'allow':
                            allow_os = rule.get('os')
                            if allow_os and allow_os.get('name') != platformType():
                                isContinue = True

            if isContinue:
                continue

            libPath = None
            url = None
            sha1 = None
            nativeKey = 'natives-'+ platformType()
            if 'natives' in lib:
                platform = lib.get('natives').get(platformType())
                if platform == None:
                    continue
                else:
                    libPath = downloads.get('classifiers').get(platform).get('path')
                    url = downloads.get('classifiers').get(platform).get('url')
                    sha1 = downloads.get('classifiers').get(platform).get('sha1')
                    nativeFilePath = os.path.join(self.config.game_version_client_native_library_dir(),os.path.basename(url))
                    if not checkFileExist(nativeFilePath,sha1):
                        continue
                    else:
                        javaClassPathList.append(nativeFilePath)
            else:
                classifiers = downloads.get('classifiers')
                if classifiers and nativeKey in downloads.get('classifiers'):
                    url = downloads.get('classifiers').get(nativeKey).get('url')
                    sha1 = downloads.get('classifiers').get(platform).get('sha1')
                    nativeFilePath = os.path.join(self.config.game_version_client_native_library_dir(),os.path.basename(url))
                    if not checkFileExist(nativeFilePath,sha1):
                        continue
                    else:
                        javaClassPathList.append(nativeFilePath)

                libPath = downloads.get('artifact').get('path')
                url = downloads.get('artifact').get('url')
                sha1 = downloads.get('artifact').get('sha1')
                if platformType() == 'windows' :
                    libPath = libPath.replace('/','\\')
                libFilePath = os.path.join(self.config.game_version_client_library_dir(), libPath)
                if not checkFileExist(libFilePath,sha1):
                    continue
                else:
                    javaClassPathList.append(libFilePath)

            index = index + 1

        if self.config.isForge:
            forge_class_path = map(lambda lib:  os.path.join(self.config.game_version_client_library_dir(),lib.get('downloads').get('artifact').get('path')) ,self.config.game_version_json_obj().get('libraries'))
            javaClassPathList.extend(forge_class_path)

        javaClassPathList.append(self.config.game_version_client_jar_file_path())

        return javaClassPathList

    def gameArguments(self, user, resolution):

        argPattern = r'\$\{(.*)\}'

        mainCls =  self.config.game_version_json_obj().get('mainClass')
        classPathList = self.javaClassPathList()

        if self.config.isPure:
            if self.config.optifine:
                optifine_config = OptiFine.json_configuration(self.config)
                if optifine_config != None:
                    mainCls = optifine_config.get('mainClass')
                    optifine_jar_paths = OptiFine.library_optifine_jar_paths(self.config)
                    classPathList = optifine_jar_paths + classPathList
            elif self.config.fabric:
                fabric_config = Fabric.json_configuration(self.config)
                if fabric_config != None:
                    mainCls = fabric_config.get('mainClass')
                    fabric_jar_paths = Fabric.library_fabric_jar_paths(self.config)
                    classPathList = fabric_jar_paths + classPathList

        sep = self.config.java_class_path_list_separator()
        classPath = sep.join(classPathList)
        (res_width, res_height) = resolution

        configuration = {
            # for game args
            "auth_player_name" : user,
            "version_name" : self.forgeGame().get('id') if self.config.isForge else self.config.game_version_json_obj().get('id'),
            "game_directory" : self.config.game_version_client_dir(),
            "assets_root" : self.config.game_version_client_assets_dir(),
            "assets_index_name" : self.config.game_version_json_obj().get('assets'),
            "auth_uuid" : ''.join(str(uuid.uuid1()).split('-')),
            "auth_access_token" : ''.join(str(uuid.uuid1()).split('-')),
            "user_type" : "mojang",
            "version_type" : "release",
            "resolution_width":res_width if res_width else "",
            "resolution_height":res_height if res_height else "",
            # for jvm args
            "natives_directory": self.config.game_version_client_native_library_dir(),
            "launcher_name": "OrzMC",
            "launcher_version": '1.0',
            "classpath": classPath,

        }

        java_path = os.popen('which java' if platformType() != 'windows' else 'where java').read().strip()
        if ' ' in java_path:
            java_path = '\"' + java_path + '\"'

        if len(java_path) <= 0:
            ColorString.error('You should installl JDK to run Minecraft')
            exit(-1)

        java_path = 'java'

        arguments = [java_path]

        mem_min = self.config.mem_min
        mem_max = self.config.mem_max

        jvm_opts = [
            '-Xms%s' % mem_min,
            '-Xmx%s' % mem_max,
            '-Djava.net.preferIPv4Stack=true'
        ]

        if self.config.isPure and self.config.fabric:
            jvm_opts.extend(Fabric.jvmOpts(self.config))

        arguments.extend(jvm_opts)

        game_arguments = self.config.game_version_json_obj().get('arguments')
        jvmArgs = game_arguments.get('jvm') if game_arguments != None else None

        if jvmArgs != None and self.config.isForge:
            jvmArgs.append('-Dfml.ignoreInvalidMinecraftCertificates=true')
            jvmArgs.append('-Dfml.ignorePatchDiscrepancies=true')

        if jvmArgs != None:
            for arg in jvmArgs:
                if isinstance(arg, str):
                    value_placeholder = re.search(argPattern,arg)
                    if value_placeholder:
                        argValue = configuration.get(value_placeholder.group(1))
                        if argValue != None:
                            argStr = matchAndReplace(argPattern,argValue,arg)
                            arguments.append(argStr)
                    else:
                        arguments.append(arg)

                elif isinstance(arg, dict):
                    isValid = False
                    rules = arg.get('rules')
                    for rule in rules:
                        if rule.get('os').get('name') == platformType() and rule.get('action') == 'allow':
                            isValid = True
                            break
                    if isValid:
                        value = arg.get('value')
                        if type(value) == list:
                            for i in range(0, len(value)):
                                s = value[i].split('=')
                                if len(s) == 2:
                                    arg = s[1]
                                    if ' ' in arg:
                                        s[1] = '\"' + arg + '\"'
                                        value[i] = '='.join(s)

                            arguments.extend(value)
                        else:
                            arguments.append(value)

        arguments.append(mainCls)

        gameArgs = game_arguments.get('game') if game_arguments != None else None

        if gameArgs != None:
            for arg in gameArgs:
                if isinstance(arg, str):
                    value_placeholder = re.search(argPattern,arg)
                    if value_placeholder:
                        argValue = configuration.get(value_placeholder.group(1))
                        if argValue != None:
                            argStr = matchAndReplace(argPattern,argValue,arg)
                            arguments.append(argStr)
                    else:
                        arguments.append(arg)
                elif isinstance(arg, dict):
                    for rule in arg.get('rules'):
                        if rule.get('action') == 'allow':
                            if rule.get('features').get('is_demo_user'):
                                if user ==  None:
                                    arguments.append(arg.get('value'))
                            if rule.get('features').get('has_custom_resolution'):
                                value = arg.get('value')
                                for arg in value:
                                    if isinstance(arg, str):
                                        value_placeholder = re.search(argPattern,arg)
                                        if value_placeholder:
                                            argValue = configuration.get(value_placeholder.group(1))
                                            if argValue != None:
                                                argStr = matchAndReplace(argPattern,argValue,arg)
                                                arguments.append(argStr)
                                        else:
                                            arguments.append(arg)

        if self.config.isForge:
            arguments.extend(self.config.game_version_json_obj().get('arguments').get('game'))

        if self.config.isPure:
            if self.config.optifine:
                optifine_config = OptiFine.json_configuration(self.config)
                if optifine_config != None:
                    arguments.extend(optifine_config.get('arguments').get('game'))
            elif self.config.fabric:
                fabric_config = Fabric.json_configuration(self.config)
                if fabric_config != None:
                    arguments.extend(fabric_config.get('arguments').get('game'))

        arguments = ' '.join(arguments)
        return arguments

    '''构建Forge客户端'''
    def extractForgeClient(self):

        self.download(
            self.config.forgeInfo.forge_installer_url,
            self.config.game_version_client_dir(),
            prefix_desc='forge installer jar file'
        )

        installerJarFilePath = os.path.basename(self.config.forgeInfo.forge_installer_url)
        extractForgeClientCmd = 'java -jar ' + installerJarFilePath

        print(ColorString.warn('Start install the forge client jar file ...'))
        os.chdir(self.config.game_version_client_dir())
        os.system(extractForgeClientCmd)
        print(ColorString.confirm('Completed! And the forge client file generated!'))

        forge_client_versions_dir = os.path.join(self.config.game_version_client_dir(), 'versions')
        pure_client_jar = os.path.join(forge_client_versions_dir, self.config.version, self.config.version + '.jar')
        dst_client_jar = os.path.join(self.config.game_version_client_dir(), os.path.basename(pure_client_jar))
        if os.path.exists(pure_client_jar) and not os.path.exists(dst_client_jar):
            shutil.move(pure_client_jar, self.config.game_version_client_dir())

        forge_version_full_name = os.path.basename(self.config.game_version_forge_json_file_path())
        (forge_version_name, _) = os.path.splitext(forge_version_full_name)
        forge_json_file = os.path.join(forge_client_versions_dir, forge_version_name, forge_version_full_name)
        print(forge_json_file)
        dst_forge_json_file = os.path.join(self.config.game_version_client_dir(), os.path.basename(forge_json_file))
        if os.path.exists(forge_json_file) and not os.path.exists(dst_forge_json_file):
            shutil.move(forge_json_file, self.config.game_version_client_dir())

        shutil.rmtree(forge_client_versions_dir)

    def writeLauncherProfilesJSON(self):
        launcher_profiles_json_file_path = self.config.game_version_launcher_profiles_json_path()
        if not os.path.exists(launcher_profiles_json_file_path):
            time = datetime.datetime.now().isoformat()
            version = self.config.version
            content = {
                'profiles': {
                    version: {
                        'created': time,
                        'type': 'pure',
                        'name': version,
                        'lastVersionId': version,
                        'lastUsed': time
                    }
                },
                'selectedProfile': version,
                'authenticationDatabase': {}
            }

            launcher_profiles_json_file_content = json.dumps(content)

            with open(launcher_profiles_json_file_path,'w',encoding='utf-8') as f:
                f.write(launcher_profiles_json_file_content)


data = open("8.txt", 'r')
a = 0
b = 0
for line in data:
    line = line.strip()
    x = len(line) + line.count("\"") + line.count("\\") + 2
    y = len(line)
    print(x, y)
    a += x
    b += y

print(a, b, a - b)


import sys

input = sys.stdin.readline
n = int(input().strip())
cnt = 0
i = 1
while True:
    if i > n:
        i = 1
    n -= i
    cnt += 1
    i += 1
    if n == 0: break
print(cnt)


"""Testing Addition"""
import logging
import pandas as pd
import os
from calc import log
from calc.calculations.multiplication import Multiplication

dirname = os.path.dirname(os.path.realpath(__file__))

def test_calculation_multiplication():
    """testing addition method with csv inputs"""
    fileName = '/Users/dhruvshah/PycharmProjects/calc2/tests/CSVFiles/Multiplication.csv'
    inputFile = os.path.join(fileName)
    df = pd.read_csv("CSVFiles/Multiplication.csv")
    print(df.head(10))
    for x, y in df.iterrows():
        mul = (y.Value_1, y.Value_2)
        record = x
        multiplication = Multiplication.create(mul)
        log.logging_data(inputFile, "multiplication", record)
        logging.debug("This is the result")
        assert multiplication.get_result() == df['Result'][x]


# A = 33
# B = "kyeah01"

# print(A,B)

# # ?. 그대로 출력하기

# import sys
# A = "시작!"

# while bool(A):
#     A = sys.stdin.read()
#     if bool(A) == False : break
#     print(A)

# 8958. OX 퀴즈

import sys

N = int(input())
a = 0
s = 0
T = []

while N:
    A = sys.stdin.readline()
    N -= 1
    for i in range(len(A)):
        if A[0] != 'O':
            if A[i] == 'O' and A[i+1] == 'O' or i == 0:
                a += 1
                T.append(a)
            if A[i] == 'O':
                s += a
        else:
            a = 1
            if A[i] == 'O':
                s += a
    print(a,s)
    print(T)
    s = a = 0
    if N == 0: break
print(a,s)
print(T)


import cv2
import pytesseract
import time
import numpy as np
import msvcrt
import os

def adjust_gamma(image, gamma=1.3):

   invGamma = 1.8 / gamma
   table = np.array([((i / 255.0) ** invGamma) * 255
      for i in np.arange(0, 256)]).astype("uint8")

   return cv2.LUT(image, table)

os.environ['TESSDATA_PREFIX'] = r'C:\Program Files\Tesseract-OCR\tessdata'

pytesseract.pytesseract.tesseract_cmd = r'C:/Program Files/Tesseract-OCR/tesseract.exe'

video_path = ".\\test2.mp4"
cap = cv2.VideoCapture(video_path)

interval = 10  # in seconds
frame_rate = cap.get(cv2.CAP_PROP_FPS)
interval_frames = int(interval * frame_rate)

#Offset for starting frame
#frame_count = 35 * frame_rate
frame_count = 10

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break

    # Check if it's time to detect text
    if frame_count % interval_frames == 0:
        #frame = frame[300:400, 350:700]
        frame = frame[400:520, 830:1080]

        #cv2.convertScaleAbs(frame, alpha=1, beta=30)
        frame = adjust_gamma(frame)
        frame = cv2.addWeighted( frame, 1.1, frame, 1, 10)
        frame = cv2.bilateralFilter(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY),1, 17, 17)
        cv2.imshow('window', frame)

        # char = msvcrt.getch().decode("utf-8")#read pressed key, decode
        # print(char)
        # if char == chr(13): #char(27) is the escape key
        #     break

        # # Perform text detection using pytesseract
        text = pytesseract.image_to_string(frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

        # Invoking the command processor and calling the pause command
        # os.system('pause')

        time.sleep(.002)

        # # Do something with the detected text (e.g., print it)
        print("Detected text:", text)

    frame_count += 1


import sys
sys.stdin = open('노드의 거리.txt')
import collections
def bfs(row, k):
    visit[row] = 1
    deq.append((row, k))

    while len(deq) != 0:
        br, bk = deq.popleft()
        if br == end:
            return bk
        for col in range(1, V + 1):
            if node[br][col] == 1 and visit[col] == 0:
                visit[col] = 1
                deq.append((col, bk + 1))
    return 0

T = int(input())

for test_case in range(1, T + 1):
    V, E = map(int, input().split())
    node = [[0 for _ in range(V + 1)] for _ in range(V + 1)]
    for i in range(E):
        num1, num2 = map(int, input().split())
        node[num1][num2] = 1
        node[num2][num1] = 1
    visit = [0 for _ in range(V + 1)]
    start, end = map(int, input().split())
    deq = collections.deque()
    result = bfs(start, 0)
    print("#{} {}".format(test_case, result))


# -*- coding: UTF-8 -*-
'''=================================================
@Author ：zhenyu.yang
@Date   ：2020/11/5 11:37 AM
=================================================='''

import sys
sys.path.append('./')
sys.path.insert(0,'/data/zhenyu.yang/modules')

import cv2
import json
import numpy as np
import random
import copy
from multiprocessing import Process
import os

def getFiles(path, suffix,prefix):
    return [os.path.join(root, file) for root, dirs, files in os.walk(path)
            for file in files if file.endswith(suffix) and file.startswith(prefix)]

def blink2numlist(blink_list,status_name):
    blink_list = [v == status_name for v in blink_list]
    blink_list.insert(0,False)
    blink_list.insert(-1,False)

    start_num = [i-1 for i in range(1,len(blink_list)) if not blink_list[i-1] and blink_list[i]]

    blink_list = blink_list[::-1]
    end_num = [len(blink_list) - i+1 for i in range(1,len(blink_list)) if not blink_list[i-1] and blink_list[i]]
    end_num = end_num[::-1]

    num_list = [abs(end-start)+1 for  start,end in zip(start_num,end_num)]

    return num_list

def get_ear(ldmk):
    eps = 1e-5
    get_distance = lambda x,y:((x[0]-y[0])**2 + (x[1]-y[1])**2 + eps)**0.5

    w = get_distance(ldmk[0],ldmk[4])

    h = [get_distance(ldmk[2],ldmk[6]),get_distance(ldmk[1],ldmk[7]),get_distance(ldmk[3],ldmk[5])]
    h = np.mean(h)
    ear = h/w
    ear = min(ear,0.7)

    return ear

def get_mouth_ear(ldmk):
    eps = 1e-5
    get_distance = lambda x,y:((x[0]-y[0])**2 + (x[1]-y[1])**2 + eps)**0.5

    w = get_distance(ldmk[60],ldmk[64])
    h = [get_distance(ldmk[62],ldmk[66]),get_distance(ldmk[61],ldmk[67]),get_distance(ldmk[63],ldmk[65])]
    h = np.mean(h)
    ear = h/w
    ear = min(ear,1)

    return ear

def get_cof_by_headpose(head_pose):

    yaw,pitch,roll = head_pose
    pitch = pitch * np.pi / 180
    yaw = -(yaw * np.pi / 180)
    roll = roll * np.pi / 180

    cof = max(np.cos(yaw), 0.4)/ max(np.cos(pitch),0.4)
    cof = min(max(cof,0.6),1.6)

    return cof

def get_fea_label(img_info):

    cof = 1

    eye_ears = []
    if 'left_eye_ldmk' in img_info and img_info['left_eye_ldmk'] is not None and  len(img_info['left_eye_ldmk']) > 4:
        eye_ears.append(get_ear(img_info['left_eye_ldmk']) * cof)
    else:
        eye_ears.append(-1)
    if 'right_eye_ldmk' in img_info and img_info['right_eye_ldmk'] is not None and len(img_info['right_eye_ldmk']) > 4:
        eye_ears.append(get_ear(img_info['right_eye_ldmk']) * cof)
    else:
        eye_ears.append(-1)

    if eye_ears[0] == -1 and eye_ears[1] == -1:
        eye_ear =  -1
    elif eye_ears[0] == -1:
        eye_ear =  eye_ears[1]
    elif eye_ears[1] == -1:
        eye_ear =  eye_ears[0]
    else:
        eye_ear = np.mean(eye_ears)

    return eye_ear

def get_yawn_info(yawn_list):
    is_yawn = []
    for v in yawn_list:
        if v == -1:
            is_yawn.append(v)
            continue
        if v > 0.6:
            is_yawn.append(1)
        else:
            is_yawn.append(0)

    is_yawn = blink2numlist(is_yawn,1)

    yawn_rate = len(is_yawn) / (sum(v != -1 for v in yawn_list) + 0.001)

    return yawn_rate

def get_statics(data_list):
    if len(data_list) == 0:
        data_list = [-1]
    data_list.sort()
    data_len = len(data_list)
    return [np.mean(data_list),data_list[0],data_list[-1],data_list[int(data_len*0.25)],data_list[int(data_len*0.5)],data_list[int(data_len*0.75)]]

def get_blink_info(ear_list):
    is_close = []

    reopen_list = [-1]
    close_list = [-1]

    for v in ear_list:
        if v == -1:
            is_close.append(v)
            continue
        if v < 0.16:
            is_close.append(1)
        else:
            is_close.append(0)

    for i in range(len(ear_list) -1):
        if is_close[i] == 0 and is_close[i+1] == 1:
            close_list.append(abs(ear_list[i+1] - ear_list[i]))

        if is_close[i] == 1 and is_close[i+1] == 0:
            reopen_list.append(abs(ear_list[i+1] - ear_list[i]))

    return close_list,reopen_list

def get_perclose(height_list):
    max_height = max(height_list)
    preclose_list = [1 - v/max_height for v in  height_list]
    preclose_50 = sum(v > 0.5 for v in preclose_list)
    preclose_70 = sum(v > 0.7 for v in preclose_list)
    preclose_90 = sum(v > 0.9 for v in preclose_list)
    return [preclose_50,preclose_70,preclose_90]

def get_eye_movement(height_list):
    height_change = [abs(height_list[i+1] - height_list[i]) for i in range(len(height_list)-1)]
    return sum(v>1 for v in height_change) / len(height_list)

def list2num(slice_list):
    num_list = []
    for slice in slice_list:
        num_list.extend(list(range(slice[0], slice[1] + 1)))
    return num_list

def get_batch_data(video_list,suffix,batch_size,start_id,dst_dir,stage= 'train',time_len = 60,random_ratio = 0.01):
    random.shuffle(video_list)
    half_frame_len = time_len * 25 // 2

    data_id = -1
    while True:
        if len(video_list) == 0:
            break

        video_path = video_list.pop()

        json_path = video_path.replace('.mp4', suffix)

        with open(json_path, 'r') as f:
            big_json = f.readlines()

        height_list = []

        for json_info in big_json:
            try:
                json_info = json.loads(json_info.strip())
            except:
                continue

            height_list.append(get_fea_label(json_info))

        bink_info = get_blink_info(height_list)

        blink_path = video_path.replace(os.path.basename(video_path), 'blink.json')
        if not os.path.exists(blink_path):
            continue

        with open(blink_path, 'r') as f:
            jsons = json.load(f)

        close_list = list2num(jsons['blink_close'])
        open_list = list2num(jsons['blink_open'])

        close_list = [i for i in range(len(height_list)) if i in close_list]
        open_list = [i for i in range(len(height_list)) if i in open_list]

        all_info =  [bink_info[0],bink_info[1],close_list,open_list]
        all_info = list(map(np.array,all_info))
        all_info = np.stack(all_info,axis=1)

        temp_data_name = '_'.join(video_path.split(os.sep)[-4:]).replace(
            '.mp4', '')
        temp_data_name = '{}.npy'.format(temp_data_name)

        np.save(os.path.join(dst_dir,temp_data_name),all_info)

    return True

def split(input,num=60):
    random.shuffle(input)

    ans = []
    sep = len(input) //num
    for i in range(num-1):
        ans.append(input[i*sep:(i+1)*sep])

    ans.append(input[(num-1)*sep:])

    return ans

if __name__ == '__main__':

    version = 'v0.1'
    suffix = '_{}.json'.format(version)

    src_dir_dict = {'test':'/data/weiyu.li/DMSData/FatigueView/test_video'

    }
    camera_list = ['ir_down','ir_front','ir_left','ir_left_up','ir_up','rgb_down','rgb_front','rgb_left','rgb_left_up','rgb_up']

    data_type = 'train'
    camera_id = 0

    for data_type in src_dir_dict.keys():
        for camera_id in range(len(camera_list)):

            src_dir = src_dir_dict[data_type]

            camera_type = camera_list[camera_id]

            dst_dir = './data/{}/{}'.format(data_type,camera_type)
            if not os.path.exists(dst_dir):
                os.makedirs(dst_dir)

            video_list = getFiles(src_dir, '.mp4', camera_type)

            if data_type == 'test':
                video_list = [v for v in video_list if 'fengchunshen' not in v and 'panbijia' not in v and 'basic' not in v]

            if data_type == 'train':
                video_list = [v for v in video_list if 'zhaoxinmei' not in v]

            all_num = 60000
            running_num = 32
            batch_size = all_num//running_num
            split_videos = split(video_list, running_num)

            process_list = []
            for i in range(running_num):
                temp_p = Process(target=get_batch_data,args=(split_videos[i],suffix,batch_size,batch_size*i,dst_dir,data_type))
                process_list.append(temp_p)

            for temp_p in process_list:
                temp_p.start()

            for temp_p in process_list:
                temp_p.join()

            print('END')


#%% 10 - Dois vetores virando um

"""
Faça um Programa que leia dois vetores com 10 elementos cada. Gere um
terceiro vetor de 20 elementos, cujos valores deverão ser compostos
pelos elementos intercalados dos dois outros vetores.
"""

def input_elementos(n):
    vetor = []
    for i in range(n):
        elemento = (input(f'Insira {n} elementos, elemento {i+1}: '))
        vetor.append(elemento)
    return vetor

vetor1 = input_elementos(10)
vetor2 = input_elementos(10)
vetor3 = []
for i in range(len(vetor1)):
    if i % 2 == 0:
        vetor3.append(vetor1[i])
    else: vetor3.append(vetor2[i])

print(', '.join([vetor for vetor in vetor3]))


# Databricks notebook source
# MAGIC %md
# MAGIC ##### Welcome  to Azurelib Academy By Deepak Goyal
# MAGIC ##### You can master the Databricks here: www.adeus.azurelib.com
# MAGIC ##### Author of this blog: Arud Seka Berne S in

# COMMAND ----------

# MAGIC %md
# MAGIC ##### Welcome  to Azurelib Academy By Deepak Goyal
# MAGIC ##### You can master the Databricks here: www.adeus.azurelib.com
# MAGIC ##### Author of this blog: Arud Seka Berne S in

# COMMAND ----------

# MAGIC %md
# MAGIC ##### Welcome  to Azurelib Academy By Deepak Goyal
# MAGIC ##### You can master the Databricks here: www.adeus.azurelib.com
# MAGIC ##### Author of this blog: Arud Seka Berne S in

# COMMAND ----------

# MAGIC %md
# MAGIC ##### Welcome  to Azurelib Academy By Deepak Goyal
# MAGIC ##### You can master the Databricks here: www.adeus.azurelib.com
# MAGIC ##### Author of this blog: Arud Seka Berne S in

# COMMAND ----------

# MAGIC %md
# MAGIC #### Pyspark Select Column in Azure Databricks

# COMMAND ----------

# MAGIC %md
# MAGIC ###### Gentle reminder:
# MAGIC In Databricks,
# MAGIC   - sparkSession made available as spark
# MAGIC   - sparkContext made available as sc
# MAGIC
# MAGIC In case, you want to create it manually, use the below code.

# COMMAND ----------

from pyspark.sql.session import SparkSession

spark = SparkSession.builder \
    .master("local[*]") \
    .appName("azurelib.com") \
    .getOrCreate()

sc = spark.sparkContext

# COMMAND ----------

# MAGIC %md
# MAGIC ##### a) Create manual PySpark DataFrame

# COMMAND ----------

data = [
    (1,"Sascha","1998-09-03"),
    (2,"Lise","2008-09-17"),
    (3,"Nola","2008-08-23"),
    (4,"Demetra","1997-06-02"),
    (5,"Lowrance","2006-07-02")
]

df = spark.createDataFrame(data, schema=["id","name","dob"])
df.printSchema()
df.show()

# COMMAND ----------

# MAGIC %md
# MAGIC ##### b) Create PySpark DataFrame by reading files

# COMMAND ----------

# replace the file_path with the source file location which you have downloaded.

df_2 = spark.read.format("csv").option("header", True).load(file_path)
df_2.printSchema()

# COMMAND ----------

# MAGIC %md
# MAGIC ##### Note: Here, I will be using the manually created dataframe

# COMMAND ----------

# MAGIC %md
# MAGIC ##### 1. Selecting single column

# COMMAND ----------

from pyspark.sql.functions import col

# 1. Selecting columns using String
df.select("name").show()

# 2. Selecting columns using python Dot Notation
df.select(df.name).show()

# 3. Selecting columns using column name as Key
df.select(df["name"]).show()

# 4. Selecting columns using col() function
df.select(col("name")).show()

# COMMAND ----------

# MAGIC %md
# MAGIC ##### 2. Selecting multiple columns

# COMMAND ----------

from pyspark.sql.functions import col

# 1. Selecting columns using String
df.select("id", "name").show()

# 2. Selecting columns using python Dot Notation
df.select(df.id, df.name).show()

# 3. Selecting columns using column name as Key
df.select(df["id"], df["name"]).show()

# 4. Selecting columns using col() function
df.select(col("id"), col("name")).show()

# COMMAND ----------

# MAGIC %md
# MAGIC ##### 3. Selecting entire column

# COMMAND ----------

# 1. Selecting all columns using "*" symbol
df.select("*").show()

# 2. Selecting all columns list
df.select(["id", "name", "dob"]).show()

# 3. Selecting all columns using columns field
df.select(df.columns).show()

# COMMAND ----------

# MAGIC %md
# MAGIC ##### 4. Selecting column by index

# COMMAND ----------

# 1. Selecting the first column
df.select(df.columns[0]).show()

# 2. Selecting all the columns from second column
df.select(df.columns[1:]).show()

# 3. Selecting the columns on every two steps
df.select(df.columns[::2]).show()

# COMMAND ----------

# MAGIC %md
# MAGIC ##### 5. Selecting columns in reverse order

# COMMAND ----------

df.select(df.columns[::-1]).show()

1. Pyspark Select Column/Pyspark Select Column - Notebook.py


# -*- coding: utf-8 -*-
from django.conf.urls import include, url
from rest_framework.urlpatterns import format_suffix_patterns

from velafrica.api import utils
from velafrica.api import views
from velafrica.api import views_public
from velafrica.api.collections import views as views_collections
from velafrica.api.counter.views import CounterEntryList, CounterEntryDetail
from velafrica.api.organisation.views import OrganisationDetail, OrganisationList
from velafrica.api.sbbtracking.views import TrackingList, TrackingEventList
from velafrica.api.stock import views as views_stock
from velafrica.api.user import views as views_user
from velafrica.api.velafrica_sud import views as views_velafrica_sud

app_name = "api"

collection = [
    url(r'^eventcategories/?$', utils.get_listview('collection', 'EventCategory').as_view(), name="eventcategories"),
    url(r'^eventcategories/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('collection', 'EventCategory').as_view(),
        name='eventcategory'),
    url(r'^events/?$', utils.get_listview('collection', 'Event').as_view(), name="events"),
    url(r'^events/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('collection', 'Event').as_view(), name='event'),
    url(r'^dropoffs/?$', utils.get_listview('collection', 'Dropoff').as_view(), name="dropoffs"),
    url(r'^dropoffs/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('collection', 'Dropoff').as_view(), name='dropoffs'),
    url(r'^tasks/?$', utils.get_listview('collection', 'Task').as_view(), name="tasks"),
    url(r'^tasks/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('collection', 'Task').as_view(), name='tasks'),
    url(r'^taskprogresses/?$', utils.get_listview('collection', 'TaskProgress').as_view(), name="tasksprogresses"),
    url(r'^taskprogresses/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('collection', 'TaskProgress').as_view(),
        name='taskprogress'),
    url(r'^collectionevents/?$', utils.get_listview('collection', 'CollectionEvent').as_view(),
        name="collectionevents"),
    url(r'^collectionevents/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('collection', 'CollectionEvent').as_view(),
        name='collectionevent'),
    url(r'^hosttypes/?$', utils.get_listview('collection', 'HostType').as_view(), name="hosttypes"),
    url(r'^hosttypes/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('collection', 'HostType').as_view(), name='hosttypes'),
]

counter = [
    url(r'^entries/?$', CounterEntryList.as_view(), name="entries"),
    url(r'^entries/(?P<pk>[0-9]+)/?$', CounterEntryDetail.as_view(), name='entry'),
]

stock = [
    url(r'^warehouses/?$', utils.get_listview('stock', 'Warehouse').as_view(), name="warehouses"),
    url(r'^warehouses/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('stock', 'Warehouse').as_view(), name='warehouse'),
    url(r'^products/?$', utils.get_listview('stock', 'Product').as_view(), name="products"),
    url(r'^products/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('stock', 'Product').as_view(), name='product'),
    url(r'^categories/?$', utils.get_listview('stock', 'Category').as_view(), name="categories"),
    url(r'^categories/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('stock', 'Category').as_view(), name='category'),
    url(r'^stocks/?$', utils.get_listview('stock', 'Stock').as_view(), name="stocks"),
    url(r'^stocks/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('stock', 'Stock').as_view(), name='stock'),
    url(r'^stocklists/?$', utils.get_listview('stock', 'StockList').as_view(), name="stocklists"),
    url(r'^stocklists/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('stock', 'StockList').as_view(), name='stocklist'),
    url(r'^stocklistpositions/?$', utils.get_listview('stock', 'StockListPosition').as_view(),
        name="stocklistpositions"),
    url(r'^stocklistposition/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('stock', 'StockListPosition').as_view(),
        name='stocklistposition'),

    url(r'^stocktransfers/?$', utils.get_listview('stock', 'StockTransfer').as_view(), name="stocktransfers"),
    url(r'^stocktransfers/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('stock', 'StockTransfer').as_view(),
        name='stocktransfer'),
    url(r'^stocktransfers/(?P<pk>[0-9]+)/book$', views_stock.book, name='stocktransfer'),
    # url(r'^stocktransfers/(?P<pk>[0-9]+)/revoke$', utils.get_retrieveview('stock', 'StockTransfer').as_view(), name='stocktransfer'),

    url(r'^stockchanges/?$', utils.get_listview('stock', 'StockChange').as_view(), name="stockchanges"),
    url(r'^stockchange/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('stock', 'StockChange').as_view(),
        name='stockchange'),
]

transport = [
    url(r'^cars/?$', utils.get_listview('transport', 'Car').as_view(), name="cars"),
    url(r'^cars/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('transport', 'Car').as_view(), name='car'),
    url(r'^velostates/?$', utils.get_listview('transport', 'VeloState').as_view(), name="velostates"),
    url(r'^velostates/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('transport', 'VeloState').as_view(), name='velostate'),
    url(r'^rides/?$', utils.get_listview('transport', 'Ride').as_view(), name="rides"),
    url(r'^rides/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('transport', 'Ride').as_view(), name='ride'),
    url(r'^drivers/?$', utils.get_listview('transport', 'Driver').as_view(), name="drivers"),
    url(r'^drivers/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('transport', 'Driver').as_view(), name='driver'),
]

organisation = [
    url(r'^organisations/?$', OrganisationList.as_view(), name="organisations"),
    url(r'^organisations/(?P<pk>[0-9]+)/?$', OrganisationDetail.as_view(), name='organisations_detail'),
    url(r'^countries/?$', utils.get_listview('organisation', 'Country').as_view(), name='countries'),
    url(r'^countries/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('organisation', 'Country').as_view(), name='country'),
]

tracking = [
    url(r'^trackings/?$', TrackingList.as_view(), name="trackings"),
    url(r'^trackings/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('sbbtracking', 'Tracking').as_view(), name='tracking'),
    url(r'^trackingevents/?$', TrackingEventList.as_view(), name="trackingevents"),
    url(r'^trackingevents/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('sbbtracking', 'TrackingEvent').as_view(),
        name='trackingevent'),
    url(r'^trackingeventtypes/?$', utils.get_listview('sbbtracking', 'TrackingEventType').as_view(),
        name="trackingeventtypes"),
    url(r'^trackingeventtypes/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('sbbtracking', 'TrackingEventType').as_view(),
        name='trackingeventtype'),
    url(r'^velotypes/?$', utils.get_listview('sbbtracking', 'VeloType').as_view(), name="velotypes"),
    url(r'^velotypes/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('sbbtracking', 'VeloType').as_view(), name='velotype'),
]

# first attempt at generic views
velafrica_sud = [
    url(r'^containers/?$', views_velafrica_sud.ContainerList.as_view(), name="containers"),
    url(r'^containers/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('velafrica_sud', 'Container').as_view(),
        name='container'),
    url(r'^forwarders/?$', utils.get_listview('velafrica_sud', 'Forwarder').as_view(), name="forwarders"),
    url(r'^forwarders/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('velafrica_sud', 'Forwarder').as_view(),
        name='forwarder'),
    url(r'^partners/?$', utils.get_listview('velafrica_sud', 'PartnerSud').as_view(), name="partners"),
    url(r'^partners/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('velafrica_sud', 'PartnerSud').as_view(),
        name='partner'),
    url(r'^reports/?$', utils.get_listview('velafrica_sud', 'Report').as_view(), name="reports"),
    url(r'^reports/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('velafrica_sud', 'Report').as_view(), name='report'),
]

bikes = [
    url(r'^bikes/?$', utils.get_listview('bikes', 'Bike').as_view(), name="bikes"),
    url(r'^bikes/(?P<pk>[0-9]+)/?$', utils.get_retrieveview('bikes', 'Bike').as_view(),
        name='container'),
]

public = [
    url(r'^dropoffs/$', views_public.get_dropoffs, name="dropoffs"),
    url(r'^collectionevents/$', views_collections.CollectionEventListPublic.as_view(), name="collectionevents-public"),
    url(r'^subscribe-newsletter/$', views_public.subscribe_newsletter, name="subscribe-newsletter"),
]

user = [
    url(r'^current/$', views_user.current_user, name="current_user"),

]

# where it all comes together
urlpatterns = [
    url(r'^$', views.api_root),
    url(r'^swagger/', include('velafrica.api.swagger.urls')),

    url(r'^user/', include((user, app_name))),
    url(r'^user/', include((user, app_name), namespace="user")),

    url(r'^organisation/', include((organisation, app_name), namespace='organisation')),
    url(r'^stock/', include((stock, app_name))),
    url(r'^bikes/', include((bikes, app_name))),
    url(r'^tracking/', include((tracking, app_name))),
    url(r'^transport/', include((transport, app_name))),
    url(r'^velafrica_sud/', include((velafrica_sud, app_name))),
    url(r'^stock/', include((stock, app_name), namespace="stock")),
    url(r'^bikes/', include((bikes, app_name), namespace="bikes")),
    url(r'^tracking/', include((tracking, app_name), namespace="tracking")),
    url(r'^transport/', include((transport, app_name), namespace="transport")),
    url(r'^velafrica_sud/', include((velafrica_sud, app_name), namespace="velafrica_sud")),
    url(r'^counter/', include('velafrica.api.counter.urls')),
    url(r'^collections/', include((collection, app_name))),
    url(r'^collections/', include((collection, app_name), namespace="collection")),
    url(r'^public/', include((public, app_name), namespace='public'))
]

urlpatterns = format_suffix_patterns(urlpatterns)


"""
Project name: fish_book
Description:
Create Time: 2020/7/31 08:15
Author: to2bage
Email: to2bage@hotmail.com
Version: 0.1
"""
from app import create_app

app = create_app()

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


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

from pygments.lexer import RegexLexer
from pygments.token import (
    Text, Name, Number, String, Comment, Punctuation,
    Keyword, Operator
)

class TOMLLexer(RegexLexer):
    """
    Lexer for TOML, a simple language for config files
    """

    name = 'TOML'
    aliases = ['toml']
    filenames = ['*.toml']

    tokens = {
        'root': [

            # Basics, comments, strings
            (r'\s+', Text),
            (r'#.*?$', Comment.Single),
            (r'"(\\\\|\\"|[^"])*"', String),
            (r'(true|false)$', Keyword.Constant),
            ('[a-zA-Z_][a-zA-Z0-9_\-]*', Name),

            # Datetime
            (r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z', Number.Integer),

            # Numbers
            (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?', Number.Float),
            (r'\d+[eE][+-]?[0-9]+j?', Number.Float),
            (r'\-?\d+', Number.Integer),

            # Punctuation
            (r'[]{}:(),;[]', Punctuation),
            (r'\.', Punctuation),

            # Operators
            (r'=', Operator)

        ]
    }


import os, sys
import pandas as pd
import subprocess as sp
from pdb import set_trace

sOutput_dir = sys.argv[1]
sProject    = sys.argv[2]

def Parsing_summary():

    if not os.path.isdir("{outdir}/Summary_result".format(outdir=sOutput_dir)):
        os.mkdir("{outdir}/Summary_result".format(outdir=sOutput_dir))

    ## Partially Cat files into Final File
    nBins       = 20
    list_sFiles = os.listdir('%s/Summary' % sOutput_dir)
    list_sFiles = [sFile for sFile in list_sFiles if not sFile.endswith('.temp')]
    list_sFiles = [sFile for sFile in list_sFiles if not sFile.endswith('Summary_all.txt')]

    nTotalJobs = len(list_sFiles)
    list_nIndexKey = [[i + 1, int(nTotalJobs * (i) / nBins), int(nTotalJobs * (i + 1) / nBins)] for i in
                      range(nBins)]

    for nIndex, nFileS, nFileE in list_nIndexKey:
        list_sInFiles = list_sFiles[nFileS:nFileE]
        list_InFiles  = ['%s/Summary/%s' % (sOutput_dir, sFile) for sFile in list_sInFiles]

        print('Summary_Part%s %s' % (nIndex, len(list_InFiles)))
        sOutFile      = '%s/Summary/Summary_Part%s.temp' % (sOutput_dir, nIndex)

        sp.call('cat %s >  %s' % (' '.join(list_InFiles), sOutFile), shell=True )
    #loop END: nIndex, nFileS, nFileE
    sp.call('cat {outdir}/Summary/Summary_Part*.temp > {outdir}/Summary/Summary_all.txt'.format(outdir=sOutput_dir), shell=True)

    dfSummary                = pd.read_table('{outdir}/Summary/Summary_all.txt'.format(outdir=sOutput_dir), header=None)
    dfSummary.columns        = ['Barcode', 'Total', 'Insertion', 'Deletion', 'Complex']
    dfSummary                = dfSummary.groupby(['Barcode']).sum()
    dfSummary['Total_indel'] = dfSummary['Insertion'] + dfSummary['Deletion'] + dfSummary['Complex']
    dfSummary['IND/TOT']     = dfSummary['Total_indel'] / dfSummary['Total']
    dfSummary['IND/TOT'].fillna(0, inplace=True)
    dfSummary.to_csv('{outdir}/Summary_result/Summary_result.tsv'.format(outdir=sOutput_dir), sep='\t')

def Annotate_final_result():

    dfCount_INDEL = pd.read_table('{outdir}/result/freq/freq_result/Indel_summary.txt'.format(outdir=sOutput_dir), header=None)
    dfSummary     = pd.read_table('{outdir}/Summary_result/Summary_result.tsv'.format(outdir=sOutput_dir), index_col='Barcode')

    dfCount_INDEL[0] = dfCount_INDEL[0].str.replace('.INDEL_freq.txt', '')
    dfCount_INDEL.set_index(0, inplace=True)
    dfConcat_result = pd.concat([dfCount_INDEL, dfSummary.loc[:,['Total_indel', 'Total', 'IND/TOT']]],axis=1)
    dfConcat_result.dropna(inplace=True)
    dfConcat_result = dfConcat_result.reset_index()
    dfConcat_result = dfConcat_result.loc[:,['index','Total_indel', 'Total', 'IND/TOT', 1,2]]
    dfConcat_result.columns = ['Barcode', 'Total_indel', 'Total', 'IND/TOT', 'Match','Info']
    dfConcat_result = dfConcat_result.round(2)
    dfConcat_result.to_csv('{outdir}/Summary_result/{project}_Final_indel_result.tsv'.format(outdir=sOutput_dir, project=sProject),
                           sep='\t', index=False)

if __name__ == '__main__':
    Parsing_summary()
    Annotate_final_result()

Supplementary Code 1 - Indel Searcher/SummarizeOutput.py


import io
import os
import requests
import pandas as pd
from google.cloud import storage
from config import gcloud_creds, bucket_name
from pathlib import Path
import shutil

"""
Pre-reqs:
1. `pip install pandas pyarrow google-cloud-storage`
2. Set GOOGLE_APPLICATION_CREDENTIALS to your project/service-account key
3. Set GCP_GCS_BUCKET as your bucket or change default value of BUCKET
"""

# services = ['fhv','green','yellow']
init_url = 'https://github.com/DataTalksClub/nyc-tlc-data/releases/download/'
# switch out the bucketname
storage_client = storage.Client.from_service_account_json(gcloud_creds)
# BUCKET = os.environ.get("GCP_GCS_BUCKET", "dtc-data-lake-bucketname")
gcs_bucket = storage_client.get_bucket(bucket_name)

def upload_to_gcs(bucket, object_name, local_file):
    """
    Ref: https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python
    """
    # # WORKAROUND to prevent timeout for files > 6 MB on 800 kbps upload speed.
    # # (Ref: https://github.com/googleapis/python-storage/issues/74)
    # storage.blob._MAX_MULTIPART_SIZE = 5 * 1024 * 1024  # 5 MB
    # storage.blob._DEFAULT_CHUNKSIZE = 5 * 1024 * 1024  # 5 MB

    # client = storage.Client()
    # bucket = client.bucket(bucket)
    blob = bucket.blob(object_name)
    blob.upload_from_filename(local_file)

def remove_files():
    # https://stackoverflow.com/questions/32834731/how-to-delete-a-file-by-extension-in-python
    dir_name = "./"
    local_data = os.listdir(dir_name)

    # remove the local CSV's
    for item in local_data:
        if item.endswith(".csv.gz"):
            os.remove(os.path.join(dir_name, item))

    # Remove the local parquet files
    # https://stackoverflow.com/questions/48892772/how-to-remove-a-directory-is-os-removedirs-and-os-rmdir-only-used-to-delete-emp
    shutil.rmtree('./data/')

def clean_data(df, service):
    '''Fix datatype issues'''

    if service == 'yellow':
        # fix datetimes
        df.tpep_pickup_datetime = pd.to_datetime(df.tpep_pickup_datetime)
        df.tpep_dropoff_datetime = pd.to_datetime(df.tpep_dropoff_datetime)

        # this fixes fields for files that have NAN values and thus aren't INTs
        # when they should be INTs
        # https://pandas.pydata.org/pandas-docs/stable/user_guide/integer_na.html#integer-na
        df.VendorID = pd.array(df.VendorID, dtype=pd.Int64Dtype())
        df.passenger_count = pd.array(df.passenger_count, dtype=pd.Int64Dtype())
        df.payment_type = pd.array(df.payment_type, dtype=pd.Int64Dtype())
        df.RatecodeID = pd.array(df.RatecodeID, dtype=pd.Int64Dtype())

    elif service == 'green':
        # fix datetimes
        df.lpep_pickup_datetime = pd.to_datetime(df.lpep_pickup_datetime)
        df.lpep_dropoff_datetime = pd.to_datetime(df.lpep_dropoff_datetime)

        # this fixes fields for files that have NAN values and thus aren't INTs
        # when they should be INTs
        # https://pandas.pydata.org/pandas-docs/stable/user_guide/integer_na.html#integer-na
        df.VendorID = pd.array(df.VendorID, dtype=pd.Int64Dtype())
        df.passenger_count = pd.array(df.passenger_count, dtype=pd.Int64Dtype())
        df.payment_type = pd.array(df.payment_type, dtype=pd.Int64Dtype())
        df.trip_type = pd.array(df.trip_type, dtype=pd.Int64Dtype())
        df.RatecodeID = pd.array(df.RatecodeID, dtype=pd.Int64Dtype())

    # elif service == 'fhv':
    else:
        # Rename columns
        df.rename({'dropOff_datetime':'dropoff_datetime'}, axis='columns', inplace=True)
        df.rename({'PUlocationID':'PULocationID'}, axis='columns', inplace=True)
        df.rename({'DOlocationID':'DOLocationID'}, axis='columns', inplace=True)

        # fix datetimes
        df.pickup_datetime = pd.to_datetime(df.pickup_datetime)
        df.dropoff_datetime = pd.to_datetime(df.dropoff_datetime)

        # this fixes fields for files that have NAN values and thus aren't INTs
        # when they should be INTs
        # https://pandas.pydata.org/pandas-docs/stable/user_guide/integer_na.html#integer-na
        df.PULocationID = pd.array(df.PULocationID, dtype=pd.Int64Dtype())
        df.DOLocationID = pd.array(df.DOLocationID, dtype=pd.Int64Dtype())

    return df

def web_to_gcs(year, service, gcs_bucket):

    # Loop through the months
    for i in range(12):

        # Set the month part of the file_name string
        month = '0'+str(i + 1)
        month = month[-2:]

        # create CSV file_name and path to write parquet file to
        file_name = f'{service}_tripdata_{year}-{month}.csv.gz'
        path = Path(f'data/{service}_tripdata_{year}-{month}.parquet').as_posix()

        # Make the directory to hold the parquet file if it doesn't exist
        os.makedirs(os.path.dirname(path), exist_ok=True)

        # Download CSV using requests via a Pandas DataFrame
        print(f'\nDownloading {file_name}...')
        request_url = f"{init_url}{service}/{file_name}"
        r = requests.get(request_url)
        open(file_name, 'wb').write(r.content)

        # Read it back into a parquet file
        print(f'Saving {file_name} to {path}...')
        df = pd.read_csv(file_name, compression='gzip')

        # clean the data and fix the data types
        df = clean_data(df, service)

        file_name = file_name.replace('.csv.gz', '.parquet')
        df.to_parquet(path, engine='pyarrow')

        # Upload parquet to GCS Bucket
        print(f'Uploading {path} to GCS...')
        upload_to_gcs(gcs_bucket, f'data/{service}/{file_name}', f'data/{file_name}')

if __name__ == '__main__':
    web_to_gcs('2019', 'green', gcs_bucket)
    web_to_gcs('2020', 'green', gcs_bucket)
    web_to_gcs('2019', 'yellow', gcs_bucket)
    web_to_gcs('2020', 'yellow', gcs_bucket)
    web_to_gcs('2019', 'fhv', gcs_bucket)
    remove_files()


import bisect

class Solution(object):
    def countSteppingNumbers(self, low, high):
        """
        :type low: int
        :type high: int
        :rtype: List[int]
        """
        s = set([i for i in range(10)])

        dic = {}
        dic[0] = s

        # print(s)

        for i in range(10):
            temp = set()
            for j in dic[i]:
                if j == 0:
                    continue
                last = j % 10
                if last > 0:
                    temp.add(j * 10 + last - 1)
                if last < 9:
                    temp.add(j * 10 + last + 1)

                first = j // (10 ** (i))
                if first > 1:
                    temp.add((first - 1) * (10 ** (i + 1)) + j)
                if first < 9:
                    temp.add((first + 1) * (10 ** (i + 1)) + j)
            dic[i + 1] = temp
            # print(temp)

        temp = []
        for i in dic.values():
            temp += list(i)
        temp.sort()
        left = bisect.bisect_left(temp, low)
        right = bisect.bisect_right(temp, high)
        return temp[left:right]


import click
import logging
import os
from typing import List, Optional

import torch
from tqdm import tqdm
from transformers import AutoTokenizer

from luke.utils.entity_vocab import EntityVocab, MASK_TOKEN, PAD_TOKEN

from dataloader import create_dataloader
from dataset import load_dataset
from model import LukeForEntityDisambiguation

logger = logging.getLogger(__name__)

@click.command()
@click.option("--model-dir", type=click.Path(exists=True), required=True)
@click.option("--dataset-dir", type=click.Path(exists=True), required=True)
@click.option("--titles-file", type=click.Path(exists=True), required=True)
@click.option("--redirects-file", type=click.Path(exists=True), required=True)
@click.option("--ppr-for-ned-dir", type=click.Path(exists=True))
@click.option(
    "-t",
    "--test-set",
    default=["test_b", "ace2004", "aquaint", "msnbc", "wikipedia", "clueweb"],
    type=click.Choice(["test_b", "test_b_ppr", "ace2004", "aquaint", "msnbc", "wikipedia", "clueweb"]),
    multiple=True,
)
@click.option("--device", type=str, default="cuda")
@click.option("--max-seq-length", type=int, default=512)
@click.option("--max-entity-length", type=int, default=128)
@click.option("--max-candidate-length", type=int, default=30)
@click.option("--max-mention-length", type=int, default=30)
@click.option(
    "--inference-mode", type=click.Choice(["global", "local"]), default="global",
)
@click.option(
    "--document-split-mode", type=click.Choice(["simple", "per_mention"]), default="simple",
)
def evaluate(
    model_dir: str,
    dataset_dir: str,
    titles_file: str,
    redirects_file: str,
    ppr_for_ned_dir: Optional[str],
    test_set: List[str],
    device: str,
    max_seq_length: int,
    max_entity_length: int,
    max_candidate_length: int,
    max_mention_length: int,
    inference_mode: str,
    document_split_mode: str,
):
    model = LukeForEntityDisambiguation.from_pretrained(model_dir).eval()
    model.to(device)
    tokenizer = AutoTokenizer.from_pretrained(model_dir)
    entity_vocab_path = os.path.join(model_dir, "entity_vocab.jsonl")
    entity_vocab = EntityVocab(entity_vocab_path)
    pad_entity_id = entity_vocab[PAD_TOKEN]
    mask_entity_id = entity_vocab[MASK_TOKEN]
    dataset = load_dataset(
        dataset_dir=dataset_dir,
        titles_file=titles_file,
        redirects_file=redirects_file,
        ppr_for_ned_dir=ppr_for_ned_dir,
    )

    for dataset_name in test_set:
        print(f"========== Dataset: {dataset_name} ==========")
        documents = dataset.get_dataset(dataset_name)
        dataloader = create_dataloader(
            documents=documents,
            tokenizer=tokenizer,
            entity_vocab=entity_vocab,
            batch_size=1,
            fold="eval",
            document_split_mode=document_split_mode,
            max_seq_length=max_seq_length,
            max_entity_length=max_entity_length,
            max_candidate_length=max_candidate_length,
            max_mention_length=max_mention_length,
        )
        candidate_indices_list = []
        eval_entity_mask_list = []
        for input_dict in tqdm(dataloader, leave=False):
            inputs = {k: v.to(device) for k, v in input_dict.items()}
            entity_ids = inputs.pop("entity_ids")
            entity_length = inputs["entity_attention_mask"].sum()
            input_entity_ids = entity_ids.new_full(entity_ids.size(), pad_entity_id)
            input_entity_ids[0, :entity_length] = mask_entity_id
            eval_entity_mask = inputs.pop("eval_entity_mask")
            eval_entity_mask_list.append(eval_entity_mask[0, :entity_length])
            with torch.no_grad():
                candidate_indices = torch.zeros(entity_length, dtype=torch.long, device=device)
                if inference_mode == "local":
                    logits = model(entity_ids=input_entity_ids, **inputs)[0]
                    for n, entity_id in enumerate(torch.argmax(logits, dim=2)[0, :entity_length]):
                        if inputs["entity_candidate_ids"][0, n].sum() != 0:
                            candidate_indices[n] = (inputs["entity_candidate_ids"][0, n] == entity_id).nonzero(
                                as_tuple=True
                            )[0][0]
                else:
                    for _ in range(entity_length):
                        logits = model(entity_ids=input_entity_ids, **inputs)[0]
                        probs = torch.nn.functional.softmax(logits, dim=2) * (
                            input_entity_ids == mask_entity_id
                        ).unsqueeze(-1).type_as(logits)
                        max_probs, max_indices = torch.max(probs.squeeze(0), dim=1)
                        target_index = torch.argmax(max_probs, dim=0)
                        input_entity_ids[0, target_index] = max_indices[target_index]
                        if inputs["entity_candidate_ids"][0, target_index].sum() != 0:
                            candidate_indices[target_index] = (
                                inputs["entity_candidate_ids"][0, target_index] == max_indices[target_index]
                            ).nonzero(as_tuple=True)[0][0]
            candidate_indices_list.append(candidate_indices)

        all_candidate_indices = torch.cat(candidate_indices_list)
        all_eval_entity_mask = torch.cat(eval_entity_mask_list)

        last_index = -1
        num_correct = 0
        num_mentions = 0
        num_mentions_with_candidates = 0
        for document in documents:
            for mention in document.mentions:
                num_mentions += 1
                index = last_index + 1
                while True:
                    if all_eval_entity_mask[index] == 1:
                        break
                    index += 1
                last_index = index

                if mention.candidates:
                    num_mentions_with_candidates += 1
                    predicted_candidate_index = all_candidate_indices[index]
                    predicted_title = mention.candidates[predicted_candidate_index].title
                    if predicted_title == mention.title:
                        num_correct += 1

        precision = num_correct / num_mentions_with_candidates
        recall = num_correct / num_mentions
        f1 = 2.0 * precision * recall / (precision + recall)
        print(f"F1: {f1:.3f}  Precision: {precision:.3f}  Recall: {recall:.3f}")

if __name__ == "__main__":
    evaluate()


from collections import defaultdict

class ThroneInheritance:
    def __init__(self, kingName: str):
        self.king = kingName
        self.graph = defaultdict(list)
        self.dead = set()

    def birth(self, parentName: str, childName: str) -> None:
        self.graph[parentName].append(childName)

    def death(self, name: str) -> None:
        self.dead.add(name)

    def getInheritanceOrder(self) -> List[str]:
        res = []
        stack = [self.king]
        while len(stack) > 0:
            curr = stack.pop()
            if curr not in self.dead:
                res.append(curr)
            for j in self.graph[curr][::-1]:
                stack.append(j)
        return res


Geeks = {
    'address': 'Toktogula 175',
    'courses': ['Android', 'Backend', 'Frontend'],
    'bag': {'fails', 'errors', 'stack'}
}
del Geeks['bag']
Geeks['address'] = 'Ibraimova 103'
Geeks['number'] = '0507052018'
Geeks['instagram'] = 'geeks_edu'
new_courses = ['IOS', 'UX/UI', 'english courses', 'design courses', 'manicurist', 'drawing', 'massage']
print(set(new_courses))
Geeks['date'] = '2018 year'
Geeks['courses'] = list(Geeks['courses'])
for i in new_courses:
    Geeks['courses'].append(i)
print(Geeks['courses'])
print(len(Geeks['courses']))

for k, v in Geeks.items():
    print(k, ':', v)


import numpy as np
import pandas as pd
import math
from sklearn.metrics import f1_score
import scipy

def compute_EXP_F1(pred,target):
    pred_labels = []
    pred = np.array(pred)
    target = np.array(target)
    for i in range(pred.shape[0]):
        l = np.argmax(pred[i])
        pred_labels.append(l)
    F1s = f1_score(target,pred_labels,average=None)
    macro_f1 = np.mean(F1s)
    return F1s,macro_f1

def compute_AU_F1(pred,label):
    pred = np.array(pred)
    label = np.array(label)
    AU_targets = [[] for i in range(12)]
    AU_preds = [[] for i in range(12)]
    F1s = []
    for i in range(pred.shape[0]):
        for j in range(12):
            p = pred[i,j]
            if p>=0.5:
                AU_preds[j].append(1)
            else:
                AU_preds[j].append(0)
            AU_targets[j].append(label[i,j])

    for i in range(12):
        F1s.append(f1_score(AU_targets[i], AU_preds[i]))

    F1s = np.array(F1s)
    F1_mean = np.mean(F1s)
    return F1s, F1_mean

def PCC(x,y):
    x = np.array(x)
    y = np.array(y)
    x[x>1] = 1
    x[x<0] = 0
    vx = x - np.mean(x)
    vy = y - np.mean(y)
    pcc = np.sum(vx * vy) / (np.sqrt(np.sum(vx**2)) * np.sqrt(np.sum(vy**2)))
    return pcc

def compute_emo_PCC(x,y):
    x = np.array(x)
    y = np.array(y)
    pccs = []
    for i in range(7):
        p = PCC(x[:,i],y[:,i])
        pccs.append(p)
    pccs = np.array(pccs)
    mean_pcc = np.mean(pccs)
    return pccs, mean_pcc

def CCC_score(x, y):
    x = np.array(x)
    y = np.array(y)
    vx = x - np.mean(x)
    vy = y - np.mean(y)
    rho = np.sum(vx * vy) / (np.sqrt(np.sum(vx**2)) * np.sqrt(np.sum(vy**2)))
    x_m = np.mean(x)
    y_m = np.mean(y)
    x_s = np.std(x)
    y_s = np.std(y)
    ccc = 2*rho*x_s*y_s/(x_s**2 + y_s**2 + (x_m - y_m)**2)
    return ccc

def compute_VA_CCC(x,y):
    x = np.array(x)
    y = np.array(y)
    x[x>1] = 1
    x[x<-1] = -1
    print(x.shape,y.shape)
    ccc1 = CCC_score(x[:,0],y[:,0])
    ccc2 = CCC_score(x[:,1],y[:,1])
    return ccc1,ccc2


from django.contrib.auth import get_user_model
from django.http import HttpResponse

from member.decorators import login_required

User = get_user_model()

__all__ = (
    'profile',
)

@login_required
def profile(request):
    return HttpResponse(f'User profile page {request.user}')


import jieba
excludes=['却说','二人','将军','不可','不能','不敢','不知','如此','荆州','如何','引兵','次日','军士','军马','大喜','天下',\
    '左右','主公','东吴','于是','今日','商议','魏兵','陛下','都督','人马','一人','汉中','众将','只见',\
        '上马','大叫','蜀兵','太守','此人','夫人','先主','后人','背后','城中','天子','一面','何不','后主',\
            '大军','忽报','先生','百姓','何故','不如','赶来','原来','先锋','然后','令人','江东','喊声','正是','下马',\
            '徐州','忽然','因此','成都','不见','未知','大败','大事','之后','一军','接应','军中','起兵','引军','可以',\
                '大惊','进兵','大怒','以为','不得','心中','下文','一声','追赶']
threekingdomsTxt=open('threekingdoms.txt', 'r',encoding='utf-8').read()
words=jieba.lcut(threekingdomsTxt)
counts={}
for word in words:
    if len(word)==1:
        continue
    elif word=='孔明' or word=='孔明曰':
        rword='诸葛亮'
    elif word=='关公' or word=='云长' or word=='关云长':
        rword='关羽'
    elif word=='玄德' or word=='玄德曰' or word=='刘皇叔':
        rword='刘备'
    elif word=='孟德' or word=='丞相':
        rword='曹操'
    else:
        rword = word
    counts[rword]=counts.get(rword,0)+1
for word in excludes:
    del counts[word]
items=list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
for i in range(20):
    word,counts=items[i]
    print('{0:<10}:{1:>5}'.format(word,counts))

【第6周】组合数据类型/文本词频统计3.py


import tensorflow as tf
import numpy as np;

labels = np.array([0., 1., 0.])
logits = np.array([2., 6., 3.])

def softmax(y):
    sum = np.sum(np.exp(y))
    y = np.exp(y) / sum
    return y

def cross_entropy(labels, logits):
    error = -labels * np.log(logits) - (1 - labels) * np.log(1 - logits)
    return error

a = softmax(logits)
print(a)
b = cross_entropy(labels, a)
print(b)
print(np.max(b))

with tf.Session() as sess:
    a = sess.run(tf.nn.softmax_cross_entropy_with_logits(labels=labels,logits=logits))
    print(a)

with tf.Session() as sess:
    a = sess.run(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=1,logits=logits))
    print(a)


# Read me
# Print nabopunkter i stedet for afstand
# Print number os dist calc
# Upload github

import math

class Point:
    def __init__(self, x, y, pos_Px, pos_Py, pos_QRx, pos_QRy):
        self.x = x
        self.y = y
        self.pos_Px = pos_Px
        self.pos_Py = pos_Py
        # self.pos_QRx = pos_QRx
        # self.pos_QRy = pos_QRy

def dist(p1, p2):
    return math.sqrt((p2.x - p1.x)**2 + (p2.y - p1.y)**2)

def minDist(P):
    m = 1000000
    for i in range(len(P)):
        for j in range(i + 1, len(P)):
            m = min(m, dist(P[j], P[i]))
    return(m)

def closestPair(Px, Py):
    N = len(Px)

    if N <= 3:
        return minDist(Px)

    # Construct Qx, Qy, Rx, Ry
    L = Px[math.floor(N/2)].x

    Qx = [] #venstre sorteret efter x
    Qy = [] #venstre sorteret efter y
    Rx = []
    Ry = []

    for i in range(N):
        # ith position in Px - add to Q or R
        if Px[i].x < L:
            Qx.append(Px[i])
        else:
            Rx.append(Px[i])
        # ith position in Py - add to Q or R
        if Py[i].x < L:
            Qy.append(Py[i])
        else:
            Ry.append(Py[i])

    for i in range(len(Qx)): #math.floor(N/2)
        Qx[i].pos_Px = i
        Qy[i].pos_Py = i
        Rx[i].pos_Px = i
        Ry[i].pos_Py = i

    # Hvert punkt i Q er nu i Qx og Qy
    dQ = closestPair(Qx, Qy)
    dR = closestPair(Rx, Ry)
    delta = min(dQ, dR)

    # Kombinér løsninger
    # Find punkt på x-akse, som deler punkterne i to lige store dele
    L = Qx[len(Qx) - 1].x

    # Find points in P where for all p dist(p, L) < delta
    Sy = []
    for i in range(len(Py)):
        if abs(Py[i].x - L) <= delta:
            Sy.append(Py[i])

    minS = 1000
    for i in range(len(Sy)):
        for j in range(i + 1, min(15, len(Sy))):
            minS = min (minS, dist(Sy[i], Sy[j]))

    return min(delta, minS)

# Read points
P = []
file = open("Input.txt","r")
line = file.readline()
while(line):
    l = [int(x) for x in line.split(" ")]
    p = Point(l[0], l[1], 0, 0, 0, 0)
    P.append(p)
    line = file.readline()
file.close()

N = len(P)

Px = sorted(P, key=lambda e: e.x)
Py = sorted(P, key=lambda e: e.y)

for i in range(N):
    Px[i].pos_Px = i
    Py[i].pos_Py = i

print("Closest pair ", closestPair(Px, Py))


from database import sql_fetch, sql_fetch_one, sql_write

def get_all():
    results = sql_fetch('SELECT reviews.id, content, rating, name FROM reviews INNER JOIN users ON users.id = reviews.user_id')
    reviews = []
    print(results)
    for row in results:

        review = {
            'id': row[0],
            'content': row[1],
            'rating': row[2],
            'user_name': row[3]
        }

        reviews.append(review)

    return reviews

def add(user_id, content):
    sql_write('INSERT INTO reviews(user_id, content) VALUES (%s, %s)', [user_id, content])


import sys

N, K = map(int, sys.stdin.readline().split(' '))

weight_value = [0 for _ in range(N)]

for i in range(N):
    weight_value[i] = list(map(int, sys.stdin.readline().split(' ')))

dp = [[0 for _ in range(K)] for _ in range(N)]
# dp[n : ~까지 물품 중][무게 제한]

for i in range(K):
    if weight_value[0][0] <= K:
        dp[0][i] = weight_value[0][1]

for i in range(1, N):
    for j in range(K):
        if weight_value[i][0] < K:
            dp[i][j] = weight_value[i][1]

print(dp[N][K])

2023 Spring/Week 4 DP II/12865_평범한 배낭/12865_최우형.py


import cv2
import matplotlib.pyplot as plt
import numpy as np
from skimage.filters import difference_of_gaussians, window
from scipy.fftpack import fftn, fftshift

imagem = cv2.imread("../Images/lena.jpg", 0)
w_imagem = imagem * window('hann', imagem.shape)
img_filtrada = difference_of_gaussians(imagem, 0.5, 50)
w_image_filtrada = img_filtrada * window('hann', imagem.shape)
im_fft_mag = fftshift(np.abs(fftn(w_imagem)))
fim_fft_mag = fftshift(np.abs(fftn(w_image_filtrada)))

plt.figure(figsize=(6.4 * 5, 4.8 * 5), constrained_layout=False)

plt.subplot(161), plt.imshow(imagem, "gray"), plt.title("Imagem Original")
plt.subplot(162), plt.imshow(np.log(im_fft_mag), "gray"), plt.title("Amplitude")
plt.subplot(163), plt.imshow(img_filtrada, "gray"), plt.title("Filtro")
plt.subplot(164), plt.imshow(np.log(fim_fft_mag), "gray"), plt.title("Amplitude e Filtro")

plt.show()


import mailslurp_client
import os
from dotenv import load_dotenv
import json

class MailGenerator:
    def __init__(self, api_key: str):
        if api_key is None:
            raise ValueError("API_KEY not found in .env file. Please create a .env file with the API_KEY variable.")

        self.configuration = mailslurp_client.Configuration()
        self.configuration.api_key['x-api-key'] = api_key

    def create_email(self) -> str:
        """
        Create an email address using the MailSlurp API.

        Returns only the ID of the email address.

        (<id-email-address>@mailslurp.com)
        """
        with mailslurp_client.ApiClient(self.configuration) as api_client:
            try:
                inbox_controller = mailslurp_client.InboxControllerApi(api_client)
                inbox = inbox_controller.create_inbox()
                return str(inbox.id)
            except mailslurp_client.ApiException as e:
                error_body = json.loads(e.body)
                error_message = error_body.get('message')
                return error_message

    def get_email_data(self, inbox_id) -> list:
        """
        Get the data of the emails in the inbox.

        Returns a list of dictionaries, each dictionary contains the following keys:
        - id            (str)
        - domain_id     (str)
        - subject       (str)
        - to            (list)
        - _from         (str)
        - bcc           (list)
        - cc            (list)
        - created_at    (datetime)
        - read          (boolean)
        - attachments   (list)
        - body          (HTML)
        """

        email_list = []
        with mailslurp_client.ApiClient(self.configuration) as api_client:
            inbox_controller = mailslurp_client.InboxControllerApi(api_client)
            email_controller = mailslurp_client.EmailControllerApi(api_client)
            emails = inbox_controller.get_emails(inbox_id=inbox_id)
            for email in emails:
                email_details = email_controller.get_email(email.id)
                email_dict = email.to_dict()
                email_dict['body'] = email_details.body
                email_list.append(email_dict)
            return email_list

if __name__ == '__main__':
    load_dotenv()
    api_key = os.getenv('API_KEY')
    client = MailGenerator(api_key)
    # id = client.create_email()
    # print(id + "@mailslurp.com")
    # input("Press Enter to continue...")
    print(client.get_email_data("70db6f58-2ff6-4b31-b461-2e84bd1f6f81"))


from functools import partial
from typing import Optional

import numpy as np
from mne.time_frequency import tfr_array_multitaper
from librosa import power_to_db

class MultitaperTransform:
    """
    MultitaperTransform:
    """

    def __init__(self, fs: int, fmin: int, fmax: int, tw: float, normalize: bool = True, *args, **kwargs) -> None:
        self.fs = fs
        self.fmin = fmin
        self.fmax = fmax
        self.tw = tw
        self.normalize = normalize
        # self.nfft = nfft
        # self.freqs = np.linspace(self.fmin, self.fmax, endpoint=False)
        self.freqs = np.arange(self.fmin, self.fmax + 0.5, 0.5)
        self.transform_fn = partial(
            tfr_array_multitaper,
            sfreq=self.fs,
            freqs=self.freqs,
            n_cycles=self.freqs / 2.0,
            output="power",
            # zero_mean=True,
        )

    def __call__(self, X: np.ndarray, annotations: np.ndarray) -> np.ndarray:
        C, T = X.shape
        Cxx = self.transform_fn(X[None, :]).squeeze()[0]
        Cxx = power_to_db(Cxx)
        if self.normalize:
            Cxx = (Cxx + 20.0) / 50.0  # Normalize so that output is relatively in range [-1, 1]

        # show a plot
        import matplotlib.pyplot as plt

        fig, axs = plt.subplots(2, 1, sharex=True)
        axs[0].plot(X[0])
        for start, stop in annotations[:, :-1]:
            axs[0].hlines(y=6, xmin=start * T, xmax=stop * T)
        vmin, vmax = None, None
        axs[1].imshow(Cxx, aspect="auto", vmin=vmin, vmax=vmax)  # , vmin=vmin, vmax=vmax)
        axs[1].set(ylim=(self.freqs[0], self.freqs[-1]))

        return Cxx


import os
import itertools
import pandas as pd
import matplotlib.pyplot as plt
import datetime

from finrl.config import config
from finrl.marketdata.yahoodownloader import YahooDownloader
from finrl.preprocessing.preprocessors import FeatureEngineer
from finrl.preprocessing.data import data_split
from finrl.env.env_stocktrading import StockTradingEnv
from finrl.model.models import DRLAgent
from finrl.trade.backtest import backtest_stats, backtest_plot, get_daily_return, get_baseline, trx_plot, trade_plot
from pprint import pprint
import sys
sys.path.append("../FinRL-multi")

def data_split2(df,ratio=0.7):
    thre = int(len(df)*ratio)
    train = df.iloc[:thre].copy()
    trade = df.iloc[thre:].copy()
    train.index = train.date.factorize()[0]
    trade.index = trade.date.factorize()[0]
    return train, trade

def save_final_asset(trade_cache):
    asset = trade_cache['asset']
    filter_asset = asset[asset > 0]
    return sum(filter_asset), len(filter_asset)

def train_save_models(
        agent,
        model_file_name,
        model_,
        model_name,
        timesteps,
        models_path,
        per_save_timesteps=None,
        early_stopping_patience=None,
        pre_model_file_name=None,
        pre_step_name='final'):
    print(f'\n**Begin train of {models_path}/{model_file_name}**')

    if pre_model_file_name is not None:
        env_train = agent.env
        pre_models_path=f"{config.PROJECT_PATH}/{config.TRAINED_MODEL_DIR}/{pre_model_file_name}"
        print(f"Load pre_train model {pre_model_file_name}")
        model_ = model_.load(f"pre_models_path/{pre_step_name}",env_train)

    trained_ = agent.train_model(
        model=model_,
        tb_log_name=model_name,
        total_timesteps=timesteps * 10000,
        save_path=models_path,
        per_save_timesteps=per_save_timesteps,
        early_stopping_patience=early_stopping_patience)

    if not os.path.exists(models_path):
        os.makedirs(models_path)

    trained_.save(f"{models_path}/final")
    print(f'**Success train of {model_file_name}**\n')
    return trained_

def predict_save_model(
        e_trade_gym,
        external_info,
        model_file_name,
        model_,
        result_path,
        models_path,
        step_name="final"):
    print(f"Predicting {model_file_name}:{step_name}/{external_info}")
    trained_model = model_.load(f"{models_path}/{step_name}")

    # backtest dataset
    trade_cache, trade_state_memory, empty_state_memory = DRLAgent.DRL_prediction(
        model=trained_model, environment=e_trade_gym)
    final_asset, trade_num = save_final_asset(trade_cache)
    asset_txt = f"{final_asset}_{trade_num}"
    f = open(f"{result_path}/{external_info}-{step_name}-{asset_txt}.txt", "a")

    trade_cache.to_csv(
        f"{result_path}/trade_cache-{external_info}-{step_name}.csv")
    trade_state_memory.to_csv(
        f"{result_path}/trade_state_memory-{external_info}-{step_name}.csv")
    empty_state_memory.to_csv(
        f"{result_path}/empty_state_memory-{external_info}-{step_name}.csv")

    trade = e_trade_gym.df
    tics = trade.tic.unique().tolist()

    trade_plot(
        trade,
        trade_cache,
        tics,
        f"{model_file_name}-{external_info}-{step_name}")
    # plt.show()
    plt.savefig(f"{result_path}/plot-{external_info}-{step_name}.jpg")
    print(f'Success')
    return True

def predict_save_models(
        models_path,
        e_trade_gym,
        model_file_name,
        model_,
        result_path,
        predict_train=False,
        e_train_gym=None,
        exact_model_name=None):
    if not os.path.exists(result_path):
        os.makedirs(result_path)
    predict_kwargs = {
        "model_": model_,
        "models_path": models_path,
        "result_path": result_path,
        "model_file_name": model_file_name,
    }
    if exact_model_name is not None:
        step_name = exact_model_name
        predict_save_model(
            step_name=step_name,
            e_trade_gym=e_trade_gym,
            external_info="backtest",
            **predict_kwargs)
        # train dataset
        if predict_train and e_train_gym is not None:
            predict_save_model(
                step_name=step_name,
                e_trade_gym=e_train_gym,
                external_info="train",
                **predict_kwargs)
    else:
        print('\n*****Predicting (every step)*****')
        model_names = os.listdir(models_path)
        model_names.sort(reverse=True)
        for step_name in model_names:
            if not step_name.endswith('.zip'):
                continue
            step_name = step_name.strip('.zip')

            predict_save_model(
                step_name=step_name,
                e_trade_gym=e_trade_gym,
                external_info="backtest",
                **predict_kwargs)
            # train dataset
            if predict_train and e_train_gym is not None:
                predict_save_model(
                    step_name=step_name,
                    e_trade_gym=e_train_gym,
                    external_info="train",
                    **predict_kwargs)
    return True

def get_processed_full(tic, state_space, flag_pro=True):
    if flag_pro:
        processed_full = pd.read_csv(
            f"./{config.DATA_SAVE_DIR}/data_{tic}_user{state_space - 1}.csv",
            index_col=0)
        # processed_full = pd.read_csv("./datasets/data_30S_user18.csv",index_col=0)
    else:
        if state_space == 24:
            processed_full = get_processed_full_24(tic)
        elif state_space == 37:
            processed_full = get_processed_full_37(tic)

    return processed_full

def get_processed_full_37(tic,state_space=37):
    raw_csv_name = "df_orig_raw.csv"
    prob_csv_name = "rf_df_model_result.csv"

    prob_df = read_prob_df(prob_csv_name, tic)
    raw_df = read_raw_df(raw_csv_name, tic)

    df = pd.merge(prob_df, raw_df, on=['date', 'tic'], how='inner')

    df.sort_values(["date", "tic"], ignore_index=True)

    fe = FeatureEngineer(
        use_technical_indicator=False,
        user_defined_feature=True
    )

    processed = fe.preprocess_data(df, state_space)

    processed_full = processed.sort_values(["date", "tic"])

    processed_full = processed_full.fillna(0)

    processed_full.sort_values(["date", "tic"], ignore_index=True)
    processed_full.index = processed_full.date.factorize()[0]

    return processed_full

def get_processed_full_24(tic):
    df = pd.read_csv(
        f"./{config.DATA_SAVE_DIR}/data_{tic}.csv",
        index_col=0)

    df.sort_values(["date", "tic"], ignore_index=True)

    fe = FeatureEngineer(
        use_technical_indicator=False,
        user_defined_feature=True
    )

    processed = fe.preprocess_data(df)

    list_ticker = processed["tic"].unique().tolist()
    list_date = list(
        pd.date_range(
            processed["date"].min(),
            processed["date"].max()).astype(str))
    combination = list(itertools.product(list_date, list_ticker))

    processed_full = pd.DataFrame(
        combination, columns=[
            "date", "tic"]).merge(
        processed, on=[
            "date", "tic"], how="left")
    processed_full = processed_full[processed_full["date"].isin(
        processed["date"])]
    processed_full = processed_full.sort_values(["date", "tic"])

    processed_full = processed_full.fillna(0)

    processed_full.sort_values(["date", "tic"], ignore_index=True)
    processed_full.index = processed_full.date.factorize()[0]

    return processed_full

def read_prob_df(prob_csv_name,tic=None):
    df = pd.read_csv(f"./{config.DATA_SAVE_DIR}/{prob_csv_name}",encoding='utf-8')
    rename_dict = {'rand_time': 'date',
                   'ts_code': 'tic',
                   'prob_Y_-3_1d_df':'d1_3',
                  'prob_Y_-2_1d_df' :'d1_2',
                  'prob_Y_-1_1d_df' :'d1_1',
                  'prob_Y_0_1d_df'  :'h1',
                  'prob_Y_1_1d_df'  :'u1_1',
                  'prob_Y_2_1d_df'  :'u1_2',
                  'prob_Y_3_1d_df'  :'u1_3',
                  'pred_Y_1_df'     :'p1',
                  'prob_Y_-3_5d_df' :'d5_3',
                  'prob_Y_-2_5d_df' :'d5_2',
                  'prob_Y_-1_5d_df' :'d5_1',
                  'prob_Y_0_5d_df'  :'h5',
                  'prob_Y_1_5d_df'  :'u5_1',
                  'prob_Y_2_5d_df'  :'u5_2',
                  'prob_Y_3_5d_df'  :'u5_3',
                  'pred_Y_5_df': 'p5'
    }
    df.rename(columns=rename_dict, inplace=True)
    columns = [
        'date',
        'tic',
        'd1_3',
        'd1_2',
        'd1_1',
        'h1',
        'u1_1',
        'u1_2',
        'u1_3',
        'p1',
        'd5_3',
        'd5_2',
        'd5_1',
        'h5',
        'u5_1',
        'u5_2',
        'u5_3',
        'p5'
    ]
    return df[df.tic==tic][columns]

def read_raw_df(raw_csv_name,tic=None):
    df = pd.read_csv(f"./{config.DATA_SAVE_DIR}/{raw_csv_name}",encoding='utf-8')
    df.rename(columns={'trade_time': 'date', 'ts_code': 'tic'}, inplace=True)
    columns = ['date', 'tic', 'close']
    if tic is None:
        return df[columns]
    return df[df.tic==tic][columns]


"""
https://www.acmicpc.net/problem/1475
"""

from bisect import bisect_left, bisect_right
import sys

input = sys.stdin.readline

n = sorted(list(input()))
dp = [0] * 10

for i in range(10):
    dp[i] = bisect_right(n, str(i)) - bisect_left(n, str(i))

dp[9] += dp[6]
dp[6] = 0

if dp[9] % 2 == 0:
    dp[9] //= 2
else:
    dp[9] = dp[9] // 2 + 1

print(max(dp))


import subprocess
import os

from .base import Install

class Scala(Install):

    def __init__(self) -> None:
        super().__init__()
        self.name = 'scala'

    def check(self):
        try:
            return subprocess.call(['scala', '-version'])
        except Exception as e:
            return -1

    def exec(self, *args, **kwargs):
        print("\n\nPrepare the command the scala package\n\n")
        subprocess.call("mkdir -p ${HOME}/soft", shell=True)
        subprocess.call("mkdir -p ${HOME}/scala", shell=True)
        subprocess.call("sudo apt-get update && sudo apt-get install -y wget", shell=True)

        print("\n\nDownload the scala package\n\n")
        download_result = subprocess.call("wget -O ${HOME}/soft/scala-2.13.1.tgz "
                                          "https://downloads.lightbend.com/scala/2.13.1/scala-2.13.1.tgz",
                                          shell=True)
        if download_result:
            print("\n\ndownload scala-2.13.1.tgz failed!\n\n")
            return

        print("\n\nUnzip the scala package\n\n")
        un_gzip_result = subprocess.call(
            "tar -zvxf ${HOME}/soft/scala-2.13.1.tgz -C ${HOME}/scala",
            shell=True)
        if un_gzip_result:
            print("\n\ntar -zvxf scala-2.13.1.tgz failed!\n\n")
            return

        print("\n\nSet the environment of scala\n\n")
        p = subprocess.Popen("ls $HOME/scala/", shell=True, stdout=subprocess.PIPE)

        environ_home = os.environ['HOME']
        jdk_dir_name = p.stdout.readline().decode('utf-8').replace('\n', '')

        with open(f'{environ_home}/.bashrc', 'a+') as f:
            f.write('\n')
            f.write(f'export SCALA_HOME="{environ_home}/scala/{jdk_dir_name}"\n')
            f.write('export PATH="$PATH:${SCALA_HOME}/bin"\n')

        return subprocess.call("source ~/.bashrc", shell=True, executable="/bin/bash")


import sys
import matplotlib.pyplot as plt
from astropy.io import ascii
import numpy as np
import pandas as pd
from pandas.tools.plotting import parallel_coordinates

def plot_on_plane(infile):

    iris_dat = ascii.read(infile, names=['slength', 'swidth', 'plength', 'pwidth', 'class'])
    setosa = iris_dat[np.where(iris_dat['class'] == 'Iris-setosa')]
    versicolor = iris_dat[np.where(iris_dat['class'] == 'Iris-versicolor')]
    virginica = iris_dat[np.where(iris_dat['class'] == 'Iris-virginica')]

    plt.plot(setosa['plength'], setosa['pwidth'], 'o', color='blue', label='setosa')
    plt.plot(versicolor['plength'], versicolor['pwidth'], 'o', color='red', label='versicolor')
    plt.plot(virginica['plength'], virginica['pwidth'], 'o', color='green', label='virginica')
    plt.legend(loc=2)
    plt.xlabel('petal length')
    plt.ylabel('petal width')
    plt.show()

def plot_parallel_coordinates(infile):

    iris_dat = pd.read_csv(infile, names=['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'class'])
    parallel_coordinates(iris_dat, 'class')
    plt.show()

if __name__ == "__main__":
    plot_on_plane(sys.argv[1])
    plot_parallel_coordinates(sys.argv[1])


from flask import Flask, render_template, request, redirect, url_for, jsonify
import pickle
from model_ubcf_main import Recommendation

recommend = Recommendation()
SBPRS_app = Flask(__name__)  # intitialize the flaks app  # common

@SBPRS_app.route('/', methods = ['POST', 'GET'])
def home():
    flag = False
    data = ""
    if request.method == 'POST':
        flag = True
        reviews_username = request.form["User_id"]
        data=recommend.getTopProducts(reviews_username)
    return render_template('SBPRS_index.html', data=data, flag=flag)

if __name__ == '__main__' :
    SBPRS_app.run(debug=True)  # this command will enable the run of your flask app or api

    #,host="0.0.0.0")


from datetime import datetime, timedelta
import time as t
import data.mls_api_parser as mlsparser
import debug

NETWORK_RETRY_SLEEP_TIME = 10.0

class Data:
    def __init__(self, config):
        # Save the parsed config
        self.config = config

        # Flag to determine when to refresh data
        self.needs_refresh = True

        self.mls_logos = self.config.mls_logos

        # Parse today's date and see if we should use today or yesterday
        self.get_current_date()
        # Fetch the teams info
        self.refresh_games()

        # What game do we want to start on?
        self.current_game_index = 0
        self.current_division_index = 0
        # self.scores = {}

    def get_current_date(self):
        return datetime.now()

    def refresh_game(self):
        self.game = self.choose_game()
        self.needs_refresh = False

    def refresh_games(self):
        attempts_remaining = 5
        while attempts_remaining > 0:
            try:
                all_games = mlsparser.get_all_games()
                if self.config.rotation_only_preferred:
                    self.games = self.__filter_list_of_games(all_games, self.config.preferred_teams)
                # if rotation is disabled, only look at the first team in the list of preferred teams
                elif not self.config.rotation_enabled:
                    self.games = self.__filter_list_of_games(all_games, [self.config.preferred_teams[0]])
                else:
                    self.games = all_games

                self.games_refresh_time = t.time()
                self.network_issues = False
                break
            except Exception as e:
                self.network_issues = True
                debug.error("Networking error while refreshing the master list of games. {} retries remaining.".format(attempts_remaining))
                debug.error("Exception: {}".format(e))
                attempts_remaining -= 1
                t.sleep(NETWORK_RETRY_SLEEP_TIME)
            except ValueError:
                self.network_issues = True
                debug.error("Value Error while refreshing master list of games. {} retries remaining.".format(attempts_remaining))
                debug.error("ValueError: Failed to refresh list of games")
                attempts_remaining -= 1
                t.sleep(NETWORK_RETRY_SLEEP_TIME)

    #     # If we run out of retries, just move on to the next game
        if attempts_remaining <= 0 and self.config.rotation_enabled:
            self.advance_to_next_game()

    def get_gametime(self):
        tz_diff = t.timezone if (t.localtime().tm_isdst == 0) else t.altzone
        gametime = datetime.strptime(self.games[self.current_game_index]['date'], "%Y-%m-%dT%H:%MZ") + timedelta(hours=(tz_diff / 60 / 60 * -1))
        return gametime

    def current_game(self):
        return self.games[self.current_game_index]

    def advance_to_next_game(self):
        self.current_game_index = self.__next_game_index()
        return self.current_game()

    def __filter_list_of_games(self, games, teams):
        return list(game for game in games if set([game['awayteam'], game['hometeam']]).intersection(set(teams)))

    def __next_game_index(self):
        counter = self.current_game_index + 1
        if counter >= len(self.games):
            counter = 0
        return counter

    #
    # Debug info

    # def print_overview_debug(self):
    #     debug.log("Overview Refreshed: {}".format(self.overview.id))
    #     debug.log("Pre: {}".format(Pregame(self.overview, self.config.time_format)))
    #     debug.log("Live: {}".format(Scoreboard(self.overview)))
    #     debug.log("Final: {}".format(Final(self.current_game())))


import pandas as pd
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt

# Read the CSV file
data = pd.read_csv('train_absorption.csv')
# Extract features (descriptors) and target (Emission max)
X = data.drop(columns=['Tag', 'Name', 'Absorption max (nm)'])
y = data['Absorption max (nm)']
y = np.array(y)  # Convert to numpy array for better handling

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Scale the features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

# Build the neural network model
model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(64, activation='relu', input_shape=(X_train_scaled.shape[1],)),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(1)  # Output layer with no activation (linear activation)
])

# Compile the model
model.compile(loss='mean_squared_error', optimizer='adam')

# Train the model
model.fit(X_train_scaled, y_train, epochs=1000, batch_size=32, validation_split=0.2)

# Evaluate the model on the test set
mse = model.evaluate(X_test_scaled, y_test)
print("Mean Squared Error on test set:", mse)

# Predict on new data (if needed)
# Here, I'm keeping this part from the original code but be aware you'll likely need to preprocess the new data similarly to how you preprocessed 'train.csv'
new_data = pd.read_csv('train_absorption.csv')
X_new = new_data.drop(columns=['Tag', 'Name', 'Absorption max (nm)'])
X_new_scaled = scaler.transform(X_new)
predicted = model.predict(X_new_scaled)

y_test = y
y_pred = predicted

# Plot actual vs. predicted values
plt.scatter(y_test, y_pred)
plt.xlabel('Actual values')
plt.ylabel('Predicted values')
plt.title('Actual vs Predicted values [Emission]')
plt.plot([min(y_test), max(y_test)], [min(y_test), max(y_test)], color='red')  # y=x line
plt.show()

# Save the model
model.save('model_absorption.h5')


class ValidationError(Exception):
    """The validation exception"""

    message = "ValidationError"

    def __init__(self, message=None):
        self.message = message or self.message
        super(ValidationError, self).__init__(self.message)

class FusionComputeClientError(Exception):

    def __init__(self, message, server_traceback=None,
                 status_code=-1, error_code=None):
        super(FusionComputeClientError, self).__init__(message)
        self.status_code = status_code
        self.error_code = error_code
        self.server_traceback = server_traceback

    def __str__(self):
        if self.status_code != -1:
            return '{0}: {1}'.format(self.status_code, self.message)
        return self.message

class IllegalParametersError(FusionComputeClientError):
    ERROR_CODE = 'illegal_parameters_error'

class ServerNotFound(FusionComputeClientError):
    ERROR_CODE = 'server_not_found'

class ServerCreationFailed(FusionComputeClientError):
    ERROR_CODE = 'server_creation_failed'

ERROR_MAPPING = dict([
    (error.ERROR_CODE, error)
    for error in [
        IllegalParametersError,
        ServerNotFound,
        ServerCreationFailed
    ]])


text = input().split()

def swap(c, i, j):
    c = list(c)
    x=c[i]
    c[i], c[j] = c[j], x
    return "".join(c)

res=[]

for key in text:
    list_zero = []
    list_element = []
    [list_zero.append(i) for i in key if i.isdigit()]
    num = int("".join(list_zero))
    num = chr(num)
    list_element.insert(0,num)
    [list_element.append(j) for j in key if not j.isdigit()]

    res.append(swap(list_element, 1, - 1))
    res.append(" ")
    list_zero.clear()
    list_element.clear()

res="".join(res)

print(res)

#vtoro reshenie
def int_to_chr(word):
    string = list(word)
    num_as_str = list()

    while string[0].isdigit():
        num_as_str.append(string[0])
        string.pop(0)

    num = int(''.join(num_as_str))
    string.insert(0, chr(num))
    return ''.join(string)

def switch_letters(word):
    letters = list(word)
    letters[1], letters[-1] = letters[-1], letters[1]
    return ''.join(letters)

print(' '.join([switch_letters(int_to_chr(word)) for word in input().split()]))

Programming fundamentals with Python/05. Lists Advanced/Exercise/decipher_this!.py


from Adminview import *
class AdminMainPage(object):
    def __init__(self,info,master=None):
        self.information=info
        self.root = master  # 定义内部变量root
        self.root.geometry('%dx%d' % (500, 350))  # 设置窗口大小
        self.createPage()

    def createPage(self):
        self.infoPage = InfoFrame(self.information,self.root)
        self.infoPage.pack()


class Union_find:
    def __init__(self, stones):
        self.rep = {(stone[0], stone[1]): (stone[0], stone[1]) for stone in stones}
        self.size = defaultdict(lambda: 1)

    def find(self, x: int) -> int:
        parent = x

        while parent != self.rep[parent]:
            parent = self.rep[parent]

        while x != parent:
            prev_parent = self.rep[x]
            self.rep[x] = parent
            x = prev_parent

        return parent

    def union(self, x: int, y: int) -> None:
        x_rep = self.find(x)
        y_rep = self.find(y)

        greater = x_rep if self.size[x_rep] >= self.size[y_rep] else y_rep
        smaller = y_rep if greater == x_rep else x_rep
        self.rep[smaller] = greater
        self.size[greater] +=  self.size[smaller]

class Solution:
    def removeStones(self, stones: List[List[int]]) -> int:
        n = len(stones)
        uf = Union_find(stones)

        for i in range(n):
            for j in range(n):
                if stones[i][0] == stones[j][0] or stones[i][1] == stones[j][1]:
                    uf.union(tuple(stones[i]), tuple(stones[j]))

        components = 0
        for stone in uf.rep:
            if stone == uf.rep[(stone)]:
                components += 1

        return n - components


"""
5
AAAA
BBBBB
ABABABAB
BABABA
AAABBB
"""

nums = int(input())

def get_num_dels(s):
    res = 0
    prev = s[0]
    for c in s[1:]:
        if c == prev:
            res += 1
        else:
            prev = c
    return res

for n in range(nums):
    num_dels = get_num_dels(input())
    print(num_dels)


# coding=utf-8

"""
Module Docstring
"""

__all__ = ['BaseProcess']

from threading import Thread
from upy_rabbitmq.worker import UpyMQWorker

from app.models.work_user import WorkUser
from app.models.work_station import WorkStation
from app.models.work_process import WorkProcess

from app.base import BaseApp
from source.processes.callback import CallbackProcess

class BaseProcess(BaseApp):
    """BaseProcess
    """

    def __init__(self):
        super().__init__()
        self.work_station = self.env("WORK_STATION")

    def generate_keys(self):
        """Queue key list generation
        """
        query_work_station = WorkStation.query.filter(
            WorkStation.key == self.work_station,
            WorkStation.status == True
        ).first()

        query_users = WorkUser.query.join(
                WorkProcess,
                WorkProcess.work_user_id == WorkUser.id
            ).filter(
                WorkProcess.work_station_id == query_work_station.id,
                WorkProcess.status == True,
                WorkUser.status == True
            ).all()

        task_name = "task_" + str(self.work_station) + "_"

        list_queue = []
        if query_users:
            for user in query_users:
                key = task_name+str(user.company_data_id)
                list_queue.append(key)
                print(key)

            return list_queue

        return None

    def initialize(self, list_keys):
        """Process Initialization
        """
        try:
            for key in list_keys:
                process = Thread(
                    target=UpyMQWorker().start_queue,
                    kwargs={
                        "key": key,
                        "callback": CallbackProcess
                        }
                )
                process.start()
        except:
            self.fail_finalization()
        finally:
            self.finalize()

    def finalize(self, **kwargs):
        """Process Completion
        """
        pass

    def fail_finalization(self, **kwargs):
        """Failed Process Completion
        """
        pass