import re
from abc import ABC, abstractmethod
from base64 import b64encode, b64decode
from typing import List

from .peer import UserspacePeer
from ..interface import Interface
from ..utils import generate_public_key

# the regular expressions below parser the protocol described at https://www.wireguard.com/xplatform/
_re_private_key = re.compile(r"private_key=([a-f0-9]{64})")
_re_listen_port = re.compile(r"listen_port=([0-9]+)")
_re_fwmark = re.compile(r"fwmark=([0-9]+)")
_re_peer = re.compile(r"(public_key=[^;]*?)(?=public_key|errno|private_key|listen_port|fwmark)", re.M)

_re_peer_public_key = re.compile(r"public_key=([a-f0-9]{64})")
_re_peer_attrs = dict(
    endpoint=re.compile(r"endpoint=(.+)"),
    preshared_key=re.compile(r"preshared_key=([a-f0-9]{64})"),
    persistent_keepalive_interval=re.compile(r"persistent_keepalive_interval=([0-9]+)"),
    allowed_ips=re.compile(r"allowed_ip=(.+)"),
    rx_bytes=re.compile(r"rx_bytes=([0-9]+)"),
    tx_bytes=re.compile(r"tx_bytes=([0-9]+)"),
    last_handshake_time_sec=re.compile(r"last_handshake_time_sec=([0-9]+)"),
    last_handshake_time_nsec=re.compile(r"last_handshake_time_nsec=([0-9]+)")
)

class UserspaceInterface(Interface, ABC):

    @abstractmethod
    def _command_get(self) -> str:
        pass

    @abstractmethod
    def _command_set(self, command: str):
        pass

    @staticmethod
    def _create_peer_from_data(data: str) -> UserspacePeer:
        public_key = b64encode(bytes.fromhex(_re_peer_public_key.findall(data)[0]))
        peer = UserspacePeer(public_key)
        for attr, regex in _re_peer_attrs.items():
            re_result = regex.findall(data)
            if len(re_result) > 0:
                value = re_result[0]
                if attr == 'preshared_key':
                    value = b64encode(bytes.fromhex(value))
                if attr == 'allowed_ips':
                    value = re_result
                if attr in ['persistent_keepalive_interval', 'rx_bytes', 'tx_bytes',
                            'last_handshake_time_sec', 'last_handshake_time_nsec']:
                    value = int(value)
                setattr(peer, attr, value)
        return peer

    def _get_data_as_dict(self) -> dict:
        data = self._command_get()
        private_key = _re_private_key.findall(data)
        if len(private_key) > 0:
            private_key = b64encode(bytes.fromhex(private_key[0]))

        listen_port = _re_listen_port.findall(data)
        if len(listen_port) > 0:
            listen_port = int(listen_port[0])

        fwmark = _re_fwmark.findall(data)
        if len(fwmark) > 0:
            fwmark = int(fwmark[0])

        peers = [self._create_peer_from_data(peer) for peer in _re_peer.findall(data)]

        return dict(
            private_key=private_key,
            listen_port=listen_port,
            fwmark=fwmark,
            peers=peers
        )

    def _get_private_key(self) -> bytes:
        data = self._get_data_as_dict()
        return data['private_key']

    def _set_private_key(self, private_key: bytes) -> None:
        hex_private_key = b64decode(private_key).hex()
        self._command_set(f"private_key={hex_private_key}")

    def _get_listen_port(self) -> int:
        data = self._get_data_as_dict()
        return data['listen_port']

    def _set_listen_port(self, listen_port: int) -> None:
        self._command_set(f"listen_port={listen_port}")

    def _get_fwmark(self) -> int:
        data = self._get_data_as_dict()
        return data['fwmark']

    def _set_fwmark(self, fwmark: int) -> None:
        self._command_set(f"fwmark={fwmark}")

    def _get_public_key(self) -> bytes:
        return generate_public_key(self.private_key)

    def _get_peers(self) -> List[UserspacePeer]:
        return self._get_data_as_dict()['peers']

    def _upsert_peer(self, peer: UserspacePeer) -> None:
        self._command_set(peer.serialize())

    def _remove_peer(self, peer: UserspacePeer) -> None:
        hex_public_key = b64decode(peer.public_key).hex()
        command = f"public_key={hex_public_key}\nremove=true"
        self._command_set(command)


from DecisionTree import DecisionTree
import math

class DecisionTreeClassifier (DecisionTree):
	"""
	A decision tree classifier that extends the DecisionTree class.

	:Attributes:
		**max_depth** (int): The maximum depth of tree.

		**min_size** (int): The minimum number of datapoints in terminal nodes.

		**n_features** (int): The number of features to be used in splitting.

		**root** (dictionary): The root of the decision tree.

		**columns** (list) : The feature names.

		**class_values** (list) : The list of the target class values.

		**cost_function** (str) : The name of the cost function to use: 'gini'.
	"""

	def __init__(self, max_depth=2, min_size=2, cost='gini'):
		"""
		Constructor for the Decision Tree Classifer.  It calls the base
		class constructor and sets the cost function.  If the cost
		parameter is not 'gini' then an exception is thrown.

		Parameters:
			**max_depth** (int): The maximum depth of tree.

			**min_size** (int): The minimum number of datapoints in terminal nodes.

			**cost** (str) : The name of the cost function to use: 'gini'.
		"""
		DecisionTree.__init__(self, max_depth, min_size)
		self.class_values = None
		self.cost_function = None
		if cost == 'gini':
			self.cost_function = cost
		else:
			raise NameError('Not valid cost function')

	def fit(self, train, target=None, n_features=None):
		"""
		Builds the classification decsision tree by recursively splitting
		tree until the the maxmimum depth, max_depth of the tree is acheived or
		the node have the minimum number of training points, min_size.

		n_features will be passed by the RandomForest as it is usually a subset
		of the total number of features. However, if one is using the class as a
		stand alone decision tree, then the n_features will automatically be

		:Parameters:
			**dataset** (list or `Pandas DataFrame <http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html>`_) : The dataset.

			**target** (str) : The name of the target variable.

			**n_features*** (int) : The number of features.
		"""
		self.class_values = list(set(row[-1] for row in train))
		self._fit(train, target, n_features)

	def predict(self, row):
		"""
		Predict the class that this sample datapoint belongs to.

		:Parameter: **row** (list or `Pandas Series <http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.html>`_) : The datapoint to classify.

		:Returns: (int) The class the data points belong to.
		"""
		if isinstance(row, list) is False:
			return self._predict(row.tolist(), self.root)
		else:
			return self._predict(row, self.root)

	def _cost(self, groups):
		"""
		Returns the associated cost for the split of the dataset
		into two groups. The cost_function will be set when the
		tree is initialized.

		Args:
			groups (list) : List of the two subdatasets after splitting.

		Returns:
			float. Either the gini-index or entropy of the split.

		"""
		return self._gini_index(groups)

	def _gini_index(self, groups):
		"""
		Returns the gini-index for the split of the dataset into two groups.

		Args:
			groups (list) : List of the two subdatasets after splitting.

		Returns:
			float. gini-index of the split.
		"""
		gini = 0.0
		for class_value in self.class_values:
			for group in groups:
				size = len(group)
				if size == 0:
					continue
				p = [row[-1] for row in group].count(class_value) / float(size)
				gini += (p * (1.0 - p))
		return gini

	def _make_leaf(self, group):
		"""
        Creates a terminal node value by selecting amoung the group that has
        the majority.

        Args:
        	group (list): The subgroup of the dataset.

       	Returns:
       		int. The majority of this groups target class values.
    	"""
		outcomes = [row[-1] for row in group]
		return max(set(outcomes), key=outcomes.count)


def lengthOfLongestSubstring2(s):
    """
    :type s: str
    :rtype: int
    """

    p1 = 0
    p2 = 0

    ans = 0

    dic = {}

    while p2 < len(s):

        if s[p2] in dic and dic[s[p2]] >= p1:
            p1= dic[s[p2]]+1
        else:
            dic[s[p2]] = p2
            ans = max(ans, 1 + p2 - p1)
            p2 = p2 + 1

    return ans

print(lengthOfLongestSubstring2("abcabcbb"))

def longestPalindrome(s):
    ans = 0

    ansstring = ""

    for i in range(len(s)):

        p1 = p2 = i

        if p1 + 1 < len(s) and s[p1] == s[p1 + 1]:
            p2 = p1 + 1

        while (p1 >= 0 and p2 < len(s)) and (s[p1] == s[p2]):
            if p2 - p1 + 1 > ans:
                ans = p2 - p1 + 1

                ansstring = s[p1:p2 + 1]

            p1 -=1
            p2 += 1

    return ansstring

print(longestPalindrome("ccc"))


import ftplib
import concurrent.futures
from colorama import Fore, Style

# meminta user memasukkan nama file yang berisi daftar FTP server yang ingin diperiksa
file_name = input("Give Me Your List:  ")

# membuka file dan membaca daftar FTP server
with open(file_name) as file:
    ftp_list = ["ftp." + server.strip() for server in file.readlines()]

# membuka file untuk menyimpan hasil
output_file = open("ftpanon.txt", "w")

# fungsi untuk memeriksa apakah anonymous FTP access diaktifkan
def check_anonymous_ftp(ftp_server):
    try:
        ftp = ftplib.FTP()
        ftp.connect(ftp_server, port=21, timeout=5) # menambahkan timeout 5 detik
        ftp.login()
        ftp.quit()
        return ftp_server
    except ftplib.all_errors:
        return None

# memeriksa setiap FTP pada daftar dan mencetak hasilnya
with concurrent.futures.ThreadPoolExecutor(max_workers=150) as executor:
    future_to_ftp = {executor.submit(check_anonymous_ftp, ftp_server): ftp_server for ftp_server in ftp_list}
    for future in concurrent.futures.as_completed(future_to_ftp):
        ftp_server = future_to_ftp[future]
        result = future.result()
        if result is not None:
            print(Fore.GREEN + f"Anonymous FTP OK pada {result}")
            output_file.write(f"{result}\n")
        else:
            print(Fore.RED + f"Anonymous FTP BAD pada {ftp_server}")
        print(Style.RESET_ALL, end="") # reset warna setelah mencetak

# menutup file setelah selesai
output_file.close()


import re
from typing import Any
from pyspark.sql.dataframe import DataFrame
from pyathena import connect as athena_connect
from spark_pipeline_framework.logger.yarn_logger import get_logger
from spark_pipeline_framework.utilities.athena.athena_source_file_type import (
    AthenaSourceFileType,
)

