import tensorflow as tf
import tensorflow_datasets as tfds
import tensorflow_addons as tfa
import gin
from model.augmentations.auto_augment import distort_image_with_autoaugment
from model.augmentations.simclr_augment import distort_simclr
from model.augmentations.simclr_augment import resize_only

@gin.configurable
def gen_pipeline_train(ds_name='mnist',
                       tfds_path='~/tensorflow_datasets',
                       size_batch=64,
                       meta_batch_size=4,
                       b_shuffle=True,
                       size_buffer_cpu=5,
                       shuffle_buffer_size=0,
                       dataset_cache=False,
                       augmentation='autoaug',
                       num_parallel_calls=10,
                       split=tfds.Split.TRAIN,
                       finetuning=False,
                       validation_set=False):
    # Load and prepare tensorflow dataset
    data, info = tfds.load(name=ds_name,
                           data_dir=tfds_path,
                           split=split,
                           shuffle_files=False,
                           with_info=True)

    @tf.function
    def _map_data(*args):
        image = args[0]['image']
        label = args[0]['label']
        label = tf.one_hot(label, info.features['label'].num_classes)

        # Cast image type and normalize to 0/1
        if validation_set:
            image = tf.cast(image, tf.float32) / 255.0
        if finetuning:
            image = tf.cast(image, tf.float32) / 255.0
            if augmentation == 'simclr':
                image = resize_only(image)
        return image, label

    @tf.function
    def _map_augment_cifar10(*inputs):
        image, label = inputs

        image = tf.image.random_flip_left_right(image)
        IMG_SIZE = image.shape[0]  # CIFAR10: 32
        # Add 4 pixels of padding
        image = tf.image.resize_with_crop_or_pad(image, IMG_SIZE + 4, IMG_SIZE + 4)
        # Random crop back to the original size
        image = tf.image.random_crop(image, size=[IMG_SIZE, IMG_SIZE, 3])

        return image, label

    @tf.function
    def _map_augment_cifar10(*inputs):
        image, label = inputs

        image = tf.image.random_flip_left_right(image)
        IMG_SIZE = image.shape[0]  # CIFAR10: 32
        # Add 4 pixels of padding
        image = tf.image.resize_with_crop_or_pad(image, IMG_SIZE + 4, IMG_SIZE + 4)
        # Random crop back to the original size
        image = tf.image.random_crop(image, size=[IMG_SIZE, IMG_SIZE, 3])

        return image, label

    def _simple_augment(image):
        image = tf.cast(image, tf.float32) / 255.
        image = tf.image.random_flip_left_right(image)
        IMG_SIZE = image.shape[0]  # CIFAR10: 32
        # Add 4 pixels of padding
        image = tf.image.resize_with_crop_or_pad(image, IMG_SIZE + 4, IMG_SIZE + 4)
        # Random crop back to the original size
        image = tf.image.random_crop(image, size=[IMG_SIZE, IMG_SIZE, 3])
        return image

    @tf.function
    @tf.autograph.experimental.do_not_convert
    def _map_augment(*args):
        image = args[0]
        label = args[1]
        if augmentation == 'autoaug':
            image1 = distort_image_with_autoaugment(image, 'cifar10')
            image2 = distort_image_with_autoaugment(image, 'cifar10')
            # image3 = distort_image_with_autoaugment(image, 'cifar10')
            image3 = _simple_augment(image)
            image1 = tf.cast(image1, tf.float32) / 255.0
            image2 = tf.cast(image2, tf.float32) / 255.0
            image3 = tf.cast(image3, tf.float32) / 255.0
        elif augmentation == 'simclr':
            image1 = distort_simclr(image)
            image2 = distort_simclr(image)
            # image3 = distort_simclr(image)
            image3 = _simple_augment(image)

        return image1, image2, image3, label

    #@tf.function
    def _batch_specific_aug(*args):
        #TODO: Add contrast?

        stddev = tf.random.uniform((1,), 0, 0.02, dtype=tf.float32)
        flip_ud = tf.random.uniform([], 0, 1)
        flip_rl = tf.random.uniform([], 0, 1)
        blur = tf.random.uniform([], 0, 1)
        brightness = tf.random.uniform((1,), -0.2, 0.2, dtype=tf.float32)
        img1 = batch_augmentation(args[0], stddev, flip_ud, flip_rl, brightness, blur)
        img2 = batch_augmentation(args[1], stddev, flip_ud, flip_rl, brightness, blur)
        img3 = batch_augmentation(args[2], stddev, flip_ud, flip_rl, brightness, blur)
        label = args[3]
        return img1, img2, img3, label

    # Map data
    dataset = data.map(map_func=_map_data, num_parallel_calls=num_parallel_calls)

    # Cache data
    if dataset_cache:
        dataset = dataset.cache()

    # Shuffle data
    if b_shuffle:
        if shuffle_buffer_size == 0:
            shuffle_buffer_size = info.splits['train'].num_examples
        dataset = dataset.shuffle(buffer_size=shuffle_buffer_size, reshuffle_each_iteration=True)

    # Map Augmentation
    if not validation_set:
        if not finetuning:
            dataset = dataset.map(map_func=_map_augment, num_parallel_calls=num_parallel_calls)
        if finetuning and split == 'train':
            dataset = dataset.map(map_func=_map_augment_cifar10, num_parallel_calls=num_parallel_calls)

    # Batching
    dataset = dataset.batch(batch_size=size_batch,
                            drop_remainder=True)  # > 1.8.0: use drop_remainder=True

    if not validation_set:
        dataset = dataset.map(map_func=_batch_specific_aug, num_parallel_calls=num_parallel_calls)

    if not validation_set:
        dataset = dataset.batch(batch_size=meta_batch_size,
                                drop_remainder=True)  # > 1.8.0: use drop_remainder=True
    if size_buffer_cpu > 0:
        dataset = dataset.prefetch(buffer_size=size_buffer_cpu)

    return dataset, info

@gin.configurable
def gen_pipeline_test_time(ds_name='mnist',
                           tfds_path='~/tensorflow_datasets',
                           size_batch=1,
                           b_shuffle=True,
                           size_buffer_cpu=5,
                           shuffle_buffer_size=0,
                           dataset_cache=False,
                           augmentation='simclr',
                           num_parallel_calls=10,
                           split='test',
                           image_repetition=True
                           ):
    # Load and prepare tensorflow dataset
    data, info = tfds.load(name=ds_name,
                           data_dir=tfds_path,
                           split=split,
                           shuffle_files=False,
                           with_info=True)

    @tf.function
    def _map_data_im_rep(*args):
        image = tf.expand_dims(args[0]['image'], 0)
        image = tf.repeat(image, repeats=size_batch, axis=0)

        label = tf.one_hot(args[0]['label'], info.features['label'].num_classes)
        label = tf.repeat(tf.expand_dims(label, 0), repeats=size_batch, axis=0)
        return image, label

    @tf.function
    def _map_data(*args):
        image = args[0]['image']
        label = args[0]['label']
        label = tf.one_hot(label, info.features['label'].num_classes)

        return image, label

    @tf.function
    @tf.autograph.experimental.do_not_convert
    def _map_test_time_augment(*args):
        image = args[0]
        label = args[1]
        if augmentation == 'autoaug':
            image1 = distort_image_with_autoaugment(image, 'cifar10')
            image2 = distort_image_with_autoaugment(image, 'cifar10')
            image1 = tf.cast(image1, tf.float32) / 255.0
            image2 = tf.cast(image2, tf.float32) / 255.0
            image3 = tf.cast(image, tf.float32) / 255.0

        elif augmentation == 'simclr':
            image1 = distort_simclr(image)
            image2 = distort_simclr(image)
            image3 = tf.cast(image, tf.float32) / 255.0

        return image1, image2, image3, label

    # Map data
    if image_repetition is True:
        dataset = data.map(map_func=_map_data_im_rep, num_parallel_calls=num_parallel_calls)
        dataset = dataset.unbatch()
    else:
        dataset = data.map(map_func=_map_data, num_parallel_calls=num_parallel_calls)

    # Cache data
    if dataset_cache:
        dataset = dataset.cache()
    if image_repetition is False:
        # Shuffle data
        if b_shuffle:
            if shuffle_buffer_size == 0:
                shuffle_buffer_size = info.splits['train'].num_examples
            dataset = dataset.shuffle(buffer_size=shuffle_buffer_size, reshuffle_each_iteration=True)
    dataset = dataset.map(map_func=_map_test_time_augment, num_parallel_calls=num_parallel_calls)
    # Batching
    dataset = dataset.batch(batch_size=size_batch,
                            drop_remainder=True)  # > 1.8.0: use drop_remainder=True

    return dataset, info

@gin.configurable
def batch_augmentation(image, stddev, flip_ud, flip_rl, brightness, blur, flip_ud_rate, flip_rl_rate, blur_rate):
    # image must be scaled in [0, 1]

    # flip up/down
    if flip_ud < flip_ud_rate:
        image = tf.image.flip_up_down(image)

    # flip right/left
    if flip_rl < flip_rl_rate:
        image = tf.image.flip_left_right(image)

    # gaussian blur
    if blur < blur_rate:
        image = tfa.image.filters.gaussian_filter2d(image)

    # brightness
    image = tf.image.adjust_brightness(image, brightness)

    # gaussian noise
    noise = tf.random.normal(shape=tf.shape(image), mean=0.0, stddev=stddev, dtype=tf.float32)  # stddev=0.01
    noise_img = tf.add(image, noise)
    noise_img = tf.clip_by_value(noise_img, 0.0, 1.0)
    return noise_img


from gensim import corpora, models, similarities
from jieba_fast import cut_for_search
'''
通过gensim的similarities 包，实现基于tfidf 的短文本聚类
'''

def readData():
    data = []
    filename = "/data/stargazer/t_brand_info.csv";
    with open(filename,encoding='utf-8',mode='r') as f:
        for line in f:
            data.append(line.split(",")[1].strip())

    return data

if __name__ == '__main__':

    data = readData()
    texts = [list(cut_for_search(document)) for document in data]

    # # 2.计算词频
    # frequency = defaultdict(int)  # 构建一个字典对象
    # # 遍历分词后的结果集，计算每个词出现的频率
    # for text in texts:
    #     for token in text:
    #         frequency[token] += 1

    # 3.创建字典（单词与编号之间的映射）
    dictionary = corpora.Dictionary(texts)
    # 4。建立语料库
    corpus = [dictionary.doc2bow(text) for text in texts]
    # 6.初始化模型
    # 初始化一个tfidf模型,可以用它来转换向量（词袋整数计数）表示方法为新的表示方法（Tfidf 实数权重）
    tfidf = models.TfidfModel(corpus)

    # 将整个语料库转为tfidf表示方法
    corpus_tfidf = tfidf[corpus]

    # 7.创建索引，
    # 此处可以将模型保存起来。这样下次就不需要再训练了
    index = similarities.MatrixSimilarity(corpus_tfidf)

    # 8.相似度计算
    # new_doc = contents[0][0]  # 假定用contents的第1篇文章对比，由于contents每个元素由id和content组成，所以是contents[0][0]
    # new_vec = dic.doc2bow(tokenization(new_doc))

    saveFilename = "/data/stargazer/simRes_matrix.csv"
    record = [False for i in range(len(data))]
    with open(saveFilename, encoding='utf-8', mode='a') as f:
        no = 1
        for i,vec in enumerate(corpus):
            if record[i] is True: continue
            idf = tfidf[vec]
            sims = list(enumerate(index[idf]))
            sorted(sims,key=lambda x:x[1],reverse=True)
            res = list(filter(lambda x:x[0] != i and x[1]>0.3 ,sims))
            f.write(str.format("{0},{1},{2}\n", no, data[i], 1 if i > 24977 else 0))
            record[i] = True
            for id,simValue in res:
                if record[id] is True: continue
                record[id] = True
                f.write(str.format("{0},{1},{2}\n", no, data[id], 1 if id > 24977 else 0))
            no = no + 1

    # new_vec_tfidf_ls = [tfidf[new_vec] for new_vec in new_vecs]  # 将要比较文档转换为tfidf表示方法
    #
    # # 计算要比较的文档与语料库中每篇文档的相似度
    # sims = [index[new_vec_tfidf] for new_vec_tfidf in new_vec_tfidf_ls]
    # print(sims)


"""Defines the GUI IO file for SU2."""
import numpy as np

