#!/usr/bin/env python3
#POST request user enumeration tool
#JoaoVitorBF

from multiprocessing import Process, Queue
from statistics import mean
from urllib3 import exceptions as urlexcept
import argparse
import requests
import math

def process_enum(queue, found_queue, wordlist, url, payload, failstr, verbose, proc_id, stop):
    try:
        # Payload to dictionary
        payload_dict = {}
        for load in payload:
            split_load = load.split(":")
            if split_load[1] != '{USER}':
                payload_dict[split_load[0]] = split_load[1]
            else:
                payload_dict[split_load[0]] = '{USER}'

        # Enumeration
        total = len(wordlist)
        for counter, user in enumerate(wordlist):
            user_payload = dict(payload_dict)
            for key, value in user_payload.items():
                if value == '{USER}':
                    user_payload[key] = user
            r = requests.post(url, data=user_payload)
            if failstr not in r.text:
                queue.put((proc_id, "FOUND", user))
                found_queue.put((proc_id, "FOUND", user))
                if stop: break
            elif verbose:
                queue.put((proc_id, "TRIED", user))
            queue.put(("PERCENT", proc_id, (counter/total)*100))
    except (urlexcept.NewConnectionError, requests.exceptions.ConnectionError):
        print("[ATTENTION] Connection error on process {}! Try lowering the amount of threads with the -c parameter.".format(proc_id))

if __name__ == "__main__":
    # Arguments
    parser = argparse.ArgumentParser(description="POST request user enumeration tool")
    parser.add_argument("wordlist", help="username wordlist")
    parser.add_argument("url", help="the URL to send requests to")
    parser.add_argument("payload", nargs='+', help="the POST request payload to send")
    parser.add_argument("failstr", help="failure string to search in the response body")
    parser.add_argument("-c", metavar="cnt", type=int, default=10, help="process (thread) count, default 10, too many processes may cause connection problems")
    parser.add_argument("-v", action="store_true", help="verbose mode")
    parser.add_argument("-s", action="store_true", help="stop on first user found")
    args = parser.parse_args()

    # Arguments to simple variables
    wordlist = args.wordlist
    url = args.url
    payload = args.payload
    verbose = args.v
    thread_count = args.c
    failstr = args.failstr
    stop = args.s

    print(""" __              __   __   __ ___
|_   _       _  |__) /  \ (_   |     _ |_  _   _ |_  _
|__ | ) |_| ||| |    \__/ __)  |    _) |_ (_| |  |_ _)
""")
    print("URL: "+url)
    print("Payload: "+str(payload))
    print("Fail string: "+failstr)
    print("Wordlist: "+wordlist)
    if verbose: print("Verbose mode")
    if stop: print("Will stop on first user found")

    print("Initializing processes...")
    # Distribute wordlist to processes
    wlfile = open(wordlist, "r", encoding="ISO-8859-1")
    tothread = 0
    wllist = [[] for i in range(thread_count)]
    for user in wlfile:
        wllist[tothread-1].append(user.strip())
        if (tothread < thread_count-1):
            tothread+=1
        else:
            tothread = 0

    # Start processes
    tries_q = Queue()
    found_q = Queue()
    processes = []
    percentage = []
    last_percentage = 0
    for i in range(thread_count):
        p = Process(target=process_enum, args=(tries_q, found_q, wllist[i], url, payload, failstr, verbose, i, stop,))
        processes.append(p)
        percentage.append(0)
        p.start()

    print("Processes started successfully! Enumerating.")
    # Main process loop
    initial_count = len(processes)
    while True:
        # Read the process output queue
        try:
            oldest = tries_q.get(False)
            if oldest[0] == 'PERCENT':
                percentage[oldest[1]] = oldest[2]
            elif oldest[1] == 'FOUND':
                print("[{}] FOUND: {}".format(oldest[0], oldest[2]))
            elif verbose:
                print("[{}] Tried: {}".format(oldest[0], oldest[2]))
        except: pass

        # Calculate completion percentage and print if /10
        total_percentage = math.ceil(mean(percentage))
        if total_percentage % 10 == 0 and total_percentage != last_percentage:
            print("{}% complete".format(total_percentage))
            last_percentage = total_percentage

        # Pop dead processes
        for k, p in enumerate(processes):
            if p.is_alive() == False:
                processes.pop(k)

        # Terminate all processes if -s flag is present
        if len(processes) < initial_count and stop:
            for p in processes:
                p.terminate()

        # Print results and terminate self if finished
        if len(processes) == 0:
            print("EnumPOST finished, and these usernames were found:")
            while True:
                try:
                    entry = found_q.get(False)
                    print("[{}] FOUND: {}".format(entry[0], entry[2]))
                except:
                    break
            quit()


from re import A
from turtle import position
import numpy as np
import cv2
from agent import AgentPictureInfo, PlayerInfo, PlayerInfoAndImages
import detectors
import loader

from tesserocr import PyTessBaseAPI, PSM, OEM

# TOP HUD CONSTANTS
# Detection Confidence Threshold for Agents
AGENT_CONF_THRESHOLD = 0.5
AGENT_SQDIFF_THRESHOLD = 0.15
FIRST_LEFT_AGENT_POS = np.array([710, 30])
AGENT_SPACING = np.array([66, 0])
HEALTH_BAR_Y = 76
HEALTH_BAR_WIDTH_HEIGHT = np.array([40, 6])
# W x H
AGENT_IMAGE_SIZE = np.array([40, 40])
# Health bar can be white or red (in BGR format)
HEALTH_BAR_COLORS = np.array([[250, 250, 250], [91, 96, 241]], dtype=np.uint8)

# SCOREBOARD CONSTANTS
SCOREBOARD_X_BOUNDS = np.array([570, 572+38])
SCOREBOARD_Y_BOUNDS = np.array([186, 875])
SCOREBOARD_AGENT_IMAGE_SIZE = np.array([33, 33])
SCOREBOARD_AGENT_SPACING = np.array([0, 34])
SCOREBOARD_FIRST_BLUE_AGENT_POS = np.array([573, 407])
SCOREBOARD_FIRST_RED_AGENT_POS = np.array([571, 638])
SCOREBOARD_ENTRY_HEIGHT = 33
USERNAME_X_BOUNDS = np.array([643, 808])
ULTIMATE_X_BOUNDS = np.array([813, 932])
KILLS_X_BOUNDS = np.array([934, 973])
DEATHS_X_BOUNDS = np.array([973, 1012])
ASSISTS_X_BOUNDS = np.array([1012, 1051])
CREDITS_X_BOUNDS = np.array([1205, 1287])

SYMBOL_IMAGE_SIZE = np.array([40, 34])
SYMBOL_CONF_THRESHOLD = 0.8

DEBUG_TEXT_OPTIONS = {
    "fontFace": cv2.FONT_HERSHEY_PLAIN,
    "fontScale": 0.8,
    "thickness": 1,
    "lineType": cv2.LINE_AA,
}
DEBUG_TEXT_OUTLINE_OPTIONS = {
    "fontFace": cv2.FONT_HERSHEY_PLAIN,
    "fontScale": 0.8,
    "thickness": 2,
    "color": (0, 0, 0),
    "lineType": cv2.LINE_AA,
}

def outlined_text(im, text, position, color):
    im = cv2.putText(im, text, position, **DEBUG_TEXT_OUTLINE_OPTIONS)
    im = cv2.putText(im, text, position, color=color, **DEBUG_TEXT_OPTIONS)
    return im

def get_debug_image(
    im,
    player_info_and_images: list[PlayerInfoAndImages]
):
    im = np.ascontiguousarray(im)
    health_offset = np.array([0, 65])
    scoreboard_y_text_offset = np.array([0, 10])
    scoreboard_text_offset = np.array([125, 0])
    colors_for_team = {"red": (0, 0, 255), "blue": (255, 0, 0)}
    for info in player_info_and_images:
        top_hud_picture = info.top_hud_picture
        scoreboard_picture = info.scoreboard_picture
        player_info = info.player
        color = colors_for_team[player_info.team]
        if top_hud_picture is not None:
            # TOP HUD INFORMATION
            im = cv2.rectangle(
                im,
                top_hud_picture.image_rect[0],
                top_hud_picture.image_rect[1],
                color,
            )
            im = outlined_text(
                im,
                player_info.agent_name,
                top_hud_picture.image_rect[0],
                color,
            )
            im = outlined_text(
                im,
                f"hp: {str(player_info.health)}",
                top_hud_picture.image_rect[0] + health_offset,
                color=color,
            )
        # SCOREBOARD INFORMATION
        im = cv2.rectangle(
            im,
            scoreboard_picture.image_rect[0],
            scoreboard_picture.image_rect[1],
            color,
        )

        im = outlined_text(
            im,
            player_info.username,
            scoreboard_picture.image_rect[0] + scoreboard_y_text_offset,
            color=color,
        )
        im = outlined_text(
            im,
            scoreboard_picture.agent_name,
            scoreboard_picture.image_rect[0] + scoreboard_y_text_offset * 2,
            color=color,
        )
        im = outlined_text(
            im,
            f"Spike: {player_info.spike_status}",
            scoreboard_picture.image_rect[0] + scoreboard_y_text_offset * 3,
            color=color,
        )
        im = outlined_text(
            im,
            f"ultimate: {str(player_info.ultimate_status)}",
            scoreboard_picture.image_rect[0]
            + scoreboard_y_text_offset
            + scoreboard_text_offset * 2,
            color=color,
        )
        im = outlined_text(
            im,
            f"kda: {str(player_info.kills)}/{str(player_info.deaths)}/{str(player_info.assists)}",
            scoreboard_picture.image_rect[0]
            + scoreboard_y_text_offset
            + scoreboard_text_offset * 3,
            color=color,
        )
        im = outlined_text(
            im,
            f"credits: {str(player_info.credits)}",
            scoreboard_picture.image_rect[0]
            + scoreboard_y_text_offset
            + scoreboard_text_offset * 5,
            color=color,
        )
    return im

def load_icons(icon_directory, symbol_directory):
    # Use hashes for agent icons in order to speed up matching
    left_icons, right_icons = loader.load_left_right_icons(icon_directory, AGENT_IMAGE_SIZE)
    scoreboard_icons = loader.hash_icons(
        loader.load_icons(icon_directory, SCOREBOARD_AGENT_IMAGE_SIZE)
    )

    spike_icon = loader.load_icons(symbol_directory + "/Spike_icon.png", SYMBOL_IMAGE_SIZE)["Spike"]
    chicken_icon = loader.load_icons(symbol_directory + "/Chickens_text.png", None)["Chickens"]
    symbol_icons = {"Spike" : spike_icon, "Chickens": chicken_icon}
    return left_icons, right_icons, scoreboard_icons, symbol_icons

def get_top_hud_positions(top_hud, left_icons, right_icons):
    _, w, _ = top_hud.shape
    FIRST_RIGHT_AGENT_POS = (
        w - FIRST_LEFT_AGENT_POS[0] - AGENT_IMAGE_SIZE[0] + 1,
        FIRST_LEFT_AGENT_POS[1],
    )
    image_agent_infos = detectors.detect_all_agents(
        top_hud,
        left_icons,
        right_icons,
        FIRST_LEFT_AGENT_POS,
        FIRST_RIGHT_AGENT_POS,
        -AGENT_SPACING,
        AGENT_SPACING,
        AGENT_IMAGE_SIZE,
        AGENT_CONF_THRESHOLD,
    )
    return image_agent_infos

def get_healths(
    top_hud, agent_picture_infos
) -> tuple[list[AgentPictureInfo], list[int]]:
    agent_healths = detectors.detect_health_bar(
        top_hud,
        agent_picture_infos,
        HEALTH_BAR_Y,
        HEALTH_BAR_WIDTH_HEIGHT,
        HEALTH_BAR_COLORS,
        DEBUG=False,
    )
    return agent_healths

