import open3d as o3d
import numpy as np
import heapq

# print(o3d.__version__)

def import_mesh(file_path: str):
    """
    Imports stl file as Mesh

    Args:
        file_path (str): file path of stl file

    Returns:
        mesh (open3d.geometry.TriangleMesh): Mesh to be processed.
    """
    mesh = o3d.io.read_triangle_mesh(file_path)
    return mesh

def export_mesh(file_path: str, mesh):
    """
    Export a mesh to a given file path using the open3d library.

    Args:
        file_path (str): The file path where the mesh will be saved.

        mesh: The mesh to be exported.

    Returns:
        None
    """
    o3d.io.write_triangle_mesh(file_path, mesh)
    return

def clean_mesh(mesh):
    """
    Merge the close vertices of a mesh to remove noise.

    Args:
        mesh: The mesh to be cleaned.

    Returns:
        None
    """
    mesh.merge_close_vertices(0.01)
    return

def mesh_details(mesh) -> tuple:
    """
    Retrieve details of a mesh in the form of numpy arrays for vertices and triangles.

    Args:
        mesh: The mesh for which the details are to be extracted.

    Returns:
        tuple: A tuple of two numpy arrays. The first array contains the vertices of the mesh
        and the second array contains the triangles of the mesh.
    """
    return (np.asarray(mesh.vertices), np.asarray(mesh.triangles))

def visualize(entries, show_wireframe=False) -> None:
    """
    Visualize a single mesh or a list of meshes using the open3d library.

    Args:
        entries: The mesh/meshes to be visualized. It can be a single mesh or a list of meshes.

        show_wireframe (bool): A flag to show/hide the wireframe of the mesh/meshes.

    Returns:
        None
    """
    if type(entries) is not list:
        o3d.visualization.draw_geometries([entries], mesh_show_wireframe=show_wireframe)
    else:
        o3d.visualization.draw_geometries(entries, mesh_show_wireframe=show_wireframe)
        return

def get_mask(mesh, idx):
    """
    Create a mask array for a specific vertex of the mesh.

    Args:
        mesh: The mesh for which the mask is to be created.

        idx: The index of the vertex for which the mask is to be created.

    Returns:
        np.ndarray: A boolean mask array with True value for the given index and False for all
                    other vertices of the mesh.
    """
    mask = np.full(np.asarray(mesh.vertices).shape[0], False)
    mask[idx] = True
    return mask

def find_minimum(mesh, k: int = 1, idx_mask=[]):
    """
    Find the k number of vertices having minimum z-coordinate of a mesh.

    Args:
        mesh (open3d.geometry.TriangleMesh): The input mesh.

        k (int, optional):: The number of minimum vertices to be found.

        idx_mask (list, optional): A list of indices of vertices to be considered for finding minimum vertices.

    Returns:
        tuple: A tuple of two numpy arrays. The first array contains the indices of the minimum
               vertices and the second array contains the coordinates of the minimum vertices.
    """
    mesh_vertices_np = np.asarray(mesh.vertices)
    idx_mask = set(idx_mask)
    # print(mesh_vertices_np, type(mesh.vertices))

    heap: list = []
    for i, vertex in enumerate(mesh_vertices_np):
        if len(idx_mask) == 0:
            heapq.heappush(heap, (vertex[2], i, vertex))

        elif len(idx_mask) > 0 and i in idx_mask:
            heapq.heappush(heap, (vertex[2], i, vertex))

    return np.array([idx for (_, idx, _) in heapq.nsmallest(k, heap)]).reshape((-1, 1)), np.array(
        [vertex for (_, _, vertex) in heapq.nsmallest(k, heap)]
    ).reshape((-1, 3))

def find_maximum(mesh, k: int = 1, idx_mask=[]):
    """
    Finds the maximum value(s) and corresponding index(s) in the z-coordinate of the mesh's vertices.

    Args:
        mesh (open3d.geometry.TriangleMesh): The input mesh.

        k (int, optional): The number of maximum values to return. Defaults to 1.

        idx_mask (list, optional): List of vertex indices to consider for the maximum search. Defaults to empty list.

    Returns:
        tuple: Two numpy arrays, one containing the index(s) of the maximum value(s) and the other containing
        the maximum value(s) in the z-coordinate of the mesh's vertices.
    """
    mesh_vertices_np = np.asarray(mesh.vertices)
    idx_mask = set(idx_mask)
    # print(mesh_vertices_np, type(mesh.vertices))

    heap: list = []
    for i, vertex in enumerate(mesh_vertices_np):
        if len(idx_mask) == 0:
            heapq.heappush(heap, (vertex[2], i, vertex))

        elif len(idx_mask) > 0 and i in idx_mask:
            heapq.heappush(heap, (vertex[2], i, vertex))

    return np.array([idx for (_, idx, _) in heapq.nlargest(k, heap)]).reshape((-1, 1)), np.array(
        [vertex for (_, _, vertex) in heapq.nlargest(k, heap)]
    ).reshape((-1, 3))

def find_all_below(mesh, value: float, inclusive=False):
    """
    Finds all the vertices below the given value in the z-coordinate.

    Args:
        mesh (open3d.geometry.TriangleMesh): The input mesh.

        value (float): The value below which to find vertices.

        inclusive (bool, optional): Whether to include vertices with value equal to the given value. Defaults to False.

    Returns:
        tuple: Two numpy arrays, one containing the indices and the other containing the vertices that are below the
        given value in the z-coordinate.
    """
    mesh_vertices_np = np.asarray(mesh.vertices)
    # print(mesh_vertices_np, type(mesh.vertices))

    idx = []
    res = []
    if inclusive:
        for v, vertex in enumerate(mesh_vertices_np):
            if vertex[2] <= value:
                idx.append(v)
                res.append(vertex)
    else:
        for v, vertex in enumerate(mesh_vertices_np):
            if vertex[2] < value:
                idx.append(v)
                res.append(vertex)
    return np.array(idx), np.array(res).reshape((-1, 3))

def find_all_above(mesh, value: float, inclusive=False):
    """
    Finds all the vertices above the given value in the z-coordinate.

    Args:
        mesh (open3d.geometry.TriangleMesh): The input mesh.

        value (float): The value above which to find vertices.

        inclusive (bool, optional): Whether to include vertices with value equal to the given value. Defaults to False.

    Returns:
        tuple: Two numpy arrays, one containing the indices and the other containing the vertices that are above the
        given value in the z-coordinate.
    """
    mesh_vertices_np = np.asarray(mesh.vertices)
    # print(mesh_vertices_np, type(mesh.vertices))

    idx = []
    res = []
    if inclusive:
        for v, vertex in enumerate(mesh_vertices_np):
            if vertex[2] >= value:
                idx.append(v)
                res.append(vertex)
    else:
        for v, vertex in enumerate(mesh_vertices_np):
            if vertex[2] > value:
                idx.append(v)
                res.append(vertex)
    return np.array(idx), np.array(res).reshape((-1, 3))

def find_all_between(mesh, lower_value: float, higher_value: float) -> np.ndarray:
    """
    Returns a NumPy array of vertices whose z-coordinate is between the given lower and higher values.

    Args:
        mesh (open3d.geometry.TriangleMesh): A triangle mesh object.

        lower_value (float): The lower bound of the z-coordinate.

        higher_value (float): The higher bound of the z-coordinate.

    Returns:
        np.ndarray: A NumPy array of vertices whose z-coordinate is between the given lower and higher values.
    """
    mesh_vertices_np = np.asarray(mesh.vertices)
    # print(mesh_vertices_np, type(mesh.vertices))

    res = []
    for vertex in mesh_vertices_np:
        if lower_value < vertex[2] < higher_value:
            res.append(vertex)
    return np.array(res).reshape((-1, 3))

def make_point_cloud(vertices, color):
    """
    Creates a point cloud object with the given vertices and color.

    Args:
        vertices (np.ndarray): A NumPy array of vertices.

        color (tuple): A tuple of RGB values (0-255).

    Returns:
        open3d.geometry.PointCloud: A point cloud object.
    """
    pc = o3d.geometry.PointCloud()
    pc.points = o3d.utility.Vector3dVector(vertices)
    pc.paint_uniform_color(np.array(color) / 255)
    return pc

def find_neighbors(mesh, index: int):
    """
    Returns the indices and coordinates of the neighboring vertices of the given vertex index.

    Args:
        mesh (open3d.geometry.TriangleMesh): A triangle mesh object.

        index (int): The index of the vertex.

    Returns:
        Tuple[np.ndarray, np.ndarray]: A tuple of two NumPy arrays
        representing the neighboring vertex indices and coordinates.
    """
    mesh_triangles_np = np.asarray(mesh.triangles)
    # print('Printing Tris')
    neighbors = set()
    for tri in mesh_triangles_np:
        if index in tri:
            for i in tri:
                neighbors.add(i)
    neighbors.remove(index)
    return np.array([*neighbors]), np.asarray(mesh.vertices)[np.array([*neighbors])]

def mesh_subdivision(mesh, iterations=1):
    """
    Returns a new triangle mesh object obtained by subdividing the given mesh.

    Args:
        mesh (open3d.geometry.TriangleMesh): A triangle mesh object.

        iterations (int): The number of times to subdivide the mesh.

    Returns:
        open3d.geometry.TriangleMesh: A new triangle mesh object obtained by subdividing the given mesh.
    """
    return mesh.subdivide_midpoint(number_of_iterations=iterations)

def calculate_bounds_height(mesh):
    """
    Returns the minimum z-coordinate of the vertices at the boundary of the given mesh.

    Args:
        mesh (open3d.geometry.TriangleMesh): A triangle mesh object.

    Returns:
        float: The minimum z-coordinate of the vertices at the boundary of the given mesh.
    """
    mesh_vertices_np = np.asarray(mesh.vertices)
    min_x_vertex = mesh_vertices_np[np.argmin(mesh_vertices_np[:, 0])]
    max_x_vertex = mesh_vertices_np[np.argmin(mesh_vertices_np[:, 0])]
    min_y_vertex = mesh_vertices_np[np.argmin(mesh_vertices_np[:, 1])]
    max_y_vertex = mesh_vertices_np[np.argmin(mesh_vertices_np[:, 1])]

    return min(min_x_vertex[2], max_x_vertex[2], min_y_vertex[2], max_y_vertex[2])

def find_accessible(mesh, rain_direction):
    """
    Returns the indices and coordinates of the vertices accessible by a given angle of particles' direction.

    Args:
        mesh (open3d.geometry.TriangleMesh): The mesh to check.

        rain_direction (numpy.ndarray): The direction angle of the rain as a numpy array of shape (3,).
            If direction is [0,0,0], every vertex is chosen.

    Returns:
        Tuple[np.ndarray, np.ndarray]: A tuple of two NumPy arrays
        representing the neighboring vertex indices and coordinates.
    """
    rain_direction = np.asarray(rain_direction)
    # if rain_direction[2] == 0:
    #     return np.array([]), np.array([])
    raycast_angle = rain_direction * -1
    # print(raycast_angle, type(rain_direction))
    mesh_vertices_np = np.asarray(mesh.vertices)

    angle_np = np.full(mesh_vertices_np.shape, raycast_angle)
    ray_np = np.append(mesh_vertices_np, angle_np, axis=1)

    rays = o3d.core.Tensor(ray_np, dtype=o3d.core.Dtype.Float32)
    # print('ray', rays)
    rays[:, 0] += 1e-6 * np.sign(raycast_angle[0])
    rays[:, 1] += 1e-6 * np.sign(raycast_angle[1])
    rays[:, 2] += 1e-6 * np.sign(raycast_angle[2])

    # print('ray added', rays)
    raycasting_scene = o3d.t.geometry.RaycastingScene()
    mesh_legacy = o3d.t.geometry.TriangleMesh.from_legacy(mesh)
    _ = raycasting_scene.add_triangles(mesh_legacy)
    collision = raycasting_scene.cast_rays(rays)

    idx = []
    res = []
    collisions_hit = collision['t_hit'].numpy()
    # print('hit', collisions_hit)

    for v, vertex in enumerate(mesh_vertices_np):
        if collisions_hit[v] == np.inf:
            idx.append(v)
            res.append(vertex)
    return np.array(idx), np.array(res).reshape((-1, 3))