from pyNastran.gui.vtk_interface import vtkTriangle, vtkQuad

from pyNastran.converters.su2.su2_reader import SU2Reader as SU2
from pyNastran.gui.gui_objects.gui_result import GuiResult
from pyNastran.gui.utils.vtk.vtk_utils import numpy_to_vtk_points

class SU2_IO:
    """supports 2d single zone SU2 models"""
    def __init__(self, gui):
        self.gui = gui

    def get_su2_wildcard_geometry_results_functions(self):
        data = ('SU2',
                'Stanford Unstructured (*.SU2)', self.load_su2_geometry,
                None, None)
        return data

    def load_su2_geometry(self, su2_filename, name='main', plot=True):
        model_name = name
        skip_reading = self.gui._remove_old_geometry(su2_filename)
        if skip_reading:
            return

        model = SU2(log=self.gui.log, debug=False)
        #self.model_type = model.model_type
        ndim, zones = model.read_su2(su2_filename)

        nnodes = 0
        nelements = 0
        nzones = len(zones)
        for i, zone in zones.items():
            nodes, elements, regions = zone
            nnodes += nodes.shape[0]
            for etype, elems in elements.items():
                nsub_elements = elems.shape[0]
                if nsub_elements:
                    nelements += nsub_elements
                #print('min of type = %s' % elems.min())

            if nzones > 1:
                self.log.warning('only reading a single zone')
                break
        assert nnodes > 0, nnodes
        assert nelements > 0, nelements

        self.gui.nnodes = nnodes
        self.gui.nelements = nelements

        self.gui.log.info('nnodes=%s nelements=%s' % (self.gui.nnodes, self.gui.nelements))

        grid = self.gui.grid
        grid.Allocate(self.gui.nelements, 1000)

        self.gui.nid_map = {}

        assert nodes is not None
        nnodes = nodes.shape[0]
        if ndim == 3:
            xmax, ymax, zmax = nodes.max(axis=0)
            xmin, ymin, zmin = nodes.min(axis=0)
            self.gui.log.info('xmax=%s xmin=%s' % (xmax, xmin))
            self.gui.log.info('ymax=%s ymin=%s' % (ymax, ymin))
            self.gui.log.info('zmax=%s zmin=%s' % (zmax, zmin))
            dim_max = max(xmax-xmin, ymax-ymin, zmax-zmin)
        elif ndim == 2:
            xmax, ymax = nodes.max(axis=0)
            xmin, ymin = nodes.min(axis=0)
            self.gui.log.info('xmax=%s xmin=%s' % (xmax, xmin))
            self.gui.log.info('ymax=%s ymin=%s' % (ymax, ymin))
            dim_max = max(xmax-xmin, ymax-ymin)

        self.gui.create_global_axes(dim_max)

        if ndim == 2:
            nodes = np.hstack([nodes, np.zeros((nnodes, 1), dtype=nodes.dtype)])
        #else:
            # ndim=3

        points = numpy_to_vtk_points(nodes)

        #nelements = 0
        #elements = {
            #5 : tris,
            #9 : quads,
        #}
        #print('dict =', elements)
        for etype, elems in elements.items():
            #print(etype, elems)
            if isinstance(elems, list):
                #print('continue')
                continue
            #print(type(elems))
            nsub_elements = elems.shape[0]
            if nsub_elements == 0:
                #print('continue')
                continue
            #print('eid_min =', elems.min())
            assert nsub_elements > 0, nsub_elements
            if etype == 5:
                for eid in range(nsub_elements):
                    elem = vtkTriangle()
                    node_ids = elems[eid, :]
                    elem.GetPointIds().SetId(0, node_ids[0])
                    elem.GetPointIds().SetId(1, node_ids[1])
                    elem.GetPointIds().SetId(2, node_ids[2])
                    #elem.GetCellType() = 5  # vtkTriangle
                    grid.InsertNextCell(5, elem.GetPointIds())
            elif etype == 9:
                for eid in range(nsub_elements):
                    elem = vtkQuad()
                    node_ids = elems[eid, :]
                    elem.GetPointIds().SetId(0, node_ids[0])
                    elem.GetPointIds().SetId(1, node_ids[1])
                    elem.GetPointIds().SetId(2, node_ids[2])
                    elem.GetPointIds().SetId(3, node_ids[3])
                    grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds())
            else:
                raise NotImplementedError(etype)

        grid.SetPoints(points)
        grid.Modified()

        # loadSTLResults - regions/loads
        self.gui.scalar_bar_actor.VisibilityOff()
        self.gui.scalar_bar_actor.Modified()

        cases = {}
        self.gui.isubcase_name_map = {}
        ID = 1

        form, cases, node_ids, element_ids = self._fill_su2_case(cases, ID, nelements, nnodes)
        self.gui.node_ids = node_ids
        self.gui.element_ids = element_ids
        self.gui._finish_results_io2(model_name, form, cases)

    def _fill_su2_case(self, cases, ID, nelements, nnodes):
        """adds the sidebar results"""
        self.gui.isubcase_name_map = {ID : ('SU2', ''), }

        #nelements = elements.shape[0]
        #nnodes = nodes.shape[0]
        icase = 0
        itime = 0
        eids = np.arange(1, nelements + 1, dtype='int32')
        nids = np.arange(1, nnodes + 1, dtype='int32')
        eid_res = GuiResult(ID, header='ElementID', title='ElementID',
                            location='centroid', scalar=eids)
        nid_res = GuiResult(ID, header='NodeID', title='NodeID',
                            location='node', scalar=nids)

        icase = 0
        itime = 0
        cases[icase] = (eid_res, (itime, 'ElementID'))
        cases[icase + 1] = (nid_res, (itime, 'NodeID'))
        form = [
            ('ElementID', icase, []),
            ('NodeID', icase + 1, []),
        ]
        return form, cases, nids, eids


# coding-utf8
from multiprocessing import Manager, Value, cpu_count, Pool
from numpy import array, empty, append, poly1d, polyfit, linalg, zeros, intersect1d
from math import sqrt
from math import tanh

import sys
from time import time
from datetime import timedelta

# import python from parent directory like pointed out here:
# https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder
import os
import sys
import inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0, parentdir)
from PyNite import FEModel3D

import pickle
import gc
gc.disable()

def print_data(text):
	"""
	Print data for debugging
	:param text: Needs a text as string (Do not pass as list)
	"""
	print("Phaenotyp |", text)

# get arguments
directory_blend = sys.argv[1]
path_import = directory_blend + "/Phaenotyp-export_mp.p"
scipy_available = sys.argv[2]
calculation_type = sys.argv[3]

# start timer
start_time = time()

def import_models():
	# get models stored as dict with frame as key
	file = open(path_import, 'rb')
	imported_models = pickle.load(file)
	file.close()

	return imported_models

# run one single fea and save result into feas (multiprocessing manager dict)
def run_fea_pn(scipy_available, calculation_type, feas, model, frame):
	# the variables model, and frame are passed to mp
	# this variables can not be returned with multiprocessing
	# instead of this a dict with multiprocessing.Manager is created
	# the dict feas stores one anlysis for each frame
	# the dict fea is created temporarily in run_fea and is wirrten to feas
	# analyze the model

	# start time
	start_time = time()

	if scipy_available == "True":
		if calculation_type == "first_order":
			model.analyze(check_statics=False, sparse=True)

		elif calculation_type == "first_order_linear":
			model.analyze_linear(check_statics=False, sparse=True)

		else:
			model.analyze_PDelta(check_stability=False, sparse=True)

	if scipy_available == "False":
		if calculation_type == "first_order":
			model.analyze(check_statics=False, sparse=False)

		elif calculation_type == "first_order_linear":
			model.analyze_linear(check_statics=False, sparse=False)

		else:
			model.analyze_PDelta(check_stability=False, sparse=False)

	feas[str(frame)] = model

	# get duration
	elapsed = time() - start_time
	text = calculation_type + " calculation for frame " + str(frame) + " done"
	text +=  " | " + str(timedelta(seconds=elapsed))
	print_data(text)
	sys.stdout.flush()

def run_fea_fd(feas, model, frame):
	# based on:
	# Oliver Natt
	# Physik mit Python
	# Simulationen, Visualisierungen und Animationen von Anfang an
	# 1. Auflage, Springer Spektrum, 2020
	# https://pyph.de/1/1/index.php?name=code&kap=5&pgm=4

	# start time
	start_time = time()

	# amount of dimensions
	dim = 3

	points_array = model[0]
	supports_ids = model[1]
	edges_array = model[2]
	forces_array = model[3]

	# amount of points, edges, supports, verts
	n_points_array = points_array.shape[0]
	n_edges_array = edges_array.shape[0]
	n_supports = len(supports_ids)
	n_verts = n_points_array - n_supports
	n_equation = n_verts * dim

	# create list of indicies
	verts_id = list(set(range(n_points_array)) - set(supports_ids))

	def vector(vertices, edge):
		v_0, v_1 = edges_array[edge]
		if vertices == v_0:
			vec = points_array[v_1] - points_array[v_0]
		else:
			vec = points_array[v_0] - points_array[v_1]
		return vec / linalg.norm(vec)

	# create equation
	model = zeros((n_equation, n_equation))
	for id, edge in enumerate(edges_array):
		for k in intersect1d(edge, verts_id):
			n = verts_id.index(k)
			model[n * dim:(n + 1) * dim, id] = vector(k, id)

	# Löse das Gleichungssystem A @ F = -forces_array nach den Kräften F.
	b = -forces_array[verts_id].reshape(-1)
	F = linalg.solve(model, b)

	# Berechne die äußeren Kräfte.
	for id, edge in enumerate(edges_array):
		for k in intersect1d(edge, supports_ids):
			forces_array[k] -= F[id] * vector(k, id)

	feas[str(frame)] = F

	# get duration
	elapsed = time() - start_time
	text = calculation_type + " calculation for frame " + str(frame) + " done"
	text +=  " | " + str(timedelta(seconds=elapsed))
	print_data(text)
	sys.stdout.flush()

def mp_pool():
	global scipy_available

	manager = Manager() # needed for mp
	feas = manager.dict() # is saving all calculations by frame

	cores = cpu_count()
	'''
	text = "rendering with " + str(cores) + " cores."
	print_data(text)
	'''

	pool = Pool(processes=cores)

	# for PyNite
	if calculation_type != "force_distribution":
		for frame, model in imported_models.items():
			pool.apply_async(run_fea_pn, args=(scipy_available, calculation_type, feas, model, frame,))

	# for force distribution
	else:
		for frame, model in imported_models.items():
			pool.apply_async(run_fea_fd, args=(feas, model, frame,))

	pool.close()
	pool.join()

	return feas

def export_models():
	# export back to blender
	path_export = directory_blend + "/Phaenotyp-return_mp.p"
	file = open(path_export, 'wb')
	pickle.dump(dict(feas), file) # use dict() to convert mp_dict to dict
	file.close()

if __name__ == "__main__":
	imported_models = import_models()
	feas = mp_pool()
	export_models()
	# give feedback to user
	end_time = time()

	# print only frames to make progress.http work correctly
	'''
	elapsed_time = end_time - start_time
	text = "time elapsed: " + str(elapsed_time) + " s"
	print_data(text)
	'''

	# exit
	sys.exit()


import unittest

"""
方式二：定义函数
"""

def suite():
    test_dir = '/'  # ./test_case为根目录下的文件夹路径
    discover = unittest.defaultTestLoader.discover(test_dir, pattern='test_*.py')
    return discover

if __name__ == '__main__':
    runner = unittest.TextTestRunner(verbosity=2)
    runner.run(suite())


class testEnvironment:
    """
    class contains all arguments needed by test and/or conversion functions:
    source paths (model, datachecker etc)
    output paths (logs, layers)
    qgis layer names
    qgis generated layers information
    qgis groups
    If relevant: 3di variables (result, rain scenario as df)
    conversion variables for model state conversions
    """

    def __init__(
        self,
        source_paths_dict,
        output_vars_dict=None,
        layers=None,
        conversion_vars=None,
        selected_tests=None,
        group_structure=None,
    ):
        self.src_paths = source_paths_dict
        self.output_vars = output_vars_dict
        # Only for use with plugin: interaction with qgis
        self.layers = layers
        self.threedi_vars = None
        self.conversion_vars = conversion_vars
        # Only for sqlite tests
        self.selected_tests = selected_tests
        self.tasks = []
        self.group_structure = group_structure


