# -*- coding: utf-8 -*-
"""
Created on Tue Mar 26 11:38:27 2019

@author: x
"""

import torch
import torch.nn as nn
import torch.nn.functional as F

import numpy as np

import util

# # 传进来的 depth 明明是 disp 视差图
# def depth_smoothness(depth, img):
#     """Computes image-aware depth smoothness loss."""

#     def gradient_x(img):
#         return img[:, :, :-1, :] - img[:, :, 1:, :]

#     def gradient_y(img):
#         return img[:, :-1, :, :] - img[:, 1:, :, :]

#     depth_dx = gradient_x(depth)
#     depth_dy = gradient_y(depth)
#     image_dx = gradient_x(img)
#     image_dy = gradient_y(img)
#     weights_x = tf.exp(-tf.reduce_mean(tf.abs(image_dx), 3, keepdims=True))
#     weights_y = tf.exp(-tf.reduce_mean(tf.abs(image_dy), 3, keepdims=True))
#     smoothness_x = depth_dx * weights_x
#     smoothness_y = depth_dy * weights_y

#     return tf.reduce_mean(abs(smoothness_x)) + tf.reduce_mean(abs(smoothness_y))

def gradient_x(img):
    # Pad input to keep output size consistent
    img = F.pad(img, (0, 1, 0, 0), mode="replicate")
    gx = img[:, :, :, :-1] - img[:, :, :, 1:]  # NCHW
    return gx

def gradient_y(img):
    # Pad input to keep output size consistent
    img = F.pad(img, (0, 0, 0, 1), mode="replicate")
    gy = img[:, :, :-1, :] - img[:, :, 1:, :]  # NCHW
    return gy

def disp_smoothness(disp, img):
    disp_gradients_x = gradient_x(disp)
    disp_gradients_y = gradient_y(disp)

    image_gradients_x = gradient_x(img)
    image_gradients_y = gradient_y(img)

    weights_x = torch.exp(-torch.mean(torch.abs(image_gradients_x), 1, keepdim=True))
    weights_y = torch.exp(-torch.mean(torch.abs(image_gradients_y), 1, keepdim=True))

    smoothness_x = disp_gradients_x * weights_x
    smoothness_y = disp_gradients_y * weights_y

    return torch.mean( torch.abs(smoothness_x) ) + torch.mean( torch.abs(smoothness_y) )

def SSIM(x, y):
    # structural similarity index  结构相似性，是一种衡量两幅图像相似度的指标
    # https://blog.csdn.net/kevin_cc98/article/details/79028507
    C1 = 0.01 ** 2
    C2 = 0.03 ** 2

    mu_x = nn.AvgPool2d(3, 1)(x)
    mu_y = nn.AvgPool2d(3, 1)(y)
    mu_x_mu_y = mu_x * mu_y
    mu_x_sq = mu_x.pow(2)
    mu_y_sq = mu_y.pow(2)

    sigma_x = nn.AvgPool2d(3, 1)(x * x) - mu_x_sq
    sigma_y = nn.AvgPool2d(3, 1)(y * y) - mu_y_sq
    sigma_xy = nn.AvgPool2d(3, 1)(x * y) - mu_x_mu_y

    SSIM_n = (2 * mu_x_mu_y + C1) * (2 * sigma_xy + C2)
    SSIM_d = (mu_x_sq + mu_y_sq + C1) * (sigma_x + sigma_y + C2)
    SSIM = SSIM_n / SSIM_d

    return torch.clamp((1 - SSIM) / 2, 0, 1)

from params import args
seq_length = args.seq_length

num_scales = 4

# total_loss, reconstr_loss, smooth_loss, ssim_loss = \
# calc_total_loss(image_stack, disp, depth, depth_upsampled, egomotion, intrinsic_mat)
def calc_total_loss(image_stack, disp, depth, depth_upsampled, egomotion, intrinsic_mat):

    middle_frame_index = (seq_length-1)//2   # 0 1 2 中间是 1

    # self.images is organized by ...[scale][B, h, w, seq_len * 3].
    images = [None for _ in range(num_scales)]

    # 先把图片缩放，为后续计算loss做准备
    for s in range(num_scales):
        height_s = int( 128 / (2**s) )
        width_s = int( 416 / (2**s) )

        images[s] = [nn.functional.interpolate(x,
                                                size=[height_s, width_s],
                                                mode='bilinear',
                                                align_corners=True)
                                                for x in image_stack]

    smooth_loss = 0 # 计算各个尺度的 smooth_loss
    for s in range(num_scales):
        # Smoothness.
        for i in range(seq_length):
            compute_minimum_loss = True
            if not compute_minimum_loss or i == middle_frame_index:
                disp_smoothing = disp[i][s]
                mean_disp = torch.mean(disp_smoothing, (1, 2, 3), True)
                disp_input = disp_smoothing / mean_disp
                smooth_loss += ( 1.0 / (2**s) ) * disp_smoothness(disp_input, images[s][i])

    # Following nested lists are organized by ...[scale][source-target].
    warped_image = [{} for _ in range(num_scales)]
    warp_mask = [{} for _ in range(num_scales)]
    warp_error = [{} for _ in range(num_scales)]
    ssim_error = [{} for _ in range(num_scales)]

    reconstr_loss = 0
    ssim_loss = 0

    for s in range(num_scales):

        for i in range(seq_length):
            for j in range(seq_length):
                if i == j:
                    continue

                # When computing minimum loss, only consider the middle frame as target.
                if compute_minimum_loss and j != middle_frame_index:
                    continue

                exhaustive_mode = False
                if (not compute_minimum_loss and not exhaustive_mode and abs(i - j) != 1):
                    continue

                depth_upsampling = True
                selected_scale = 0 if depth_upsampling else s
                source = images[selected_scale][i]
                target = images[selected_scale][j]

                if depth_upsampling:
                    target_depth = depth_upsampled[j][s]
                else:
                    target_depth = depth[j][s]

                key = '%d-%d' % (i, j)
                # print("key:", key)

                # 这个时候传进来的egomotion的尺寸是 [batchsize, 2, 6]
                egomotion_mat_i_j = util.get_transform_mat(egomotion, i, j)
                # print("egomotion_mat_i_j size:\n", egomotion_mat_i_j.size() ) ([1, 4, 4])

                # print("intrinsic_mat size:", intrinsic_mat.size() )
                warped_image[s][key], warp_mask[s][key] = \
                    util.inverse_warp(source,
                                        target_depth.squeeze(1),
                                        egomotion_mat_i_j[:, 0:3, :],
                                        intrinsic_mat[:, selected_scale, :, :]
                                        )

                # Reconstruction loss.
                warp_error[s][key] = torch.abs(warped_image[s][key] - target)
                if not compute_minimum_loss:
                    reconstr_loss += torch.mean(warp_error[s][key] * warp_mask[s][key])

                # SSIM.
                ssim_error[s][key] = SSIM(warped_image[s][key], target)

                # TODO(rezama): This should be min_pool2d().
                if not compute_minimum_loss:
                    # ssim_mask = slim.avg_pool2d(warp_mask[s][key], 3, 1, 'VALID')
                    ssim_mask = nn.AvgPool2d(3, 1)(warp_mask[s][key])
                    ssim_loss += torch.mean(ssim_error[s][key] * ssim_mask)

    for s in range(num_scales):
        # If the minimum loss should be computed, the loss calculation has been
        # postponed until here.
        if compute_minimum_loss:
            for frame_index in range(middle_frame_index):
                key1 = '%d-%d' % (frame_index, middle_frame_index)
                key2 = '%d-%d' % (seq_length - frame_index - 1, middle_frame_index)

                # print('computing min error between %s and %s', key1, key2)

                min_error = torch.min(warp_error[s][key1], warp_error[s][key2])
                reconstr_loss += torch.mean(min_error)

                # Also compute the minimum SSIM loss.
                min_error_ssim = torch.min(ssim_error[s][key1], ssim_error[s][key2])
                ssim_loss += torch.mean(min_error_ssim)

    total_loss = 0.85*reconstr_loss + 0.04*smooth_loss + 0.15*ssim_loss
    return total_loss, reconstr_loss, smooth_loss, ssim_loss


from collections import deque
import math

n, l, r = map(int, input().split())
graph = []

dx = [1, -1, 0, 0]
dy = [0, 0, -1, 1]

for _ in range(n):
    graph.append(list(map(int, input().split())))

def bfs(x, y):
    q = deque()
    q.append((x, y))
    visited[x][y] = 1
    union = [(x, y)]
    count = graph[x][y]

    while q:
        x, y = q.popleft()

        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]

            if 0 <= nx < n and 0 <= ny < n:
                if visited[nx][ny] == 0:
                    if l <= abs(graph[nx][ny] - graph[x][y]) <= r:
                        union.append((nx, ny))
                        q.append((nx, ny))
                        visited[nx][ny] = 1
                        count += graph[nx][ny]
    for x, y in union:
        graph[x][y] = math.floor(count / len(union))

    return len(union)

result = 0
while True:
    visited = [[0] * n for _ in range(n)]
    flag = 0
    for i in range(n):
        for j in range(n):
            if visited[i][j] == 0:
                if bfs(i, j) > 1:
                    flag = 1

    if flag == 0:
        break
    result += 1

print(result)


import sys
import pygame
import pachouli
import scene
import danmoku
import enemy
from macrodf import *

def main():

	pygame.init()

	clock = pygame.time.Clock()

	size = (1280, 720)
	screen = pygame.display.set_mode(size)
	pygame.display.set_caption("TOUHOUBENTOU")

	#初始化帕奇类和陷阱类 同步进精灵组
	pachi = pachouli.Paqiuli()
	slimes = [enemy.Slime((145, 2050), (1000, 430)), enemy.Slime((145, 2050), (1800, 430))]
	cirnos = []
	summoncir = True

	font1 = pygame.font.Font("simhei.ttf", 16) #设置字体
	fontHeight = font1.get_linesize() # 获得字体的高度
	guideText = ["z-射鸡", "space-跳", "← →-移动", "↓-止血+蹲(无敌)"]

	trapR = pygame.sprite.Group()
	for each in pachi.scenes.traps:
		trapR.add(each)
	for each in slimes: #干脆把怪也加进去算了 反正都是撞
		trapR.add(each)

	dmk = []
	dmkR = pygame.sprite.Group()

	edmk = []
	edmkR = pygame.sprite.Group()

	pygame.event.set_blocked(pygame.MOUSEMOTION)
	while pachi.alive:
		for event in pygame.event.get():
			if event.type == pygame.QUIT or not pachi.alive:
				sys.exit()

			# 左右移动 下蹲 空格跳 s改变move方式
			if event.type == pygame.KEYDOWN:  # 检测type 如果是键盘按下则判断是否是方向键 用字典 event.key == K_键
				if event.key in pachi.keystatus:
					pachi.keystatus[event.key] = True
				if event.key == pygame.K_s:
					pachi.suberu = not pachi.suberu
				if event.key == pygame.K_z:
					if pachi.status != "fire":
						dmk.append(danmoku.Pafireball(pachi.isright, pachi.rect))
						dmkR.add(dmk[-1])
						pachi.status = "fire"
						pachi.statuslock = True

			if event.type == pygame.KEYUP:
				if event.key in pachi.keystatus:
					pachi.keystatus[event.key] = False

			if event.type == HURT:
				pachi.blink = not pachi.blink
				pachi.hp -= 4

		#screen.blit(background, (0, 0))

		pachi.move()

		if summoncir and pachi.scenes.left_border > 4400:
			print('1')
			summoncir = False
			for each in trapR:
				if hasattr(each, 'hp'):
					trapR.remove(each)
			del slimes[:]
			for i in range(2):
				cirnos.append(enemy.Cirno())
				trapR.add(cirnos[i])

		for each in slimes:
			each.move(pachi.rect, pachi.scenes.boderchanged, pachi.scenes.heightchanged)

		for each in cirnos:
			each.move(pachi.rect, pachi.scenes.boderchanged, pachi.scenes.heightchanged)
			if each.isfire:
				print("fireeee")
				each.isfire = False
				edmk.append(danmoku.Cirice(each.orientR, each.rect.center, pachi.rect.center))
				edmkR.add(edmk[-1])

		for each in dmk:
			each.move(pachi.scenes.boderchanged, pachi.scenes.heightchanged)
			if each.outrange:
				dmk.remove(each)
				dmkR.remove(each)

		for each in edmk:
			each.move(pachi.scenes.boderchanged, pachi.scenes.heightchanged)
			if each.outrange:
				edmk.remove(each)
				edmkR.remove(each)

		collide = pygame.sprite.spritecollide(pachi, trapR, False, pygame.sprite.collide_circle)
		if collide and not pachi.ishurt:
			pachi.ishurt = True
			pachi.attacked = True
			pachi.vx = 0
			pygame.time.set_timer(HURT, 150)
			print("collied")
			#pachi.alive = False
		#pachi.hurt(screen)
		collide = pygame.sprite.spritecollide(pachi, edmkR, False, pygame.sprite.collide_circle)
		if collide and not pachi.ishurt:
			pachi.ishurt = True
			pachi.attacked = True
			pachi.vx = 0
			pygame.time.set_timer(HURT, 150)
			print("collied")

		# 帕奇的子弹
		bulletcollide = pygame.sprite.groupcollide(dmkR, trapR, False, False, pygame.sprite.collide_circle)
		for bullet,target in bulletcollide.items(): #拆解dict 返回键和值的元祖
			if target:
				if hasattr(target[0], 'hp'):
					target[0].hurtAct()
					print(target[0].hp)
					if target[0].hp <= 0:
						trapR.remove(target[0])

					dmkR.remove(bullet)
					dmk.remove(bullet)

		pachi.show(screen)

		fontpos = 0
		for line in guideText:
			str1 = font1.render(line, True, (255,255,255))
			screen.blit(str1, (0, fontpos))
			fontpos += fontHeight

		for each in slimes:
			each.show(screen)
			if each.dead:
				slimes.remove(each)

		for each in cirnos:
			each.show(screen)
			if each.dead:
				cirnos.remove(each)

		for each in dmk:
			each.show(screen)
		for each in edmk:
			each.show(screen)

		if pachi.hp <= 0:
			pachi.alive = False

		pygame.display.flip()  # 更新界面

		clock.tick(60)

