import logging
import time

from django.db import transaction
from rest_framework.response import Response
from rest_framework.views import APIView

from lock.models import Lockable

logger = logging.getLogger('default')

class TestLockViewSet(APIView):

    def get(self, request):
        _pk = int(request.GET.get('pk'))
        _data = int(request.GET.get('data'))
        logger.warn('pk:' + str(_pk))
        _return_data = {
            'code': 0,
            'message': 'OK',
        }
        try:
            _lockable = self._transaction_get(_pk, _data)
            _return_data['result'] = str(_lockable)
        except Exception as e:
            logger.error('exception: ' + str(e))
            _return_data['result'] = str(e)
        return Response(status=200, data=_return_data)

    WAIT = 10

    @transaction.atomic
    def _transaction_get(self, pk, data):
        _lockable = Lockable.objects.select_for_update().get(pk=pk)

        logger.debug('select lockable row %s...' % str(_lockable))
        for i in range(self.WAIT):
            logger.debug('waiting %s seconds...' % (self.WAIT - i))
            time.sleep(1)
        _lockable.lockable_number += data
        logger.debug('updating lockable row %s...' % str(_lockable))
        _lockable.save()
        logger.debug('updated lockable row %s...' % str(_lockable))

        try:
            _lockable = self._transaction_get2(pk, data, raise_exception=True)
        except Exception as e:
            logger.error(str(e))

        _lockable = self._transaction_get2(pk, data)

        logger.debug('select lockable row %s...' % str(_lockable))
        for i in range(self.WAIT):
            logger.debug('waiting %s seconds...' % (self.WAIT - i))
            time.sleep(1)
        _lockable.lockable_number += data
        logger.debug('updating lockable row %s...' % str(_lockable))
        _lockable.save()
        logger.debug('updated lockable row %s...' % str(_lockable))

        return self._transaction_get2(pk, data)

    @transaction.atomic
    def _transaction_get2(self, pk, data, raise_exception=False):
        _lockable = Lockable.objects.select_for_update().get(pk=pk)
        logger.debug('select lockable row %s...' % str(_lockable))
        for i in range(self.WAIT):
            logger.debug('waiting %s seconds...' % (self.WAIT - i))
            time.sleep(1)
        _lockable.lockable_number += data
        logger.debug('updating lockable row %s...' % str(_lockable))
        _lockable.save()
        logger.debug('updated lockable row %s...' % str(_lockable))
        if raise_exception:
            logger.debug('Raise exception with lockable row %s...' % str(_lockable))
            raise Exception('Test exception...')
        return _lockable


# REFERENCE : https://www.hackerrank.com/challenges/py-collections-deque/problem

# collections.deque()
#  A deque is a double-ended queue.
#  It can be used to add or remove elements from both ends.
#  Deque support thread safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.

from collections import deque
d = deque()                                  # initialize deque()

d.append(1)                                  # deque([1])

d.appendleft(2)                              # deque([2, 1])

d.clear()                                    # deque([])

d.extend('1')                                # deque(['1'])

d.extendleft('234')                          # deque(['4', '3', '2', '1'])

d.count('1')                                 # 1

d.pop()                                      # deque(['4', '3', '2'])

d.popleft()                                  # deque(['3', '2'])

d.extend('7896')
d.remove('2')                                # deque(['3', '7', '8', '9', '6'])

d.reverse()                                  # deque(['6', '9', '8', '7', '3'])


url = 'https://www.apple.com/itunes/charts/songs/'
output_file_name = 'itunes_topsong.xlsx'

from urllib.request import urlopen
from bs4 import BeautifulSoup
import pyexcel

data = urlopen(url).read()
html_content = data.decode('utf-8')

# html_file = open('itunes_topsong.html', 'wb') # write
# html_file.write(data)
# html_file.close()

soup = BeautifulSoup(html_content, 'html.parser')
div = soup.find_all('div', 'section-content')

li_list = div[1].find_all('li')

top_list = []

for li in li_list:
    Name = li.h3.string
    Artist = li.h4.string
    song = {
    'Name': Name,
    'Artist': Artist,
    }
    top_list.append(song)
# print(top_list)
# pyexcel.save_as(records = top_list, dest_file_name = 'itunes_topsong.xlsx')

from youtube_dl import YoutubeDL

download_list = []
stt = 0
for i in top_list:
    down_song = []
    k = i['Name'] + ' ' + i['Artist']
    down_song.append(k)
    # download_list.append(k)
    stt += 1
    print(stt, ": ", k)
# print(download_list)
    options = {
        'default_search': 'ytsearch',
        'max_downloads': 1
        # 'outtmpl': '%(title)s-%(id)s.%(ext)s'
    }
    # print(options['outtmpl'])
    dl = YoutubeDL(options)
    dl.download(down_song)


import random
import string
import json
from flask import Response
from lms.Domain.Users import User
from lms.Domain.Students import Student

def get_response(answer):
    answer_js = json.dumps(answer)
    if answer.get('status') == 'error':
        return Response(answer_js, status=400, mimetype='application/json')
    return Response(answer_js, status=200, mimetype='application/json')

def generate_validation_code(string_length=6):
    return ''.join(random.choices(string.ascii_uppercase + string.digits, k=string_length))

def blank_resp():
    return {
        'data': [],
        'error_message': '',
        'status': 'ok'
    }

def init_user(form):
    return User(
        name=form.name.data,
        surname=form.surname.data,
        second_name=form.second_name.data,
        status=form.status.data,
        registration_uid=generate_validation_code()
    )

def init_student(form, user_id, group_id):
    return Student(
        user_id=user_id,
        group_id=group_id,
        year=form.year.data,
        degree=form.degree.data,
        education_form=form.education_form.data,
        education_basis=form.education_basis.data
    )

def init_full_user(form):
    user = User(
        name=form.name.data,
        surname=form.surname.data,
        second_name=form.second_name.data,
        status=form.status.data,
        registration_uid=generate_validation_code(),
        email=form.email.data,
        username=form.username.data
    )
    user.set_password(form.password.data)
    return user


from typing import List, Optional, Union

import torch
import torch.nn as nn
from mmengine.model import BaseModule

from mmselfsup.registry import MODELS

@MODELS.register_module()
class MoCoV2Neck(BaseModule):
    """The non-linear neck of MoCo v2: fc-relu-fc.

    Args:
        in_channels (int): Number of input channels.
        hid_channels (int): Number of hidden channels.
        out_channels (int): Number of output channels.
        with_avg_pool (bool): Whether to apply the global
            average pooling after backbone. Defaults to True.
        init_cfg (dict or list[dict], optional): Initialization config dict.
            Defaults to None.
    """

    def __init__(self,
                 in_channels: int,
                 hid_channels: int,
                 out_channels: int,
                 with_avg_pool: bool = True,
                 init_cfg: Optional[Union[dict, List[dict]]] = None) -> None:
        super().__init__(init_cfg)
        self.with_avg_pool = with_avg_pool
        if with_avg_pool:
            self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.mlp = nn.Sequential(
            nn.Linear(in_channels, hid_channels), nn.ReLU(inplace=True),
            nn.Linear(hid_channels, out_channels))

    def forward(self, x: List[torch.Tensor]) -> List[torch.Tensor]:
        """Forward function.

        Args:
            x (List[torch.Tensor]): The feature map of backbone.

        Returns:
            List[torch.Tensor]: The output features.
        """
        assert len(x) == 1
        x = x[0]
        if self.with_avg_pool:
            x = self.avgpool(x)
        return [self.mlp(x.view(x.size(0), -1))]


import random
import pyautogui as pag
import cv2
import pytesseract
import ctypes
from PIL import Image
import numpy as np
import re
import time
import os
import json
from gen_curve import get_curve
import math
import concurrent.futures

SAFE_EDGE = (2350, 580)
SET_WIDTH = 2560
SET_HEIGHT = 1440
SENSITIVITY_POS = ((1595, 280), (1695, 310))
CHANGE_HERO_POS = ((2165, 625), (2490, 680))
HERO_NAME_POS = ((2170, 555), (2485, 600))
OPTIONS_BTN_POS = ((1170, 700), (1750, 765))
CONTROLS_BTN_POS = ((425, 85), (600, 125))
LOCATE_CONFIDENCE = 0.6
TWEENS = [pag.easeInQuad, pag.easeOutQuad, pag.easeInOutQuad]
TYPING_INTERVAL = 0.25

def preprocess(img):
    img = img.convert("RGB")

    # Convert the image to a NumPy array
    img_np = np.array(img)

    # Define the color codes to filter for
    color_codes = [
        (255, 255, 255),
    ]

    # Define the color similarity threshold
    threshold = 60  # Adjust this value to control the leeway in color matching

    # Filter for colors in the screenshot using NumPy operations
    filtered_mask = np.zeros_like(img_np[:, :, 0], dtype=bool)
    for color_code in color_codes:
        color_code_np = np.array(color_code)
        color_difference = np.sum(np.abs(img_np[:, :, :3] - color_code_np), axis=2)
        matches = color_difference <= threshold
        filtered_mask |= matches

    # Expand the filtered mask to have the same shape as the screenshot image array
    filtered_mask_expanded = np.expand_dims(filtered_mask, axis=2)
    filtered_mask_expanded = np.repeat(filtered_mask_expanded, 3, axis=2)

    # Create a filtered image using the filtered mask
    filtered_image_np = np.where(filtered_mask_expanded, img_np, 0)
    thres_img = cv2.threshold(filtered_image_np, 127, 255, cv2.THRESH_BINARY_INV)[1]

    return Image.fromarray(thres_img)

def get_left_top_width_height(pos):
    """
    Returns a tuple of four values representing the left, top, width, and height of a rectangle
    defined by the two corners of a rectangle given as the argument 'pos'.

    Args:
    pos (tuple): A tuple of two tuples, representing the top-left and bottom-right corners of a rectangle.

    Returns:
    tuple: A tuple of four integers representing the left, top, width, and height of the rectangle.

    Example:
    If pos = ((10, 20), (50, 80)), the function returns (10, 20, 40, 60), which represents a rectangle
    with top-left corner at (10, 20), width 40, and height 60.
    """
    l_x = pos[0][0]
    r_x = pos[1][0]

    l_y = pos[0][1]
    r_y = pos[1][1]

    return (l_x, l_y, r_x - l_x, r_y - l_y)

def get_monitor_resolution():
    """
    Returns the resolution of the primary monitor in pixels as a tuple (width, height).
    This function uses the `GetSystemMetrics()` function from the Windows user32.dll library,
    so it is only compatible with Windows operating systems.

    Returns:
    - tuple: A tuple of integers representing the width and height of the primary monitor's resolution.
    """
    user32 = ctypes.windll.user32
    return user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)

def get_scalars():
    """
    Returns the scaling factors to fit the desired width and height
    (SET_WIDTH and SET_HEIGHT) into the user's monitor resolution.

    Returns:
    - w_scalar (float): The scaling factor for the width dimension.
    - h_scalar (float): The scaling factor for the height dimension.
    """
    u_width, u_height = get_monitor_resolution()

    w_scalar = SET_WIDTH / u_width
    h_scalar = SET_HEIGHT / u_height

    if u_width < SET_WIDTH:
        w_scalar = u_width / SET_WIDTH

    if u_height < SET_HEIGHT:
        h_scalar = u_height / SET_HEIGHT

    return w_scalar, h_scalar