from typing import List
import pytest

from wagtail.core.models import Page
from modules.publications.models import PublicationPage, PublicationIndexPage
from wagtail.tests.utils.form_data import rich_text, streamfield, nested_form_data
from wagtail.core.blocks.stream_block import StreamValue
import wagtail.core.blocks.stream_block
from wagtail.core.blocks import RichTextBlock

pytestmark = pytest.mark.django_db

def _create_publication_page(title: str, parent: Page) -> PublicationPage:
    """Abstracting this allows us to test more scenarios than just passing the
    fixture around.

    Args:
        title (str): The page title
        parent (Page): A page to attach our post to

    Returns:
        PublicationPage: Description
    """
    p = PublicationPage()
    p.title = title
    parent.add_child(instance=p)
    p.save_revision().publish()
    return p

def _create_publication_index_page(title: str, parent: Page) -> PublicationIndexPage:
    """Abstracting this allows us to test more scenarios than just passing the
    fixture around.

    Args:
        title (str): The page title
        parent (Page): A page to attach our post index page to

    Returns:
        PublicationIndexPage: Description
    """
    p = PublicationIndexPage()
    p.title = title
    parent.add_child(instance=p)
    p.save_revision().publish()
    return p

@pytest.fixture(scope="function")
def publication_index_page(home_page) -> PublicationIndexPage:
    p = _create_publication_index_page("Test Publication Index Page", home_page)
    return p

@pytest.fixture(scope="function")
def publication_page(publication_index_page) -> PublicationPage:
    p = _create_publication_page("Test Publication Page", publication_index_page)
    return p

@pytest.fixture(scope="function")
def publication_pages(publication_index_page) -> List[PublicationPage]:
    """Fixture providing 10 PublicationPage objects attached to publication_index_page"""
    rv = []
    for _ in range(0, 10):
        p = _create_publication_page(
            f"Test Publication Page {_}", publication_index_page
        )
        rv.append(p)
    return rv


'''

Histogram Backprojection:

In this blog, we will discuss Histogram Backprojection, a technique that is used for image segmentation or finding objects of interest in an image.
It was proposed by Michael J. Swain, Dana H. Ballard in their paper Indexing via color histograms, Third international conference on computer vision,1990.
This was one of the first works that use color to address the classic problem of Classification and Localisation in computer vision.

To understand this technique, knowledge of histograms (particularly 2-D histograms) is a must.
If you haven’t encountered 2-d histograms yet, I suggest you to read What is a 2-D histogram?     '''

# importing libraies
import cv2
import numpy as np
import matplotlib.pyplot as plt

# resize the image
height_Img = 700
width_Img = 500

# importing Images
image = cv2.imread("C:\\Users\\Rafiun Nesha\\Desktop\\images\\goalkeeper.jpg")
main_image = cv2.resize(image, (height_Img, width_Img))
main_img_hsv = cv2.cvtColor(main_image, cv2.COLOR_BGR2HSV)

roi_img = cv2.imread("C:\\Users\\Rafiun Nesha\\Desktop\\images\\pitch_ground.jpg")
roi = cv2.resize(roi_img, (height_Img, width_Img))
roi_hsv = cv2.cvtColor(roi_img, cv2.COLOR_BGR2HSV)

hue, saturation, value = cv2.split(roi_hsv)

# Draw a histogram of roi
roi_hist = cv2.calcHist([roi_hsv],[0,1], None, [180,256], [0,180,0,256])
mask = cv2.calcBackProject([main_img_hsv], [0, 1], roi_hist, [0, 180, 0, 256], 1)

# add filter for removing noise
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))
mask = cv2.filter2D(mask, -1, kernel)
_, mask = cv2.threshold(mask, 100, 255, cv2.THRESH_BINARY)

# applying bitwise for background
mask = cv2.merge((mask, mask, mask))
result = cv2.bitwise_and(main_image, mask)

cv2.imshow("Mask", mask)
cv2.imshow("Main_image", main_image)
cv2.imshow("Result", result)
cv2.imshow("ROI", roi)
cv2.imshow("ROI_hsv", roi_hsv)

cv2.waitKey(0)
cv2.destroyAllWindows()


"""
Фикстуры вида пайтест, которые можно передавать в методы и функции в качестве параметров
https://stepik.org/lesson/237257/step/3?unit=209645

браузеры закрывает встроенная в пайтест фикстура - сборщик мусора, но лучше закрывать браузер явно.
"""
import pytest
from selenium import webdriver

link = "http://selenium1py.pythonanywhere.com/"

@pytest.fixture
def browser():
    print("\nЗапускаем браузер")
    browser = webdriver.Chrome()
    return browser

class TestMainPage1:

    def test_guest_should_see_login_link(self, browser):
        browser.get(link)
        browser.find_element_by_css_selector("#login_link")

    def test_guest_should_see_basket_link_on_the_main_page(self, browser):
        browser.get(link)
        browser.find_element_by_css_selector(".basket-mini .btn-group > a")


sbt_libs="""
libraryDependencies += "org.apache.spark" % "spark-core_2.10" % "1.4.0"
libraryDependencies += "org.apache.spark" % "spark-mllib_2.10" % "1.4.0"
libraryDependencies += "org.apache.spark" % "spark-streaming_2.10" % "1.4.0"
libraryDependencies += "org.apache.spark" % "spark-streaming-kafka_2.10" % "1.4.0"
libraryDependencies += "org.mongodb" %% "casbah" % "3.0.0"
libraryDependencies += "org.jblas" % "jblas" % "1.2.4"

"""

sbt_libs=sbt_libs.split("\n")

import listUtil

sbt_libs=listUtil.remove_none(sbt_libs)

# from  strUtil.strUtil  import one_slash_to_two
import    strUtil.strUtil
# import strUtil
# strUtil.
# import StrU
def parse_lib(lib_str):
    # lib_str
    lib_str=strUtil.strUtil.front_del_str(lib_str,"libraryDependencies += ")
    lib_str:str
    sps=lib_str.split(" % ")
    end_idx=-1
    groupId=sps[0]
    groupId=groupId[1:end_idx]
    artifactId=sps[1]
    artifactId=artifactId[1:end_idx]
    try:
        version=sps[2]
        version=version[1:end_idx]
        # return groupId,artifactId,version
        return f"""
        <dependency>
                <groupId>{groupId}</groupId>
                <artifactId>{artifactId}</artifactId>
                <version>{version}</version>
            </dependency>"""
    except Exception  as e:
        print("error",e)
        print("error lib_str",lib_str)

for i in  sbt_libs:
    print(parse_lib(i))


import asyncio
from typing import Optional, Protocol

from .abstract import AbstractService
from .container import ServiceContainerMixin
from .exceptions import UnhealthyException
from .tasks import TasksMixin
from .bus import ServiceBusMixin, ServiceBus

class WithServiceBus(Protocol):
    def set_service_bus(self, bus: ServiceBus): ...

class Service(ServiceBusMixin, ServiceContainerMixin, TasksMixin, AbstractService):
    """Base service class.

    Your services should be inherited from this class.

    Service class provides a basic asynchronous service lifecycle methods.
    You should extend it for your needs.
    """
    _monitoring_task: Optional[asyncio.Task] = None
    #: interval in seconds to sleep between healthcheck runs
    _monitoring_interval: float = .1

    def __init__(self, *, loop=None, bus: ServiceBus = None, monitoring_interval: float = .1):
        self._loop = loop
        self._monitoring_interval = monitoring_interval
        super().__init__()
        self.set_service_bus(bus)

    async def start(self):
        """Start service.

        Set `running` flag to `True`, start service tasks, nested services
        and create monitoring task.

        You can override this method in your service implementation to apply custom
        start logic. But don't forget to invoke super implementation.
        """
        self.log.debug("Starting")
        self.running = True
        self.log.debug("Starting service tasks...")
        await self._start_service_tasks()
        try:
            self.log.debug("Starting nested services...")
            await self._start_nested_services()
        except Exception:
            self.log.exception("Failed to start nested service")
            self.running = False
            self.should_stop = True
            await self._stop_service_tasks()
            raise
        self._monitoring_task = self.loop.create_task(self.monitoring_task(),
                                                      name=f"{self.name}.monitoring_task")
        await self.start_bus_reader()
        self.log.debug("Service was started")

    async def stop(self):
        """Stop service.

        Set `should_stop` flag to `True`, `running` to `False` and start shutdown sequence.

        Nested services will be stopped first, service tasks will be cancelled than.

        You can override this method in your service implementation to apply custom
        start logic. But don't forget to invoke super implementation.
        """
        self.should_stop = True
        self.running = False
        await self.stop_bus_reader()
        if self._monitoring_task:
            self._monitoring_task.cancel()
        self.log.debug("Stopping nested services...")
        await self._stop_nested_services()
        self.log.debug("Stopping service tasks...")
        await self._stop_service_tasks()
        self.log.debug("Service was stopped")

    async def monitoring_task(self):
        """Monitoring task.

        Started with a service. Run healthcheck periodically and force service
        to stop if it failed.
        """
        while not self.should_stop:
            try:
                await self.healthcheck()
            except UnhealthyException:
                self.log.exception("Healthcheck failed with exception")
                break
            except Exception:  # noqa
                self.log.exception("Service healthcheck failed with unexpected exception")
                break
            await asyncio.sleep(self._monitoring_interval)
        # terminate service on exit
        if not self.should_stop:
            await self.stop()

    def nested_service_pre_start(self, service: WithServiceBus):
        if not self.bus:  # pragma: no cover
            raise RuntimeError("Service bus is not initialized")
        service.set_service_bus(self.bus)
        return service


import logging

from rest_framework import serializers

from helium.feed.models import ExternalCalendar
from helium.feed.services import icalexternalcalendarservice
from helium.feed.services.icalexternalcalendarservice import HeliumICalError

__author__ = "Alex Laird"
__copyright__ = "Copyright 2021, Helium Edu"
__version__ = "1.4.46"

logger = logging.getLogger(__name__)

class ExternalCalendarSerializer(serializers.ModelSerializer):
    class Meta:
        model = ExternalCalendar
        fields = ('id', 'title', 'url', 'color', 'shown_on_calendar', 'user',)
        read_only_fields = ('user',)

    def validate(self, attrs):
        """
        Ensure a valid ICAL URL is given. If not, disable the calendar.

        :param attrs: the data to be saved
        :return: the validated data
        """
        url = attrs.get('url', None)
        if not url and self.instance:
            url = self.instance.url

        if url and (not self.instance or (self.instance and url != self.instance.url)):
            try:
                icalexternalcalendarservice.validate_url(url)
            except HeliumICalError:
                logger.info(f"Unable to validate external ICAL URL {url}, so disabling the calendar.")

                if self.instance:
                    self.instance.shown_on_calendar = False
                    self.instance.save()

                attrs['shown_on_calendar'] = False

        return attrs


import numpy as np
from sklearn.cluster import KMeans
from scipy.spatial.distance import cdist