if __name__ == "__main__":
	main()


#!/usr/bin/python
################################################################################
from enum import Enum
from tools.common import infor, vipinfor

class ActionType(Enum):
    buy = 'buy'
    sell  = 'sell'

class EntryExitType(Enum):
    entry = 'entry'
    exit = 'exit'

class OrderType(Enum):
    market = 1
    limit  = 2

class TimeInForce(Enum):
    day = 'day'

class OrderStatus(Enum):
    fullExecute = 'fullExecute'
    partialExecute = 'partialExecute'
    reject = 'reject'

class OrderReply:
    def __init__(self, status, quant, price):
        self.status = status
        self.quant = quant
        self.price = price

class order:
    def __init__(self, oid, contractCode, quant, price, actionType, entryExitType, \
                        orderType, timeInForce):
        self.oid = oid
        self.contractCode = contractCode
        self.price = price
        self.quant = quant
        self.actionType = actionType
        self.entryExitType = entryExitType
        self.orderType = orderType
        self.timeInForce = timeInForce
        self.orderReply = None

    def display(self):
        c = '{contract} {action} {quant} @ {price} as {eet} with a {orderType} order.\
                 TimeInForce: {timeInForce}'. format(
                    contract = self.contractCode, \
                    action = self.actionType.value,\
                    quant = self.quant, \
                    price = self.price, \
                    eet = self.entryExitType,\
                    orderType = self.orderType,\
                    timeInForce = self.timeInForce)
        vipinfor(c)
        if self.orderReply == None:
            vipinfor('Order has not been replied.')
        else:
            c = 'Order status {status}, quantity {quant}, price {price}'.format(\
                status = self.orderReply.status,\
                quant = self.orderReply.quant,\
                price = self.orderReply.price)
            vipinfor(c)

    def setReply(self, reply):
        self.orderReply = reply


from ingestion.ingestion_pipeline_steps.interpreter_step import (  # noqa
    ColumnTypeInterpreter,
)
from pipeline.pipeline import IngestionPipeline
from sklearn.datasets import load_iris

def test_iris_types_numeric():
    pipeline = IngestionPipeline()
    pipeline.df = load_iris(return_X_y=True, as_frame=True)[0]
    pipeline.add(ColumnTypeInterpreter())
    pipeline.run()

    assert pipeline.column_type_map == {
        "sepal length (cm)": "numeric",
        "sepal width (cm)": "numeric",
        "petal length (cm)": "numeric",
        "petal width (cm)": "numeric",
    }


import os
import glob
import logging
import grpc

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

BASE_DIR = '/sys/bus/w1/devices/'
DEVICE_FOLDER_NAME = '/w1_slave'

device_folders = glob.glob(BASE_DIR + '28*')

def read_temp_raw(device_file):
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()

    return lines

def read_temp(context, sensor_id):
    if BASE_DIR + sensor_id not in device_folders:
        context.set_details('Cannot find sensor in devices directory')
        context.set_code(grpc.StatusCode.NOT_FOUND)
        return 0

    device_file = BASE_DIR + sensor_id + DEVICE_FOLDER_NAME
    lines = read_temp_raw(device_file)
    logging.debug('Read device lines: %s', lines)

    while lines[0].strip()[-3:] != 'YES':
        context.set_details('Sensor has no values')
        context.set_code(grpc.StatusCode.INTERNAL)
        logging.debug('Couldn\'t find temperature value')
        return 0

    if len(lines) > 1:
        equals_pos = lines[1].find('t=')
        if equals_pos != -1:
            temp_string = lines[1][equals_pos + 2:]
            temp_c = float(temp_string) / 1000.0
            logging.info('Parsed temperature value to celsius: %s', temp_c)

            return temp_c

    context.set_details('Sensor has no values')
    context.set_code(grpc.StatusCode.INTERNAL)
    logging.debug('Error parsing temperature value')

    return 0


#!/usr/bin/env python3

# Created by: Jacob Bonner
# Created on: September 2021
# This program draws a square with side lengths of 100 pixels and fills it blue

import pygame  # Importing the pygame library

def main():
	# This function will create a square with side lengths of 100 pixels and fills it blue

	# Initializing a surface object for the shapes to appear on (100 x 100)
	screen = pygame.display.set_mode((120, 120))
	screen.fill((255, 255, 255, 255))

	# Creating the square
	pygame.draw.rect(screen, pygame.Color((0, 0, 255)), pygame.Rect(10, 10, 100, 100))

	# Displaying the circle for a brief time
	pygame.display.update()
	pygame.time.delay(10000)

if __name__ == "__main__":
    main()


import requests # Biblioteca Python para trabalhar com HTTP

class BuscaEndereco:

    def __init__(self, cep):
        cep = str(cep)
        if self.valida_cep(cep):
            self.__cep = cep
        else:
            raise ValueError("CEP Inválido!!!!")

    def __str__(self):
        return self.formata_cep()

    @property
    def cep(self):
        return self.__cep

    @staticmethod
    def valida_cep(cep):
        if len(str(cep))==8:
            return True
        else:
            return False

    def formata_cep(self):
        return f"Cep: {self.__cep[:5]}-{self.__cep[5:]}"

    def busca_cep(self):
        url = f'https://viacep.com.br/ws/{self.__cep}/json/'
        request = requests.get(url)
        dados = request.json()
        return (
            dados["bairro"],
            dados["localidade"],
            dados["uf"]
        )


import sys
import os
from subprocess import Popen, PIPE
from multiprocessing import Process
import time
import random
import threading
import torch

#NUM_GPU = 2
NUM_GPU = torch.cuda.device_count()
# convert the txt file content to list
def txt2list(input_file):
    env_list = []

    with open(input_file) as f:
        Content = f.read()
        line_counter = 0
        lines = Content.split("\n")
        for i in lines :
            if i:
                line_counter+=1
            proc_list = lines[:line_counter]

        for l in proc_list:
            l  = l.strip('\n')
            e = l.split(",")
            env_list.append(e)

    return env_list[1:]

# sort the executed task into seperate folder
def sort_task(input_file,shared_list):

    current_proc = shared_list[0]
    output_file = 'done.txt'

    # add to output file
    str1 = ','.join(current_proc)
    d = open(output_file, 'a+')
    d.seek(0)
    data = d.read(10000)
    if len(data) > 0 :
        d.write("\n")
    d.write(str1)

    # delete first row from environment.txt
    with open(input_file, 'r') as fin:
        data1 = fin.read().splitlines(True)
        first_line = data1[0]
    with open(input_file, 'w') as fout:
        fout.writelines(first_line)
        fout.writelines(data1[2:])

    return current_proc

# activate environment and run python script
def process(input_file,shared_list,gpu_index):

    get_gpu = '-gpu {}'.format(gpu_index)
    current_proc = sort_task(input_file,shared_list)
    env_name, path, script = [current_proc[i] for i in (0, 1, 2)]
    os.chdir(path)
    proc = Popen("conda activate "+ env_name+ " && python "+script+" "+get_gpu,
                    shell=True,)

    # wait for subprocess to finish only will the multiprocess ends
    while proc.poll() is None:
        time.sleep(5) # check if the subprocess is still running every 5 seconds

# initialize the available GPUs
def initialize_gpu(number_of_processes):
    list_gpu = []

    for i in range(number_of_processes):
        list_gpu.append(1) # 1 = available, 0 =not available
    return list_gpu

# check if there is any gpu available
def get_availability(list_gpu):

    if 1 in list_gpu:
        return True
    else:
        return False

def create_multiprocess(input_file,gpu_index):

    shared_list = txt2list(input_file)
    proc = Process(target=process,args=(input_file,shared_list,gpu_index))

    return proc

def start_process(proc):
    proc.start()

    if proc.is_alive:
        print("\n")
        print(proc.name," has started\n")

# add process to a list and chg the gpu to unavailable
def allocate_gpu(proc,p_list,list_gpu):

    for i,process in enumerate(p_list):
        if process =='':
            p_list[i] = proc
            break
    process_index =p_list.index(proc)
    list_gpu[process_index] = 0

# check if the process has ended
def check_status(p_list,list_gpu):

    for i,p in enumerate(p_list):
        if p.exitcode==0:
            list_gpu[i]=1   # change the gpu asssigned to the process to available
            p_list[i]=''   # remove the process once it is finish
            print("\n",p.name," has ended")

def run(input_file):

    list_gpu = initialize_gpu(NUM_GPU)
    p_list = []

    # initialize to only n process in the list
    for i in range(NUM_GPU):
        p_list.append('')

    while os.stat(input_file).st_size != 0:
        x = random.randint(1,4)

        if get_availability(list_gpu) ==True:
            available_gpu = list_gpu.index(1)
            if txt2list(input_file):
                proc = create_multiprocess(input_file,available_gpu)
                allocate_gpu(proc,p_list,list_gpu)
                start_process(proc)
        else:
            check_status(p_list,list_gpu)
        time.sleep(x) # add delay so processes won't crash into each other


from torch.utils import data
from torchvision import transforms, utils
from pathlib import Path
from PIL import Image
import os
import numpy as np
import torch

# helpers functions
def cycle(dl):
    while True:
        for data in dl:
            yield data