def get_scaled_position(x, y):
    """
    Takes in the x and y coordinates and returns their scaled position on the user's screen, based on the user's monitor resolution and the desired width and height set in SET_WIDTH and SET_HEIGHT constants.

    Args:
        x (int): The x-coordinate of the position to be scaled.
        y (int): The y-coordinate of the position to be scaled.

    Returns:
        A tuple (x, y) containing the scaled position of the input coordinates on the user's screen.
    """
    w_scalar, h_scalar = get_scalars()

    x *= w_scalar
    y *= h_scalar

    return (x, y)

def get_scaled_pos(pos):
    """
    Scales the position of a rectangular region by the user's monitor resolution to ensure
    that the screenshot is taken with the same aspect ratio regardless of the user's screen resolution.

    Args:
        pos (tuple): A tuple of four coordinates (x1, y1, x2, y2) defining a rectangular region.

    Returns:
        list: A list of two tuples containing the scaled coordinates of the top-left and bottom-right corners
              of the rectangular region.
    """
    scaled_pos = []

    for corner in pos:
        corner_pos = get_scaled_position(*corner)
        scaled_pos.append(corner_pos)

    return scaled_pos

def get_cropped_screenshot(area, do_preprocess):
    """
    Get a cropped screenshot of a specified area on the screen.

    Args:
    area (tuple): A tuple of integers representing the area to capture in the format (left, top, width, height).
    do_preprocess (bool): A flag indicating whether to preprocess the captured image before returning.

    Returns:
    PIL.Image: A PIL.Image object representing the captured screenshot.

    Note:
    - The area should be specified in screen coordinates.
    - If do_preprocess is True, the captured image will be preprocessed using the preprocess function before returning.
    """
    img = pag.screenshot(region=area)
    if do_preprocess:
        img = preprocess(img)
    return img

def clean_string(str):
    """
    Cleans a given string by removing all non-alphanumeric characters except for spaces and periods.

    Args:
    str (str): The string to be cleaned.

    Returns:
    str: The cleaned string.

    Example:
    >>> clean_string("Hi! This is a string with (a lot) of [punctuation] and $ymbols.")
    'Hi This is a string with a lot of punctuation and yymbols'
    """
    res = re.findall(r"([\d\w\s\.]+)\W", str)
    return res[0].strip("\n") if len(res) else ""

def get_pos_in_area(area):
    """
    Generate a random position within the given area.

    Args:
        area: A tuple containing two tuples representing the top-left and bottom-right corners
              of the area, respectively. Each corner is represented by a tuple of (x, y) coordinates.

    Returns:
        A tuple containing the x and y coordinates of the randomly generated position within the area.
    """
    x = random.randint(area[0][0], area[1][0])
    y = random.randint(area[0][1], area[1][1])
    return x, y

def get_centre_pos_from_box(box):
    """Calculate the center position of a bounding box object.

    Args:
        box (pyautogui.Box): A bounding box object.

    Returns:
        tuple: A tuple of the x and y coordinates of the center of the box.
    """
    return (box.left + box.width // 2, box.top + box.height // 2)

def get_centre_pos(left, top, width, height):
    """Calculate the center position of a rectangle given its coordinates.

    Args:
        left (int): The x-coordinate of the top-left corner of the rectangle.
        top (int): The y-coordinate of the top-left corner of the rectangle.
        width (int): The width of the rectangle.
        height (int): The height of the rectangle.

    Returns:
        tuple: A tuple of the x and y coordinates of the center of the rectangle.
    """
    return (left + width // 2, top + height // 2)

def get_text_from_position(pos, preprocess=False):
    """
    Extracts text from a given position on the screen using Tesseract OCR engine.

    Args:
    pos (tuple): A tuple of 2 tuples representing the top-left and bottom-right corners of the rectangular area on the screen.
    preprocess (bool, optional): Whether to preprocess the screenshot before performing OCR. Defaults to False.

    Returns:
    str: The text extracted from the specified position on the screen, cleaned and formatted.
    """
    area = get_left_top_width_height(pos)
    img = get_cropped_screenshot(area, preprocess)
    text = pytesseract.image_to_string(img)
    return clean_string(text)

def click_in_area(area, human_movement):
    """
    This function generates a random position within a given area, moves the cursor to that position, clicks on it and
    waits for a random time interval to simulate a human-like clicking behaviour.

    Parameters:
    -----------
    area: tuple
        A tuple of two tuples, each containing the (x,y) coordinates of the top-left and bottom-right corners of the
        rectangular area.

    Returns:
    --------
    None
    """
    pos = get_pos_in_area(area)
    move_to_pos(pos, human_movement)
    pag.click()

def click_on_pos(pos, human_movement):
    """
    This function moves the cursor to the given position and clicks on it while waiting for a random time interval to
    simulate a human-like clicking behaviour.

    Parameters:
    -----------
    pos: tuple
        A tuple containing the (x,y) coordinates of the position to click on.

    Returns:
    --------
    None
    """
    move_to_pos(pos, human_movement)
    pag.click()

def move_along_curve(curve):
    for i, (x, y) in enumerate(curve):
        if i % random.randint(1, 3) == 0:
            pag.moveTo(x, y, duration=random.uniform(0.005, 0.05))

def get_distance(pos1, pos2):
    x1, y1 = pos1
    x2, y2 = pos2
    return math.sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))

def move_to_pos(pos, human_movement):
    if human_movement:
        start_pos = pag.position()
        distance = get_distance(start_pos, pos)
        num_points = int(distance // 50)
        curve = get_curve(start_pos, pos, num_points)
        move_along_curve(curve)
    pag.moveTo(pos, duration=random.uniform(0.01, 0.02), tween=random.choice(TWEENS))

def get_hero_data_locations(screenshot, human_movement):
    data = []
    filenames = [os.path.join("heroes", filename) for filename in os.listdir("heroes")]
    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
        futures = []
        for img_path in filenames:
            futures.append(executor.submit(get_hero_card_location, img_path, screenshot))

        for future in concurrent.futures.as_completed(futures):
            hero_img_path, location = future.result()
            click_on_pos(location, human_movement)
            pos = get_pos_in_area(CHANGE_HERO_POS)

            with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
                hero_data_future = executor.submit(get_hero_data)
                move_cursor_future = executor.submit(move_to_pos, pos, human_movement)

                sensitivity, hero_name = hero_data_future.result()
                move_cursor_future.result()

            pag.click(pos)

            data.append(
                {"name": hero_name, "sensitivity": sensitivity, "filepath": hero_img_path}
            )
            print(hero_name, sensitivity)

    return data

def get_hero_card_location(hero_img_path, screenshot):
    hero_card = pag.locate(hero_img_path, screenshot, confidence=LOCATE_CONFIDENCE)
    if not hero_card:
        print("BAD!")
        return {}
    centre = get_centre_pos_from_box(hero_card)

    return hero_img_path, centre

def get_hero_data():
    sensitivity = get_text_from_position(SENSITIVITY_POS, True)
    hero_name = get_text_from_position(HERO_NAME_POS)
    return sensitivity, hero_name

def get_all_heroes_screenshot(human_movement):
    click_in_area(OPTIONS_BTN_POS, human_movement)
    click_in_area(CONTROLS_BTN_POS, human_movement)
    click_in_area(CHANGE_HERO_POS, human_movement)
    move_to_pos(SAFE_EDGE, human_movement)
    # sleep so we take a screenshot of the correct page
    time.sleep(1)
    return pag.screenshot()

def get_sensitivity_data(human_movement=True):
    """Get sensitivity data for all heroes.

    Returns:
        A list of dictionaries where each dictionary contains the following keys:
        - name: the name of the hero (str)
        - sensitivity: the sensitivity of the hero (float)
        - filepath: the filepath to the hero's image (str)
    """

    data = []

    all_heroes_img = get_all_heroes_screenshot(human_movement)

    data = get_hero_data_locations(all_heroes_img, human_movement)

    return data

def set_sensitivity_data(data, human_movement=True):
    """
    Sets the sensitivity data for the heroes specified in the data list.

    Args:
        data (list): A list of dictionaries containing hero data. Each dictionary should have
                     the following keys: 'name', 'sensitivity', and 'filepath'. 'name' is the
                     name of the hero, 'sensitivity' is a string representing the sensitivity
                     value, and 'filepath' is the file path of the hero image.

    Returns:
        None
    """
    all_heroes_img = get_all_heroes_screenshot(human_movement)

    for hero in data:
        hero_card = pag.locate(
            hero["filepath"], all_heroes_img, confidence=LOCATE_CONFIDENCE
        )
        if not hero_card:
            print("BAD!", hero["filepath"])
            continue
        print(hero["filepath"])
        centre = get_centre_pos_from_box(hero_card)
        click_on_pos(centre, human_movement)
        sens_centre = get_centre_pos(*get_left_top_width_height(SENSITIVITY_POS))
        click_on_pos(sens_centre, human_movement)
        pag.keyDown("ctrl")
        pag.press("a")
        pag.keyUp("ctrl")
        pag.write(hero["sensitivity"], interval=TYPING_INTERVAL)
        pag.press("enter")
        click_in_area(CHANGE_HERO_POS, human_movement)

def save_settings_to_json(filename, human_movement):
    data = get_sensitivity_data(human_movement)
    with open(filename, "w+") as fn:
        json.dump(data, fn)

def load_settings_from_json(filename, human_movement):
    with open(filename, "r") as fn:
        data = json.load(fn)
    set_sensitivity_data(data, human_movement)

if __name__ == "__main__":

    save_settings_to_json("settings.json", False)
    # load_settings_from_json("settings.json", False)


import pygame
from random import choice, randrange

resl = WIDTH,HIGHT =1352,832
TILE = 104

cols,rows = WIDTH//TILE,HIGHT//TILE

pygame.init()
screen = pygame.display.set_mode(resl)
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 60)