def run_kmeans_clustering(data, max_clusters):
    """
    This function will run kmeans clustering up to the max_profile number, and select an optimal cluster number.
    The function removes any observations assigned to independant cluster's; considering them outliers
    inhibitive to the intention of the model.

    It will then return a kmeans model fitted to the training data.

    Args:
        - data: df, training data
    Returns:
        - clusterer: fitted KMeans model
    """
    # perform an initial check to ensure we have more obs than possible clusters
    n_rows = len(data)
    if n_rows < max_clusters:
        max_clusters = n_rows
    else:
        max_clusters = max_clusters

    # create containers for pertinent information
    cluster_number = []
    distortions = []
    rocs = []
    cluster_rocs = []
    rate_of_change = 0

    # iterate through the number of clusters
    n_clusters = 0

    for i in range(max_clusters):
        # set min_obs_threshold, min observations to retain a cluster
        min_obs_threshold = len(data) * 0.05
        _pass = False

        # increment the number of clusters and fit
        n_clusters += 1
        clusterer = KMeans(n_clusters=n_clusters)
        clusters = clusterer.fit_predict(data)

        # check if our clusters have <= min_obs_threshold observation, if so this is inviable
        assignment_list = clusters.tolist()
        cluster_instances = [0] * n_clusters
        for x in assignment_list:
            cluster_instances[x] += 1

        for ind in range(n_clusters):
            # if the cluster has < min_obs_threshold, discard the cluster and the observations assigned to it
            if cluster_instances[ind] <= max(min_obs_threshold, 1):
                data = data[[member != ind for member in assignment_list]]
                assignment_list = (filter((ind).__ne__, assignment_list))
                _pass = True
        # if _pass has been switched to true, we must re-iterate with the same cluster number,
        # albeit with discarded observations
        if _pass:
            n_clusters -= 1
            continue

        # calculate cluster centers, and distortion
        centers = []
        for cluster in set(clusters):
            center = np.mean(data[clusters == cluster])
            centers.append(center)
        # calculate the distortion
        distortion_new = np.sum(
            np.min(cdist(data, centers, 'euclidean'), axis=1) / data.shape[0]) / n_clusters

        # store information to ID optimal cluster
        if n_clusters >= 2:
            rate_of_change = distortion_new - distortion
            clust_rate_of_change = n_clusters / (n_clusters - 1)
            distortion = distortion_new
        if n_clusters == 1:
            distortion = distortion_new
            clust_rate_of_change = 0

        # append to the containers
        cluster_number.append(n_clusters)
        distortions.append(distortion)
        rocs.append(rate_of_change)
        cluster_rocs.append(clust_rate_of_change)

    # calculate the strength of information gain
    strengths = np.divide(rocs, cluster_rocs)
    strengths = np.nan_to_num(strengths, 0)

    # keep either then optimal cluster based on strength, or max_clusters
    if min(strengths) < 0:
        optimal_clusters = min([index for index, strength in enumerate(strengths) if strength < 0]) + 1
    else:
        optimal_clusters = max_clusters

    # fit a final model on the optimal_cluster and set it as the number of profiles
    clusterer = KMeans(n_clusters=optimal_clusters)
    clusterer.fit(data)
    n_clusters = optimal_clusters

    return clusterer, n_clusters


import json

from io import StringIO

from django.test.utils import override_settings
from django.core.management import call_command
from django.test import TestCase

from akvo.core_forms.models import Forms
from akvo.core_forms.serializers.form import (
    FormDefinitionSerializer
)

@override_settings(USE_TZ=False)
class FormSeederTestCase(TestCase):
    def call_command(self, *args, **kwargs):
        out = StringIO()
        call_command(
            "form_seeder",
            *args,
            stdout=out,
            stderr=StringIO(),
            **kwargs,
        )
        return out.getvalue()

    def test_call_command(self):
        self.maxDiff = None
        forms = Forms.objects.all().delete()
        json_forms = [
            "IDH Form",
        ]

        # RUN SEED NEW FORM
        output = self.call_command("--file=./source/forms/1693403249322.json")
        output = list(filter(lambda x: len(x), output.split("\n")))
        forms = Forms.objects.all()
        self.assertEqual(forms.count(), 1)
        for form in forms:
            self.assertIn(
                f"Form Created | {form.name} V{form.version}", output
            )
            self.assertIn(form.name, json_forms)

        # RUN UPDATE EXISTING FORM
        output = self.call_command("--file=./source/forms/1693403249322.json")
        output = list(filter(lambda x: len(x), output.split("\n")))
        forms = Forms.objects.all()
        # form_ids = [form.id for form in forms]
        for form in forms:
            if form.version == 2:
                self.assertIn(
                    f"Form Updated | {form.name} V{form.version}", output
                )
            # FOR NON PRODUCTION FORM
            if form.version == 1:
                self.assertIn(
                    f"Form Created | {form.name} V{form.version}", output
                )
            self.assertIn(form.name, json_forms)

    def test_get_form_after_seed_return_expected_form_definition(self):
        self.call_command("--file=./source/forms/1693403249322.json")
        form = Forms.objects.filter(id=1693403249322).first()
        form_serializer = FormDefinitionSerializer(instance=form)
        form_definition = form_serializer.data
        # Load expected form definition from JSON
        with open('./source/static/expected_form_definition.json', 'r') as f:
            expected_form_definition = json.load(f)
        self.assertEqual(form_definition, expected_form_definition)


import random
import string
from words import words

def get_valid_word(words):
    word = random.choice(words)  # randomly choses word from word list

    while '-' in word or ' ' in word:
        word = random.choice(words)

    return word.upper()

def hangman():
    word = get_valid_word(words)
    word_letters = set(word)  # letters in the word
    alphabet = set(string.ascii_uppercase)
    used_letters = set()  # what the user has guessed
    hangman = ''  # string displaying how many mistakes are left
    hangman_progress = 0
    hangman_list = ['|\n',
                    ' \\\n',
                    '  O\n',
                    ' /|\\\n',
                    '/ | \\\n',
                    '  |\n',
                    ' / \\\n',
                    '/   \\\n']

    while len(word_letters) > 0 and hangman_progress < len(hangman_list):
        # letters used
        print("You have used these letters: ", " ".join(used_letters))
        print(hangman)

        word_list = [
            letter if letter in used_letters else '_' for letter in word]
        print(" ".join(word_list))

        # getting user input
        user_letter = input("Guess a letter: ").upper()

        if user_letter in alphabet - used_letters:
            used_letters.add(user_letter)

            if user_letter in word_letters:
                word_letters.remove(user_letter)
            else:
                hangman += hangman_list[hangman_progress]
                hangman_progress += 1

        elif user_letter in used_letters:
            print("You already entered that letter. Please enter another letter.")

        else:
            print("Invalid character. Please enter a letter. ")

    if hangman_progress == len(hangman_list):
        print(hangman)
        print(f"You lose. The word was {word}. Try again!")
    else:
        print(f"Congrats, you guessed the word {word}!")

hangman()


# 1. 게임에 필요한 라이브러리[파일] 불러오기[import]
import pygame # 파이게임 파일 불러오기 [ 임포트 ]
from pygame.locals import QUIT, Rect, KEYDOWN, K_UP, K_LEFT, K_RIGHT, K_DOWN

import random # 랜덤 파일 불러오기 [ 임포트 ]
import sys # 시스템 파일 불러오기 [ 임포트 ]

# 2. 게임에 필요한 기본설정
pygame.init() # 파이게임 초기값
화면 = pygame.display.set_mode((1000,1000)) # 화면구성
프레임 = pygame.time.Clock() # 시간체크
# Frame Per Second : FPS : 초당 프레임 [정지사진] 수

음식 = [ ] # 여러개 음식을 저장할 리스트
뱀 = [ ] # 어러개 뱀 꼬리를 저장할 리스트
(가로,세로) = (50,50) # 가로길이 세로길이 # 튜플선언

# 3. 함수 만들기
    # i) 음식 함수 : 뱀이 음식을 먹었을 때 새로운 음식추가 혹은 게임시작시 음식 추가
def 음식생성() : # 임의의 장소에 음식을 배치하는 함수 [코드묶음] 선언
    while True : # 무한반복
        위치 = (random.randint(0, 가로-1), random.randint(0, 세로-1))
                    # 0 ~~ 49 의 난수 생성
        if 위치 in 음식 or 위치 in 뱀 : # 해당 위치에 음식이나 뱀이 있으면
            continue # while문 다시 실행

        else: # 해당 위치에 음식이나 뱀이 없으면
            음식.append(위치) # 음식 리스트에 해당 위치 추가
            break # while 끝내기

    # ii) 음식 이동 함수
def 음식이동(위치) : # 음식을 다른 위치로 이동
    임의변수 = 음식.index(위치) # 음식리스트에서 위치에 해당하는 음식 찾기
    del 음식[임의변수] # 찾은 음식 삭제
    음식생성() # 음식 생성 함수 호출

    # iii) 그리기 함수
def 그리기(점수판, 게임종료) :
    화면.fill((0,0,0)) # 검정색으로 칠하기

    # 음식 그리기
    for food in 음식 :
        pygame.draw.ellipse(화면, (0,255,0), Rect(food[0]*20, food[1]*20, 20, 20))
                # 타원그리기 ( 화면이름 , (킬라[RGB]) , 타원 (x축, y축, 가로크기, 세로크기)
    # 뱀 그리기
    for body in 뱀 :
        pygame.draw.rect(화면, (0, 255, 255), Rect(body[0]*20, body[1]*20, 20, 20))

    # 점수 그리기
    if 점수판 !=None :
        화면.blit(점수판, (10, 10)) # x좌표 y좌표에 표기

    # 게임 종료 메세지
    if 게임종료 : # 게임종료메세지가 내용이 있으면
        게임종료메세지 = my글꼴.render("게임 종료 [ 획득점수는 : " + str(점수) + "]", True, (255,255,255))
        화면.blit(게임종료메세지, (300,500))

    pygame.display.update() # 화면 업데이트

# 4. 게임실행

my글꼴 = pygame.font.SysFont("malgungothic", 30) # 글꼴, 글자크기

키 = K_DOWN # 아래키
게임종료 = False # 게임종료 스위치
뱀.append( ( int(세로/2), int(가로/2) ) ) # 뱀을 화면 가운데 위치
점수 = 0
게임종료메세지 = 0
for i in range(30) : # 30개의 음식 생성
    음식생성()

while True : # 계속 반복하기

    # 키보드 동작하기
    for 동작 in pygame.event.get() : # 파이게임 이벤트[동작] 있을경우
        if 동작.type == QUIT : # 이벤트가 종료하면
            pygame.quit() # 파이게임 종료
            sys.exit() # 시스템[모든코드] 종료
        elif 동작.type == KEYDOWN : # 키를 눌렀을 때
            키 = 동작.key

    # 키를 눌렀을때
    if not 게임종료 : # 게임종료가 False 이면
        if 키 == K_LEFT :
            머리 = (뱀[0][0]-1 , 뱀[0][1])
        elif 키 == K_RIGHT :
            머리 = (뱀[0][0] +1, 뱀[0][1])
        elif 키 == K_UP:
            머리 = (뱀[0][0] , 뱀[0][1] -1)
        elif 키 == K_DOWN :
            머리 = (뱀[0][0] , 뱀[0][1] +1)

        # 뱀 몸에 닿았을 때 혹은 화면 밖으로 나갔을 때 게임 종료
        if 머리 in 뱀 or 머리[0] < 0 or 머리[0] >= 세로 or 머리[1] < 0 or 머리[1] >= 가로:

            게임종료 = True

        # 뱀 머리 추가
        뱀.insert(0, 머리)

        # 음식을 먹었을때 꼬리 추가 아니면 추가 X
        if 머리 in 음식 : # 만약에 머리가 음식에 닿았을때
            음식이동(머리) # 새로운 음식 추가
            # 1점 추가
            점수 += 1

        else : # 음식을 못 먹었으면
            뱀.pop() # 리스트 내 가장 뒤에있는 항목 제거

        점수판 = my글꼴.render("현재 먹은 횟수 : " + str(점수), True, (255,255,255))

    그리기( 점수판, 게임종료)

    프레임.tick(5)


import pytest

from quart import jsonify, Quart, request, Response, session
from quart.datastructures import CIMultiDict
from quart.exceptions import BadRequest
from quart.testing import make_test_headers_and_path, QuartClient as Client

@pytest.mark.asyncio
async def test_methods() -> None:
    app = Quart(__name__)

    methods = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT', 'TRACE']

    @app.route('/', methods=methods)
    async def echo() -> str:
        return request.method

    client = Client(app)

    for method in methods:
        func = getattr(client, method.lower())
        response = await func('/')
        assert method in (await response.get_data(raw=False))

def test_build_headers_and_path() -> None:
    headers, path = make_test_headers_and_path(Quart(__name__), '/path', None, {'a': 'b'})
    assert path == '/path?a=b'
    assert headers['Remote-Addr'] == '127.0.0.1'
    assert headers['User-Agent'] == 'Quart'
    assert headers['host'] == 'localhost'

@pytest.mark.parametrize(
    'headers, expected',
    [
        (
            None,
            CIMultiDict({'Remote-Addr': '127.0.0.1', 'User-Agent': 'Quart',  'host': 'localhost'}),
        ),
        (
            CIMultiDict({'Remote-Addr': '1.2.3.4', 'User-Agent': 'Quarty', 'host': 'quart.com'}),
            CIMultiDict({'Remote-Addr': '1.2.3.4', 'User-Agent': 'Quarty', 'host': 'quart.com'}),
        ),
    ],
)
def test_build_headers_and_path_headers_defaults(
        headers: CIMultiDict, expected: CIMultiDict,
) -> None:
    result, _ = make_test_headers_and_path(Quart(__name__), '/path', headers)
    assert result == expected

