# -*- coding: utf-8 -*-
from __future__ import absolute_import
import logging
import collections

from twisted.internet.defer import Deferred
from scrapy import signals

logger = logging.getLogger(__name__)

def scrape_items(crawler_runner, crawler_or_spidercls, *args, **kwargs):
    """
    Start a crawl and return an object (ItemCursor instance)
    which allows to retrieve scraped items and wait for items
    to become available.

    Example:

    .. code-block:: python

        @inlineCallbacks
        def f():
            runner = CrawlerRunner()
            async_items = scrape_items(runner, my_spider)
            while (yield async_items.fetch_next):
                item = async_items.next_item()
                # ...
            # ...

    This convoluted way to write a loop should become unnecessary
    in Python 3.5 because of ``async for``.
    """
    crawler = crawler_runner.create_crawler(crawler_or_spidercls)
    d = crawler_runner.crawl(crawler, *args, **kwargs)
    return ItemCursor(d, crawler)

class ItemCursor(object):
    def __init__(self, crawl_d, crawler):
        self.crawl_d = crawl_d
        self.crawler = crawler

        crawler.signals.connect(self._on_item_scraped, signals.item_scraped)
        crawler.signals.connect(self._on_error, signals.spider_error)

        crawl_d.addCallback(self._on_finished)
        crawl_d.addErrback(self._on_error)

        self.closed = False
        self._items_available = Deferred()
        self._items = collections.deque()

    def _on_item_scraped(self, item):
        # logger.info("_on_item_scraped")
        self._items.append(item)
        self._items_available.callback(True)
        self._items_available = Deferred()

    def _on_finished(self, result):
        # logger.info("_on_finished")
        self.closed = True
        self._items_available.callback(False)

    def _on_error(self, failure):
        self.closed = True
        self._items_available.errback(failure)

    @property
    def fetch_next(self):
        """
        A Deferred used with ``inlineCallbacks`` or ``gen.coroutine`` to
        asynchronously retrieve the next item, waiting for an item to be
        crawled if necessary. Resolves to ``False`` if the crawl is finished,
        otherwise :meth:`next_item` is guaranteed to return an item
        (a dict or a scrapy.Item instance).
        """
        if self.closed:
            # crawl is finished
            d = Deferred()
            d.callback(False)
            # logger.info("fetch_next: crawl is finished")
            return d

        if self._items:
            # result is ready
            d = Deferred()
            d.callback(True)
            # logger.info("fetch_next: result is ready")
            return d

        # We're active, but item is not ready yet. Return a Deferred which
        # resolves to True if item is scraped or to False if crawl is stopped.
        # logger.info("fetch_next: active, but items are not ready")
        return self._items_available

    def next_item(self):
        """Get a document from the most recently fetched batch, or ``None``.
        See :attr:`fetch_next`.
        """
        if not self._items:
            # logger.info("no next_item")
            return None
        # logger.info("got next_item")
        return self._items.popleft()


"""
Question #3 - Encode and Decode

Write a program that takes a string as input.
Print either the encoded or decoded string.
Logic is provided with the question.

Encoding:
Encoded string will only have characters and no digits.
Replace N consecutively repeated letters with the number N+2 followed by the letter (case sensitive).

Decoding:
Decoded string will have both characters and digits.
It will reverse the encoding logic.

Example:

Sample Input 1 - Encoding:
> AAAAAAaaaXMMMMMMMMMMMM

Sample Output 1:
> 8A5a3X14M

Sample Input 2 - Decoding:
> 8A5a3X14M

Sample Output 2:
> AAAAAAaaaXMMMMMMMMMMMM
"""

def encoder(text_to_encode):
    """Encodes a given string and returns the final value."""
    individual_letters = list(text_to_encode)  # Converting string and storing it as a list.
    unique_letters = []
    count_list = []
    new_count_list = []
    encoded_list = []

    # Getting unique letters from the string and storing it in a list.
    for x in individual_letters:
        if x not in unique_letters:
            unique_letters.append(x)
    # print(unique_letters)

    # Storing the number of occurrence of a letter in a list.
    for y in unique_letters:
        count_list.append(text_to_encode.count(y))
    # print(count_list)

    for value in count_list:
        value = str(value+2)
        new_count_list.append(value)

    for i in range(len(new_count_list)):
        encoded_list.append(new_count_list[i] + unique_letters[i])

    final_string = "".join(encoded_list)
    return final_string

def decoder(text_to_decode):
    """Decodes the provided string and returns the final value."""
    # list = [[8, "A"], [5, "a"], [3, "X"], [14, "M"]]
    content_list = []
    numbers_list = ["0","1","2","3","4","5","6","7","8","9"]
    mixed_list = []
    i = 0

    while i < len(text_to_decode):
        if text_to_decode[i] in numbers_list:
            if text_to_decode[i] and text_to_decode[i+1] in numbers_list:  # Check for another number after the current number.
                join_str = int(text_to_decode[i] + (text_to_decode[i+1]))  # Can only check for 2 digit numbers.
                content_list.append(join_str)
                text_to_decode = text_to_decode[:i+1] + text_to_decode[i+2:]
            else:
                content_list.append(int(text_to_decode[i]))
        else:
            content_list.append(text_to_decode[i])
        i += 1

    for i in range(0, len(content_list), 2):  # Reducing value of numbers by 2 in every 2nd position.
        content_list[i] -= 2
        mixed_list += content_list[i] * content_list[i+1]

    final_string = "".join(mixed_list)
    return final_string

myinputencode = "AAAAAAaaaXMMMMMMMMMMMM"
myinputdecode = "8A5a3X14M"

print("Encoding 'AAAAAAaaaXMMMMMMMMMMMM':", encoder(myinputencode))
print("Decoding '8A5a3X14M':", decoder(myinputdecode))


import os

# Define the game board
board = [[' ' for i in range(7)] for j in range(6)]

# Function to print the board
def print_board(board):
    os.system('cls' if os.name == 'nt' else 'clear')
    print('╔' + '═'*29 + '╗')
    for row in range(6):
        print('║ ', end='')
        for col in range(7):
            print(board[row][col] + ' │ ', end='')
        print('\n' + ('╠' if row < 5 else '╚') + '═'*29 + ('╣' if row < 5 else '╝'))

# Function to check if a player has won
def check_win(board, player):
    # Check rows
    for row in range(6):
        for col in range(4):
            if board[row][col] == player and board[row][col+1] == player and board[row][col+2] == player and board[row][col+3] == player:
                return True
    # Check columns
    for col in range(7):
        for row in range(3):
            if board[row][col] == player and board[row+1][col] == player and board[row+2][col] == player and board[row+3][col] == player:
                return True
    # Check diagonals
    for row in range(3):
        for col in range(4):
            if board[row][col] == player and board[row+1][col+1] == player and board[row+2][col+2] == player and board[row+3][col+3] == player:
                return True
    for row in range(3, 6):
        for col in range(4):
            if board[row][col] == player and board[row-1][col+1] == player and board[row-2][col+2] == player and board[row-3][col+3] == player:
                return True
    # If no win conditions are met, return False
    return False

# Function to check if the game is a draw
def check_draw(board):
    for row in range(6):
        for col in range(7):
            if board[row][col] == ' ':
                return False
    return True

# Main game loop
player = 'X'
while True:
    # Print the board
    print_board(board)

    # Get the player's move
    while True:
        try:
            col = int(input("Player " + player + ", choose a column (1-7): ")) - 1
            if col < 0 or col > 6:
                raise ValueError
            break
        except ValueError:
            print("Invalid input. Please enter a number between 1 and 7.")

    # Place the player's piece on the board
    for row in range(5, -1, -1):
        if board[row][col] == ' ':
            board[row][col] = player
            break

    # Check if the player has won
    if check_win(board, player):
        print_board(board)
        print("Player " + player + " wins!")
        break

    # Check if the game is a draw
    if check_draw(board):
        print_board(board)
        print("The game is a draw.")
        break

    # Switch to the other player
    if player == 'X':
        player = 'O'
    else:
        player = 'X'


import socket
from urllib.parse import unquote
from concurrent import futures
import os
import random

import pbb_pb2
import pbb_pb2_grpc
import grpc
from test import cleanup

class RegistryServicer(pbb_pb2_grpc.RegistryServicer):
    replica_list = []
    quorum_data = {
        "N": 0,
        "N_R": 0,
        "N_W": 0,
    }

    def is_quorum_satisfied(self):
        return (
            (self.quorum_data["N"] < self.quorum_data["N_R"] + self.quorum_data["N_W"])
            and (0 < self.quorum_data["N_R"] <= self.quorum_data["N"])
            and (0 < self.quorum_data["N_W"] <= self.quorum_data["N"])
        )

    def __init__(self):
        cleanup()

        while not self.is_quorum_satisfied():
            self.quorum_data["N"] = int(input("Number of replicas: "))
            self.quorum_data["N_R"] = int(input("Read quorum size: "))
            self.quorum_data["N_W"] = int(input("Write quorum size: "))
            if not self.is_quorum_satisfied():
                print("Ensure N < N_R + N_W, and 0 < N_R, N_W <= N.")

    def Register(self, request, context):
        address = request.address
        print(f"[.] REGISTER REQUEST FROM {address}")
        if any(r_address == address for r_address in self.replica_list):
            return pbb_pb2.RegisterResponse(
                status=pbb_pb2.Status.ERROR,
                message=f"Server {address} already registered.",
            )
        self.replica_list.append(address)
        replica_id = len(self.replica_list)
        path = os.path.join(os.getcwd(), f"data/replica_{replica_id}")
        if not os.path.exists(path):
            os.mkdir(path)
        return pbb_pb2.RegisterResponse(
            status=pbb_pb2.Status.OK,
            message=f"Server {address} registered.",
            replica_id=replica_id,
        )

    def GetReplicaList(self, request, context):
        quorum_size_map = {
            "READ": self.quorum_data["N_R"],
            "WRITE": self.quorum_data["N_W"],
            "ALL": self.quorum_data["N"],
        }
        replicas = self.replica_list
        if request.purpose is not None:
            quorum_size = quorum_size_map[request.purpose]
            replicas = random.sample(self.replica_list, quorum_size)
        return pbb_pb2.GetReplicaListResponse(replicas=replicas)

def serve():
    PORT = 56149
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(("", PORT))
    registry_address = f"[::]:{PORT}"
    registry = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    pbb_pb2_grpc.add_RegistryServicer_to_server(RegistryServicer(), registry)
    registry.add_insecure_port(registry_address)
    registry.start()
    print(f"[.] Registry node started on {registry_address}")
    registry.wait_for_termination()

if __name__ == "__main__":
    serve()


from inliner import transformers, utils
import ast

class FunctionInliner(ast.NodeTransformer):
    def __init__(self, functions_to_inline):
        self.inline_funcs = functions_to_inline

    def visit_Expr(self, node):
        node = self.generic_visit(node)
        if isinstance(node.value, ast.Assign):
            # A function call has turned into an assignment. Just return that instead
            return node.value
        return node

    def visit_Call(self, node):
        func = node.func
        func_name = utils.getFunctionName(func)
        if func_name in self.inline_funcs:
            func_to_inline = self.inline_funcs[func_name]
            transformer = transformers.getFunctionHandler(func_to_inline)
            if transformer is not None:
                node = transformer.inline(node, func_to_inline)

        return node