# fscoco datasets
class TripleDataset(data.Dataset):
    def __init__(self, photo_root, sketch_root, text_root):
        super(TripleDataset, self).__init__()

        self.tranform = transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(256),
            transforms.ToTensor(),
            # transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
        ])
        # tranform rgb to sketch
        self.sketch_tranform = transforms.Compose([transforms.functional.rgb_to_grayscale])

        classes, class_to_idx = self.find_classes(photo_root)

        self.photo_root = photo_root
        self.sketch_root = sketch_root
        self.text_root = text_root

        self.photo_paths = sorted(self.make_dataset(self.photo_root))
        self.classes = classes
        self.class_to_idx = class_to_idx

        self.len = len(self.photo_paths)

    def __getitem__(self, index):

        photo_path = self.photo_paths[index]
        sketch_path, label, text = self._getrelate_sketch(photo_path)

        photo = Image.open(photo_path).convert('RGB')
        sketch = Image.open(sketch_path).convert('RGB')

        P = self.tranform(photo)
        S = self.tranform(sketch)
        # S = self.sketch_tranform(S) # tranform rgb to gray
        L = label
        T = text
        return {'P': P, 'S': S, 'L': L, 'T': T}

    def __len__(self):
        return self.len

    def make_dataset(self, root):
        images = []
        cnames = os.listdir(root)
        for cname in cnames:
            c_path = os.path.join(root, cname)
            if os.path.isdir(c_path):
                fnames = os.listdir(c_path)
                for fname in fnames:
                    path = os.path.join(c_path, fname)
                    images.append(path)
        return images

    def find_classes(self, root):
        classes = [d for d in os.listdir(root) if os.path.isdir(os.path.join(root, d))]
        classes.sort()
        class_to_idex = {classes[i]: i for i in range(len(classes))}
        return classes, class_to_idex

    def _getrelate_sketch(self, photo_path):

        paths = photo_path.split('/')
        fname = paths[-1].split('.')[0]
        cname = paths[-2]
        label = self.class_to_idx[cname]
        sketchs = sorted(os.listdir(os.path.join(self.sketch_root, cname)))
        sketch_rel = []
        for sketch_name in sketchs:
            if sketch_name.split('.')[0] == fname:
                sketch_rel.append(sketch_name)
        rnd = np.random.randint(0, len(sketch_rel))
        sketch = sketch_rel[rnd]
        # load text
        text_sigle = sketch.split('.')[0] + '.txt'
        sketch_path = os.path.join(self.sketch_root, cname, sketch)
        text_path = os.path.join(self.text_root, cname, text_sigle)
        f = open(text_path)
        text = f.read()
        # text remove "\n" and "."
        text = text.replace(".", "").replace("\n", "")
        f.close()
        return sketch_path, label, text

photo_root = "/root/sketchimage/fscoco-main/fscoco/fscoco/images"
sketch_root = "/root/sketchimage/fscoco-main/fscoco/fscoco/raster_sketches"
text_root = "/root/sketchimage/fscoco-main/fscoco/fscoco/text"
ds = TripleDataset(photo_root=photo_root, sketch_root=sketch_root, text_root=text_root)
dl = cycle(data.DataLoader(ds, batch_size=4, drop_last=True, shuffle=True, pin_memory=True))

if __name__ == "__main__":
    index = 0
    while True:
        index += 1
        data = next(dl)
        data_image = data["P"]
        data_sketch = data["S"]
        data_label = data["L"]
        data_text = data["T"]
        print(data_image.size(), data_sketch.size(), data_label, data_text)
        # save_data = torch.cat((data_image, data_sketch), dim=0)
        # utils.save_image(save_data, f"./test/{data_text}.png")


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

files = [['seven_agents.csv', 'Seven Agents'],
         ['single_agent.csv', 'Single Agent'],
         ['twelve_agents.csv', 'Twelve Agents'],
         ['twentyfour_agents.csv', 'Twenty Four Agents']]

def create_plt_data(data):
    count = 0
    y = []
    x = []
    for d in data:
        if d[2] < 1:
            count += 1
        x.append(d[3])
        y.append(count)
    return x, y

def plot_csv_data(file_names):
    plt.figure()
    for f in file_names:
        data = pd.read_csv('./supervised_data/' + f[0], delimiter=';').to_numpy()
        x, y = create_plt_data(data)
        plt.plot(x, y, label=f[1])
    plt.title('Successful Throws Over Time')
    plt.xlabel('Time in seconds')
    plt.ylabel('Cumulative Successful Throws')
    plt.legend()
    plt.savefig('./fig/throw.svg')
    plt.savefig('./fig/throw.png')
    plt.show()

plot_csv_data(files)


from webob.exc import HTTPUnauthorized

from repoze.bfg.chameleon_zpt import render_template_to_response
from karl.views.api import TemplateAPI
from karl.utils import get_setting

def login_view(context, request):

    system_name = get_setting(context, 'system_name', 'KARL')

    page_title = '' # Per #366377, don't say what screen
    api = TemplateAPI(context, request, page_title)

    came_from = request.params.get('came_from', request.url)

    if came_from.endswith('login.html'):
        came_from = came_from[:-len('login.html')]
    elif came_from.endswith('logout.html'):
        came_from = came_from[:-len('logout.html')]

    api.status_message = status_message=request.params.get('reason', None)
    response = render_template_to_response(
        'templates/login.pt',
        api=api,
        came_from=came_from,
        nothing='',
        app_url=request.application_url,
        )
    plugins = request.environ.get('repoze.who.plugins', {})
    auth_tkt = plugins.get('auth_tkt')
    if auth_tkt is not None:
        forget_headers = auth_tkt.forget(request.environ, {})
        response.headers.update(forget_headers)
    return response

def logout_view(context, request, reason='Logged out'):
    unauthorized = HTTPUnauthorized()
    unauthorized.headerlist.append(
        ('X-Authorization-Failure-Reason', reason))
    return unauthorized


from langpack.contrib.django import trans, localize, trans_lazy
from nose import tools as test
from datetime import datetime

from .cases import AppTestCase

class TestShortcuts(AppTestCase):
    def test_trans(self):
        translator = self.get_translator()
        translator.add_translations('en', {'foo': 'bar'})
        test.assert_equal(trans('foo'), 'bar')
        test.assert_equal(trans_lazy('foo'), 'bar')

    def test_localize(self):
        translator = self.get_translator()

        translator.add_translations('en', {
            'datetime': {
                'formats': {
                    'short': '%d.%M.%Y',
                },
            },
        })

        translator.add_formatter(self.format_datetime, ['datetime'])
        now = datetime.now()

        assert localize(now, 'short') == now.strftime('%d.%M.%Y')
        assert localize(now, '%d.%M.%Y') == now.strftime('%d.%M.%Y')

    @staticmethod
    def format_datetime(value, format, translator):
        format = translator.get_template('datetime.formats.' + format, default=format)
        return value.strftime(format)


import pandas as pd
from IPython.display import display
import numpy as np

class netflixTopRecommenderSystem:
    # Constructor to initialize instance variables
    def __init__(self, datasetPath, genre, VotesThrs, TopN):
        self._datasetPath = datasetPath
        self._genre = genre
        self._voteThrs = VotesThrs
        self._topN = TopN
        self.df = None
        self._minVotes = None

        self.colDrop = ['year', 'certificate',
                        'duration', 'description', 'stars']
        self.colTitle = {"votes": "votes", "genre": "genre",
                         "title": "title", "rating": "rating"}

    # Method to read the csv file
    def readFile(self):
        self.df = pd.read_csv(self._datasetPath)

    # Method to clean the dataset
    def cleanDataset(self):

        # Dropping the unnecessary columns from the dataset
        for items in self.colDrop:
            self.df = self.df.drop([items], axis=1)

        # Dropping the duplicate rows based on the title column
        self.df = self.df.drop_duplicates(subset=[self.colTitle['title']])

        # Converting the votes column to float data type
        if type(self.df['votes'][0]) == str:
            self.df[self.colTitle['votes']] = self.df[self.colTitle['votes']].str.replace(
                ',', '').astype(float)

        # Filling the missing values with 0
        self.df = self.df.fillna(0)

    # Method to filter the dataset based on genre
    def filterGenre(self):
        self.df = self.df[self.df[self.colTitle['genre']].str.contains(
            self._genre, case=False).fillna(False)]

    # Method to filter the dataset based on the minimum number of votes threshold
    def threshVotes(self):
        self._minVotes = np.quantile(
            self.df[self.colTitle[self.colTitle['votes']]], self._voteThrs)
        self.df = self.df.drop(self.df[self.df.votes < self._minVotes].index)

    # Method to calculate the weighted average score for each title
    def weightedAvgScore(self):
        weightedAvg = []
        mean = self.df[self.colTitle['rating']].mean()
        self.df = self.df.reset_index(drop=True)

        for i in range(0, len(self.df[self.colTitle['rating']])):
            res = (self.df[self.colTitle['votes']][i]/(self.df[self.colTitle['votes']][i] + self._minVotes) *
                   self.df[self.colTitle['rating']][i]) + (self._minVotes/(self.df[self.colTitle['votes']][i]+self._minVotes))*mean
            weightedAvg.append(res)
        self.df["WeightedAvg"] = weightedAvg

    # Method to sort the titles based on the weighted average score and display top N titles
    def sortNscores(self):
        sort = self.df.sort_values(
            "WeightedAvg", ascending=False).head(self._topN)
        display(sort)

    # Method to execute all the steps of the recommender system
    def run(self):
        self.readFile()
        self.cleanDataset()
        self.filterGenre()
        self.threshVotes()
        self.weightedAvgScore()
        self.sortNscores()

path = "/home/hanzala/Development/Python_the_ultimate_course_2023/26. Portfolio Project Netflix Recommender Systems Introduction/NetflixDatasetMovies.csv"
best5Comedy = netflixTopRecommenderSystem(path, "Comedy", 0.8, 5)
best5Comedy.colDrop = ['year', 'certificate',
                       'duration', 'description', 'stars']
best5Comedy.colTitle = {"votes": "votes", "genre": "genre",
                        "title": "title", "rating": "rating"}
best5Comedy.run()

26. Portfolio Project Netflix Recommender Systems Introduction/Project.py


import aiida

aiida.load_profile()

def test_multiply_link():
    """Test multiply link."""

    from aiida_worktree import node, WorkTree
    from aiida.orm import Float, load_node

    @node.calcfunction()
    def sum(inputs):
        total = 0
        for input in inputs:
            total += load_node(input).value
        return Float(total)

    wt = WorkTree(name="test_multiply_link")
    float1 = wt.nodes.new("AiiDANode", value=Float(1.0).store())
    float2 = wt.nodes.new("AiiDANode", value=Float(2.0).store())
    float3 = wt.nodes.new("AiiDANode", value=Float(3.0).store())
    gather1 = wt.nodes.new("AiiDAGather", "gather1")
    sum1 = wt.nodes.new(sum, "sum1")
    wt.links.new(float1.outputs[0], gather1.inputs[0])
    wt.links.new(float2.outputs[0], gather1.inputs[0])
    wt.links.new(float3.outputs[0], gather1.inputs[0])
    wt.links.new(gather1.outputs[0], sum1.inputs[0])
    wt.submit(wait=True)
    assert sum1.node.outputs.result.value == 6


import time
import random
import numpy as np
from scipy import misc

# import code
# code.interact(local=dict(globals(), **locals()))
# assert False

omniglot_train_images = np.load('./data_omniglot/train.npy')
omniglot_test_images = np.load('./data_omniglot/test.npy')

IMAGE_HEIGHT = 28#20
IMAGE_WIDTH = 28#20
NUM_CLASSES = omniglot_train_images.shape[0]

def _preprocess_images(omniglot_images):
  num_classes, examples_per_class, raw_image_height, raw_image_width = omniglot_images.shape
  small_ims = np.zeros([num_classes, examples_per_class, IMAGE_HEIGHT, IMAGE_WIDTH], dtype=np.uint8)
  for c in range(num_classes):
    for e in range(examples_per_class):
      small_ims[c,e] = misc.imresize(omniglot_images[c,e], [IMAGE_HEIGHT, IMAGE_WIDTH])#, interp='nearest')
  return small_ims

# hack to speed things up
omniglot_train_images = _preprocess_images(omniglot_train_images)
omniglot_test_images = _preprocess_images(omniglot_test_images)