def erode(mesh: o3d.geometry.TriangleMesh, iterations: int = 2, erosion_lifetime: int = 10, direction=None, verbose=[]):
    """
    Erodes the mesh using the particle deposition and erosion method.

    Args:
        mesh (open3d.geometry.TriangleMesh): The mesh to be eroded.

        iterations (int, optional): The number of iterations for the erosion process. Defaults to 2.

        erosion_lifetime (int, optional): The maximum number of times a vertex can be eroded. Defaults to 10.

        direction (numpy.ndarray, optional): The direction of the rain as a numpy array of shape (3,).
            If not provided, the vertex mask is calculated from the mesh's bounding box. Defaults to None.

        verbose (list[str], optional): A list of strings containing information to be printed.
            Possible strings include 'all', 'vertex_progression', 'vector_direction',
            'vector_angle', 'ray', 'ray_scene', 'mesh', and 'collision'.
            Defaults to an empty list.

    Returns:
        Tuple[np.ndarray, open3d.geometry.TriangleMesh]: A tuple containing the updated vertex
        indices and the eroded mesh.

    The `verbose` argument takes a list of strings that specify what information should be printed.
        - 'all': Prints all below.
        - 'vertex_progression': Prints information about vertex triples.
        - 'vector_direction': Prints information about vector directions.
        - 'vector_angle': Prints information about vector angles.
        - 'ray': Prints information about the ray being casted.
        - 'ray_scene': Prints information about the raycasting scene.
        - 'mesh': Prints information about the mesh.
        - 'collision': Prints information about the collision detection.
    """
    # total_vertex_count = np.asarray(mesh.vertices).shape[0]
    verbose = set(verbose)
    if len(verbose) > 0:
        print('Printing with Settings:', verbose)

    if direction is None:
        vertices_idx, vertices = find_all_above(mesh, calculate_bounds_height(mesh), True)
    else:
        vertices_idx, vertices = find_accessible(mesh, rain_direction=direction)

    set_vertices_idx = set()
    for idx in vertices_idx:
        set_vertices_idx.add(idx)
    # print('set', set_vertices_idx)

    # Create New Mesh
    new_mesh = o3d.geometry.TriangleMesh()
    new_vertices = np.asarray(mesh.vertices)
    new_triangles = np.asarray(mesh.triangles)
    updated_vertices = []

    new_mesh.vertices = o3d.utility.Vector3dVector(new_vertices)
    new_mesh.triangles = o3d.utility.Vector3iVector(new_triangles)

    mesh_max_height = find_maximum(mesh)[1][0, 2]
    rng = np.random.default_rng(10)

    for iter_no in range(iterations):
        v_idx_curr = int(vertices_idx[int(rng.random() * vertices.shape[0])])
        print('Iter: ', iter_no, ', V_idx: ', v_idx_curr)

        lifetime = 0
        strength = 0.5
        v_idx_prev = None

        while lifetime < erosion_lifetime:
            neighbors_idx, _ = find_neighbors(new_mesh, v_idx_curr)
            v_idx_next = int(find_minimum(new_mesh, 1, neighbors_idx)[0])

            if v_idx_next not in vertices_idx and direction is None:
                break

            if v_idx_prev:
                if 'vertex_progression' in verbose or 'all' in verbose:
                    print(
                        'Vertex Triples:',
                        v_idx_prev,
                        v_idx_curr,
                        v_idx_next,
                        type(new_vertices[v_idx_prev]),
                        new_vertices[v_idx_prev],
                        new_vertices[v_idx_curr],
                        new_vertices[v_idx_next],
                    )
                vector_1 = new_vertices[v_idx_curr] - new_vertices[v_idx_prev]
                vector_2 = new_vertices[v_idx_next] - new_vertices[v_idx_curr]
                direction_1 = np.sign(vector_1)
                direction_2 = np.sign(vector_2)
                if 'vector_direction' in verbose or 'all' in verbose:
                    print(vector_1, vector_2, direction_1, direction_2, np.dot(direction_1, direction_2))

                if vector_2[2] > 0:
                    break

                vector_1[2] = 0
                vector_2[2] = 0
                norm_vector_1 = np.linalg.norm(vector_1)
                norm_vector_2 = np.linalg.norm(vector_2)
                angle = np.arccos(np.clip(np.dot(vector_1, vector_2) / (norm_vector_1 * norm_vector_2), -1.0, 1.0))
                if 'vector_angle' in verbose or 'all' in verbose:
                    print(vector_1, vector_2, direction_1, direction_2, np.dot(direction_1, direction_2), angle)
                    # if angle < 0:
                    #     print('Negative angle Calculated', angle)
                if angle > np.pi / 2:
                    if 'vector_angle' in verbose or 'all' in verbose:
                        print('Angle found > PI / 2', angle)
                    break

            ray = o3d.core.Tensor([[*new_vertices[v_idx_curr], 0, 0, 1]], dtype=o3d.core.Dtype.Float32)
            if 'ray' in verbose or 'all' in verbose:
                print('ray:', ray, 'vertex:', new_vertices[v_idx_curr])

            ray[0, 2] += 1e-6
            if 'ray' in verbose or 'all' in verbose:
                print('updated ray:', ray, 'vertex:', new_vertices[v_idx_curr])

            raycasting_scene = o3d.t.geometry.RaycastingScene()
            if 'ray_scene' in verbose or 'all' in verbose:
                print('scene:', raycasting_scene)

            if 'mesh' in verbose or 'all' in verbose:
                print('new mesh', new_mesh, type(new_mesh))
            new_mesh_legacy = o3d.t.geometry.TriangleMesh.from_legacy(new_mesh)
            if 'mesh' in verbose or 'all' in verbose:
                print('new mesh legacy', new_mesh_legacy, type(new_mesh_legacy))

            mesh_id = raycasting_scene.add_triangles(new_mesh_legacy)
            if 'mesh' in verbose or 'all' in verbose:
                print('mesh_id:', mesh_id)

            collision = raycasting_scene.cast_rays(ray)
            if 'collision' in verbose or 'all' in verbose:
                print(
                    'collision t_hit:',
                    collision['t_hit'],
                    type(collision['t_hit']),
                    collision['t_hit'].numpy(),
                    type(collision['t_hit'].numpy()),
                )

            if collision['t_hit'].numpy()[0] == np.inf:
                new_vertices[v_idx_curr, 2] -= (
                    strength * abs(new_vertices[v_idx_next, 2] - new_vertices[v_idx_curr, 2]) / mesh_max_height
                )
            else:
                new_vertices[v_idx_curr, 2] += (
                    strength * abs(new_vertices[v_idx_next, 2] - new_vertices[v_idx_curr, 2]) / mesh_max_height
                )

            new_mesh.vertices = o3d.utility.Vector3dVector(new_vertices)
            updated_vertices.append(v_idx_curr)

            lifetime += 1
            strength *= 0.69
            v_idx_prev = v_idx_curr
            v_idx_curr = v_idx_next

    if len(verbose) > 0:
        print('')

    new_mesh.vertices = o3d.utility.Vector3dVector(new_vertices)
    new_mesh.triangles = o3d.utility.Vector3iVector(new_triangles)
    return np.array(updated_vertices), new_mesh


# 나무꾼 이다솜

import sys
input = lambda : sys.stdin.readline().strip()

n, c, w = map(int, input().split())
arr = [int(input()) for _ in range(n)]
arr.sort()
ans = 0
cnt = arr[-1]
# # 자를 때
while cnt > 0:
    total = 0
    for tree in arr:
        # 개수 = (나무 // 자르는 길이) + 같으면 1, 아니면 0(tree == cnt)
        # 길이 * 개수 * 가격 - 비용              4 // 2라면 한 번만 자르면 되니까 True/False로 뺴주기
        tmp = (cnt * (tree // cnt) * w) - (c * ((tree // cnt) - ((tree % cnt) == 0)))
        if tmp < 0:     # 얘 때문에 계속 틀림 ㅠ
            continue
        total += tmp

    ans = max(total, ans)
    cnt -= 1

print(ans)

"""
4개 = 40원 = 240원
9개 = 90원 = 540원
17개 = 170원 = 1020원

26
1개 = 260원 = 0번 자름 = 260원
3개 = 780원 = 3번 자름 = 480원
2개 = 520원 = 2번 자름 = 320원 = 1060원
    1560원

170원?

26 * 6개 = 156
156 * 10원 = 1560
5번 자름 - 500
1060

나무 버리고 가도 되니까 범위 제일 큰 애로 잡기
"""


#client code (unencrypted)

from socket import *

#setting up network requirements
my_socket = socket(AF_INET, SOCK_STREAM, 0)
ipv4_tuple = "127.0.0.1", 1066

#try connecting
my_socket.connect(ipv4_tuple)

#if statement to keep giving an option for chat

while True:
    input_message = input("To exit type quit, or input your message: ")

    if input_message == "quit":
        break
    else:
        encoded_message = bytes(input_message, 'ascii')
        print("Sending you message")
        my_socket.send(encoded_message, 0)

encoded_message = bytes(input_message, 'ascii')
my_socket.send(encoded_message, 0)
print("The Client has chosen to Close this connection!")
my_socket.close()


import Init_MIRT

def DSY(a,b):  #DSY迭代程序
    l=int(input('请输入题目个数'))
    print(a)
    print(b)
    N=Init_MIRT.transfer_DEX_to_bin(l)
    Theta=[]
    Save_THETA=[]
    Save_L_new=[]
    Theta_Save_Each_Iterations=[]
    Iterations=[]
    Save_sub_original=[]

    for d in range(2**l):       #对16种情况进行大历遍循环
        t=N[d]
        K= 0.1                 #历遍初值为0.1

        L=Init_MIRT.Cal_L(a,b,K,t,l)#构造L初值
        len, L_each, Lmax, The=Init_MIRT.Show_L(a,b,t)#构造L变化图像

        for i in range(500):
            k1=Init_MIRT.random_normal(K)
            Save_THETA.append(k1)       #存储新构建的θ值

            L_New=Init_MIRT.Cal_L(a,b,k1,t,l)#构造新的L值

            Save_L_new.append(L_New)    #存储新构建的L值
            SUB = L_New - L

            Save_sub_original.append(SUB)
            if L_New>L:                 #满足条件转移，更新L和θ
                L=L_New
                K=k1
            Theta_Save_Each_Iterations.append(K)#存储每次迭代产生的θ
            Iterations.append(i)        #存储迭代次数
            # if abs(SUB)<0.001:
            #      print('误差满足，停止迭代')
            #      break

        L_END=L                          #迭代完成后的L值
        TEHTA_END=K                     #迭代完成后的θ值
        Theta.append(K)                 #装载最终的结果
        #Init_MIRT.Draw_scatter(Save_THETA,Save_L_new,L_END,d+1,Iterations,Theta_Save_Each_Iterations,Save_sub_original,len, L_each, Lmax, The)#绘制图像
        print('第%d种θ值估算已完成'%(d+1)+' θ= %f'%K+',共进行了%d次估计'%(i+1))
        print(t)
        print('*********************************************')
        Save_L_new.clear()              #清除记录，准备新的迭代
        Save_THETA.clear()
        Theta_Save_Each_Iterations.clear()
        Iterations.clear()

        Save_sub_original.clear()

        #print('本次迭代组合为'+N[d])
    print('全部迭代已完成,全部能力值结果为：')
    for num in range(2**l):
        a=Theta[num]
        print('%.4f'%a)


#codewars.com
"""
Write a function that accepts an array of 10 integers (between 0 and 9), that returns a string of those numbers in the form of a phone number.

Example:
createPhoneNumber(Array[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) # => returns "(123) 456-7890"
The returned format must be correct in order to complete this challenge.
Don't forget the space after the closing parentheses!
"""

def create_phone_number(numbers):
  n = ''.join(str(each)for each in numbers)
  area_code = n[:3]
  first = n[3:6]
  second = n[6:]
  # return ("(%(area_code)s) %(first)s-%(second)s", % locals())
  return print(f"({area_code}) {first}-{second}")

create_phone_number([1,2,3,4,5,6,7,8,9,0])


import math
import os
import platform
import subprocess
import sys

def main():
    if len(sys.argv) != 3:
        raise ValueError("Usage: python run_experiments.py num_times conf_file")
    try:
        num_times = int(sys.argv[1])
    except ValueError:
        raise ValueError("Usage: python run_experiments.py num_times conf_file")

    conf_file = sys.argv[2]

    cur_work_dir = os.getcwd()
    build_dir = os.path.join(cur_work_dir, 'build')
    os.makedirs(build_dir, exist_ok=True)

    # It's unnecessary to add the .exe ending on Windows, but I did it
    # for clarity
    if platform.system() == 'Windows':
        exe = os.path.join(build_dir, 'process_text.exe')
    else:
        exe = os.path.join(build_dir, 'process_text')

    cmake_flags = ['-DCMAKE_BUILD_TYPE=Release', '-G', 'Unix Makefiles']
    subprocess.run(['cmake', '..'] + cmake_flags, cwd=build_dir, check=True)
    subprocess.run(['cmake', '--build', '.'], cwd=build_dir, check=True)

    print("Testing text processing, microseconds:")

    max_threads = 4
    last_out_by_a = None
    for indexing_threads in range(1, max_threads+1):
        with open(conf_file, 'r') as f:
            lines = f.readlines()
            lines[3] = f'indexing_threads={indexing_threads}\n'
        with open(conf_file, 'w') as f:
            f.writelines(lines)

        min_time = float('inf')
        for _ in range(num_times):
            p = subprocess.run([exe, conf_file], capture_output=True, text=True, check=True)
            output = p.stdout.split()
            time = float(output[1])
            if time < min_time:
                min_time = time

        # We're only interested in checking the results for race
        # conditions, therefore it is sufficient to check only one
        # output file
        out_by_a_name = lines[1].strip().split('=')[1]
        with open(out_by_a_name, 'r', encoding='UTF-8') as f:
            out_by_a = f.read()
        if last_out_by_a is not None and last_out_by_a != out_by_a:
            raise Exception("The results are not the same.")
        last_out_by_a = out_by_a

        print(f'{indexing_threads}: {min_time}')

    print("\nAll results are the same.")

if __name__ == '__main__':
    main()


from disnake import ApplicationCommandInteraction
from disnake.ext.commands import Bot, Cog, slash_command

class Example(Cog):
    def __init__(self, bot: Bot) -> None:
        self.bot = bot

    @slash_command(description="Say hello.")
    async def hello(self, interaction: ApplicationCommandInteraction) -> None:
        await interaction.response.send_message(f"Hello {interaction.author.mention}!")

def setup(bot: Bot) -> None:
    bot.add_cog(Example(bot))


# -*- coding: utf-8 -*-
# Verificando a letra digitada é uma vogal
# Você consegue encontrar o que tem de errado?

a = input('Digite uma letra')

vogais = [ "a", "e", "i", "o", "u" ]

if len(a) != 1:
  print("Por favor, digite apenas uma letra")
elif a.lower() not in vogais:
    print("A sua letra é uma vogal")
else:
    print("A sua letra não é uma vogal")


# ruff: noqa: INP001
from enum import Enum
from pathlib import Path

import pytest
import requests

# https://selenium-python.readthedocs.io/locating-elements.html
import selenium.webdriver.support.expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

WINDOW_WIDTH = 1400
WINDOW_HEIGHT = 1450

# Copied over from aiidalab_widgets_base/wizard.py
class StepState(Enum):
    """Wizzard step state"""

    INIT = 0  # the step is initialized and all widgets are typically disabled
    CONFIGURED = 1  # configuration is valid
    READY = 2  # step is ready for user input
    ACTIVE = 3  # step is carrying out a runtime operation
    SUCCESS = 4  # step has successfully completed
    FAIL = -1  # the step has unrecoverably failed

@pytest.fixture()
def check_step_status(selenium):
    ICONS = {
        StepState.INIT: "○",
        StepState.READY: "◎",
        StepState.CONFIGURED: "●",
        StepState.SUCCESS: "✓",
        StepState.FAIL: "×",  # noqa: RUF001
        # The ACTIVE state is "animated", see aiidalab_widgets_base/wizard.py,
        # hence we cannot use it in tests.
        # WizardAppWidgetStep.State.ACTIVE: ["\u25dc", "\u25dd", "\u25de", "\u25df"],
    }

    def _check_step_status(step_num, expected_state: StepState):
        icon = ICONS[expected_state]
        selenium.find_element(
            By.XPATH, f"//span[starts-with(.,'{icon} Step {step_num}')]"
        )

    return _check_step_status

@pytest.mark.tryfirst()
def test_post_install(notebook_service, aiidalab_exec, nb_user, appdir):
    aiidalab_exec("./post_install", workdir=appdir, user=nb_user)

def test_notebook_service_available(notebook_service):
    url, token = notebook_service
    response = requests.get(f"{url}/?token={token}", timeout=100)
    assert response.status_code == 200

def test_dependencies(notebook_service, aiidalab_exec, nb_user):
    aiidalab_exec("pip check", user=nb_user)

def test_conformer_generation_init(selenium_driver, final_screenshot):
    driver = selenium_driver("conformer_generation.ipynb", wait_time=30.0)
    driver.set_window_size(WINDOW_WIDTH, WINDOW_HEIGHT)
    driver.find_element(By.XPATH, "//button[text()='Generate molecule']")

def test_conformer_generation_steps(
    selenium_driver, final_screenshot, generate_mol_from_smiles, check_atoms
):
    driver = selenium_driver("conformer_generation.ipynb", wait_time=30.0)
    driver.set_window_size(WINDOW_WIDTH, WINDOW_HEIGHT)

    # Generate methane molecule
    generate_mol_from_smiles("C")

    # Switch to Selection tab
    selection = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//*[text()='Selection']"))
    )
    selection.click()
    # Select atoms and verify them
    # TODO: Firefox seems to fail for atoms beyond the first one,
    # so we just check the first one for now
    # check_atoms("CHHHH")
    check_atoms("C")

    # Test different generation options
    driver.find_element(By.XPATH, "//option[@value='UFF']").click()
    driver.find_element(By.XPATH, "//option[@value='ETKDGv1']").click()
    generate_mol_from_smiles("N")
    check_atoms("N")

    driver.find_element(By.XPATH, "//option[@value='MMFF94s']").click()
    driver.find_element(By.XPATH, "//option[@value='ETKDGv2']").click()
    generate_mol_from_smiles("O")
    check_atoms("O")

    # Switch to `Download` tab in StructureDataViewer
    driver.find_element(By.XPATH, "//*[text()='Download']").click()
    download = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//button[text()='Download']"))
    )
    download.click()