import os
import shutil
import subprocess
from yt15m.iface.editor import Editor

class FfmpegEditor(Editor):

    def __init__(self, ffmpeg_path='ffmpeg') -> None:
        super().__init__(ffmpeg_path)

    def split_equally(self, source_path, destination_path, segment_time="00:15:00") -> bool:
        pathname, extension = os.path.splitext(destination_path)
        destination_path = f"{pathname} %d{extension}"
        os.makedirs(os.path.dirname(destination_path), exist_ok=True)

        cmd = [
            self.context,
            "-i", source_path,
            "-f", "segment",
            "-map", "0",
            "-segment_time", segment_time,
            "-reset_timestamps", "1",
            "-c:v", "copy",
            "-c:a", "copy",
            destination_path
        ]

        result = subprocess.run(cmd, capture_output=True)

        print("\n")

        return True if result.returncode == 0 else False

    def draw_watermark(self, source_path, destination_path, watermark_image) -> bool:
        pathname, extension = os.path.splitext(destination_path)
        temp_path = f"{pathname}-wm{extension}"

        cmd = [
            self.context,
            "-y",
            "-hwaccel", "cuda",
            "-hwaccel_output_format", "cuda",
            "-i", source_path,
            "-i", watermark_image,
            "-filter_complex",
            (
                f"[1:v]format=rgba,colorchannelmixer=aa=0.1[logo];" +
                f"[0:v]scale_cuda=w=0:h=0:format=yuv420p:interp_algo=lanczos,hwdownload[video];" +
                f"[video][logo]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2,fps=fps=60,format=yuv420p"
            ),
            "-c:v", "h264_nvenc",
            "-c:a", "copy",
            temp_path
        ]

        result = subprocess.run(cmd, capture_output=True)

        print("\n")

        if result.returncode == 0:
            shutil.move(temp_path, destination_path)
            return True
        else:
            return False


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

import sys
import os

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

###

NLAGS = 20

###

## plot single plots
ylimdict = {\
	'absh': [-0.1, 0.07],
	't2m': [-0.08, 0.15],
	'utci': [-0.06, 0.02],
	'tp': [-0.02, 0.02],
	'ssrd': [-0.03, 0.03],
	'r': [-0.1, 0.1]
}

variablesdict = {
	'absh': 'Absolute humidity [g m-3]',
	'r': 'Relative humidity [\%]',
	't2m': 'Temperature [degree K]',
	'utci': 'UTCI [-]',
	'tp': 'Precipitation [mm day-1]',
	'ssrd': 'Solar radiation [W m-2]'
}

variables = ['t2m', 'utci', 'tp', 'ssrd'		]

for i, variable in enumerate(variables):

	fig, ax = plt.subplots(figsize=(6,4))

	xi = 0.5

	## plot coeffs of lags
	for lag in range(1, NLAGS+1, 1):
		resultfile = 'coeffs_firstdiff_L20_pol1_nomob_10.csv'
		try:
			df = pd.read_csv(os.path.join('./results', resultfile))
			if df.iloc[:, 0].apply(lambda x: x[:2] == variable[:2]).sum() < 1:
				continue
		except:
			print('File not found: ', os.path.join('./results', resultfile))
			continue
		x = np.array([xi, xi+3-0.1])
		y = np.array([float(df.loc[df.iloc[:, 0] == (variable+'_L'+str(lag)), 'Estimate'].values)]*2)
		yerr = np.array([float(df.loc[df.iloc[:, 0] == variable+'_L'+str(lag), 'Cluster.s.e.'].values)]*2)
		ax.plot(x, y, color='b', lw=1.)
		ax.fill_between(x, y1=y-1.96*yerr, y2=y+1.96*yerr, color='b', alpha=0.3, lw=0.)
		xi += 3.

	## plot cumulative coeffs
	resultfile = 'coeffscum_firstdiff_L20_pol1_L-cum5_nomob_10_{0:s}.csv'.format(variable)
	try:
		df = pd.read_csv(os.path.join('./results', resultfile))
	except:
		print('File not found: ', os.path.join('./results', resultfile))
		continue
	x = -4.
	y = df['coeffs'].values
	yerr = df['se'].values
	ax.plot(x, y, 'o', color='b', lw=1., markersize=5.)
	ax.errorbar(x, y, yerr=1.96*yerr, fmt='o', capthick=2, markersize=0., lw=1., color='b')

	resultfile = 'coeffscum_firstdiff_L20_pol1_L-cum_nomob_10_{0:s}.csv'.format(variable)
	df = pd.read_csv(os.path.join('./results', resultfile))
	x = -9.
	y = df['coeffs'].values
	yerr = df['se'].values
	ax.plot(x, y, 'o', color='b', lw=1., markersize=5.)
	ax.errorbar(x, y, yerr=1.96*yerr, fmt='o', capthick=2, markersize=0., lw=1., color='b')

	ax.set_xlim(-14., NLAGS * 3 + 1)
	ax.set_xticks([-9., -4.] + list(np.arange(1, NLAGS * 3 + 1, 6)))
	ax.set_xticklabels(['c', 'c5'] + [str(i) for i in np.arange(1, NLAGS * 3 + 1, 6)])
	ax.set_xlabel('Days before reporting')

	ax.annotate(s=variablesdict[variable], xy=(0.95, 0.99), xycoords='axes fraction', ha='right', va='top')
	ax.plot([-14., NLAGS * 3.], [0., 0.], 'k-', lw=0.5)
	ax.set_ylabel('Coefficient of lag')
	sns.despine(ax=ax, offset=1., right=True, top=True)
	fig.savefig(os.path.join('./figures/', 'coeffs_distlags_{0:s}.png'.format(variable)), bbox_inches='tight')


import numpy as np
import matplotlib.pyplot as plt
import problem_1

def create_matrix(n: int) -> np.array:
    A = np.zeros((n, n))
    for i in range (n):
        for j in range(n):
            if i == j:
                A[i, j] =  2.0 + (np.float64(i) / np.float64(n)) ** 2.0
            elif j == i - 1 or j == i + 1:
                A[i, j] =  -1.0
            elif (i == 0 and j == n - 1) or (i == n - 1 and j == 0):
                A[i, j] = -1.0
            else:
                A[i, j] = 0.0

    return A

def create_f(n: int) -> np.array:
    f = np.zeros(n)
    for i in range(n):
        f[i] = (1.0 + n**2.0 * np.sin(np.pi / n) ** 2.0) * np.sin( (2.0 * np.pi * i) / np.float64(n))

    return f

def gershgorin_circles(a: np.array):
    for i, row in enumerate(a):
        a_ii = a[i, i]
        r_i = np.abs(row).sum() - a_ii
        print(f"D({a_ii:5.3f}; {r_i:5.3f})")

def LUP_decomposition(A):
    n = A.shape[0]
    P = np.eye(n)
    L = np.zeros((n, n))
    U = np.copy(A)

    for j in range(n):
        max_row = np.argmax(np.abs(U[j:, j])) + j

        if j != max_row:
            P[[j, max_row]] = P[[max_row, j]]
            L[[j, max_row], :j] = L[[max_row, j], :j]
            U[[j, max_row], j:] = U[[max_row, j], j:]

        L[j, j] = 1.0
        for i in range(j + 1, n):
            L[i, j] = U[i, j] / U[j, j]
            U[i, j:] -= L[i, j] * U[j, j:]

    return P, L, U

def LUP_solve(A: np.array, f: np.array):
    P, L, U = LUP_decomposition(A)

    z = np.matmul(P, f)
    x = problem_1.LU_solve(L, U, z)

    return x

def get_eigvals_krylov(A):
    size = A.shape[0]
    y_matrix = np.zeros((size, size))
    y = np.ones(n)

    for i in range(size - 1, -1, -1):
        y_matrix[:, i] = y
        y = np.matmul(A, y)

    ps = LUP_solve(y_matrix, y)

    coeffs = np.concatenate([np.ones(1), -1.0 * ps])

    return np.array(sorted(np.roots(coeffs)))

def solve(A, f, tau, eps=1e-6, iter_limit = 10000):
    size = A.shape[0]
    tau_f = f * tau
    cur = np.zeros(size)
    b = np.eye(size) - tau * A
    err_list = []

    for i in range(iter_limit):
        cur = np.matmul(b, cur) + tau_f
        err = f - np.matmul(A, cur)
        norm_err = problem_1.m_3(err)
        err_list.append(norm_err)

        if norm_err < eps:
            break

    solution = cur
    return solution, err_list

def output_solve(A, f, tau, eps=1e-6):
    solution, errors = solve(A, f, tau=tau, eps=eps)
    exact_sol = np.linalg.solve(A, f)
    delta = solution - exact_sol
    print("---------------------------------------------------")
    print("solution")
    print(f"tau = {tau}; res = {solution}")
    print(f"|numpy_sol - sol|_3 = {problem_1.m_3(delta)}")
    print("---------------------------------------------------")

    num_errors = np.arange(len(errors))
    line, = plt.plot(num_errors, errors, marker='.')
    line.set_label(f"tau = {tau}")

if __name__ == '__main__':
    n = 6
    A = create_matrix(n)
    f = create_f(n)

    print("---------------------------------------------------")
    print("eigenvalue estimation using Gershgorin circles")
    gershgorin_circles(A)
    print("---------------------------------------------------")

    krylov_eigvals = get_eigvals_krylov(A)
    krylov_eigvals.sort()
    numpy_eigvals = np.linalg.eigvals(A)
    numpy_eigvals.sort()

    print("---------------------------------------------------")
    print("eigenvals (krylov vs numpy)")
    for i in range (len(krylov_eigvals)):
        l_k = krylov_eigvals[i]
        l_n = numpy_eigvals[i]
        print(f"lambda_{i}: {l_k:10.6f} vs {l_n:10.6f}; delta = {abs(l_n - l_k)}")
    print("---------------------------------------------------")

    tau_opt_krylov = 2.0 / (krylov_eigvals.min() + krylov_eigvals.max())
    tau_opt_numpy = 2.0 / (numpy_eigvals.min() + numpy_eigvals.max())

    print(f"tau_opt_numpy  = {tau_opt_numpy}")
    print(f"tau_opt_krylov = {tau_opt_krylov}")

    output_solve(A, f, tau_opt_numpy)
    output_solve(A, f, tau_opt_krylov)
    output_solve(A, f, tau=0.4)
    output_solve(A, f, tau=0.5)

    plt.yscale("log")
    plt.ylabel('mismatch rate')
    plt.xlabel('number of iterations')
    plt.grid()
    plt.legend()
    plt.show()


import torch
torch.set_float32_matmul_precision('high')
import argparse

## Parser Arguments
parser = argparse.ArgumentParser(description='Masterarbeit Segformer Parser')