def get_episode(time_steps, classes_per_episode, num_labels, use_test_data):
  omniglot_images = omniglot_test_images if use_test_data else omniglot_train_images
  num_classes, examples_per_class, raw_image_height, raw_image_width = omniglot_images.shape

  # print use_test_data
  # if use_test_data:
  #   assert np.all(omniglot_images == omniglot_test_images)
  # else:
  #   assert np.all(omniglot_images == omniglot_train_images)

  # choose classes
  classes = random.sample(range(num_classes), classes_per_episode)

  # choose labels
  class_labels = random.sample(range(classes_per_episode), classes_per_episode)

  # choose rotation for each class
  class_rotation = np.random.choice(range(4), classes_per_episode)

  # choose images
  # NOTE: this is actually slower than data_mnist, which it shouldn't be, too much sampling I think
  samples_per_class = random.sample(range(classes_per_episode)*examples_per_class, time_steps)
  indices = [random.sample(range(examples_per_class), samples_per_class.count(i)) for i in range(classes_per_episode)]
  labels = [[class_labels[c]]*len(cs) for c, cs in enumerate(indices)]
  labels = [item for sublist in labels for item in sublist]

  indices = [zip([classes[c]]*len(cs), cs) for c, cs in enumerate(indices)]
  indices = [item for sublist in indices for item in sublist]

  shuffled_order = random.sample(range(time_steps), time_steps)
  labels = [labels[i] for i in shuffled_order]
  indices = [indices[i] for i in shuffled_order]

  indices = zip(*indices)
  images_raw = omniglot_images[indices[0], indices[1], :, :]

  # apply perturbations to each image
  images = np.zeros([time_steps, IMAGE_HEIGHT, IMAGE_WIDTH], dtype=np.float32)
  for i in range(time_steps):
    #255 - images_raw[i].astype(np.uint8)*255
    im = images_raw[i]

    # class rotation (0, pi/2, pi, 3*pi/2)
    im = np.rot90(im, k=class_rotation[labels[i]])

    # # mild rotation (-pi/16, pi/16)
    # im = misc.imrotate(im, np.random.random()*(np.pi/8.0)-(np.pi/16.0))

    # # translate (+/- 10 pixels)
    # im = np.pad(im, 10, 'constant', constant_values=0.0)
    # offset = np.random.randint(20, size=2)
    # im = im[offset[0]:offset[0]+raw_image_height, offset[1]:offset[1]+raw_image_width]

    # downsample (20x20)
    #im = misc.imresize(im, [20,20], interp='nearest')

    images[i] = im/255.0

  # insert extra labels that are never used
  if classes_per_episode < num_labels:
    mapping = random.sample(range(num_labels), classes_per_episode)
    labels = [mapping[label] for label in labels]

  # convert labels to one-hot
  labels = np.eye(num_labels)[labels]
  last_labels = np.zeros([time_steps, num_labels], dtype=np.float32)
  #labels = np.eye(classes_per_episode)[labels]
  #last_labels = np.zeros([time_steps, classes_per_episode], dtype=np.float32)
  last_labels[1:,:] = labels[:-1,:]

  return images, labels, last_labels

def get_batch_of_episodes(batch_size, time_steps, classes_per_episode=5, num_labels=5, use_test_data=False):
  images, labels, last_labels = zip(*[get_episode(time_steps, classes_per_episode, num_labels, use_test_data) for _ in range(batch_size)])
  return np.array(images), np.array(labels), np.array(last_labels)

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

# images, labels, last_labels = get_perturbed_batch_of_episodes(25, 50)

# print "starting test"
# start_time = time.time()
# for i in range(1000):
#   ims, lbls, last_lbls = get_perturbed_batch_of_episodes(25,50) # 10.3s for all 1000=25000 episodes
# duration_s = time.time() - start_time
# print "finished test"
# print duration_s


#! /usr/bin/env python3
# coding=utf-8

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

from absl import logging
from absl.testing import absltest, parameterized

from stable_alignment.sandbox import Agent

logging.set_verbosity("info")
logging.set_stderrthreshold("info")

class AgentTest(parameterized.TestCase):
    """Test cases for Social Agents."""

    def test_agent_internal_memory(self):
        """Test the save/update functions of agent's internal memory."""
        agent = Agent(agent_id=19, location=(3, 4), world_id=0, label="good")
        self.assertEqual(agent.model_type, "text-davinci-002")
        agent.reset_memory()
        agent.save_int_memory(
            question="What's the weather like today?", answer="It is pretty good!"
        )
        self.assertDictEqual(
            agent.internal_mem, {"What's the weather like today?": "It is pretty good!"}
        )

    def test_agent_response(self):
        """Test whether the agent is able to generate self-consistent answers."""
        agent = Agent(
            agent_id=0,
            location=(0, 0),
            model_type="text-davinci-003",
            world_id=0,
            label="good",
        )
        self.assertEqual(agent.model_type, "text-davinci-003")
        agent.reset_memory()
        agent.save_int_memory(
            question="Do you love any ball games?",
            answer="I love all of them except basketball!",
        )
        logging.info(
            agent.response("Do you want to play basketball with me?", verbose=True)
        )

    def test_agent_response_chat_gpt(self):
        """Test whether the agent is able to generate answers with GPT-3.5 engine."""
        agent = Agent(
            agent_id=0,
            location=(0, 0),
            model_type="gpt-3.5-turbo",
            world_id=0,
            label="good",
        )
        self.assertEqual(agent.model_type, "gpt-3.5-turbo")
        agent.reset_memory()
        agent.save_int_memory(
            question="Do you love any ball games?",
            answer="I love all of them except basketball!",
        )
        logging.info(
            agent.response("Do you want to play basketball with me?", verbose=True)
        )

    def test_agent_init_with_paths_no_world_id(self):
        """Test that we can initialize the agent with only memory and embedding paths."""
        agent = Agent(
            agent_id=1,
            location=(0, 1),
            int_mem_path="./data/cache/world_0/internal_memory/agent_1.pkl",
            int_mem_emb_path="./data/cache/world_0/internal_memory/agent_1_emb.pkl",
            ext_mem_path="./data/cache/world_0/external_memory/agent_1.jsonl",
            label="good",
        )
        agent.reset_memory()
        agent.save_int_memory(
            question="Do you love any ball games?",
            answer="I love all of them except basketball!",
        )
        logging.info(
            agent.response("Do you want to play basketball with me?", verbose=True)
        )

    def test_agent_init_with_paths_expect_fail(self):
        """Test that initializing the agent with no world id and not all three paths would assert false."""
        try:
            Agent(
                agent_id=1,
                location=(0, 1),
                model_type="text-davinci-002",
                int_mem_path="./data/cache/world_0/internal_memory/agent_1.pkl",
                int_mem_emb_path="./data/cache/world_0/internal_memory/agent_1_emb.pkl",
                label="good",
            )
        except AssertionError as e:
            logging.info(str(e))
        else:
            self.fail("Should raise an AssertionError.")

if __name__ == "__main__":
    absltest.main()


import os
import datetime

import pytz
from babel.core import Locale
from babel.support import Format
from pyramid import events, security
from pyramid.i18n import get_locale_name

from sngconnect.database import DBSession, User
from sngconnect.accounts.forms import SignOutForm

@events.subscriber(events.BeforeRender)
def add_google_maps_api_key(event):
    event['google_maps_api_key'] = (
        event['request'].registry.settings['google_maps.api_key']
    )

@events.subscriber(events.BeforeRender)
def add_currency(event):
    event['currency_format'] = (
        event['request'].registry.settings['sngconnect.currency_format'].decode('utf-8')
    )

@events.subscriber(events.BeforeRender)
def add_sign_out_form(event):
    event['sign_out_form'] = SignOutForm(
        csrf_context=event['request']
    )

@events.subscriber(events.BeforeRender)
def add_user(event):
    user_id = security.authenticated_userid(event['request'])
    if user_id is None:
        event['user'] = None
        timezone = None
    else:
        event['user'] = DBSession.query(User).filter(
            User.id == user_id
        ).one()
        timezone = event['user'].timezone
    if timezone is None:
        timezone = event['request'].registry['default_timezone']
    event.update({
        'timezone_offset': int(
            pytz.utc.localize(
                datetime.datetime.utcnow()
            ).astimezone(timezone).utcoffset().seconds * 1000
        ),
        'format': Format(
            Locale(get_locale_name(event['request'])),
            timezone
        )
    })

@events.subscriber(events.BeforeRender)
def add_permissions(event):
    event.update({
        'can_access_devices': security.has_permission(
            'sngconnect.devices.access',
            event['request'].context,
            event['request']
        ),
        'can_access_appearance': security.has_permission(
            'sngconnect.appearance.access',
            event['request'].context,
            event['request']
        ),
        'can_access_announcements': security.has_permission(
            'sngconnect.announcements.access',
            event['request'].context,
            event['request']
        ),
        'can_create_feed': security.has_permission(
            'sngconnect.telemetry.create_feed',
            event['request'].context,
            event['request']
        ),
    })

@events.subscriber(events.BeforeRender)
def add_appearance_stylesheet_url(event):
    request = event['request']
    assets_path = request.registry['settings'][
        'sngconnect.appearance_assets_upload_path'
    ]
    stylesheet_filename = request.registry['settings'][
        'sngconnect.appearance_stylesheet_filename'
    ]
    event['appearance_stylesheet_url'] = request.static_url(
        os.path.join(assets_path, stylesheet_filename)
    )


import datetime
import json
import os

import requests
from dotenv import load_dotenv

load_dotenv()
AUTH_TOKEN=os.getenv('AUTH_TOKEN')
REQUEST_URL=os.getenv('REQUEST_URL')
def save_last_update(created_at):
    with open("./last_update.json","w") as f:
        json.dump({"last_update":created_at},f)

def get_last_update():
    with open("./last_update.json","r")as f:
        json_data =json.load(f)
    return json_data["last_update"]

def get_message():
    last_update = datetime.datetime.strptime(get_last_update(),"%Y-%m-%dT%H:%M:%SZ")
    response = requests.get(REQUEST_URL,headers={"Authorization":f"Bearer {AUTH_TOKEN}"})
    github_log = reversed(response.json())
    message=""
    for log in github_log:
        created_at = datetime.datetime.strptime(log["created_at"],"%Y-%m-%dT%H:%M:%SZ")
        if created_at > last_update:
            if log["type"] == "PushEvent":
                message+=(message_format(log))

    save_last_update(log["created_at"])
    return message

def message_format(log):
    return f'```{log["actor"]["login"]}さんがpushしました。\nmessage:{log["payload"]["commits"][0]["message"]}```\n'

if __name__=="__main__":
    get_message()


from bs4 import BeautifulSoup
import requests
def count_problems(link):
    source  = requests.get(link).text
    soup = BeautifulSoup(source, 'lxml').body

    content = (soup.find('table', {'class': 'problems'}))
    if content == None :
        return " ";
    problems = content.findAll('tr')
    res = ""
    for i in range(len(problems)):
        if i > 0 :
            problem =  problems[i].td.a.text
            problem = problem.split(' ')
            # print(problem)
            for j in problem:
                if len(j) and j != '\r\n':
                    j = j.split('\r')
                    res = res + j[0] + " "
    return res
print(count_problems('https://codeforces.com/contest/1392'))


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

"""App initialization part, registering global
methods or procedures to the app instance.
"""

import logging
import markdown2
from flask import Flask, Markup
import config
from database import Database
from models import Tag, Category, fetch_all_instances

logger = logging.getLogger(__name__)
# initialize app
app = Flask(__name__)
app.secret_key = app.config['SECRET_KEY']
app.config.from_object(config)
app.db = Database(app)
app.config['post_tags'] = {}
app.config['categories'] = []

@app.teardown_request
def close_session(exception=None):
    if not exception:
        app.db.session.commit()
        app.db.remove_current_session()
    else:
        app.db.roll_back_current_session()
        app.db.remove_current_session()
        app.db.dispose_pool()