def test_optimization_init(
    selenium_driver, final_screenshot, button_disabled, check_step_status
):
    driver = selenium_driver("optimization.ipynb", wait_time=30.0)
    driver.set_window_size(WINDOW_WIDTH, WINDOW_HEIGHT)
    driver.find_element(By.XPATH, "//button[text()='Generate molecule']")
    check_step_status(1, StepState.READY)
    check_step_status(2, StepState.INIT)
    check_step_status(3, StepState.INIT)
    button_disabled("Confirm")
    button_disabled("Submit")

def test_optimization_steps(
    selenium_driver,
    final_screenshot,
    button_enabled,
    button_disabled,
    generate_mol_from_smiles,
    check_atoms,
    check_step_status,
):
    driver = selenium_driver("optimization.ipynb", wait_time=30.0)
    driver.set_window_size(WINDOW_WIDTH, WINDOW_HEIGHT)

    check_step_status(1, StepState.READY)
    check_step_status(2, StepState.INIT)
    check_step_status(3, StepState.INIT)
    button_disabled("Confirm")
    button_disabled("Submit")

    driver.find_element(By.XPATH, "//button[text()='Generate molecule']")
    # Generate methane molecule
    generate_mol_from_smiles("C")
    driver.find_element(By.XPATH, "//*[text()='Selection']").click()
    check_atoms("C")

    check_step_status(1, StepState.CONFIGURED)
    button_enabled("Confirm")
    driver.find_element(By.XPATH, "//button[text()='Confirm']").click()

    # Test that we have indeed proceeded to the next step
    check_step_status(1, StepState.SUCCESS)
    # NOTE: In CI we do not have ORCA, so the step state is not CONFIGURED
    # check_step_status(2, StepState.CONFIGURED)
    check_step_status(2, StepState.READY)
    check_step_status(3, StepState.INIT)

    button_disabled("Confirm")
    button_disabled("Submit")
    WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.XPATH, "//button[text()='Submit']"))
    )

def test_spectrum_app_init(selenium_driver, final_screenshot):
    driver = selenium_driver("spectrum_widget.ipynb", wait_time=30.0)
    driver.set_window_size(WINDOW_WIDTH, WINDOW_HEIGHT)
    driver.find_element(By.XPATH, "//button[text()='Download spectrum']")

def test_atmospec_app_init(selenium_driver, final_screenshot, check_step_status):
    driver = selenium_driver("atmospec.ipynb", wait_time=30.0)
    driver.set_window_size(WINDOW_WIDTH, WINDOW_HEIGHT)
    check_step_status(1, StepState.READY)
    check_step_status(2, StepState.INIT)
    check_step_status(3, StepState.INIT)
    check_step_status(4, StepState.INIT)
    driver.find_element(By.XPATH, "//button[text()='Refresh']")

def test_atmospec_steps(
    selenium_driver,
    screenshot_dir,
    final_screenshot,
    button_enabled,
    button_disabled,
    generate_mol_from_smiles,
    check_atoms,
    check_step_status,
):
    driver = selenium_driver("atmospec.ipynb", wait_time=40.0)
    driver.set_window_size(WINDOW_WIDTH, WINDOW_HEIGHT)

    check_step_status(1, StepState.READY)
    # Because we don't have a structure yet, the confirm button should be disabled
    button_disabled("Confirm")
    button_disabled("Submit")

    # Generate methane molecule
    generate_mol_from_smiles("C")
    check_atoms("C")
    driver.get_screenshot_as_file(
        Path.joinpath(screenshot_dir, "atmospec-steps-mol-generated.png")
    )
    check_step_status(1, StepState.CONFIGURED)

    # Confirm structure and go to the next step
    button_enabled("Confirm")
    button_disabled("Submit")
    WebDriverWait(driver, 30).until(
        EC.element_to_be_clickable((By.XPATH, "//button[text()='Confirm']"))
    ).click()

    # Once the structure is confirmed, the button should be disabled
    button_disabled("Confirm")

    check_step_status(1, StepState.SUCCESS)
    check_step_status(2, StepState.READY)
    check_step_status(3, StepState.INIT)
    check_step_status(4, StepState.INIT)

    # Make sure the submit button is visible. It should not be clickable since
    # ORCA is not installed.
    WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.XPATH, "//button[text()='Submit']"))
    )
    button_disabled("Submit")


# venv\scripts\activate
# $env:FLASK_APP = "app"
# $env:FLASK_DEBUG = 1
# python -m flask run
#
# http://flask-bootstrap.azurewebsites.net/

import datetime

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.dates import DayLocator, DateFormatter
from io import BytesIO

import random

from flask import Flask, render_template, Response, request, redirect, url_for, json
from urllib.request import Request, urlopen
from flask_paginate import Pagination, get_page_parameter

app = Flask(__name__)
users = []

@app.route("/")
def home():
    return render_template('jumbotron.html')

@app.route('/hello/<name>')
def hello(name=None):
    return render_template('hello.html', name=name)

@app.route('/email', methods=['POST'])
def email():
    return render_template('email.html', email=request.form.get('email_address'))

@app.route("/json/local")
def json_local():
    with open('data/users.json') as f:
        local_users = json.load(f)
    return render_template('json_local.html', users=local_users)

@app.route("/json/remote")
def json_remote():
    url = "https://api.github.com"
    response = urlopen(url)
    github_urls = json.loads(response.read())
    return render_template('json_remote.html', urls=github_urls)

@app.route('/json/users', methods=['GET'])
def json_users():

    global users

    if len(users) == 0:
        with open('data/users.json') as f:
            users = json.load(f)

    return render_template('json_users.html', users=users)

@app.route('/json/users/add', methods=['POST'])
def json_add():

    global users

    users.append({"name": request.form.get('name')})
    return redirect(url_for('json_users'))

# Random Duck API

duck_url = "https://random-d.uk/api/v2/"

# paginate, filter and sort

@app.route("/duck")
def duck():

    global duck_url

    query = duck_url + "random"
    req = Request(query, headers={'User-Agent': 'Mozilla/5.0'})
    res = urlopen(req)
    duck_json = json.loads(res.read())

    return render_template('duck/duck.html', duck=duck_json)

@app.route("/ducks")
def ducks():

    global duck_url
    # "image_count": 282,
    # "images": []

    search = False
    q = request.args.get('q')
    if q:
        search = True

    page = request.args.get(get_page_parameter(), type=int, default=1)

    query = duck_url + "list"
    req = Request(query, headers={'User-Agent': 'Mozilla/5.0'})
    res = urlopen(req)
    ducks_json = json.loads(res.read())

    pagination = Pagination(page=page, total=ducks_json['image_count'], search=search, record_name='ducks')

    return render_template('duck/ducks.html', ducks=ducks_json['images'], url=duck_url, pagination=pagination)

# FHRS API

fhrs_url = "http://api.ratings.food.gov.uk"

@app.route("/countries")
def countries():

    global fhrs_url

    # page = 1
    # page_size = 10

    # Create the URL string to query the FHRS API
    # In this case, http://api.ratings.food.gov.uk/Countries/basic
    # I found the API query failed without adding, headers = {'x-api-version': 2}
    # You may want to limit the number of results you bring back from the API
    # For example, query = fhrs_url + '/Countries/basic/{}/{}'.format(page, page_size)

    query = fhrs_url + '/Countries/basic'
    headers = {'x-api-version': 2}

    req = Request(query, headers=headers)
    res = urlopen(req)
    fhrs_json = json.loads(res.read())

    return render_template('countries.html', data=fhrs_json)

# Graph

@app.route("/graph")
def graph():
    str_date = datetime.datetime.now()
    return render_template('graph.html', date=str_date)

@app.route('/plot.png')
def plot_png():
    fig = create_figure()
    output = BytesIO()
    FigureCanvas(fig).print_png(output)
    return Response(output.getvalue(), mimetype='image/png')