class Athena:
    # S3_STAGING_PATH = "s3://bwell-helix-log-scratchpad-ue1/athena_query_results/"

    @staticmethod
    def drop_create_table(
        table_name: str,
        schema_name: str,
        df: DataFrame,
        s3_source_path: str,
        source_file_type: AthenaSourceFileType,
        s3_temp_folder: str,
    ) -> None:
        drop_ddl: str = Athena._generate_athena_drop_table_ddl(table_name)
        Athena._execute_ddl(schema_name, drop_ddl, s3_temp_folder=s3_temp_folder)
        create_ddl: str = Athena._generate_athena_create_table_ddl(
            df, table_name, s3_source_path, source_file_type
        )
        Athena._execute_ddl(schema_name, create_ddl, s3_temp_folder=s3_temp_folder)

    @staticmethod
    def _generate_athena_drop_table_ddl(athena_table_name: str) -> str:
        return f"DROP TABLE IF EXISTS `{athena_table_name}`"

    # noinspection PyPep8Naming,PyMissingOrEmptyDocstring
    @staticmethod
    def _generate_athena_create_table_ddl(
        df: DataFrame,
        athena_table_name: str,
        s3_location: str,
        source_file_type: AthenaSourceFileType,
    ) -> str:
        s3_location = s3_location.replace("s3a://", "s3://")
        ddl = []
        fields = df.schema

        ddl.append(f"CREATE EXTERNAL TABLE IF NOT EXISTS `{athena_table_name}` (")

        for field in fields.fieldNames()[:-1]:
            ddl.append(
                f"\t`{fields[field].name}` {Athena._get_ddl_data_type(fields[field])},"
            )
        last = fields[fields.fieldNames()[-1]]
        ddl.append(f"\t`{last.name}` {Athena._get_ddl_data_type(last)}")
        ddl.append(")")

        if source_file_type == AthenaSourceFileType.CSV_QUOTED:
            ddl.append("ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'")
            ddl.append("WITH SERDEPROPERTIES (")
            ddl.append("  'separatorChar' = ',',")
            ddl.append("  'quoteChar' = '\"',")
            ddl.append("  'escapeChar' = '\\\\'")
            ddl.append("  )")
        elif source_file_type == AthenaSourceFileType.CSV_UNQUOTED:
            ddl.append("ROW FORMAT DELIMITED")
            ddl.append("  FIELDS TERMINATED BY ','")
            ddl.append("  ESCAPED BY '\\\\'")
            ddl.append("  LINES TERMINATED BY '\\n'")
        elif source_file_type == AthenaSourceFileType.JSONL:
            ddl.append("ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'")
        elif source_file_type == AthenaSourceFileType.PARQUET:
            ddl.append("STORED AS PARQUET")

        ddl.append(f"LOCATION '{s3_location}'")

        if source_file_type == AthenaSourceFileType.PARQUET:
            ddl.append('tblproperties ("parquet.compression"="SNAPPY")')

        return "\n".join(ddl)

    @staticmethod
    def _get_ddl_data_type(field: Any) -> str:
        # wrap all field names in backticks to avoid reserved word collisions
        return str(
            re.sub("(\\w*?)?:(\\w*?)", "`\\1`:\\2", field.dataType.simpleString())
        )

    @staticmethod
    def _execute_ddl(schema_name: str, ddl: str, s3_temp_folder: str) -> None:
        try:
            cursor = athena_connect(
                schema_name=schema_name, s3_staging_dir=s3_temp_folder
            ).cursor()
            cursor.execute(ddl)
        except Exception as e:
            logger = get_logger(__name__)
            logger.exception(
                f"ERROR executing Athena query on schema: {schema_name} using temp_folder:{s3_temp_folder}"
            )
            logger.error("---------- Query ------------")
            logger.error(ddl)
            logger.error("------------------------------")
            logger.error(e)


'''使用一个logger实例'''

import logging
import auxiliary

# 创建名为 spam_application 的记录器
logger = logging.getLogger('application')
logger.setLevel(logging.DEBUG)

# 创建级别为 DEBUG 的文件日志处理器
fh = logging.FileHandler('spam.log', encoding='utf-8')
fh.setLevel(logging.DEBUG)

# 创建级别为 ERROR 的控制器日志处理器
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# 创建格式器 加到日志处理器中
formatter = logging.Formatter(
    '[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s', '%Y-%m-%d %H:%M:%S')
fh.setFormatter(formatter)
ch.setFormatter(formatter)

logger.addHandler(fh)
logger.addHandler(ch)

logger.info('main 收到一个请求')
# logger.info('creating an instance of auxiliary.Auxiliary')
# a = auxiliary.Auxiliary()
# logger.info('created an instance of auxiliar.Auxiliary')
# logger.info('calling auxiliary.Auxiliary.aux_foo')
# a.aux_foo()
# logger.info('done with auxiliary.Auxiliary.aux_foo')
# logger.info('calling auxiliary.foo()')
auxiliary.foo()
# logger.info('done with auxiliary.foo()')


import argparse
import pandas as pd
import functools
import waterbirds.data_builder as wb
import tensorflow as tf
import numpy as np
from causallearn.utils.KCI.KCI import KCI_CInd

# NOTE: the original causallearn code was modified to take epsilon as an argument.
# in the original code, its value is hardcoded.

def get_waterbirds_high_dim_x(random_seed, base_dir, pixel=128,
	weighted=False, batch_size=1000):
	"""Function to get the data."""
	experiment_directory = (
			f"{base_dir}/experiment_data/rs{random_seed}")
	train_data = pd.read_csv(
			f'{experiment_directory}/train.txt'
			).values.tolist()
	train_data = [
			tuple(train_data[i][0].split(',')) for i in range(len(train_data))
	]

	map_to_image_label_given_pixel = functools.partial(wb.map_to_image_label,
					pixel=pixel, weighted=weighted)

	train_dataset = tf.data.Dataset.from_tensor_slices(train_data)
	train_dataset = train_dataset.map(map_to_image_label_given_pixel, num_parallel_calls=1)
	train_dataset = train_dataset.batch(batch_size,
			drop_remainder=False).repeat(1)

	tr_data = []
	for batch_id, examples in enumerate(train_dataset):
			print(batch_id)
			x, labels_weights = examples
			x = x.numpy()
			x = x.reshape(x.shape[0], pixel*pixel*3)
			pix_cols = [f'x{i}' for i in range(x.shape[1])]
			labels = labels_weights['labels'].numpy()
			label_cols = [f'y{i}' for i in range(labels.shape[1])]
			x = pd.DataFrame(np.hstack([x, labels]),
											columns = pix_cols + label_cols)
			tr_data.append(x)

	tr_data = pd.concat(tr_data)
	return tr_data

def get_high_dim_x(experiment_name, random_seed, base_dir,
	pixel=128, weighted=False, batch_size=1000):
	if experiment_name == "waterbirds":
		return	get_waterbirds_high_dim_x(
			random_seed=random_seed,
			base_dir=base_dir,
			pixel=pixel,
			weighted=weighted,
			batch_size=batch_size)
	else:
		raise NotImplementedError("not yet!")

def get_y_waterbirds(random_seed, base_dir):
	experiment_directory = (
		f"{base_dir}/experiment_data/rs{random_seed}")

	tr_data = pd.read_csv(f'{experiment_directory}/train.txt')
	tr_data
	tr_data = tr_data['0'].str.split(",", expand=True)
	D = tr_data.shape[1]-4
	tr_data.columns = ['bird_img', 'bird_seg', 'back_img', 'noise_img'] + \
			[f'y{i}' for i in range(D)]
	tr_data.drop(['bird_img', 'bird_seg', 'back_img', 'noise_img'], axis=1, inplace=True)
	for i in range(D):
			tr_data[f'y{i}'] = tr_data[f'y{i}'].astype(np.float32)

	return tr_data

def get_y(experiment_name, random_seed, base_dir):
	if experiment_name == 'waterbirds':
		return get_y_waterbirds(random_seed=random_seed,
			base_dir=base_dir)
	else:
		raise NotImplementedError("not yet!")

def y_independence_test(random_seed, v_dim, base_dir, experiment_name):
	df = get_y(experiment_name=experiment_name,
		random_seed=random_seed,
		base_dir=base_dir
		)
	D = v_dim + 1

	var_variance = df.var(axis=0)
	var_variance = (var_variance == 0.0).tolist()
	if sum(var_variance) > 0.0:
		drop_vars = [i for i, i_true in enumerate(var_variance) if i_true]
		print(f'the following variables had 0 variance {df.columns[drop_vars]}')
		non_zero_var_labels = list(set(range(D)) - set(drop_vars))
	else:
		non_zero_var_labels = [i for i in range(D)]

	p_values = []
	for i in range(1, D):
		if i in non_zero_var_labels:
			kci_obj = KCI_CInd(kernelX='Gaussian',
								 kernelY='Gaussian',
								 kernelZ='Gaussian',
								 est_width='median',
								 epsilon=1e-3)

			p, _ = kci_obj.compute_pvalue(
				data_x=df.values[:, 0][:, np.newaxis],
				data_y=df.values[:, i][:, np.newaxis],
				data_z = df.values[:,[j for j in non_zero_var_labels if ((j!=0) & (j!=i))]]
				)
		else:
			p = 0.0
		p_values.append(p)
	return p_values

def x_independence_test(random_seed, pixel, batch_size,
	 model_name, xv_mode, v_dim, base_dir, x_mode, experiment_name):
	if x_mode == 'sufficient_stat':
		df = pd.read_csv((f'{base_dir}/final_models/opt_pred_rs{random_seed}_{model_name}_{xv_mode}'
			f'_pix{pixel}_bs{batch_size}_vdim{v_dim}.csv'))
		df.columns = df.columns.str.replace("pred", "x")
		df.drop(['model', 'dist'], inplace=True, axis=1)
	else:
		df = get_high_dim_x(
			experiment_name=experiment_name,
			random_seed=random_seed,
			base_dir=base_dir,
			pixel=128,
			weighted=False,
			batch_size=1000)

	D = v_dim + 1
	y_cols = [i for i in range(df.shape[1] - D, df.shape[1])]
	x_cols = list(set(range(df.shape[1])) - set(y_cols))

	# if any of the variables have 0 variance, the test fails
	# need to remove them first.
	var_variance = df.var(axis=0)

	var_variance = (var_variance == 0.0).tolist()
	if sum(var_variance) > 0.0:
		drop_vars = [i for i, i_true in enumerate(var_variance) if i_true]
		print(f'the following variables had 0 variance {df.columns[drop_vars]}')
		drop_y_vars = list(set(y_cols) & set(drop_vars))
		y_cols_reduced = list(set(y_cols) - set(drop_vars))
		x_cols = list(set(x_cols) - set(drop_vars))
	else:
		y_cols_reduced = y_cols

	p_values = []
	for i in y_cols[1:]:
		if i in y_cols_reduced:
			kci_obj = KCI_CInd(kernelX='Gaussian',
												 kernelY='Gaussian',
												 kernelZ='Gaussian',
												 est_width='median',
												 epsilon=1e-3)

			p, _ = kci_obj.compute_pvalue(
				data_x=df.values[:, x_cols],
				data_y=df.values[:, i][:, np.newaxis],
				data_z = df.values[:,[j for j in y_cols_reduced if (j!=i)]]
					)
		else:
			p = 0.0
		p_values.append(p)

	return p_values