@pytest.mark.asyncio
async def test_json() -> None:
    app = Quart(__name__)

    @app.route('/', methods=['POST'])
    async def echo() -> Response:
        data = await request.get_json()
        return jsonify(data)

    client = Client(app)
    response = await client.post('/', json={'a': 'b'})
    assert (await response.get_json()) == {'a': 'b'}

@pytest.mark.asyncio
async def test_form() -> None:
    app = Quart(__name__)

    @app.route('/', methods=['POST'])
    async def echo() -> Response:
        data = await request.form
        return jsonify(**data)

    client = Client(app)
    response = await client.post('/', form={'a': 'b'})
    assert (await response.get_json()) == {'a': 'b'}

@pytest.mark.asyncio
async def test_data() -> None:
    app = Quart(__name__)

    @app.route('/', methods=['POST'])
    async def echo() -> Response:
        data = await request.get_data(True)
        return data

    client = Client(app)
    headers = {'Content-Type': 'application/octet-stream'}
    response = await client.post('/', data=b'ABCDEFG', headers=headers)
    assert (await response.get_data(True)) == b'ABCDEFG'

@pytest.mark.asyncio
async def test_query_string() -> None:
    app = Quart(__name__)

    @app.route('/', methods=['GET'])
    async def echo() -> Response:
        data = request.args
        return jsonify(**data)

    client = Client(app)
    response = await client.get('/', query_string={'a': 'b'})
    assert (await response.get_json()) == {'a': 'b'}

@pytest.mark.asyncio
async def test_cookie_jar() -> None:
    app = Quart(__name__)
    app.secret_key = 'secret'

    @app.route('/', methods=['GET'])
    async def echo() -> Response:
        foo = session.get('foo')
        bar = request.cookies.get('bar')
        session['foo'] = 'bar'
        response = jsonify({'foo': foo, 'bar': bar})
        response.set_cookie('bar', 'foo')
        return response

    client = Client(app)
    response = await client.get('/')
    assert (await response.get_json()) == {'foo': None, 'bar': None}
    response = await client.get('/')
    assert (await response.get_json()) == {'foo': 'bar', 'bar': 'foo'}

@pytest.mark.asyncio
async def test_set_cookie() -> None:
    app = Quart(__name__)

    @app.route('/', methods=['GET'])
    async def echo() -> Response:
        return jsonify({'foo': request.cookies.get('foo')})

    client = Client(app)
    client.set_cookie('foo', 'bar')
    response = await client.get('/')
    assert (await response.get_json()) == {'foo': 'bar'}

@pytest.mark.asyncio
async def test_websocket_bad_request() -> None:
    app = Quart(__name__)

    @app.route('/')
    async def index() -> str:
        return ''

    test_client = app.test_client()
    with pytest.raises(BadRequest):
        with test_client.websocket('/'):
            pass


class BaseGeometry:

    def area(self):
        """A public class method
        Args:
          it takes no arguments
        Returns:
           the try command raises an exception when an error is encountered
        """
        try:
           ara=self.w*self.h
           return ara
        except Exception:
          raise Exception ("area() is not implemented")

    def integer_validator(self,name,value):
          """The public class method
          Args:
              name(str):first parameter
              value(int):second parameter
          Returns:
              the try command raises a TypeError when the value argument is not an integer
              the try command raises a ValueError when the value argument is less than 0
          """

          if (type(value)) != int:
            raise TypeError ("{} must be an integer".format(name))
          elif value<=0:
            raise ValueError ("{} must be greater than 0".format(name))

bg=BaseGeometry()
bg.integer_validator("my_int",12)
bg.integer_validator("width",89)

try:
   bg.integer_validator("name","john")
except Exception as e:
   print("[{}] {}".format(e.__class__.__name__,e))

try:
   bg.integer_validator("age",0)
except Exception as e:
   print("[{}] {}".format(e.__class__.__name__,e))

try:
   bg.integer_validator("distance",-4)
except Exception as e:
   print("[{}] {}".format(e.__class__.__name__,e))


class Solution:
    def slowestKey(
            self,
            releaseTimes: List[int],
            keysPressed: str) -> str:  # 8.37% 25.65%
        max_d = releaseTimes[0]
        out = [keysPressed[0]]
        for i in range(1, len(releaseTimes)):
            cur = releaseTimes[i] - releaseTimes[i-1]
            if cur > max_d:
                max_d = cur
                out = [keysPressed[i]]
            elif cur == max_d:
                out.append(keysPressed[i])
        out.sort()
        return out[-1]

    def slowestKey_refactored(
            self,
            releaseTimes: List[int],
            keysPressed: str) -> str:  # 8.37% 55.83%
        max_d = releaseTimes[0]
        out = 0
        for i in range(1, len(releaseTimes)):
            cur = releaseTimes[i] - releaseTimes[i-1]
            if cur > max_d:
                max_d = cur
                out = i
            elif cur == max_d:
                if keysPressed[out] < keysPressed[i]:
                    out = i
        return keysPressed[out]

    def slowestKey_best_speed(self, rel: List[int], key: str) -> str:  # ~7 ~80
        ind = 0  # 12.11% 5.25% with releaseTimes keysPressed variable names
        max_diff = rel[0]
        for i in range(1, len(rel)):
            diff = rel[i] - rel[i-1]
            if diff > max_diff:
                max_diff = diff
                ind = i
            elif diff == max_diff:
                if key[i] > key[ind]:
                    ind = i
        return key[ind]

1600 - 1699/1629 - Slowest Key/1629 - Slowest Key.py


import json
from pathlib import Path

from deepdiff import DeepDiff

from harvester import bucket_name
from harvester.extract import main
from harvester.utils.json import open_json
from harvester.utils.s3 import get_s3_object

TEST_DIR = Path(__file__).parents[1]

def test_extract_dcatus(get_dcatus_job, create_client):
    """download dcat-us json file and store result in s3 bucket.
    get_dcatus_job (dict)           :   fixture containing job data
    create_client  (boto3.client)   :   S3 client object
    """

    S3_client = create_client
    S3_client.create_bucket(Bucket=bucket_name)

    res = main(get_dcatus_job, S3_client)
    assert res

    # check if the s3 record matches the test record
    dcatus_json = TEST_DIR / "harvest-sources" / "dcatus.json"
    dcatus_record_obj = open_json(dcatus_json)["dataset"][0]

    s3_record_obj = get_s3_object(S3_client, "test-bucket", res["s3_paths"][0])
    s3_record_obj = s3_record_obj["Body"].read().decode("utf-8")
    s3_record_obj = json.loads(s3_record_obj)

    assert len(DeepDiff(dcatus_record_obj, s3_record_obj)) == 0

def test_extract_bad_url(get_bad_url, create_client):
    """attempt to download a bad url.
    get_bad_url (dict)              :   fixture containing job data with bad url
    create_client  (boto3.client)   :   S3 client object
    """

    S3_client = create_client
    S3_client.create_bucket(Bucket=bucket_name)

    assert str(main(get_bad_url, S3_client)) == "non-200 status code"

    paginator = S3_client.get_paginator("list_objects_v2")
    page_iterator = paginator.paginate(Bucket="test-bucket")

    for page in page_iterator:
        assert page["KeyCount"] == 0

def test_extract_bad_json(get_bad_json, create_client):
    """attempt to download a bad url.
    get_bad_json (dict)             :   fixture containing job data with bad json
    create_client  (boto3.client)   :   S3 client object
    """

    S3_client = create_client
    S3_client.create_bucket(Bucket=bucket_name)

    error = (
        "Expecting property name enclosed "
        "in double quotes: line 4 column 1 (char 25)"
    )
    assert str(main(get_bad_json, S3_client)) == error

def test_extract_no_dataset_key(get_no_dataset_key_dcatus_json, create_client):
    """attempt to download a invalid dcatus catalog.
    get_no_dataset_key_dcatus_json (dict)
        :   fixture containing dcatus with no 'dataset' key
    create_client  (boto3.client)   :   S3 client object
    """

    S3_client = create_client
    S3_client.create_bucket(Bucket=bucket_name)

    assert main(get_no_dataset_key_dcatus_json, S3_client) == "invalid dcatus catalog"

def test_bad_s3_client(create_bad_client):
    """attempt to create an S3 client using a bad url endpoint
    create_bad_client (boto3.client)    : S3 client object.
    """

    S3_client = create_bad_client

    assert str(S3_client) == "Invalid endpoint: garbage"


import os
from os import system, startfile
import graphviz

class Grafica:
    def __init__(self, matriz, frecuencia):
        self.matriz = matriz
        self.frecuencia = frecuencia
        self.create_graph()

    def create_graph(self):
        img_route = "Matriz de frecuencia" + str(self.frecuencia)


import os, os.path
import win32com.client

xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename=r"C:\Users\Nuwan\Documents\excelsheet.xlsm", ReadOnly=1)

directory = r"E:\Google Drive - AssetOwl\Test PCR data - rentfindinspector"

for root, subdirectories, files in os.walk(directory):

    has_converted = False
    has_word = False

    for file in files:
        if file.endswith(".docx"):
            has_word = True
        elif file.endswith(".xlsx"):
            has_converted = True

    if has_word and not has_converted:
        xl.Application.Run("excelsheet.xlsm!Module1.PDF_To_Excel", root, root)
        print("Converted folder : ", root)

xl.Application.Quit()
del xl


class Solution:
    def exist(self, board: List[List[str]], word: str) -> bool:
        def dfs(board,x,y,word,i):
            if x < 0 or x >= len(board) or y < 0 or y >= len(board[0]) or i >= len(word):
                return False

            if board[x][y] == word[i]:
                temp = board[x][y]
                board[x][y] = ""

                if i == len(word)-1:
                    return True
                b = dfs(board,x+1,y,word,i+1) or dfs(board,x-1,y,word,i+1) or dfs(board,x,y+1,word,i+1) or dfs(board,x,y-1,word,i+1)
                board[x][y] = temp
                return b

        for x in range(len(board)):
            for y in range(len(board[0])):
                z = board
                if board[x][y] == word[0] and dfs(board,x,y,word,0):
                    return True
        return False


import os
import json
from tabulate import tabulate
from colorama import Fore, Style
from datetime import datetime
import re

#add other values here if you want to change up the scoring
def calculate_score(data):
    return data['kilojoules']

def extract_time_from_name(name):
    duration_match = re.search("(\d+)\smin", name)
    return int(duration_match.group(1)) if duration_match else None

def format_ride_time(ride_time):
    datetime_object = datetime.strptime(ride_time, '%Y-%m-%dT%H:%M:%SZ')
    formatted_string = datetime_object.strftime('%Y-%B-%d')
    return formatted_string

def categorize_ride(data):
    duration = data['elapsed_time'] / 60  # duration in minutes
    expected_duration = extract_time_from_name(data['name'])
    # the ride will be categorized only if name duration and actual duration are almost the same (with some error tolerance, you can change it)
    if expected_duration and abs(duration - expected_duration) <= 2:
        return expected_duration
    return None

directory = './data_cleansing/activities'
files = os.listdir(directory)
peloton_ride_scores = {}

for filename in files:
    if filename.endswith('.json'):
        filepath = os.path.join(directory, filename)

        with open(filepath, 'r') as file:
            data = json.load(file)

            try:
                external_id = data.get('external_id')
                trainer = data.get('trainer', False)
                activity_type = data.get('type')
                sport_type = data.get('sport_type')

                if (external_id is not None and external_id.endswith('.tcx') and len(external_id) == 36 and trainer and activity_type == 'Ride' and sport_type == 'Ride'):
                    ride_category = categorize_ride(data)
                    if ride_category is not None:
                        score = calculate_score(data)
                        peloton_ride_scores.setdefault(ride_category, {})[filename] = score

            except (KeyError, TypeError) as e:
                continue

categories_order = [5, 10, 15, 20, 30, 45, 60, 75, 90]
sorted_peloton_ride_scores = {k: peloton_ride_scores[k] for k in categories_order if k in peloton_ride_scores}

colors = [Fore.GREEN, Fore.YELLOW, Fore.BLUE, Fore.MAGENTA, Fore.RED, Fore.CYAN, Fore.LIGHTRED_EX, Fore.LIGHTGREEN_EX, Fore.LIGHTYELLOW_EX]