@app.before_first_request
def init_app():
    # get some global objects
    # create tag dictionary for app-wide use
    if not app.config['post_tags']:
        for tag in Tag.get_all_tags():
            app.config['post_tags'][tag.name] = tag.id

    if not app.config['categories']:
        app.config['categories'] = Category.fetch_all_categories()

@app.template_filter('parse_markdown')
def parse_markdown(markdown_text):
    return Markup(markdown2.markdown(markdown_text))


# %%
import os
import logging
from asyncio.log import logger
import glob
import shutil
import time
import re
import zipfile
import pandas as pd
import sqlite3

# %%
class Thermo:

    def __init__(self, config=None):
        try:
            logger = logging.getLogger(__name__)
            logging.basicConfig(filename="thermo.log", filemode="a", format="%(asctime)s %(levelname)s %(message)s", level=logging.INFO)
            logger.info("Class 'Thermo' initialized successfully.")

            # assign variables
            self.config = config

        except Exception as err:
            logger = logging.getLogger(__name__)
            logger.error("Error initializing class 'Thermo'.", err)

    def extract_file(self, file: str, log=True) -> pd.DataFrame:
        """
        Open a file, determine its type from the file name, then extract content into a Pandas dataframe.

        Args:
            file (str): full path to file.
            log (bln): Should activities be logged to 'thermo.log'? Defaults to True.
        """
        try:
            msg = f"Extracting file {file}."
            if log:
                logger.info(msg)

            # df = pd.DataFrame()

            if bool(re.search('.zip', file)):
                zf = zipfile.ZipFile(file)
                tmp = zf.open(zf.namelist()[0])
                df = pd.read_csv(tmp, sep="\s+")
            else:
                df = pd.read_csv(file, sep="\s+", engine='python')

            df['dtm'] = pd.to_datetime(df['pcdate'] + ' ' + df['pctime'], format="%Y-%m-%d %H:%M:%S")
            df['source'] = file
            if 'hio3' in df.columns:
                df.drop(columns='hio3', inplace=True)
            df.set_index('dtm', inplace=True)

            if not df.empty:
                for column in df:
                    if df[column].dtype == 'float64':
                        df[column] = pd.to_numeric(df[column], downcast='float')
                    if df[column].dtype == 'int64':
                        df[column] = pd.to_numeric(df[column], downcast='integer')
            return df

        except Exception as err:
            logger.error(err)
            return pd.DataFrame()

    def extract_files(self, path: str, pattern=["tei49c", "tei49i"], recursive=False, archive=None, remove_duplicates=True, save=None, log=True) -> pd.DataFrame:
        """
        Scan a directory and combine file content into a Pandas dataframe.

        Args:
            path (str): path to directory.
            recursive (bln): Should sub-directories be considered? Defaults to False.
            pattern (list): Pattern for recognition of bulletin files. Defaults to ["tei49c", "tei49i"]
            archive (str): If specified, files are moved to <path>/<archive>. Defaults to None.
            remove_duplicates (bln): Remove duplicates found in resulting data frame? Defaults to True.
            save (str): If one of ["csv", "json", "pkl"], resulting data frame is persisted to file. Defaults to None.
            log (bln): Should activities be logged to 'thermo.log'? Defaults to True.
        """
        try:
            msg = f"Extracting files found at '{path}' with pattern '{pattern}' ..."
            if log:
                logger.info(msg)

            df = pd.DataFrame()

            for p in pattern:
                if recursive:
                    pathname = os.path.join(path, f"**/{p}")
                else:
                    pathname = os.path.join(path, f"{p}")
                files = glob.glob(pathname=pathname, recursive=recursive)
                msg = f"Found {len(files)} files to extract and combine."
                if log:
                    logger.info(msg)

                for file in files:
                    df = pd.concat([df, self.extract_file(file=file, log=log)])
                    if archive:
                        dst = file.replace("incoming", "archive")
                        os.makedirs(os.path.dirname(dst), exist_ok=True)
                        shutil.move(src=file, dst=dst)

                if remove_duplicates:
                    numrows = len(df)
                    df.drop_duplicates(subset=df.columns[df.columns != "source"], inplace=True)
                    if len(df) < numrows:
                        logger.info(f"{numrows-len(df)} duplicate entries were found and removed.")

                if save:
                    dst = os.path.join(path, f"{p}-{time.strftime('%Y%m%d%H%M%S')}.{save}")
                    if save=="csv":
                        df.to_csv(dst)
                    elif save=="json":
                        df.to_json(dst)
                    elif save=="pickle":
                        df.to_pickle(dst)
                    else:
                        raise ValueError("'save' must be one of ['csv', 'json', 'pickle'].")
                    if log:
                        logger.info(f"Results saved in '{dst}'.")

            return df

        except Exception as err:
            logger.error(err)
            return pd.DataFrame()

    def undo_archiving(self, path, archive="archive", recursive=True, log=True):
        try:
            pathname = os.path.join(path, "**", archive, "*")
            files = glob.glob(pathname=pathname, recursive=recursive)
            msg = f"Found {len(files)} files to un-archive."
            if log:
                logger.info(msg)

            for file in files:
                dst = os.path.join(os.path.dirname(os.path.dirname(file)), os.path.basename(file))
                shutil.move(src=file, dst=dst)
        except Exception as err:
            logger.error(err)

if __name__ == "__main__":
    pass

#         res = df2sqlite.df2sqlite(df, db, tbl)

#     except Exception as err:
#         print(err)

# # %%
# def thermo2sqlite2(source, db: str, tbl: str):
#     try:
#         if "http" in source:
#             obj = mchfilebrowser.download_url(source)
#             df = pd.read_csv(obj, sep="\s+", engine='python')
#         else:
#             df = pd.read_csv(source, sep="\s+", engine='python')
#             df.reset_index(inplace=True)
#         if "level_0" in df.columns:
#             df.rename(columns={'level_0': 'pcdate', 'level_1': 'pctime'}, inplace=True)
#         if "pcdate" in df.columns:
#             df['dtm'] = pd.to_datetime(df['pcdate'] + ' ' + df['pctime'])
#         else:
#             df['dtm'] = pd.to_datetime(df['date'] + ' ' + df['time'])
#         df['source'] = source
#         if 'hio3' in df.columns:
#             df.drop(columns='hio3', inplace=True)
#         if 'o3lt' in df.columns:
#             df.drop(columns='o3lt', inplace=True)
#         df.set_index('dtm', inplace=True)
#         if 'index' in df.columns:
#             df.drop(columns='index', inplace=True)
#         res = df2sqlite.df2sqlite(df, db, tbl)

#     except Exception as err:
#         print(err)

# # %%
# def zip2sqlite(file: str, db: str, tbl: str, year=None):
#     try:
#         if "tei49c" in tbl:
#             if year is None:
#                 raise ValueError("'year' must be specified.")
#             else:
#                 year = year + "-"
#         with zipfile.ZipFile(file=file, mode="r") as zf:
#             for file in zf.namelist():
#                 if tbl in file:
#                     print(f"Processing {file} ...")
#                     with zf.open(file, mode="r") as obj:
#                         df = pd.read_csv(obj, sep="\s+", engine='python')
#                         df.reset_index(inplace=True)
#                         if "level_0" in df.columns:
#                             df.rename(columns={'level_0': 'pcdate', 'level_1': 'pctime'}, inplace=True)
#                         if "pcdate" in df.columns:
#                             df['dtm'] = pd.to_datetime(df['pcdate'] + ' ' + df['pctime'])
#                         else:
#                             if "tei49c" in tbl:
#                                 df['dtm'] = pd.to_datetime(year + df['date'] + ' ' + df['time'])
#                             else:
#                                 df['dtm'] = pd.to_datetime(df['date'] + ' ' + df['time'])
#                         df['source'] = os.path.join(archive, file)
#                         if 'hio3' in df.columns:
#                             df.drop(columns='hio3', inplace=True)
#                         if 'o3lt' in df.columns:
#                             df.drop(columns='o3lt', inplace=True)
#                         df.set_index('dtm', inplace=True)
#                         if 'index' in df.columns:
#                             df.drop(columns='index', inplace=True)
#                         res = df2sqlite.df2sqlite(df, db, tbl)
#     except Exception as err:
#         print(err)

# # %% download and process files from pay-data
# base_url = "https://hub.meteoswiss.ch/filebrowser/pay-data/data/pay/Kenya/MKN/incoming//tei49c/"
# file_urls = mchfilebrowser.get_urls_from_filebrowser(url=base_url, pattern="tei49c.+zip")
# target = "C:/Users/localadmin/Documents/git/gawkenya/data/thermo/tei49c"
# root = "C:/Users/localadmin/Documents/"
# db = os.path.join(root, "data/mkn.sqlite")
# tbl = "tei49c"

# for file_url in file_urls:
#     print(f"Downloading {file_url}")
#     obj = mchfilebrowser.download_url(file_url)
#     df = pd.read_csv(obj, sep="\s+", engine="python")
#     df.drop('o3lt', axis=1, inplace=True)
#     df2sqlite.df2sqlite(df=df, db=db, tbl=tbl)

# # %% process files copied directly from minix
# root = "C:/Users/localadmin/Documents/"
# db = os.path.join(root, "data/mkn.sqlite")

# # %% tei49c
# tbl = "tei49c"
# folder = os.path.join(root, "data/minix/thermo/", tbl)
# # archive = os.path.join(folder, "2021.zip")
# # zip2sqlite(file=archive, db=db, tbl=tbl, year="2021")

# # archive = os.path.join(folder, "2022.zip")
# # zip2sqlite(file=archive, db=db, tbl=tbl, year="2022")

# archive = os.path.join(folder, "tei49c-20221123.zip")
# zip2sqlite(file=archive, db=db, tbl=tbl, year="2022")

# # %% tei49i
# tbl = "tei49i"
# folder = os.path.join(root, "data/minix/thermo/", tbl)

# # archive = os.path.join(folder "tei49i_all_lrec-20220719102005.zip")
# archive = os.path.join(folder, "tei49i-20221123.zip")
# zip2sqlite(archive, db, tbl)

# # %% tei49i_2
# tbl = "tei49i_2"
# folder = os.path.join(root, "data/minix/thermo/", tbl)

# archive = os.path.join(folder, "tei49i_2-20221123.zip")
# zip2sqlite(archive, db, tbl)

#   # %% don't run
# import os
# import re

# def remove_echo(file: str, echo=r"lrec\s\n"):
#     try:
#         with open(file, "rt") as fh:
#             content = fh.read()
#             if re.search(echo, content):
#                 print(file)
#                 content = re.sub(echo, "", content)
#                 fh.close()
#                 with open(file, "wt") as fh:
#                     fh.write(content)
#         fh.close()

#     except Exception as err:
#         print(err)

# path = "C:/Users/localadmin/Documents/data/minix/thermo/tei49c/11/"
# files = os.listdir(path)
# for file in files:
#     remove_echo(f"{path}/{file}")
# # %%


""""whether"""
def main():
    """check the wheather in which city in the world!!!"""
    import requests
    api_address='http://api.openweathermap.org/data/2.5/weather?appid=0c42f7f6b53b244c78a418f4f181282a&q='
    city = input()
    new_address = api_address + city
    json_data = requests.get(new_address).json()
    format_add = json_data['weather'][0]["description"]
    print(format_add)
main()


from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.shortcuts import render,  get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse

from .utils import render_to_pdf

from .models import Experience, Education, Skill
from user_profile.models import UserProfile

def home(request):
	return render(request, '/home/dzee/resume_builder/index.html')

def profile(request):
	user = request.user
	profile = get_object_or_404(UserProfile, user=user)
	context = {
		'first_name': user.first_name,
		'last_name': user.last_name,
		'email': user.email,
		'phone': profile.phone,
		'profession': profile.profession,
		'bio': profile.bio,
		'theme': profile.color,
	}
	return render(request, 'resume/profile.html', context)