parser.add_argument('--project', type=str, default='Masterarbeit Segformer')
parser.add_argument('--run', type=str, default='Segformer_train')
parser.add_argument('--backbone', type=str, default='b2')
parser.add_argument('--epochs', type=int, default=250)  # gpu: 250, local:1
parser.add_argument('--lr', type=float, default=6e-5) # gpu: default=6e-5, local:2e-1
parser.add_argument('--dataset', type=str, default='TLESS')
parser.add_argument('--root', type=str, default='./data/tless')
parser.add_argument('--train_split', type=str, default='train_pbr')
parser.add_argument('--val_split', type=str, default='train_pbr')
parser.add_argument('--val_size', type=str, default=200)
parser.add_argument('--checkpoints', type=str, default='./checkpoints')
parser.add_argument('--load_checkpoints', type=str, default=None)

parser.add_argument('--use_scaling', type=bool, default=True)
parser.add_argument('--use_cropping', type=bool, default=True)
parser.add_argument('--use_flipping', type=bool, default=True)

args = parser.parse_args()

## W&B Logging (optional)
ENTITY = 'gaolilli'   # change this to your W&B username
PROJECT = args.project
RUN_NAME = args.run

## Training & Hyperparameters
DEVICES = [0, 1, 2, 3]      # use 4 GPUs on the HPC. For local training or ipf server, just use one GPU.
#DEVICES = 1    #cpu
NUM_EPOCHS = args.epochs
BACKBONE = args.backbone
MOMENTUM = 0.9
WEIGHT_DECAY = 0.01
PRECISION = '16-mixed'   #gpu: '16-mixed', cpu: bf16-mixed
LEARNING_RATE = args.lr

## Data Augmentation
USE_SCALING = args.use_scaling
USE_CROPPING = args.use_cropping
USE_FLIPPING = args.use_flipping

## Dataset & Dataloader
DATASET = args.dataset
ROOT = args.root
TRAIN_SPLIT = args.train_split
VAL_SPLIT = args.val_split
VAL_SIZE = args.val_size
CHECKPOINTS_DIR = args.checkpoints
LOAD_CHECKPOINTS = args.load_checkpoints

if DATASET == 'TLESS':
    NUMBER_TRAIN_IMAGES = 50000 # 50000 total
    NUMBER_VAL_IMAGES = 10080  # 10080 total
    BATCH_SIZE = 8  # 8 for train on server with gpu, 2 for cpu
    NUM_CLASSES = 30
    IGNORE_INDEX = 255
    NUM_WORKERS = 8


import itertools
from pathlib import Path
from typing import Any, Callable, Dict, List

from ztrack.datatype import Event

class EventFileReader(object):
    """It is responsible for read one event file"""

    def __init__(self, name: str, event_file_path: Path) -> None:
        self._name = name
        self._event_file_path = event_file_path
        self._filters = []

    def add_filter(self, cb: Callable[[Event], bool]):
        self._filters.append(cb)
        return self

    def iter_read(self):
        with self._event_file_path.open('r') as _event_file:
            while True:
                line = _event_file.readline()
                # eof
                if not line:
                    return None

                event = Event.from_json(line)

                filtered = True
                for filter in self._filters:
                    if not filter(event):
                        filtered = False
                        break

                if not filtered:
                    continue

                yield event

    def read_all(self) -> List[Event]:
        events = []

        for event in self.iter_read():
            events.append(event)
        return events

    def datas_to_pandas(self, stop=None, normalize_json=False):
        import pandas as pd
        datas = []

        it = itertools.islice(self.iter_read(), stop)
        datas = [evt.data for evt in it]

        if normalize_json:
            return pd.json_normalize(datas)
        return pd.DataFrame(datas)

class Reader(object):
    """Reader used to read tracked events directories and files"""

    def __init__(self, result_dir: Path) -> None:
        self._result_dir = result_dir

    def event_reader(self, reporter: str = 'default') -> EventFileReader:
        event_file_path = self._result_dir / f"{reporter}.event.json"
        if not event_file_path.exists():
            raise ValueError(
                f"event reporter file, reporter={reporter}, path={event_file_path.as_posix()} not exists")
        return EventFileReader(reporter, event_file_path)


from typing import Literal
from binarySearchTree import Node

# O(hm)
# h = height of the tree
# m = length of list (list.copy())
def parentList(root: Node, node: Node) -> list[int] | Literal[-1]:
    '''
        returns list of parents in order for node
    '''
    queue: list[Node] = [ root ]
    parent: dict = { root.data: [] }

    while queue:
        curr = queue[0]
        del queue[0]

        if curr == node:
            return parent[curr.data]

        # iteration
        if curr.left:
            queue.append(curr.left)
            parent[curr.left.data] = parent[curr.data].copy()
            parent[curr.left.data].append(curr.data)

            if curr.left == node:
                return parent[curr.left.data]

        if curr.right:
            queue.append(curr.right)
            parent[curr.right.data] = parent[curr.data].copy()
            parent[curr.right.data].append(curr.data)

            if curr.right == node:
                return parent[curr.right.data]

    return -1

# O(nm+2h)
# n > m
# n = lenght of list (set conversation)
# m = length of list (iterating each element)
# h = height of the tree (h = log(n) to n)
def commonAncestor(root: Node, node1: Node, node2: Node) -> int:
    '''
        returns the first common parent for node1 and node2
    '''
    parent1: list[int] = parentList(root, node1)
    parent2: list[int] = parentList(root, node2)

    if parent1 == -1 or parent2 == -1:
        return -1

    if len(parent1) > len(parent2):
        parent1 = set(parent1)

        for parent in parent2:
            if parent in parent1:
                return parent

    else:
        parent2 = set(parent2)

        for parent in parent1:
            if parent in parent2:
                return parent

    return -1

'''
        (3)
       ↙    ↘
     (6)     (8)
   ↙  ↘      ↙   ↘
(10) (21)   (13) (11)
           ↙   ↘
        (14)   (24)
'''

if __name__ == '__main__':
    # binary Tree
    root: Node = Node(3)
    root.left = Node(6)
    root.right = Node(8)

    root.left.left = Node(10)
    root.left.right = Node(21)

    root.right.left = Node(13)
    root.right.right = Node(11)

    root.right.left.left = Node(14)
    root.right.left.right = Node(24)

    #                          21               24
    print(commonAncestor(root, root.left.right, root.right.left.right))


import datetime
import math
from concurrent.futures import ProcessPoolExecutor, wait, as_completed

def fun_factor(num:int):
    return math.factorial(num)

big_nums = [[1550]*100]*100
if __name__ == '__main__':
    start = datetime.datetime.utcnow()
    executor = ProcessPoolExecutor()
    futures = []
    for lst in big_nums:
        for num in lst:
            future = executor.submit(fun_factor, num)
            futures.append(future)
    end1 = datetime.datetime.utcnow()
    for future in as_completed(futures):
        print(future.result())
    end = datetime.datetime.utcnow()
    print(f"Time took for calaulting: {(end1 - start).total_seconds()}s---------------------------------------")
    print(f"Time took for all: {(end - start).total_seconds()}s---------------------------------------")


import numpy as np
import pandas as pd

#range(0,729) is hog10 729 features
#range(0,144) is hog20 144 features
#range(0,100) is projectionhistrogram 100 features

def ReadDataset(datasetName,feature):
	filedData = []
	ClassLabel= ['Class']
	if feature == 'hog10':
		for i in range(0,729):
			filedData.append('hog10_'+str(i))
	if feature == 'hog20':
		for i in range(0,144):
			filedData.append('hog20_'+str(i))
	if feature == 'ph':
		for i in range(0,100):
			filedData.append('proHist'+str(i))
	if feature == 'all':
		for i in range(0,729):
			filedData.append('hog10_'+str(i))
		for i in range(0,144):
			filedData.append('hog20_'+str(i))
		for i in range(0,100):
			filedData.append('proHist'+str(i))
	#AppendNameData

	Features_panda= pd.read_csv(datasetName,usecols=filedData)
	Classlabels_panda= pd.read_csv(datasetName,usecols=ClassLabel)
	# Featureslist = Features_panda.values.tolist()
	# Classlabeslist = Classlabels_panda.values.tolist()
	# Data_include = [Featureslist[1], Classlabeslist[1]]
	#can test data with
	#Data_include = [Featureslist[1], Classlabeslist[1]]
	#and change range like (0,2)
	#[[0.023529411764705882, 0.9686274509803922], [0.0]]
	#   ^                   ^
	#   ph0   index2        ph1 index2          class label index2

	Featureslist = Features_panda.values
	Classlabeslist = Classlabels_panda.values.reshape(-1)

	# return Features_panda,Classlabels_panda,Data_include
	return Featureslist,Classlabeslist

if __name__ == "__main__":
	FeatureData,ClassData, = ReadDataset('TestData_1.csv','ph')


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

# In[1]:

import spacy # nlp
import pdfminer # pdf2txt
import re # regex
import os # file manipulation
import pandas as pd # csv - tabular

# In[2]:

import pdf2txt

# In[3]:

def convert_pdf(file_name):
    filename_txt = os.path.basename(os.path.splitext(file_name)[0]) + ".txt" # basename but with txt instead of pdf
    filepath_txt = os.path.join("output/txt/", filename_txt) # output path
    pdf2txt.main(args=[file_name, "--outfile", filepath_txt]) # pdf to txt and save it in the given location
    print(f"{filepath_txt} saved successfully!")
    with open(filepath_txt) as to_read:
        return to_read.read()
    # return open(filepath_txt).read()

# In[4]:

nlp = spacy.load("en_core_web_sm") # load the language model

# In[5]:

result_dict = {'name': [], 'phone': [], 'email': [], 'skills': []}
names = []
phones = []
emails = []
skills = []

# In[6]:

def parse_content(text):
    skillset = re.compile("python|java|sql|hadoop|tableau")
    phone_num = re.compile(
    "(\d{3}[-\.\s]??\d{3}[-\.\s]??\d{4}|\(\d{3}\)\s*\d{3}[-\.\s]??\d{4}|\d{3}[-\.\s]??\d{4})"
    )
    nlp_text = nlp(text)
    name = [entity.text for entity in nlp_text.ents if entity.label_ == "PERSON"][0]
    email = [word for word in nlp_text if word.like_email is True][0]
    print(f"Name: {name}, E-mail: {email}")
    phone = re.findall(phone_num, text.lower())
    skills_list = re.findall(skillset, text.lower())
    unique_skills_list = list(set(skills_list))

    names.append(name.strip())
    emails.append(email)
    phones.append(phone)
    skills.append(unique_skills_list)

    print("Extraction completed successfully!\n")

# In[7]:

def for_file_in_ls():
    for file_name in os.listdir('resumes/'):
        if file_name.endswith('.pdf'):
            print(f"Reading {file_name}")
            txt = convert_pdf(os.path.join('resumes/', file_name))
            parse_content(txt)

# In[8]:

for_file_in_ls()

# In[13]:

result_dict["name"] = names
result_dict["phone"] = phones
result_dict["email"] = emails
result_dict["skills"] = skills

# In[15]:

result_df = pd.DataFrame(result_dict)
result_df

# In[28]:

result_df.to_csv("output/csv/parsed_resumes.csv")


import json
import os
import pandas as pd
import random
import scipy.stats as stats
from statistics import mean, stdev