class Cell:
    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.walls = {'top':True,'right':True,'bottom':True,'left':True}
        self.visited = False
        self.map = False
        self.goal = False

    def draw_currtent_cell(self):
        x,y = self.x*TILE,self.y*TILE
        pygame.draw.rect(screen, pygame.Color('green'), (x+2,y+2,TILE-2,TILE-2))

    def draw(self):
        x,y = self.x*TILE,self.y*TILE

        if self.map:
            pygame.draw.rect(screen, pygame.Color((100,100,100)), (x,y,TILE,TILE))

        if self.goal:
             pygame.draw.rect(screen, pygame.Color('red'), (x,y,TILE,TILE))
        if self.visited:
            pygame.draw.rect(screen, pygame.Color('black'), (x,y,TILE,TILE))
        if self.walls['top']:
            pygame.draw.line(screen, pygame.Color('black'), (x, y), (x + TILE, y),2)
        if self.walls['right']:
            pygame.draw.line(screen, pygame.Color('black'), (x + TILE, y), (x + TILE, y + TILE),2)
        if self.walls['bottom']:
            pygame.draw.line(screen, pygame.Color('black'), (x + TILE, y + TILE), (x , y + TILE),2)
        if self.walls['left']:
            pygame.draw.line(screen, pygame.Color('black'), (x, y + TILE), (x, y),2)

    def check_cell(self, x, y):
        find_index = lambda x, y: x + y * cols
        if x < 0 or x > cols - 1 or y < 0 or y > rows - 1:
            return False
        return grid_cells[find_index(x, y)]

    def check_neighborsDFS(self):
        neighbors = []
        top = self.check_cell(self.x, self.y - 1)
        right = self.check_cell(self.x + 1, self.y)
        bottom = self.check_cell(self.x, self.y + 1)
        left = self.check_cell(self.x - 1, self.y)
        if top and not top.visited and not top.map:
            neighbors.append(top)
        if right and not right.visited and not right.map:
            neighbors.append(right)
        if bottom and not bottom.visited and not bottom.map:
            neighbors.append(bottom)
        if left and not left.visited and not left.map:
            neighbors.append(left)
        return choice(neighbors) if neighbors else False

    def check_neighborsA(self,goalx,goaly):
        top = self.check_cell(self.x, self.y - 1)
        right = self.check_cell(self.x + 1, self.y)
        bottom = self.check_cell(self.x, self.y + 1)
        left = self.check_cell(self.x - 1, self.y)

        topH = abs(top.x - goalx)+abs(top.y-goaly)
        rightH = abs(right.x - goalx)+abs(right.y-goaly)
        bottomH = abs(bottom.x - goalx)+abs(bottom.y-goaly)
        leftH = abs(left.x - goalx)+abs(left.y-goaly)
        if not top or  top.visited or  top.map:
            topH = 1000
        if not right or  right.visited or  right.map:
            rightH = 1000
        if not bottom or  bottom.visited or  bottom.map:
            bottomH = 1000
        if not left or  left.visited or  left.map:
            leftH = 1000
        print("toph= ",topH," rightH= ",rightH," bottomH= ",bottomH," leftH= ",leftH)
        if top and not top.visited and not top.map:
            if topH<=rightH and topH<=bottomH and topH<=leftH:
                return top if top else False
        if right and not right.visited and not right.map:
            if rightH<=topH and rightH<=bottomH and rightH<=leftH:
                return right if right else False
        if bottom and not bottom.visited and not bottom.map:
            if bottomH<=rightH and bottomH<=topH and bottomH<=leftH:
                return bottom if bottom else False
        if left and not left.visited and not left.map:
            if leftH<=rightH and leftH<=bottomH and leftH<=topH:
                return left if left else False

class Button:
	def __init__(self,text,width,height,pos,elevation):
		#Core attributes
		self.pressed = False
		self.elevation = elevation
		self.dynamic_elecation = elevation
		self.original_y_pos = pos[1]
		# top rectangle
		self.top_rect = pygame.Rect(pos,(width,height))
		self.top_color = '#475F77'

		# bottom rectangle
		self.bottom_rect = pygame.Rect(pos,(width,height))
		self.bottom_color = '#354B5E'
		#text
		self.text_surf = font.render(text, True, (0,0,255))
		self.text_rect = self.text_surf.get_rect(center = self.top_rect.center)

	def draw(self):
		# elevation logic
		self.top_rect.y = self.original_y_pos - self.dynamic_elecation
		self.text_rect.center = self.top_rect.center

		self.bottom_rect.midtop = self.top_rect.midtop
		self.bottom_rect.height = self.top_rect.height + self.dynamic_elecation

		pygame.draw.rect(screen,self.bottom_color, self.bottom_rect,border_radius = 12)
		pygame.draw.rect(screen,self.top_color, self.top_rect,border_radius = 12)
		screen.blit(self.text_surf, self.text_rect)
		self.check_click()

	def check_click(self):
		mouse_pos = pygame.mouse.get_pos()
		if self.top_rect.collidepoint(mouse_pos):
			self.top_color = '#D74B4B'
			if pygame.mouse.get_pressed()[0]:
				self.dynamic_elecation = 0
				self.pressed = True
		else:
			self.dynamic_elecation = self.elevation
			self.top_color = '#475F77'

button1 = Button('DFS',200,40,(376,416),5)
button2 = Button('A*',200,40,(776,416),5)
grid_cells = [Cell(col, row) for row in range(rows) for col in range(cols)]
current_cell = grid_cells[67]
map = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,17,25,26,31,33,38,39,42,47,49,51,52,56,61,64,65,70,72,76,77,78,81,86,90,91,92,93,94,95,96,97,98,99,100,101,102,103]
goal = 35
player = 67
stack = []

#draw map and goal

for i in map:
    grid_cells[i].map = True
    grid_cells[i].draw()

grid_cells[goal].goal = True
grid_cells[goal].draw()

speed = 30
while True:
    screen.fill(pygame.Color('white'))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    if button1.pressed or button2.pressed:
        speed = 1
        if button1.pressed:
            if not current_cell.goal:
                [cell.draw() for cell in grid_cells]
                current_cell.visited = True
                current_cell.draw_currtent_cell()

                next_cell = current_cell.check_neighborsDFS()
                if next_cell:
                    next_cell.visited = True
                    stack.append(next_cell)
                    current_cell = next_cell
                else:
                    current_cell =stack.pop()
            else:
                font = pygame.font.SysFont(None, 60)
                img = font.render('win', True, (0,0,255))
                screen.blit(img, (676, 416))
        else:
            if not current_cell.goal:
                [cell.draw() for cell in grid_cells]
                current_cell.visited = True
                current_cell.draw_currtent_cell()

                next_cell = current_cell.check_neighborsA(grid_cells[goal].x,grid_cells[goal].y)
                if next_cell:
                    next_cell.visited = True
                    stack.append(next_cell)
                    current_cell = next_cell
                else:
                    current_cell =stack.pop()
            else:
                font = pygame.font.SysFont(None, 60)
                img = font.render('win', True, (0,0,255))
                screen.blit(img, (676, 416))
    else:
        button1.draw()
        button2.draw()
    pygame.display.flip()
    clock.tick(speed)


from flask import Flask, render_template, request, Response, redirect,url_for
app = Flask(__name__)

import matplotlib.pyplot as plt
import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
import contextily
import geopandas as gpd
import io
import pandas as pd

stazioni = pd.read_csv("/workspace/Flask/static/files/coordfix_ripetitori_radiofonici_milano_160120_loc_final.csv",sep= ";")
stazionigeo =gpd.read_file("/workspace/Flask/static/files/ds710_coordfix_ripetitori_radiofonici_milano_160120_loc_final.geojson",sep= ";")
quartieri = gpd.read_file("/workspace/Flask/static/files/ds964_nil_wm.zip")

@app.route('/', methods=['GET'])
def home():
    return render_template("verificab/home.html")

@app.route('/ricerca', methods=['GET'])
def ricerca():
    quartierilista = quartieri.NIL.to_list()
    quartierilista.sort()

    return render_template("verificab/ricerca.html",quart = quartierilista)

@app.route('/risultato', methods=['GET'])
def elenco():
    quartiereutente = request.args["opzioni"]

    quartierescelto = quartieri[quartieri['NIL']==quartiereutente]
    stazioniquart = stazionigeo[stazionigeo.within(quartierescelto.geometry.squeeze())]

    return render_template("verificab/risultato.html",nomistazioni = stazioniquart.to_html())

#_______________________________________________________________________________________________________________________________

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

    return render_template("verificab/inserire.html")

@app.route('/contenuto', methods=['GET'])
def contenuto():
    global stazioninelquartiere,quartiereutente1

    nomequartiere = request.args["nomequart"]
    quartiereutente1 = quartieri[quartieri['NIL']==nomequartiere]
    stazioninelquartiere = stazionigeo[stazionigeo.within(quartiereutente1.geometry.squeeze())]
    return render_template("verificab/mappa.html")

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

    fig, ax = plt.subplots(figsize=(12, 8))

    stazioninelquartiere.to_crs(epsg=3857).plot(ax=ax,color="k")
    quartiereutente1.to_crs(epsg=3857).plot(ax=ax, alpha=0.5,edgecolor='k')
    contextily.add_basemap(ax=ax)

    output = io.BytesIO()
    FigureCanvas(fig).print_png(output)
    return Response(output.getvalue(), mimetype='image/png')
#______________________________________________________________________________________________________
@app.route('/grafico', methods=['GET'])
def grafico():
    global risultato
    risultato = stazioni.groupby("MUNICIPIO")["OPERATORE"].count().reset_index()

    return render_template("verificab/grafico.html",risultato = risultato.to_html())

@app.route('/graficoimmagine', methods=['GET'])
def graficoimmagine():
    fig, ax = plt.subplots(figsize = (6,4))

    x = risultato.MUNICIPIO
    y = risultato.OPERATORE

    ax.bar(x, y, color = "#304C89")
    #visualizzazione del grafico
    output = io.BytesIO()
    FigureCanvas(fig).print_png(output)
    return Response(output.getvalue(), mimetype='image/png')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=3245, debug=True)


from django.contrib import admin
from .models import Report, Ward, PollingUnit, AgentProfile

@admin.register(Ward)
class WardAdmin(admin.ModelAdmin):
    list_display = [
        "local_government",
        "name",
        "added_at",
    ]

@admin.register(PollingUnit)
class PollingUnitAdmin(admin.ModelAdmin):
    list_display = [
        "name",
        "ward",
        "added_at",
    ]

@admin.register(AgentProfile)
class AgentProfileAdmin(admin.ModelAdmin):
    def full_name(self, obj):
        return obj.user.get_full_name()

    def email(self, obj):
        return obj.user.email

    list_display = [
        "full_name",
        "email",
        "phone_number",
        "address",
        "assigned_ward",
    ]

@admin.register(Report)
class ReportAdmin(admin.ModelAdmin):
    def ward(self, obj):
        return obj.polling_unit.ward
    list_display = [
        "agent",
        "ward",
        "polling_unit",
        "uploaded_at",
        "recorded_at",
        "pdp",
        "apc",
        "nnpp",
        "image",
    ]


from django.urls import path
from . import views

#Handles all the urls. Use after view.py.
urlpatterns = [
    path('', views.index, name='index'),
    path('book_detail/<int:book_id>', views.book_detail, name='book_detail'),
    path('book_delete/<int:book_id>', views.book_delete, name='book_delete'),
    path('postbook', views.postbook, name='postbook'),
    path('displaybooks', views.displaybooks, name='displaybooks'),
    path('mybooks', views.mybooks, name='mybooks'),
    path('requestbook', views.requestbook, name='requestbook'),
    path('request_delete/<int:request_id>', views.request_delete, name='request_delete'),
    path('book_addCart/<int:book_id>', views.book_addCart, name='book_addCart'),
    path('shoppingcart', views.shoppingcart, name='shoppingcart'),
    path('cart_delete/<int:cart_id>', views.cart_delete, name='cart_delete'),

]


# master.py
from __future__ import absolute_import
import json
import sys
import celery_tasks as c
from time import time, sleep

tasks_start = time()

tasks = []

N = 10

jsonSample = json.loads(open('./sample-generated.json').read())

#json_string = jsonSample
json_string = json.dumps(jsonSample)