for idx, (category, rides) in enumerate(sorted_peloton_ride_scores.items()):
    sorted_rides = sorted(rides.items(), key=lambda x: x[1], reverse=True)
    #want to see the worst rides?  Uncomment this line -> sorted_rides = sorted(rides.items(), key=lambda x: x[1])
    table = []

    for filename, score in sorted_rides[:5]:
        with open(os.path.join(directory, filename), 'r') as file:
            data = json.load(file)

        ride_time = format_ride_time(data.get('start_date_local', 'N/A'))
        average_speed = data.get('average_speed', 'N/A') * 2.237 if data.get('average_speed') else 'N/A'  # km/h to miles/h
        total_distance = data.get('distance', 'N/A') / 1609 if data.get('distance') else 'N/A' # meter to mile
        average_watts = data.get('average_watts', 'N/A')
        kudos_count = data.get('kudos_count', 'N/A')
        kj_per_min = score / category if category != 0 else 'N/A' # add new metric kj/min
        ride_name = data.get('name', 'N/A')

        table.append([category, ride_time, score, average_speed, total_distance, average_watts, kj_per_min, kudos_count, filename, ride_name]) #update table append

    print('\n' + colors[idx] + f'Top rides for {category} mins:' + Style.RESET_ALL)
    headers = ["Mins", "Ride Date", "kJ Score", "Avg Speed(mph)", "Distance(miles)", "Avg Watts", "kJ/min", "Kudos Count", "Filename", "Ride Name"] #update headers
    print(tabulate(table, headers=headers))


import random
def main():
    dictionary = {}
    n = int(input("Enter the number of key-value pairs: "))
    for _ in range(n):
        key = random.randrange(0, 100)
        value = input(f"Enter value for key {key}: ")
        dictionary[key] = value
    total_numbers = 0
    total_sum = 0
    concatenated_strings = ""
    special_char_strings = []
    for value in dictionary.values():
        if value.isdigit():
            total_numbers += 1
            total_sum += int(value)
        elif isinstance(value, str):
            concatenated_strings += value
            if all(char in "!@#$%^&*()_+[]{}|;':,.<>?/~`" for char in value):
                special_char_strings.append(value)
    if total_numbers > 0:
        average = total_sum / total_numbers
        print(f"Average of numeric values: {average}")
    print(f"Concatenated string: {concatenated_strings}")
    search_value = input("Enter a value to search for: ")
    found_keys = [key for key, value in dictionary.items() if value == search_value]
    if found_keys:
        print(f"Keys with value '{search_value}': {found_keys}")
    else:
        print(f"No keys found with value '{search_value}'")
    print("Values with only special characters:")
    for value in special_char_strings:
        print(value)
if __name__ == "__main__":
    main()


import cgen as c
from sympy import Mod
import pytest

from devito import Grid, Eq, Function, TimeFunction, Operator, sin
from devito.ir.equations import DummyEq
from devito.ir.iet import (Block, Expression, Callable, FindNodes, FindSections,
                           FindSymbols, IsPerfectIteration, Transformer,
                           Conditional, printAST, Iteration)
from devito.types import SpaceDimension, Array

@pytest.fixture(scope="module")
def dims():
    x, y, z = Grid((3, 3, 3)).dimensions
    return {'i': SpaceDimension(name='i'),
            'j': SpaceDimension(name='j'),
            'k': SpaceDimension(name='k'),
            'l': SpaceDimension(name='l'),
            's': SpaceDimension(name='s'),
            'q': SpaceDimension(name='q'),
            'x': x, 'y': y, 'z': z}

@pytest.fixture(scope="module")
def iters(dims):
    return [lambda ex: Iteration(ex, dims['i'], (0, 3, 1)),
            lambda ex: Iteration(ex, dims['j'], (0, 5, 1)),
            lambda ex: Iteration(ex, dims['k'], (0, 7, 1)),
            lambda ex: Iteration(ex, dims['s'], (0, 4, 1)),
            lambda ex: Iteration(ex, dims['q'], (0, 4, 1)),
            lambda ex: Iteration(ex, dims['l'], (0, 6, 1)),
            lambda ex: Iteration(ex, dims['x'], (0, 5, 1)),
            lambda ex: Iteration(ex, dims['y'], (0, 7, 1)),
            lambda ex: Iteration(ex, dims['z'], (0, 7, 1))]

@pytest.fixture(scope="module")
def exprs(dims):
    a = Array(name='a', shape=(3,), dimensions=(dims["i"],)).indexify()
    b = Array(name='b', shape=(3,), dimensions=(dims["i"],)).indexify()
    return [Expression(DummyEq(a, a + b + 5.)),
            Expression(DummyEq(a, b - a)),
            Expression(DummyEq(a, 4 * (b * a))),
            Expression(DummyEq(a, (6. / b) + (8. * a)))]

@pytest.fixture(scope="module")
def block1(exprs, iters):
    # Perfect loop nest:
    # for i
    #   for j
    #     for k
    #       expr0
    return iters[0](iters[1](iters[2](exprs[0])))

@pytest.fixture(scope="module")
def block2(exprs, iters):
    # Non-perfect simple loop nest:
    # for i
    #   expr0
    #   for j
    #     for k
    #       expr1
    return iters[0]([exprs[0], iters[1](iters[2](exprs[1]))])

@pytest.fixture(scope="module")
def block3(exprs, iters):
    # Non-perfect non-trivial loop nest:
    # for i
    #   for s
    #     expr0
    #   for j
    #     for k
    #       expr1
    #       expr2
    #   for p
    #     expr3
    return iters[0]([iters[3](exprs[0]),
                     iters[1](iters[2]([exprs[1], exprs[2]])),
                     iters[4](exprs[3])])

@pytest.fixture(scope="module")
def block4(exprs, iters, dims):
    # Non-perfect loop nest due to conditional
    # for i
    #   if i % 2 == 0
    #   for j
    return iters[0](Conditional(Eq(Mod(dims['i'], 2), 0), iters[1](exprs[0])))

def test_printAST(block1, block2, block3, block4):
    str1 = printAST(block1)
    assert str1 in """
<Iteration i::i::(0, 3, 1)>
  <Iteration j::j::(0, 5, 1)>
    <Iteration k::k::(0, 7, 1)>
      <Expression a[i] = a[i] + b[i] + 5.0>
"""

    str2 = printAST(block2)
    assert str2 in """
<Iteration i::i::(0, 3, 1)>
  <Expression a[i] = a[i] + b[i] + 5.0>
  <Iteration j::j::(0, 5, 1)>
    <Iteration k::k::(0, 7, 1)>
      <Expression a[i] = -a[i] + b[i]>
"""

    str3 = printAST(block3)
    assert str3 in """
<Iteration i::i::(0, 3, 1)>
  <Iteration s::s::(0, 4, 1)>
    <Expression a[i] = a[i] + b[i] + 5.0>
  <Iteration j::j::(0, 5, 1)>
    <Iteration k::k::(0, 7, 1)>
      <Expression a[i] = -a[i] + b[i]>
      <Expression a[i] = 4*a[i]*b[i]>
  <Iteration q::q::(0, 4, 1)>
    <Expression a[i] = 8.0*a[i] + 6.0/b[i]>
"""

    str4 = printAST(block4)
    assert str4 in """
<Iteration i::i::(0, 3, 1)>
  <If Eq(Mod(i, 2), 0)>
    <Iteration j::j::(0, 5, 1)>
      <Expression a[i] = a[i] + b[i] + 5.0>
"""

def test_create_cgen_tree(block1, block2, block3):
    assert str(Callable('foo', block1, 'void', ()).ccode) == """\
void foo()
{
  for (int i = 0; i <= 3; i += 1)
  {
    for (int j = 0; j <= 5; j += 1)
    {
      for (int k = 0; k <= 7; k += 1)
      {
        a[i] = a[i] + b[i] + 5.0F;
      }
    }
  }
}"""
    assert str(Callable('foo', block2, 'void', ()).ccode) == """\
void foo()
{
  for (int i = 0; i <= 3; i += 1)
  {
    a[i] = a[i] + b[i] + 5.0F;

    for (int j = 0; j <= 5; j += 1)
    {
      for (int k = 0; k <= 7; k += 1)
      {
        a[i] = -a[i] + b[i];
      }
    }
  }
}"""
    assert str(Callable('foo', block3, 'void', ()).ccode) == """\
void foo()
{
  for (int i = 0; i <= 3; i += 1)
  {
    for (int s = 0; s <= 4; s += 1)
    {
      a[i] = a[i] + b[i] + 5.0F;
    }
    for (int j = 0; j <= 5; j += 1)
    {
      for (int k = 0; k <= 7; k += 1)
      {
        a[i] = -a[i] + b[i];
        a[i] = 4*a[i]*b[i];
      }
    }
    for (int q = 0; q <= 4; q += 1)
    {
      a[i] = 8.0F*a[i] + 6.0F/b[i];
    }
  }
}"""

def test_find_sections(exprs, block1, block2, block3):
    finder = FindSections()

    sections = finder.visit(block1)
    assert len(sections) == 1

    sections = finder.visit(block2)
    assert len(sections) == 2
    found = list(sections.values())
    assert len(found[0]) == 1
    assert len(found[1]) == 1

    sections = finder.visit(block3)
    assert len(sections) == 3
    found = list(sections.values())
    assert len(found[0]) == 1
    assert len(found[1]) == 2
    assert len(found[2]) == 1

def test_is_perfect_iteration(block1, block2, block3, block4):
    checker = IsPerfectIteration()

    assert checker.visit(block1) is True
    assert checker.visit(block1.nodes[0]) is True
    assert checker.visit(block1.nodes[0].nodes[0]) is True

    assert checker.visit(block2) is False
    assert checker.visit(block2.nodes[1]) is True
    assert checker.visit(block2.nodes[1].nodes[0]) is True

    assert checker.visit(block3) is False
    assert checker.visit(block3.nodes[0]) is True
    assert checker.visit(block3.nodes[1]) is True
    assert checker.visit(block3.nodes[2]) is True

    assert checker.visit(block4) is False
    assert checker.visit(block4.nodes[0]) is False
    assert checker.visit(block4.nodes[0].then_body) is True

def test_transformer_wrap(exprs, block1, block2, block3):
    """Basic transformer test that wraps an expression in comments"""
    line1 = '// This is the opening comment'
    line2 = '// This is the closing comment'
    wrapper = lambda n: Block(c.Line(line1), n, c.Line(line2))
    transformer = Transformer({exprs[0]: wrapper(exprs[0])})

    for block in [block1, block2, block3]:
        newblock = transformer.visit(block)
        newcode = str(newblock.ccode)
        oldnumlines = len(str(block.ccode).split('\n'))
        newnumlines = len(newcode.split('\n'))
        assert newnumlines >= oldnumlines + 2
        assert line1 in newcode
        assert line2 in newcode
        assert "a[i] = a[i] + b[i] + 5.0F;" in newcode

def test_transformer_replace(exprs, block1, block2, block3):
    """Basic transformer test that replaces an expression"""
    line1 = '// Replaced expression'
    replacer = Block(c.Line(line1))
    transformer = Transformer({exprs[0]: replacer})

    for block in [block1, block2, block3]:
        newblock = transformer.visit(block)
        newcode = str(newblock.ccode)
        oldnumlines = len(str(block.ccode).split('\n'))
        newnumlines = len(newcode.split('\n'))
        assert newnumlines >= oldnumlines
        assert line1 in newcode
        assert "a[i0] = a[i0] + b[i0] + 5.0F;" not in newcode

def test_transformer_replace_function_body(block1, block2):
    """Create a Function and replace its body with another."""
    args = FindSymbols().visit(block1)
    f = Callable('foo', block1, 'void', args)
    assert str(f.ccode) == """void foo(float *restrict a_vec, float *restrict b_vec)
{
  for (int i = 0; i <= 3; i += 1)
  {
    for (int j = 0; j <= 5; j += 1)
    {
      for (int k = 0; k <= 7; k += 1)
      {
        a[i] = a[i] + b[i] + 5.0F;
      }
    }
  }
}"""

    f = Transformer({block1: block2}).visit(f)
    assert str(f.ccode) == """void foo(float *restrict a_vec, float *restrict b_vec)
{
  for (int i = 0; i <= 3; i += 1)
  {
    a[i] = a[i] + b[i] + 5.0F;

    for (int j = 0; j <= 5; j += 1)
    {
      for (int k = 0; k <= 7; k += 1)
      {
        a[i] = -a[i] + b[i];
      }
    }
  }
}"""