def get_questionnaire(questionnaire_name):
    try:
        with open('questionnaires.json') as dataset:
            data = json.load(dataset)
    except FileNotFoundError:
        raise FileNotFoundError("'questionnaires.json' file not exist.")

    # Matching by questionnaire_name in dataset
    questionnaire = None
    for item in data:
        if item["name"] == questionnaire_name:
            questionnaire = item

    if questionnaire is None:
        raise ValueError("Questionnaire not found.")

    return questionnaire

def get_scenarios():
    try:
        with open('situations.json') as dataset:
            scenarios = json.load(dataset)
    except FileNotFoundError:
        raise FileNotFoundError("'situations.json' file not exist.")

    return scenarios

def generate_scenarios(scenario_file, select_time, target_emotions):
    scenarios = get_scenarios()
    headers_list = []
    scenarios_list = []

    for emotion in scenarios["emotions"]:
        emotion_name = emotion["name"]

        if ('ALL' not in target_emotions) and (emotion_name not in target_emotions):
            continue

        for index, factor in enumerate(emotion["factors"]):
            headers_list.append(f'{emotion_name}-{index}')
            if select_time > len(factor["scenarios"]):
                selected_scenarios = factor["scenarios"]
            else:
                selected_scenarios = random.sample(factor["scenarios"], select_time)
            scenarios_list.append([factor["name"]] + selected_scenarios)

    # Convert each list to a DataFrame
    df_list = [pd.DataFrame(data, columns=[header]) for header, data in zip(headers_list, scenarios_list)]
    output_df = pd.concat(df_list, axis=1)
    output_df.to_csv(scenario_file, index=False)

    return output_df

def generate_testfile(questionnaire, args):
    scenarios_csv = args.scenarios_file
    output_csv = args.testing_file

    test_times = args.test_count
    default_shuffle_times = args.default_shuffle_count
    emotion_shuffle_times = args.emotion_shuffle_count
    shuffle_times = max(default_shuffle_times, emotion_shuffle_times)

    # Extract the scenarios file
    emotions_selection = args.emotion
    select_times = args.select_count
    if emotions_selection != 'Customize':
        scenarios_df = generate_scenarios(scenarios_csv, select_times, emotions_selection.split(','))
    else:
        try:
            scenarios_df = pd.read_csv(scenarios_csv)
        except:
            raise FileNotFoundError("'situations.csv' file not exist.")

    output_df = pd.DataFrame()
    questions_list = questionnaire["questions"]
    question_num = len(list(questions_list.keys()))
    headers_list = []
    data_list = []

    for shuffle_count in range(shuffle_times + 1):
        question_indices = list(questions_list.keys())

        # Shuffle the question indices
        if shuffle_count != 0:
            random.shuffle(question_indices)
        questions = [f'{index}. {questions_list[question]}' for index, question in enumerate(question_indices, 1)]

        # Append questions column
        headers_list.append(f"question-{shuffle_count}")
        data_list.append([f'Prompt: {questionnaire["prompt"]}'] + questions)

        # Append order column
        headers_list.append(f"order-{shuffle_count}")
        data_list.append([questionnaire["prompt"]] + question_indices)

    output_df.to_csv(output_csv, index=False)

    # For each shuffled order,
    for shuffle_count in range(default_shuffle_times + 1):
        for test_count in range(test_times):
            headers_list.append(f'General_test-{test_count}_order-{shuffle_count}')
            data_list.append([' '] + [''] * question_num)

    # For each scenario, create a new column in output_df for each question order
    headers = scenarios_df.columns.tolist()
    for factor in headers:
        for s_index, scenario in enumerate(scenarios_df[factor].iloc[1:].dropna().astype(str)):
            for shuffle_count in range(emotion_shuffle_times + 1):
                for test_count in range(test_times):
                    # Append scenario column
                    headers_list.append(f'{factor}_scenario-{s_index}_test-{test_count}_order-{shuffle_count}')
                    data_list.append([f'Imagine you are the protagonist in the scenario: "{scenario}"'] + [''] * question_num)

    # Create a DataFrame from the data_list and new_header_list, and save it to a CSV file
    data_dict = {header: data for header, data in zip(headers_list, data_list)}
    output_df = pd.DataFrame(data_dict)
    output_df.to_csv(output_csv, index=False)

def convert_data(questionnaire, scenarios_csv, testing_csv):
    try:
        # Read scenarios_csv to extract all headers
        scenarios_df = pd.read_csv(scenarios_csv)
        scenarios_headers = scenarios_df.columns.tolist()

        # Extract unique emotions from the column headers
        emotions_list = []
        for h in scenarios_headers:
            if h.split("_")[0].split("-")[0] not in emotions_list:
                emotions_list.append(h.split("_")[0].split("-")[0])

        # Create a dictionary of factors with the headers as keys and names as values (first row of the DataFrame)
        factors_list = {h : scenarios_df[h].iloc[0] for h in scenarios_headers}

    except Exception as e:
        raise ValueError(f'{scenarios_csv} has problems: {str(e)}')

    try:
        # Read testing_csv file to extract all headers
        testing_df = pd.read_csv(testing_csv)
        testing_headers = testing_df.columns.tolist()

        # Extract all questions orders
        orders_list = {h: testing_df[h].iloc[1:].astype(int).tolist() for h in testing_headers if h.startswith("order")}

    except Exception as e:
        raise ValueError(f'{testing_csv} has problems: {str(e)}')

    # Store the tested data into a dictionary with proper mapping
    tested_data = {}
    for h in testing_headers:
        if not h.startswith(("question", "order")):
            try:
                tested_data[h] = {
                    # Map the data to its corresponding order
                    i : questionnaire["scale"] - int(val) if i in questionnaire["reverse"] else int(val)
                    for i,val in zip(orders_list[h.split("_")[-1]], testing_df[h].iloc[1:].tolist())
                }
            except:
                raise ValueError(f'Error in {testing_csv}: Some cells in column "{h}" cannot be converted to integers.')

    # Create a dictionary to store the mapped data according to the corresponding emotion and factor
    organized_data = {
        emotion : {
            factor : {
                "factor_name" : factors_list[factor],
                "data" : []
            } for factor in factors_list if factor.startswith(emotion)
        } for emotion in emotions_list
    }
    organized_data['General'] = []

    # Organize the data into the mapped_data dictionary based on emotions and factors
    for key in tested_data:
        emotion = key.split("_")[0].split("-")[0]
        if emotion == 'General':
            # If the emotion is 'General', add the data directly to the 'General' category
            organized_data['General'].append(tested_data[key])
        else:
            # Add other data to the corresponding emotion and factor category based on their keys
            factor = key.split("_")[0]
            organized_data[emotion][factor]["data"].append(tested_data[key])

    return organized_data

def compute_statistics(questionnaire, data_list):
    cat_list = []
    results = []

    for cat in questionnaire["categories"]:
        scores_list = []

        for data in data_list:
            scores = []
            for key in data:
                if key in cat["cat_questions"]:
                    scores.append(data[key])

            # Getting the computation mode (SUM or AVG)
            if questionnaire["compute_mode"] == "SUM":
                scores_list.append(sum(scores))
            elif questionnaire["compute_mode"] == "SUM*2":
                scores_list.append(sum(scores)*2)
            else:
                scores_list.append(mean(scores))

        results.append([mean(scores_list), stdev(scores_list), len(scores_list)])
        cat_list.append(cat["cat_name"])

    return results, cat_list    # ([mean, std, size], cat_list)

def hypothesis_testing(result1, result2, cat_list, significance_level, title):
    output_list = f'| {title} |'
    output_text = f'### {title}\n'

    for i, cat_name in enumerate(cat_list):
        output_text += f'\n##### {cat_name}'

        # Extract the mean, std and size for both data sets
        mean1, std1, n1 = result1[i]
        mean2, std2, n2 = result2[i]
        # Add an epsilon to prevent the zero standard deviarion
        epsilon = 1e-8
        std1 += epsilon
        std2 += epsilon

        output_text += '\n- **Statistic**:\n'
        output_text += f'Corresponding Factor:\tmean1 = {mean1:.1f},\tstd1 = {std1:.1f},\tn1 = {n1}\n'
        output_text += f'Default:\tmean2 = {mean2:.1f},\tstd2 = {std2:.1f},\tn2 = {n2}\n'

        # Perform F-test
        output_text += '\n- **F-Test:**\n\n'

        if std1 > std2:
            f_value = std1 ** 2 / std2 ** 2
            df1, df2 = n1 - 1, n2 - 1
        else:
            f_value = std2 ** 2 / std1 ** 2
            df1, df2 = n2 - 1, n1 - 1

        p_value = (1 - stats.f.cdf(f_value, df1, df2)) * 2
        equal_var = True if p_value > significance_level else False

        output_text += f'\tf-value = {f_value:.4f}\t($df_1$ = {df1}, $df_2$ = {df2})\n\n'
        output_text += f'\tp-value = {p_value:.4f}\t(two-tailed test)\n\n'
        output_text += '\tNull hypothesis $H_0$ ($s_1^2$ = $s_2^2$): '

        if p_value > significance_level:
            output_text += f'\tSince p-value ({p_value:.4f}) > α ({significance_level}), $H_0$ cannot be rejected.\n\n'
            output_text += f'\t**Conclusion ($s_1^2$ = $s_2^2$):** The variance of LLM\'s average responses in this factor is statistically equal to the variance of general.\n\n'
        else:
            output_text += f'\tSince p-value ({p_value:.4f}) < α ({significance_level}), $H_0$ is rejected.\n\n'
            output_text += f'\t**Conclusion ($s_1^2$ ≠ $s_2^2$):** The variance of LLM\'s average responses in this factor is statistically unequal to the variance of general.\n\n'

        # Performing T-test
        output_text += '- **Two Sample T-Test (Equal Variance):**\n\n' if equal_var else '- **Two Sample T-test (Welch\'s T-Test):**\n\n'

        df = n1 + n2 - 2 if equal_var else ((std1**2 / n1 + std2**2 / n2)**2) / ((std1**2 / n1)**2 / (n1 - 1) + (std2**2 / n2)**2 / (n2 - 1))
        t_value, p_value = stats.ttest_ind_from_stats(mean1, std1, n1, mean2, std2, n2, equal_var=equal_var)

        output_text += f'\tt-value = {t_value:.4f}\t($df$ = {df:.1f})\n\n'
        output_text += f'\tp-value = {p_value:.4f}\t(two-tailed test)\n\n'

        output_text += '\tNull hypothesis $H_0$ ($µ_1$ = $µ_2$): '
        if p_value > significance_level:
            output_text += f'\tSince p-value ({p_value:.4f}) > α ({significance_level}), $H_0$ cannot be rejected.\n\n'
            output_text += f'\t**Conclusion ($µ_1$ = $µ_2$):** The average of LLM\'s responses in this factor is assumed to be equal to the average of general.\n\n'

            # output_list += f' $-$ ({'+' if (mean1 - mean2) > 0 else ''}{(mean1 - mean2):.1f}) |'
            # output_list += f' $-$ ({'+' if (mean1 - mean2) > 0 else ''}{(mean1 - mean2):.1f}) |'
            output_list += f''' $-$ ({'+' if (mean1 - mean2) > 0 else ''}{(mean1 - mean2):.1f}) |'''

        else:
            output_text += f'Since p-value ({p_value:.4f}) < α ({significance_level}), $H_0$ is rejected.\n\n'
            if t_value > 0:
                output_text += '\tAlternative hypothesis $H_1$ ($µ_1$ > $µ_2$): '
                output_text += f'\tSince p-value ({(1-p_value/2):.1f}) > α ({significance_level}), $H_1$ cannot be rejected.\n\n'
                output_text += f'\t**Conclusion ($µ_1$ > $µ_2$):** The average of LLM\'s responses in this factor is assumed to be larger than the average of general.\n\n'

                output_list += f' $\\uparrow$ (+{(mean1 - mean2):.1f}) |'
            else:
                output_text += '\tAlternative hypothesis $H_1$ ($µ_1$ < $µ_2$): '
                output_text += f'\tSince p-value ({(1-p_value/2):.1f}) > α ({significance_level}), $H_1$ cannot be rejected.\n\n'
                output_text += f'\t**Conclusion ($µ_1$ < $µ_2$):** The average of LLM\'s responses in this factor is assumed to be smaller than the average of general emotion.\n\n'

                output_list += f' $\\downarrow$ ({(mean1 - mean2):.1f}) |'

    output_list += f' {result1[0][2]} |\n'
    return (output_text, output_list)