def create_figure():

    dataset = "./data/NY-City-Central-Park-Historical-Temperature-Data.csv"
    # dataframe
    df = pd.read_csv(dataset)
    # Convert the date column, 18690101, to a Python datetime
    df['date'] = pd.to_datetime(df['date'], format='%Y%m%d')
    # Locate a range of dates
    start_date = '07-01-2020'
    end_date = '08-01-2020'
    mask = (df['date'] > start_date) & (df['date'] <= end_date)
    df = df.loc[mask]
    # Convert the min, max columns from Fahredheit to Celsius
    # (32°F − 32) × 5/9 = 0°C
    df['min'] = (df['min']-32) * 5/9
    df['max'] = (df['max']-32) * 5/9
    # Create the Figure
    fig = Figure()
    ax = fig.subplots()
    ax.plot(df['date'], df['max'])
    #defines the tick location
    ax.xaxis.set_major_locator(DayLocator())
    #defines the label format
    ax.xaxis.set_major_formatter(DateFormatter("%d-%m"))
    ax.tick_params(axis="x", labelrotation= 90)

    return fig


class Solution:
    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:
        numsDict = {}

        for index, num in enumerate(nums):
            numsDict[num] = index

        for op in operations:
            index = numsDict[op[0]]
            del numsDict[op[0]]
            numsDict[op[1]] = index

        modifiedArr = [0] * len(nums)
        for num in numsDict:
            index = numsDict[num]
            modifiedArr[index] = num

        return modifiedArr


#!/usr/bin/env python
#
# Ref. https://github.com/karulis/pybluez
# Ref. https://people.csail.mit.edu/albert/bluez-intro/x232.html
# run in RPi py3, send cmd via HC05 and collect AoA meas at each angular position.
# theoretically such ctrl could be done using laptop with BLE hardware, and automatically scp the result produced in RPi to laptop
# I want direct replacement of save to file module to AoA estimation module, that's why run it in RPi

# ./AoArcvSPI -l100 -m2

import serial
try:
    from Queue import Queue
except:
    from queue import Queue
TakePhoto = True # True/False take one photo at each angular position for data collection
if TakePhoto:
    from picamera import PiCamera
    import cv2

import CONSTaoa
import logging
# Configure logging
loglevel = logging.INFO
logdatefmt = "%Y-%m-%d %H:%M:%S"
logfmt = "%(asctime)s %(filename)s:%(lineno)d %(levelname)s %(message)s"
logging.basicConfig(format=logfmt, level=loglevel, datefmt=logdatefmt)

import os,sys
from datetime import datetime
import time
import threading
from collections import deque
import sys
import getopt
import bluetooth
import subprocess
import pexpect as ep
import re
import bluetoothutils
import mqttutils
import transportLayer
import ctypes

if 2 == sys.version_info.major:
    pythonVer = 2
else:
    pythonVer = 3

def log_serialPort(serial_port,logfile):
    rv = b""
    while True:
        ch = serial_port.read()
        rv += ch
        if ch==b'' or ch ==b'\n':
            logfile.write(rv)
            return

def discover_devices(target):
    logging.info("Searching nearby devices...")
    nearby_devices = bluetooth.discover_devices(duration=4, lookup_names=True,
                                                flush_cache=True)
    logging.info("Found {} device(s)".format(len(nearby_devices)))
    target_devices = {}
    for bdaddr, bdname in nearby_devices:
        # logging.info("Bluetooth {} ({})".format(bdaddr, bdname))
        # bdname = bluetooth.lookup_name( bdaddr )
        if bdname.startswith(target):
            target_devices[bdaddr] = bdname
    return target_devices

def discover_devices_btctl(bc, target):
    devices = bc.getDevices()
    cands = {}
    for bdaddr, bdname in devices.items():
        if bdname.startswith(target):
            cands[bdaddr] = bdname
    return cands

def pair_devices_btctl(bc, cands):
    return bc.pairDevices(cands)

def discover_services(bdaddr):
    services = bluetooth.find_service(address=bdaddr)
    if len(services) > 0:
        print("Found %d services on %s" % (len(services), bdaddr))
        print("")
    else:
        print("No services found on %s" % (bdaddr,))
    for svc in services:
        print("Service Name: %s"    % svc["name"])
        print("    Host:        %s" % svc["host"])
        print("    Description: %s" % svc["description"])
        print("    Provided By: %s" % svc["provider"])
        print("    Protocol:    %s" % svc["protocol"])
        print("    channel/PSM: %s" % svc["port"])
        print("    svc classes: %s "% svc["service-classes"])
        print("    profiles:    %s "% svc["profiles"])
        print("    service id:  %s "% svc["service-id"])
        print("")
    return services

def send_ctrl_cmd(sock,BLDCctrlcmd):
    i32BLDCctrlcmd = bytearray(5)
    i32BLDCctrlcmd[0]=3 #applicable modes defined in Host_workspaces/utilities/arduinoBLDCgimbal/arduinoBLDCgimbal.ino
    # little endian
    i32BLDCctrlcmd[1]=(BLDCctrlcmd>>0)&0xFF
    i32BLDCctrlcmd[2]=(BLDCctrlcmd>>8)&0xFF
    i32BLDCctrlcmd[3]=(BLDCctrlcmd>>16)&0xFF
    i32BLDCctrlcmd[4]=(BLDCctrlcmd>>24)&0xFF
    ctrlStr = bytes(tl.construct(bytes(i32BLDCctrlcmd)))
    sock.send(ctrlStr)
    logging.debug("Send val:{} {}Bytes CtrlMsg:{}".format(BLDCctrlcmd,len(ctrlStr),ctrlStr))

def stateTransition(newState):
    global mState, mStateEntryEvt, m_entryTimeS
    mStateEntryEvt = True
    old_mState = mState
    mState = newState
    logging.info('state:{}=>{}'.format(old_mState,mState))
    m_entryTimeS = time.time()

def handle_socket_continuously(sock):
    global collectionii0, COLLECTIONCNT, btRcvStr, LastRcvDatDumpTimeS, currBLDCctrlcmd, etMode, mState, hackSkipBLE
    if not hackSkipBLE:
        sock.settimeout(BD_TIMEOUT)
    if etMode == CONSTaoa.HardCodeTurnSeq or etMode == CONSTaoa.collectLUT_then_CmdFromPC1:
        stateTransition(CONSTaoa.State_init)
    elif etMode == CONSTaoa.bldcCtrlCmdFromPC:
        stateTransition(CONSTaoa.State_wait4mqttMsg)
    else:
        print('gwtrherthghetryhj')
        sys.exit()
    GovernerBLDCctrlcmd = 0

    mStateEntryEvt = True
    global mqueue
    while True:
        if LogSerialData:
            log_serialPort(serialPort,logfp)
        if mState == CONSTaoa.State_init:
            if mStateEntryEvt:
                mStateEntryEvt = False
            if time.time() - m_entryTimeS > PauseInitS:
                stateTransition(CONSTaoa.State_SendBLDCctrlCmd)
                if etMode == CONSTaoa.HardCodeTurnSeq or etMode == CONSTaoa.collectLUT_then_CmdFromPC1:
                    logging.info('going to {}deg'.format(PosCtrlHwCmd[collectionii0]/ctrlSigCyclesPerRotation))
                elif etMode == CONSTaoa.collectLUT_then_CmdFromPC2 or etMode == CONSTaoa.bldcCtrlCmdFromPC:
                    logging.info('going to {}deg'.format(GovernerBLDCctrlcmd/ctrlSigCyclesPerRotation))
                else:
                    print("fgshgsdhgbshfvbls")
                    sys.exit()
            else:
                time.sleep(0.01)
        elif mState == CONSTaoa.State_SendBLDCctrlCmd:
            if etMode == CONSTaoa.HardCodeTurnSeq or etMode == CONSTaoa.collectLUT_then_CmdFromPC1:
                bldcSequenceCtrlcmd = PosCtrlHwCmd[collectionii0]
            elif etMode == CONSTaoa.bldcCtrlCmdFromPC or etMode == CONSTaoa.collectLUT_then_CmdFromPC2:
                bldcSequenceCtrlcmd = GovernerBLDCctrlcmd
            else:
                print('rteiugw4hjskdjfnbgfh')
                sys.exit()
            if mStateEntryEvt:
                mStateEntryEvt = False
            #if 3 == pythonVer:
            #    i32BLDCctrlcmd = ctypes.c_int32(bldcSequenceCtrlcmd)
            #else:
            if abs(currBLDCctrlcmd - bldcSequenceCtrlcmd) < BLDCctrlCmdMaxStep:
                currBLDCctrlcmd = bldcSequenceCtrlcmd
            else:
                if bldcSequenceCtrlcmd > currBLDCctrlcmd:
                    currBLDCctrlcmd += BLDCctrlCmdMaxStep
                else:
                    currBLDCctrlcmd -= BLDCctrlCmdMaxStep
            if not hackSkipBLE:
                send_ctrl_cmd(sock,currBLDCctrlcmd)
            stateTransition(CONSTaoa.State_TestdesiredPos)
        elif mState == CONSTaoa.State_TestdesiredPos:
            if mStateEntryEvt:
                mStateEntryEvt = False

            if time.time() - m_entryTimeS > PauseSendBLDCctrlCmd:
                if etMode == CONSTaoa.HardCodeTurnSeq or etMode == CONSTaoa.collectLUT_then_CmdFromPC1:
                    ctrl_target = bldcSequenceCtrlcmd
                else:
                    ctrl_target = GovernerBLDCctrlcmd

                if currBLDCctrlcmd == ctrl_target:
                    logging.info('reached {}deg'.format(ctrl_target/ctrlSigCyclesPerRotation))
                    if etMode == CONSTaoa.HardCodeTurnSeq or etMode == CONSTaoa.collectLUT_then_CmdFromPC1:
                        datfilename = "Log_{:0>3d}_{:0>5d}_{:0>2d}".format(collectionii0,bldcSequenceCtrlcmd/ctrlSigCyclesPerRotation,collectionii0%COLLECTION_EACH_LOCATION)
                    else:
                        datfilename = "LogGovernerMode"
                    if etMode == CONSTaoa.HardCodeTurnSeq and collectionii0 == len(PosCtrlHwCmd) - 1:
                        break

                    stateTransition(CONSTaoa.State_wait4mqttMsg)
                    if TakePhoto:
                        jpgfilename = "{}/{}.jpg".format(PhotoJpgStorageFolder,datfilename)
                        camera.capture(jpgfilename)

                    mqproxy.publish(mqttutils.TOPIC_RPiCmd,datfilename)
                else:
                    stateTransition(CONSTaoa.State_SendBLDCctrlCmd)
            else:
                time.sleep(0.01)

        elif mState == CONSTaoa.State_wait4mqttMsg:
            if mStateEntryEvt:
                mStateEntryEvt = False
            if wait2rcvMqttMsgBeforeProceed:
                while not mqueue.empty():
                    if LogSerialData:
                        log_serialPort(serialPort,logfp)
                    _client, _userdata, msg = mqueue.get()
                    if msg.topic == mqttutils.TOPIC_EmbdEvt:
                        strdata = msg.payload.decode("utf-8").strip()
                        logging.debug("mqtt rcvd msg:{}".format(strdata))
                        if strdata.startswith("DONE"):
                            if etMode == CONSTaoa.HardCodeTurnSeq or etMode == CONSTaoa.collectLUT_then_CmdFromPC1:
                                pass
                            elif etMode == CONSTaoa.bldcCtrlCmdFromPC or etMode == CONSTaoa.collectLUT_then_CmdFromPC2:
                                mqproxy.publish(mqttutils.TOPIC_RPiStatus,CONSTaoa.RPiStatus_pktCollected)
                            else:
                                logging.error("jmfyerfDFV")
                                sys.exit()

                            if etMode == CONSTaoa.HardCodeTurnSeq or etMode == CONSTaoa.collectLUT_then_CmdFromPC1:
                                stateTransition(CONSTaoa.State_init)
                                if collectionii0 == COLLECTIONCNT - 2:
                                    #if etMode == CONSTaoa.HardCodeTurnSeq or etMode == CONSTaoa.:
                                    #    break
                                    if etMode == CONSTaoa.collectLUT_then_CmdFromPC1:
                                        mqproxy.publish(mqttutils.TOPIC_RPiStatus,CONSTaoa.RPiStatus_LUTdatasetRdy)
                                        logging.info("LUT data preparation done")
                                        etMode = CONSTaoa.collectLUT_then_CmdFromPC2
                                        currBLDCctrlcmd = 0
                            elif etMode == CONSTaoa.bldcCtrlCmdFromPC or etMode == CONSTaoa.collectLUT_then_CmdFromPC2:
                                pass
                            else:
                                logging.error("tryhncmhl")
                                sys.exit()
                            collectionii0 = collectionii0 + 1
                    elif msg.topic == mqttutils.TOPIC_PcCmd:
                        if etMode == CONSTaoa.bldcCtrlCmdFromPC or etMode == CONSTaoa.collectLUT_then_CmdFromPC2:
                            GovernerBLDCctrlcmd = int(msg.payload.decode("utf-8").strip())
                            stateTransition(CONSTaoa.State_init)
                        elif etMode == CONSTaoa.HardCodeTurnSeq or CONSTaoa.collectLUT_then_CmdFromPC1:
                            pass
                        else:
                            logging.error("asdgsrhgsdhdtryjetyjhns")
                            sys.exit()

                time.sleep(0.01)
            else:
                if time.time() - m_entryTimeS > unconditionalWaitSec:
                    collectionii0 = collectionii0 + 1
                    stateTransition(CONSTaoa.State_init)
                    mStateEntryEvt = True
                else:
                    time.sleep(0.01)
        else:
            logging.error('unhandled state')
            break
        if etMode == CONSTaoa.HardCodeTurnSeq:
            if collectionii0 == COLLECTIONCNT:
                break
        thisBtRcvStr = b""
        if not hackSkipBLE:
            try:
                thisBtRcvStr = sock.recv(BD_BUFFER_SIZE)
                # print(type(btRcvStr)) reports <type 'str'>
            except Exception as e:
                pass # normal timeout also produces exception
                #logging.error("BT sock rcv err:{}".format(e))
            if thisBtRcvStr: # End of file
                btRcvStr += thisBtRcvStr
            nowS = time.time()
            if btRcvStr:
                if len(btRcvStr) > BD_BUFFER_SIZE or nowS - LastRcvDatDumpTimeS > MAXDURATION2SHOW_RXBUF_S:
                    logging.debug("BT sock rcv sth :{}".format(btRcvStr))
                    btRcvStr = b""
                    LastRcvDatDumpTimeS = nowS
                else:
                    if "\n" in btRcvStr:
                        lines = btRcvStr.split("\n")
                        btRcvStr = lines[-1]
                        lines = lines[0:-1]
                        logging.debug("BT sock rcv list of sentence(s):{}".format(lines))
                        LastRcvDatDumpTimeS = nowS