def test_transformer_add_replace(exprs, block2, block3):
    """Basic transformer test that adds one expression and replaces another"""
    line1 = '// Replaced expression'
    line2 = '// Adding a simple line'
    replacer = Block(c.Line(line1))
    adder = lambda n: Block(c.Line(line2), n)
    transformer = Transformer({exprs[0]: replacer,
                               exprs[1]: adder(exprs[1])})

    for block in [block2, block3]:
        newblock = transformer.visit(block)
        newcode = str(newblock.ccode)
        oldnumlines = len(str(block.ccode).split('\n'))
        newnumlines = len(newcode.split('\n'))
        assert newnumlines >= oldnumlines + 1
        assert line1 in newcode
        assert line2 in newcode
        assert "a[i0] = a[i0] + b[i0] + 5.0F;" not in newcode

def test_nested_transformer(exprs, iters, block2):
    """When created with the kwarg ``nested=True``, a Transformer performs
    nested replacements. This test simultaneously replace an inner expression
    and an Iteration sorrounding it."""
    target_loop = block2.nodes[1]
    target_expr = target_loop.nodes[0].nodes[0]
    mapper = {target_loop: iters[3](target_loop.nodes[0]),
              target_expr: exprs[3]}
    processed = Transformer(mapper, nested=True).visit(block2)
    assert printAST(processed) == """<Iteration i::i::(0, 3, 1)>
  <Expression a[i] = a[i] + b[i] + 5.0>
  <Iteration s::s::(0, 4, 1)>
    <Iteration k::k::(0, 7, 1)>
      <Expression a[i] = 8.0*a[i] + 6.0/b[i]>"""

def test_find_symbols():
    grid = Grid(shape=(4, 4))
    x, y = grid.dimensions

    f = Function(name='f', grid=grid)

    op = Operator(Eq(f, f[x-1, y] + f[x+1, y] + 1))

    symbols = FindSymbols('indexeds').visit(op)

    assert len(symbols) == 3

def test_find_symbols_with_duplicates():
    grid = Grid(shape=(4, 4))
    x, y = grid.dimensions

    f = TimeFunction(name='f', grid=grid)
    g = Function(name='g', grid=grid)

    eq = Eq(f.forward, sin(g)*f + g)

    op = Operator(eq)

    exprs = FindNodes(Expression).visit(op)

    assert len(exprs) == 2

    # Two syntactically identical indexeds r0[x, y], but they're different
    # objects because they are constructed in two different places during
    # the CIRE pass
    r0a = exprs[0].output
    r0b = exprs[1].expr.rhs.args[0].args[0]
    assert r0a.function is r0b.function
    assert r0a.name == r0b.name == "r0"
    assert hash(r0a) == hash(r0b)
    assert r0a is not r0b

    # So we expect FindSymbols to catch five Indexeds in total
    symbols = FindSymbols('indexeds').visit(op)
    assert len(symbols) == 5


#22 栈的压入、弹出序列
class Solution:
    def IsPopOrder(self, pushV, popV):
        # write code here
        #1,2,3,4,5是某栈的压入顺序，序列4,5,3,2,1
        stack = []
        for i in pushV:
            stack.append(i)
            while stack and stack[-1] == popV[0]:
                stack.pop()
                popV.pop(0)
        return True if not stack else False

22栈的压入弹出序列.py


#!/usr/bin/env python2

import os

cwd=os.getcwd()
memdir=cwd+'/src/object_detection/memory'

def listCat():
	categories = [dI for dI in os.listdir(memdir) if os.path.isdir(os.path.join(memdir,dI))]
	return(categories)

def listObj(category):
	objects=os.listdir(memdir+'/' + category)
	for o in objects:
		if ".txt" not in o:
			objects.remove(o)
	return(objects)


"""
Class for handle reliable multicast and unicast.

Inspired by https://github.com/daeyun/reliable-multicast-chat
"""
from queue import PriorityQueue
import socket
import struct
import threading
import time
import json
from localshit.utils import utils
from localshit.utils.utils import logging

class ReliableSocketWorker:
    def __init__(self, running, hosts, port=10033):
        self.port = port
        self.hosts = hosts
        self.running = running

        self.my_id = self.hosts.current_member_ip
        self.message_max_size = 4096
        self.message_id_counter = 0
        self.threads = []

        self.has_received = {}
        self.has_acknowledged = {}  # saves acknowledged messages
        self.unack_messages = []  # messages with pending acknowledgement
        self.holdback_queue = []

        self.holdback_sequence_counter = 0
        self.sequence_counter = 0
        self.SEQUENCER_ID = 0

        self.queue = PriorityQueue()
        self.mutex = threading.Lock()
        self.my_timestamp = {}
        for host in self.hosts.members:
            self.my_timestamp[host] = 0

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
        self.sock.bind((utils.get_host_address(), port))
        self.sock.settimeout(0.01)

    def unicast_send(
        self, destination, message, msg_id=None, is_ack=False, timestamp=None,
    ):
        """ Push an outgoing message to the message queue. """
        if timestamp is None:
            timestamp = self.my_timestamp

        is_msg_id_specified = msg_id is not None
        if msg_id is None:
            msg_id = self.message_id_counter

        # pack message with utils, make vector timestamp
        message = self.pack_message(
            [self.my_id, msg_id, is_ack, json.dumps(timestamp), message]
        )

        # append every new message excluding ack msg or without id to unack_messages. they will be removed as soon as message was acknowledged
        if not is_ack and not is_msg_id_specified:
            with self.mutex:
                self.unack_messages.append((destination, message))

        dest_ip = destination
        dest_port = self.port
        send_time = time.time()
        # put to queue, which sends the messages out
        self.queue.put((send_time, message, dest_ip, dest_port, msg_id))

    def unicast_receive(self):
        """ Receive UDP messages from other chat processes and store them in the holdback queue.
            Returns True if new message was received. """

        data, _ = self.sock.recvfrom(self.message_max_size)
        [sender, message_id, is_ack, message_timestamp, message] = self.unpack_message(
            data
        )

        logging.debug("Received #%s from %s" % (message_id, sender))

        # add sender to timestamps if not yet
        if sender not in self.my_timestamp:
            self.my_timestamp[sender] = message_timestamp[sender]

        # check if hosts in hold-back queue are up-to-date with ring
        new_timestamp = {}
        for host in self.my_timestamp:
            if host in self.hosts.members:
                new_timestamp[host] = self.my_timestamp[host]
        self.my_timestamp = new_timestamp

        logging.debug("Vector Clock: %s" % self.my_timestamp)

        # check if ack message type
        if is_ack:
            # save that ack was send
            self.has_acknowledged[(sender, message_id)] = True
        else:
            # if normal message, send acknowledgement to the sender
            self.unicast_send(sender, "ACK", msg_id=message_id, is_ack=True)
            # check if message was send more than once
            if (sender, message_id) not in self.has_received:
                self.has_received[(sender, message_id)] = True
                self.holdback_queue.append((sender, message_timestamp, message))
                self.update_holdback_queue_casual()
                return True
        return False

    def update_holdback_queue_casual(self):
        """ Compare message timestamps to ensure casual ordering. """
        while True:
            new_holdback_queue = []
            removed_messages = []
            logging.debug("Holdback-queue size: %s" % (len(self.holdback_queue)))
            # check with vector timestamp, if ordering is correct or some messages are missing
            for sender, v, message in self.holdback_queue:
                should_remove = True
                for item in v:
                    if item == sender:
                        if v[item] != self.my_timestamp[item]:
                            should_remove = False
                        if v[item] > self.my_timestamp[item]:
                            should_remove = False
                if not should_remove:
                    new_holdback_queue.append((sender, v, message))
                else:
                    removed_messages.append((sender, v, message))

            # deliver the messages which are removed from holdback queue in this update cycle
            for sender, v, message in removed_messages:
                self.my_timestamp[sender] += 1  # update own vector_clock timestamp
                self.deliver(sender, message)

            self.holdback_queue = new_holdback_queue

            if not removed_messages:
                break

    def multicast(self, message):
        """ Unicast the message to all known clients. """
        # immitate multicast as for loop with unicasts. only on this way we get reliable multicast
        for destination in self.hosts.members:
            self.unicast_send(destination, message)
        self.message_id_counter += 1

    def pack_message(self, message_list):
        return (";".join([str(x) for x in message_list])).encode("utf-8")

    def unpack_message(self, message):
        message = message.decode("utf-8")
        (sender, message_id, is_ack, vector_str, message,) = message.split(";", 4)

        message_id = int(message_id)
        timestamp = json.loads(vector_str)
        is_ack = is_ack in ["True", "true", "1"]

        return [sender, message_id, is_ack, timestamp, message]

    def ip2int(self, addr):
        return struct.unpack("!I", socket.inet_aton(addr))[0]

    def int2ip(self, addr):
        return socket.inet_ntoa(struct.pack("!I", addr))

    def message_queue_handler(self, running):
        """ Thread that actually sends out messages when send time <= current_time. """
        # TODO: if we have removed randomness in sending messages, can we simplify this?
        while running:
            (send_time, message, ip, port, msg_id) = self.queue.get(block=True)
            if send_time <= time.time():
                logging.debug("Send #%s to %s" % (msg_id, ip))
                self.sock.sendto(message, (ip, port))
            else:
                self.queue.put((send_time, message, ip, port, msg_id))
                time.sleep(0.01)

    def ack_handler(self, running):
        """ Thread that re-sends all unacknowledged messages. """
        while running:
            time.sleep(0.2)

            with self.mutex:
                new_unack_messages = []
                for dest_id, packed_message in self.unack_messages:
                    [
                        _,
                        message_id,
                        is_ack,
                        message_timestamp,
                        message,
                    ] = self.unpack_message(packed_message)
                    # check if message was not acknowledged and add it to new list, but send no ACK-ACK msg
                    if (dest_id, message_id) not in self.has_acknowledged:
                        new_unack_messages.append((dest_id, packed_message))
                self.unack_messages = new_unack_messages

    def incoming_message_handler(self, running):
        """ Thread that listens for incoming UDP messages """
        while running:
            try:
                self.unicast_receive()
            except (socket.timeout, BlockingIOError):
                pass

    def deliver(self, sender, message):
        """ Do something with the received message. """
        # TODO: save message to database
        if sender != self.hosts.current_member_ip:
            self.multicast_delivered(sender, message)
        else:
            logging.debug("received own message.")
            self.multicast_delivered(sender, message)

    def multicast_delivered(self, sender, message):
        pass

    def set_fn_delivered(self, fn):
        self.multicast_delivered = fn

    def run(self):
        """ Initialize and start all threads. """
        thread_routines = [
            self.ack_handler,
            self.message_queue_handler,
            self.incoming_message_handler,
        ]

        count = 1
        for thread_routine in thread_routines:
            thread = threading.Thread(
                target=thread_routine,
                args=(self.running,),
                name="ReliableSocketWorker-%s" % count,
            )
            thread.daemon = True
            thread.start()
            logging.info("Thread %s started." % thread.name)
            self.threads.append(thread)
            count += 1


import pdfplumber
import pandas as pd
import openpyxl
import matplotlib.pyplot as plt

tableList = []
column = []

def initTableList():
    tableList.append('合并资产负债表')
    tableList.append('母公司资产负债表')
    tableList.append('合并利润表')
    tableList.append('母公司利润表')
    tableList.append('合并现金流量表')
    tableList.append('母公司现金流量表')

def getDf(tables):
    df = None
    for table in tables:
        df = pd.DataFrame(table)
        # 第一列当成表头：
        df = pd.DataFrame(table[1:],columns=table[0])
        global column
        column = table[0]
        # print("column1 = ", column)
    return df

def getDfWithoutHead(tables):
    df = None
    # print("column2 = ", column)
    for table in tables:
        if table[0][0] == '项目':
            break
        df = pd.DataFrame(table[0:],columns=column)
        # print(df)
    return df