def get_scoreboard_positions(im, icons):
    scoreboard = im[
        SCOREBOARD_Y_BOUNDS[0] : SCOREBOARD_Y_BOUNDS[1],
        SCOREBOARD_X_BOUNDS[0] : SCOREBOARD_X_BOUNDS[1],
        :,
    ]
    agent_pictures = detectors.find_all_agents(
        scoreboard,
        icons,
        SCOREBOARD_AGENT_IMAGE_SIZE,
        AGENT_SQDIFF_THRESHOLD,
        SCOREBOARD_X_BOUNDS,
        SCOREBOARD_Y_BOUNDS,
    )
    return agent_pictures

def update_scoreboard_agent_pictures(im, icons, past_agent_pictures: list[AgentPictureInfo]):
    positions = []
    for agent_picture in past_agent_pictures:
        positions.append(agent_picture.image_rect)

    agent_pictures = detectors.detect_agent_at_positions_on_team(
        im,
        icons,
        positions,
        0.25,
        past_agent_pictures,
    )
    return agent_pictures

def get_scoreboard_positions_static(im, icons):
    agent_pictures = detectors.detect_all_agents(
        im,
        icons,
        icons,
        SCOREBOARD_FIRST_BLUE_AGENT_POS,
        SCOREBOARD_FIRST_RED_AGENT_POS,
        SCOREBOARD_AGENT_SPACING,
        SCOREBOARD_AGENT_SPACING,
        SCOREBOARD_AGENT_IMAGE_SIZE,
        AGENT_CONF_THRESHOLD,
        # DEBUG=True
    )
    return agent_pictures

def scoreboard_detectors(im, tesseract, agent_pictures, spike_icon, chickens):
    return detectors.detect_scoreboard(
        im,
        tesseract,
        agent_pictures,
        spike_icon,
        chickens,
        SYMBOL_CONF_THRESHOLD,
        SCOREBOARD_ENTRY_HEIGHT,
        USERNAME_X_BOUNDS,
        ULTIMATE_X_BOUNDS,
        KILLS_X_BOUNDS,
        DEATHS_X_BOUNDS,
        ASSISTS_X_BOUNDS,
        CREDITS_X_BOUNDS,
    )

def match_scoreboard_and_top_hud_data(
    scoreboard_agent_pictures, scoreboard_infos, top_hud_agent_pictures, agent_healths
):
    # Copy these lists because we are going to mutate them
    agent_healths = list(agent_healths)
    top_hud_agent_pictures = list(top_hud_agent_pictures)

    matched_pictures = dict()
    # Match agents from top hud to scoreboard
    for i, (agent_picture_scoreboard, board_row) in enumerate(
        zip(scoreboard_agent_pictures, scoreboard_infos)
    ):
        match_index = next(
            (
                i
                for i, x in enumerate(top_hud_agent_pictures)
                if (
                    x.agent_name == agent_picture_scoreboard.agent_name
                    and x.team == agent_picture_scoreboard.team
                )
            ),
            None,
        )
        # assert (
        #     match_index is not None
        # ), f"{agent_picture_scoreboard} did not have match in {top_hud_agent_pictures}"
        if match_index is None:
            matched_pictures[(None, agent_picture_scoreboard)] = PlayerInfo(
                board_row.username,
                board_row.ultimate_status,
                board_row.kills,
                board_row.deaths,
                board_row.assists,
                board_row.credits,
                board_row.spike_status,
                agent_picture_scoreboard.agent_name,
                agent_picture_scoreboard.team,
                0,  # No match so they're probably dead
            )
            continue
        match = top_hud_agent_pictures.pop(match_index)
        matched_pictures[(match, agent_picture_scoreboard)] = PlayerInfo(
            board_row.username,
            board_row.ultimate_status,
            board_row.kills,
            board_row.deaths,
            board_row.assists,
            board_row.credits,
            board_row.spike_status,
            agent_picture_scoreboard.agent_name,
            agent_picture_scoreboard.team,
            agent_healths.pop(match_index),
        )
    return matched_pictures_formatted(matched_pictures)

def create_tesseract():
    tesseract = PyTessBaseAPI(
        lang='eng', path="./tessdata", psm=PSM.SINGLE_LINE, oem=OEM.DEFAULT
    )
    tesseract.SetVariable("tessedit_do_invert", "0")
    return tesseract

def matched_pictures_formatted(matched_pictures):
    out = []
    for (top_hud_picture, scoreboard_picture), player_info in matched_pictures.items():
        out.append(PlayerInfoAndImages(top_hud_picture, scoreboard_picture, player_info))
    return out


# main.py
import logging
import asyncio
from mouse_handler.factory import get_mouse_handler

mouse = get_mouse_handler() #LinuxMouseHandler_wayland()

event_queue = asyncio.Queue()

async def get_events(event_queue):
    print("Entered get_events")
    async for move_value in mouse.move():
        await event_queue.put(move_value)
        logging.debug(f"Put {move_value} into queue")

async def process_events(event_queue):
    print("Entered process_events")
    while True:
        event = await event_queue.get()
        logging.debug(f"Processed1: {event}")
        # do a thing w/ event
        # print(f"Processed: {event}")

# if __name__ == "__main__":
#     asyncio.ensure_future(get_events(event_queue))
#     asyncio.ensure_future(process_events(event_queue))
#     loop = asyncio.get_event_loop()
#     loop.run_forever()


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

"""
Initialization module for tpDcc.dccs.maya
"""

from __future__ import print_function, division, absolute_import

import os
import sys
import inspect
import logging

import maya.cmds

from tpDcc.core import dcc
from tpDcc.managers import resources
from tpDcc.libs.python import path as path_utils

# =================================================================================

PACKAGE = 'tpDcc.dccs.maya'

# =================================================================================

def get_module_path():
    """
    Returns path where tpDcc.dccs.maya module is stored
    :return: str
    """

    try:
        mod_dir = os.path.dirname(inspect.getframeinfo(inspect.currentframe()).filename)
    except Exception:
        try:
            mod_dir = os.path.dirname(__file__)
        except Exception:
            try:
                import tpDcc.dccs.maya
                mod_dir = tpDcc.dccs.maya.__path__[0]
            except Exception:
                return None

    return mod_dir

def externals_path():
    """
    Returns the paths where tpDcc.dccs.maya externals packages are stored
    :return: str
    """

    return os.path.join(get_module_path(), 'externals')

def update_paths():
    """
    Adds path to system paths at startup
    """

    ext_path = externals_path()
    python_path = os.path.join(ext_path, 'python')
    maya_path = os.path.join(python_path, str(maya.cmds.about(v=True)))

    paths_to_update = [externals_path(), maya_path]

    for p in paths_to_update:
        if os.path.isdir(p) and p not in sys.path:
            sys.path.append(p)

    # Update custom tpDcc.dccs.maya plugins path
    tpdcc_maya_plugins_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'plugins')
    if not os.path.isdir(tpdcc_maya_plugins_path):
        return False
    tpdcc_maya_plugins_path = path_utils.clean_path(tpdcc_maya_plugins_path)

    maya_plugin_path = os.getenv('MAYA_PLUG_IN_PATH', None)
    if not maya_plugin_path:
        os.environ['MAYA_PLUG_IN_PATH'] = tpdcc_maya_plugins_path
    else:
        current_plugin_paths = os.environ['MAYA_PLUG_IN_PATH'].split(os.pathsep)
        for current_plugin_path in current_plugin_paths:
            if path_utils.clean_path(current_plugin_path) == tpdcc_maya_plugins_path:
                return True
        os.environ['MAYA_PLUG_IN_PATH'] = '{}{}{}'.format(
            os.environ['MAYA_PLUG_IN_PATH'], os.pathsep, tpdcc_maya_plugins_path)

def create_logger(dev=False):
    """
    Returns logger of current module
    """

    logger_directory = os.path.normpath(os.path.join(os.path.expanduser('~'), 'tpDcc', 'logs'))
    if not os.path.isdir(logger_directory):
        os.makedirs(logger_directory)

    logging_config = os.path.normpath(os.path.join(os.path.dirname(__file__), '__logging__.ini'))

    logging.config.fileConfig(logging_config, disable_existing_loggers=False)
    logger = logging.getLogger(PACKAGE.replace('.', '-'))
    dev = os.getenv('TPDCC_DEV', dev)
    if dev:
        logger.setLevel(logging.DEBUG)
        for handler in logger.handlers:
            handler.setLevel(logging.DEBUG)

    return logger

def init_dcc(dev=False):
    """
    Initializes module
    :param dev: bool, Whether to launch code in dev mode or not
    """

    update_paths()
    register_resources()

    create_logger(dev=dev)

    register_commands()
    load_plugins()
    create_metadata_manager()

def get_tpdcc_maya_plugins_path():
    """
    Returns path where tpdcc Maya plugins are located
    :return: str
    """

    return os.path.join(os.path.abspath(os.path.dirname(__file__)), 'plugins')

def get_tpdcc_maya_api_commands_path():
    """
    Returns path where tpdcc Maya plugins are located
    :return: str
    """

    return os.path.join(os.path.abspath(os.path.dirname(__file__)), 'api', 'commands')

def load_plugins(do_reload=True):
    from tpDcc.dccs.maya.core import helpers

    plugins_path = get_tpdcc_maya_plugins_path()
    if not os.path.isdir(plugins_path):
        return False

    for plugin_file in os.listdir(plugins_path):
        if not plugin_file:
            continue
        plugin_ext = os.path.splitext(plugin_file)[-1]
        if not plugin_ext == '.py':
            continue
        plugin_path = path_utils.clean_path(os.path.join(plugins_path, plugin_file))
        if do_reload:
            if helpers.is_plugin_loaded(plugin_path):
                helpers.unload_plugin(plugin_path)
        helpers.load_plugin(plugin_path)

def register_commands():
    from tpDcc.core import command

    commands_path = get_tpdcc_maya_api_commands_path()
    if not os.path.isdir(commands_path):
        return False

    runner = command.CommandRunner()
    if not runner:
        return False

    runner.manager().register_path(commands_path, package_name='tpDcc')

    return True

def create_metadata_manager():
    """
    Creates MetaDataManager for Maya
    """

    from tpDcc.dccs.maya.managers import metadatamanager

    metadatamanager.register_meta_classes()
    metadatamanager.register_meta_types()
    metadatamanager.register_meta_nodes()

def register_resources():
    """
    Registers tpDcc.dccs.maya resources path
    """

    resources_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'resources')
    resources.register_resource(resources_path, key=dcc.Dccs.Maya)


import os
import sys

sys.path.append('/home/legal')

__file__ = "/home/legal/.venv-legal/bin/activate"

old_os_path = os.environ['PATH']
os.environ['PATH'] = os.path.dirname(os.path.abspath(__file__)) + os.pathsep + old_os_path
base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if sys.platform == 'win32':
    site_packages = os.path.join(base, 'Lib', 'site-packages')
else:
    site_packages = os.path.join(
        base,
        'lib',
        'python{0.major:d}.{0.minor:d}'.format(sys.version_info),
        'site-packages')
prev_sys_path = list(sys.path)
import site
site.addsitedir(site_packages)
sys.real_prefix = sys.prefix
sys.prefix = base
# Move the added items to the front of the path:
new_sys_path = []
for item in list(sys.path):
    if item not in prev_sys_path:
        new_sys_path.append(item)
        sys.path.remove(item)
sys.path[:0] = new_sys_path

os.environ['DJANGO_SETTINGS_MODULE'] = 'legal.settings'

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()


#!/usr/bin/env python

import yaml

from keystoneauth1.identity import v3
from keystoneauth1 import session
from keystoneclient.v3 import client as keystone_client
from keystoneclient.v3 import services as services
from novaclient import client

def mk_auth(cred_name):
    with open('/root/.config/openstack/clouds.yaml') as f:
        os_cloud = yaml.load(
            f,Loader=yaml.FullLoader
        )['clouds'][cred_name]['auth']
        ac_id = os_cloud['application_credential_id']
        ac_secret = os_cloud['application_credential_secret']
        auth_url = os_cloud['auth_url']

    auth = v3.ApplicationCredential(
        auth_url=auth_url,
        application_credential_id=ac_id,
        application_credential_secret=ac_secret,
    )
    return(auth)