print( "JSON Object size : %s bytes" % len(json_string))
#print( "JSON Object size : %s bytes" % sys.getsizeof(json_string) )

message_count = 0

for k in range(N):
    #print(json_string)
    tasks.append(c.goScanmission.delay(json_string))
    message_count = message_count + 1

print('Message Count : ' + str(message_count))

ready= []

collect_start = time()

for k, t in enumerate(tasks):
    response = t.wait()
    ready.append(response)

done = time()

print('celery', collect_start - tasks_start, done - tasks_start)


from datetime import date

from sqlalchemy import and_
from sqlalchemy.orm import Session

from src.database.models import Contact, User
from src.schemas import ContactModel

async def get_contacts(skip: int, limit: int, first_name: str, last_name: str, email: str, user: User, db: Session):
    """
    The get_contacts function returns a list of contacts that match the search criteria.
        If no search criteria is provided, it will return all contacts for the user.

    :param skip: int: Skip the first n number of contacts in the database
    :param limit: int: Limit the number of results returned
    :param first_name: str: Filter the contacts by first name
    :param last_name: str: Filter the contacts by last name
    :param email: str: Filter the contacts by email
    :param user: User: Get the user id of the logged in user
    :param db: Session: Access the database
    :return: A list of contacts
    :doc-author: Trelent
    """
    first_name_query = db.query(Contact).filter(and_(Contact.first_name == first_name, Contact.user_id == user.id))
    last_name_query = db.query(Contact).filter(and_(Contact.last_name == last_name, Contact.user_id == user.id))
    email_query = db.query(Contact).filter(and_(Contact.email == email, Contact.user_id == user.id))
    if first_name and last_name and email:
        return first_name_query.union(last_name_query).union(email_query).all()
    if first_name and last_name:
        return first_name_query.union(last_name_query).all()
    if first_name and email:
        return first_name_query.union(email_query).all()
    if last_name and email:
        return last_name_query.union(email_query).all()
    if first_name:
        return first_name_query.all()
    if last_name:
        return last_name_query.all()
    if email:
        return email_query.all()
    return db.query(Contact).filter(Contact.user_id == user.id).offset(skip).limit(limit).all()

async def get_contacts_birthdays(skip: int, limit: int, user: User, db: Session):
    """
    The get_contacts_birthdays function returns a list of contacts with birthdays in the next 7 days.
        Args:
            skip (int): The number of contacts to skip.
            limit (int): The maximum number of contacts to return.
            user (User): A User object containing information about the current user, including their id and email address.
            db (Session): An SQLAlchemy Session object used for querying the database.

    :param skip: int: Skip a number of contacts in the database
    :param limit: int: Limit the number of contacts returned
    :param user: User: Get the user id from the database
    :param db: Session: Pass the database session to the function
    :return: A list of contacts that have their birthdays in the next 7 days
    :doc-author: Trelent
    """
    contacts_with_birthdays = []
    today = date.today()
    current_year = today.year
    contacts = db.query(Contact).filter(Contact.user_id == user.id).offset(skip).limit(limit).all()
    for contact in contacts:
        td = contact.date_of_birth.replace(year=current_year) - today
        if 0 <= td.days <= 7:
            contacts_with_birthdays.append(contact)
        else:
            continue
    return contacts_with_birthdays

async def get_contact_by_id(contact_id: int, user: User, db: Session):
    """
    The get_contact_by_id function returns a contact by its id.
        Args:
            contact_id (int): The id of the contact to be returned.
            user (User): The user who is requesting the return of this specific contact.
            db (Session): A database session object that will be used to query for contacts in our database.
        Returns:
            Contact: A single Contact object from our database, or None if no such Contact exists.

    :param contact_id: int: Get the contact by its id
    :param user: User: Get the user id from the database
    :param db: Session: Pass a database session to the function
    :return: A contact object with the given id if it exists
    :doc-author: Trelent
    """
    return db.query(Contact).filter(and_(Contact.id == contact_id, Contact.user_id == user.id)).first()

async def create_contact(body: ContactModel, user: User, db: Session):
    """
    The create_contact function creates a new contact in the database.

    :param body: ContactModel: Pass the request body to the function
    :param user: User: Get the user id from the database
    :param db: Session: Access the database
    :return: A contact object
    :doc-author: Trelent
    """
    contact = Contact(**body.dict(), user_id=user.id)
    db.add(contact)
    db.commit()
    db.refresh(contact)
    return contact

async def update_contact(contact_id: int, body: ContactModel, user: User, db: Session):
    """
    The update_contact function updates a contact in the database.
        Args:
            contact_id (int): The id of the contact to update.
            body (ContactModel): The updated information for the specified user's contact.
            user (User): The current logged-in user, used to verify that they are updating their own contacts and not someone else's.
            db (Session): A connection to our database, used for querying and committing changes.

    :param contact_id: int: Identify the contact that is being updated
    :param body: ContactModel: Pass the contact object to the function
    :param user: User: Get the user id from the token
    :param db: Session: Access the database, and the user: user parameter is used to get information about the current user
    :return: The contact object
    :doc-author: Trelent
    """
    contact = db.query(Contact).filter(and_(Contact.id == contact_id, Contact.user_id == user.id)).first()
    if contact:
        contact.first_name = body.first_name
        contact.last_name = body.last_name
        contact.email = body.email
        contact.phone = body.phone
        contact.date_of_birth = body.date_of_birth
        db.commit()
    return contact

async def remove_contact(contact_id: int, user: User, db: Session):
    """
    The remove_contact function removes a contact from the database.
        Args:
            contact_id (int): The id of the contact to be removed.
            user (User): The user who owns the contacts list.
            db (Session): A session object for interacting with the database.
        Returns:
            Contact: The deleted Contact object.

    :param contact_id: int: Specify the id of the contact to be removed
    :param user: User: Get the user_id from the user object
    :param db: Session: Pass the database session to the function
    :return: The contact that was removed
    :doc-author: Trelent
    """
    contact = db.query(Contact).filter(and_(Contact.id == contact_id, Contact.user_id == user.id)).first()
    if contact:
        db.delete(contact)
        db.commit()
    return contact


from typing import Literal
from pydantic import BaseModel, ConfigDict

class TrainPlace(BaseModel):
    model_config = ConfigDict(frozen=True)

    trainNumber: str
    carNumber: str
    compartmentNumber: str
    placeNumber: str
    carType: Literal['КУПЕ', 'ПЛАЦ']
    price: float
    isLower: bool
    isSide: bool
    isNearToilet: bool


import mrcfile
import numpy as np

def write_mrc_dataset(mrc_path: str, array: np.array, dtype="float32"):
    array = np.array(array, dtype=dtype)
    with mrcfile.new(mrc_path, overwrite=True) as mrc:
        mrc.set_data(array)
    print("Dataset saved in", mrc_path)
    return


from setuptools import setup, find_packages

with open("README.md", "r") as fh:
    long_description = fh.read()

setup(
    name='koi',
    packages=find_packages(exclude=['build', 'doc', 'templates']),
    version='0.0.0',
    install_requires=[
        # "numpy",
        # "scipy",
        # "torch"
    ],
    python_requires='>=3.6',
    license="MIT",
    description='koi: wip package',
    long_description=long_description,
    long_description_content_type="text/markdown",
    author='Luca Bergamin',
    author_email='luca.bergamin95+github@gmail.com',
    url='https://github.com/bouncybutton/koi',
    download_url='https://github.com/bouncybutton/koi',
    keywords=['pytorch', 'machine-learning', 'variational-autoencoder', 'anomaly-detection'],
    classifiers=[
        'Topic :: Scientific/Engineering :: Artificial Intelligence',
        'License :: OSI Approved :: MIT License',
    ]
)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Mood',
            fields=[
                ('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)),
                ('status', models.CharField(max_length=20)),
            ],
        ),
        migrations.CreateModel(
            name='Post',
            fields=[
                ('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)),
                ('nickname', models.CharField(max_length=20, default='一位不愿透露姓名的勇士')),
                ('message', models.TextField()),
                ('del_pass', models.CharField(max_length=10)),
                ('pub_time', models.DateTimeField(auto_now=True)),
                ('enabled', models.BooleanField(default=False)),
                ('mood', models.ForeignKey(to='formtest.Mood')),
            ],
        ),
    ]


import arrow
from dateutil.parser import parse

class DateHelper:
    @staticmethod
    def serialize_datetime(dt):
        return arrow.get(dt).format('YYYY-MM-DDTHH:mm') if dt else None

    @staticmethod
    def file_date_parse(dt):
        try:
         d = arrow.get(dt, 'YYYY:MM:DD HH:mm:ss').format('YYYY-MM-DDTHH:mm') if dt else None
         return d
        except Exception as e:
            print (e)
            return None

    @staticmethod
    def parse_datetime(dt):
        try:
            d = arrow.get(dt,'YYYY-MM-DDTHH:mm').datetime.replace(tzinfo=None) if dt else None
            return d
        except Exception as e:
            print(e)
            return None

    @staticmethod
    def parse_date(str_date):
        # Handle pandas naT (null dates values)
        if (str_date != str_date):
            return None
        try:
            d = parse(str(str_date))
            return str(d)
        except Exception as e:
            print (e)
            return None


import requests
import pandas as pd
from xml.etree import ElementTree
import gspread
from gspread_dataframe import set_with_dataframe

# https://medium.com/@robertopreste/from-xml-to-pandas-dataframes-9292980b1c1c
# def parse_XML(xml_file, df_cols):
#     """Parse the input XML file and store the result in a pandas
#     DataFrame with the given columns.

#     The first element of df_cols is supposed to be the identifier
#     variable, which is an attribute of each node element in the
#     XML data; other features will be parsed from the text content
#     of each sub-element.
#     """

#     xtree = et.parse(xml_file)
#     xroot = xtree.getroot()
#     rows = []

#     for node in xroot:
#         res = []
#         res.append(node.attrib.get(df_cols[0]))
#         for el in df_cols[1:]:
#             if node is not None and node.find(el) is not None:
#                 res.append(node.find(el).text)
#             else:
#                 res.append(None)
#         rows.append({df_cols[i]: res[i]
#                      for i, _ in enumerate(df_cols)})

#     out_df = pd.DataFrame(rows, columns=df_cols)

#     return out_df

chile = 'CHL'
australia = 'AUS'
colombia = 'COL'
estados_unidos = 'USA'
canada = 'CAN'
costa_rica ='CRI'