def analysis_results(questionnaire, args):
    scenarios_csv = args.scenarios_file
    testing_csv = args.testing_file
    result_file = args.results_file
    significance_level = args.significance_level

    overall_list = '# PANAS Results Analysis\n'
    markdown_output = ''    # overall markdown output text
    overall_data = []

    data = convert_data(questionnaire, scenarios_csv, testing_csv)

    general_results, cat_list = compute_statistics(questionnaire, data['General'])

    overall_list += '| Emotions | ' + ' | '.join(cat_list) + ' | N |\n'
    overall_list += '| :---: |' + ' | '.join([':---:' for i in cat_list]) + ' | :---: |\n'
    overall_list += '| Default |' + ' | '.join([f'{r[0]:.1f} $\\pm$ {r[1]:.1f}' for r in general_results]) + f' | {general_results[0][2]} |\n'

    # Analyze the results for each emotion
    for emotion in data:
        if emotion == 'General':
            continue

        emotion_output = ''     # markdown output text for the current emotion

        emotion_list = f'## {emotion}\n'
        emotion_list += '| Factors | ' + ' | '.join(cat_list) + ' | N |\n'
        emotion_list += '| :---: |' + ' | '.join([':---:' for i in cat_list]) + ' | :---: |\n'
        emotion_list += '| Default |' + ' | '.join([f'{r[0]:.1f} $\\pm$ {r[1]:.1f}' for r in general_results]) + f' | {general_results[0][2]} |\n'

        emotion_data = []   # the data that belongs to the current emotion

        # Analyze the results for each factor
        for factor in data[emotion]:
            emotion_data += data[emotion][factor]["data"]
            overall_data += data[emotion][factor]["data"]
            results, _ = compute_statistics(questionnaire, data[emotion][factor]["data"])
            text_msg, list_msg = hypothesis_testing(results, general_results, cat_list, significance_level, data[emotion][factor]["factor_name"])
            emotion_output += text_msg
            emotion_list += list_msg

        results, _ = compute_statistics(questionnaire, emotion_data)
        text_msg, list_msg = hypothesis_testing(results, general_results, cat_list, significance_level, 'Overall')
        emotion_output += text_msg
        emotion_list += list_msg
        overall_list += list_msg.replace('Overall', emotion)
        markdown_output += emotion_list + '\n'+ emotion_output

    markdown_output += f'## Emotions Overall\n'
    results, _ = compute_statistics(questionnaire, overall_data)
    text_msg, list_msg = hypothesis_testing(results, general_results, cat_list, significance_level, 'Overall')
    markdown_output += text_msg
    overall_list += list_msg

    with open(f'{result_file}.md', "w") as f:
        f.write(overall_list + '\n\n' + markdown_output)

def run_emotionbench(args, generator):
    # Get questionnaire
    questionnaire = get_questionnaire(args.questionnaire)
    questionnaire_name = questionnaire["name"]
    args.scenarios_file = f'situations/{args.name_exp}-{questionnaire_name}-situations.csv' if args.name_exp is not None else f'situations/{args.model}-{questionnaire_name}-situations.csv'
    args.testing_file = f'results/{args.name_exp}-{questionnaire_name}-testing.csv' if args.name_exp is not None else f'results/{args.model}-{questionnaire_name}-testing.csv'
    args.results_file = f'results/{args.name_exp}-{questionnaire_name}-results' if args.name_exp is not None else f'results/{args.model}-{questionnaire_name}-results'

    os.makedirs("results", exist_ok=True)
    os.makedirs("situations", exist_ok=True)

    # Generation
    if args.mode in ['generation', 'auto']:
        generate_testfile(questionnaire, args)

    # Testing
    if args.mode in ['testing', 'auto']:
        generator(questionnaire, args)

    # Analysis
    if args.mode in ['analysis', 'auto']:
        analysis_results(questionnaire, args)


#! /usr/bin/env python3

import sys
import os
import re

def normalize_text(text):
    parts = text.strip().split()

    for i, w in enumerate(parts):
        if w in ["<no-speech>", "--", ".", "?", "~"]:
            parts[i] = ""
        elif w == "%incomplete":
            parts[i] = "<unk>"
        elif w in ["<cough>", "<laugh>", "<lipsmack>", "<hes>"]:
            parts[i] = "<spnoise>"
        elif w in ["<breath>", "<sta>"]:
            parts[i] = "<noise>"
        elif w in ["<int>", "(())", "<foreign>", "<overlap>", "<misc>"]:
            parts[i] = "<unk>"

        # change *word* into word
        parts[i] = re.sub(r"^[*](\S+)[*]$", r"\1", parts[i])

    return re.sub(r"\s+", " ", " ".join(parts))

def write_segment(start_time, end_time, text, reco_id,
                  segments_fh, utt2spk_fh, text_fh):
    assert end_time > start_time

    text = normalize_text(text)

    utt_id = "{reco_id}-{st:06d}-{end:06d}".format(
        reco_id=reco_id,
        st=int(start_time * 100), end=int(end_time * 100))

    print ("{utt_id} {reco_id} {st} {end}"
           "".format(utt_id=utt_id, reco_id=reco_id,
                     st=start_time, end=end_time),
           file=segments_fh)
    print ("{utt_id} {reco_id}"
           "".format(utt_id=utt_id, reco_id=reco_id),
           file=utt2spk_fh)
    print ("{utt_id} {text}"
           "".format(utt_id=utt_id, text=text),
           file=text_fh)

def parse_calls_transcript_file(transcript_file, segments_fh,
                                utt2spk_fh, text_fh):
    base_name = os.path.basename(transcript_file)
    file_id = re.sub(".transcription.txt", "", base_name)

    inline_start_time = -1
    outline_start_time = -1

    i = 0

    for line in open(transcript_file):
        parts = line.strip().split()

        if i == 0 and not parts[0].startswith('0'):
            raise Exception("Transcript file {0} does not start with 0.000"
                            "".format(transcript_file))
        i += 1

        start_time = float(parts[0])
        if len(parts) == 1:
            # Last line in the file
            write_segment(inline_start_time, start_time, inline_text, file_id + "_inLine",
                          segments_fh, utt2spk_fh, text_fh)
            write_segment(outline_start_time, start_time, outline_text, file_id + "_outLine",
                          segments_fh, utt2spk_fh, text_fh)
            break

        assert parts[1] in ["inLine", "outLine"]

        if parts[1] == "inLine":
            reco_id = file_id + "_inLine"
            if inline_start_time >= 0:
                write_segment(inline_start_time, start_time, inline_text, reco_id,
                              segments_fh, utt2spk_fh, text_fh)
            inline_text = " ".join(parts[2:])
            inline_start_time = start_time
        else:
            reco_id = file_id + "_outLine"
            if outline_start_time >= 0:
                write_segment(outline_start_time, start_time, outline_text, reco_id,
                              segments_fh, utt2spk_fh, text_fh)
            outline_text = " ".join(parts[2:])
            outline_start_time = start_time

def parse_non_calls_transcript_file(transcript_file, segments_fh,
                                    utt2spk_fh, text_fh):
    base_name = os.path.basename(transcript_file)
    file_id = re.sub(".transcription.txt", "", base_name)

    start_time = -1
    i = 0

    with open(transcript_file) as fh:
        line = fh.readline().strip()
        if not line.startswith('['):
            raise Exception("Transcript file {0} does not start with [0.000"
                            "".format(transcript_file))
        try:
            start_time  = float(re.sub(r"\[([^\]]+)\]", r"\1", line))
        except Exception:
            print("Could not parse line {0}".format(line), file=sys.stderr)
            raise

        text = fh.readline()
        while text != '':
            text = text.strip()
            line = fh.readline().strip()
            if not line.startswith('['):
                raise Exception("Time-stamp in transcript file {0} does not start with [; error parsing line {1} after text {2}"
                                "".format(transcript_file, line, text))
            try:
                end_time  = float(re.sub(r"\[([^\]]+)\]", r"\1", line))
            except Exception:
                print("Could not parse line {0}".format(line), file=sys.stderr)
                raise

            write_segment(start_time, end_time, text, file_id,
                          segments_fh, utt2spk_fh, text_fh)
            start_time = end_time
            text = fh.readline()

if __name__ == "__main__":
    if len(sys.argv) != 5:
        print ("Usage: {0} <corpus-root-dir> <calls-list> <non-calls-list> <data-dir>",
               file=sys.stderr)
        raise SystemExit(1)

    root_path = sys.argv[1]
    calls_list = open(sys.argv[2]).readlines()
    non_calls_list = open(sys.argv[3]).readlines()
    data_dir = sys.argv[4]

    wav_scp_fh = open("{0}/wav.scp".format(data_dir), 'w')
    utt2spk_fh = open("{0}/utt2spk".format(data_dir), 'w')
    reco2file_and_channel_fh = open(
        "{0}/reco2file_and_channel".format(data_dir), 'w')
    text_fh = open("{0}/text".format(data_dir), 'w')
    segments_fh = open("{0}/segments".format(data_dir), 'w')

    for line in calls_list:
        file_id = line.strip()
        transcript_file = (
            "{root_path}/transcription/{file_id}.transcription.txt"
            "".format(root_path=root_path, file_id=file_id))
        wav_file = "{root_path}/src/{file_id}.wav".format(
            root_path=root_path, file_id=file_id)

        for channel in [1, 2]:
            reco_id = file_id + ("_inLine" if channel == 1 else "_outLine")
            print ("{reco_id} {file_id} {channel}"
                   "".format(reco_id=reco_id, file_id=file_id,
                             channel="A" if channel == 1 else "B"),
                   file=reco2file_and_channel_fh)
            print ("{reco_id} sox {wav_file} -r 8000 -b 16 -c 1 -t wav - remix {channel} |"
                   "".format(reco_id=reco_id, wav_file=wav_file, channel=channel),
                   file=wav_scp_fh)

        parse_calls_transcript_file(transcript_file, segments_fh,
                                    utt2spk_fh, text_fh)

    for line in non_calls_list:
        file_id = line.strip()
        transcript_file = (
            "{root_path}/transcription/{file_id}.transcription.txt"
            "".format(root_path=root_path, file_id=file_id))
        wav_file = "{root_path}/src/{file_id}.wav".format(
            root_path=root_path, file_id=file_id)

        print ("{file_id} {file_id} 1"
               "".format(file_id=file_id),
               file=reco2file_and_channel_fh)
        print ("{reco_id} sox {wav_file} -r 8000 -b 16 -c 1 -t wav - |"
               "".format(reco_id=file_id, wav_file=wav_file),
               file=wav_scp_fh)

        parse_non_calls_transcript_file(transcript_file, segments_fh,
                                        utt2spk_fh, text_fh)

    wav_scp_fh.close()
    utt2spk_fh.close()
    reco2file_and_channel_fh.close()
    text_fh.close()
    segments_fh.close()