def main():
    ks_auth = mk_auth("nerc")
    ks_session = session.Session(ks_auth)
    ks_client = keystone_client.Client(session=ks_session)
    nova_client = client.Client(version=2,session=ks_session)
    projects = ks_client.projects.list()
    print(nova_client.servers.list(search_opts={'all_tenants':1}))
    return 0

if __name__ == "__main__":
    main()


#!/bin/python3

import sys

def jimOrders(orders):
    # Complete this function
    ans = list()
    d = dict()
    for i in range(len(orders)):
        ans.append( [orders[i][0]+orders[i][1],i+1] )
    ans.sort()
    ans2 = list()
    for i in ans:
        ans2.append(i[1] )
    return ans2

if __name__ == "__main__":
    n = int(input().strip())
    orders = [ ]
    for orders_i in range(n):
        orders_t = [ int(orders_temp) for orders_temp in input().strip().split(' ') ]
        orders.append(orders_t)
    result = jimOrders(orders)
    print(" ".join(map(str, result)))


def fn(nums):
    n = len(nums)+1
    s = (0+n-1)*n//2
    # print(s)
    for x in nums:
        s-=x
    # print(s)
    return s

nums = [0,2,3,4]
print(fn(nums))


from azureml.core import Workspace
from azureml.core import Experiment

ws = Workspace.get("BERT", subscription_id="cdf3e529-94ee-4f54-a219-4720963fee3b")
print('Workspace name: ' + ws.name,
      'Azure region: ' + ws.location,
      'Subscription id: ' + ws.subscription_id,
      'Resource group: ' + ws.resource_group, sep='\n')

workspace=Workspace.get("BERT", subscription_id="cdf3e529-94ee-4f54-a219-4720963fee3b")
experiment_name = 'azure-service-classifier'
experiment = Experiment(workspace, name=experiment_name)

from azureml.train.dnn import TensorFlow

from azureml.core.runconfig import RunConfiguration

# Edit a run configuration property on the fly.
run_local = RunConfiguration()

run_local.environment.python.user_managed_dependencies = True

script_params = {
    # to mount files referenced by mnist dataset
    '--data_dir': './1-Training/data-shared-inbox',
    '--max_seq_length': 128,
    '--batch_size': 32,
    '--learning_rate': 3e-5,
    '--steps_per_epoch': 150,
    '--num_epochs': 3,
    '--export_dir':'./1-Training/outputs/model'
}

estimator1 = TensorFlow(source_directory='/mnt/c/Users/phill/azure-ml/bert-stack-overflow',
                        entry_script='./1-Training/train.py',
                        compute_target="local",
                        script_params = script_params,
                        framework_version='2.0')

run1 = experiment.submit(estimator1)


import re
import requests
import threading
from com.tianhao.tibet.Utils.filter_tags import filter_tags