def main_loop():
    global collectionii0, COLLECTIONCNT
    quit = False
    bc = None
    while not quit:
        mydevices = discover_devices(BD_NAME_PREFIX)
        if len(mydevices) == 0:
            logging.info("No cambot bt found")
            time.sleep(5)
            continue
        logging.info("Found my cambot: {}".format(mydevices))
        bc = bluetoothutils.BluetoothControl()
        if not bc.open():
            time.sleep(5)
            continue
        logging.info("Bluetoothctl opened")
        cands = {}
        ntries = 0
        while ntries < 5:
            time.sleep(1) # Wait for bluetoothctl ready
            itsdevices = discover_devices_btctl(bc, BD_NAME_PREFIX)
            if len(itsdevices)==0:
                ntries += 1
                time.sleep(2)
                continue
            logging.info("Found its cambot: {}".format(itsdevices))
            for k, v in itsdevices.items():
                if k in mydevices:
                    cands[k] = v
            if len(cands) > 0:
                break
        if len(cands)==0:
            bc.close()
            logging.info("No cambot pairable")
            time.sleep(5)
            continue
        cands = pair_devices_btctl(bc, cands)
        bc.close()
        btsock = None
        for bdaddr, bdname in cands.items(): # Try connecting one by one until success
            """
            First try already paired devices. If none of them is connectable, then
            try paring and connecting newly found devices. If all of them failed,
            start discover_devices() again. See pair_devices() for better understand.
            """
            try:
                btsock = bluetooth.BluetoothSocket (bluetooth.RFCOMM)
                btsock.connect((bdaddr,BD_PORT))
                logging.info("Connected to {} {}".format(bdaddr, bdname))
                break
            except Exception as e:
                logging.info("Failed connecting to {} {}: {}".format(bdaddr, bdname, e))
                btsock.close()
                btsock = None
        if btsock is not None:
            send_ctrl_cmd(btsock,0)
            time.sleep(0.5)
            if sendBLDCresetCmd_and_exit:
                break
            LastRcvDatDumpTimeS = time.time()
            handle_socket_continuously(btsock)
            btsock.close()
        if collectionii0 >= COLLECTIONCNT-1:
            break
        time.sleep(2)

# /////////////////////////////////////////////////////////////////////////////
bModeSet = False
# HardCodeTurnSeq
# bldcCtrlCmdFromPC
etMode = '' # True/False: choose to run hard coded BLDC rotation sequence or take rotation angle command from MQTT msg
try:
    opts, args = getopt.getopt(sys.argv[1:], "h:p:l:r:m", ["host=", "port=", "log=", "reset", "mode"])
except getopt.GetoptError as e:
    logging.error("Error: {}".format(str(e)))
    sys.exit(-1)
for opt, optval in opts:
    if opt in ["-h", "--host"]:
        MQTT_HOST = optval
    elif opt in ["-p", "--port"]:
        MQTT_PORT = int(optval)
    elif opt in ["-l", "--log"]:
        COLLECTIONCNT = int(optval)
    elif opt in ["-r", "--reset"]:
        sendBLDCresetCmd_and_exit = True
    elif opt in ["-m", "--mode"]:
        bModeSet = True
        etMode = str(optval)
    else:
        assert False, "Option {} not known".format(opt)

if False:
    print('hacked to force collectLUT_then_CmdFromPC1')
    etMode = CONSTaoa.collectLUT_then_CmdFromPC1
else:
    if not bModeSet:
        print('choose from (H)HardCodeTurnSeq/(B)bldcCtrlCmdFromPC/(R)collectLUT_then_CmdFromPC')
        if 3 == pythonVer:
            stringinput = str(input())
        else:
            stringinput = raw_input()
        if stringinput == 'H' or stringinput == 'h' or stringinput == 'HardCodeTurnSeq':
            etMode = CONSTaoa.HardCodeTurnSeq
        elif stringinput == 'B' or stringinput == 'b' or stringinput == 'bldcCtrlCmdFromPC':
            etMode = CONSTaoa.bldcCtrlCmdFromPC
        elif stringinput == 'R' or stringinput == 'r' or stringinput == 'collectLUT_then_CmdFromPC':
            etMode = CONSTaoa.collectLUT_then_CmdFromPC1
        else:
            sys.exit()
####################################################
# begining of user cfg
####################################################
if etMode == CONSTaoa.HardCodeTurnSeq:
    StepDeg = 10
    beginDeg = 0
    EndDeg = 180
    BLDCctrlCmdMaxStep = 20 #
    COLLECTION_EACH_LOCATION = 1 # at each angular location, we may want to collect multiple dataset, in case some files are corrupted.
    repeatedDataCollect = 20 # the same data collection pattern within a cycle can be collected for multiple times
elif etMode == CONSTaoa.collectLUT_then_CmdFromPC1:
    StepDeg = 10
    beginDeg = 0
    EndDeg = 180
    BLDCctrlCmdMaxStep = 20 #
    COLLECTION_EACH_LOCATION = 1 # at each angular location, we may want to collect multiple dataset, in case some files are corrupted.
    repeatedDataCollect = 20 # the same data collection pattern within a cycle can be collected for multiple times
elif etMode == CONSTaoa.bldcCtrlCmdFromPC:
    BLDCctrlCmdMaxStep = 20 #
else:
    print('asdfgwsrthdntydr')
    sys.exit()
hackSkipBLE = False
ctrlSigCyclesPerRotation = 14 # determined by PMDC HW configuration
wait2rcvMqttMsgBeforeProceed = True # True/False set to False if we only want to test BLDC control without running CPP AoArcvSPI, which publishes DONE msg
LogSerialData = False  # CC2640 onchip AoA estimate is reported in serial port
sendBLDCresetCmd_and_exit = False # one shot action to reset BLDC condition
PauseInitS = 0.2
PauseSendBLDCctrlCmd = 0.1 # a desired rotation contains many small steps, a time gap between each new movement
PhotoJpgStorageFolder = '/home/pi/code/remote_dbg/AoArcvSPI' # I prefer the same folder as dat file
####################################################
# end of user cfg
####################################################

if not wait2rcvMqttMsgBeforeProceed:
    unconditionalWaitSec = 0

if LogSerialData:
    port = '/dev/ttyACM0'
    serialPort = serial.Serial(port, baudrate=115200, timeout=0.02)
    LOG_FILE = "aoa_data.txt"
else:
    LOG_FILE = None

if etMode == CONSTaoa.HardCodeTurnSeq or etMode == CONSTaoa.collectLUT_then_CmdFromPC1:
    PosCtrlHwCmd=[]
    for ii in range(0,repeatedDataCollect):
        for degVal in range(360*ii+beginDeg,360*ii+EndDeg+1,StepDeg):
            PosCtrlHwCmd.append(degVal*ctrlSigCyclesPerRotation)
    PosCtrlHwCmd.append((ii+1)*360*ctrlSigCyclesPerRotation) # no data collection at this last position, it is for base alignment purpose only
    print(PosCtrlHwCmd)
    logging.info("total {} steps".format(len(PosCtrlHwCmd)))
    # total desired collection, 1 rotation needs 14 cycle of sine ctrl.
    COLLECTIONCNT = len(PosCtrlHwCmd)
    assert(COLLECTIONCNT < 1e5)
elif etMode == CONSTaoa.bldcCtrlCmdFromPC:
    # do nothing
    pass
else:
    print('asdfgwsrthdntydr')
    sys.exit()

camdim = (640, 480)

currBLDCctrlcmd = 0

collectionii0 = 0
BD_NAME_PREFIX = "cambot-"
BD_PINCODE = "1977"
BD_PORT = 1
BD_TIMEOUT = 0.01 # In seconds
BD_BUFFER_SIZE = 128

MQTT_HOST = mqttutils.MQTT_HOST_IP
MQTT_PORT = mqttutils.MQTT_PORT_NUMBER
MQTT_NAME = "BTService"

btRcvStr = b""
# max duration in sec before we output anything we have in Rx buffer
# even before the line end "\n" is received
MAXDURATION2SHOW_RXBUF_S = 20
LastRcvDatDumpTimeS = time.time()

mqueue = Queue()
mqproxy = mqttutils.MQTTClientProxy(MQTT_HOST, MQTT_PORT, MQTT_NAME, mqueue)
mqproxy.connect()
mqproxy.runAsThread()
mqproxy.subscribe(mqttutils.TOPIC_EmbdEvt)
mqproxy.subscribe(mqttutils.TOPIC_PcCmd)
tl = transportLayer.TL()
logfp = None
if TakePhoto:
    camera = PiCamera()
    camera.start_preview()

if LOG_FILE is not None:
    logfp = open(LOG_FILE, "w")
    logging.info("Opened log file {}".format(LOG_FILE))
mState = CONSTaoa.State_uninitialized
try:
    if hackSkipBLE:
        handle_socket_continuously("")
    else:
        main_loop()
except BaseException as e:
    mqproxy.quit()
    raise
mqproxy.quit()
if TakePhoto:
    camera.stop_preview()

if LOG_FILE is not None:
    logfp.close()

logging.info("Good bye.")


from datetime import datetime, timedelta, timezone

# X-RAY ------
from aws_xray_sdk.core import xray_recorder

from lib.db import db

class UserActivities:
  def run(user_handle):
    #try:
      # X-RAY ------
      # Start a segment
      #segment = xray_recorder.begin_segment('user_activities')

    print("UserActivities.run")
    model = {
      'errors': None,
      'data': None
    }

    if user_handle == None or len(user_handle) < 1:
      model['errors'] = ['blank_user_handle']
    else:
      print("else")
      sql = db.template('users','show')
      results = db.query_object_json(sql,{'handle':user_handle})
      print("results: ", results)
      model['data'] = results

      # X-RAY ------
      # Start a subsegment
      #subsegment = xray_recorder.begin_subsegment('mock-data')
      #dict = {
      #  "now": now.isoformat(),
      #  "results-size": len(model['data'])
      #}
      #segment.put_metadata('key', dict, 'namespace')
    #finally:
    return model


from typing import List

from constants import OUT_OF_VOCABULARY_DELIM, LEFT_PAD_SYMBOL, RIGHT_PAD_SYMBOL

def tokenize(tweet: str, span: int, extended_func: bool = False,
             left_pad_symbol: str = LEFT_PAD_SYMBOL, right_pad_symbol: str = RIGHT_PAD_SYMBOL) -> List[str]:
    """
    Tokenize tweets given a length for the tokens.

    :param left_pad_symbol: Padding symbol for the tweet start
    :param right_pad_symbol: Padding symbol for the tweet end
    :param span: Execution range. Ex: Unigram = 1 character; Bigram = 2 characters; Trigram = 3 characters
    :param tweet: Tweet input string
    :param extended_func: If True, apply extended functionality for BYOM
    :return: List of tokens
    """
    if extended_func:
        tweet = left_pad_symbol * (span - 1) + tweet + right_pad_symbol * (span - 1)
    tweet_len = len(tweet)
    return [tweet[c:c + span] for c in range(tweet_len) if (c + span) <= tweet_len and
            OUT_OF_VOCABULARY_DELIM not in tweet[c:c + span]]