def main(seed_list, x_mode, test_pval, experiment_name, pixel,
	batch_size, model_name, xv_mode, v_dim, base_dir, **args):
	seed_list = [int(i) for i in seed_list.split(",")]

	results = []
	for random_seed in seed_list:
		print(f'======= Random seed :{random_seed} ========')
		x_pvals = x_independence_test(
			random_seed = random_seed,
			pixel = pixel,
			batch_size = batch_size,
			model_name = model_name,
			xv_mode = xv_mode,
			v_dim = v_dim,
			base_dir = base_dir,
			x_mode = x_mode,
			experiment_name=experiment_name
			)

		y_pvals = y_independence_test(
			random_seed = random_seed,
			v_dim=v_dim,
			base_dir = base_dir,
			experiment_name=experiment_name
			)

		x_rel_bool = [x_pval <= test_pval for x_pval in x_pvals]
		x_rel = [i + 1 for i, i_true in enumerate(x_rel_bool) if i_true]

		y_rel_bool = [y_pval <= test_pval for y_pval in y_pvals]
		y_rel = [i + 1 for i, i_true in enumerate(y_rel_bool) if i_true]

		rel_aux = list(set(x_rel) & set(y_rel))

		rs_results = [random_seed, x_mode] + x_pvals + y_pvals + [rel_aux]
		results.append(rs_results)

	results = pd.DataFrame(results)
	results.columns = ['random_seed', 'x_mode'] + \
		[f'x_ci_y{i}' for i in range(1, len(x_pvals) + 1)] + \
		[f'y0_ci_y{i}' for i in range(1, len(y_pvals) + 1)] + \
		['relevant_aux_labs']

	print(results[['random_seed', 'x_mode', 'relevant_aux_labs']].head(10))

	results.to_csv(
		(f'{base_dir}/final_models/indep_tests_{x_mode}'
		f'_pix{pixel}_bs{batch_size}.csv'),
		index=False)

if __name__ == "__main__":

	parser = argparse.ArgumentParser()

	parser.add_argument('--experiment_name', '-experiment_name',
		help="which dataset?",
		choices=['waterbirds', 'chexpert', 'dr'],
		type=str)

	parser.add_argument('--base_dir', '-base_dir',
		help="Base directory where the final model will be saved",
		type=str)

	parser.add_argument('--random_seed', '-random_seed',
		help="Random seed for which we want to get predictions",
		default=-1,
		type=int)

	parser.add_argument('--seed_list', '-seed_list',
		help=("Comma separated list of seeds to get predictions for. "
				"overrides random seed"),
		type=str)

	parser.add_argument('--batch_size', '-batch_size',
		help="batch size",
		type=int)

	parser.add_argument('--pixel', '-pixel',
		help="pixels",
		type=int)

	parser.add_argument('--v_dim', '-v_dim',
		help="number of additional dimensions",
		type=int)

	parser.add_argument('--model_name', '-model_name',
		default='first_step',
		help="Which model to predict for",
		type=str)

	parser.add_argument('--xv_mode', '-xv_mode',
		default='classic',
		choices=['classic', 'two_step'],
		help=("which cross validation algorithm do you want to get preds for"),
		type=str)

	parser.add_argument('--x_mode', '-x_mode',
		default='sufficient_stat',
		choices=['sufficient_stat', 'high_dim'],
		help=("use high dim raw x or reduce using sufficient statistic?"),
		type=str)

	parser.add_argument('--test_pval', '-test_pval',
		default=0.001,
		help=("what is on the pval for the independence test?"),
		type=float)

	args = vars(parser.parse_args())
	main(**args)


import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from sklearn import ensemble, neighbors, linear_model, metrics, preprocessing
from datetime import datetime
import glob, re
data = {
    'ar': pd.read_csv('../input/air_reserve.csv'),
    'as': pd.read_csv('../input/air_store_info.csv'),
    'tra': pd.read_csv('../input/air_visit_data.csv'),
    'hol': pd.read_csv('../input/date_info.csv').rename(columns={'calendar_date': 'visit_date'}),
    'hr': pd.read_csv('../input/hpg_reserve.csv'),
    'hs': pd.read_csv('../input/hpg_store_info.csv'),
    'tes': pd.read_csv('../input/sample_submission.csv'),
    'id': pd.read_csv('../input/store_id_relation.csv')
}
data['id'].sample(3)
data['hr'] = data['hr'].merge(data['id'], how='inner', on=['hpg_store_id'])
data['hr'].sample(5)
# Notice datetime initially an object. We will fix that
np.dtype(data['hr']['visit_datetime'])
for df in ['ar', 'hr']:
    data[df]['visit_datetime'] = pd.to_datetime(data[df]['visit_datetime'])
    data[df]['visit_datetime'] = data[df]['visit_datetime'].dt.date
    data[df]['reserve_datetime'] = pd.to_datetime(data[df]['reserve_datetime'])
    data[df]['reserve_datetime'] = data[df]['reserve_datetime'].dt.date
    data[df]['reserve_datetime_diff'] = data[df].apply(
        lambda r: (r['visit_datetime'] - r['reserve_datetime']).days, axis=1)
    data[df] = data[df].groupby(['air_store_id', 'visit_datetime'], as_index=False)[['reserve_datetime_diff',
                                                                                'reserve_visitors']].sum().rename(columns={'visit_datetime':'visit_date'})
data['hr'].sample(5)
data['ar'].sample(4)
data['as'].sample(5)
lbl = preprocessing.LabelEncoder()
data['as']['air_genre_name'] = lbl.fit_transform(data['as']['air_genre_name'])
data['as']['air_area_name'] = lbl.fit_transform(data['as']['air_area_name'])
data['as'].sample(3)
data['tra'].sample(4)
data['tra']['visit_date'] = pd.to_datetime(data['tra']['visit_date'])
data['tra']['day_of_week'] = data['tra']['visit_date'].dt.dayofweek
data['tra']['month'] = data['tra']['visit_date'].dt.month
data['tra']['year'] = data['tra']['visit_date'].dt.year
data['tra']['visit_date'] = data['tra']['visit_date'].dt.date

data['tra'].sample(4)
data['hol'].sample(5)
data['hol'] = data['hol'].drop(columns=['day_of_week'])
data['hol']['visit_date'] = pd.to_datetime(data['hol']['visit_date'])
data['hol']['visit_date'] = data['hol']['visit_date'].dt.date
data['hol'].sample(4)
data['hs'].sample(4)
data['tes'].sample(3)
data['tes']['visit_date'] = data['tes']['id'].map(lambda x: str(x).split('_')[2])
data['tes']['air_store_id'] = data['tes']['id'].map(lambda x: '_'.join(x.split('_')[:2]))
data['tes']['visit_date'] = pd.to_datetime(data['tes']['visit_date'])
data['tes']['day_of_week'] = data['tes']['visit_date'].dt.dayofweek
data['tes']['month'] = data['tes']['visit_date'].dt.month
data['tes']['year'] = data['tes']['visit_date'].dt.year
data['tes']['visit_date'] = data['tes']['visit_date'].dt.date
data['tes'].sample(4)
# Store each unique restaurant in an array
unique_stores = data['tes']['air_store_id'].unique()
# Break each restaurant into 7 rows to track each day of the week
stores = pd.concat([pd.DataFrame({'air_store_id': unique_stores,
                                  'day_of_week': [i]*len(unique_stores)}) for i in range(7)],
                   axis=0, ignore_index=True)
# Make a temporary variable to store new 'tra' features on visitors
# Then merge that new feature into our stores dataframe
tmp = data['tra'].groupby(['air_store_id', 'day_of_week'],
                          as_index=False)['visitors'].min().rename(columns={'visitors':'min_visitors'})
stores = stores.merge(tmp, how='left', on=['air_store_id','day_of_week'])
# Continue this process for mean, max, and count of visitors
tmp = data['tra'].groupby(['air_store_id', 'day_of_week'],
                         as_index=False)['visitors'].mean().rename(columns={'visitors':'mean_visitors'})
stores = stores.merge(tmp, how='left', on=['air_store_id', 'day_of_week'])
tmp = data['tra'].groupby(['air_store_id', 'day_of_week'],
                          as_index=False)['visitors'].median().rename(columns={'visitors':'median_visitors'})
stores = stores.merge(tmp, how='left', on=['air_store_id', 'day_of_week'])
tmp = data['tra'].groupby(['air_store_id', 'day_of_week'],
                          as_index=False)['visitors'].max().rename(columns={'visitors':'max_visitors'})
stores = stores.merge(tmp, how='left', on=['air_store_id', 'day_of_week'])
tmp = data['tra'].groupby(['air_store_id', 'day_of_week'],
                          as_index=False)['visitors'].count().rename(columns={'visitors':'visitor count'})
stores = stores.merge(tmp, how='left', on=['air_store_id', 'day_of_week'])
# Now we'll merge 'as' in
stores = stores.merge(data['as'], how='left', on=['air_store_id'])
# Check everything looks good and is machine readable
stores.head(4)
train = pd.merge(data['tra'], data['hol'], how='left', on=['visit_date'])
train = pd.merge(data['tra'], stores, how='left', on=['air_store_id', 'day_of_week'])
test = pd.merge(data['tes'], data['hol'], how='left', on=['visit_date'])
test = pd.merge(data['tes'], stores, how='left', on=['air_store_id', 'day_of_week'])
test.sample(3)
data['ar'] = data['ar'].merge(data['hr'], how='left', on=['air_store_id', 'visit_date', 'reserve_datetime_diff', 'reserve_visitors'])
data['ar'].sample(4)
train = train.merge(data['ar'], how='left', on=['air_store_id', 'visit_date'])
test = test.merge(data['ar'], how='left', on=['air_store_id', 'visit_date'])
train.sample(5)
col = [c for c in train if c not in ['id', 'air_store_id', 'visit_date', 'visitors']]
train = train.fillna(-1)
test = test.fillna(-1)
train.sample(4)
def RMSLE(y, pred):
    return metrics.mean_squared_error(y, pred)**0.5