indicadores = ['Number of deaths', 'Number of infant deaths', 'Number of under-five deaths',
'Mortality rate for 5-14 year-olds (probability of dying per 1000 children aged 5-14 years)','Adult mortality rate (probability of dying between 15 and 60 years per 1000 population)',
'Estimates of number of homicides','Crude suicide rates (per 100 000 population)','Mortality rate attributed to unintentional poisoning (per 100 000 population)',
'Number of deaths attributed to non-communicable diseases, by type of disease and sex', 'Estimated road traffic death rate (per 100 000 population)', 'Estimated number of road traffic deaths',
'Mean BMI (kg/m²) (crude estimate)','Mean BMI (kg/m²) (age-standardized estimate)',  'Prevalence of obesity among adults, BMI ≥ 30 (age-standardized estimate) (%)',
'Prevalence of obesity among children and adolescents, BMI > +2 standard deviations above the median (crude estimate) (%)', 'Prevalence of overweight among adults, BMI ≥ 25 (age-standardized estimate) (%)',
'Prevalence of overweight among children and adolescents, BMI > +1 standard deviations above the median (crude estimate) (%)','Prevalence of underweight among adults, BMI < 18.5 (age-standardized estimate) (%)', 'Prevalence of thinness among children and adolescents, BMI < -2 standard deviations below the median (crude estimate) (%)',
'Alcohol, recorded per capita (15+) consumption (in litres of pure alcohol)', 'Estimate of daily cigarette smoking prevalence (%)', 'Estimate of daily tobacco smoking prevalence (%)', 'Estimate of current cigarette smoking prevalence (%)',
'Estimate of current tobacco smoking prevalence (%)', 'Mean systolic blood pressure (crude estimate)','Mean fasting blood glucose (mmol/l) (crude estimate)', 'Mean Total Cholesterol (crude estimate)' ]

indicadores_muertes = ['Number of deaths', 'Number of infant deaths', 'Number of under-five deaths',
'Mortality rate for 5-14 year-olds (probability of dying per 1000 children aged 5-14 years)','Adult mortality rate (probability of dying between 15 and 60 years per 1000 population)',
'Estimates of number of homicides','Crude suicide rates (per 100 000 population)','Mortality rate attributed to unintentional poisoning (per 100 000 population)',
'Number of deaths attributed to non-communicable diseases, by type of disease and sex', 'Estimated road traffic death rate (per 100 000 population)', 'Estimated number of road traffic deaths']
indicadores_peso =['Mean BMI (kg/m²) (crude estimate)','Mean BMI (kg/m²) (age-standardized estimate)',
'Prevalence of obesity among adults, BMI ≥ 30 (age-standardized estimate) (%)',
'Prevalence of obesity among children and adolescents, BMI > +2 standard deviations above the median (crude estimate) (%)',
'Prevalence of overweight among adults, BMI ≥ 25 (age-standardized estimate) (%)',
'Prevalence of overweight among children and adolescents, BMI > +1 standard deviations above the median (crude estimate) (%)',
'Prevalence of underweight among adults, BMI < 18.5 (age-standardized estimate) (%)',
'Prevalence of thinness among children and adolescents, BMI < -2 standard deviations below the median (crude estimate) (%)']
otros_indicadores = ['Alcohol, recorded per capita (15+) consumption (in litres of pure alcohol)', 'Estimate of daily cigarette smoking prevalence (%)', 'Estimate of daily tobacco smoking prevalence (%)', 'Estimate of current cigarette smoking prevalence (%)',
'Estimate of current tobacco smoking prevalence (%)', 'Mean systolic blood pressure (crude estimate)','Mean fasting blood glucose (mmol/l) (crude estimate)', 'Mean Total Cholesterol (crude estimate)']

chile_info = requests.get('http://tarea-4.2021-1.tallerdeintegracion.cl/gho_CHL.xml')
australia_info = requests.get('http://tarea-4.2021-1.tallerdeintegracion.cl/gho_AUS.xml')
colombia_info = requests.get('http://tarea-4.2021-1.tallerdeintegracion.cl/gho_COL.xml')
estados_unidos_info = requests.get('http://tarea-4.2021-1.tallerdeintegracion.cl/gho_USA.xml')
canada_info = requests.get('http://tarea-4.2021-1.tallerdeintegracion.cl/gho_CAN.xml')
costa_rica_info = requests.get('http://tarea-4.2021-1.tallerdeintegracion.cl/gho_CRI.xml')

chile_excel_root = ElementTree.fromstring(chile_info.content)
australia_excel_root = ElementTree.fromstring(australia_info.content)
colombia_excel_root = ElementTree.fromstring(colombia_info.content)
estados_unidos_excel_root = ElementTree.fromstring(estados_unidos_info.content)
canada_excel_root = ElementTree.fromstring(canada_info.content)
costa_rica_root = ElementTree.fromstring(costa_rica_info.content)

def parse_XML(root, indicadores):
    Fact = root.findall('Fact')
    df_cols = ['GHO', 'COUNTRY', 'SEX', 'YEAR', 'GHECAUSES', 'AGEGROUP', 'Display', 'Numeric', 'Low', 'High']
    rows = []
    for node in Fact:
        res = []
        nombre = node.find(df_cols[0]).text
        if nombre in indicadores:
            res.append(nombre)
            for el in df_cols[1:]:
                if node is not None and node.find(el) is not None:
                    res.append(node.find(el).text)
                else:
                    res.append(None)
            rows.append({df_cols[i]: res[i] for i, _ in enumerate(df_cols)})
    out_df = pd.DataFrame(rows, columns=df_cols)
    return out_df

df_chile_muertes = parse_XML(chile_excel_root, indicadores_muertes)
df_australia_muertes = parse_XML(australia_excel_root, indicadores_muertes)
df_colombia_muertes = parse_XML(colombia_excel_root, indicadores_muertes)
df_estados_unidos_muertes = parse_XML(estados_unidos_excel_root, indicadores_muertes)
df_canada_muertes = parse_XML(canada_excel_root, indicadores_muertes)
df_costa_rica_muertes = parse_XML(costa_rica_root, indicadores_muertes)

df_muerte = pd.concat([df_chile_muertes, df_australia_muertes, df_colombia_muertes, df_estados_unidos_muertes, df_canada_muertes, df_costa_rica_muertes])

df_chile_peso = parse_XML(chile_excel_root, indicadores_peso)
df_australia_peso = parse_XML(australia_excel_root, indicadores_peso)
df_colombia_peso = parse_XML(colombia_excel_root, indicadores_peso)
df_estados_unidos_peso = parse_XML(estados_unidos_excel_root, indicadores_peso)
df_canada_peso = parse_XML(canada_excel_root, indicadores_peso)
df_costa_rica_peso = parse_XML(costa_rica_root, indicadores_peso)

df_peso = pd.concat([df_chile_peso, df_australia_peso, df_colombia_peso, df_estados_unidos_peso, df_canada_peso, df_costa_rica_peso])

df_chile_otros = parse_XML(chile_excel_root, otros_indicadores)
df_australia_otros = parse_XML(australia_excel_root, otros_indicadores)
df_colombia_otros = parse_XML(colombia_excel_root, otros_indicadores)
df_estados_unidos_otros = parse_XML(estados_unidos_excel_root, otros_indicadores)
df_canada_otros = parse_XML(canada_excel_root, otros_indicadores)
df_costa_rica_otros = parse_XML(costa_rica_root, otros_indicadores)

df_otros = pd.concat([df_chile_otros, df_australia_otros, df_colombia_otros, df_estados_unidos_otros, df_canada_otros, df_costa_rica_otros])

# # out_df.to_excel("algo.xlsx")

print("hola")

# ACCES GOOGLE SHEET
gc = gspread.service_account(
    filename='taller-tarea-4-315516-d690cdb3da75.json')
# 'your_google_sheet_ID'
sh = gc.open_by_key('1FPVgjpA8FentUjEFL-Lnq5gVcL15Eogv8tk7SQ5qhBI')
# sheet_index_no -> 0 first sheet, 1 second  sheet
worksheet = sh.get_worksheet(0)
worksheet2 = sh.get_worksheet(1)
worksheet3 = sh.get_worksheet(2)

# # APPEND DATA TO SHEET
# your_dataframe = pd.DataFrame()
# ->This exports your dataframe to the googleSheet
set_with_dataframe(worksheet, df_muerte)
set_with_dataframe(worksheet2, df_peso)
set_with_dataframe(worksheet3, df_otros)


import json
import argparse
import os.path as osp
from datetime import timezone, datetime

# keys = title/title_text; createdAt; text; author; total_count; tags
# only need to remove title and author and total_count if the fields dont meet the requirements
# keep all other records

# part 1 & 2
def format_title(post):
    if "title" in post:
        return post
    elif "title_text" in post:
        post["title"] = post.pop("title_text")
        return post
    elif "title" not in post or "title_text" not in post:
        #print('does not contain title field')
        return None

# part 3 & 4
def createdat_to_utc(post):
    if "createdAt" not in post:
        return post

    day = post["createdAt"]
    try:
        time = datetime.strptime(day,"%Y-%m-%dT%H:%M:%S%z")
    except:
        #print("invalid datetime")
        post=None
    else:
        utc_time = datetime.fromtimestamp(time.timestamp(), tz=timezone.utc)
        post["createdAt"] = utc_time.strftime("%Y-%m-%dT%H:%M:%S%z")
    return post;

# part 6
def remove_author(post):
    if post["author"]=="N/A" or post["author"]==None:
        return None

    elif len(post["author"])==0: #empty
        return None
    else:
        return post

# part 7 & 8
def total_count_to_int(post):

    if "total_count" not in post: # if key not present, keep record
        return post

    if (type(post["total_count"]) == int) or (type(post["total_count"]) == float) or (type(post["total_count"]) == str):
        try:
            count = int(post["total_count"])
        except:
            #print("cant cast to int")
            #return None # empty if cant cast
            post = None
        else:
            post["total_count"] = count
    else:
        post = None
    return post
    # if type(post["total_count"]) == int:
    #     return post
    # elif type(post["total_count"]) == float:
    #     count = int(float(post["total_count"]))
    #     post["total_count"] = count
    #     return post
    # elif type(post["total_count"]) == str:
    #     try:
    #         count = int(float(post["total_count"]))
    #     except ValueError as e:
    #         #del post
    #         print("cant cast from str to int")
    #         return None
    #     else:
    #         post["total_count"] = count
    #         return post
    # else:
    #     return None

# part 9
def split_tags(post):
    if "tags" not in post:
        return post

    l = []
    for i in post["tags"]:
        words = i.split()
        if len(words)>1:
            for w in words:
                l.append(w)
        else:
            l.append(i)
    post["tags"] = l
    return post

# part 4
def valid_json(line):
    try:
        valid = json.loads(line)
    except:
        #print("invalid json dictionaries")
        return None
    else:
        return True

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-i",required=True, help="input json file")
    parser.add_argument("-o",required=True, help="output json file")

    args = parser.parse_args()
    input = args.i # full path file
    output = args.o
    if osp.exists(input):
        with open(input, 'r') as file:
            d = []
            for line in file:
                valid = valid_json(line)
                if (valid==True):
                    post = json.loads(line)
                    if format_title(post)==None or remove_author(post)==None or total_count_to_int(post)==None:
                        post=None
                    else:
                        createdat_to_utc(post)
                        total_count_to_int(post)
                        split_tags(post)
                    if (post!=None):
                        d.append(post)
            # for line in file:

            #     try:
            #         valid = json.loads(line)
            #     except:
            #         del line
            #     else:
            #         if format_title(valid)==None or remove_author(valid)==None or total_count_to_int(valid)==None:
            #             valid=None
            #         else:
            #             createdat_to_utc(valid)
            #             total_count_to_int(valid)
            #             split_tags(valid)
            #         if (valid!=None):
            #             d.append(valid)
            #         #print(d)
            #print(len(d))
            with open(output,'w') as out:
                for items in d:
                    json.dump(items,out)
                    out.write("\n")
    else:
        print("Error: input file does not exist")