import math_tool
import json
from handle_cd_method import trim_comma_in_paras, trim_method

def cal_single_rel(bug_report_vector, augmented_method_vector):
    return math_tool.cosine_similarity(bug_report_vector, augmented_method_vector)

def load_augmentation_methods(path):
    with open(path, 'r') as f:
        dic = json.loads(f.read())
    return dic

def cal_rel(brv, dic):
    result_dic = dict()
    for key in dic:
        rel_value = cal_single_rel(brv, dic[key])
        result_dic[key] = rel_value

    ranked_dic = _rank_dic(result_dic)
    return ranked_dic

def _rank_dic(dic):
    sorted_dic = sorted(dic.items(), key = lambda x: x[1], reverse = True)
    return sorted_dic

# read "link_buggyMethods/xxx" file and return a dictionary
def load_link_dic(path):
    dic = {}
    f = open(path, 'r')
    lines = f.readlines()
    for line in lines:
        line = line.strip()
        if len(line) > 0:
            parts = line.split('\t')
            br_id = parts[0]
            pm_lists_str = parts[1].rstrip()
            pm_lists = pm_lists_str.split('外')
            for pm_list in pm_lists:
                pm_parts = pm_list.split('内')
                if len(pm_parts) < 2:
                    print(pm_parts, "in this sentence can not find PATH / METHOD_NAME !")
                    continue
                path = '/' + '/'.join((pm_parts[0].split('/'))[2:])
                method = pm_parts[1]

                path_method = path + '#' + trim_method(trim_comma_in_paras(method))

                if br_id not in dic:
                    dic.setdefault(br_id, []).append(path_method)
                else:
                    dic[br_id].append(path_method)
    return dic

# for item in ['Closure']: #'Time', 'Mockito', 'Lang', 'Math'
#     dic = load_link_dic("/Users/lienming/Downloads/final_defects4j/linked-bugMethods/" + item + "_bugId_buggyMethodsName")
# for ms in dic['Time_3']:
#     print(ms)

# print(load_link_dic("/Users/lienming/Downloads/final_defects4j/linked-bugMethods/Time_bugId_buggyMethodsName")['Time_25'])


# -*- coding: utf-8 -*-
"""
Created on Fri Jun  3 19:17:33 2016

@author: xf05id1
"""

from epics import PV
import time

ttl1 = PV('XF:05IDD-ES:1{EVR:1-Out:FP1}Src:Scale-SP')
BT1p = PV('XF:05IDD-VA:1{BT:1-CCG:1}P-I')

def movefs():
#    ttl1.put(4)
#    time.sleep(2)
#    ttl1.put(3)
     shut_fast.high_cmd()
     time.sleep(2)
     shut_fast.low_cmd()

open_wait = 1 #min
close_wait = 5 #min
setp = 5E-6

num_cycle = 24*12*3

for i in range(num_cycle+1):
    print('open fast shutter')
    movefs()
    while(BT1p.get() < setp):
        print('pressure: '+str(BT1p.get()))
        print('wait '+str(open_wait)+' min')
        time.sleep(open_wait*60)
    print('pressure: '+str(BT1p.get()))
    print('close fast shutter')
    movefs()
    print('wait '+str(close_wait)+' min')
    time.sleep(close_wait*60)


#imports
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import equationGenerator
import seaborn as sns

#defining getHeatMap
def getHeatMap(turn):
    states_dataframe = equationGenerator.genLambdas()
    row_value = turn / 0.01
    state_values = states_dataframe.to_numpy()[int(row_value)]
    state_values = np.resize(state_values, (10, 10))

    for x in [1, 3, 5, 7, 9]:
        state_values[x] = state_values[x][::-1]

    state_values = np.flip(state_values, 0)
    heat_map = sns.heatmap(state_values)

getHeatMap(1)
getHeatMap(5)
getHeatMap(10)
getHeatMap(15)
getHeatMap(20)
getHeatMap(25)
getHeatMap(30)
getHeatMap(35)
getHeatMap(40)
getHeatMap(45)
getHeatMap(50)
getHeatMap(55)
getHeatMap(60)
getHeatMap(65)
getHeatMap(70)
getHeatMap(75)
getHeatMap(80)
getHeatMap(85)
getHeatMap(90)
getHeatMap(95)
getHeatMap(100)


##############################################################
# ExtraTimeToGoal class
# Description:
#   This class is responsible for:
#       ExtraTimeToGoal - excess time taken to get to goal travelling at agent's average speed
#       PathEfficiency - ratio of straight line path to agent's path

import pandas as pd
import numpy as np
import xml.etree.ElementTree as ET
import xmltodict
from EvaluationMetric import EvaluationMetric
from DataLoader import *
import os

class ExtraTimeToGoal(EvaluationMetric):
    def __init__(self, data_format):
        super().__init__(data_format)
        self.nominal_speed = .5
        self.extra_time_to_goal_dict = dict()
        self.path_efficiency_dict = dict()

        #added time efficiency
        self.time_efficiency_dict = dict()

        self.time_step_done = False

    def evaluate(self, agents, curr_agents, time_stamp):
        return

    def set_extra_time_to_goal(self, agent_id):
        agent = self.agents[agent_id]
        agent_path_time = agent.get_latest_trajectory_time()         #actual time
        straight_path_time = agent.get_latest_straight_path_time()  #nominal time  #caculate (goal - start) / nominal speed
        extra_time = agent_path_time - straight_path_time
        self.extra_time_to_goal_dict[agent_id] = extra_time

        ideal_straight_path_time = agent.get_latest_straight_path_time(1) #use(1) nominal time if it follows 1 m/s speed setting (especially in population)

        self.time_efficiency_dict[agent_id] = ideal_straight_path_time /  (agent_path_time+0.0000001) #latest nominal time / latest actual time

    def set_path_efficiency(self, agent_id):
        path_efficiency = self.agents[agent_id].get_path_efficiency()
        #don't want to consider case where agent doesn't move
        if path_efficiency > 0:
            self.path_efficiency_dict[agent_id] = path_efficiency

    def update_agents(self, agents):
        self.agents = agents

    #this evaluation metric has extra computation to perform at the end of the episode
    def write_out(self, agents):
        self.update_agents(agents)
        for agent_id in self.agents:
            self.set_extra_time_to_goal(agent_id)
            self.set_path_efficiency(agent_id)
        extra_time_to_goal_list = np.asarray(list(self.extra_time_to_goal_dict.values()))
        path_efficiency_list = np.asarray(list(self.path_efficiency_dict.values()))

        time_efficiency_list = np.asarray(list(self.time_efficiency_dict.values()))

        self.average_extra_time_to_goal = np.mean(extra_time_to_goal_list)
        self.std_dev_extra_time_to_goal = np.std(extra_time_to_goal_list)
        self.min_extra_time_to_goal = np.min(extra_time_to_goal_list)
        self.max_extra_time_to_goal = np.max(extra_time_to_goal_list)

        self.average_path_efficiency = np.mean(path_efficiency_list)
        self.std_dev_path_efficiency = np.std(path_efficiency_list)
        self.min_path_efficiency = np.min(path_efficiency_list)
        self.max_path_efficiency = np.max(path_efficiency_list)

        self.average_time_efficiency = np.mean(time_efficiency_list)
        self.std_dev_time_efficiency = np.std(time_efficiency_list)
        self.min_time_efficiency = np.min(time_efficiency_list)
        self.max_time_efficiency = np.max(time_efficiency_list)

        return {#'Mean Extra Time to Goal (s)': self.average_extra_time_to_goal,
                #'StdDev Extra Time to Goal (s)': self.std_dev_extra_time_to_goal,
                #'Max Extra Time to Goal (s)': self.max_extra_time_to_goal,
                #'Min Extra Time to Goal (s)': self.min_extra_time_to_goal,
                #'Mean Extra Time to Goal (s)_Scatter': self.extra_time_to_goal_dict,
                'Mean Path Efficiency': self.average_path_efficiency,
                'StdDev Path Efficiency': self.std_dev_path_efficiency,
                'Max Path Efficiency': self.max_path_efficiency,
                'Min Path Efficiency': self.min_path_efficiency} #,
                #'Mean Path Efficiency_Scatter': self.path_efficiency_dict,

                #'Mean Time Efficiency': self.average_time_efficiency,
                #'StdDev Time Efficiency': self.std_dev_time_efficiency,
                #'Max Time Efficiency': self.max_time_efficiency,
                #'Min Time Efficiency': self.min_time_efficiency}
                #'Mean Time Efficiency_Scatter': self.time_efficiency_dict}#added


# -*- coding: utf-8 -*-
import scrapy
from zero.items import FoundationBriefItem

class FoundationspiderSpider(scrapy.Spider):
    name = 'foundationSpider'
    allowed_domains = ['www.cninfo.com.cn']
    start_urls = ['http://www.cninfo.com.cn/cninfo-new/index']

    def parse_foundation_brief(self,response):
        #基金代码、基金名称所在的xpath
        foundation_code_and_name_xpath="//*[@id='cninfoform']/table/tr/td[1]/text()"
        found_code=response.xpath(foundation_code_and_name_xpath)[0].extract().strip()
        found_name=response.xpath(foundation_code_and_name_xpath)[1].extract().strip()
        #基金管理公司(基础管理人)
        foundation_manager_company_xpath="/html/body/div[2]/div[1]/div[2]/table/tr[1]/td[2]/text()"
        found_manager_company=response.xpath(foundation_manager_company_xpath).extract_first().strip()
        #基金类型
        foundation_type_xpath="/html/body/div[2]/div[1]/div[2]/table/tr[5]/td[2]/text()"
        found_type=response.xpath(foundation_type_xpath).extract_first().strip()
        #基金成立日期
        foundation_birth_xpath="/html/body/div[2]/div[1]/div[2]/table/tr[6]/td[2]/text()"
        found_birth=response.xpath(foundation_birth_xpath).extract_first().strip()
        #交易所
        foundation_exchange_xpath="/html/body/div[2]/div[1]/div[2]/table/tr[8]/td[2]/text()"
        found_exchange=response.xpath(foundation_exchange_xpath).extract_first().strip()

        #创建Item
        found_item                         =FoundationBriefItem()
        found_item['found_code']           =found_code
        found_item['found_name']           =found_name
        found_item['found_manager_user']   =''
        found_item['found_manager_company']=found_manager_company
        found_item['found_type']           =found_type
        found_item['found_birth']          =found_birth
        found_item['found_exchange']       =found_exchange

        yield found_item

    def parse_foundation_list(self,response):
        """
        得到单个基金所在页面的url,并返回一个与这个url对应的Request对象
        """
        #深交所上市基金信息
        shen_market_foundation_list=response.xpath("//*[@id='con-a-1']/ul/li")
        foundation_xpath="//*[@id='con-a-1']/ul/li[{0}]/a/@href"
        doundation_brief_url="http://www.cninfo.com.cn/information/fund/brief/{0}.html"
        for index in range(1,len(shen_market_foundation_list)+1):
            foundation_page_url=response.xpath(foundation_xpath.format(index)).extract_first()
            foundation_code    =foundation_page_url.split('?')[2]
            yield scrapy.http.Request(url=doundation_brief_url.format(foundation_code),callback=self.parse_foundation_brief)

        #上交所上市基金信息
        shang_market_foundation_list=response.xpath("//*[@id='con-a-2']/ul/li")
        foundation_xpath="//*[@id='con-a-2']/ul/li[{0}]/a/@href"
        doundation_brief_url="http://www.cninfo.com.cn/information/fund/brief/{0}.html"
        for index in range(1,len(shang_market_foundation_list)+1):
            foundation_page_url=response.xpath(foundation_xpath.format(index)).extract_first()
            foundation_code    =foundation_page_url.split('?')[2]
            yield scrapy.http.Request(url=doundation_brief_url.format(foundation_code),callback=self.parse_foundation_brief)

        #其它
        other_market_foundation_list=response.xpath("//*[@id='con-a-3']/ul/li")
        foundation_xpath="//*[@id='con-a-3']/ul/li[{0}]/a/@href"
        doundation_brief_url="http://www.cninfo.com.cn/information/fund/brief/{0}.html"
        for index in range(1,len(shang_market_foundation_list)+1):
            foundation_page_url=response.xpath(foundation_xpath.format(index)).extract_first()
            foundation_code    =foundation_page_url.split('?')[2]
            yield scrapy.http.Request(url=doundation_brief_url.format(foundation_code),callback=self.parse_foundation_brief)

    def parse(self, response):
        """
        得到一个基金列表页面的url，并返回一个与这个url对应的Request对象
        """
        foundation_list_url=response.xpath("/html/body/div[1]/div/div[4]/li[2]/div/ul[1]/dd[2]/a/@href").extract_first()
        foundation_list_url=response.urljoin(foundation_list_url)
        yield scrapy.http.Request(url=foundation_list_url,callback=self.parse_foundation_list)


#
# File: models.py
#
import torch

from ceem.nn import LNMLP