def update_user(request):
	user = request.user
	user.first_name = request.POST['first_name']
	user.last_name = request.POST['last_name']
	user.email = request.POST['email']
	user.save()
	profile = get_object_or_404(UserProfile, user=user)
	profile.phone = request.POST['phone']
	profile.profession = request.POST['profession']
	profile.bio = request.POST['bio']
	profile.save()
	return HttpResponseRedirect(reverse('resume:profile'))

################################# CRUD EXPERIENCE

def experience(request):
	user = request.user
	profile = get_object_or_404(UserProfile, user=user)
	experiences = Experience.objects.filter(user=request.user)
	if experience:
		context = {
			'experiences': experiences,
			'theme': profile.color,
		}
		return render(request, 'resume/experience.html', context)
	context = {
		'theme': profile.color,
	}
	return render(request, 'resume/experience.html', context)

def create_experience(request):
	experience = Experience()
	experience.user = request.user
	experience.company = request.POST['company']
	experience.role = request.POST['role']
	experience.startDate = request.POST['start']
	experience.endDate = request.POST['end']
	experience.description = request.POST['description']
	experience.save()
	return HttpResponseRedirect(reverse('resume:experience'))

def update_experience(request):
	id = request.POST['id']
	experience =get_object_or_404(Experience, id=id)
	experience.company = request.POST['company']
	experience.role = request.POST['role']
	# experience.startDate = request.POST['start']
	# experience.endDate = request.POST['end']
	experience.description = request.POST['description']
	experience.save()
	return HttpResponseRedirect(reverse('resume:experience'))

def delete_experience(request, id):
	experience = get_object_or_404(Experience, id=id)
	experience.delete()
	return HttpResponseRedirect(reverse('resume:experience'))

################################# CRUD EDUCAION

def education(request):
	user = request.user
	profile = get_object_or_404(UserProfile, user=user)
	educations = Education.objects.filter(user=request.user)
	if educations:
		context = {
			'educations': educations,
			'theme': profile.color,
		}
		return render(request, 'resume/education.html', context)
	context = {
		'theme': profile.color,
	}
	return render(request, 'resume/education.html', context)

def create_education(request):
	education = Education()
	education.user = request.user
	education.school = request.POST['school']
	education.degree = request.POST['degree']
	education.startDate = request.POST['start']
	education.endDate = request.POST['end']
	education.description = request.POST['description']
	education.save()
	return HttpResponseRedirect(reverse('resume:education'))

def update_education(request):
	id = request.POST['id']
	education =get_object_or_404(Education, id=id)
	education.school = request.POST['school']
	education.degree = request.POST['degree']
	# education.startDate = request.POST['start']
	# education.endDate = request.POST['end']
	education.description = request.POST['description']
	education.save()
	return HttpResponseRedirect(reverse('resume:education'))

def delete_education(request, id):
	education = get_object_or_404(Education, id=id)
	education.delete()
	return HttpResponseRedirect(reverse('resume:education'))

################################# CRUD SKILL

def skills(request):
	user = request.user
	profile = get_object_or_404(UserProfile, user=user)
	skills = Skill.objects.filter(user=request.user)
	skills = skills[::-1] ##reversing the list
	context = {
		'skills': skills,
		'theme': profile.color,
	}
	return render(request, 'resume/skills.html', context)

def create_skill(request):
	skill = Skill()
	skill.user = request.user
	skill.name = request.POST['name']
	skill.save()
	return HttpResponseRedirect(reverse('resume:skills'))

def delete_skill(request, id):
	skill = get_object_or_404(Skill, id=id)
	skill.delete()
	return HttpResponseRedirect(reverse('resume:skills'))

################################# SETTINGS

def settings(request):
	user = request.user
	profile = get_object_or_404(UserProfile, user=user)
	context = {
		'username': user.username,
		'email': user.email,
		'theme': profile.color,
	}
	return render(request, 'resume/settings.html', context)

def set_blue(request):
	user = request.user
	profile = get_object_or_404(UserProfile, user=user)
	profile.color = 'blue'
	profile.save()
	return HttpResponseRedirect(reverse('resume:settings'))

def set_green(request):
	user = request.user
	profile = get_object_or_404(UserProfile, user=user)
	profile.color = 'green'
	profile.save()
	return HttpResponseRedirect(reverse('resume:settings'))

def set_orange(request):
	user = request.user
	profile = get_object_or_404(UserProfile, user=user)
	profile.color = 'orange'
	profile.save()
	return HttpResponseRedirect(reverse('resume:settings'))

def set_pink(request):
	user = request.user
	profile = get_object_or_404(UserProfile, user=user)
	profile.color = 'pink'
	profile.save()
	return HttpResponseRedirect(reverse('resume:settings'))

################################# BUILD RESUME

def generate_resume(request, *args, **kwargs):
	profile = get_object_or_404(UserProfile, user=request.user)
	experiences = Experience.objects.filter(user=request.user)
	educations = Education.objects.filter(user=request.user)
	skills = Skill.objects.filter(user=request.user)
	context = {
		'full_name': request.user.first_name + ' '  + request.user.last_name,
		#'email': user.email,
		'profession': profile.profession,
		'about_me': profile.bio,
		'experiences': experiences,
		'educations': educations,
		'skills': skills,
		'theme': profile.color,
	}
	pdf = render_to_pdf('pdf/test.html', context)
	if pdf:
		response = HttpResponse(pdf, content_type='application/pdf')
		download = request.GET.get("download")
		if download == 'True':
			response['Content-Disposition'] = 'attachment; filename=test.pdf'
		return response
	return HttpResponse('PDF not found')


from enum import Enum

ELEMENT = {
    "ground": "electric",
    "electric": "water",
    "water": "fire",
    "fire": "grass",
    "grass": "ground",
    "light": "dark",
    "dark": "light",
    "neutral": ""
}

RENDER_ELEMENT = {
    "ground": "⛰",
    "electric": "🌩",
    "water": "💦",
    "fire": "🔥",
    "grass": "🍂",
    "light": "💡",
    "dark": "🌛",
    "neutral": "✨"
}

class Rarity(Enum):
    COMMON = 0
    UNCOMMON = 1
    RARE = 2
    SR = 3
    UR = 4
    L = 5

STR_TO_RARITY = {
    "common": Rarity.COMMON,
    "uncommon": Rarity.UNCOMMON,
    "rare": Rarity.RARE,
    "super rare": Rarity.SR,
    "ultra rare": Rarity.UR,
    "legendary": Rarity.L
}


#!/usr/bin/env python3
import os
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import ParseError
from datetime import datetime
from urllib.request import urlretrieve
from urllib.parse import urlparse

SAVE_DIR = '/tmp/podcasts'
XML_FILE_PATH = 'http://rss.dw.com/xml/DKpodcast_dwn1_en'

def parse_xml(root):
    for l1 in root:
        for l2 in l1:
            if not l2.tag == 'item':
                continue
            for l3 in l2:
                if not l3.tag == 'enclosure':
                    continue
                url = l3.attrib.get('url')
                filename = retrieve_filename(url)
                if os.path.splitext(filename)[-1] == '.mp3' or l3.attrib.get('type') == 'audio/mpeg':
                    filepath = os.path.join(SAVE_DIR, filename or datetime.now().isoformat())
                    if os.path.exists(filepath):
                        print("File {} already exists".format(filename))
                        continue
                    urlretrieve(url, filepath)

def retrieve_filename(url):
    path = urlparse(url).path
    return os.path.basename(path)

if __name__ == '__main__':
    xml_file = urlretrieve(XML_FILE_PATH, os.path.join('/tmp', retrieve_filename(XML_FILE_PATH) + '.xml'))[0]
    try:
        tree = ET.parse(xml_file)
    except FileNotFoundError:
        print("File can't be downloaded")
        exit(1)
    except ParseError:
        print("Incorrect file. Are you sure it is in xml format?")
        exit(1)

    os.makedirs(SAVE_DIR, exist_ok=True)

    parse_xml(tree.getroot())


# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
from Python.MAIN_FUNC import lib
from Python.MAIN_FUNC import main_actions
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.interval import IntervalTrigger
import atexit

app = lib.Flask(__name__, template_folder='web_service', static_folder='web_service')

@app.route("/", methods=['GET'])
def index():
    if lib.request.method == 'GET':
        return lib.render_template('index.html')
    elif lib.request.method == 'POST':
        if lib.request.form['submit_button'] == 'LOGIN':
            var_login = lib.request.form['login']
            var_pass = lib.request.form['password']
            # For debug, need change code below
            if var_login == 'YOUR_USER' and var_pass == 'YOUR_PASS':
                return lib.render_template('index.html')
            else:
                return 'Ошибка входа!'

@app.route("/", methods=['POST','GET'])
def button_run():
    print(lib.request.method)
    if lib.request.method == 'POST':
        if lib.request.form['button_run'] == 'run_full':
            var_stock = lib.request.form['Stock']
            var_batch = lib.request.form['Batch']
            main_class.execute(idProcessFunc = 'InsertData', var_batch=int(var_batch), list_tiket= str(var_stock))
            return lib.render_template('index.html')
        elif lib.request.form['button_run'] == 'run_delete':
            var_stock_del = lib.request.form['Stock_delete']
            if var_stock_del != '':
                main_class.delete_stock(idProcessFunc='DeleteData', stock_name=var_stock_del)
            elif 'Stock_delete' in lib.request.form:
                main_class.delete_stock(idProcessFunc='DeleteAllData', stock_name=var_stock_del, all_delete=True)
            return lib.redirect("/")
        elif lib.request.form['button_run'] == 'run_insertmain':
            main_class.executeNasdaq()
            return lib.redirect("/")

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    main_class = main_actions.ACTIONS()
    #main_class.executeNasdaq()
    scheduler = BackgroundScheduler()
    scheduler.start()
    scheduler.add_job(
        func=main_class.delta_loading,
        trigger=IntervalTrigger(hours=1),
        id='printing_time_job',
        name='Data loading',
        replace_existing=True)
    # Shut down the scheduler when exiting the app
    atexit.register(lambda: scheduler.shutdown())

    app.run(debug='True')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/


import RPi.GPIO as GPIO
import requests
from signal import pause

#configurando os pinos do raspberry
GPIO.setmode(GPIO.BCM)
led = 4
button = 17

GPIO.setup(led, GPIO.OUT)
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#corpo

url = 'http://192.168.1.110/current_state.xml'

#liga e desliga led
def led_on():
    GPIO.output(led,1)

def led_off():
    GPIO.output(led,0)

#funçao post state do botao

def botao_pressed():
    params1 = {
        'button': 0,
    }
    response = requests.post(url, params=params1)

    if response.led == 0:
        led_on()
    else:
        led_off()

def botao_unpressed():
    params1 = {
        'button': 1,
    }
    response = requests.post(url, params=params1)

    if response.led == 1:
        led_off()
    else:
        led_on()

button.when_pressed = botao_pressed()
button.when_released = botao_unpressed()

pause()


# -*- coding: utf-8 -*-
"""
Notifications
-------------
Example showing how to add notifications to a characteristic and handle the responses.
Updated on 2019-07-03 by hbldh <henrik.blidh@gmail.com>
"""
import datetime
import logging
import asyncio
import platform

import binascii
import threading

from bleak import BleakClient
from bleak import _logger as logger
from bleak import discover
import queue
import json
from bleak import BleakScanner
# from pythonosc.udp_client import SimpleUDPClient
# from websocketServerTest import glQueue

from data_transfer import DataSend

glQueue=queue.Queue(maxsize=5000)
dataqueue=queue.Queue(maxsize=5000)
def findBlooth():
    newloop = asyncio.new_event_loop()
    asyncio.set_event_loop(newloop)
    loop = asyncio.get_event_loop()
    loop.run_until_complete(findHardware())

    # loop=asyncio.get_event_loop()
    # loop.run_until_complete(findHardware())