if __name__ == "__main__":
    main()


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

from pydantic import BaseModel
from db import Base
from db import ENGINE
from db import session
from model import NogizakaMemberTable

import urllib.request
from urllib.parse import urljoin
from bs4 import BeautifulSoup
import sys

base_url = 'http://www.nogizaka46.com/member/'

def get_html():
    req = urllib.request.Request(base_url, headers={'User-Agent': 'Mozilla/5.0'})
    return urllib.request.urlopen(req).read()

def namelist():
    ret = []
    html = get_html()
    soup = BeautifulSoup(html, "html.parser")
    units = soup.find_all("div", {"class": "unit"})
    for unit in units:
        if unit.find('span',  {"class": "main"}) is not None:
            kanji = unit.find('span',  {"class": "main"}).string
            kana = unit.find('span',  {"class": "sub"}).string if unit.find('span',  {"class": "sub"}) is not None else unit.find('span',  {"class": "sub2"}).string
            url = urljoin(base_url, unit.find('a')['href'])
            req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
            next_html = urllib.request.urlopen(req).read()
            soup2 = BeautifulSoup(next_html, "html.parser")
            birthday = soup2.find_all("dd")[0].string
            ret.append({
                "name" : kanji,
                "name_kana" : kana,
                "birthday" : birthday,
            })
    return ret

def main():
    session.execute('truncate table {}'.format(NogizakaMemberTable.__table__))
    ret = namelist()
    for elem in ret:
        print(elem)
        member = NogizakaMemberTable()
        member.name = elem["name"]
        member.namekana = elem["name_kana"]
        member.birthday = elem["birthday"]
        session.add(member)
        session.commit()

if __name__ == '__main__':
    main()


'''
Original provided by

 * @author [Zizhao Zhang]
 * @email [zizhao@cise.ufl.edu]
 * @create date 2017-05-25 02:21:13
 * @modify date 2017-05-25 02:21:13
 * @desc [description]
'''
import os
import logging
#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'  # FATAL
#logging.getLogger('tensorflow').setLevel(logging.FATAL)

from tensorflow.keras import models
from tensorflow.keras import layers
from pathlib import Path
import urllib.request
import h5py

weights_url = "https://github.com/fabrylab/Deformation_Cytometer/releases/download/v0.4/ImmuneNIH_20x_edgeOnly_2020_11_24.h5"

def get_store_path(url):
    url = Path(url)
    return Path(__file__).parent / url.parent.name / url.name

store_path = get_store_path(weights_url)

class UNet(models.Model):
    def __init__(self, img_shape, num_class, d=32, weights=weights_url):
        concat_axis = 3
        inputs = layers.Input(shape=img_shape)
        self.shape = img_shape

        conv1 = layers.Conv2D(d, (3, 3), activation='relu', padding='same', name='conv1_1')(inputs)
        conv1 = layers.Conv2D(d, (3, 3), activation='relu', padding='same')(conv1)
        pool1 = layers.MaxPooling2D(pool_size=(2, 2))(conv1)
        conv2 = layers.Conv2D(d * 2, (3, 3), activation='relu', padding='same')(pool1)
        conv2 = layers.Conv2D(d * 2, (3, 3), activation='relu', padding='same')(conv2)
        pool2 = layers.MaxPooling2D(pool_size=(2, 2))(conv2)

        conv3 = layers.Conv2D(d * 4, (3, 3), activation='relu', padding='same')(pool2)
        conv3 = layers.Conv2D(d * 4, (3, 3), activation='relu', padding='same')(conv3)
        pool3 = layers.MaxPooling2D(pool_size=(2, 2))(conv3)

        conv4 = layers.Conv2D(d * 8, (3, 3), activation='relu', padding='same')(pool3)
        conv4 = layers.Conv2D(d * 8, (3, 3), activation='relu', padding='same')(conv4)
        pool4 = layers.MaxPooling2D(pool_size=(2, 2))(conv4)

        conv5 = layers.Conv2D(d * 16, (3, 3), activation='relu', padding='same')(pool4)
        conv5 = layers.Conv2D(d * 16, (3, 3), activation='relu', padding='same')(conv5)

        up_conv5 = layers.UpSampling2D(size=(2, 2))(conv5)
        ch, cw = self.get_crop_shape(conv4, up_conv5)
        crop_conv4 = layers.Cropping2D(cropping=(ch, cw))(conv4)
        up6 = layers.concatenate([up_conv5, crop_conv4], axis=concat_axis)
        conv6 = layers.Conv2D(d * 8, (3, 3), activation='relu', padding='same')(up6)
        conv6 = layers.Conv2D(d * 8, (3, 3), activation='relu', padding='same')(conv6)

        up_conv6 = layers.UpSampling2D(size=(2, 2))(conv6)
        ch, cw = self.get_crop_shape(conv3, up_conv6)
        crop_conv3 = layers.Cropping2D(cropping=(ch, cw))(conv3)
        up7 = layers.concatenate([up_conv6, crop_conv3], axis=concat_axis)
        conv7 = layers.Conv2D(d * 4, (3, 3), activation='relu', padding='same')(up7)
        conv7 = layers.Conv2D(d * 4, (3, 3), activation='relu', padding='same')(conv7)

        up_conv7 = layers.UpSampling2D(size=(2, 2))(conv7)
        ch, cw = self.get_crop_shape(conv2, up_conv7)
        crop_conv2 = layers.Cropping2D(cropping=(ch, cw))(conv2)
        up8 = layers.concatenate([up_conv7, crop_conv2], axis=concat_axis)
        conv8 = layers.Conv2D(d * 2, (3, 3), activation='relu', padding='same')(up8)
        conv8 = layers.Conv2D(d * 2, (3, 3), activation='relu', padding='same')(conv8)

        up_conv8 = layers.UpSampling2D(size=(2, 2))(conv8)
        ch, cw = self.get_crop_shape(conv1, up_conv8)
        crop_conv1 = layers.Cropping2D(cropping=(ch, cw))(conv1)
        up9 = layers.concatenate([up_conv8, crop_conv1], axis=concat_axis)
        conv9 = layers.Conv2D(d, (3, 3), activation='relu', padding='same')(up9)
        conv9 = layers.Conv2D(d, (3, 3), activation='relu', padding='same')(conv9)

        ch, cw = self.get_crop_shape(inputs, conv9)
        conv9 = layers.ZeroPadding2D(padding=((ch[0], ch[1]), (cw[0], cw[1])))(conv9)
        conv10 = layers.Conv2D(num_class, (1, 1), activation="sigmoid")(conv9)

        super().__init__(inputs=inputs, outputs=conv10)

        if weights is not None:
            self.load_weight_file(weights)

    def get_crop_shape(self, target, refer):
        # width, the 3rd dimension
        cw = (target.get_shape()[2] - refer.get_shape()[2])
        assert (cw >= 0)
        if cw % 2 != 0:
            cw1, cw2 = int(cw/2), int(cw/2) + 1
        else:
            cw1, cw2 = int(cw/2), int(cw/2)
        # height, the 2nd dimension
        ch = (target.get_shape()[1] - refer.get_shape()[1])
        assert (ch >= 0)
        if ch % 2 != 0:
            ch1, ch2 = int(ch/2), int(ch/2) + 1
        else:
            ch1, ch2 = int(ch/2), int(ch/2)

        return (ch1, ch2), (cw1, cw2)

    def load_weight_file(self, url):
        # load from a local file or an url (cached)
        if str(url).startswith("http"):
            filename = get_store_path(url)
            if not filename.exists():
                filename.parent.mkdir(exist_ok=True)
                urllib.request.urlretrieve(str(url), filename)
            self.load_weights(str(filename))
        else:
            self.load_weights(str(url))


import matplotlib.pyplot as plt

x1 = [1,2,3,4,5]
y1 = [1,2,4,8,16]

x2 = [1,2,3,4,5]
y2 = [1,3,9,13,16]

x3 = [1,2,3,4,5]
y3 = [2,4,6,8,10]

plt.plot(x1, y1, 'ro--', label='students')
plt.plot(x2, y2, 'bo-', label='grades')
plt.plot(x3, y3, 'go-', label='visitors')
plt.legend(loc='best')

plt.title('Title')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.show()


from setuptools import find_packages, setup

from aurora import __version__

with open('README.md') as f:
    long_description = f.read()

setup(name='aurora',
      version=__version__,
      packages=find_packages(),
      install_requires=[
          'jinja2>=2.10',
          'mistune>=0.8.4',
          'docopt>=0.6.2',
      ],
      entry_points={
          'console_scripts': ['aurora = aurora.cli:cli']
      },
      author='Thomas Ip',
      author_email='hkmp7tommy@gmail.com',
      description='Aurora is a minimalist, blog-aware static site generator.',
      long_description=long_description,
      long_description_content_type='text/markdown',
      license='MIT',
      keywords='blog static-site-generator jinja2 markdown',
      url='https://github.com/tommyip/aurora',
      classifiers=[
          'Development Status :: 3 - Alpha',
          'Environment :: Console',
          'License :: OSI Approved :: MIT License',
          'Operating System :: OS Independent',
          'Programming Language :: Python :: 3',
      ])


from tkinter import *
from PIL import Image, ImageTk

root = Tk()
root.title("Daniel")
root.iconbitmap('c:/users/gusta/star.ico')

photo = ImageTk.PhotoImage(Image.open("c:/users/gusta/icon.png"))

root.mainloop()


import rhythmdb, rb
try:
  import dbus
  use_gossip = True
except ImportError:
  use_gossip = False
try:
  import empathy
  use_empathy = True
  empathy_idle = empathy.Idle ()
except ImportError:
  use_empathy = False

NORMAL_SONG_ARTIST = 'artist'
NORMAL_SONG_TITLE  = 'title'
NORMAL_SONG_ALBUM  = 'album'
STREAM_SONG_ARTIST = 'rb:stream-song-artist'
STREAM_SONG_TITLE  = 'rb:stream-song-title'
STREAM_SONG_ALBUM  = 'rb:stream-song-album'

BUS_NAME = 'org.gnome.Gossip'
OBJ_PATH = '/org/gnome/Gossip'
IFACE_NAME = 'org.gnome.Gossip'