etc = ensemble.ExtraTreesRegressor(n_estimators=225, max_depth=5, n_jobs=-1, random_state=3)
knn = neighbors.KNeighborsRegressor(n_jobs=-1, n_neighbors=4)
etc.fit(train[col], np.log1p(train['visitors'].values))
knn.fit(train[col], np.log1p(train['visitors'].values))
test['visitors'] = (etc.predict(test[col]) / 2) + (knn.predict(test[col]) / 2)
test['visitors'] = np.expm1(test['visitors']).clip(lower=0.)
sub1 = test[['id', 'visitors']].copy()
# Create a glob expression for finding and reading all .csv files in the data warehouse
dfs = {re.search('/([^/\.]*)\.csv', fn).group(1):
      pd.read_csv(fn) for fn in glob.glob('../input/*.csv')}

# store the CSVs locally for quick access
for k, v in dfs.items(): locals()[k] = v
#date_info['weight'] = ((date_info.index + 1) / len(date_info))       # LB 0.509
#date_info['weight'] = ((date_info.index + 1) / len(date_info)) ** 2  # LB 0.503
#date_info['weight'] = ((date_info.index + 1) / len(date_info)) ** 3  # LB 0.500
#date_info['weight'] = ((date_info.index + 1) / len(date_info)) ** 4  # LB 0.498
date_info['weight'] = ((date_info.index + 1) / len(date_info)) ** 5 # LB 0.497
visit_data = air_visit_data.merge(date_info, left_on = 'visit_date', right_on = 'calendar_date', how = 'left')
visit_data.drop('calendar_date', axis=1, inplace = True)
visit_data['visitors'] = visit_data.visitors.map(pd.np.log1p)
visit_data.head()
wmean = lambda x:((x.weight * x.visitors).sum() / x.weight.sum())
visitors = visit_data.groupby(['air_store_id', 'day_of_week', 'holiday_flg']).apply(wmean).rename('visitors').reset_index()
visitors.head()
sample_submission['air_store_id'] = sample_submission.id.map(lambda x: '_'.join(x.split('_')[:-1]))
sample_submission['calendar_date'] = sample_submission.id.map(lambda x: x.split('_')[2])
sample_submission.drop('visitors', axis=1, inplace=True)
sample_submission = sample_submission.merge(date_info, on='calendar_date', how='left')
sample_submission = sample_submission.merge(visitors, on=[
    'air_store_id', 'day_of_week', 'holiday_flg'], how='left')
sample_submission.head()
sample_submission.visitors.isnull().sum()
missings = sample_submission.visitors.isnull()
sample_submission.loc[missings, 'visitors'] = sample_submission[missings].merge(
    visitors[visitors.holiday_flg == 0], on=('air_store_id', 'day_of_week'),
    how='left')['visitors_y'].values
sample_submission.isnull().sum()
missings = sample_submission.visitors.isnull()
sample_submission.loc[missings, 'visitors'] = sample_submission[missings].merge(
    visitors[['air_store_id', 'visitors']].groupby('air_store_id').mean().reset_index(),
    on='air_store_id', how='left')['visitors_y'].values
# Double check we filled all the missing values
sample_submission.isnull().sum()
sample_submission['visitors'] = sample_submission.visitors.map(pd.np.expm1)
sub2 = sample_submission[['id', 'visitors']].copy()
sub_merge = pd.merge(sub1, sub2, on='id', how='inner')
## Geometric Mean
sub_merge['visitors'] = (sub_merge['visitors_x'] * sub_merge['visitors_y']) ** (1/2)
sub_merge[['id', 'visitors']].to_csv('sub_geo_mean.csv', index = False)
sub_merge[['id', 'visitors']].head()
## Harmonic Mean
sub_merge['visitors'] = 2/(1/sub_merge['visitors_x'] + 1/sub_merge['visitors_y'])
sub_merge[['id', 'visitors']].to_csv('sub_hrm_mean.csv', index = False)
sub_merge[['id', 'visitors']].head()


from ML.KNN.module_Selection import module_Selection
from ML.KNN.Knn import Knn
from sklearn import datasets
if __name__ == '__main__':
    iris = datasets.load_iris()
    x = iris.data
    y = iris.target
    module = module_Selection(rate=0.4, seed=666)
    x_train, y_train, x_test, y_test = module.train_test_split(x,y)
    # print(len(x_train))
    # print(len(y_train))
    knn = Knn(k=6)
    knn.fit(x_train,y_train)
    y_pre = knn.predict(x_test)
    print('预测结果:{value}'.format(value=y_pre))
    print('预测准确率:{0}%'.format(int(sum(y_pre==y_test)/len(x_test)*100)))


"""
EXERCÍCIO 069: Análise de Dados do Grupo
Crie um programa que leia a idade e o sexo de várias pessoas. A cada pessoa cadastrada,
o programa deverá perguntar se o usuário quer ou não continuar. No final, mostre:
A) Quantas pessoas têm mais de 18 anos.
B) Quantos homens foram cadastrados.
C) Quantas mulheres têm menos de 20 anos.
"""
sm = sf = 0
i = imulher = 0
print('Ficha de Cadastro\n---')
while True:
    nome = str(input('Nome: '))
    sexo = ' '
    while sexo not in 'MF':
        sexo = str(input('Sexo F/M: ')).upper().strip()[0]
    if sexo == 'M':
        sm += 1
    else:
        sf += 1
    idade = int(input('Idade: '))
    if idade >= 18:
        i += 1
    if idade < 20 and sexo == 'F':
        imulher += 1
    cont = ' '
    while cont not in 'SN':
        cont = str(input('Deseja cadastrar mais pessoas? S/N: ')).upper().strip()[0]
    if cont == 'N':
        break
print(f'Pessoas maiores de 18 anos: {i}')
print(f'Mulheres com menos de 20 anos: {imulher}')
print(f'Homens cadastrados: {sm}')


from django.urls import path
from . import views

# Naming the whole urlpatterns
app_name = 'cart_app'

urlpatterns = [
    # The base URL is declared in the main urls.py file as 'cart/'
    path('', views.cart, name='cart'),
    path('add_cart/<int:product_id>/', views.add_to_cart, name='add_cart'),
    path('decrease_cart/<int:product_id>/<int:cart_item_id>/', views.decrease_cart, name='decrease_cart'),
    path('remove_cart/<int:product_id>/<int:cart_item_id>/', views.remove_cart, name='remove_cart'),
     # URL for Checkout
    path('checkout/', views.checkout, name='checkout')
]


# Given a grid of single-digit integer values representing heights of trees in a forest,
# Viewing distance = how many other trees it can see
# Scenic score = product of viewing distance in every direction
# Note: If a tree is on edge, its viewing distance is 0 that direction.
# WHAT IS THE HIGHEST POSSIBLE SCENIC SCORE FOR ANY TREE?
def vd_left(row,column,this_tree):
    if column==0:
        return 0 #tree is on edge
    vd = 1
    for c in range(column):
        if forest_array[row][column-c-1]<this_tree and (column-c>1):
            #fixed edge cases of adding too many close to end
            vd+=1
        else: #stop looking once we've found a taller tree
            break
    return vd

def vd_right(row,column,this_tree):
    if column==width-1:
        return 0 # tree is on edge
    vd = 1
    for c in range(1,width-column):
        if forest_array[row][column+c]<this_tree and (column+c+1) < width:
            # fixed edge cases of adding too many close to end
            vd+=1
        else: # stop looking once we've found a taller tree
            break
    return vd

def vd_up(row,column,this_tree):
    if row==0:
        return 0 # tree is on edge
    vd=1
    for r in range(row):
        if forest_array[row-r-1][column] < this_tree and (row - r > 1):
            # fixed edge cases of adding too many close to end
            vd += 1
        else:  # stop looking once we've found a taller tree
            break
    return vd

def vd_down(row,column,this_tree):
    if row==height-1:
        return 0 # tree is on edge
    vd=1
    for r in range(1,height-row):
        if forest_array[row+r][column] < this_tree and (row +r + 1) < height:
            # fixed edge cases of adding too many close to end
            vd += 1
        else:  # stop looking once we've found a taller tree
            break
    return vd

def visible_from_bottom(row,column,this_tree):
    diff = height - row
    for r in range(1,diff):
        if forest_array[row+r][column] >= this_tree:
            return False
    return True

def get_scenic_score(row,column,this_tree):
    return vd_left(row,column,this_tree) * vd_right(row,column,this_tree) * vd_up(row,column,this_tree) * vd_down(row,column,this_tree)

file_path = 'day8_input.txt'  # tree heights
with open(file_path) as input_file:
    forest = input_file.readlines()  # read all input lines
height = len(forest)
width = len(forest[0].strip())

forest_array = [] # create a 2-D array of integer values for trees, with every tree accessible via forest_array[row][column]
for row in range(height):
    new_row = list(forest[row].strip())
    for i in range(width):
        new_row[i] = int(new_row[i])
    forest_array.append(new_row)

max_scenic_score = 0
for row in range(height):
    for column in range(width):
        this_tree = forest_array[row][column] # get current tree's height
        ss=get_scenic_score(row, column, this_tree)
        if ss > max_scenic_score:
            max_scenic_score = ss
print(max_scenic_score)


import unittest
import responses
import digitalocean

from .BaseTest import BaseTest

class TestImage(BaseTest):

    def setUp(self):
        super(TestImage, self).setUp()
        self.image = digitalocean.Image(id=449676856, token=self.token)

    @responses.activate
    def test_load(self):
        data = self.load_from_file('images/single.json')

        responses.add(responses.GET,
                      '{}images/{}'.format(self.base_url, self.image.id),
                      body=data,
                      status=200,
                      content_type='application/json')

        self.image.load()

        self.assertEqual(responses.calls[0].request.url,
                         self.base_url + 'images/449676856')
        self.assertEqual(self.image.id, 449676856)
        self.assertEqual(self.image.name, 'My Snapshot')
        self.assertEqual(self.image.distribution, 'Ubuntu')
        self.assertEqual(self.image.public, False)
        self.assertEqual(self.image.created_at, "2014-08-18T16:35:40Z")
        self.assertEqual(self.image.size_gigabytes, 2.34)
        self.assertEqual(self.image.min_disk_size, 20)

    @responses.activate
    def test_destroy(self):
        responses.add(responses.DELETE,
                      '{}images/{}/'.format(self.base_url, self.image.id),
                      status=204,
                      content_type='application/json')

        self.image.destroy()

        self.assertEqual(responses.calls[0].request.url,
                         self.base_url + 'images/449676856/')

    @responses.activate
    def test_transfer(self):
        data = self.load_from_file('images/transfer.json')

        responses.add(responses.POST,
                      '{}images/{}/actions/'.format(
                        self.base_url, self.image.id),
                      body=data,
                      status=201,
                      content_type='application/json')

        res = self.image.transfer(new_region_slug='lon1')

        self.assertEqual(responses.calls[0].request.url,
                         self.base_url + 'images/449676856/actions/')
        self.assertEqual(res['action']['type'], 'transfer')
        self.assertEqual(res['action']['status'], 'in-progress')
        self.assertEqual(res['action']['id'], 68212728)

    @responses.activate
    def test_rename(self):
        data = self.load_from_file('images/rename.json')

        responses.add(responses.PUT,
                      '{}images/{}'.format(self.base_url, self.image.id),
                      body=data,
                      status=200,
                      content_type='application/json')

        res = self.image.rename(new_name='Descriptive name')

        self.assertEqual(responses.calls[0].request.url,
                         self.base_url + 'images/449676856')
        self.assertEqual(res['image']['name'], 'Descriptive name')

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