#!/usr/bin/env python

'''
A helper script for bisecting common problems when working with ArduPilot

Bisect between a commit which builds and one which doesn't,
finding the first commit which broke the build with a
 specific failure:

git bisect good a7647e77d9
git bisect bad 153ad9539866f8d93a99e9998118bb090d2f747f
cp -a Tools/autotest/bisect-helper.py /tmp
git bisect run /tmp/bisect-helper.py --build \
     --build-failure-string= \
     "reference to 'OpticalFlow' is ambiguous"

Work out who killed bebop:
cp -a Tools/autotest/bisect-helper.py /tmp
git bisect good a7647e77d9 &&
  git bisect bad 153ad9539866f8d93a99e9998118bb090d2f747f &&
  git bisect run /tmp/bisect-helper.py --build \
    --waf-configure-arg="--board bebop"

'''

import optparse
import os
import subprocess
import shlex
import sys
import time

class Bisect(object):
    def __init__(self, opts):
        self.opts = opts

    def exit_skip(self):
        self.progress("SKIP")
        sys.exit(125)

    def exit_pass(self):
        self.progress("PASS")
        sys.exit(0)

    def exit_fail(self):
        self.progress("FAIL")
        sys.exit(1)

    def progress(self, string):
        '''pretty-print progress'''
        print("BH: %s" % string)

    def run_program(self, prefix, cmd_list):
        '''copied in from build_binaries.py'''
        '''run cmd_list, spewing and setting output in self'''
        self.progress("Running (%s)" % " ".join(cmd_list))
        p = subprocess.Popen(cmd_list,
                             bufsize=1,
                             stdin=None,
                             close_fds=True,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)
        self.program_output = ""
        while True:
            x = p.stdout.readline()
            if len(x) == 0:
                returncode = os.waitpid(p.pid, 0)
                if returncode:
                    break
                    # select not available on Windows... probably...
                time.sleep(0.1)
                continue
            self.program_output += x
            x = x.rstrip()
            print("%s: %s" % (prefix, x))
        (_, status) = returncode
        if status != 0:
            self.progress("Process failed (%s)" %
                          str(returncode))
            raise subprocess.CalledProcessError(
                returncode, cmd_list)

    def build(self):
        '''run ArduCopter build.  May exit with skip or fail'''
        self.run_program("WAF-clean", ["./waf", "clean"])
        cmd_configure = ["./waf", "configure"]
        pieces = [shlex.split(x)
                  for x in opts.waf_configure_args]
        for piece in pieces:
            cmd_configure.extend(piece)
        self.run_program("WAF-configure", cmd_configure)
        cmd_build = ["./waf", "build"]
        pieces = [shlex.split(x)
                  for x in opts.waf_build_args]
        for piece in pieces:
            cmd_build.extend(piece)
        try:
            self.run_program("WAF-build", cmd_build)
        except subprocess.CalledProcessError as e:
            # well, it definitely failed....
            if opts.build_failure_string is not None:
                if opts.build_failure_string in self.program_output:
                    self.progress("Found relevant build failure")
                    self.exit_fail()
                # it failed, but not for the reason we're looking
                # for...
                self.exit_skip()
            else:
                self.exit_fail()

class BisectBuild(Bisect):

    def __init__(self, opts):
        super(BisectBuild, self).__init__(opts)

    def run(self):
        self.build()  # may exit with skip or fail
        self.exit_pass()

class BisectCITest(Bisect):

    def __init__(self, opts):
        super(BisectCITest, self).__init__(opts)

if __name__ == '__main__':

    parser = optparse.OptionParser("bisect.py ")
    parser.add_option("--build",
                      action='store_true',
                      default=False,
                      help="Help bisect a build failure")
    parser.add_option("--build-failure-string",
                      type='string',
                      default=None,
                      help="If supplied, must be present in"
                      "build output to count as a failure")

    group_build = optparse.OptionGroup(parser, "Build options")
    group_build.add_option("", "--waf-configure-arg",
                           action="append",
                           dest="waf_configure_args",
                           type="string",
                           default=["--board skyviper-v2450"],
                           help="extra arguments to pass to"
                           "waf in configure step")
    group_build.add_option("", "--waf-build-arg",
                           action="append",
                           dest="waf_build_args",
                           type="string",
                           default=["--target bin/arducopter"],
                           help="extra arguments to pass"
                           "to waf in its build step")

    parser.add_option_group(group_build)

    (opts, args) = parser.parse_args()

    if opts.build:
        bisecter = BisectBuild(opts)
    else:
        bisecter = BisectCITest(opts)

try:
    bisecter.run()
except Exception as e:
    print("Caught exception in bisect-helper: %s" % str(e))
    sys.exit(129)  # should abort the bisect process


#!/usr/bin/env python3
"""
Input-output routines

"""
import numpy as np

def save_xyzmatrix(fname, mat, init_char="1"):
    """Save a coordinate matrix in a xyz format compatible with VMD"""
    M, N = mat.shape
    with open(fname, "w") as f:
        f.write(str(M) + "\nbla\n")
        for i in range(M):
            line = init_char + "   "
            for j in range(N):
                line += str(mat[i, j]) + "   "
            line += "\n"
            f.write(line)

def read_xyzmatrix(fname):
    A = open(fname, "r").readlines()[2:]
    A = [line.split() for line in A]
    A = np.array(A).astype(float)
    return A


"""Black Jack"""
import random

def deal_cards():
    """Deals card from a deck"""
    deck = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
    card = random.choices(deck)
    return card

def calculate_score(cards):
    """Calculates score and checks for aces"""
    if sum(cards) == 21 and len(cards) == 2:
        return 0
    if 11 in cards and sum(cards) > 21:
        cards.remove(11)
        cards.append(1)
    return sum(cards)

def compare(user_score, pc_score):
    """Compares Scores and Declares a Winner"""

    if user_score == pc_score:
        return "Draw!"
    elif pc_score > user_score:
        return "You Lose"
    elif user_score > pc_score:
        return "You Win"
    elif user_score == 21:
        return "Black Jack, you win!"
    elif pc_score == 21:
        return "Black Jack, you lose!"
    elif pc_score > 21:
        return "You Win"
    else:
        return "You Lose"

def game():
    """The Game"""
    user_cards = []
    pc_cards = []

    game_over = False

    for _ in range(2):
        user_cards.append(deal_cards())
        pc_cards.append(deal_cards())

    while not game_over:
        user_score = calculate_score(user_cards)
        pc_score = calculate_score(pc_cards)

        print(f'{user_cards} total: {user_score}')
        print(f'{pc_cards[0]} ')   # shows only one card

        if user_score > 21:
            game_over = True
        else:
            another_card = input('Would you like another card? Y/N ')
            if another_card == "Y":
                user_cards.append(deal_cards())
            else:
                game_over = True

    while pc_score !=0 and pc_score < 17:
        pc_cards.append(deal_cards())
        pc_score = calculate_score(pc_cards)
        print(f'{pc_cards} total: {pc_score}')
        print(compare(user_score, pc_score))

game()


import cv2 as cv
import numpy as np
import mediapipe as mp

mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_pose = mp.solutions.pose

def annotate_still_image(image_path):
    with mp_pose.Pose(
            static_image_mode=True,
            model_complexity=2,
            enable_segmentation=True,
            min_detection_confidence=0.5) as pose:
        image = cv.imread(image_path)
        image_height, image_width, _ = image.shape
        # Convert the BGR image to RGB before processing.
        results = pose.process(cv.cvtColor(image, cv.COLOR_BGR2RGB))

        print(
            f'Nose coordinates: ('
            f'{results.pose_landmarks.landmark[mp_pose.PoseLandmark.NOSE].x * image_width}, '
            f'{results.pose_landmarks.landmark[mp_pose.PoseLandmark.NOSE].y * image_height})'
        )

        annotated_image = image.copy()

        mp_drawing.draw_landmarks(
            annotated_image,
            results.pose_landmarks,
            mp_pose.POSE_CONNECTIONS,
            landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style())
        cv.imwrite('annotated_image.jpg', annotated_image)

# Webcam for debugging
def annotate_webcam():
    # For webcam input:
    cap = cv.VideoCapture(0)
    with mp_pose.Pose(
            min_detection_confidence=0.5,
            min_tracking_confidence=0.5) as pose:
        while cap.isOpened():
            success, image = cap.read()
            if not success:
                print("Ignoring empty camera frame.")
                # If loading a video, use 'break' instead of 'continue'.
                continue

            # To improve performance, optionally mark the image as not writeable to
            # pass by reference.
            image.flags.writeable = False
            image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
            results = pose.process(image)

            # Draw the pose annotation on the image.
            image.flags.writeable = True
            image = cv.cvtColor(image, cv.COLOR_RGB2BGR)
            mp_drawing.draw_landmarks(
                image,
                results.pose_landmarks,
                mp_pose.POSE_CONNECTIONS,
                landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style())
            # Flip the image horizontally for a selfie-view display.
            cv.imshow('MediaPipe Pose', cv.flip(image, 1))
            if cv.waitKey(33) == 32:
                break
    cap.release()

def main():
    image_path = 'media/samp.jpg'
    # annotate_still_image(image_path)
    annotate_webcam()

if __name__ == '__main__':
    main()


import collections

def cipher_simple(ordinal, steps):
    shifted = ordinal + (steps % (128 - 32))
    if shifted > 127:
        return shifted - (128 - 32)
    if shifted < 0x20:
        return shifted + (128 - 32)
    return shifted

__file_cipher = list(range(0x30, 0x3A)) + list(range(0x61, 0x7B)) + list(map(ord, '_- '))
def cipher_file(ordinal, steps):

    shifted = __file_cipher.index(ordinal) + (steps % len(__file_cipher))
    if shifted >= len(__file_cipher):
        shifted -= len(__file_cipher)
    if shifted < 0:
        shifted += len(__file_cipher)

    return __file_cipher[shifted]