class IMStatusPlugin (rb.Plugin):
  def __init__ (self):
    rb.Plugin.__init__ (self)

  def activate (self, shell):
    self.shell = shell
    sp = shell.get_player ()
    self.psc_id  = sp.connect ('playing-song-changed',
                               self.playing_entry_changed)
    self.pc_id   = sp.connect ('playing-changed',
                               self.playing_changed)
    self.pspc_id = sp.connect ('playing-song-property-changed',
                               self.playing_song_property_changed)

    self.current_entry = None
    self.current_artist = None
    self.current_title = None
    self.current_album = None

    self.save_status ()

    if sp.get_playing ():
      self.set_entry (sp.get_playing_entry ())

  def deactivate (self, shell):
    self.shell = None
    sp = shell.get_player ()
    sp.disconnect (self.psc_id)
    sp.disconnect (self.pc_id)
    sp.disconnect (self.pspc_id)

    if self.current_entry is not None:
      self.restore_status ()

  def playing_changed (self, sp, playing):
    if playing:
      self.set_entry (sp.get_playing_entry ())
    else:
      self.current_entry = None
      self.restore_status ()

  def playing_entry_changed (self, sp, entry):
    if sp.get_playing ():
      self.set_entry (entry)

  def playing_song_property_changed (self, sp, uri, property, old, new):
    relevant = False
    if sp.get_playing () and property in (NORMAL_SONG_ARTIST, STREAM_SONG_ARTIST):
      self.current_artist = new
      relevant = True
    elif sp.get_playing () and property in (NORMAL_SONG_TITLE, STREAM_SONG_TITLE):
      self.current_title = new
      relevant = True
    elif sp.get_playing () and property in (NORMAL_SONG_ALBUM, STREAM_SONG_ALBUM):
      self.current_album = new
      relevant = True

    if relevant:
      self.set_status ()

  def set_entry (self, entry):
    if entry == self.current_entry:
      return

    if self.current_entry == None:
      self.save_status ()
    self.current_entry = entry

    if entry is None:
      self.restore_status ()
      return

    self.set_status_from_entry ()

  def set_status_from_entry (self):
    db = self.shell.get_property ("db")
    self.current_artist = db.entry_get (self.current_entry, rhythmdb.PROP_ARTIST)
    self.current_title  = db.entry_get (self.current_entry, rhythmdb.PROP_TITLE)
    self.current_album  = db.entry_get (self.current_entry, rhythmdb.PROP_ALBUM)

    if self.current_entry.get_entry_type().category == rhythmdb.ENTRY_STREAM:
      if not self.current_artist:
        self.current_artist = db.entry_request_extra_metadata (self.current_entry, STREAM_SONG_ARTIST)
      if not self.current_title:
        self.current_title  = db.entry_request_extra_metadata (self.current_entry, STREAM_SONG_TITLE)
      if not self.current_album:
        self.current_album  = db.entry_request_extra_metadata (self.current_entry, STREAM_SONG_ALBUM)

    self.set_status ()

  def set_status (self):
    subs = {
        'artist': self.current_artist,
        'album': self.current_album,
        'title': self.current_title
    }
    if self.current_artist:
      if self.current_title:
        # Translators: do not translate %(artist)s or %(title)s, they are
        # string substitution markers (like %s) for the artist and title of
        # the current playing song.  They can be reordered if necessary.
        new_status = _(u"♫ %(artist)s - %(title)s ♫") % subs
      elif self.current_album:
        # Translators: do not translate %(artist)s or %(album)s, they are
        # string substitution markers (like %s) for the artist and album name
        # of the current playing song.  They can be reordered if necessary.
        new_status = _(u"♫ %(artist)s - %(album)s ♫") % subs
    elif self.current_album:
      # Translators: do not translate %(album)s, it is a string substitution
      # marker (like %s) for the album name of the current playing song.
      new_status = _(u"♫ %(album)s ♫") % subs
    elif self.current_title:
      # Translators: do not translate %(title)s, it is a string substitution
      # marker (like %s) for the title of the current playing song.
      new_status = _(u"♫ %(title)s ♫") % subs
    else:
      new_status = _(u"♫ Listening to music... ♫")

    self.set_gossip_status (new_status)
    self.set_empathy_status (new_status)

  def save_status (self):
    self.saved_gossip = self.get_gossip_status ()
    self.saved_empathy = self.get_empathy_status ()

  def restore_status (self):
    if self.saved_gossip != None:
      self.set_gossip_status (self.saved_gossip)
    if self.saved_empathy != None:
      self.set_empathy_status (self.saved_empathy)

  def set_gossip_status (self, new_status):
    if not use_gossip:
      return

    try:
      bus = dbus.SessionBus ()
      gossip_obj = bus.get_object (BUS_NAME, OBJ_PATH)
      gossip = dbus.Interface (gossip_obj, IFACE_NAME)

      state, status = gossip.GetPresence ("")
      gossip.SetPresence (state, new_status)
    except dbus.DBusException:
      pass

  def get_gossip_status (self):
    if not use_gossip:
      return

    try:
      bus = dbus.SessionBus ()
      gossip_obj = bus.get_object (BUS_NAME, OBJ_PATH)
      gossip = dbus.Interface (gossip_obj, IFACE_NAME)

      state, status = gossip.GetPresence ("")
      return status
    except dbus.DBusException:
      return None

  def set_empathy_status (self, new_status):
    if not use_empathy:
      return

    empathy_idle.set_status (new_status)

  def get_empathy_status (self):
    if not use_empathy:
      return

    return empathy_idle.get_status ()


import time
from typing import List

from httpx import AsyncClient
from telegram import Update
from telegram.constants import ParseMode
from telegram.ext import ContextTypes

from Exon import StartTime, exon
from Exon.modules.disable import DisableAbleCommandHandler
from Exon.modules.helper_funcs.chat_status import check_admin

sites_list = {
    "ᴛᴇʟᴇɢʀᴀᴍ": "https://api.telegram.org",
    "ᴋᴀɪᴢᴏᴋᴜ": "https://animekaizoku.com",
    "ᴋᴀʏᴏ": "https://animekayo.com",
    "ᴊɪᴋᴀɴ": "https://api.jikan.moe/v3",
}

def get_readable_time(seconds: int) -> str:
    count = 0
    ping_time = ""
    time_list = []
    time_suffix_list = ["s", "ᴍ", "ʜ", "ᴅᴀʏs"]

    while count < 4:
        count += 1
        if count < 3:
            remainder, result = divmod(seconds, 60)
        else:
            remainder, result = divmod(seconds, 24)
        if seconds == 0 and remainder == 0:
            break
        time_list.append(int(result))
        seconds = int(remainder)

    for x in range(len(time_list)):
        time_list[x] = str(time_list[x]) + time_suffix_list[x]
    if len(time_list) == 4:
        ping_time += time_list.pop() + ", "

    time_list.reverse()
    ping_time += ":".join(time_list)

    return ping_time

async def ping_func(to_ping: List[str]) -> List[str]:
    ping_result = []

    for each_ping in to_ping:
        start_time = time.time()
        site_to_ping = sites_list[each_ping]
        async with AsyncClient() as client:
            r = await client.get(site_to_ping)
        end_time = time.time()
        ping_time = str(round((end_time - start_time), 2)) + "s"

        pinged_site = f"{each_ping}"

        if each_ping == "ᴋᴀɪᴢᴏᴋᴜ" or each_ping == "Kayo":
            pinged_site = f'{each_ping}'
            ping_time = f"{ping_time} (Status: {r.status_code})"

        ping_text = f"{pinged_site}: {ping_time}"
        ping_result.append(ping_text)

    return ping_result

async def ping(update: Update, context: ContextTypes.DEFAULT_TYPE):
    msg = update.effective_message

    start_time = time.time()
    message = await msg.reply_text("⚡")
    end_time = time.time()
    telegram_ping = str(round((end_time - start_time) * 1000, 3)) + " ms"
    uptime = get_readable_time((time.time() - StartTime))

    await message.edit_text(
        "𝗣𝗢𝗡𝗚 🥀!!\n"
        "ᴛɪᴍᴇ ᴛᴀᴋᴇɴ: {}\n"
        "sᴇʀᴠɪᴄᴇ ᴜᴘᴛɪᴍᴇ: {}".format(telegram_ping, uptime),
        parse_mode=ParseMode.HTML,
    )

@check_admin(only_sudo=True)
async def pingall(update: Update, context: ContextTypes.DEFAULT_TYPE):
    to_ping = ["ᴋᴀɪᴢᴏᴋᴜ", "ᴋᴀʏᴏ", "ᴛᴇʟᴇɢʀᴀᴍ", "ᴊɪᴋᴀɴ"]
    pinged_list = await ping_func(to_ping)
    pinged_list.insert(2, "")
    uptime = get_readable_time((time.time() - StartTime))

    reply_msg = "⏱ᴘɪɴɢ ʀᴇsᴜʟᴛs ᴀʀᴇ:\n"
    reply_msg += "\n".join(pinged_list)
    reply_msg += "\nsᴇʀᴠɪᴄᴇ ᴜᴘᴛɪᴍᴇ: {}".format(uptime)

    await update.effective_message.reply_text(
        reply_msg,
        parse_mode=ParseMode.HTML,
        disable_web_page_preview=True,
    )

PING_HANDLER = DisableAbleCommandHandler("ping", ping)
PINGALL_HANDLER = DisableAbleCommandHandler("pingall", pingall)

exon.add_handler(PING_HANDLER)
exon.add_handler(PINGALL_HANDLER)

__command_list__ = ["ping", "pingall"]
__handlers__ = [PING_HANDLER, PINGALL_HANDLER]


import os
import base64
import json
import uuid
import functions_framework
from google.api_core.client_options import ClientOptions
from google.cloud import language_v1
from google.cloud import pubsub_v1

@functions_framework.cloud_event
# receive new text events from pub sub
def new_text(cloud_event):
    debugx = False
    if os.environ.get('DEBUGX') == "1":
        debugx = True

    msg_content = base64.b64decode(cloud_event.data["message"]["data"]).decode()
    json_data = json.loads(msg_content)

    if "eid" in json_data:
        eid = json_data['eid']
    else:
        eid = uuid.uuid4().hex

    if debugx:
        print(f"DEBUGX:" + eid + ":" + msg_content)

    extract_syntax(msg_content, eid)

# extract word tokens and filter by parts of speech
def extract_syntax(msg_content, eid):
    debugx = False
    if os.environ.get('DEBUGX') == "1":
        debugx = True

    client = language_v1.LanguageServiceClient()
    type_ = language_v1.Document.Type.PLAIN_TEXT

    if debugx:
        print(f"DEBUGX:{eid}:pubsub data {msg_content}")

    json_data = json.loads(msg_content)

    language = "en"
    document = {"content": json_data['extracted_text'], "type_": type_, "language": language}

    encoding_type = language_v1.EncodingType.UTF8

    response = client.analyze_syntax(
        request={"document": document, "encoding_type": encoding_type}
    )

    # Extract the categories we want
    useful_parts_of_speech = ["NOUN", "ADJ", "VERB"]

    # An optional filter of words can also be implemented here
    extracted_words = ""
    for token in response.tokens:
        text = token.text
        part_of_speech = token.part_of_speech
        if language_v1.PartOfSpeech.Tag(part_of_speech.tag).name in useful_parts_of_speech:
            if extracted_words == "":
                extracted_words = extracted_words + text.content
            else:
                extracted_words = extracted_words + " " + text.content

    if debugx:
        print(f"DEBUGX:{eid}: Extracted words: {extracted_words}")

    publish_text(json_data['bucket'], json_data['file_name'], extracted_words, eid)