import torch

x_data = torch.Tensor([[1.0], [2.0], [3.0]])
y_data = torch.Tensor([[2.0], [4.0], [6.0]])

class LinearModel(torch.nn.Module):  # 通过Module构造出来的对象，会自动根据计算图实现backward的过程
    # 初始化对象时默认执行的函数
    def __init__(self):
        super(LinearModel, self).__init__()  # 不用管，这么写就完事了 just do it :)
        self.linear = torch.nn.Linear(1, 1)  # 构造一个对象，(1, 1) 一个是输入的特征维度，一个是输出的特征维度

    # 必须实现，在前馈的过程中需要执行的计算
    def forward(self, x):
        y_pred = self.linear(x)
        return y_pred

model = LinearModel()  # 可callable
# 也是继承至nn.module,参数：size_average：是否求均值,reduce：最终是否要求和，将维度降下来
criterion = torch.nn.MSELoss(size_average=False)  # 计算损失函数,返回值是(y_pred,y)
# 优化器 model.parameters()：取得model中的成员里有相应的权重，就都加到训练的集合中, lr: 学习率
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)

# Train Cycle
for epoch in range(100):
    y_pred = model(x_data)
    loss = criterion(y_pred, y_data)  # 计算损失
    print(epoch, loss)

    optimizer.zero_grad()  # 梯度归零
    loss.backward()  # 反向转播
    optimizer.step()  # 进行更新

print('w = ', model.linear.weight.item())
print('b = ', model.linear.bias.item())

x_test = torch.Tensor([[4.0]])
y_test = model(x_test)
print("y_pred = ", y_test.data)


import tensorflow as tf
import numpy as np
# from sklearn.preprocessing import MinMaxScaler

def MinMaxScaler(data):
    numerator = data - np.min(data, 0)
    denominator = np.max(data, 0) - np.min(data, 0)
    # noise term prevents the zero division
    return numerator / (denominator + 1e-5)

DATA_COLUMN = 13

# xy = np.loadtxt('data-03-diabetes.csv', delimiter=',', dtype=np.float32)
xy = np.loadtxt('.\\2.Logistic Classification\\heart.csv', delimiter=',', dtype=np.float32)
xy = MinMaxScaler(xy)
print(xy)

x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]

# placeholders for a tensor that will be always fed.
X = tf.placeholder(tf.float32, shape=[None, DATA_COLUMN])
Y = tf.placeholder(tf.float32, shape=[None, 1])

W = tf.Variable(tf.random_normal([DATA_COLUMN, 1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
# Hypothesis using sigmoid: tf.div(1., 1. + tf.exp(tf.matmul(X, W)))
hypothesis = tf.sigmoid(tf.matmul(X, W) + b)
# cost/loss function
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis))
train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)

# Accuracy computation
# True if hypothesis>0.5 else False
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))

# Launch graph
with tf.Session() as sess:
   # Initialize TensorFlow variables
   sess.run(tf.global_variables_initializer())

   for step in range(10001):
       cost_val, _ = sess.run([cost, train], feed_dict={X: x_data, Y: y_data})
       if step % 200 == 0:
           print(step, cost_val)

   # Accuracy report
   h, c, a = sess.run([hypothesis, predicted, accuracy],
                      feed_dict={X: x_data, Y: y_data})
   print("\nHypothesis: ", h, "\nCorrect (Y): ", c, "\nAccuracy: ", a)


import socket

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

s.bind((socket.gethostname(), 1234))
print("UDP server listening")

while True:
    message, addr = s.recvfrom(1024)
    clientMsg = f"Message from Client:{message}"
    clientIP  = f"Client IP Address:{addr}"
    print(clientMsg)
    print(clientIP)

    s.sendto(b"Hello client", addr)


import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np

def btn2_1():
	cap = cv.VideoCapture('Q2_Image/opticalFlow.mp4')
	# setup all parameters
	params = cv.SimpleBlobDetector_Params()
	params.minThreshold = 1
	params.maxThreshold = 255
	params.filterByArea = True
	params.minArea = 40
	params.maxArea = 60
	params.filterByCircularity = True
	params.minCircularity = 0.8
	params.filterByConvexity = True
	params.minConvexity = 0.8
	params.filterByInertia = True
	params.minInertiaRatio = 0.4
	# detect blobs in the image
	detector = cv.SimpleBlobDetector_create(params)
	ret, frame = cap.read()
	gray = cv.cvtColor(frame, cv.COLOR_RGB2GRAY)
	keypoints = detector.detect(gray)
	# show the result
	for i in range(len(keypoints)):
		image = cv.rectangle(	frame,
								(round(keypoints[i].pt[0] - 5.5), round(keypoints[i].pt[1] - 5.5)),
								(round(keypoints[i].pt[0] + 5.5), round(keypoints[i].pt[1] + 5.5)),
								(0, 0, 255),
								1)
		image = cv.line(	image,
							(round(keypoints[i].pt[0]), round(keypoints[i].pt[1] - 5.5)),
							(round(keypoints[i].pt[0]), round(keypoints[i].pt[1] + 5.5)),
							(0, 0, 255),
							1)
		image = cv.line(	image,
							(round(keypoints[i].pt[0] - 5.5), round(keypoints[i].pt[1])),
							(round(keypoints[i].pt[0] + 5.5), round(keypoints[i].pt[1])),
							(0, 0, 255),
							1)
	cv.imshow('preprocess', image)

def btn2_2():
	cap = cv.VideoCapture('Q2_Image/opticalFlow.mp4')

	lk_params = dict(	winSize=(15, 15),
						maxLevel=2,
						criteria=(cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 0.03))

	# setup all parameters
	params = cv.SimpleBlobDetector_Params()
	params.minThreshold = 1
	params.maxThreshold = 255
	params.filterByArea = True
	params.minArea = 40
	params.maxArea = 60
	params.filterByCircularity = True
	params.minCircularity = 0.8
	params.filterByConvexity = True
	params.minConvexity = 0.8
	params.filterByInertia = True
	params.minInertiaRatio = 0.4

	ret, last_frame = cap.read()
	detector = cv.SimpleBlobDetector_create(params)
	keypoints = detector.detect(last_frame)
	p0 = np.zeros((7, 1, 2), dtype='float32')

	for i in range(len(keypoints)):
		p = np.array([[round(keypoints[i].pt[0]), round(keypoints[i].pt[1])]], dtype='float32')
		p0[i] = p

	last_gray = cv.cvtColor(last_frame, cv.COLOR_BGR2GRAY)

	w = plt.imshow(last_gray)
	plt.ion()

	mask = np.zeros_like(last_frame)

	while ret:
		ret, frame = cap.read()
		if not ret:
			break
		gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)

		p1, st, err = cv.calcOpticalFlowPyrLK(last_gray, gray, p0, None, **lk_params)
		good_new = p1[st == 1]
		good_old = p0[st == 1]
		for i, (new, old) in enumerate(zip(good_new, good_old)):
			a, b = new.ravel()
			c, d = old.ravel()
			mask = cv.line(mask, (a, b), (c, d), (0, 0, 255), 2)
			frame = cv.circle(frame, (a, b), 5, (0, 0, 255), -1)
		img = cv.add(frame, mask)
		cv.imshow('video tracking', img)
		cv.waitKey(1)
		# Now update the previous frame and previous points
		last_gray = gray.copy()
		p0 = good_new.reshape(-1, 1, 2)

	cap.release()


#!/usr/bin/python3
import sys

def safe_function(fct, *args):
    try:
        res = fct(*args)
        return res
    except (IndexError, ZeroDivisionError) as err:
        sys.stderr.write('Exception: {}\n'.format(err))
        return None


from dataclasses import field

N, R, T = map(int, input().split())
p = [int(input()) for _ in range(N)]

# p = []
# for _ in range(N):
#     p.append(int(input()))

filled = [0] * R
res = 0

for i in range(1, T + 1):
    for j in range(N):
        if filled[(p[j] * i) % R] <= 0:
            res += 1
        else:
            filled[(p[j] * i) % R] -= 1
    if i > 1:
        for j in range(N):
            filled[(p[j] * i) % R] += 1
    # print(filled)
print(res)


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

from tests.base import TestCase

from vilya.models.linecomment import CommitLineComment, PullLineComment
from vilya.models.consts import (
    LINECOMMENT_TYPE_COMMIT, LINECOMMENT_TYPE_PULL)

TARGET_ID = 123
FROM_SHA = '2a200e45b0e223d13477e'
TO_SHA = '2a200e45b0e223d13477e'
OLD_PATH = 'testfolder/testfile.py'
NEW_PATH = 'testfolder/testfile2.py'
FROM_OID = '2a200e45b0e223d13477e'  # TODO: oids
TO_OID = '2a200e45b0e223d13477e'
AUTHOR = 'user1'
CONTENT1 = 'test line comment content'
CONTENT2 = 'another test line comment content'
CONTENT_ZH = u'你好,再见'