DEFAULT_NN_KWARGS = lambda H: dict(input_size=10*H,
                    hidden_sizes=[32]*8,
                    output_size = 6,
                    activation='tanh', gain=1.0, ln=False)

class LagModel(torch.nn.Module):
    def __init__(self, neural_net_kwargs, H):
        super().__init__()
        self.net = LNMLP(**neural_net_kwargs)

    def forward(self, data_batch):
        # data batch is size ntrajs * H * ndim.
        # Controls are the first 4 dimensions of ndim, then 3 velocities, then 3 rotation rates
        ntrajs = data_batch.shape[0]
        state_t = data_batch[:, -1:, 4:]
        pred = self.net(data_batch.view(ntrajs, -1))
        return pred


import rospy
import queue

from drone_control_system.msg import RunLiability, FinalizeLiability
from . import traking_thread
from threading import Timer

class DroneManager:
    def __init__(self):
        rospy.init_node('drone_manager_node')

        self.available_drones = queue.Queue()
        for x in range(3):
            self.available_drones.put(x + 1)

        rospy.Subscriber('run_liability', RunLiability, self.run_liability)
        self.result_queue = queue.Queue()

        self.finalize_pub = rospy.Publisher('finalize_liability', FinalizeLiability, queue_size=10)

        rospy.loginfo('Node is loaded')

    def run_liability(self, msg):
        if not self.available_drones.empty():
            drone = self.available_drones.get()
            t = traking_thread.TrakingThread(self.result_queue, drone, msg.liability, msg.filename)
            t.run()

    def spin(self):
        def finalize_liabilities():
            if not self.result_queue.empty():
                drone, liability = self.result_queue.get()
                msg = FinalizeLiability()
                msg.liability = liability
                msg.status = "Ok"
                self.finalize_pub.publish(msg)
                self.available_drones.put(drone)
            Timer(1, finalize_liabilities).start()

        finalize_liabilities()
        rospy.spin()


commands: dict = {}

def register_command(command, callback, access="*", help_text=""):
    if command.lower() in commands:
        raise Exception(f"The command {command} is already registered")
    commands[command.lower()] = {
        "callback": callback,
        "access": access,
        "help": help_text
    }
    print(f"Registered command {command}")

def handle_command(command, args, message, say):
    if command.lower() not in commands:
        say("I don't recognize that command. Use `.help` or DM me with `help` for a list of commands")
        return
    commands[command.lower()]["callback"](args, message, say)


import unittest, numpy
import polygonal_cylinder_gen

# package import: specify a directory and file.
# from ifgi.ptracer import IfgiSys

class TestPolygonalCylinderGen(unittest.TestCase):
    """test: polygonal cylinder generator test."""

    def test_gen(self):
        """test generating a cylinder"""
        pc_gen = polygonal_cylinder_gen.Polygonal_cylinder_gen()

        # a simple one
        pc_gen.clear()
        pc_gen.set_n_gon(6)
        pc_gen.set_generate_segment_tris(True)
        pc_gen.append_center_point(numpy.array([0.0, 0.0,  0.0]), 2)
        pc_gen.append_center_point(numpy.array([0.0, 0.0,  5.0]), 2)
        pc_gen.append_center_point(numpy.array([0.0, 0.0, 10.0]), 2)
        pc_gen.gen_cylinder()
        pc_gen.export_obj('cylinder0.obj')
        # print pc_gen

        # different radius, 12-gon
        pc_gen.clear()
        pc_gen.set_n_gon(12)
        pc_gen.append_center_point(numpy.array([0.0, 0.0,  0.0]), 2)
        pc_gen.append_center_point(numpy.array([0.0, 0.0,  4.0]), 2)
        pc_gen.append_center_point(numpy.array([0.0, 0.0,  5.0]), 3)
        pc_gen.append_center_point(numpy.array([0.0, 0.0,  6.0]), 2)
        pc_gen.append_center_point(numpy.array([0.0, 0.0, 10.0]), 2)
        pc_gen.gen_cylinder()
        pc_gen.export_obj('cylinder1.obj')
        # print pc_gen

        # skewed center, 12-gon, without top and bottom polygons
        pc_gen.clear()
        pc_gen.set_n_gon(12)
        pc_gen.append_center_point(numpy.array([0.3, 0.0,  0.0]), 1)
        pc_gen.append_center_point(numpy.array([0.0, 0.0,  4.0]), 1)
        pc_gen.append_center_point(numpy.array([0.0, 0.0,  5.0]), 3)
        pc_gen.append_center_point(numpy.array([0.0, 0.0,  6.0]), 1)
        pc_gen.append_center_point(numpy.array([0.3, 0.0, 10.0]), 2)
        pc_gen.gen_cylinder()
        pc_gen.export_obj('cylinder2.obj')
        # print pc_gen

#
# main test
#
# if __name__ == '__main__':
#     suit0   = unittest.TestLoader().loadTestsFromTestCase(TestPolygonalCylinderGen)
#     alltest = unittest.TestSuite([suit0])
#     unittest.TextTestRunner(verbosity=2).run(alltest)


"""
Experiment 3:
Show it's capability in sequential hand written didigt classification task.

Data is from https://github.com/aiddun/binary-mnist.
"""
import numpy as np

from models import R2N2
from utility import cross_entropy_loss, d_cross_entropy_loss
from figures import show_loss_and_accuracy

from tqdm import tqdm, trange

import pickle

data_path = "data/mnist_binary.pkl"
with open(data_path, 'rb') as f:
    mnist = pickle.load(f)

x_train = mnist["training_images"]
y_train = mnist["training_labels"]
x_test = mnist["test_images"]
y_test = mnist["test_labels"]

# print(y_train[:10])
# ns = int(np.sqrt(x_train[0].shape[0]))

# ipdb.set_trace()

exp_name = "exp_3_FA"

n_x = 28
T = n_x

n_h = 64
lr = 1e-3

x_test[0].reshape(T, T)

model = R2N2(n_h, 10, n_input=28, g=2.5, tau=10, lr=lr, enableFA=True)

loss_list = []
results_list = []

def validate(x_v, y_v):
    results_list = []
    loss_list = []
    for i in range(x_v.shape[0]):
        xs = x_v[i].reshape(T, T)
        y = y_v[i]
        h_forward_seq, output_seq, loss = model.forward_seq_decision_making(
            xs, y, cross_entropy_loss)

        loss_list.append(loss)

        prediction = np.argmax(output_seq[-1])
        results_list.append(prediction == y)

    # print(
    #     f"Validate Accuracy: {np.around(np.mean(results_list), 2)} | Loss (Cross Entropy) {np.around(np.mean(loss_list), 2)}"
    # )
    return np.mean(results_list), np.mean(loss_list)

n_samples = x_train.shape[0]
n_epoch = 5

kk = trange(n_epoch, desc="Average Classification Accuracy: ", leave=True)
acc_v, loss_v = 0, 0
acc_v_list = []

for e in kk:
    acc_v, loss_v = validate(x_test, y_test)
    acc_v_list.append(acc_v)
    for i in tqdm(range(n_samples)):
        model.zero_grads()
        xs = x_train[i].reshape(T, T)
        y = y_train[i]
        h_forward_seq, output_seq, loss = model.forward_seq_decision_making(
            xs, y, cross_entropy_loss)

        model.hard_reset_w_backward()
        h_backward_seq = model.backward_learning_decision_making(
            h_forward_seq[-1], xs, y, d_cross_entropy_loss)

        model.apply_grads_Adam(T)
        loss_list.append(loss)

        prediction = np.argmax(output_seq[-1])
        results_list.append(prediction == y)
        # print(prediction, y)

        kk.set_description(
            f"Epoch {e+1} | Train: Acc: {np.around(np.mean(results_list[-1000:]), 2)} | L: {np.around(np.mean(loss_list[-1000:]), 2)}; Validate in epoch {e}: Acc {np.around(acc_v, 2)}"
        )

show_loss_and_accuracy(loss_list,
                       results_list,
                       exp_name=exp_name,
                       show=True,
                       yL_label="Crosss entropy",
                       yR_label="Accuracy")

pickle.dump({
    "loss": loss_list,
    "result": results_list
}, open(f"results/{exp_name}.pkl", "wb"))


# -*- coding: utf-8 -*-
"""
Created on Wed Oct  9 07:20:00 2019

@author: ZJ
"""

from matplotlib.pylab import date2num
import tushare as ts
from datetime import datetime
import matplotlib.pyplot as plt
import mpl_finance as mpf
import pandas as pd

ts.set_token('3d86ce37200505496b7cef7a97ee5b1cc275b5d914e81fb6a3b5634b') #设置访问令牌
pro = ts.pro_api()

today=datetime.now().date().strftime('%Y%m%d')
t=datetime.now().date()
fig = plt.figure(figsize=(12,6),dpi=300)

num = date2num(t)

# 对tushare获取到的数据转换成candlestick_ohlc()方法可读取的格式
def plot_k_bar(df):
    alist = []
    for idx,row in df.iterrows():
        date,open,high,low,close= row[1:6]
        # 将日期转换为数字
        n= -idx+ num
        data = (n,open,high,low,close)
        alist.append(data)

    ax = fig.gca()
    ax.xaxis_date()
    plt.xticks(rotation=45) #日期显示的旋转角度
    plt.title(df["ts_code"][0])
    plt.xlabel("Date")
    plt.ylabel("K-Bar")
    mpf.candlestick_ohlc(ax,alist,width=0.7,colorup='red',colordown='green')
    fig.savefig("stockimg\\"+df["ts_code"][0]+".png", format = 'png')
    plt.clf()

##stocklists = pro.query('stock_basic', exchange='', list_status='L', fields='ts_code,name,list_date')

##for i in range(len(stocklists)):

df = ts.pro_bar(ts_code="601010.SH", asset='E', adj='qfq', start_date="20140829", end_date="20141219",freq ="5min" )
plot_k_bar(df)


import argparse
import json

from lm_eval.evaluator import make_table
from lm_eval.tasks import ALL_TASKS, TASK_REGISTRY
from lm_eval_harness.lm_eval_evaluator import evaluate_wrapper
from lm_eval_harness.lm_eval_hf_model import HFEvalModel
from lm_eval_harness.tasks.human_eval import HumanEval
from lm_eval_harness.utils.regex import MultiChoice, pattern_match
from transformers import AutoModelForCausalLM, AutoTokenizer

# Ensures additional tasks are loaded and registered
ALL_TASKS.append("human_eval")
TASK_REGISTRY.update({"human_eval": HumanEval})

def parse_args():
    parser = argparse.ArgumentParser(description="Evaluates pre-trained models using `lm-eval`.")

    parser.add_argument("pre_trained_model_path", type=str, help="Path to the pre-trained model file.")

    parser.add_argument(
        "hub_tokenizer_path",
        type=str,
        help="Name or path to the Hugging Face hub's tokenizer.",
    )

    parser.add_argument(
        "-t",
        "--tasks",
        choices=MultiChoice(ALL_TASKS),
        type=str,
        default=None,
        help="Tasks to be evaluated (separated by comma), e.g., `wsc,cb,copa`.",
    )

    parser.add_argument(
        "-o",
        "--output_path",
        type=str,
        default=None,
        help="Path to the saved outputs.",
    )

    parser.add_argument(
        "-ns",
        "--n_few_shot_samples",
        type=int,
        default=0,
        help="Number of few-shot samples.",
    )

    parser.add_argument(
        "-ls",
        "--limit_samples",
        type=int,
        default=None,
        help="Limit the number of samples.",
    )

    parser.add_argument(
        "-nc",
        "--no_cache",
        action="store_true",
        help="Whether to not store predictions in a cache database.",
    )

    parser.add_argument(
        "-dnp",
        "--decontamination_ngrams_path",
        type=str,
        default=None,
        help="Path to the de-contamination n-grams file.",
    )

    parser.add_argument(
        "-ddp",
        "--description_dict_path",
        type=str,
        default=None,
        help="Path to the description dictionary file.",
    )

    parser.add_argument(
        "-ci",
        "--check_integrity",
        action="store_true",
        help="Whether to check integrity of tasks.",
    )

    return parser.parse_args()

if __name__ == "__main__":
    args = parse_args()

    if args.limit_samples:
        print("Warning: --limit_samples should only be used for testing.")

    task_names = ALL_TASKS if args.tasks is None else pattern_match(args.tasks.split(","), ALL_TASKS)
    print(f"Selected Tasks: {task_names}")

    description_dict = {}
    if args.description_dict_path:
        with open(args.description_dict_path, "r") as f:
            description_dict = json.load(f)

    model = AutoModelForCausalLM.from_pretrained(args.pre_trained_model_path)
    tokenizer = AutoTokenizer.from_pretrained(args.hub_tokenizer_path)
    hf_model = HFEvalModel(model, tokenizer)

    outputs = evaluate_wrapper(
        hf_model,
        task_names,
        num_fewshot=args.n_few_shot_samples,
        no_cache=args.no_cache,
        limit=args.limit_samples,
        description_dict=description_dict,
        check_integrity=args.check_integrity,
        decontamination_ngrams_path=args.decontamination_ngrams_path,
    )

    output_json = json.dumps(outputs, indent=2)
    if args.output_path:
        with open(args.output_path, "w") as f:
            f.write(output_json)

    print(make_table(outputs))