# Publish the extracted words to pub sub
def publish_text(bucket_name: str, file_name: str, syntax_data, eid):
    debugx = False
    if os.environ.get('DEBUGX') == "1":
        debugx = True

    project_id = os.environ.get('PROJECT_ID')
    topic_id = os.environ.get('TOPIC_ID')

    publisher = pubsub_v1.PublisherClient()
    topic_path = publisher.topic_path(project_id, topic_id)

    if debugx:
        print(f"Debug:{eid}: Topic path: {topic_path}")
    data = {}
    data['bucket'] = bucket_name
    data['file_name'] = file_name
    data['syntax_data'] = syntax_data
    data['eid'] = eid

    json_data = json.dumps(data)
    data = json_data.encode("utf-8")
    if debugx:
        print(f"DEBUGX:" + eid + ":" + "Data sent:" + json_data)

    future = publisher.publish(topic_path, data)
    if debugx:
        print(f"DEBUGX::{eid}: {future.result()}")


import string
import math
import numpy as np
import matplotlib.pyplot as plt

data=open('../results/Test1_2.txt')

for line in data:
    if line=="\n":
        break

data.readline()

index=[]
X1=[]
X2=[]
F=[]
fitVal=[]

for line in data:
    (i,x1,x2,f,fit)=line.split()
    index.append(int(i))
    X1.append(float(x1))
    X2.append(float(x2))
    F.append(math.log10(float(f)))
    fitVal.append(float(fit))

plt.plot(index,X1,label="X1")
plt.plot(index,X2,label="X2")
plt.xlabel("Iteration Step")
plt.ylabel("X")
plt.legend()
plt.plot()
plt.savefig('../pic/T1-2_res_x1_x2.png')
plt.close()

plt.plot(index,F)
plt.xlabel("Iteration Step")
plt.ylabel("log(F(x1,x2))")
plt.plot()
plt.savefig('../pic/T1-2_res_f.png')
plt.close()

Multi-objective Optimization in Fluid Mechanics/script/t1-2_draw_res.py


from langchain.document_loaders import PyPDFDirectoryLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import Chroma

def process_and_save_vectors():
    # Load PDF documents from a directory
    #PyPDFDirectoryLoader load and split the pdf present in documents into pages

    loader = PyPDFDirectoryLoader("documents")
    pages = loader.load_and_split()
    print(len(pages))
    #RecursiveCharacterTextSplitter split pages into smaller chunks
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=1024, chunk_overlap=20)
    documents = text_splitter.split_documents(pages)
    print(len(documents))

    # Initialize HuggingFaceEmbeddings for sentence embeddings
    embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")

    # Create and persist a Chroma vector database from the documents
    vectordb = Chroma.from_documents(
        documents,
        embeddings,
        persist_directory='./data'
    )
    vectordb.persist()

if __name__ == "__main__":
    process_and_save_vectors()


from functools import lru_cache

import logzero
import numpy as np
import pandas as pd
import pmdarima as pm
from logzero import logger
from pmdarima import arima, model_selection, pipeline
from pmdarima import preprocessing as ppc

# Connect to logzero log file
log_path = "logs/"
log_file = "dashboard_app.log"
logzero.logfile(
    log_path + log_file,
    maxBytes=1e5,
    backupCount=1,
    disableStderrLogger=True,
)
logger.info(f"pmd_tools logger initialized")

@lru_cache(maxsize=64)
def get_train_test(df, feature, test_size):
    """Produce and return time-series friendly train and test dataframes"""

    columns = df.columns.tolist()

    train, test = model_selection.train_test_split(df, test_size=test_periods)

    train = pd.DataFrame(train, index=df[: len(train)].index, columns=columns)
    test = pd.DataFrame(test, index=df[len(train) :].index, columns=columns)

    return train, test

def get_arima_fft_model(train, fc_periods):

    autoarima = arima.AutoARIMA(
        stepwise=True,
        trace=1,
        seasonal=False,  # because we use Fourier
        intercept="auto",
        suppress_warnings=True,
        error_action="ignore",
        random_state=42,
        m=1,
        start_p=1,
        start_q=1,
        max_p=3,
        max_q=4,
    )

    pipe = pipeline.Pipeline(
        [
            ("fourier", ppc.FourierFeaturizer(m=12, k=2)),
            ("arima", autoarima),
        ]
    )

    pipe.fit(train)

    return pipe

def get_arima_auto_model(train, fc_periods):

    model = pm.AutoARIMA(
        stepwise=True,
        trace=1,
        seasonal=True,
        intercept="auto",
        suppress_warnings=True,
        error_action="ignore",
        random_state=42,
        m=12,
        start_p=1,
        start_q=1,
        max_p=3,
        max_q=3,
        start_P=1,
        start_Q=1,
        max_P=2,
        max_Q=2,
        # start_D=0,
        # max_D=2,
    )

    model.fit(train)

    return model


import os
import time
import warnings
warnings.filterwarnings('ignore')

import torch
import cv2
from tqdm import tqdm

from openpose.body.estimator import BodyPoseEstimator
from openpose.utils import draw_body_connections, draw_keypoints

def get_img_paths(img_folder):
    image_names = [x for x in os.listdir(img_folder) if x.endswith('.jpg') or x.endswith('.jpeg')]
    image_paths = [
        os.path.join(img_folder, image)
        for image in image_names
        if image.endswith('.jpg') or image.endswith('.jpeg')
    ]
    return image_paths, image_names

def load_img(path):
    return cv2.imread(path)

def save_img(img, path):
    cv2.imwrite(path, img)

def main(input_folder, output_folder, use_cuda=False):

    io_time = 0
    compute_time = 0
    other_time = 0

    model_download_time = time.time()
    estimator = BodyPoseEstimator(pretrained=True, use_cuda=use_cuda)
    model_download_time = time.time() - model_download_time

    other_t = time.time()
    image_paths, image_names = get_img_paths(input_folder)
    other_time += time.time() - other_t

    print(f'Processing {len(image_names)} images using {"GPU" if use_cuda else "CPU"} ...')
    for image_path, image_name in tqdm(zip(image_paths, image_names)):
        io_t = time.time()
        img_input = load_img(image_path)
        io_time += time.time() - io_t

        compute_t = time.time()
        keypoints = estimator(img_input)
        img_output = draw_body_connections(img_input, keypoints, thickness=4, alpha=0.7)
        img_output = draw_keypoints(img_output, keypoints, radius=5, alpha=0.8)
        compute_time += time.time() - compute_t

        io_t = time.time()
        save_path = os.path.join(output_folder, image_name)
        save_img(img_output, save_path)
        io_time += time.time() - io_t

    return other_time, io_time, compute_time

if __name__ == "__main__":
    input_folder = r"C:\Users\Mobin\Desktop\Mobin\eca-dl\data\image\batch32"
    use_cuda = False

    other_time = time.time()
    path, folder = os.path.split(input_folder)
    output_folder = os.path.join(path, folder+'_pose')
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    other_ti = other_time + time.time()
    # print(f'-- other time is: {other_time} seconds')

    other_time, io_time, compute_time = main(input_folder, output_folder, use_cuda)
    print(f'-- other time is: {other_time+other_ti} seconds')
    print(f'-- io time is: {io_time} seconds')
    print(f'-- compute time is: {compute_time} seconds')


from http import HTTPStatus

from src.app import jwt
from src.common.response import BaseResponse
from src.db.redis import redis_client

@jwt.token_in_blocklist_loader
def check_if_token_is_revoked(jwt_header, jwt_payload: dict):
    jti = jwt_payload["jti"]
    token_in_redis = redis_client.get(jti)
    return token_in_redis is not None

@jwt.revoked_token_loader
def revoked_token_callback(jwt_header, jwt_payload: dict):
    return (
        BaseResponse(
            success=False, error={"msg": "The token has been revoked."}
        ).dict(),
        HTTPStatus.NOT_FOUND,
    )

@jwt.needs_fresh_token_loader
def token_not_fresh_callback(jwt_header, jwt_payload):
    return (
        BaseResponse(
            success=False, error={"msg": "The token is not fresh."}
        ).dict(),
        HTTPStatus.UNAUTHORIZED,
    )


import cx_Freeze
import os

os.environ['TCL_LIBRARY'] = r'C:\Users\igrok\AppData\Local\Programs\Python\Python37-32\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\igrok\AppData\Local\Programs\Python\Python37-32\tcl\tk8.6'

base = 'Win32GUI'

tcl = "C:/Users/igrok/AppData/Local/Programs/Python/Python37-32/DLLs/tcl86t.dll"
tk = "C:/Users/igrok/AppData/Local/Programs/Python/Python37-32/DLLs/tk86t.dll"

vcruntime140 = "vcruntime140-32.dll"

packages = ["pygame", "random", "math", "sys", "time"]
include_files = ["img/earth.png", "img/jupiter.png", "img/mars.png", "img/mercury.png",
"img/neptune.png", "img/saturn.png", "img/sun.png", "img/uranus.png", "img/venus.png",
"img/meteor.png", tcl, tk, vcruntime140, "python37-32.dll",
"music/music.mp3", "img/question.png","img/settings_ico.png",
"text/all_text_eng.txt", "text/all_text_ru.txt", "text/hint_hole_eng.txt",
"text/hint_hole_ru.txt", "text/hint_solar_eng.txt", "text/hint_solar_ru.txt",
"text/text_planets_eng.txt", "text/text_planets_ru.txt", "font/font.ttf"]
includes = ["dots", "dot_hole", "functions", "planets", "settings"]

# executables = []

cx_Freeze.setup(
    name = "Black Hole",
    options = {"build_exe": {"packages": packages,
                            "include_files": include_files,
                            "includes" : includes,
                            'include_msvcr': True
                            }},
    executables = [cx_Freeze.Executable("main.py",
                                        base = base,
                                        icon = "ico.ico",
                                        targetName = "Space.exe")],
    version='1.5.0'
)


#!/usr/bin/env python

# System.
import os

# Arguments.
import argparse

# Parser.
from syntaxic import parser

# Helper.
from helper import Helper

class Assembler:
	"""
	Assembles programs.
	"""

	@staticmethod
	def __parse_arguments():
		"""
		Parses the arguments.

		:return: The parsed arguments.
		"""

		# Parse the arguments.
		argument_parser = argparse.ArgumentParser()
		argument_parser.add_argument("file", help="select the input assembly file")
		argument_parser.add_argument("-o", "--output", help="select the output logisim file")
		return vars(argument_parser.parse_args())

	@staticmethod
	def assemble(input_path: str, output_path: str):
		"""
		Assembles a program.

		:param input_path: The input assembly file.
		:param output_path: The output logisim file.
		"""

		# Check the input file.
		if os.path.exists(input_path):

			# Read the assembly file.
			input_file = open(input_path, "r")
			data = input_file.read()
			input_file.close()

			# Write the logisim file.
			output_file = open(output_path, "w")
			output = parser.parse(data)
			output_file.write(output)
			output_file.close()

			# Check the output file.
			if not os.path.exists(output_path):
				print("Assembler error: Error while assembling the program.")

		else:
			print("Assembler error: No such file '{}'.".format(input_path))

	@staticmethod
	def run():
		"""
		Runs the assembler.
		"""

		# Parse the arguments.
		options = Assembler.__parse_arguments()

		# Set the default output path.
		if options["output"] is None:
			options["output"] = Helper.replace_extension(options["file"], "out")

		# Assemble the program.
		Assembler.assemble(options["file"], options["output"])

# Run the assembler.
if __name__ == "__main__":
	Assembler.run()