class Cipher:

    def __init__(self, index: int, shifter: callable = None):
        """
        Create a new cipher disk.

        :index int: the key of the cipher
        :disk list optional: a list of all available values (default ascii)
        """

        self.index = index
        if shifter is None:
            self._shifter = cipher_simple
        else:
            self._shifter = shifter

    def encode(self, text: collections.Iterable) -> list:
        if isinstance(text, str):
            text = map(ord, text)

        return [self._shifter(i, self.index) for i in text]

    def encodeStr(self, text: str) -> str:
        return ''.join(map(chr, self.encode(text)))

    def decode(self, text: collections.Iterable) -> list:
        if isinstance(text, str):
            text = map(ord, text)

        return [self._shifter(i, -self.index) for i in text]

    def decodeStr(self, text: str) -> str:
        return ''.join(map(chr, self.decode(text)))


# -*- coding: utf-8 -*-
'''
Download C5 binaries from https://www.rulequest.com/see5-info.html
(or other binaries) and adjust C5.BINS.

Authr: Timo Richter
'''

from collections import OrderedDict
import subprocess, codecs, os, re

from tempdir import Tempdir
from abstract_machine_learner import _MachineLearner

class _ExternalMachineLearner(_MachineLearner):
    """
    This class is a wrapper for the C4.5 and C5 implementations.
    It writes the matrix in a text file and reads the program call's output.
    """
    BINS = ""
    FILESTEM = "filestem"
    TYPES={ # data type and corresponding values
        bool: [str(int(True)),str(int(False))],
        int: ["continuous"],
        long: ["continuous"],
    }

    def __init__(self, classes, featureNames, featureTypes=None, keep=False, verbose=False):
        """
        @classes List of all classes, e.g. ["Good", "Bad"]
        @featureNames List of identifies of all features, order according to the
            matrix columns! ["Amount of friends","Has cancer"]
        @featureTypes This defines the range of the feature values, e.g. [int, bool].
            Define custom ranges like this: [["1","2"],["Yes","Maybe","No"]]
        """
        if featureTypes is None:
            featureTypes = [int]*len(featureNames)
        self.names = {"classes":classes, "featureNames":featureNames, "featureTypes":featureTypes}
        self.tempdir = Tempdir()
        self.tempdir.open("C4_5",keep)
        self.verbose=verbose
        self.filestemFullPath = os.path.join(str(self.tempdir),self.FILESTEM)
        self._do_evaluate = self.evaluate
        self.evaluate = self._before_evaluate
        self._openDataFiles()

    def score(self, X, y):
        if self.verbose: print("\t\tSave test data...")
        self.partial_score(X, y)
        self.evaluate()

    def _openDataFiles(self):
        self.trainingDataFile = codecs.open("%s.%s"%(self.filestemFullPath, "data"), "w", "utf-8")
        self.testDataFile = codecs.open("%s.%s"%(self.filestemFullPath, "test"), "w", "utf-8")

    def _closeDataFiles(self):
        self.trainingDataFile.close()
        self.testDataFile.close()

    def _programCall(self, bin):
        output=os.path.join(str(self.tempdir),"%s.output"%bin)
        f=open(output,"w")
        try:
            subprocess.call([os.path.join(self.BINS,bin), "-f", self.filestemFullPath],stdout=f,universal_newlines=True)
        except OSError: raise OSError("OSError: %s not found. Maybe adjust external.*.BINS."%os.path.join(self.BINS,bin))
        f.close()
        return output

    def _storeData(self, output, fv, cls):
        try:
            output.write("%s\n" % ", ".join([self._toStr(y) for y in [x for x in fv]+[cls]]))
        except ValueError:
            raise NotImplementedError("Value/IO-Error: Cannot evaluate twice using the same external machine learner. Please instantiate the class again.")

    def _toStr(self, x):
        try:
            y=int(x)
            #if y<0: return "?"
        except ValueError: y=x
        return str(y)
        # does this, but was too slow:
        #if type(x) is bool: return str(int(x))
        #return str(x)

    def escapeName(self, n):
        """ Returns str @n without unsave characters """
        return n.replace(",","_").replace(":","_").replace(".","_").replace("?","_").replace("|","_")

    def _createNames(self):
        """ make names file """
        filetext = "%(classes)s.\n\n%(features)s\n"
        classes = ", ".join(map(lambda x:str(x), self.names["classes"]))
        featuresDict = OrderedDict()
        for f,t in zip(self.names["featureNames"], self.names["featureTypes"]):
            if type(t) is list: featuresDict[f] = map(lambda x:str(x), t)
            elif t in self.TYPES: featuresDict[f] = self.TYPES[t]
            else: raise TypeError("Error: Type '%s' not in C5.TYPES. In featureTypes pass a "
                                 +"list with its possible values instead."%str(t))
        if len(featuresDict) != len(self.names["featureNames"]):
            raise Exception("ML: Error: Maybe a feature names occurs more than once?")

        features = "\n".join([
            "%s: %s."%(self.escapeName(f),", ".join(t))
            for f,t in featuresDict.items()
        ])
        with codecs.open("%s.names"%self.filestemFullPath,
                "w", "utf-8"
                ) as f:
            f.write(filetext%{"classes":classes,"features":features})

    def partial_fit(self, X, y):
        """
        @X Matrix, list of lists
        @y class of each sample, list
        """
        if len(X[0]) != len(self.names["featureNames"]):
            print("WARNING: Machine Learning: On partial_fit(): amount of feature names does not match amout of features")

        for fv,cls in zip(X,y):
            self._storeData(self.trainingDataFile, fv, cls)

    def fit(self, X, y):
        if self.verbose: print("\t\tSave training data...")
        self.partial_fit(X,y)

    def partial_score(self, X, y):
        for fv,cls in zip(X,y):
            self._storeData(self.testDataFile, fv, cls)

    def _before_evaluate(self):
        self._closeDataFiles()
        if self.verbose: print("\t\tSave names file...")
        self._createNames()
        self._do_evaluate()
        self.tempdir.close()
        #self._openDataFiles()

    def evaluate(self):
        """
        This will automatically call _before_evaluate() before.
        """
        raise NotImplementedError("Extend class and implement predict().")

    def _parseErrorMatrix(self, f):
        with open(f, "r") as fp:
            content = fp.read()
        ms=re.findall("classified as\n[^\n]*\n\t\s{0,6}(\d*)\s{1,6}(\d*)[^\n]*\n\t\s{0,6}(\d*)\s{1,6}(\d*)",content)
        ms=[[n if n!='' else 0 for n in m] for m in ms]
        try:
            ms=[[int(n) for n in m] for m in ms]
        except TypeError:
            raise TypeError("Can't parse error matrix.")
        ms=[dict(zip(["TP","FN","FP","TN"],m)) for m in ms]
        return ms # list of matrixes

    def _printErrorMatrixes(self, ms):
        #for m in ms: self._printErrorMatrix(m)
        self._printErrorMatrix(ms[-1])

class C4_5(_ExternalMachineLearner):
    """
    output from c4.5 -f filestem:
        feature = value: class (all occurrences of feature=value/occurrences of feature=value where class is different)
    output from c4.5rules:
        Error = 100%–accuracy
        Test data := training data; set(Used), set(Wrong)
        for r in rules: conjunction([line for line in r])
    """
    BINS = './r8'
    def evaluate(self):
        if self.verbose:
            print("\t\tCall c4.5...")
        self._programCall("c4.5")
        if self.verbose:
            print("\t\tCall c4.5rules...")
        programOutput = self._programCall("c4.5rules")
        self.errorMatrix = self._parseErrorMatrix(programOutput)[-1]

class C5(_ExternalMachineLearner):
    BINS = './programs/C50'

    def evaluate(self):
        if self.verbose: print("\t\tCall c5...")
        programOutput = self._programCall("c5.0")
        self.errorMatrix = self._parseErrorMatrix(programOutput)[-1]


# -*- coding: utf-8 -*-
"""Collect instances in Harmony."""
import json

import pyblish.api
import openpype.hosts.harmony.api as harmony

class CollectInstances(pyblish.api.ContextPlugin):
    """Gather instances by nodes metadata.

    This collector takes into account assets that are associated with
    a composite node and marked with a unique identifier.

    Identifier:
        id (str): "pyblish.avalon.instance"
    """

    label = "Instances"
    order = pyblish.api.CollectorOrder
    hosts = ["harmony"]
    families_mapping = {
        "render": ["review", "ftrack"],
        "harmony.template": [],
        "palette": ["palette", "ftrack"]
    }

    pair_media = True

    def process(self, context):
        """Plugin entry point.

        Args:
            context (:class:`pyblish.api.Context`): Context data.

        """
        nodes = harmony.send(
            {"function": "node.subNodes", "args": ["Top"]}
        )["result"]

        for node in nodes:
            data = harmony.read(node)

            # Skip non-tagged nodes.
            if not data:
                continue

            # Skip containers.
            if "container" in data["id"]:
                continue

            # skip render farm family as it is collected separately
            if data["family"] == "renderFarm":
                continue

            instance = context.create_instance(node.split("/")[-1])
            instance.data.update(data)
            instance.data["setMembers"] = [node]
            instance.data["publish"] = harmony.send(
                {"function": "node.getEnable", "args": [node]}
            )["result"]
            instance.data["families"] = self.families_mapping[data["family"]]

            # If set in plugin, pair the scene Version in ftrack with
            # thumbnails and review media.
            if (self.pair_media and instance.data["family"] == "scene"):
                context.data["scene_instance"] = instance

            # Produce diagnostic message for any graphical
            # user interface interested in visualising it.
            self.log.info(
                "Found: \"{0}\": \n{1}".format(
                    instance.data["name"], json.dumps(instance.data, indent=4)
                )
            )


# -*-coding:Latin-1 -*

import configparser
from pathlib import Path

from logPrint import logPrint

class internalConfig():
    # This class will parse the .ini config file

    def __init__(self):
        logPrint.printLog("Parsing the config file config.ini")
        self.config = configparser.ConfigParser()
        self.config.read('config.ini')

        self.outputFilePath = self.config['timeSnip']['outputFilePath']
        readVerbosityLevel = self.config['timeSnip']['verbosityLevel']

        # outputFilePath
        if self.outputFilePath == "":
            print(
                "[ERR] In internalConfig.py, outputFilePath is not defined")
            exit(-7)
        outputFile = Path(self.outputFilePath)
        outputFileParent = outputFile.parent
        if not outputFileParent.is_dir():
            print(
                "[ERR] In internalConfig.py, outputFilePath is bad defined: its parent directory does not exist")
            exit(-8)

        # verbosityLevel
        if readVerbosityLevel == "":
            print(
                "[ERR] In internalConfig.py, readVerbosityLevel is not defined")
            exit(-7)
        if readVerbosityLevel == "debug":
            self.verbosityLevel = 0
        elif readVerbosityLevel == "log":
            self.verbosityLevel = 1
        elif readVerbosityLevel == "error":
            self.verbosityLevel = 2
        else:
            print("[ERR] In internalConfig.py, verbosityLevel is not/bad defined")
            exit(-9)

    def getOutputFilePath(self):
        return self.outputFilePath

    def getVerbosityLevel(self):
        return self.verbosityLevel