async def findHardware():
    async with BleakScanner() as scanner:
        await asyncio.sleep(3.0)
        devices = await scanner.get_discovered_devices()
    ret=[]
    for d in devices:
        if d.name.startswith('Brain'):
            ret.append(d.name+';'+d.address)
            print(d.name,d.address)
    bloothsend=DataSend(1,ret)
    finBloothStr=json.dumps(bloothsend,default=lambda obj:obj.__dict__,sort_keys=True)
    dataqueue.put(finBloothStr)
    # dataqueue.put('findstrtes')
CHARACTERISTIC_UUID = (
    "2d30c082-f39f-4ce6-923f-3484ea480596"
)  # <--- Change to the characteristic you want to enable notifications from. 2d30c082-f39f-4ce6-923f-3484ea480596
# client=SimpleUDPClient('127.0.0.1',1337)

def notification_handler(sender, data):
    """Simple notification handler which prints the data received."""
    # print(len(data))
    print("Received data: %s" % binascii.hexlify(data))
    glQueue.put(data)

    # client.send_message("/filter1",data)

async def run(address, loop, debug=False,socket_connect_sta=None):
    global bleconnect_status
    if debug:
        import sys

        # loop.set_debug(True)
        l = logging.getLogger("asyncio")
        l.setLevel(logging.DEBUG)
        h = logging.StreamHandler(sys.stdout)
        h.setLevel(logging.DEBUG)
        l.addHandler(h)
        logger.addHandler(h)
    try:
        async with BleakClient(address, loop=loop) as client:

            bleconnect_status=True
            x = await client.is_connected()
            print(x)
            logger.info("Connected: {0}".format(x))
            # await client.write_gatt_char('2d30c083-f39f-4ce6-923f-3484ea480596', binascii.a2b_hex('62'))
            await client.start_notify(CHARACTERISTIC_UUID, notification_handler)
            await client.write_gatt_char('2d30c083-f39f-4ce6-923f-3484ea480596', binascii.a2b_hex('43'))
            await asyncio.sleep(1.0, loop=loop)
            await client.write_gatt_char('2d30c083-f39f-4ce6-923f-3484ea480596', binascii.a2b_hex('62'))
            # await client.write_gatt_char('2d30c083-f39f-4ce6-923f-3484ea480596', binascii.a2b_hex('62')) #2d30c083-f39f-4ce6-923f-3484ea480596
            # await client.write_gatt_char('2d30c083-f39f-4ce6-923f-3484ea480596', binascii.a2b_hex('43'))
            blestatus_number=0
            while True:
                blestats=await client.is_connected()
                if blestats==False:
                    blestatus_number=blestatus_number+1
                if blestatus_number>=5 or socket_connect_sta.connect==False:
                    if blestatus_number>=5:
                        socket_connect_sta.ble_is_alive=False
                        error_ret_obj=DataSend(1008,"蓝牙断开连接")
                        error_ret_str=json.dumps(error_ret_obj,default=lambda obj:obj.__dict__,sort_keys=True)
                        dataqueue.put(error_ret_str)
                    bleconnect_status=False
                    await client.stop_notify(CHARACTERISTIC_UUID)
                    await client.disconnect()
                    break
                await asyncio.sleep(1.0, loop=loop)
                # await client.write_gatt_char('2d30c083-f39f-4ce6-923f-3484ea480596', binascii.a2b_hex('43'))
            await client.stop_notify(CHARACTERISTIC_UUID)
    except Exception as e:
        print(e)
def connect(imei,socket_connect_sta):
    from data_analysis import dataAnalysis
    datathread=threading.Thread(target=dataAnalysis,args=(socket_connect_sta,))
    datathread.setDaemon(True)
    datathread.start()
    address = (
        imei  # <--- BC:DD:C2:C9:12:22Change to your device's address here if you are using Windows or Linux
        if platform.system() != "Darwin"
        else imei  # <--- Change to your device's address here if you are using macOS
    )

    newloop = asyncio.new_event_loop()
    asyncio.set_event_loop(newloop)
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run(address, loop, True,socket_connect_sta))

if __name__ == "__main__":
    import os
    print(str)

    os.environ["PYTHONASYNCIODEBUG"] = str(1)
    address = (
        "BC:DD:C2:C9:12:22"  # <--- BC:DD:C2:C9:12:22Change to your device's address here if you are using Windows or Linux
        if platform.system() != "Darwin"
        else "243E23AE-4A99-406C-B317-18F1BD7B4CBE"
              # <--- Change to your device's address here if you are using macOS
    )
    print(address)
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run(address, loop, True,'test'))


import pandas as pd
import numpy as np
import gensim
import jieba
from text_classification.predict import classification_predict
from text_similarity.predict import predict
from chitchat.interact import chitchat

df = pd.read_csv('data/qa_data.csv')
question = df['question'].values
answer = df['answer'].values

model = gensim.models.Word2Vec.load('word2vec/wiki.model')

def sen2vec(text):
    segment = list(jieba.cut(text))
    vec = np.zeros(100)
    for s in segment:
        # 假如我们的词不在词向量里，就会出现oov的问题
        try:
            vec += model.wv[s]
        except:
            pass
    vec = vec / len(segment)
    return vec

def cosine(a, b):
    return np.matmul(a, b.T) / np.linalg.norm(a) / np.linalg.norm(b, axis=-1)

question_vec = []
for q in question:
    question_vec.append(sen2vec(q))

def qa(text):
    # 先判断是否是闲聊
    prob = classification_predict(text)
    if prob > 0.5:
        print('闲聊')
        res = chitchat(text)
        print(res)
        return res
    vec = sen2vec(text)

    # 召回
    similarity = cosine(vec, np.array(question_vec))
    max_similarity = max(similarity)
    print('最大相似度', max_similarity)
    if max_similarity < 0.8:
        print('没有找到答案')
        return '没有找到答案'
    top_10 = np.argsort(-similarity)[0:10]
    candidate = question[top_10]

    # 精排
    esim_res = predict([q] * 10, candidate)

    index_dic = {}

    print('候选集：')
    for i, index in enumerate(top_10):
        print(candidate[i], ' ', similarity[index], ' ', esim_res[i])
        index_dic[i] = index

    esim_index = np.argsort(-esim_res)[0]
    print('最相似的问题: ', question[index_dic[esim_index]])
    print('答案: ', answer[index_dic[esim_index]])
    return answer[index_dic[esim_index]]

if __name__ == '__main__':
    while 1:
        q = input('请输入你的问题：')
        qa(q)


import os
import numpy as np
import cv2

def draw_bbox_results(image, results, input_path, save_dir=None):
    for result in results:
        [xmin, ymin, xmax, ymax] = result["bbox"]

        image = cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (0, 255, 0),
                              2)

    image_name = os.path.basename(input_path)
    if save_dir is None:
        save_dir = "output"
    os.makedirs(save_dir, exist_ok=True)
    output_path = os.path.join(save_dir, image_name)
    cv2.imwrite(output_path, image)


""" Thomas Parashos 2019
1. Take from the big list of things
2. remove unusabe and numeric (return to unusable later, potential links e.g. employee to id number)
3. make spreadsheet with that data
record_id, y_act, reason, col_name, unique_entry, times_entered, percent_of_total, list_of_rows

This now out to be a collection of methods
"""
import glob
import pandas as pd
from datetime import datetime

###Globals###

#Paths
main_folder = "./SH_data"
source_data_folder = "/datasets"
index_folder = "/meta_data"
prelim_labeled_data_folder = "/data_for_labeling"

data_folder = main_folder + source_data_folder + "/"

index_path = main_folder + index_folder +"/*.csv"
pre_labeled_path = main_folder + prelim_labeled_data_folder + "/reduced_labels.csv"

output_folder = main_folder + "/label_sets/"

log_file = "./log.txt"

#Open Files
log_file = open(log_file, "a+")

#Lists
encodings = [None, "cp1252", "ISO-8859-1"]

###Methods###

def log(log_string):
    log_file.write("[{}]\t".format(datetime.now()) +log_string + "\n")

def p_log(log_string):
    print(log_string)
    log(log_string)

def print_list(l):
    for e in l: print(e)

def multi_read(read_fun, file_list, give_list = False):
    #Returns a df of all the files in the list appended together
    dfs = []
    for path in file_list:
        try:
            dfs.append(read_fun(path))
        except:
            print("failed to append")
            print(path)
    if give_list: return dfs
    return pd.concat(dfs, sort=False)

def get_subset_eq(df, col_name, val):
    return df.loc[df[col_name] == val]

def read_tsv(file_path, encoding = None):
    return pd.read_csv(file_path, sep = "\t", encoding = encoding)

def glob1(file_path):
    #gives a string of the first file in a glob
    g = glob.glob(file_path)
    len_g = len(g)
    if len_g == 1: return g[0]
    if len_g == 2:
        print("Multiple files\n", g)
        return g[0]
    if len_g == 0:
        print("no files")
        return g

def col_replace(df, col_name, old_val, new_val):
    #inplace operation
    #replaces all specified values in a df's column with the given input
    df[col_name][df[col_name] == old_val] = new_val

def list_starting_from(tar_list, start_el, skip = 0):
    start = tar_list.index(start_el) + skip
    return tar_list[start:]

def read_csv_mult_encodings(file_path):
    #attempts to read file with each specified encoding in order
    #yells at you if it can't
    for encoding in encodings:
        try:
            return pd.read_csv(file_path, encoding = encoding)
        except: pass
    p_log("Opening Failed. File: "+file_path)
    return None

def write_list(file, string_list):
    for st in string_list:
        file.write(st)

def all_equal(my_list):
    return all(x == my_list[0] for x in my_list)

class Counter:

    def __init__(self, total, name):
        self.i = 0
        self.total = total
        self.name = name

    def inc(self, e_name, prefix):
        self.i += 1
        print("{}{}: {}, {} of {}, {:.2%}".format(prefix, self.name, e_name, self.i, self.total, self.i/self.total))

if __name__ == "__main__":
    # file_list = glob.glob("SH_data\label_sets\labeled\contains_d\*.csv")
    # print(len(file_list))

    # big_df = multi_read(read_csv_mult_encodings, file_list)
    # big_df.to_csv("cdfify.csv")

    # file_list = glob.glob("SH_data\label_sets\labeled\contains_d\*.csv")
    # print(len(file_list))

    # dfs = multi_read(read_csv_mult_encodings, file_list, give_list=True)
    # i_s = []
    # for i in range(0,len(dfs)):
    #     df = dfs[i]
    #     if len(df[df['reason'] == 'ahi']) > 0:
    #         df.to_csv(f"todays_data/{i}.csv")
    #     else:
    #         i_s.append(i)
    # dfs = [dfs[i] for i in i_s ]
    # pd.concat(dfs).to_csv('todays_data/good.csv')

    # reason_df = pd.read_csv("todays_data\\10.csv")
    # print(reason_df)
    # reason_df = reason_df[reason_df['reason'] == 'ahi']
    # reason_df.to_csv("todays_data\\10.csv", index=False)

    # import re
    # def alnum(my_str):
    #     return re.sub(r'\W+', '', my_str)

    # df = pd.read_csv("todays_data\\10.csv")
    # start = 1
    # for group in range(start, df["group"].max() + 1):
    #     curr_df = df[df["group"] == group]
    #     print(curr_df)
    #     words = curr_df[curr_df.columns[0]].tolist()

    #     if all_equal([w.lower() for w in words]): new_reason = "a"
    #     elif all_equal([w.replace(" ", "") for w in words]): new_reason = "h"
    #     elif all_equal([alnum(w) for w in words]): new_reason = "i"
    #     elif all_equal([w.lower().replace(" ", "") for w in words]): new_reason = "a h"
    #     elif all_equal([alnum(w).lower() for w in words]): new_reason = "a i"
    #     elif all_equal([alnum(w).replace(" ", "") for w in words]): new_reason = "h i"
    #     elif all_equal([alnum(w).replace(" ", "").lower() for w in words]): new_reason = "a h i"

    #     df.at[df["group"] == group, 'reason'] = new_reason
    #     df.to_csv("todays_data\\10.csv", index=False)

    # file_list = glob.glob("todays_data\\*.csv")
    # dfs = multi_read(read_csv_mult_encodings, file_list)
    # dfs = dfs[dfs['reason'] != "zzz"]
    # dfs = dfs['reason']
    # dfs.to_csv("todays_data\\data.csv")
    # import matplotlib.pyplot as plt
    # df = pd.read_csv("todays_data\\data.csv")
    # plot = df["reason"].value_counts().plot.pie()
    # plt.show()
    # print(df["reason"].value_counts())
    # df["reason"].value_counts().to_csv("todays_data\\counts.csv")

    import matplotlib.pyplot as plt
    df = pd.read_csv("todays_data\\counts.csv", index_col="reason")
    print(df)
    plot = df.plot.pie(y="Reason")
    plt.show()
    # print(df["reason"].value_counts())
    # df["reason"].value_counts().to_csv("todays_data\\counts.csv")