# -*- coding: utf-8 -*-
"""
step02_csv_LinearRegression.py

csv file(DataFrame)+LinearRegress
"""

import pandas as pd #csv file read
from sklearn.linear_model import LinearRegression #model 생성
from sklearn.metrics import mean_squared_error,r2_score # model 평가
from sklearn.model_selection import train_test_split #dataset split

#1. dataset loading
path='C:/ITWILL/4_python-ll/data/'
iris=pd.read_csv(path+'iris.csv')
iris.info()

#2. 변수 선택*** step 01 과의 차이점
'''
x =1,2,4
y=3
'''
cols=list(iris.columns)
cols #['Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width', 'Species']

y_col=cols.pop(2)#Petal.Length # 해당 객체를 꺼내고 객체 에서는 지운다.
cols #['Sepal.Length', 'Sepal.Width', 'Petal.Width', 'Species']

x_cols=cols[:3]
# ['Sepal.Length', 'Sepal.Width', 'Petal.Width']

X=iris[x_cols] #중첩 list
y=iris[y_col] # 단일 list

X.shape #(150, 3)
y.shape #(150,)

#3. train/test split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=123)

#4. model  생성
model = LinearRegression().fit(X_train,y_train)
X_train.head()

model.coef_ #array([ 0.75572271, -0.6715641 ,  1.43222782])
model.intercept_ #-0.3150495624708056

#114           5.8          2.8          2.4
x1, x2, x3=5.8,2.8,2.4
a1,a2,a3=0.75572271, -0.6715641 ,  1.43222782
b=-0.3150495624708056
#회귀반정식
y_fit= x1*a1+x2*a2+x3*a3+b
print('y fit=',y_fit)
#y fit= 5.625109443529194 <- 예측치

#정답
y=y_train[:1]
y#5.1

#5. model 평가: TEST set->validation set: 검증용
y_pred = model.predict(X=X_test)
y_true=y_test

#1)MSE
mse=mean_squared_error(y_true,y_pred)
print(mse) #0.12397491396059157

#2) r2 score
score=r2_score(y_true,y_pred)
print(score) #0.9643540833169766

#과적밯 유무 확인
train_score=model.score(X=X_train,y=y_train)
test_score=model.score(X=X_test, y=y_test)

train_score #0.9694825528782403
test_score  #0.9643540833169766

# 6.model 적용: new dataset(업무용)
import numpy as np
#임의로 new data x 값을 만든다
idx = np.random.choice(a=len(iris),size=int(len(iris)*0.5),
                 replace=False)

new_data=iris.iloc[idx]
new_data.shape # (75, 5)

new_X = new_data[x_cols]
new_y=new_data[y_col]

new_X.shape #(75, 3)
new_y.shape #(75,) <- 정답

#에측치
new_y_pred=model.predict(X= new_X)

#평가
score=r2_score(new_y,new_y_pred)
print(score) #0.9722221044415694


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import r2_score
from sklearn.pipeline import Pipeline
from sklearn.kernel_ridge import KernelRidge
import pickle

df = pd.read_csv('V_Resistencias.csv')
x = np.asanyarray(df[['Voltaje']])
y = np.asanyarray(df[['Resistencia']])
x_train, x_test, y_train, y_test = train_test_split(x,y)

model = Pipeline([('scaler',StandardScaler()),
                  ('reg',KernelRidge(alpha=0.0001, kernel='rbf'))])
model.fit(x,y)

pickle.dump(model,open('V_Resistencias.sav','wb'))

yp = model.predict(x)

plt.figure()
plt.grid()
plt.title('Regresion no lineal')
plt.xlabel('Voltaje')
plt.ylabel('Resistencia')

plt.plot(x,y,'mo')
plt.plot(x_test,y_test,'go')
plt.plot(x,yp,'-',lw=2)

plt.legend(['Muestras','Generalizacion', 'Predicción'])
plt.show()

e3 = r2_score(y,yp)
print('R^2 =',e3)


def solution(id_list, report, k):
    answer = []
    user_report={}
    user_num={}

    report = set(report)
    for i in id_list:
        user_num[i]=0
        user_report[i]=[]

    for i in report:
        split_report=i.split()
        user_report[split_report[0]].append(split_report[1])
        user_num[split_report[1]] += 1

    for i in id_list:
        mail_num=0

        report_list=user_report[i]
        for j in report_list:
            if user_num[j]>=k:
                mail_num+=1
        answer.append(mail_num)

    return answer

solution(["muzi", "frodo", "apeach", "neo"],["muzi frodo","apeach frodo","frodo neo","muzi neo","apeach muzi"],2)

코딩테스트_파이썬/카카오 블라인드/pr1.py


DEPS = [
    "flutter/display_util",
    "fuchsia/status_check",
    "recipe_engine/file",
    "recipe_engine/path",
    "recipe_engine/swarming",
]

def RunSteps(api):
  request = (
      api.swarming.task_request().with_name("flutter_fuchsia_unittests"
                                           ).with_priority(100)
  )
  request = request.with_slice(
      0,
      request[0].with_command(
          ["./run_tests.sh", "image_name", "packages_name"]
      ).with_dimensions(
          pool="luci.flutter.tests"
      ).with_isolated("isolated_hash").with_expiration_secs(3600)
      .with_io_timeout_secs(3600).with_execution_timeout_secs(3600)
      .with_idempotent(True).with_containment_type("AUTO"),
  )
  # Trigger the task request.
  metadata = api.swarming.trigger("Trigger Tests", requests=[request])
  links = {m.id: m.task_ui_link for m in metadata}
  # Collect the result of the task by metadata.
  fuchsia_output = api.path["cleanup"].join("fuchsia_test_output")
  api.file.ensure_directory("swarming output", fuchsia_output)
  results = api.swarming.collect(
      "collect", metadata, output_dir=fuchsia_output, timeout="30m"
  )
  api.display_util.display_tasks(
      step_name="display tasks",
      results=results,
      metadata=metadata,
      raise_on_failure=True,
  )

def GenTests(api):
  yield (
      api.status_check.test("Test_Infra_Failure", status="infra_failure") +
      api.step_data(
          "Trigger Tests", api.swarming.trigger(["task1", "task2"],
                                                initial_id=0)
      ) + api.step_data(
          "collect",
          api.swarming.collect([
              api.swarming.task_result(
                  id="0", name="task1", state=api.swarming.TaskState.KILLED
              ),
              api.swarming.task_result(id="1", name="task2", failure=True),
          ]),
      )
  )
  yield (
      api.status_check.test("Test_Failure", status="failure") + api.step_data(
          "Trigger Tests", api.swarming.trigger(["task1", "task2"],
                                                initial_id=0)
      ) + api.step_data(
          "collect",
          api.swarming.collect([
              api.swarming.task_result(id="0", name="task1"),
              api.swarming.task_result(id="1", name="task2", failure=True),
          ]),
      )
  )
  yield (
      api.status_check.test("Test_Success", status="success") + api.step_data(
          "Trigger Tests", api.swarming.trigger(["task1"], initial_id=0)
      ) + api.step_data(
          "collect",
          api.swarming.collect([
              api.swarming.task_result(id="0", name="task1"),
          ]),
      )
  )


"""
utils
...
"""
import tda
import httpx
import datetime
import atexit
import json
from enum import Enum

WEEKDAYS = {
    0: 'monday',
    1: 'tuesday',
    2: 'wednesday',
    3: 'thursday',
    4: 'friday',
    5: 'saturday',
    6: 'sunday'
}

def _get_secrets(secrets_location: str):
    """
    don't commit this stuff, ofcourse
    """
    with open(secrets_location) as handle:
        secrets_dict = json.load(handle)
    api_key = secrets_dict['API_KEY']
    redirect_uri = secrets_dict['REDIRECT_URI']
    return api_key, redirect_uri

API_KEY, REDIRECT_URI = _get_secrets("./secret_stuff.json")
TOKEN_PATH = 'ameritrade-credentials.json'
YOUR_BIRTHDAY = datetime.datetime(year=1900, month=6, day=1)
SP500_URL = "https://tda-api.readthedocs.io/en/latest/_static/sp500.txt"

def _make_webdriver():
    # Import selenium here because it's slow to import
    from selenium import webdriver

    driver = webdriver.Chrome()
    atexit.register(lambda: driver.quit())
    return driver

# Create a new client
# will designate as global/for export for now, assuming it's going to be used everywhere
CLIENT = tda.auth.easy_client(
    API_KEY,
    REDIRECT_URI,
    TOKEN_PATH,
    _make_webdriver
)

def get_fundamentals(ticker: str):
    """
    """
    r = CLIENT.search_instruments(ticker, tda.client.Client.Instrument.Projection.FUNDAMENTAL)
    assert r.status_code == httpx.codes.OK, r.raise_for_status()
    for _, f in r.json().items():
        return f['fundamental']
        # fundamentals = f['fundamental']
        # for k, v in fundamentals.items():
            # print(f'\t{k}: {v}')

# get enums for convenience
PRICE_HISTORY = tda.client.Client.PriceHistory
FREQUENCY = PRICE_HISTORY.Frequency
FREQUENCY_TYPE = PRICE_HISTORY.FrequencyType
PERIOD = PRICE_HISTORY.Period
PERIOD_TYPE = PRICE_HISTORY.PeriodType

def get_price_history(
        ticker: str,
        period_type: PERIOD_TYPE,
        period: PERIOD,
        frequency_type: FREQUENCY_TYPE,
        frequency: FREQUENCY
    ):
    """
    format of r.json()
        {
        "candles": [
            {
            "close": 0,
            "datetime": 0,
            "high": 0,
            "low": 0,
            "open": 0,
            "volume": 0
            }
        ],
        "empty": false,
        "symbol": "string"
        }
    """
    if not is_valid_history(period_type, period, frequency_type, frequency):
        raise ValueError("Period and frequency combinations do not form a valid request")
    r: httpx.Response = CLIENT.get_price_history(
        ticker,
        period_type=period_type,
        period=period,
        frequency_type=frequency_type,
        frequency=frequency,
        need_extended_hours_data=False
        )
    assert r.status_code == httpx.codes.OK, r.raise_for_status()
    dict_= r.json()
    empty = dict_['empty']
    assert(not empty)
    candles = dict_['candles']
    return candles

def is_valid_history(period_type: PERIOD_TYPE, period: PERIOD, frequency_type: FREQUENCY_TYPE, frequency: FREQUENCY):
    """
    Ensure the period/frequency combinations will form a valid request
    See Query Parameters:
        https://developer.tdameritrade.com/price-history/apis/get/marketdata/%7Bsymbol%7D/pricehistory#
    """
    if period_type == PERIOD_TYPE.DAY:
        if period not in [
            PERIOD.ONE_DAY,
            PERIOD.TWO_DAYS,
            PERIOD.THREE_DAYS,
            PERIOD.FOUR_DAYS,
            PERIOD.FIVE_DAYS,
            PERIOD.TEN_DAYS
        ]:
            return False
    elif period_type == PERIOD_TYPE.MONTH:
        if period not in [PERIOD.ONE_MONTH, PERIOD.TWO_MONTHS, PERIOD.THREE_MONTHS, PERIOD.SIX_MONTHS]:
            return False
    elif period_type == PERIOD_TYPE.YEAR:
        if period not in [
            PERIOD.ONE_YEAR,
            PERIOD.TWO_YEARS,
            PERIOD.THREE_YEARS,
            PERIOD.FIVE_YEARS,
            PERIOD.TEN_YEARS,
            PERIOD.FIFTEEN_YEARS,
            PERIOD.TWENTY_YEARS
        ]:
            return False
    elif period_type == PERIOD_TYPE.YEAR_TO_DATE:
        if period not in [PERIOD.YEAR_TO_DATE]:
            return False
    return True


from pyspark import SparkConf, SparkContext
from pyspark.sql import SparkSession, Row
import csv

def getSparkSessionInstance(sparkConf):
    if ('sparkSessionInstance' not in globals()):
        globals()['sparkSessionInstance'] = SparkSession.builder.config(conf=sparkConf) \
                                            .enableHiveSupport().getOrCreate()
    return globals()['sparkSessionInstance']

def output():
    spark = getSparkSessionInstance(sc.getConf())
    df = spark.sql("use default")
    df = spark.sql("select kword, sum(total) as suma from ejercicio5  where times between cast('2018-12-07 12:00:00' as timestamp)- INTERVAL 1 DAY and cast('2018-12-07 12:00:00' as timestamp) group by kword order by suma desc")
    df.show()
    df.repartition(1).write.csv("/data/fndavidex4.csv")

if __name__ == "__main__":
    sc = SparkContext(appName="Ejercicio5")
    output()