def splitTables(pdf):
    initTableList()
    tablePageRange = {}
    index = []
    pageNumber = 0
    for page in pdf.pages:
        text = page.extract_text()
        if text.find('财务报表调整情况说明') != -1:
            index.append(pageNumber)
        if len(index) == 7:
            break
        for table in tableList:
            if text.find(table) != -1:
                # print("111111",table ,"111111")
                index.append(pageNumber)
        pageNumber += 1
    i = 0
    for table in tableList:
        # if i < 5:
        tablePageRange[table] = index[i:i+2]
        i += 1
        # else:
        #     tablePageRange[table] = [index[i],24]
    # print("2",tablePageRange,"2")
    # print("3",index,"3")
    return tablePageRange

with pdfplumber.open("D:\投资\财报\巨星科技\\2021年第一季度报告全文.PDF") as pdf:

    tablePageRange = splitTables(pdf)
    for key,value in tablePageRange.items():
        # print(value)
        dfList = []
        i = 0
        for page in range(value[0],value[1]+1):
            # print("page =",page)
            if i == 0 :
                dfList.append(getDf(pdf.pages[page].extract_tables()))
                i += 1
            else:
                dfList.append(getDfWithoutHead(pdf.pages[page].extract_tables()))
        dfCombine = pd.concat(dfList, ignore_index=True, sort=False)
        dfCombine.to_excel("D:\投资\财报\巨星科技\\2021年第一季度报告全文_"+key+".xlsx", sheet_name=key)
        print('finish',key)

# with pdfplumber.open("D:\投资\财报\巨星科技\\2021年第一季度报告全文.PDF") as pdf:
#     page14 = pdf.pages[14]
#     # page13 = pdf.pages[12]
#     text14 = page14.extract_text()
#     #print(text14)
#     tables14 = page14.extract_tables()
#     print(tables14)
#     # tables13 = page13.extract_tables()
#     # print(tables12)
#     dfList = []
#     for table in tables14:
#         print(table[0])
#         print(table[0][0] =='其他综合收益')
#         # for row in table:
#         #     print(row)
#         print("#########")
#
#     # df12 = getDf(tables14)
#     df14 = getDfWithoutHead(tables14)
#     # dfCombine = df12.append(df13,sort=False)
#     # dfCombine = pd.concat([df12,df13],ignore_index=True,sort=False)
#     # print(df.head(2))
#         # print(df.tail(3))
#         # print(df.index)
#         # print(df.columns)
#         # #print(df.describe)
#         # print(df[1:2]['2021年3月31日'])
#         # print(df.iloc[1,1])
#         # # df=df.cumsum()
#         # # plt.figure()
#         # # df.plot()
#     # df14.to_excel("D:\投资\财报\巨星科技\\2021年第一季度报告全文.xlsx",sheet_name='Sheet1')


from django_components import component

@component.register("icon")
class Icon(component.Component):
    template_name = "icon/icon.html"

    def get_context_data(self, icon, icon_type, icon_class):
        return {
            "icon": icon,
            "icon_type": icon_type,
            "icon_class": icon_class,
        }

# {% component "icon" icon="" icon_type="" icon_class="" %}
# icon: "information-circle", ... (same naming as the website: https://heroicons.com/)
# icon_type: "outline", "solid", "mini"
# icon_class: defaults with "w-5 h-5", you can add other classes for tailwind "..."

# NOTE need to load following line at the top of component's html to do nested components
# {% load component_tags %}


# 문제 url: https://swexpertacademy.com/main/learn/course/subjectDetail.do?courseId=AVuPDYSqAAbw5UW6&subjectId=AWUYG3y62EcDFAVT#
from collections import deque

def bfs(i):
    q = deque()  # 빈큐
    q.append(i)  # 시작 정점을 추가한다.
    visit[i] = 1
    while q:
        v = q.popleft()
        # 해당 정점의 인접정점을 순회한다.
        for j in arr[v]:
            # 방문하지 않았다면, 무리에 추가한다.
            if not visit[j]:
                visit[j] = 1 # 같은 팀!
                q.append(j)  # 같은 팀!

T = int(input())

for t in range(1, T+1):
    N, M = map(int, input().split())
    list_ = list(map(int, input().split()))
    arr = [[] for _ in range(N+1)]
    visit = [0]*(N+1)
    team = 0

    # 인접 리스트 생성
    for i in range(M):
        arr[list_[i*2]].append(list_[i*2+1])
        arr[list_[i*2+1]].append(list_[i*2])

    # 모든 정점을 순회한다.
    for i in range(1, N+1):
        if visit[i]:    # 방문한 정점은 이미 팀이 있다.
            continue
        if not arr[i]:  # 인접 정점이 없으면 혼자 팀이다.
            team += 1
            visit[i] = 1
            continue
        bfs(i)          # 해당 정점와 같은 팀인 정점을 탐색한다.
        team += 1

    print(f'#{t} {team}')


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun May  8 20:02:06 2022

@author: oyxl
"""

import numpy as np
import pandas as pd
import copy

class gmoverid():

    # public variable

    # private variable

    def __init__(self, path, mostype):
        self.path = path
        self.mostype = mostype
        self.name = str.upper(self.mostype)

    # def getData(self):

    def var_split(data):
        x_array = np.zeros([data.shape[0], int(data.shape[1]/2)])
        y_array = copy.copy(x_array)

if __name__ == '__main__':
    a = gmoverid('data/', 'nmos')
    print(a.name)


""" Исключения в Python """

"""

result = 1 / 0

Если запустить этот код мы получим ошибку ZeroDivisionError.
Более корректно называть это исключением.

Существует (как минимум) два различимых вида ошибок: синтаксические ошибки (syntax errors) и исключения (exceptions).

Синтаксические ошибки, появляются во время разбора кода интерпретатором. С точки зрения синтаксиса в коде выше ошибки нет, интерпретатор видит деление одного integer на другой.

Однако в процессе выполнения возникает исключение. Интерпретатор разобрал код, но провести операцию не смог.
Таким образом ошибки, обнаруженные при исполнении, называются исключениями (exceptions).

Исключения бывают разных типов и тип исключения выводится в сообщении об ошибке, например ZeroDivisionError, NameError, ValueError

"""

""" Давайте обрабатывать!

Существует возможность написать код, который будет перехватывать избранные исключения. Посмотрите на представленный пример, в котором пользователю предлагают вводить число до тех пор, пока оно не окажется корректным целым. Тем не менее, пользователь может прервать программу (используя сочетание клавиш Control-C или какое-либо другое, поддерживаемое операционной системой)
    Заметьте — о вызванном пользователем прерывании сигнализирует исключение KeyboardInterrupt.

"""
while True:
    try:
        x = int(input("Input a number:"))
        break
    except ValueError:
        print("Incorrect integer")

"""

Оператор try работает следующим образом:

В начале исполняется блок try (операторы между ключевыми словами try и except).
Если при этом не появляется исключений, блок except не выполняется и оператор try заканчивает работу.
Если во время выполнения блока try было возбуждено какое-либо исключение, оставшаяся часть блока не выполняется. Затем, если тип этого исключения совпадает с исключением, указанным после ключевого слова except, выполняется блок except, а по его завершению выполнение продолжается сразу после оператора try-except.
Если порождается исключение, не совпадающее по типу с указанным в блоке except — оно передаётся внешним операторам try; если ни одного обработчика не найдено, исключение считается необработанным (unhandled exception), и выполнение полностью останавливается и выводится сообщение об ошибке.

Блок except может указывать несколько исключений в виде заключённого в скобки кортежа.

"""
try:
    x = int(input("Input another number:"))
except (RuntimeError, TypeError, NameError, ValueError):
    print("Caught an exception")

"""
В последнем блоке except можно не указывать имени (или имён) исключений. Тогда он будет действовать как обработчик всех исключений.
"""

while True:
    try:
        x = int(input("Input a number:"))
        break
    except:
        print("Incorrect integer")

""" Получить доступ к информации об исключении можно используя sys.exc_info()[0] """
import sys
while True:
    try:
        x = int(input("Input a number:"))
        break
    except:
        print("Caught exception:",sys.exc_info()[0])

""" Более простой способ: записать экземпляр исключения в переменную """

while True:
    try:
        x = int(input("Input a number:"))
        break
    except ValueError as e:
        print("Incorrect integer", e)

"""
Исключения могут охватывать несколько уровней.

При возбуждении исключения оно передается "вверх" пока не достигнет самого высокого уровня или не будет "поймано" блоком except.

Это значит, что исключения внутри функции не вызовут ошибку, если функция будет в блоке try-except:
"""
def zero_division():
    return 1/0

try:
    zero_division()
except:
    print("Caught exception")

"""
Можно увеличить вложенность
"""

def level_3():
    return 1/0

def level_2():
    return level_3()

def level_1():
    return level_2

try:
    level_1()
except:
    print("Caught exception")

"""
Можно порождать свои исключения оператором raise
"""
try:
    raise(Exception("amazing!"))
except Exception as e:
    print(e)

"""
Можно добавить в блок try-except блоки else и finally.

Блок else будет выполнен если try не породил исключений.

Блок finally будет выполнен в любом случае.

"""

def divide(x, y):
    try:
        result = x / y
    except ZeroDivisionError:
        print("Zero division!")
    else:
        print("Result ", result)
    finally:
        print("finally")

print(divide(1,2))
print(divide(1,0))

"""
Можно создавать собственные исключения - для этого нужно объявить новый класс, наследующийся от Exception.

"""

class MyError(Exception):
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)

try:
    raise MyError(2*2)
except MyError as e:
    print('My exception occurred, value:', e.value)

"""
    Источник:
    http://pep8.ru/doc/tutorial-3.1/8.html

    Для закрепления:
        добавить обработку исключений в парсер, чтобы программа не "вылетала" при неудачных попытках читать и писать несуществующие или заблокированные файлы (исключение IOError например).
"""


import os
import pdfplumber
from docx import Document
from openpyxl import load_workbook

# supported file types
supported_files = ['.docx', '.pdf', '.xlsx', '.txt']

# Returns all the text in a file as a list
# Return an empty list if no text is found
def get_file_text(file) -> list:
    # List object to return any text found
    file_text = []

    # split filename into name and extension
    name, ext = os.path.splitext(file)

    if ext.lower() == '.pdf':
        # this is a pdf file - open it with pdfplumber
        # https://github.com/jsvine/pdfplumber
        with pdfplumber.open(file) as pdf:
            # loop through each page in the pdf
            for p in pdf.pages:
                # extract the text as a str
                text = p.extract_text()
                for line in text.split('\n'):
                    file_text.append(line)
    elif ext.lower() == '.docx':
        # this is a docx file
        # https://python-docx.readthedocs.io/en/latest/user/text.html
        document = Document(file)
        for p in document.paragraphs:
            file_text.append(p.text)
    elif ext.lower() == '.xlsx':
        # This is a xlsx
        # https://openpyxl.readthedocs.io/en/stable/tutorial.html#loading-from-a-file
        wb = load_workbook(file)
        # scan through all the worksheets
        for ws in wb:
            for row in ws.values:
                # Save a row as a single line rather than adding each cell to a list
                row_list = []
                for value in row:
                    # Don't add empty cells to the list
                    if value is not None:
                        row_list.append(value.rstrip(' '))
                file_text.append(' '.join(row_list))
    elif ext.lower() == '.txt':
        # This is a text file
        with open(file) as f:
            for line in f.readlines():
                file_text.append(line.rstrip())
    else:
        raise RuntimeError('Unsupported file type: ' + ext)
    return file_text

def scan_files() -> list:
    # list of files found
    found_list = []

    # Walk all files starting from the files directory
    for root, dirs, files in os.walk('files'):
        # look for supported file types
        for name in files:
            file, ext = os.path.splitext(name)
            # if the file extension is in the supported list
            # add it to the found list.  Handle case sensitivity
            if ext.lower() in supported_files:
                found_list.append(os.path.join(root, name))
    return found_list

def show_aggie_pride():

    slogan_list = ['Aggie Pride - Worldwide',
                    'Aggie Pride!',
                    'And That\'s On My 1891',
                    'Aggies ❤ engineering',
                   'Aggies Do!',
                   'I like my Aggie Pride!'
                  ]

    slogan_list = ['Aggie Pride - Worldwide', 'Aggies Do!', 'Aggie Pride!', 'And That\'s On My 1891',
                   'Aggies ❤ engineering', 'Aggies Do!', 'Aggies Get it Done!', 'NCAT',
                   'Aggie born aggie bred, when I die Ill be aggie dead', 'Aggies Do']

    return slogan_list

if __name__ == '__main__':
    print(show_aggie_pride())
    print('---')
    print(scan_files())