class LineCommentTest(TestCase):

    def test_add_comment(self):
        # commit
        c1 = CommitLineComment.add(TARGET_ID, FROM_SHA, TO_SHA,
                                   OLD_PATH, NEW_PATH, FROM_OID, TO_OID,
                                   20, 30, AUTHOR, CONTENT1)
        assert c1.target_id == TARGET_ID
        assert c1.target_type == LINECOMMENT_TYPE_COMMIT
        assert c1.from_sha == FROM_SHA
        assert c1.to_sha == TO_SHA
        assert c1.old_path == OLD_PATH
        assert c1.new_path == NEW_PATH
        assert c1.from_oid == FROM_OID
        assert c1.to_oid == TO_OID
        assert c1.old_linenum == 20
        assert c1.new_linenum == 30
        assert c1.linenum == (20, 30)
        assert c1.author == AUTHOR
        assert c1.content == CONTENT1
        assert c1.position is None
        assert c1.paths

        # pull
        c2 = PullLineComment.add(TARGET_ID, FROM_SHA, TO_SHA,
                                 OLD_PATH, NEW_PATH, FROM_OID, TO_OID,
                                 20, 30, AUTHOR, CONTENT2)
        assert c2.target_id == TARGET_ID
        assert c2.target_type == LINECOMMENT_TYPE_PULL
        assert c2.from_sha == FROM_SHA
        assert c2.to_sha == TO_SHA
        assert c2.old_path == OLD_PATH
        assert c2.new_path == NEW_PATH
        assert c2.from_oid == FROM_OID
        assert c2.to_oid == TO_OID
        assert c2.old_linenum == 20
        assert c2.new_linenum == 30
        assert c2.linenum == (20, 30)
        assert c2.author == AUTHOR
        assert c2.content == CONTENT2
        assert c2.position is None
        assert c2.paths

    def test_update_comment(self):
        # commit
        c1 = CommitLineComment.add(TARGET_ID, FROM_SHA, TO_SHA,
                                   OLD_PATH, NEW_PATH, FROM_OID, TO_OID,
                                   20, 30, AUTHOR, CONTENT1)
        assert c1.content == CONTENT1
        c1.update(CONTENT2)
        c1 = CommitLineComment.get(c1.id)
        assert c1.content == CONTENT2

        # pull
        c2 = PullLineComment.add(TARGET_ID, FROM_SHA, TO_SHA,
                                 OLD_PATH, NEW_PATH, FROM_OID, TO_OID,
                                 20, 30, AUTHOR, CONTENT2)
        assert c2.content == CONTENT2
        c2.update(CONTENT_ZH)
        c2 = CommitLineComment.get(c2.id)
        assert c2.content == CONTENT_ZH

    def test_delete_comment(self):
        # commit
        self.clear_comments(CommitLineComment, TARGET_ID, FROM_SHA)
        c1 = CommitLineComment.add(TARGET_ID, FROM_SHA, TO_SHA,
                                   OLD_PATH, NEW_PATH, FROM_OID, TO_OID,
                                   20, 30, AUTHOR, CONTENT1)
        cs = CommitLineComment.gets_by_target_and_ref(TARGET_ID, FROM_SHA)
        assert len(cs) == 1
        c1.delete()
        cs = CommitLineComment.gets_by_target_and_ref(TARGET_ID, FROM_SHA)
        assert len(cs) == 0

        # pull
        self.clear_comments(PullLineComment, TARGET_ID, FROM_SHA)
        c2 = PullLineComment.add(TARGET_ID, FROM_SHA, TO_SHA,
                                 OLD_PATH, NEW_PATH, FROM_OID, TO_OID,
                                 20, 30, AUTHOR, CONTENT1)
        cs = PullLineComment.gets_by_target_and_ref(TARGET_ID, FROM_SHA)
        assert len(cs) == 1
        c2.delete()
        cs = PullLineComment.gets_by_target_and_ref(TARGET_ID, FROM_SHA)
        assert len(cs) == 0

    def test_gets_by_target_and_ref(self):
        # commit
        self.clear_comments(CommitLineComment, TARGET_ID, FROM_SHA)
        c1 = CommitLineComment.add(TARGET_ID, FROM_SHA, TO_SHA,
                                   OLD_PATH, NEW_PATH, FROM_OID, TO_OID,
                                   20, 30, AUTHOR, CONTENT1)
        c2 = CommitLineComment.add(TARGET_ID, FROM_SHA, TO_SHA,
                                   OLD_PATH, NEW_PATH, FROM_OID, TO_OID,
                                   20, 30, AUTHOR, CONTENT1)
        c3 = CommitLineComment.add(TARGET_ID, FROM_SHA, TO_SHA,
                                   OLD_PATH, NEW_PATH, FROM_OID, TO_OID,
                                   20, 30, AUTHOR, CONTENT1)
        cs = CommitLineComment.gets_by_target_and_ref(TARGET_ID, FROM_SHA)
        assert len(cs) == 3

        self.clear_comments(PullLineComment, TARGET_ID, FROM_SHA)
        # pull
        PullLineComment.add(TARGET_ID, FROM_SHA, TO_SHA,
                            OLD_PATH, NEW_PATH, FROM_OID, TO_OID,
                            20, 30, AUTHOR, CONTENT1)
        PullLineComment.add(TARGET_ID, FROM_SHA, TO_SHA,
                            OLD_PATH, NEW_PATH, FROM_OID, TO_OID,
                            20, 30, AUTHOR, CONTENT1)
        cs = PullLineComment.gets_by_target_and_ref(TARGET_ID, FROM_SHA)
        assert len(cs) == 2

    def clear_comments(self, classObj, TARGET_ID, FROM_SHA):
        cs = classObj.gets_by_target_and_ref(TARGET_ID, FROM_SHA)
        classObj.delete_multi(cs)


import numpy as np
import difflib
import synonyms
import spacy

nlp = spacy.load("ru_core_news_lg")
# Позроверка поздаровался ли
def is_greet_in_phrase(doc):
    greet_list = synonyms.greet_list
    for token in doc:
        if token.lemma_ in greet_list and (
            token.dep_ == "amod" or token.dep_ == "ROOT"
        ):
            return True
    return False

# Проверка представился ли
def is_introduce_in_phrase(doc):
    intriduce_list = synonyms.intriduce_list
    for token in doc:
        # Проверяем на наличие именнованной сущиности Person, смотрим отношение nsubj к главному слову из списка для фраз "меня зовут", "моё имя", "с вами говорит", на всякий случай проверяем по леммам
        if token.ent_type_ == "PER" and token.head.lemma_ in intriduce_list:
            return True, token.text
        elif (
            token.lemma_ == "это"
            and token.dep_ == "nsubj"
            and token.head.dep_ == "ROOT"
        ):
            return True, token.head.text
    return False

# Получаем название компании
def similarity(s1, s2):
    normalized1 = s1.lower()
    normalized2 = s2.lower()
    matcher = difflib.SequenceMatcher(None, normalized1, normalized2)
    return matcher.ratio()

def get_company_name(doc):
    company_list = synonyms.company_list
    for i, token in enumerate(doc):
        count = i
        if token.lemma_ == "компания":
            company_name_list = list()
            for c, item in enumerate(company_list):
                company_name_list.append(similarity(doc[count + 1].text, item))
            company_index = company_name_list.index(max(company_name_list))
            if max(company_name_list) < 0.6:
                company_name = doc[count + 1].text
            else:
                company_name = company_list[company_index]
            return True, company_name
    return False

# проверка прощания
def is_bye(doc):
    byes_list = synonyms.byes_list
    for token in doc:
        if token.lemma_ in byes_list[0] or (
            token.lemma_ in byes_list[1] and token.dep_ == "case"
        ):
            return True
    return False

# получаем фарзы которые проверяем на наличие сущностей (dlg_len определяет n - первых и последних)
def get_phrases(arr, dlg_len, c, manager_phrases_index):
    if dlg_len > 0:
        phrases = arr[:, 3][manager_phrases_index[c][:dlg_len]]
        phrase_index = [manager_phrases_index[c][:dlg_len]][0]
        return phrases, phrase_index
    if dlg_len < 0:
        phrases = arr[:, 3][manager_phrases_index[c][dlg_len:]]
        phrase_index = [manager_phrases_index[c][dlg_len:]][0]
        return phrases, phrase_index

# проверяем фразы на наличие сущностей
def chek_phrase(phrases, phrase_index, function):
    for n, phrase in enumerate(phrases):
        doc = nlp(str(phrase))
        if function(doc):
            index = phrase_index[n]
            return (index, function(doc))
    return False

def get_result(arr, dlg_ids, dlg_len, manager_phrases_index):
    # проходимся по всем диалогам
    for c, item in enumerate(dlg_ids):
        # dlg_len = 3 #сколько фраз проверять

        phrases, phrase_index = get_phrases(arr, dlg_len, c, manager_phrases_index)

        greet = chek_phrase(phrases, phrase_index, is_greet_in_phrase)
        intro = chek_phrase(phrases, phrase_index, is_introduce_in_phrase)
        company = chek_phrase(phrases, phrase_index, get_company_name)
        dlg_len = dlg_len * -1  # сберем три последние

        phrases, phrase_index = get_phrases(arr, dlg_len, c, manager_phrases_index)
        bye = chek_phrase(phrases, phrase_index, is_bye)

        insight_txt = ""
        intro_n_comp = ""

        if greet:
            arr[:, 4][greet[0]] = "greeting=True"
            arr[:, 5][greet[0]] = "greeting=True"
            insight_txt += "greeting=True, "
        if not greet:
            ins_index = manager_phrases_index[c][0]  # первая фраза менеджера
            arr[:, 4][ins_index] = "greeting=False"
            arr[:, 5][ins_index] = "greeting=False"
            insight_txt += "greeting=False, "

        if intro:
            arr[:, 4][intro[0]] = "introduction=True"
            arr[:, 6][intro[0]] = "introduction=True"
            intro_n_comp += "introduction=True, "
            insight_txt += "introduction=True, "
            arr[:, 7][intro[0]] = intro[1][1]
        if not intro:
            intro_n_comp += "introduction=False, "
            insight_txt += "introduction=False, "
            arr[:, 6][ins_index] = "introduction=False"

        if company:
            intro_n_comp += "company=True"
            insight_txt += "company=True, "
            arr[:, 4][company[0]] = intro_n_comp
            arr[:, 8][company[0]] = company[1][1]
        if not company:
            insight_txt += "company=False, "
            intro_n_comp += "company=False"

        if bye:
            arr[:, 4][bye[0]] = "bying=True"
            arr[:, 9][bye[0]] = "bying=True"
            insight_txt += "bying=True"
        if not bye:
            insight_txt += "bying=False"
            ins_index = np.where(arr[:, 0] == item)[0][-1]
            arr[:, 4][ins_index] = "bying=False"  # последняя фраза
            arr[:, 9][ins_index] = "bying=False"

        if greet and bye:
            arr[:, 10][greet[0]] = "greet-bye=True"
        else:
            arr[:, 10][manager_phrases_index[c][0]] = "greet-bye=False"

        ins_index = np.where(arr[:, 0] == item)[0][0]  # индекс первой фразы диалога
        arr[:, 4][ins_index] = insight_txt
    return arr


import pygame
from sys import exit
from config import *
from debug import debug
from level import Level
from player import Player

class Game:
    """Create and run the entire game."""

    def __init__(self):
        # General setup
        pygame.init()
        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        main_icon = pygame.image.load('./Graphics/Images/player.png')
        pygame.display.set_caption('Schmoo')
        pygame.display.set_icon(main_icon)
        self.clock = pygame.time.Clock()

        # Creating player
        self.player = Player()

        # Creating the level
        self.running = True
        self.tavern_0 = Level("./Graphics/Levels/Tavern_0", self.player)

    def main(self):
        """Main game loop. Displays the gameplay."""
        while self.running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False

            # Updating the screen
            self.screen.fill(BLACK)
            self.tavern_0.run()
            pygame.display.update()
            self.clock.tick(FPS)

            # if BATTLE:
                # im thkinking this is where we check if there is a fight happening and update the stuff.