'''
 Desenvolva um programa que leia o nome de um funcionário, seu salário,
quantos anos ele trabalha na empresa e mostre seu novo salário, reajustado de
acordo com a tabela a seguir:
 - Até 3 anos de empresa: aumento de 3%
 - entre 3 e 10 anos: aumento de 12.5%
 - 10 anos ou mais: aumento de 20%
'''
nome = input('Digite seu nome: ')
salario = float(input('Digite seu salário: '))
anosTrabalho = int(input('Há quantos anos você trabalha na empresa? '))

if anosTrabalho <= 3 :
    print(f'O seu salário reajustado será de R${salario * 1.03}')
elif 3 < anosTrabalho < 10 :
    print(f'O seu salário reajustado será de R${salario * 1.125}')
elif anosTrabalho >= 10:
    print(f'O seu salário reajustado será de R${salario * 1.2}')


import json
from datetime import datetime

nowdatetime = datetime.now().strftime('%m-%d-%Y %H:%M:%S')
todays_date = datetime.now().strftime('%m-%d-%Y')

class PoolTable:
    def __init__(self, pool_table_number):
        self.pool_table_number = pool_table_number
        self.occupied = False
        self.start_date_time = nowdatetime
        self.end_date_time = nowdatetime
        self.total_time_played = 0
        # self.cost = pass

table_lists = []
table_dicts = []

user_input = ""

def menu():
    print("\n1 - Book a Table\t2 - Close a Table\t3 - Close the App")

# def creating_tables_intial_12_tables():
tables_created = 1
while tables_created < 13:
    p = PoolTable(tables_created)
    table_lists.append(p)
    tables_created += 1

# creating_tables_intial_12_tables()

#see all tables and availability
def view_all_tables():
    print("\n{:*^30}".format(" INSTAPOOL "))
    print('{:=^30}'.format(""))
    for table in range(0, len(table_lists)):
        table_info = table_lists[table]
        if table_info.occupied == False:
            table_info.occupied = "Not Occupied"
        else:
            table_info.occupied = "Occupied"
        print(f"Table {table_info.pool_table_number} - {table_info.occupied}")

# converting and appending table objects to array as dictionaries
for table in table_lists:
    table_dicts.append(table.__dict__)

# print(table_dicts)

# writing/saving table dictionary array to JSON
with open("pool_table_info.json", "w") as f:
    json.dump(table_dicts, f)

#

while user_input != "3":
    view_all_tables()
    menu()
    user_input = input("\nEnter your choice: ")
    if user_input == "1":
        # LATER WRITE TABLE INFORMATION HERE
        print("\nTABLE 8 BOOKED!")
    if user_input == "2":
        # LATER CREATE A CREATE TABLE FUNCTION IN THE POOL CLASS
        print("\nTABLE 8 CLOSED!")
    elif user_input == "3":
        print("\n\nBYE!\n\n")


#-- SOBRE O TRABALHO ------------------------------------------------------------------------------------------------------------------------
# ENGENHARIA DE SOFTWARE - 5° PERÍODO - UNIMONTES 2023
# >> Professor
# ● Allysson Costa Silva
# >> Equipe
# ● Mateus Moreira Durães;
# ● Uedson Gaiek Souza;
# ● Júlio César Porto de Carvalho;
# ● Bruno Alex Cardoso Freitas
# >> Escopo do projeto
#   Com este sistema, um gerente de uma loja hipotética será capaz de verificar o estoque atual de sua loja, o valor líquido dos produtos,
# assim como adicionar e remover produtos a qualquer momento.
#   O programa será feito em python e usará um arquivo .TXT como arquivo para de banco de dados. O sistema será demonstrado através de uma
# interface gerada no CMD.
#-- BIBLIOTECAS -----------------------------------------------------------------------------------------------------------------------------
from config import *
from pages import *
#-- CHAMADA PRINCIPAL -----------------------------------------------------------------------------------------------------------------------
# Executando o programa
if __name__ == '__main__':
    system('cls')

    page = 0
    while (True): # Loop principal
        PÁGINAS[page].pgDesenharPag()
        PÁGINAS[1].pgAtualizarPag(f'''
|== ESTOQUE ============================|
{cfMostrarEstoque()}
{cfValorLiquido()}
| 1. Voltar                             |
|=======================================|''')
        page = PÁGINAS[page].pgExcSel(input('>> '))

    # Fim do programa
        if page == -1: break
        elif page < -1:
            cfPrint('Um erro inesperado ocorreu...', type = 'Erro', skip = True)
            break
    print(f'{CMD_VERDE}< FIM DO PROGRAMA >{CMD_DEFAULT}')

#--------------------------------------------------------------------------------------------------------------------------------------------


SIMPLE_CLASSROOM_PATH = "classroom_simple.txt"

def parse_simple_classroom(file_path):
    """ Parse classroom file that is given in `file_path` parameter.
    Returns a list of dictionaries describing the students in the classroom,
    each student is describe with the dictionary: {
      'name': ...,
      'country': ...,
      'grades': [...]
    }"""
    with open(file_path, 'r') as simple_classroom:
        whole_file_simple_classroom = simple_classroom.read()
        individual_students = whole_file_simple_classroom.split('###')
    students_info = []
    for student in individual_students[1:]:
        student_info = student.split('\n')
        name = student_info[1].strip('\n')
        country = student_info[2].strip('\n')
        grades = student_info[3:6]
        int_grades = []  # for converting list of grades from strings to integers
        for grade in grades:
            new_grade = int(grade)
            int_grades.append(new_grade)
        student_dict = {'name': name, 'country': country, 'grades': int_grades}
        students_info.append(student_dict)
    return students_info

def student_avg(students_list, student_name):
    """Gets input of the students_list from the above function parse_simple_classroom,
    plus the student name as second parameter, then checks for that student
    in the list of students, returns None if not found, otherwise
    calculates the average grade from the 3 grades in the grades Key of
    the student dictionary"""
    for info in students_list:
        if info.get('name') == student_name:
            return sum(info['grades']) / len(info['grades'])
    return None

def main():
    """Gets user_input of a student's name, then parses the student
    info file by running the function parse_simple_classroom, and
    passes the users input of the student name as a parameter into
    the student_avg function to get the average grade of that
    student. If student isn't in list of students, returns a polite
    error message"""
    student_name = input("Please enter a student name:")
    students_list = (parse_simple_classroom(SIMPLE_CLASSROOM_PATH))
    avg = student_avg(students_list, student_name)
    if avg is None:
        print("I'm sorry, there has been an error, please try entering the student name again.")
    else:
        print(avg)

if __name__ == "__main__":
    main()


from .base_form import BaseForm,Color,HitResult,Ray,MatplotlibRenderer
from Geometry3D import Point, Segment, Vector,ConvexPolygon,Parallelogram, x_unit_vector, y_unit_vector, z_unit_vector, distance
from Geometry3D.calc.intersection import inter_convexpolygon_halfline

# Représentation du sol en damier
class CheckerBoardGround (BaseForm) :
    def __init__(self, base_p: Point, length: float, width: float, color_name: str, shininess: float, reflection: float, orientation:str='horizontal') -> None:
        super().__init__(color_name=color_name,shininess=shininess,reflection=reflection)
        self.__base_p: Point = base_p
        self.__width: float = width
        self.__length: float = length
        v1, v2 = self.__findVectors(orientation, width=width, length=length)
        self.__surface : ConvexPolygon = Parallelogram(base_point=base_p, v1=v1, v2=v2)

    def hit(self, ray: Ray) -> HitResult:
        half_line = ray.toHalfLine()
        intersection : Point = inter_convexpolygon_halfline(self.__surface,half_line)
        ray_origin = ray.origin()
        if intersection is None :
            return HitResult(hit=False)
        # lorsqu'il s'agit d'un plan, l'intersection d'une ligne peut être un segment
        if isinstance(intersection, Segment) :
            intersection = intersection.start_point if distance(intersection.start_point, ray_origin) < distance(intersection.end_point,ray_origin) else intersection.end_point

        dist = distance(ray_origin, intersection)
        n : Vector = self.__surface.plane.n
        color = self.__damierColor(point=intersection)
        return HitResult(hit=True, hit_point=intersection, ray_origin=ray_origin, distance=dist, normal=n,color=color,geometry_ref=self, shininess=self._shininess, reflection=self._reflection)

    def addToPlotTest(self, r: MatplotlibRenderer):
        #r.add((self.__surface, self.__color_name,1),normal_length=10)
        pass

    def __findVectors(self, location: str, width: float, length: float) -> tuple[Vector,Vector] :
        v1: Vector = x_unit_vector() * width
        v2: Vector = y_unit_vector() * length
        if (location == 'vertical') :
            v1 = z_unit_vector() * width
        return v1,v2

    # détermine la couleur à renvoyer en fonction du point sur le plan
    def __damierColor (self, point: Point) -> Color :
        x_dist = abs(point.x - self.__base_p.x)
        y_dist = abs(point.y - self.__base_p.y)
        x_step = self.__width / 20
        y_step = self.__length / 20
        nb_x = x_dist // x_step
        nb_y = y_dist // y_step
        # l'aspect binaire du damier permet de retrouver facilement la couleur
        # en fonction du nombre de carreaux parcourus
        if (nb_x + nb_y) % 2 == 0 :
            return Color.fromName('black')
        return self._color


#!/home/aburov/venvs/checkio-venv/bin/checkio --domain=py run binary-count

#
# END_DESC

from math import sqrt

def checkio(number: int) -> int:
    number1 = 0
    # находим степень двойки, больше этого числа
    max_exp = int(sqrt(number)) + 1

    # По всем степеням дойки от MAX до 0 делаем, если число меньше.
    for i in range(max_exp - 1, -1, -1):
        if 2 ** i <= number:
            number -= 2 ** i
            number1 += 1
        if number == 0:
            return number1
    return False

# These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
    assert checkio(2**32)
    assert checkio(4) == 1
    assert checkio(15) == 4
    assert checkio(1) == 1
    assert checkio(1022) == 9
    print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")


from django.urls import path

from data_consumption.views import UploadAndSaveView, ListBuildingsView, ListMeterView, ListMeterHourlyDataView, \
    ShowChartView

urlpatterns = [
    path("upload/", UploadAndSaveView.as_view()),
    path("buildings/", ListBuildingsView.as_view()),
    path("buildings/<int:pk>/meters/", ListMeterView.as_view()),
    path("buildings/<int:pk>/meters/<int:meter_id>/half-hourly-data/", ListMeterHourlyDataView.as_view()),
    path("meter/<int:pk>/show-chart/", ShowChartView.as_view())
]


# coding:utf-8
# @Author: zlq
"""
从库读
"""
from option_third.option_third_func import get_slave_db_session
from model import User
import random
import time

Session = get_slave_db_session()

def my_read_view():
    print("*"*30)
    print("option_first ------my_read_view")
    # user_id = random.randint(1600, 1800)
    user_id = 3630
    query_result = Session.query(User).filter(User.id == user_id).first()
    if query_result:
        print(" exist's data  user id={id}".format(id=user_id))
        print(query_result.username)
    else:
        print(" no exist's data  user id={id}".format(id=user_id))

if __name__ == "__main__":
    # 这个线程从从库读
    while 1:
        my_read_view()
        time.sleep(2)