def getContent(url, title, file):
    i = 0
    headers = {
        'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
    }

    resp = requests.get(url,headers=headers)
    resp.encoding = 'utf-8'
    data = resp.text
    # 正则，取出文本块内容
    partten = re.compile(r'
(.*?)
', re.S)

    try:
        rexdata_1 = partten.search(data).group(0)
    except:
        i = i + 1
        print(i)
        pass

    # 去除所有html标签  css标签内容   特殊字符的转换&nbsp 换成空格
    rexdata = filter_tags(rexdata_1)

    with open(f'../news/tbtibetcn/{file}/{title}.txt', mode='a',encoding='utf-8') as f:
        # 写入标题
        f.write(title + '\r\n')
        # 写入文章
        f.write(rexdata.strip())
    f.close()

def getTitleAndUrl(urls,file):

    # 获取标题 和  正文的url           正则表达式对象
    parttern_title_url = re.compile(r'
(.*?)', re.S)

    # 遍历取出所有的title和url
    for url in urls:
        resp = requests.get(url)
        resp.encoding = 'utf-8'
        data = resp.text
        #利用正则筛选目标对象
        data_title_url = parttern_title_url.finditer(data)

        threads = []
        for it in  data_title_url:
            # 取出title和url
            url_content = urls[0] + it.group(1).split('./')[-1]

            # 用标题命名文件，替换不能用于文件名的特殊字符，换成_下划线
            rstr = r"[\/\\\:\*\?\"\<\>\|]"  # '/ \ : * ? " < > |'
            title = re.sub(rstr, "_", it.group(2)).strip()  # 替换为下划线

            # 调用getContent方法，获取文本并写入文件【多线程】
            threads.append(threading.Thread(target=getContent,args=(url_content,title,file)))
            # getContent(url_content,title,file)
        for thread in threads:
            thread.start()

        print(f'{url}页面完成')

if __name__ == '__main__':
    # 确定首页网址
    # 1、歌手【གཞས་པ།】
    urls = ['http://tb.tibet.cn/tb/disport/people/']
    # for i in range(1,1):
    #     urls.append(f'http://tb.tibet.cn/tb/disport/people/index_{i}.html')

    # 确定文件写入位置：相对路径
    file = 'རོལ་དབྱངས།/གཞས་པ།'
    getTitleAndUrl(urls,file)

    # 2、现代【དེང་གཞས།】
    urls = ['http://tb.tibet.cn/tb/disport/lxgq/']
    for i in range(1,14):
        urls.append(f'http://tb.tibet.cn/tb/disport/lxgq/index_{i}.html')

    # 确定文件写入位置：相对路径
    file = 'རོལ་དབྱངས།/དེང་གཞས།'
    getTitleAndUrl(urls,file)

    # 3、民歌【དམངས་གླུ།】
    urls = ['http://tb.tibet.cn/tb/disport/folk/']
    # for i in range(1,1):
    #     urls.append(f'http://tb.tibet.cn/tb/disport/folk/index_{i}.html')

    # 确定文件写入位置：相对路径
    file = 'རོལ་དབྱངས།/དམངས་གླུ།'
    getTitleAndUrl(urls,file)

    # 4、山歌【ལ་གཞས།】
    urls = ['http://tb.tibet.cn/tb/disport/love/']
    # for i in range(1,1):
    #     urls.append(f'http://tb.tibet.cn/tb/disport/love/index_{i}.html')

    # 确定文件写入位置：相对路径
    file = 'རོལ་དབྱངས།/ལ་གཞས།'
    getTitleAndUrl(urls,file)

    # 5、打击乐【རྡུང་ལེན།】
    urls = ['http://tb.tibet.cn/tb/disport/play/']
    # for i in range(1,1):
    #     urls.append(f'http://tb.tibet.cn/tb/disport/play/index_{i}.html')

    # 确定文件写入位置：相对路径
    file = 'རོལ་དབྱངས།/རྡུང་ལེན།'
    getTitleAndUrl(urls,file)

    # 6、格萨尔王传【གླིང་སྒྲུང་།】
    urls = ['http://tb.tibet.cn/tb/disport/gesar/']
    # for i in range(1,1):
    #     urls.append(f'http://tb.tibet.cn/tb/disport/gesar/index_{i}.html')

    # 确定文件写入位置：相对路径
    file = 'རོལ་དབྱངས།/གླིང་སྒྲུང་།'
    getTitleAndUrl(urls,file)

    # 7、儿歌【བྱིས་གླུ།】
    urls = ['http://tb.tibet.cn/tb/disport/child/']
    # for i in range(1,1):
    #     urls.append(f'http://tb.tibet.cn/tb/disport/child/index_{i}.html')

    # 确定文件写入位置：相对路径
    file = 'རོལ་དབྱངས།/བྱིས་གླུ།'
    getTitleAndUrl(urls,file)


from django.http import HttpResponse
from django.shortcuts import render
from .models import Contacto

# Create your views here.

def conctactoWeb(request):
    if request.method=="POST":
        contacto=Contacto()
        Nombre=request.POST.get('Nombre')
        Apellido=request.POST.get('Apellido')
        Email=request.POST.get('Email')
        Telefono=request.POST.get('Telefono')
        Asunto=request.POST.get('Asunto')
        Mensaje=request.POST.get('Mensaje')
        contacto.Nombre=Nombre
        contacto.Apellido=Apellido
        contacto.Email=Email
        contacto.Telefono=Telefono
        contacto.Asunto=Asunto
        contacto.Mensaje=Mensaje
        contacto.save()
        return HttpResponse("
Gracias Por Contactarnos
")
    return render(request, "contacto/contacto.html")


import matplotlib.pyplot as plt
from scipy.ndimage.filters import gaussian_filter1d
import numpy as np
import csv
import os

SAMPLES = 300

def read_csv_data(path):
    if not path.endswith('.csv'):
        raise Exception("A csv file is expected.")
        return
    parse_path = os.path.join(os.getcwd(), path)

    data = np.genfromtxt(parse_path, delimiter=',')
    return data

def overlap_frequencies(files):
    folder = r'Lab\\'
    ext = '.csv'

    # create canvas
    fig, ax = plt.subplots(figsize=(10, 10))

    name1 = files[0][4:].replace('hz', ' Hz')
    name2 = files[1][4:].replace('hz', ' Hz')
    name3 = files[2][4:].replace('hz', ' Hz')
    name4 = files[3][4:].replace('hz', ' Hz')
    name5 = files[4][4:].replace('hz', ' Hz')

    data1 = read_csv_data(folder + files[0] + ext)
    data2 = read_csv_data(folder + files[1] + ext)
    data3 = read_csv_data(folder + files[2] + ext)
    data4 = read_csv_data(folder + files[3] + ext)
    data5 = read_csv_data(folder + files[4] + ext)

    plt.plot(data1[:SAMPLES, 0], data1[:SAMPLES, 1], '--',
             color='#448ABD', label=name1, alpha=0.6)
    plt.plot(data2[:SAMPLES, 0], data2[:SAMPLES, 1], '#FF6F6A', label=name2)
    plt.plot(data3[:SAMPLES, 0], data3[:SAMPLES, 1], '--',
             color='#E6C42D', label=name3, alpha=0.6)
    plt.plot(data4[:SAMPLES, 0], data4[:SAMPLES, 1], '--',
             color='#A04FF9', label=name4, alpha=0.6)
    plt.plot(data5[:SAMPLES, 0], data5[:SAMPLES, 1], '--',
             color='#4FF9DF', label=name5, alpha=0.6)

    # increase axes range
    plt.ylim(top=0.03, bottom=-0.03)
    plt.xlim()
    plt.xlim(left=0.01, right=0.03)

    ax.legend(frameon=False, loc='lower center', ncol=8)
    # ax.axis('equal')
    ax.set_xlabel('time [s]')
    ax.set_ylabel('Current Amplitude [mA]')
    # plt.xticks(np.arange(0,0.5, 0.1))

    plt.show()

def impedance_plot():
    data = []
    fig, ax = plt.subplots(figsize=(10, 10))
    current = np.array([3.4469, 4.3049, 5.2610, 6.4027, 7.7114,
                        9.2484, 11.0950, 13.2450, 15.5940, 17.8750, 17.8070,
                        16.1130, 14.6040, 13.1770, 12.0110, 10.9660, 10.0950, 9.3771, 8.7427,
                        8.1825])
    impedance = np.array([
        290.1158,
        232.2934,
        190.0779,
        156.1841,
        129.6781,
        108.1268,
        90.1307,
        75.5002,
        64.1272,
        55.9441,
        56.1577,
        62.0617,
        68.4744,
        75.8898,
        83.2570,
        91.1910,
        99.0589,
        106.6428,
        114.3811,
        122.2120
    ])
    smoothed_impedance = gaussian_filter1d(impedance, sigma=2)
    smoothed_current = gaussian_filter1d(current, sigma=2)

    x = np.linspace(0, 8, len(current))
    x_i = np.linspace(0, 8, len(impedance))

    plt.plot(x, smoothed_current, color='#F46941',
             label='Corriente [mA]', alpha=1)
    # plt.plot(x_i, impedance, '--', color='#F46941', label='Corriente [mA]', alpha=0.5)
    plt.plot(x_i, smoothed_impedance, color='#4196D8',
             label='Impedancia [Ω]', alpha=1)
    plt.ylim(top=260, bottom=0)
    ax.set_xticklabels([])
    ax.set_xticks([])
    plt.legend(loc='upper center')
    plt.show()

def main():
    files = [r"rlc_90hz", 'rlc_160hz', 'rlc_200hz', 'rlc_60hz', 'rlc_270hz']
    # overlap_frequencies(files)
    impedance_plot()

if __name__ == "__main__":
    main()


import datetime
import logging
import mimetypes
import os
from typing import Optional, List

from fs import errors
from fs.info import Info
from fs.mode import Mode
from fs.path import dirname
from fs_gcsfs import GCSFS
from fs_gcsfs._gcsfs import GCSFile
from google.cloud.storage import Blob

logger = logging.getLogger(__name__)

DIR_BLOB_IMPOSTOR = object()

class PatchedGCSFS(GCSFS):
    def _is_dir_key(self, key: str):
        """ guesses if key was made by _path_to_dir_key """
        return key.endswith(self.DELIMITER)

    def _blobs_with_prefix_exist(self, prefix):
        """ finds a blob with a given prefix and returns a bool """
        response = self.bucket.list_blobs(prefix=prefix, delimiter=self.DELIMITER)
        for _ in response:
            return True
        return False

    def _is_pseudo_dir(self, key):
        """ returns if the key is a dir key, an """
        return self._is_dir_key(key) and self._blobs_with_prefix_exist(key)

    def _get_blob(self, key: str) -> Optional[Blob]:
        print(datetime.datetime.now(), "_get_blob", key)
        result = super()._get_blob(key)
        if not result and self._is_pseudo_dir(key):
            # Hack so that other parts of GCSFS get a truthy value. It works because GCSFS only checks if the returned
            # Optional[Blob] is None or not in case of directories
            # Well, there is one place where it doesn't use GCSFS._get_blob, in openbin, when checking the parent dir
            # it uses GCSFS.bucket.get_blob which bypasses our ugly hack here. So we have to override openbin too
            return DIR_BLOB_IMPOSTOR
        return result

    def getinfo(self, path: str, namespaces: Optional[List[str]] = None, check_parent_dir: bool = None) -> Info:
        if check_parent_dir is None:
            check_parent_dir = False
        return super().getinfo(path=path, namespaces=namespaces, check_parent_dir=check_parent_dir)

    def openbin(self, path: str, mode: str = "r", buffering: int = -1, **options) -> "GCSFile":
        # Have to override openbin because it checks if the parent dir exists using self.bucket.get_blob directly
        _mode = Mode(mode)
        _mode.validate_bin()
        self.check()
        _path = self.validatepath(path)
        _key = self._path_to_key(_path)

        def on_close(gcs_file):
            if _mode.create or _mode.writing:
                gcs_file.raw.seek(0)
                blob = self._get_blob(_key)
                if not blob:
                    blob = self.bucket.blob(_key)
                mime_type, encoding = mimetypes.guess_type(path)
                if encoding is not None:
                    mime_type = None
                blob.upload_from_file(gcs_file.raw, content_type=mime_type)
            gcs_file.raw.close()

        if _mode.create:
            try:
                info = self.getinfo(path)
            except errors.ResourceNotFound:
                pass
            else:
                if _mode.exclusive:
                    raise errors.FileExists(path)
                if info.is_dir:
                    raise errors.FileExpected(path)

            gcs_file = GCSFile.factory(path, _mode, on_close=on_close)

            if _mode.appending:
                blob = self._get_blob(_key)
                if blob:  # in case there is an existing blob in GCS, we download it and seek until the end of the stream
                    gcs_file.seek(0, os.SEEK_END)
                    blob.download_to_file(gcs_file.raw)

            return gcs_file

        if self.strict:
            info = self.getinfo(path)
            if info.is_dir:
                raise errors.FileExpected(path)

        gcs_file = GCSFile.factory(path, _mode, on_close=on_close)
        blob = self._get_blob(_key)
        if not blob:
            raise errors.ResourceNotFound(path)

        blob.download_to_file(gcs_file.raw)
        gcs_file.seek(0)
        return gcs_file


# Dealing with files and error/exception handling

# There is a built in method in python called open("")
# file = open("orders.text") # Looks for the file called orders.text

# Open a file that exists
#try:
#    file = open("orders.text")
#    print("File is found.")
#except FileNotFoundError as errmsg:
#    print("The above block of code was not executed." + str(errmsg))
#    #raise
#finally:
#    print("Your task is to read the data and display it as a list.")

# Open a file that does not exist
#    file = open("ordeeeers.text")
#    print("File is found.")
#except FileNotFoundError as errmsg:
#    print("File not found." + str(errmsg))
#    #raise
#finally:
#    print("Your task is to read the data and display it as a list.")

# Second iteration:
# Your task is to read the data and display it as a list
#file1 = open("orders.text")
#print(file1.read())
#file1.close()

# 3rd iteration
# Create a function to read the data and display it as a list (to the same job as before) DRY
def open_read_display(name):
    try:
        file = open(name) # Open the file
        print("File is found and opened.")
    except FileNotFoundError as errmsg:
        print("File not found." + str(errmsg))
        raise
    finally:
        print("Here is the text...")
        for line in file.readlines():
            print(line.rstrip('\n'))
        file.close()

open_read_display("orders.text")

# -----GOOD EXAMPLE-----
#def open_using_with_and_print(file):
#    try:
#        with open(file, "r") as file:
#            for line in file.readlines():
#                print(line.rstrip('\n'))
#    except FileNotFoundError:
#        print("file cannot be found or directory is incorrect, please check the details provided")
#        raise
#    finally:
#        print("\nPlease chose the items from the list  and enjoy your HAPPY MEAL")
#open_using_with_and_print("orders.text")

# Create a function to write an additional line to orders.text
def write_file(name, item):
    try:
        file = open(name, "w") # Open the file with the mode of written
        print("File is found and we will process to write.")
    except FileNotFoundError as errmsg:
        print("File not found." + str(errmsg))
        raise
    finally:
        file.write(item + "\n")
        print("List of items updated.")
        file.close()

write_file("orders.text", "Sandwich")

# -----GOOD EXAMPLE-----
#def write_to_file(file, order_item):
#    try:
#        with open(file, "w") as file:
#            file.write(order_item + '\n')
#    except FileNotFoundError:
#        print("file cannot be found or directory is incorrect, please check the details provided")
#        raise

#write_to_file("order.txt", "Ice Cream")


import base64
from datetime import datetime, timedelta

TAG = 'HVT'

EVENT_PROPERTY_NAMES = [ "Ambient_Temp",
                         "Surface_Temp",
                         "Ambient_Pressure",
                         "Ambient_Humidity",
                         "Battery",
                         "Vib_Accel_X_Axis",
                         "Vib_Accel_Y_Axis",
                         "Vib_Accel_Z_Axis",
                         "Vib_Velocity_X_Axis",
                         "Vib_Velocity_Y_Axis",
                         "Vib_Velocity_Z_Axis",
                         "Audio"];

def post_process(message):
    if message.get('meta') is None or message['meta'].get('raw') is None or message['meta'].get('fPort') is None:
        print(f'[{TAG}] A message have no meta.raw from Group ID:{message["grpid"]}, Node ID:{message["nid"]} <= meta:{message.get("meta")})')

        return message

    byte = base64.b64decode(message['meta']['raw'])
    pktTime = (byte[3] << 24) | (byte[2] << 16) | (byte[1] << 8) | byte[0]
    pktTime = datetime.utcfromtimestamp(pktTime).isoformat() + 'Z'

    if message['meta']['fPort'] == 2: # Periodic measurements
        GCONVERSION = 9.8

        for i in range(4, 10):
            if byte[i] > 127:
                byte[i] = (256 - byte[i]) * (-1)

        Ambient_Temp_Max = byte[4]
        Ambient_Temp_Min = byte[5]
        Ambient_Temp_Avg = byte[6]
        Surface_Temp_Max = byte[7]
        Surface_Temp_Min = byte[8]
        Surface_Temp_Avg = byte[9]
        Ambient_Pressure_Max = (byte[10] * 3) + 300
        Ambient_Pressure_Min = (byte[11] * 3) + 300
        Ambient_Pressure_Avg = (byte[12] * 3) + 300
        Ambient_Humidity_Max = byte[13]
        Ambient_Humidity_Min = byte[14]
        Ambient_Humidity_Avg = byte[15]
        Vib_Accel_X_Axis_Max = byte[16] /GCONVERSION
        Vib_Accel_X_Axis_Min = byte[17] /GCONVERSION
        Vib_Accel_X_Axis_RMS = byte[18] /GCONVERSION
        Vib_Accel_Y_Axis_Max = byte[19] /GCONVERSION
        Vib_Accel_Y_Axis_Min = byte[20] /GCONVERSION
        Vib_Accel_Y_Axis_RMS = byte[21] /GCONVERSION
        Vib_Accel_Z_Axis_Max = byte[22] /GCONVERSION
        Vib_Accel_Z_Axis_Min = byte[23] /GCONVERSION
        Vib_Accel_Z_Axis_RMS = byte[24] /GCONVERSION
        Vib_Velocity_X_Axis_Max = byte[25]/10 # mm/sec with factor 10 from device
        Vib_Velocity_X_Axis_Min = byte[26]/10
        Vib_Velocity_X_Axis_RMS = byte[27]/10
        Vib_Velocity_Y_Axis_Max = byte[28]/10
        Vib_Velocity_Y_Axis_Min = byte[29]/10
        Vib_Velocity_Y_Axis_RMS = byte[30]/10
        Vib_Velocity_Z_Axis_Max = byte[31]/10
        Vib_Velocity_Z_Axis_Min = byte[32]/10
        Vib_Velocity_Z_Axis_RMS = byte[33]/10
        Audio_dBSPL = byte[34]
        Audio_Max = byte[35]
        Audio_Min = byte[36]
        Remaining_battery_perc = byte[37]

        message['data'] = {
            'timestamp': pktTime,
            'var_Ambient_Temp_Max': Ambient_Temp_Max,
            'var_Ambient_Temp_Min': Ambient_Temp_Min,
            'var_Ambient_Temp_Avg': Ambient_Temp_Avg,
            'var_Surface_Temp_Max': Surface_Temp_Max,
            'var_Surface_Temp_Min': Surface_Temp_Min,
            'var_Surface_Temp_Avg': Surface_Temp_Avg,
            'var_Ambient_Pressure_Max': Ambient_Pressure_Max,
            'var_Ambient_Pressure_Min': Ambient_Pressure_Min,
            'var_Ambient_Pressure_Avg': Ambient_Pressure_Avg,
            'var_Ambient_Humidity_Max': Ambient_Humidity_Max,
            'var_Ambient_Humidity_Min': Ambient_Humidity_Min,
            'var_Ambient_Humidity_Avg': Ambient_Humidity_Avg,
            'var_Vib_Accel_X_Axis_Max': Vib_Accel_X_Axis_Max,
            'var_Vib_Accel_X_Axis_Min': Vib_Accel_X_Axis_Min,
            'var_Vib_Accel_X_Axis_RMS': Vib_Accel_X_Axis_RMS,
            'var_Vib_Accel_Y_Axis_Max': Vib_Accel_Y_Axis_Max,
            'var_Vib_Accel_Y_Axis_Min': Vib_Accel_Y_Axis_Min,
            'var_Vib_Accel_Y_Axis_RMS': Vib_Accel_Y_Axis_RMS,
            'var_Vib_Accel_Z_Axis_Max': Vib_Accel_Z_Axis_Max,
            'var_Vib_Accel_Z_Axis_Min': Vib_Accel_Z_Axis_Min,
            'var_Vib_Accel_Z_Axis_RMS': Vib_Accel_Z_Axis_RMS,
            'var_Vib_Velocity_X_Axis_Max': Vib_Velocity_X_Axis_Max,
            'var_Vib_Velocity_X_Axis_Min': Vib_Velocity_X_Axis_Min,
            'var_Vib_Velocity_X_Axis_RMS': Vib_Velocity_X_Axis_RMS,
            'var_Vib_Velocity_Y_Axis_Max': Vib_Velocity_Y_Axis_Max,
            'var_Vib_Velocity_Y_Axis_Min': Vib_Velocity_Y_Axis_Min,
            'var_Vib_Velocity_Y_Axis_RMS': Vib_Velocity_Y_Axis_RMS,
            'var_Vib_Velocity_Z_Axis_Max': Vib_Velocity_Z_Axis_Max,
            'var_Vib_Velocity_Z_Axis_Min': Vib_Velocity_Z_Axis_Min,
            'var_Vib_Velocity_Z_Axis_RMS': Vib_Velocity_Z_Axis_RMS,
            'var_Audio_Max': Audio_Max,
            'var_Audio_Min': Audio_Min,
            'var_Audio_dBSPL': Audio_dBSPL,
            'var_Remaining_battery_perc': Remaining_battery_perc,
        }

    elif message['meta']['fPort'] == 8:  # Events
        decoded_8 = {
            'timestamp': pktTime,
        };

        Sensor_Type = byte[4];
        Event_Type = byte[5];

        if Sensor_Type < 4: #Regular Sensor measurement alarms
            Event_Data = byte[6]
            if Sensor_Type <= 1: #Ambient Temp, Surface Temp
                if Event_Data > 127:
                    Event_Data = (256 - byte[6])*(-1)
            elif Sensor_Type == 2: #Ambient Pressure
                Event_Data = ((byte[6] * 3) + 300);
            decoded_8["event_" + EVENT_PROPERTY_NAMES[Sensor_Type] + "_Type"] = Event_Type
            decoded_8["event_" + EVENT_PROPERTY_NAMES[Sensor_Type] + "_Data"] = Event_Data
            message['data'] = decoded_8
        elif Sensor_Type > 4 and Sensor_Type <= 11: #Vibration & Acoustics alarms
            Freq_Band = byte[7]
            Freq_Value = (byte[10] << 16) | (byte[9] << 8) | byte[8]
            Amplitude = byte[11]
            if Sensor_Type != 11: #acoustics doesn't have factor 10 for amplitude
                Amplitude = Amplitude/10;
            decoded_8["event_" + EVENT_PROPERTY_NAMES[Sensor_Type] + "_Type"] = Event_Type
            decoded_8["event_" + EVENT_PROPERTY_NAMES[Sensor_Type] + "_Freq" + Freq_Band] = Freq_Value
            decoded_8["event_" + EVENT_PROPERTY_NAMES[Sensor_Type] + "_Amp" + Freq_Band] = Amplitude
            message['data'] = decoded_8
        elif Sensor_Type == 4: #Battery Alarm
            decoded_8["event_" + EVENT_PROPERTY_NAMES[Sensor_Type] + "_Type"] = Event_Type

            if Event_Type == 1: #Battery Voltage Low
                decoded_8["event_" + EVENT_PROPERTY_NAMES[Sensor_Type] + "_Voltage"] = 3 + (byte[6] * 0.004)
            elif Event_Type == 2: #Battery Life Changed
                decoded_8["event_" + EVENT_PROPERTY_NAMES[Sensor_Type] + "_Life"] = byte[6]
            message['data'] = decoded_8

    elif message['meta']['fPort'] == 11: # Diagnostics
        Diag_Status = byte[5] << 8 | byte[4]
        decoded_11 = {
            'timestamp': pktTime,
            'Diag_Status': Diag_Status,
        }

        # all measurements
        for Sensor_Type in range(0, 12):
            decoded_11["event_" + EVENT_PROPERTY_NAMES[Sensor_Type] + "_Type"] = byte[Sensor_Type + 6]

        message['data'] = decoded_11;
    else:
        print(f"[{TAG}] unknown format ({message['meta']['fPort']}, {len(byte)})")

    return message


#! /usr/bin/env python3
import os
from f90_regex import *
from collections import OrderedDict
from default_type_definition import write_definition
from default_type_procedures import write_alloc, write_init, write_dealloc
from allowed_modules import allowed_modules
tree_root = 0

class Use(object):
    typ = "Use"
    def __init__(self, module, quantities):
        print(module, quantities)
        self.module = module.lower()
        self.quantities = quantities

class Constant(object):
    typ = "Constant"
    def __init__(self, name, type_base,
                 type_extra=None, dimension=None,
                 pointer=None, target=None, default=None):
        self.name = name.lower()
        self.type_base = type_base
        self.type_extra = type_extra
        self.dimension = dimension
        self.pointer = pointer
        self.target = target
        self.default = default

class Variable(object):
    typ = "Variable"
    def __init__(self, name, type_base,
                 type_extra=None, allocatable=None, dimension=None,
                 intent=None, pointer=None, target=None, default=None):
        self.name = name.lower()
        self.type_base = type_base
        self.type_extra = type_extra
        self.allocatable = allocatable
        self.dimension = dimension
        self.intent = intent
        self.pointer = pointer
        self.target = target
        self.default = default

class Line(object):
    typ = "Line"
    def __init__(self, input_string, fileobj_in):
        self.bare = ''
        self.code = []
        self.comment = []
        continue_line = True
        continuing = False
        #self.empty = False
        while continue_line:
            # preprocess
            try:
                code = re.match(r'(\s*#.*)$', input_string).group(0)
                comment = ''
            except:
                # Remove comments
                try:
                    code, comment = re.match(r'\s*(.*?)(\s*!.*)$', input_string).groups()
                except:
                    #pass
                    code = re.match(r'\s*(.*?)\s*$', input_string).group(1)
                    comment = ''

            # Concatenate continued lines
            try:
                code = re.match(r'^\s*(&)?\s*(.*?)(&)\s*$',code).group(2)
                input_string = fileobj_in.readline()
                continuing = True
            except:
                # account for empty line, we should continue to the next
                if continuing and len(code.strip()) == 0:
                    input_string = fileobj_in.readline()
                    code = ""
                else:
                    continuing = False
                    continue_line = False
                    code = re.match(r'^\s*(&)?\s*(.*?)(&)?\s*$',code).group(2)

            self.bare += code+' '
            self.code.append(code)
            self.comment.append(comment)
        self.empty = len(self.bare) < 2

class EmptyLine(Line):

    def __init__(self):
        self.bare = ''
        self.code = ['']
        self.comment = ['']

class Project(object):

    def __init__(self, name, root):
        self.name = name
        self.root = root
        self.tree_level = tree_root
        self.sources = {}

    def add_sourcefile(self, filename):
        #self.sources.append(Sourcefile(filename))
        if filename in self.sources:
            print("WARNING, source file {} is being read twice.".format(filename))
        shortdir = os.path.relpath(os.path.dirname(filename), start=self.root)+"/"
        name = shortdir+os.path.split(filename)[1]
        self.sources[name]=Sourcefile(shortdir, name, filename, self)

    def scan_sources(self):
        for key, src in self.sources.items():
            #print('Scanning', src.name)
            src.scan()

    def make_project_tree(self):
        print('PROJECT '+self.name)
        for key, src in self.sources.items():
            print('  |')
            src.print_contains()

    def locate(self, name, typ=None):
        name = name.lower()
        location_ = []
        if name in self.sources:
            if typ is None:
                return self.sources[name], location_
            else:
                if self.sources[name].typ == typ:
                    return self.sources[name], location_
        for key, src in self.sources.items():
            #print('Scanning', src.name)
            block, location = src.locate(name, location_, typ)
            if block is not None:
                return block, location
        return None, None

    def get_blocktypes(self, typ):
        blocks = []
        if typ == "Procedure":
            typ = ["Subroutine","Function","Interface"]
        else:
            typ = [typ]
        for key, src in self.sources.items():
            #if isinstance(src, class_obj):
            if src.typ in typ:
                blocks.append(src)
            else:
                blocks.extend(src.get_blocktypes(typ))

        if len(blocks) > 0: return blocks

        return blocks

class Codeblock(object):

    def __init__(self,name):
        self.name = name.lower()
        self.preprocesses = OrderedDict()
        self.contains = OrderedDict()
        self.uses = [] #OrderedDict()
        self.use2use = None
        self.use2arg = None
        self.declares = OrderedDict()
        self.datatypes = OrderedDict()
        self.lines = []
        self.patched_name = ''
        self.typ = ''
        self.location = {"project":None,
                        "file":None,
                        "module":None,
                        "procedure":None}

    def add_uses(self, use):
        self.uses.append(use)
        return
        if use.module not in self.uses:
            if use.quantities is None:
                self.uses[use.module] = None
                return
            else:
                self.uses[use.module] = OrderedDict()

        for key, quantity in use.quantities.items():
            self.uses[use.module][key] = quantity

    def add_preprocess(self,obj):
        self.preprocesses[obj.name] = obj

    def add_contains(self,obj):
        self.contains[obj.name] = obj

    def add_datatype(self, datatype):
        if self.typ not in ["Module","Preprocess"]:
            print("WARNING, the datatype {} is being defined outside a module ({} {}).".format(datatype.name,self.typ, self.name))
        self.datatypes[datatype.name] = datatype

    def print_contains(self):
        print('   '*self.tree_level+'---- '+self.__str__())
        #self.print_contains()
        for obj_ in self.contains:
            obj = self.contains[obj_]
            print('   '*self.tree_level+'   |')
            #print ('---'*obj.tree_level,obj.typ,obj.name)
            obj.print_contains()

    def locate(self, name, location_=[], typ=None):
        name = name.lower()
        if name in self.contains:
            if typ is None:
                return self.contains[name], location_+[self.name]
            else:
                if self.contains[name].typ == typ:
                    return self.contains[name], location_+[self.name]
        for key, src in self.contains.items():
            location = location_+[self.name]
            block, location = src.locate(name, location, typ)
            if block is not None:
                return block, location
        return None, None

    def get_blocktypes(self, typ):
        blocks = []
        for key, obj in self.contains.items():
            #if isinstance(obj, class_obj):
            if obj.typ in typ:
                blocks.append(obj)
            else:
                blocks.extend(obj.get_blocktypes(typ))
        return blocks

    def check_uses(self, blacklisted=None):
        #self.use2use, self.use2arg = self._upstream_uses()
        #self.use2use = OrderedDict()
        #self.use2arg = OrderedDict()
        branch = []
        branch = self._get_tree_branch(branch)
        use2use = OrderedDict()
        use2arg = OrderedDict()
        for block in branch[::-1]:
            use2use, use2arg = block._build_uses(blacklisted, use2use, use2arg)
        self.use2use = use2use
        self.use2arg = use2arg

        if 1 > 0:
            print(self)
            print('USE: ',self.use2use)
            print('ARG: ',self.use2arg)
        return

    def _build_uses(self, blacklisted, upstream_use2use, upstream_use2arg):
        if self.use2arg is not None and self.use2use is not None:
            return self.use2use, self.use2arg

        project = get_project(self)
        count = 0
        self.use2arg = upstream_use2arg.copy()
        self.use2use = upstream_use2use.copy()
        for use in self.uses:
            if blacklisted is None or use.module in blacklisted:
                count += 1
                use2use, use2arg = _check_proper_use(use, self, project)
                if use.module not in self.use2use:
                    self.use2use[use.module] = OrderedDict()
                if use.module not in self.use2arg:
                    self.use2arg[use.module] = OrderedDict()
                for var, alias in use2use.items():
                    self.use2use[use.module][var] = alias
                for var, alias in use2arg.items():
                    self.use2arg[use.module][var] = alias
            else:
                self.use2use[use.module] = use.quantities
        return self.use2use, self.use2arg

    def _upstream_proper_use(self):
        if self.typ == "Module":

            return OrderedDict(), OrderedDict()
        else:
            return self.parent._upstream_proper_use()

    def _get_tree_branch(self, branch=[]):
        print(self)
        branch.append(self)
        if self.typ == "Module":
            return branch
        if self.parent.typ == "Sourcefile":
            return branch
        return self.parent._get_tree_branch(branch)

    def get_patched(self):
        print(self)
        for mod_name, quantities in self.use2use.items():
            print("use {}, only: ".format(mod_name))
            for q in quantities:
                print(q)
        return

    def __str__(self):
        return "{} {}".format(self.typ, self.name)

def _use2string(uses, args):
    lines = []
    for module, quantities in uses.items():
        if len(quantities) > 0:
            qs = []
            for var, alias in quantities.items():
                if alias is None:
                    qs.append(var)
                else:
                    qs.append("{} => {}".format(alias, var))
            line = "use {}, only: ".format(module)+", ".join(qs)
            lines.append(line)
    for module, quantities in args.items():
        if len(quantities) > 0:
            line = "use {}, only: {}_autotype".format(module, module)
            lines.append(line)

    return lines

def _arg2def(args):
    lines = []
    for module, quantities in args.items():
        if len(quantities) > 0:
            line = "type({}_autotype) :: {}".format(module, module)
            lines.append(line)
    return lines

def _arg2associate(args):
    lines = ['associate(']
    spaces = ' '*9
    for module, quantities in args.items():
        if len(quantities) > 0:
            for var, alias in quantities.items():
                if alias is None:
                    line = '{} {} => {}%{}'.format(spaces, var, module, var)
                    lines.append(line)
                else:
                    line = '{} {} => {}%{}'.format(spaces, alias, module, var)
                    lines.append(line)
    lines.append('{})'.format(spaces))
    return lines

def _extendargs(original_args, args):
    new_args = original_args.copy()
    for module, quantities in args.items():
        if len(quantities) > 0:
            new_args.append(module)
    return new_args

class Preprocess(Codeblock):

    def __init__(self, name, parent):
        super().__init__(name)
        self.typ = 'Preprocess'
        self.parent = parent
        self.startstatement = None
        self.endstatement = None
        self.tree_level = parent.tree_level + 1

    def set_startstatement(self, string):
        self.startstatement = string

    def set_endstatement(self, string):
        self.endstatement = string

    def write_preprocess(self, f):
        f.write("\n")
        if self.startstatement is not None:
            f.write(self.startstatement + "\n")
        write_definition(self, f)
        if self.endstatement is not None:
            f.write(self.endstatement + "\n")

class Sourcefile(Codeblock):

    def __init__(self, shortdir, name, fullname, parent):
        super().__init__(name)
        self.fullname = fullname
        self.parent = parent
        self.tree_level = tree_root
        self.typ = 'Sourcefile'
        self.shortdir = shortdir
        self.tree_level = parent.tree_level + 1
        self.name = name

    def __str__(self):
        return 'FILE '+self.name

    def scan(self):
        context = []
        context.append(self)
        location = self.location.copy()
        location['file'] = self
        with open(self.fullname,'r') as src:
            print(self.name)
            for line in src:

                line = Line(line,src)
                #self.lines.append(Line(line,src))

                # Parse line to update context
                # (i.e. are we entering a new module/routine/function?)
                if len(line.bare.strip()) > 0 :
                    output = line_to_context(line, context, location)
                    #if isinstance(context[-1], Module):
                    #    location['module'] = context[-1]
                    #elif isinstance(context[-1], Procedure):
                    #    location['procedure'] = context[-1]
                    #print(location)
                    #print(output,context)
                    if abs(output) == 1:
                        pass
                        #print(self.lines[-1].bare)
                        #print(output, context)
                    if  not output == 0 and not output == 1 :
                        pass
                        #print('WARNING: ',line)

    def print_source(self):
        for line in self.lines:
            npartial = len(line.code)
            if npartial > 1:
                for ipar, code in enumerate(line.code):
                    if ipar < npartial -1:
                        print( code + ' & ' + line.comment[ipar])
                    else:
                        print( code + line.comment[ipar])

            else:
                print (line.code[0] +' '+ line.comment[0])

    def print_bare_source(self):
        for line in self.lines:
            if not line.empty:
                print(line.bare)

class Interface(Codeblock):

    def __init__(self, name, parent):
        super().__init__(name)
        self.parent = parent
        self.tree_level = parent.tree_level + 1
        self.typ = "Interface"

class Procedure(Codeblock):

    def __init__(self, name, argstring, parent):
        super().__init__(name)
        self.parent = parent
        self.tree_level = parent.tree_level + 1
        self.args, self.argstring = str_to_arg(argstring)

class Subroutine(Procedure):

    def __init__(self,name,argstring,parent):
        super().__init__(name, argstring, parent)
        self.typ = 'Subroutine'

    def __str__(self):
        return "SUBROUTINE {}({})".format(self.name, ", ".join(self.args))

class Function(Procedure):

    def __init__(self,name,argstring,parent, result_type=None, result_name=None):
        super().__init__(name, argstring, parent)
        self.typ = 'Function'
        self.result_type = result_type
        self.result_name = result_name

    def __str__(self):
        header = ""
        if self.result_type is not None:
            header += result_typ+" "
        header += "FUNCTION {}({})".format(self.name, ", ".join(self.args))
        if self.result_name is not None:
            header += " RESULT({})".format(self.result_name)
        return header

class Module(Codeblock):

    def __init__(self,name,parent):
        super().__init__(name)
        self.parent = parent
        self.tree_level = parent.tree_level + 1
        self.typ = 'Module'

    def patch(self):
        obj = Module(self.name+"_patched", self.parent)
        #obj.name = self.name+"_patched"
        obj.lines = []
        new_args = []
        project = get_project(self)
        for line in self.lines:
            if line.typ == "Use":
                use2use, use2arg = _check_proper_use(line, self, project)
                print(use2use, use2arg)
                #if use2use is not None:
                #    obj.lines.append(use2use)
        return obj
                #break

    def objectify(self):
        self.patched_name = self.name#+"_module"
        if len(self.datatypes) > 0 :
            #directory = self.parent.shortdir+"autogenerated/"
            #skip_module_file = "./{}skip_modules.txt".format(directory)
            print("WARNING, the module '{}' already declares Datatypes, applying extra care.".format(self.name))
            #with open(skip_module_file, "a") as f:
            #    f.write(self.name+"\n")
            for key, datatype in self.datatypes.items():
                if key == self.patched_name:
                    print("WARNING, the module '{}' already declares the {} Datatype.".format(self.name, key))
                    datatype.patched_name = datatype.name+"_legacy"
        directory = self.parent.shortdir+"autogenerated/"
        definition_file = "./{}{}_definition.f90".format(directory, self.name)
        procedures_file = "./{}{}_procedures.f90".format(directory, self.name)
        #print(definition_file)
        #print(procedures_file)
        with open(definition_file,"w") as f:
            # the datatype definition
            procedures = write_definition(self,f)

        if False:
            with open(procedures_file,"w") as f:
                # the default procedure list
                f.write(str(procedures)+"\n\n")
                # the alloc blueprint
                write_alloc(self,f)
                # the init blueprint
                write_init(self,f)
                # the dealloc blueprint
                write_dealloc(self,f)

    def locate(self, name, location_=[], typ=None):
        name = name.lower()
        block, location = super().locate(name, location_, typ)
        if block is not None:
            return block, location
        if typ is None or typ=="Variable":
            if name in self.declares:
                return self.declares[name], location_+[self.name]
        if typ is None or typ=="Datatype":
            if name in self.datatypes:
                return self.datatypes[name], location_+[self.name]
        return None, None

class Datatype(Codeblock):
    def __init__(self,name,parent):
        super().__init__(name)
        self.parent = parent
        if parent is None:
            self.tree_level = tree_root
        else:
            self.tree_level = parent.tree_level + 1
        self.typ = 'Datatype'

def parse_declaration(context,mo):
    #if context.typ not in ["Module", "Datatype", "Preprocess"]: return

    type_base = mo.group('type_base').strip() if mo.group('type_base') else ""
    type_base = type_base.lower() if len(type_base.strip()) > 0 else None

    type_extra = mo.group('type_extra').strip() if mo.group('type_extra') else ""
    type_extra = type_extra.lower() if len(type_extra) > 0 else None

    options = mo.group('options').strip() if mo.group('options') else ""
    options = options if len(options) > 0 else None

    variables = mo.group('vars').strip() if mo.group('vars') else ""
    variables = variables if len(variables) > 0 else None

    #print(type_base, type_extra, options, variables)
    attributes = _parse_declaration_options(options)
    var_list = _parse_declaration_variables(variables, type_base, type_extra, attributes)

    for var in var_list:
        var_name = var.name
        if var_name in context.declares:
            print("WARNING, variable {} in module {} is being declared twice.".format(var_name,context.name))
        context.declares[var_name] = var
        context.lines.append(var)
    #context.declares.extend(var_list)
    return

def _parse_declaration_options(options):
    attributes = {"allocatable":None, "dimension":None, "intent":None , "pointer":None, "target":None, "parameter":None}
    if options is None: return attributes
    leftover = options
    mo = options_rgx.match(leftover)
    while mo is not None:
        option = mo.group('option')
        sli = mo.group('slice')
        if option is not None:
            if option.lower() in ['allocatable', 'pointer', 'target', 'parameter']:
                attributes[option.lower()] = True
            elif option.lower() == "dimension":
                attributes["dimension"] = sli.strip().lower()
            elif option.lower() == "intent":
                attributes["intent"] = sli.strip().lower()
        #print(mo.group(0))
        leftover = leftover[len(mo.group(0)):]
        mo = options_rgx.match(leftover)
    #print(attributes)
    return attributes

def _parse_declaration_variables(variables, type_base, type_extra, attributes):
        var_list = []
        leftover = variables
        mo = vars_rgx.match(variables)
        while mo is not None:
            #var_dict = OrderedDict()
            #var_dict['name'] = mo.group('variable')
            #var_dict['type_base'] = type_base
            #var_dict['type_extra'] = type_extra
            #var_dict['allocatable'] = attributes['allocatable']
            #var_dict['dimension'] = attributes['dimension']
            #var_dict['intent'] = attributes['intent']
            #var_dict['pointer'] = attributes['pointer']
            #var_dict['target'] = attributes['target']
            #var_dict['default'] = mo.group('assign').lower() if mo.group('assign') else None
            #if mo.group('slice') is not None:
            #    var_dict['dimension'] = mo.group('slice').strip()
            #var_list.append(var_dict)
            name = mo.group('variable').lower().strip()
            allocatable = attributes['allocatable']
            dimension = attributes['dimension']
            intent = attributes['intent']
            pointer = attributes['pointer']
            target = attributes['target']
            parameter = attributes['parameter']
            default = mo.group('assign').lower() if mo.group('assign') else None
            if mo.group('slice') is not None:
                dimension = mo.group('slice').strip()
            if parameter:
                var = Constant(name=name, type_base=type_base, type_extra=type_extra,
                               dimension=dimension, pointer=pointer, target=target, default=default)
            else:
                var = Variable(name=name, type_base=type_base, type_extra=type_extra, allocatable=allocatable,
                               dimension=dimension, intent=intent, pointer=pointer, target=target, default=default)
            var_list.append(var)
            #print(mo.group(0))
            leftover = leftover[len(mo.group('subtract')):]
            mo = vars_rgx.match(leftover)
        #print()
        #for var in var_list:
        #    print(var)
        #    print()
        return var_list

def get_project(codeblock):
    tree_level = codeblock.tree_level
    project = codeblock
    while tree_level > tree_root:
        project = project.parent
        tree_level = project.tree_level
    return project

def _check_proper_use(use, codeblock, project):
    if use.module in allowed_modules:
        return use.quantities, OrderedDict()
    module, trash = project.locate(use.module, "Module")
    #module = project.get_blocktypes(typ="Module")[use.module]
    if module is None:
        print("ERROR, module {} couldn't be located in the project".format(use.module))
        raise Exception
        #return None, None
    if module.typ != "Module":
        print("ERROR, module {} couldn't be located in the project".format(use.module))
        raise Exception
    use2use = OrderedDict()
    use2arg = OrderedDict()
    #print(use.module, use.quantities)
    if len(use.quantities) == 0:
        print("WARNING, using all the quantities from the module {}".format(module.name))
        for key, procedure in module.contains.items():
            if procedure.typ in ["Subroutine","Function","Interface"]:
                #use2use.append(key)
                use2use[key] = None
        for key, quantity in module.declares.items():
            if quantity.typ in ["Constant"]:
                use2use[key] = None
            else:
                use2arg[key] = None
        for key, quantity in module.datatypes.items():
            if quantity.typ in ["Datatype"]:
                use2use[key] = None
            else:
                use2arg[key] = None
        return use2use, use2arg
    #print(use.quantities)
    for q, alias in use.quantities.items():
        quantity, trash = module.locate(q)
        if quantity is None:
            print("WARNING, quantity {} couldn't be located in module {}".format(q, module.name))
            #return None, None
        elif quantity.typ in ["Subroutine","Function","Interface", "Datatype", "Constant"]:
            #use2use.append(quantity.name)
            use2use[quantity.name] = alias
        else:
            #use2arg.append(quantity.name)
            use2arg[quantity.name] = alias
        #print(quantity.name,quantity.typ)
    return use2use, use2arg

def str_to_use(string):
    if string.strip() == "": return OrderedDict()
    args = string.split(",")
    if len(args) == 0: return None
    args_dict = OrderedDict()
    for arg in args:
        alias = None
        if "=>" in arg:
            alias, arg = arg.split("=>")
            alias = alias.strip()
        args_dict[arg.strip()] = alias
    return args_dict

def str_to_arg(string):
    if string is None: return [], ""
    args = [arg.strip() for arg in string.split(",") if len(arg.strip())>0]
    if len(args) == 0: return [], ""
    return args, ", ".join(args)

def line_to_context(line_obj, context, location):

    curr_context = context[-1]
    line = line_obj.bare

    chk = 0

    #print(line)

    # CHECK single line PREPROCESS (include|define)
    mo = ppsl_rgx.match(line)
    if mo is not None:
        return chk
        context.append(Preprocess(mo.group(0).lower(),curr_context))
        context[-1].set_startstatement(mo.group(0).lower())
        curr_context.add_preprocess(context[-1])
        context.pop()
        return chk

    # CHECK PREPROCESS IF
    mo = ppif_rgx.match(line)
    if mo is not None:
        return chk
        context.append(Preprocess(mo.group(0).lower(),curr_context))
        context[-1].set_startstatement(mo.group(0).lower())
        curr_context.add_preprocess(context[-1])
        return chk

    # CHECK PREPROCESS ELSE, ELIF
    mo = ppelse_rgx.match(line)
    if mo is not None:
        return chk
        context.pop()
        curr_context = context[-1]
        context.append(Preprocess(mo.group(0).lower(),curr_context))
        context[-1].set_startstatement(mo.group(0).lower())
        curr_context.add_preprocess(context[-1])
        return chk

    # CHECK PREPROCESS ENDIF
    mo = ppendif_rgx.match(line)
    if mo is not None:
        return chk
        curr_context.set_endstatement(mo.group(0).lower())
        context.pop()
        return chk

    # CHECK USE STATEMENTS
    mo = use_rgx.match(line)
    if mo is not None:
        #chk += 1
        use_dict = str_to_use(mo.group(2))
        use = Use(mo.group(1), use_dict)
        curr_context.add_uses(use)
        curr_context.lines.append(use)
        return chk

    # CHECK DECLARATION STATEMENTS
    mo = declaration_rgx.match(line)
    if mo is not None:
        #chk += 1
        if mo.group('type_base').strip().lower() == "type" and mo.group('type_extra') is None:
            pass
        else:
            parse_declaration(curr_context,mo)
            return chk

    # CHECK NEW SUBROUTINE
    mo = subroutine_rgx.match(line)
    if mo is not None:
        chk += 1
        subroutine = Subroutine(mo.group(1).lower(), mo.group(2),curr_context)
        context.append(subroutine)
        curr_context.add_contains(subroutine)
        curr_context.lines.append(subroutine)
        return chk

    # CHECK END SUBROUTINE
    mo = endsubroutine_rgx.match(line)
    if mo is not None:
        chk -= 1
        try:
            check = mo.group(1).lower() == curr_context.name
            end_name = mo.group(1).lower()
        except AttributeError:
            check = False
            end_name = "None"
        if not check:
            pass
            #print('WARNING: ',end_name, curr_context.name, line)
        #print(context[-1])
        context.pop()
        location['procedure'] = None
        return chk

    # CHECK NEW FUNCTION
    mo = function_rgx.match(line)
    if mo is not None:
        chk += 1
        function = Function(mo.group(1).lower(), mo.group(2),curr_context)
        context.append(function)
        curr_context.add_contains(function)
        curr_context.lines.append(function)
        return chk

    # CHECK END FUNCTION
    mo = endfunction_rgx.match(line)
    if mo is not None:
        chk -= 1
        #print(context[-1])
        context.pop()
        location['procedure'] = None
        return chk

    # CHECK NEW TYPE
    mo = type_rgx.match(line)
    if mo is not None:
        print(line)
        chk += 1
        datatype = Datatype(mo.group("name").lower(),curr_context)
        context.append(datatype)
        curr_context.add_datatype(datatype)
        curr_context.lines.append(datatype)
        return chk

    # CHECK END TYPE
    mo = endtype_rgx.match(line)
    if mo is not None:
        chk -= 1
        #print(context[-1])
        context.pop()
        #location['datatype'] = None
        return chk

    # CHECK NEW MODULE
    mo = module_rgx.match(line)
    if mo is not None:
        chk += 1
        module = Module(mo.group(1).lower(),curr_context)
        context.append(module)
        curr_context.add_contains(module)
        curr_context.lines.append(module)
        return chk

    # CHECK END MODULE
    mo = endmodule_rgx.match(line)
    if mo is not None:
        chk -= 1
        #print(context[-1])
        context.pop()
        location['module'] = None
        return chk

    # CHECK NEW Interface
    mo = interface_rgx.match(line)
    if mo is not None:
        chk += 1
        interface = Interface(mo.group("name").lower(),curr_context)
        context.append(interface)
        curr_context.add_contains(interface)
        curr_context.lines.append(interface)
        return chk

    # CHECK END ITERFACE
    mo = endinterface_rgx.match(line)
    if mo is not None:
        if curr_context.typ == "Interface":
            chk -= 1
            #print(context[-1])
            context.pop()
            return chk

    curr_context.lines.append(line_obj)

    return chk

if __name__ == "__main__":
    import sys
    sources  = [sys.argv[1]]
    project = Project('Test')
    for fil in sources:
        project.add_sourcefile(fil)
    project.scan_sources()
    project.make_project_tree()


from django.shortcuts import render
from .models import Student,Contact
from django.http import JsonResponse,HttpRequest

# Create view for Student
def get_student(request:HttpRequest)->JsonResponse:
    """
    Get all student
    """
    students = Student.objects.all()
    data = {
        'results':[]
    }
    for student in students:
        contact = student.contact # Get contact of student
        data['results'].append({
            'first_name':student.first_name,
            'last_name':student.last_name,
            'phone':contact.phone,
            'address':contact.address

        })

    return JsonResponse(data)

# Create view for Contact

def get_contact(request:HttpRequest)->JsonResponse:
    """
    Get all contact
    """
    contacts = Contact.objects.filter(address='Samarqand')
    data = {
        'results':[]
    }
    for contact in contacts:
        # student = contact.student # Get student of contact
        student= Student.objects.get(contact=contact)
        data['results'].append({

            'phone':contact.phone,
            'address':contact.address,
            'first_name':student.first_name,
            'last_name':student.last_name

        })

    return JsonResponse(data)


__copyright__ = "Copyright (c) 2021 Jina AI Limited. All rights reserved."
__license__ = "Apache-2.0"

import json
import os
from glob import glob

import pytest
import requests
from jina.flow import Flow

SHARDS_DOC = 2
SHARDS_CHUNK_SEG = 2
SHARDS_INDEXER = 2
JINA_TOPK = 2
GIF_BLOB = 'tests/data/*.gif'
NR_CASES_SEARCH = 3
REQ_SIZE = 1

@pytest.fixture()
def env_setup(tmpdir):
    os.environ['SHARDS_DOC'] = str(SHARDS_DOC)
    os.environ['JINA_TOPK'] = str(JINA_TOPK)
    os.environ['SHARDS_CHUNK_SEG'] = str(SHARDS_CHUNK_SEG)
    os.environ['SHARDS_INDEXER'] = str(SHARDS_INDEXER)
    os.environ['JINA_WORKSPACE'] = './workspace'
    os.environ['JINA_PORT'] = os.environ.get('JINA_PORT', str(45680))

def index_documents():
    f = Flow().load_config('flow-index.yml')

    with f:
        f.index_files(GIF_BLOB, request_size=REQ_SIZE, read_mode='rb')

def get_flow():
    f = Flow().load_config('flow-query.yml')
    f.use_grpc_gateway()
    return f

def validate(req):
    for doc in req.search.docs:
        print(doc)
        assert len(doc.matches) == JINA_TOPK
        for match in doc.matches:
            assert match.uri.startswith('data:image/gif')

def test_query(env_setup):
    index_documents()
    f = get_flow()
    with f:
        f.search_files(glob(GIF_BLOB)[:NR_CASES_SEARCH], on_done=validate)


import sys
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT

conn = psycopg2.connect(database="tcount",user="postgres",host="localhost",port="5432")
cur = conn.cursor()

cur.execute("SELECT word, count FROM tweetwordcount order by count desc limit 20")
records = cur.fetchall()
conn.commit()
conn.close()

words = [rec[0] for rec in records]
counts = [rec[1] for rec in records]

plt.bar(words,counts)
plt.ylabel('word counts')
plt.xlabel('words')
plt.title('Top 20 words')
plt.savefig('Plot.png')


import os
import unittest
from pathlib import Path
from typing import List

from binaryrts.util.io import slice_file_into_chunks

RESOURCES_DIR: Path = Path(os.path.dirname(__file__)) / "resources"

class IOUtilTestCase(unittest.TestCase):
    def test_slice_file_into_chunks(self):
        expected: List[str] = [
            "#define MAX(a,b) a>b?a:b\n",
            "int main()\n{\n\treturn 0;\n}",
        ]
        actual: List[str] = slice_file_into_chunks(
            RESOURCES_DIR / "main.cpp", [(1, 1), (11, 8)]
        )
        self.assertEqual(expected, actual)

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


import torch
import numpy as np
import pandas as pd
from tqdm import tqdm
import lightgbm as lgb
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset,TensorDataset, DataLoader,RandomSampler

from contextlib import contextmanager
from datetime import datetime, timedelta
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.model_selection import KFold, StratifiedKFold,GroupKFold
from torch.utils.data import Dataset, TensorDataset, DataLoader, RandomSampler
from sklearn.metrics import roc_auc_score,mean_squared_error,average_precision_score,log_loss

today = datetime.now().today()
current_date = str(today.date())
# current_time = str(today.time().strftime('%H:%M:%S'))

class LGBM():
    def __init__(self, train, test, param):
        self.train = train
        self.test = test
        self.param = param
        # self.seed = seed

    def train_val_split(self):
        seed = self.param['seed']
        train = self.train
        target = self.param['target_var']
        split_rate = self.param['split_rate']
        X_train, X_val, y_train, y_val = train_test_split(train.drop(target, axis = 1), train[[target]], test_size=split_rate, random_state=seed)

        return X_train, X_val, y_train, y_val

    def fit(self):
        train = self.train
        folds = self.param['folds']
        params = self.param['params']
        rounds = self.param['rounds']
        verbose = self.param['verbose']
        features = self.param['features']
        label_name = self.param['label_name']
        early_stop = self.param['early_stop']
        id_name = self.param['id_name']
        seed = self.param['seed']
        oof = train[[id_name]]
        oof[label_name] = 0
        feature_importance = []

        # StratifiedKFold: val_size: 1/n_splits
        skf = StratifiedKFold(n_splits=folds, shuffle=True, random_state=seed)
        skf_split = skf.split(train, train[[label_name]])

        for fold, (train_idx, val_idx) in enumerate(skf_split):
            evals_result_dic = {}
            # train_cids = train.loc[train_idx,id_name].values

            train_data = lgb.Dataset(train.loc[train_idx,features], label=train.loc[train_idx,label_name])
            val_data = lgb.Dataset(train.loc[val_idx,features], label=train.loc[val_idx,label_name])
            model = lgb.train(params,
                train_set = train_data,
                num_boost_round = rounds,
                valid_sets = [train_data, val_data],
                evals_result = evals_result_dic,
                early_stopping_rounds = early_stop,
                verbose_eval = verbose
            )
            model.save_model(f'{current_date}fold-{fold}.ckpt')
            valid_preds = model.predict(train.loc[val_idx,features], num_iteration=model.best_iteration)
            oof.loc[val_idx,label_name] = valid_preds

            importance_gain = model.feature_importance(importance_type='gain')
            importance_split = model.feature_importance(importance_type='split')
            feature_name = model.feature_name()
            new_feature_importance_df = pd.DataFrame({'feature_name':feature_name,
                                                      'importance_gain':importance_gain,
                                                      'importance_split':importance_split})
            feature_importance.append(new_feature_importance_df)

        feature_importance_df = pd.concat(feature_importance)
        feature_importance_df = feature_importance_df.groupby(['feature_name']).mean().reset_index()
        feature_importance_df = feature_importance_df.sort_values(by=['importance_gain'],ascending=False)
        feature_importance_df.to_csv(f'{current_date}-feature_importance.csv',index=False)

        oof.to_csv(f'{current_date}-oof.csv',index=False)

    def output_predict(self):
        test = self.test
        folds = self.param['folds']
        features = self.param['features']
        sub = None

        for fold in range(folds):
            model = lgb.Booster(model_file=f'{current_date}fold-{fold}.ckpt')
            test_preds = model.predict(test[features], num_iteration=model.best_iteration)
            sub['prediction'] += (test_preds / folds)
        # sub[[id_name,'prediction']].to_csv(output_path + '/submission.csv.zip', compression='zip',index=False)
        return sub

class GNN():
    def __init__(self, train, test, param):
        self.train = train
        self.test = test
        self.param = param
        # self.seed = seed

    def fit(self):
        train = self.train
        feature_name = self.param['feature_name']
        obj_max = self.param['obj_max']
        epochs = self.param['epochs']
        smoothing = self.param['smoothing']
        patience = self.param['patience']
        lr = self.param['lr']
        batch_size = self.param['batch_size']
        folds = self.param['folds']
        seed = self.param['seed']
        id_name = self.param['id_name']
        label_name = self.param['label_name']
        oof = train[[id_name]]
        oof[label_name] = 0

        skf = StratifiedKFold(n_splits = folds, shuffle=True, random_state=seed)
        for fold, (trn_index, val_index) in enumerate(skf.split(train_y,train_y[label_name])):

            return


def is_prime(n):
    if n == 2:
        return True
    if n % 2 == 0:
        return False
    reng = int((n ** 0.5)+ 1)
    for i in range(3, reng, 2):
        if n % i == 0:
            return False
    return True

for i in range(int(input())):
    n = int(input())

    if is_prime(n):
        print(n, "is a prime")
    else:
        print(n, "is not a prime")


from util import *

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('dataset_name', type=str, help='the name of dataset (KAIST or UCLA)')
    parser.add_argument('method_name', type=str, help='the name of method (GARL)')
    parser.add_argument('mode', type=str, help='train or test')
    args = parser.parse_args()

    dataset_conf = importlib.import_module('datasets.' + args.dataset_name + '.conf_temp').DATASET_CONF
    env_conf = importlib.import_module('env.conf_temp').ENV_CONF
    method_conf = importlib.import_module('methods.' + args.method_name + '.conf_temp_' + args.dataset_name).METHOD_CONF
    log_conf = importlib.import_module('log.conf_temp').LOG_CONF

    log_conf['dataset_name'] = args.dataset_name
    log_conf['method_name'] = args.method_name

    global_dict_init()
    set_global_dict_value('dataset_conf', dataset_conf)
    set_global_dict_value('env_conf', env_conf)
    set_global_dict_value('method_conf', method_conf)
    set_global_dict_value('log_conf', log_conf)

    fix_random_seed(get_global_dict_value('method_conf')['seed'])
    if args.mode == 'train':
        main = importlib.import_module('methods.' + args.method_name + '.main')
        main.main()
    elif args.mode == 'test':
        main_test = importlib.import_module('methods.' + args.method_name + '.main_test')
        main_test.main()


from fastapi import APIRouter
from fastapi import status, Path, Body
from fastapi import Response, HTTPException

from .models import Read, Replication, Write
from app.db import Database, ReplicationDatabase
from app.db import NoSuchKeyError
from app.db import KeyAlreadyExistsError
from app.restore import main as restore_process

router = APIRouter(
    prefix="/db",
    tags = ["Database Actions"],
    responses={404: {"description": "Not found"}}
)

def execute_db_action(action, db_type, *args, **kwargs):
    try:

        db = db_type()
        task = getattr(db, action)
        return task(*args, **kwargs)
    except NoSuchKeyError as nske:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=str(nske)
        )
    except KeyAlreadyExistsError as kaee:
        raise HTTPException(
            status_code=status.HTTP_409_CONFLICT,
            detail=str(kaee)
        )
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=str(e)
        )

@router.get(
    path="/",
    status_code=status.HTTP_200_OK,
)
def get_all():
    return {
        "keys": execute_db_action("get_keys", Database)
    }

@router.post(
    path="/",
    status_code=status.HTTP_201_CREATED,
)
def insert(obj: Write = Body(...)):
    obj = obj.dict()
    execute_db_action("save", Database, **obj)
    return { "result": "OK" }

@router.get(
    path="/{key}/",
    status_code=status.HTTP_200_OK,
)
def get(key: str = Path(...)):
    return {
        "value": execute_db_action("get", Database, key=key)
    }

@router.put(
    path="/{key}/",
    status_code=status.HTTP_200_OK,
    deprecated=True
)
def update(obj: Write = Body(...)):
    obj = obj.dict()
    execute_db_action("update", Database, **obj)
    return { "result": "OK" }

@router.delete(
    path="/{key}/",
    status_code=status.HTTP_204_NO_CONTENT,
)
def delete(key: str = Path(...), rep: Replication = Body(...)):
    execute_db_action("delete", Database, key = key, **(rep.dict()))
    return Response(status_code=status.HTTP_204_NO_CONTENT)

# ===================REPLICATION=====================================

@router.get(
    path="/replication/",
    status_code=status.HTTP_200_OK,
)
def rep_get_all():
    return {
        "keys": execute_db_action("get_keys", ReplicationDatabase)
    }

@router.post(
    path="/replication/",
    status_code=status.HTTP_201_CREATED,
)
def rep_insert(obj: Write = Body(...)):
    obj = obj.dict()
    execute_db_action("save", ReplicationDatabase, **obj)
    return { "result": "OK" }

@router.get(
    path="/replication/{key}/",
    status_code=status.HTTP_200_OK,
)
def rep_get(key: str = Path(...)):
    return {
        "value": execute_db_action("get", ReplicationDatabase, key=key)
    }

@router.put(
    path="/replication/{key}/",
    status_code=status.HTTP_200_OK,
    deprecated=True
)
def rep_update(obj: Write = Body(...)):
    obj = obj.dict()
    execute_db_action("update", ReplicationDatabase, **obj)
    return { "result": "OK" }

@router.delete(
    path="/replication/{key}/",
    status_code=status.HTTP_204_NO_CONTENT,
)
def rep_delete(key: str = Path(...), rep: Replication = Body(...)):
    execute_db_action("delete", ReplicationDatabase, key = key, **(rep.dict()))
    return Response(status_code=status.HTTP_204_NO_CONTENT)

@router.post(
    path="/replication/restore/",
    status_code=status.HTTP_200_OK
)
def restore(rep: Replication = Body(...)):
    restore_process(rep.node_url)


import json

FILE1 = 'train/result.json'
NEW = 'd1.txt'
IMG_DIR = 'd1/'

with open(NEW, 'w') as new:
    with open(FILE1, 'r') as file1:
        label_dict = json.load(file1)
        length = len(label_dict)
        idx = 0
        for name, label in label_dict.items():
            name = IMG_DIR + name + '.jpg'
            new.write(name)
            new.write('\t')
            new.write(str(label))
            if idx != length - 1:
                new.write('\n')
            idx += 1


import os
import torch
from setuptools import setup
from torch.utils.cpp_extension import BuildExtension, CUDAExtension

include_dirs = torch.utils.cpp_extension.include_paths()
print(include_dirs)
include_dirs.append('/media/yanshi/windows/attention_kernel/src')
include_dirs.append('/media/yanshi/windows/attention_kernel/pybind11-master/include')
print(include_dirs)

setup(
    name="attention_package",
    version="0.2",
    description="attention layer",
    # url="https://github.com/jbarker-nvidia/pytorch-correlation",
    author="Saurus",
    author_email="jia1saurus@gmail.com",
    ext_modules = [
        CUDAExtension(name='at_cuda',
        include_dirs = include_dirs,
        sources=['src/attention_kernel.cu', 'src/attention_cuda.cpp'])
    ],
    cmdclass={
        'build_ext' : BuildExtension
    }
)


# 7. Given a string, compute recursively (no loops) a new string where all the
# lowercase 'x' chars have been changed to 'y' chars.

def xtoy(string):
    if string == "":
        return string
    elif string[0] == "x":
        return "y" + xtoy(string[1:])
    else:
        return string[0] + xtoy(string[1:])

print(xtoy("taxxi"))


import json
import os
import warnings
from typing import Optional

import utils

SETTINGS_FILE_PATH = "settings.json"
WARN_CANNOT_OPEN = ("Предупреждение:"
                    f"Не удалось открыть файл настроек \'{SETTINGS_FILE_PATH}\'."
                    "Будут использованы настройки по молчанию.")
WARN_CANNOT_SAVE = ("Ошибка:"
                    f"Не удалось сохранить настройки в \'{SETTINGS_FILE_PATH}\'."
                    "Возможно доступ к файлу заблокирован.")

__settings = {
    "data_path": ".data.d"
}

# settings api

def get_data_path():
    return __settings["data_path"]

def save() -> bool:
    "Сохраняет настройки в файл. Возвращает False, если не удалось сохранить."
    try:
        with open(SETTINGS_FILE_PATH, 'wt', encoding='UTF-8') as file:
            json.dump(__settings, file, indent=2)
    except Exception:
        warnings.warn(WARN_CANNOT_SAVE)
        return False

    return True

# aux

@utils.once
def __init():
    """Перезаписывает параметры по-умолчанию значениями из файла настроек,
    если таковой имеется.
    """
    if not os.path.isfile(SETTINGS_FILE_PATH):
        return

    raw_settings: Optional[dict] = None
    try:
        with open(SETTINGS_FILE_PATH, 'rt', encoding='UTF-8') as file:
            raw_settings = json.load(file)
    except OSError:
        warnings.warn(WARN_CANNOT_OPEN)

    if raw_settings is None:
        return

    for k in (__settings.keys() & raw_settings.keys()):
        __settings[k] = raw_settings[k]

# initialize the module

__init()