0
if __name__ == '__main__':
    # Creating the game
    game = Game()
    game.main()

    # Closing the code
    pygame.quit()
    exit()


import os
import time
import errno
import select
import threading
import libvirt

from .utils import version_compare, Is_Fedora

class eventListenerThread(threading.Thread):
    def __init__(self, event_source, event_id, event_type, event_detail, logger, rand=None):
        """
        A thread that will keep looping until callback is called with a event matches
        given event id, detail and type.

        use join(timeout) and stop to wait for a event
        eg:
            thread = eventListenerThread("domain_name",
                                         libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                         libvirt.VIR_DOMAIN_EVENT_DEFINED,
                                         libvirt.VIR_DOMAIN_EVENT_DEFINED_ADDED,
                                         logger,
                                         "ksNsd*2s1."
            )
            try:
                thread.start()
                # Test Code here
                # Remember to add thread.callback to event handlers
                thread.join(10)
                if thread.result:
                    # Got wanted event
                else:
                    # Didn't get the event :(
            finally:
                thread.stop()

        :param event_source: Where the waiting event should come from
            could be a domain/node device/others.
        :param event_id: (Not actually used yet).
        :param event_type: Event type to wait for.
        :param event_detail: Event detail to wait for.
        :param rand: A random string, you can use it to verify opaque's integrity.
            If not given, callback's opaque will be ignored.
            If given, callback's opaque need to be the same string.
        """
        threading.Thread.__init__(self, name="libvirtEventListener")
        self.result = None
        self.rand = rand
        self.logger = logger
        self.event_id = event_id
        self.event_type = event_type
        self.event_detail = event_detail
        self.event_source = event_source

    def callback(self, conn, source, event_type, event_detail, opaque):
        random = opaque
        logger = self.logger
        if hasattr(source, 'name'):
            src_name = source.name()
        else:
            src_name = str(source)
        if hasattr(source, 'UUIDString'):
            src_idtype, src_id = 'UUID: ', source.UUIDString()
        elif hasattr(source, 'ID'):
            src_idtype, src_id = 'ID: ', str(source.ID())
        else:
            src_idtype, src_id = 'NO ID Avaliable', ''

        logger.info("Got EVENT: From %s (%s%s), Type:%s, Detail:%s" %
                    (src_name, src_idtype, src_id, event_type, event_detail))

        if random != self.rand:
            logger.error("Callback's opaque is currupted !")
            return
        if self.event_type is not None and event_type != self.event_type:
            logger.error("Wrong event type!")
            return
        if self.event_detail is not None and event_detail != self.event_detail:
            logger.error("Wrong event detail!")
            return
        if self.event_source is not None and source.name() != self.event_source:
            logger.error("Wrong event source!")
            return
        logger.info("Just as expected!")
        self.result = True
        return

    def run(self):
        while self.result is None:
            time.sleep(1)

    def stop(self):
        self.result = False

class eventListenerThreadThreshold(threading.Thread):
    def __init__(self, event_source, event_id, threshold, logger, rand=None):
        threading.Thread.__init__(self, name="libvirtEventListener")
        self.result = None
        self.rand = rand
        self.logger = logger
        self.event_id = event_id
        self.event_source = event_source
        self.threshold = threshold

    def callbackThreshold(self, conn, source, dev, path, threshold, excess, opaque):
        random = opaque
        logger = self.logger
        if hasattr(source, 'name'):
            src_name = source.name()
        else:
            src_name = str(source)
        if hasattr(source, 'UUIDString'):
            src_idtype, src_id = 'UUID: ', source.UUIDString()
        elif hasattr(source, 'ID'):
            src_idtype, src_id = 'ID: ', str(source.ID())
        else:
            src_idtype, src_id = 'NO ID Avaliable', ''

        logger.info("Got EVENT: From %s (%s%s), Threshold:%s" %
                    (src_name, src_idtype, src_id, threshold))

        if random != self.rand:
            logger.error("Callback's opaque is currupted !")
            return
        if self.event_source is not None and source.name() != self.event_source:
            logger.error("Wrong event source!")
            return
        if self.threshold is not None and str(threshold) != self.threshold:
            logger.error("Wrong threshold!")
            return

        logger.info("Just as expected!")
        self.result = True

    def run(self):
        while self.result is None:
            time.sleep(1)

    def stop(self):
        self.result = False