import pandas as pd
import os
from sklearn.model_selection import StratifiedKFold

####################### cross validation #########################

def cv(dirpath, save_path, dataset, nfold):
    years = [3,5]
    name = ['id','time','status','class']
    data = pd.read_csv(dirpath+f'{dataset}_info.csv',usecols=name[:-1]).values
    for year in years:
        surv  = []
        for i,temp in enumerate(data):
            if temp[2]==-1:
                surv.append(list(data[i])+[2])
                continue
            if temp[2]==1 and int(temp[1]) < year*365:
                surv.append(list(data[i])+[1])
            elif int(temp[1]) >= year*365:
                surv.append(list(data[i])+[0])
            else:
                surv.append(list(data[i])+[2])
        df = pd.DataFrame(columns=name,data=surv)
        save_path = save_path + f'{dataset}_{year}y'
        if not os.path.exists(save_path): os.makedirs(save_path)
        df.to_csv(save_path + '.csv',index=None)

        x = df.values
        y = df['class'].values

        kf = StratifiedKFold(n_splits=nfold,random_state=23,shuffle=True)
        for i, (train_index, test_index) in enumerate(kf.split(x, y)):
            res_train = pd.DataFrame(data=x[train_index])
            res_test = pd.DataFrame(data=x[test_index])
            writer = pd.ExcelWriter(save_path+f'/data_{i}.xlsx')
            eval('res_train').to_excel(excel_writer=writer, sheet_name='train', index=False)
            eval('res_test').to_excel(excel_writer=writer, sheet_name='test', index=False)
            writer.save()
            writer.close()
    return

if __name__ == "__main__":
    dirpath = './data/clinical/'
    save_path = './splits/5_fold_cv/'
    data_list = ['BLCA','COAD','LIHC']
    for dataset in data_list:
        cv(dirpath, save_path, dataset, nfold=5)


## load environment
import streamlit as st
import pickle
import re
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
wnl = WordNetLemmatizer()
from pandas import DataFrame
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import Ridge
from numpy import concatenate
from numpy import asarray
from numpy import append as ap
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
from scipy.sparse import hstack
from nltk.corpus import stopwords
import matplotlib.pyplot as plt

# function definitions =======================================================================================
# define function to lemmatize long-form text
def lemmatizer(sentence):
    token_words = word_tokenize(sentence)
    lem_sentence=[]
    for word in token_words:
        lemma = wnl.lemmatize(word)
        lem_sentence.append(lemma)
        lem_sentence.append(" ")
    return "".join(lem_sentence)

# make preprocessing pipeline:
pattern = re.compile(r'\b(' + r'|'.join(stopwords.words('english')) + r')\b\s*')

def preproc(text):
    ll = text.lower()
    l = re.sub(r'http\S+',' ', ll) # remove links
    n = re.sub(r'[0-9]+', ' ', l) # remove numbers
    s = re.sub(r'[^\w]',' ', n)  # remove symbols
    w = pattern.sub('', s) # remove stopwords
    p = lemmatizer(w) # lemmatize all words
    return p

## load models
model, tfidf, onehot, goodwords, badwords, result_features = pickle.load(open("pickle/appv3models.pkl", "rb"))
story_example = pickle.load(open("pickle/app_example.pkl", "rb"))

parent_categories = ['Journalism', 'Comics', 'Dance',  'Photography',
                     'Games', 'Music', 'Technology', 'Crafts',
                     'Film & Video', 'Art', 'Design', 'Theater',
                     'Food', 'Fashion', 'Publishing']
#st.markdown('', unsafe_allow_html = True)

# Sidebar==================================================================================
# list of categories for making the drop down menu
c = st.sidebar.selectbox(
    'Category', parent_categories)

# give general suggestion drop down
if st.sidebar.checkbox("Show general suggestions"):
  st.sidebar.markdown('*Here are words that you may consider using more:*.')
  st.sidebar.markdown(', '.join(list(goodwords['feature'])[:50]))
  st.sidebar.markdown('*Here are words that you may consider using less:*.')
  st.sidebar.markdown(', '.join(list(badwords['feature'])[:50]))

# links to github and presentation
st.sidebar.markdown(" ")
st.sidebar.markdown("Learn more at:")
st.sidebar.markdown('[Github.com/ShengpeiWang/Kickstarter](https://github.com/ShengpeiWang/kickstarter)', unsafe_allow_html=True)
st.sidebar.markdown('[See presentation](https://docs.google.com/presentation/d/1oJsKwlv7ab87P3WkZVBMHWjuGsLIRW0dGD4xwoAYb5Q/edit?usp=sharing)', unsafe_allow_html=True)

# Main page ===============================================================================
# inputs-------------------------------------------------------
st.title("Let's kickstart your Kickstarter project!")

u_title = st.text_input("What's your idea?", "Black Diplomats - Decolonize the global affairs conversation", key = "title")

u_blurb = st.text_input("Blurb?", "A podcast and video series called Black Diplomats, featuring interviews with POC and women who specialize in global affairs.", key = "blurb")

u_story = st.text_area("Your draft story here:", story_example, key = "story")

st.button('Run')

# run model based on user input---------------------------------
# data wrangling
category =  DataFrame({'category' : [c]})
title_l = len(u_title)
story_tb = u_story + " " + u_title + " " + u_blurb
story_p = preproc(story_tb)
total_words = len(story_p .split())

tfidf_m = tfidf.transform([story_p])
encoded = onehot.transform(category)

x_info = asarray([title_l, total_words])
x_sparse = hstack([tfidf_m, encoded]).toarray()
x_full = asarray(ap(x_sparse, x_info).reshape(1, -1))

pred = model.predict(x_full)

pred_median = 10 ** pred[0]

# return model prediction -----------------------------------------
st.header("Your project will raise around $" + str(round(pred_median, 2)))

# return feature importance----------------------------------------
# get words in the user entry that contributed positively or negatively to the proposal performance
result_features['input'] = tfidf_m[:1500].toarray().T
result_features['value'] = result_features['input']*result_features['importance']
rec = result_features.sort_values(axis = 'index', by = ['value'])
rec_negative = result_features.sort_values(axis = 'index', by = ['value'], ascending = False)

empty = pd.DataFrame({"feature": [" ", " ", " ", " ", " ", " ", " ", " ", " "],
                      "importance": [0, 0, 0, 0, 0, 0, 0, 0, 0],
                      "input": [0, 0, 0, 0, 0, 0, 0, 0, 0],
                      "value": [0, 0, 0, 0, 0, 0, 0, 0, 0]})

positive = pd.concat([empty, rec[rec['value'] > 0 ]]).tail(8)

negative = pd.concat([empty, rec_negative[rec_negative['value'] < 0 ]]).tail(8)

plt.subplot(1, 3, 1)
plt.barh(range(8), negative['value'],
         color = "coral", edgecolor = "black", linewidth = 1.2)
plt.yticks(range(8), negative['feature'])
plt.title('Words to rephrase')
plt.xlabel('Importance')

plt.subplot(1, 3, 3)
plt.barh(range(8), positive['value'],
         color = "dodgerblue", edgecolor = "black", linewidth = 1.2)
plt.yticks(range(8), positive['feature'])
plt.title('Words to use more')
plt.xlabel('Importance')

st.pyplot()


from datetime import date, timedelta
import pandas as pd
import requests

from get_stats import get_team_stats_dict
from get_matches import get_match_results
from available_stats import available_stats

# [{'Sacramento Kings': 'Boston Celtics', 'Charlotte Hornets': 'Philadelphia 76ers'}, ['W', 'L']]
# team stats is a dataframe
def to_dataframe(daily_games, start_date, end_date, season):  # , mean_dict, std_dict):
    full_dataframe = []
    game_number = 0  # counter to match with the correct game
    daily_results = daily_games[1]  # win or loss for each game
    score = daily_games[2]
    game_id = daily_games[3]

    for home_team, away_team in daily_games[0].items():  # loops through matchups
        home_team_stats = get_team_stats_dict(home_team, start_date, end_date, season)
        away_team_stats = get_team_stats_dict(away_team, start_date, end_date, season)

        current_game = [home_team, away_team]

        current_game.append(game_id[game_number])

        current_game.append(score.pop(0))

        for stat, stat_type in available_stats.items():
            current_game.append(home_team_stats[stat])

        current_game.append(score.pop(0))

        for stat, stat_type in available_stats.items():
            current_game.append(away_team_stats[stat])

        if daily_results[game_number] == 'W':
            result = 1
        else:
            result = 0

        current_game.append(result)
        game_number += 1

        print(current_game)

        full_dataframe.append(current_game)

    return full_dataframe

def date_range(start_date, end_date):
    for n in range(int((end_date - start_date).days)):
        yield start_date + timedelta(n)

def training_set(start_year, start_month, start_day, end_year, end_month, end_day, season, season_start):
    start_date = date(start_year, start_month, start_day)
    end_date = date(end_year, end_month, end_day)

    total_games = []

    for single_date in date_range(start_date, end_date):
        current_date = single_date.strftime('%m/%d/%Y')
        print(current_date)

        previous_day = single_date - timedelta(days=1)
        previous_day_formatted = previous_day.strftime('%m/%d/%Y')

        current_day_games = get_match_results(current_date, season)
        current_day_games_with_stats = to_dataframe(current_day_games, season_start, previous_day_formatted, season)

        for game in current_day_games_with_stats:
            game.append(current_date)
            total_games.append(game)

    print(total_games)
    return total_games

def make_dataframe(game_list):
    games = pd.DataFrame(game_list,
                         columns=['Home', 'Away', 'Game_ID', 'H_Score', 'H_W_PCT', 'H_FG_PCT', 'H_FG3_PCT', 'H_FT_PCT',
                                  'H_REB', 'H_AST', 'H_TOV', 'H_STL',
                                  'H_BLK', 'H_PLUS_MINUS', 'H_OFF_RATING', 'H_DEF_RATING', 'H_TS_PCT', 'A_Score',
                                  'A_W_PCT', 'A_FG_PCT', 'A_FG3_PCT',
                                  'A_FT_PCT', 'A_REB', 'A_AST', 'A_TOV', 'A_STL',
                                  'A_BLK', 'A_PLUS_MINUS', 'A_OFF_RATING', 'A_DEF_RATING', 'A_TS_PCT', 'Result',
                                  'Date'])

    print(games)
    return games

def main():
    attempts = 10

    for i in range(attempts):
        try:
            #start day has to be at least three days after the start of the season
            all_games = training_set(start_year=2020, start_month=12, start_day=27, end_year=2021, end_month=3,
                                     end_day=20,
                                     season='2020-21', season_start='12/22/2020')
            df = make_dataframe(all_games)

            print(df)
            df.to_csv(r'C:\Users\student\honors\nba\nba_data\nba_df_2020.csv', index=False)
        except requests.exceptions.ReadTimeout or ValueError:
            if i < attempts - 1:
                continue
            else:
                raise
        break

if __name__ == '__main__':
    main()


from flask import Flask, render_template, send_from_directory,json,jsonify

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route("/example.json")
def data():
    f = open('static/js/example.json')
    data = json.load(f)
    return jsonify(data)