###############################################################
# Following class is a wrap up of the event loop
# from libvirt-python/examples/event-test.py
###############################################################
#
# This general purpose event loop will support waiting for file handle
# I/O and errors events, as well as scheduling repeatable timers with
# a fixed interval.
#
# It is a pure python implementation based around the poll() API
#
class virEventLoopPureThread(threading.Thread):
    """
    To use this standalone pure event loop,
    Init a instance, regist and run it. Remember to stop the running thread
    before test ends.
    eg:
        loop = virEventLoopPure(logger)
        try:
            loop.regist(libvirt)
            loop.start()
            # Run test here
        finally:
            loop.stop()
    """
    # This class contains the data we need to track for a
    # single file handle

    class virEventLoopPureHandle:

        def __init__(self, handle, fd, events, cb, opaque):
            self.handle = handle
            self.fd = fd
            self.events = events
            self.cb = cb
            self.opaque = opaque

        def get_id(self):
            return self.handle

        def get_fd(self):
            return self.fd

        def get_events(self):
            return self.events

        def set_events(self, events):
            self.events = events

        def dispatch(self, events):
            self.cb(self.handle,
                    self.fd,
                    events,
                    self.opaque)

    # This class contains the data we need to track for a
    # single periodic timer
    class virEventLoopPureTimer:

        def __init__(self, timer, interval, cb, opaque):
            self.timer = timer
            self.interval = interval
            self.cb = cb
            self.opaque = opaque
            self.lastfired = 0

        def get_id(self):
            return self.timer

        def get_interval(self):
            return self.interval

        def set_interval(self, interval):
            self.interval = interval

        def get_last_fired(self):
            return self.lastfired

        def set_last_fired(self, now):
            self.lastfired = now

        def dispatch(self):
            self.cb(self.timer,
                    self.opaque)

    def __init__(self, logger):
        threading.Thread.__init__(self, name="libvirtEventLoop-Pure")
        self.poll = select.poll()
        self.pipetrick = os.pipe()
        self.pendingWakeup = False
        self.runningPoll = False
        self.nextHandleID = 1
        self.nextTimerID = 1
        self.handles = []
        self.timers = []
        self.quit = False
        self.logger = logger
        if Is_Fedora() or version_compare("libvirt-python", 3, 7, 0, logger):
            self.cleanup = []

        # The event loop can be used from multiple threads at once.
        # Specifically while the main thread is sleeping in poll()
        # waiting for events to occur, another thread may come along
        # and add/update/remove a file handle, or timer. When this
        # happens we need to interrupt the poll() sleep in the other
        # thread, so that it'll see the file handle / timer changes.
        #
        # Using OS level signals for this is very unreliable and
        # hard to implement correctly. Thus we use the real classic
        # "self pipe" trick. A anonymous pipe, with one end registered
        # with the event loop for input events. When we need to force
        # the main thread out of a poll() sleep, we simple write a
        # single byte of data to the other end of the pipe.
        self.logger.debug("Self pipe watch %d write %d" % (self.pipetrick[0],
                                                           self.pipetrick[1]))
        self.poll.register(self.pipetrick[0], select.POLLIN)

    # Calculate when the next timeout is due to occurr, returning
    # the absolute timestamp for the next timeout, or 0 if there is
    # no timeout due
    def next_timeout(self):
        next = 0
        for t in self.timers:
            last = t.get_last_fired()
            interval = t.get_interval()
            if interval < 0:
                continue
            if next == 0 or (last + interval) < next:
                next = last + interval

        return next

    # Lookup a virEventLoopPureHandle object based on file descriptor
    def get_handle_by_fd(self, fd):
        for h in self.handles:
            if h.get_fd() == fd:
                return h
        return None

    # Lookup a virEventLoopPureHandle object based on its event loop ID
    def get_handle_by_id(self, handleID):
        for h in self.handles:
            if h.get_id() == handleID:
                return h
        return None

    # This is the heart of the event loop, performing one single
    # iteration. It asks when the next timeout is due, and then
    # calcuates the maximum amount of time it is able to sleep
    # for in poll() pending file handle events.
    #
    # It then goes into the poll() sleep.
    #
    # When poll() returns, there will zero or more file handle
    # events which need to be dispatched to registered callbacks
    # It may also be time to fire some periodic timers.
    #
    # Due to the coarse granularity of schedular timeslices, if
    # we ask for a sleep of 500ms in order to satisfy a timer, we
    # may return upto 1 schedular timeslice early. So even though
    # our sleep timeout was reached, the registered timer may not
    # technically be at its expiry point. This leads to us going
    # back around the loop with a crazy 5ms sleep. So when checking
    # if timeouts are due, we allow a margin of 20ms, to avoid
    # these pointless repeated tiny sleeps.
    def run_once(self):
        sleep = -1
        self.runningPoll = True

        if version_compare("libvirt-python", 3, 7, 0, self.logger):
            for opaque in self.cleanup:
                libvirt.virEventInvokeFreeCallback(opaque)
            self.cleanup = []

        try:
            next = self.next_timeout()
            #self.logger.debug("Next timeout due at %d" % next)
            if next > 0:
                now = int(time.time() * 1000)
                if now >= next:
                    sleep = 0
                else:
                    sleep = (next - now) / 1000.0

            #self.logger.debug("Poll with a sleep of %d" % sleep)
            events = self.poll.poll(sleep)

            # Dispatch any file handle events that occurred
            for (fd, revents) in events:
                # See if the events was from the self-pipe
                # telling us to wakup. if so, then discard
                # the data just continue
                if fd == self.pipetrick[0]:
                    self.pendingWakeup = False
                    data = os.read(fd, 1)
                    continue

                h = self.get_handle_by_fd(fd)
                if h:
                    #self.logger.debug("Dispatch fd %d handle %d events %d" %
                    #                  (fd, h.get_id(), revents))
                    h.dispatch(self.events_from_poll(revents))

            now = int(time.time() * 1000)
            for t in self.timers:
                interval = t.get_interval()
                if interval < 0:
                    continue

                want = t.get_last_fired() + interval
                # Deduct 20ms, since scheduler timeslice
                # means we could be ever so slightly early
                if now >= (want - 20):
                    #self.logger.debug("Dispatch timer %d now %s want %s" %
                    #                  (t.get_id(), str(now), str(want)))
                    t.set_last_fired(now)
                    t.dispatch()

        except (os.error, select.error) as e:
            if e.args[0] != errno.EINTR:
                raise
        finally:
            self.runningPoll = False

    # Actually the event loop forever
    def run_loop(self):
        self.quit = False
        while not self.quit:
            self.run_once()

    def interrupt(self):
        if self.runningPoll and not self.pendingWakeup:
            self.pendingWakeup = True
            os.write(self.pipetrick[1], 'c'.encode())

    # Registers a new file handle 'fd', monitoring  for 'events' (libvirt
    # event constants), firing the callback  cb() when an event occurs.
    # Returns a unique integer identier for this handle, that should be
    # used to later update/remove it
    def add_handle(self, fd, events, cb, opaque):
        handleID = self.nextHandleID + 1
        self.nextHandleID = self.nextHandleID + 1

        h = self.virEventLoopPureHandle(handleID, fd, events, cb, opaque)
        self.handles.append(h)

        self.poll.register(fd, self.events_to_poll(events))
        self.interrupt()

        #self.logger.debug("Add handle %d fd %d events %d" %
        #                  (handleID, fd, events))

        return handleID

    # Registers a new timer with periodic expiry at 'interval' ms,
    # firing cb() each time the timer expires. If 'interval' is -1,
    # then the timer is registered, but not enabled
    # Returns a unique integer identier for this handle, that should be
    # used to later update/remove it
    def add_timer(self, interval, cb, opaque):
        timerID = self.nextTimerID + 1
        self.nextTimerID = self.nextTimerID + 1

        h = self.virEventLoopPureTimer(timerID, interval, cb, opaque)
        self.timers.append(h)
        self.interrupt()

        #self.logger.debug("Add timer %d interval %d" % (timerID, interval))

        return timerID

    # Change the set of events to be monitored on the file handle
    def update_handle(self, handleID, events):
        h = self.get_handle_by_id(handleID)
        if h:
            h.set_events(events)
            self.poll.unregister(h.get_fd())
            self.poll.register(h.get_fd(), self.events_to_poll(events))
            self.interrupt()

            #self.logger.debug("Update handle %d fd %d events %d" %
            #                  (handleID, h.get_fd(), events))

    # Change the periodic frequency of the timer
    def update_timer(self, timerID, interval):
        for h in self.timers:
            if h.get_id() == timerID:
                h.set_interval(interval)
                self.interrupt()

                #self.logger.debug("Update timer %d interval %d" %
                #                  (timerID, interval))
                break

    # Stop monitoring for events on the file handle
    def remove_handle(self, handleID):
        handles = []
        for h in self.handles:
            if h.get_id() == handleID:
                self.poll.unregister(h.get_fd())
                if version_compare("libvirt-python", 3, 7, 0, self.logger):
                    self.cleanup.append(h.opaque)
                #self.logger.debug("Remove handle %d fd %d" %
                #                  (handleID, h.get_fd()))
            else:
                handles.append(h)
        self.handles = handles
        self.interrupt()

    # Stop firing the periodic timer
    def remove_timer(self, timerID):
        timers = []
        for h in self.timers:
            if version_compare("libvirt-python", 3, 7, 0, self.logger):
                if h.get_id() != timerID:
                    timers.append(h)
                else:
                    self.cleanup.append(h.opaque)
            else:
                if h.get_id() != timerID:
                    timers.append(h)
        self.timers = timers
        self.interrupt()

    # Convert from libvirt event constants, to poll() events constants
    def events_to_poll(self, events):
        ret = 0
        if events & libvirt.VIR_EVENT_HANDLE_READABLE:
            ret |= select.POLLIN
        if events & libvirt.VIR_EVENT_HANDLE_WRITABLE:
            ret |= select.POLLOUT
        if events & libvirt.VIR_EVENT_HANDLE_ERROR:
            ret |= select.POLLERR
        if events & libvirt.VIR_EVENT_HANDLE_HANGUP:
            ret |= select.POLLHUP
        return ret

    # Convert from poll() event constants, to libvirt events constants
    def events_from_poll(self, events):
        ret = 0
        if events & select.POLLIN:
            ret |= libvirt.VIR_EVENT_HANDLE_READABLE
        if events & select.POLLOUT:
            ret |= libvirt.VIR_EVENT_HANDLE_WRITABLE
        if events & select.POLLNVAL:
            ret |= libvirt.VIR_EVENT_HANDLE_ERROR
        if events & select.POLLERR:
            ret |= libvirt.VIR_EVENT_HANDLE_ERROR
        if events & select.POLLHUP:
            ret |= libvirt.VIR_EVENT_HANDLE_HANGUP
        return ret

    def regist(self, libvirt):
        # Now glue an instance of the general event loop into libvirt's event loop
        # These next set of 6 methods are the glue between the official
        # libvirt events API, and our particular impl of the event loop
        #
        # There is no reason why the 'virEventLoopPure' has to be used.
        # An application could easily may these 6 glue methods hook into
        # another event loop such as GLib's, or something like the python
        # Twisted event framework.

        def virEventAddHandleImpl(fd, events, cb, opaque):
            return self.add_handle(fd, events, cb, opaque)

        def virEventUpdateHandleImpl(handleID, events):
            return self.update_handle(handleID, events)

        def virEventRemoveHandleImpl(handleID):
            return self.remove_handle(handleID)

        def virEventAddTimerImpl(interval, cb, opaque):
            return self.add_timer(interval, cb, opaque)

        def virEventUpdateTimerImpl(timerID, interval):
            return self.update_timer(timerID, interval)

        def virEventRemoveTimerImpl(timerID):
            return self.remove_timer(timerID)

        # This tells libvirt what event loop implementation it
        # should use
        libvirt.virEventRegisterImpl(virEventAddHandleImpl,
                                     virEventUpdateHandleImpl,
                                     virEventRemoveHandleImpl,
                                     virEventAddTimerImpl,
                                     virEventUpdateTimerImpl,
                                     virEventRemoveTimerImpl)

    def unregist(self, libvirt):
        # Not used yet
        pass

    def start(self):
        self.setDaemon(True)
        threading.Thread.start(self)

    def run(self):
        self.run_loop()

    def stop(self):
        self.quit = True
        self.interrupt()

def eventLoopPure(logger):
    eventLoop = virEventLoopPureThread(logger)
    eventLoop.regist(libvirt)
    eventLoop.start()

    return 0


from openai import OpenAI
import openai
from config import OPENAI_KEY
import os
openai.api_key = os.getenv("OPENAI_API_KEY")
# openai.api_key = OPENAI_KEY

client = OpenAI()

def img2txt(url, data_name="weibo"):
  print("Generating Image Captioning...")
  if "http:" in url:
      response = client.chat.completions.create(
        model="gpt-4-vision-preview",
        messages=[
          {
            "role": "user",
            "content": [
              {"type": "text", "text": "Let's think step by step. First, extract all the text in the image (OCR). Then, describe this picture. If it is a photo, how many characters are in this pic? what are they doing? what are their relations. what is the background? what activity is this? If it is a chart, what kind of chart it is? what interesting data does it have? what is the title or purpose of this chart? use 200 words to answer"},
              {
                "type": "image_url",
                "image_url": {
                  "url": f"{url}",
                },
              },
            ],
          }
        ],
        max_tokens=300,
      )
      return response.choices[0].message.content
  else:
      import base64
      import requests
      # OpenAI API Key
      # api_key = OPENAI_KEY
      api_key = os.getenv("OPENAI_API_KEY")

      # Function to encode the image
      def encode_image(image_path):
        if data_name == "weibo":
          rumor_img_dir = "../nlp-project/Data/weibo/nonrumor_images/"
          rumor_img_dir = os.path.abspath(rumor_img_dir)

          non_rumor_img_dir = "../nlp-project/Data/weibo/rumor_images/"
          non_rumor_img_dir = os.path.abspath(non_rumor_img_dir)

          for filename in os.listdir(rumor_img_dir):
            if filename == image_path:
              image_path = rumor_img_dir + "/" + image_path
              break

          for filename in os.listdir(non_rumor_img_dir):
            if filename == image_path:
              image_path = non_rumor_img_dir + "/" + image_path
              break

          with open(image_path, "rb") as image_file:
            return base64.b64encode(image_file.read()).decode('utf-8')

        elif data_name == "twitter":
          img_dir = "../nlp-project/Data/twitter/Mediaeval2016_TestSet_Images"

          for filename in os.listdir(img_dir):
            if filename[:-4] == image_path:
              image_path = img_dir + "/" + image_path + ".jpg"
              break

          with open(image_path, "rb") as image_file:
            return base64.b64encode(image_file.read()).decode('utf-8')

      # Path to your image
      image_path = url

      # Getting the base64 string
      base64_image = encode_image(image_path)

      headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {api_key}"
      }

      payload = {
        "model": "gpt-4-vision-preview",
        "messages": [
          {
            "role": "user",
            "content": [
              {
                "type": "text",
                "text": "Let's think step by step. First, extract all the text in the image (OCR). Then, describe this picture. If it is a photo, how many characters are in this pic? what are they doing? what are their relations. what is the background? what activity is this? If it is a chart, what kind of chart it is? what interesting data does it have? what is the title or purpose of this chart? use 200 words to answer"
              },
              {
                "type": "image_url",
                "image_url": {
                  "url": f"data:image/jpeg;base64,{base64_image}"
                }
              }
            ]
          }
        ],
        "max_tokens": 300
      }

      response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)

      return eval(response.text)["choices"][0]["message"]["content"]


# El ciclo for
"""
varias_palabras = []
num_palabras = 0

num_palabras = int(input("¿Cuantas palabras quieres ingresar? "))

# Si se necesita iterar sobre una secuencia de números usamos la función range()
# Range() generar números del cero al numero que especifiques -1

for i in range(num_palabras) :
    print("Palabra ", i + 1, " - Te quedan ", num_palabras - i - 1)
    palabra = input("Introduce la palabra: ")
    varias_palabras.append(palabra)

print("Este es el resultado")
print(varias_palabras)

"""
print("-----------------------------------------------------------------")

# Buscador de números primos
# Un número primo es el que solo se divide por si mismo y por 1

for n in range(2, 100) :
    es_primo = True
    for x in range(2, n) :
        if n % x == 0 :
            es_primo = False
            break
    if es_primo :
        print(n, "es número primo")

print("-----------------------------------------------------------------")
# CONTINUE
# Detector de pares e impares
for num in range(2, 10) :
    if num % 2 == 0 :
        print(num, "es par")
        continue
    print(num, "es impar")