import random, time, math, json, string
from enum import Enum
from typing import Tuple

from ducktape.tests.test import TestContext
from ducktape.utils.util import wait_until
from rptest.clients.rpk import RpkTool
from rptest.clients.types import TopicSpec
from rptest.services.cluster import cluster
from rptest.services.redpanda import MetricsEndpoint
from rptest.services.rpk_producer import RpkProducer
from rptest.tests.redpanda_test import RedpandaTest
from rptest.services.kcat_consumer import KcatConsumer
from rptest.clients.kafka_cat import KafkaCat

# This file is about throughput limiting that works at shard/node/cluster (SNC)
# levels, like cluster-wide and node-wide throughput limits

kiB = 1024
MiB = 1024 * kiB

class ThroughputLimitsSnc(RedpandaTest):
    """
    Tests for throughput limiting that works at shard/node/cluster (SNC)
    levels, like cluster-wide and node-wide throughput limits
    """
    def __init__(self, test_ctx: TestContext, *args, **kwargs):
        super(ThroughputLimitsSnc, self).__init__(test_ctx,
                                                  num_brokers=3,
                                                  *args,
                                                  **kwargs)
        rnd_seed_override = test_ctx.globals.get("random_seed")
        if rnd_seed_override is None:
            # default seed value is composed from
            # - current time - the way the default generator ctor does
            # - hash of the redpanda service instance - to avoid possible
            #   identical seeds while running multiple tests in parallel
            #   with --repeat
            self.rnd_seed = int(time.time() * 1_000_000_000) + hash(
                self.redpanda.service_id)
        else:
            self.rnd_seed = rnd_seed_override
        self.logger.info(f"Random seed: {self.rnd_seed}")
        self.rnd = random.Random(self.rnd_seed)
        self.rpk = RpkTool(self.redpanda)

    class ConfigProp(Enum):
        QUOTA_NODE_MAX_IN = "kafka_throughput_limit_node_in_bps"
        QUOTA_NODE_MAX_EG = "kafka_throughput_limit_node_out_bps"
        THROTTLE_DELAY_MAX_MS = "max_kafka_throttle_delay_ms"
        BAL_WINDOW_MS = "kafka_quota_balancer_window_ms"
        BAL_PERIOD_MS = "kafka_quota_balancer_node_period_ms"
        QUOTA_SHARD_MIN_RATIO = "kafka_quota_balancer_min_shard_throughput_ratio"
        QUOTA_SHARD_MIN_BPS = "kafka_quota_balancer_min_shard_throughput_bps"
        CONTROLLED_API_KEYS = "kafka_throughput_controlled_api_keys"
        THROUGHPUT_CONTROL = "kafka_throughput_control"

    api_names = [
        "add_offsets_to_txn", "add_partitions_to_txn", "alter_configs",
        "alter_partition_reassignments", "api_versions", "create_acls",
        "create_partitions", "create_topics", "delete_acls", "delete_groups",
        "delete_topics", "describe_acls", "describe_configs",
        "describe_groups", "describe_log_dirs", "describe_producers",
        "describe_transactions", "end_txn", "fetch", "find_coordinator",
        "heartbeat", "incremental_alter_configs", "init_producer_id",
        "join_group", "leave_group", "list_groups", "list_offsets",
        "list_partition_reassignments", "list_transactions", "metadata",
        "offset_commit", "offset_delete", "offset_fetch",
        "offset_for_leader_epoch", "produce", "sasl_authenticate",
        "sasl_handshake", "sync_group", "txn_offset_commit"
    ]

    def binexp_random(self, min: int, max: int):
        min_exp = min.bit_length() - 1
        max_exp = max.bit_length() - 1
        return math.floor(2**(self.rnd.random() * (max_exp - min_exp) +
                              min_exp))

    def get_config_parameter_random_value(self, prop: ConfigProp):
        if prop in [
                self.ConfigProp.QUOTA_NODE_MAX_IN,
                self.ConfigProp.QUOTA_NODE_MAX_EG
        ]:
            r = self.rnd.randrange(4)
            min = 256  # practical minimum
            if r == 0:
                return None
            if r == 1:
                return min
            return self.binexp_random(min, 2**40)  # up to 1 TB/s

        if prop == self.ConfigProp.QUOTA_SHARD_MIN_BPS:
            r = self.rnd.randrange(3)
            if r == 0:
                return 0
            return self.binexp_random(0, 2**30)  # up to 1 GB/s

        if prop == self.ConfigProp.QUOTA_SHARD_MIN_RATIO:
            r = self.rnd.randrange(3)
            if r == 0:
                return 0
            if r == 1:
                return 1
            return self.rnd.random()

        if prop == self.ConfigProp.THROTTLE_DELAY_MAX_MS:
            r = self.rnd.randrange(3)
            if r == 0:
                return 0
            return self.binexp_random(0, 2**25)  # up to ~1 year

        if prop == self.ConfigProp.BAL_WINDOW_MS:
            r = self.rnd.randrange(4)
            min = 1
            max = 2147483647
            if r == 0:
                return min
            if r == 1:
                return max
            return self.binexp_random(min, max)

        if prop == self.ConfigProp.BAL_PERIOD_MS:
            r = self.rnd.randrange(3)
            if r == 0:
                return 0
            return self.binexp_random(0, 2**22)  # up to ~1.5 months

        if prop == self.ConfigProp.CONTROLLED_API_KEYS:
            keys_num = len(self.api_names)
            keys = set()
            for _ in range(self.rnd.randrange(keys_num)):
                keys.add(self.api_names[self.rnd.randrange(keys_num)])
            return list(keys)

        if prop == self.ConfigProp.THROUGHPUT_CONTROL:
            throughput_control = []
            letters = string.digits + string.ascii_letters + ' '
            group_names = set()
            for _ in range(self.rnd.randrange(4)):
                tc_item = {}

                r = self.rnd.randrange(3)
                if r != 0:
                    while True:
                        new_name = ''.join(
                            self.rnd.choice(letters)
                            for _ in range(self.binexp_random(0, 512)))
                        if new_name not in group_names:
                            break
                    tc_item['name'] = new_name
                    group_names.add(new_name)

                r = self.rnd.randrange(3)
                if r == 0:
                    tc_item['client_id'] = 'client_id 1'
                elif r == 2:
                    tc_item['client_id'] = 'client_\d+'

                throughput_control.append(tc_item)
            return throughput_control

        raise Exception(f"Unsupported ConfigProp: {prop}")

    def current_effective_node_quota(self) -> Tuple[int, int]:
        metrics = self.redpanda.metrics_sample(
            "quotas_quota_effective", metrics_endpoint=MetricsEndpoint.METRICS)

        assert metrics, "Effective quota metric is missing"
        self.logger.debug(f"Samples: {metrics.samples}")

        node_quota_in = sum(
            int(s.value) for s in metrics.label_filter({
                "direction": "ingress"
            }).samples)
        node_quota_eg = sum(
            int(s.value) for s in metrics.label_filter({
                "direction": "egress"
            }).samples)
        return node_quota_in, node_quota_eg

    def setUp(self):
        pass

    @cluster(num_nodes=3)
    def test_configuration(self):
        """
        Test various configuration patterns, including extreme ones,
        verify that it does not wreck havoc onto cluster
        """

        self.config = {}
        for prop in list(self.ConfigProp):
            val = self.get_config_parameter_random_value(prop)
            self.config[prop] = val
            if not val is None:
                self.redpanda.add_extra_rp_conf({prop.value: val})
        self.logger.info(
            f"Initial cluster props: {self.redpanda._extra_rp_conf}")
        super(ThroughputLimitsSnc, self).setUp()

        # TBD: parameterize to run under load or not

        errors = []
        # 12 passes approximately take 30 secs on a 3-node cluster
        for _ in range(12):

            change = {}
            for _ in range(self.rnd.randrange(4)):
                config_param = self.rnd.choice(list(self.ConfigProp))
                config_value = self.get_config_parameter_random_value(
                    config_param)
                self.config[config_param] = config_value
                change[config_param.value] = config_value
            self.logger.info(f"Changing cluster prop: {change}")
            self.redpanda.set_cluster_config(change)

            # set_cluster_config waits for the prop to be replicated
            # it takes time so no need for a sleep()

            def check_node_quota_metric(self, config_prop: self.ConfigProp,
                                        effective_node_quota: int) -> str:
                config_val = self.config[config_prop]
                if config_val is None:
                    return
                expected_node_quota = config_val * len(self.redpanda.nodes)
                if effective_node_quota == expected_node_quota:
                    return
                e = (
                    f"Expected quota value mismatch. "
                    f"Effective {effective_node_quota} != expected {expected_node_quota}. "
                    f"Direction: {config_prop.name}")
                self.logger.error(e)
                return e

            effective_node_quota = self.current_effective_node_quota()
            self.logger.debug(
                f"current_effective_node_quota: {effective_node_quota}")
            errors.append(
                check_node_quota_metric(self,
                                        self.ConfigProp.QUOTA_NODE_MAX_IN,
                                        effective_node_quota[0]))
            errors.append(
                check_node_quota_metric(self,
                                        self.ConfigProp.QUOTA_NODE_MAX_EG,
                                        effective_node_quota[1]))

        errors = set([e for e in errors if e])
        assert len(errors) == 0, (
            f"Test has failed with {len(errors)} distinct errors. "
            f"{errors}, rnd_seed: {self.rnd_seed}")

    @cluster(num_nodes=6)
    def test_consumers(self):
        """
        Non-KIP-219-compliant consumers with capability beyond the configured
        egress limit should not timeout or prevent other such consumers from
        joining a cgroup
        """
        self.redpanda.add_extra_rp_conf({
            self.ConfigProp.QUOTA_NODE_MAX_EG.value:
            400 * kiB,
            "kafka_batch_max_bytes":
            1 * MiB,
        })
        self.redpanda.set_seed_servers(self.redpanda.nodes)
        self.redpanda.start(omit_seeds_on_idx_one=False)

        partition_count = 2
        self.topics = [TopicSpec(partition_count=partition_count)]
        self._create_initial_topics()

        msg_count = 2000
        producer = RpkProducer(self.test_context,
                               self.redpanda,
                               self.topic,
                               msg_size=10 * kiB,
                               msg_count=msg_count // partition_count,
                               printable=True)
        producer.start()

        def on_message(consumer: KcatConsumer, message: dict):
            message["payload"] = f"<{len(message['payload'])} bytes>"
            consumer._redpanda.logger.debug(f"{consumer._caption}{message}")

        consumer = KcatConsumer(self.test_context,
                                self.redpanda,
                                self.topic,
                                offset=KcatConsumer.OffsetMeta.beginning,
                                cgroup_name="cg01",
                                auto_commit_interval_ms=500)
        consumer.set_on_message(on_message)
        consumer.start()
        wait_until(
            lambda: consumer.consumed_total >= msg_count // 10,
            timeout_sec=10,
            backoff_sec=0.001,
            err_msg=
            "Timeout waiting for the first consumer to receive 10% of messages"
        )

        consumer2 = KcatConsumer(
            self.test_context,
            self.redpanda,
            self.topic,
            offset=KcatConsumer.OffsetMeta.stored,
            offset_default=KcatConsumer.OffsetDefaultMeta.beginning,
            cgroup_name="cg01",
            auto_commit_interval_ms=500)
        consumer2.set_on_message(on_message)
        consumer2.start()
        # ensure that the 2nd consumer has connected and joined the cgroup
        # by checking that it has started to consume
        wait_until(
            lambda: consumer2.consumed_total > 0,
            timeout_sec=30,
            err_msg=
            "Timeout waiting for the second consumer to join the cgroup and consume something"
        )

        producer.stop()
        consumer.stop()
        consumer2.stop()


from flask_restx import Api
from flask import Blueprint

from .main.controller.auth_controller import api as auth_ns
from .main.controller.category_controller import api as category_ns
from .main.controller.daliy_activation_controller import api as daliy_activation_ns
from .main.controller.employee_controller import api as employee_ns
from .main.controller.activity_controller import api as activity_ns
from .main.controller.scan_controller import api as scan_ns

blueprint = Blueprint('web', __name__)
staticBlueprint = Blueprint('css', __name__, static_folder='static', static_url_path='/static')

authorizations = {
    'apikey': {
        'type': 'apiKey',
        'in': 'header',
        'name': 'Authorization'
    }
}

api = Api(
    blueprint,
    title='FLASK RESTPLUS(RESTX) API BOILER-PLATE WITH JWT',
    version='1.0',
    description='a boilerplate for flask restplus (restx) web service',
    authorizations=authorizations,
    security='apikey'
)

api.add_namespace(auth_ns)
api.add_namespace(activity_ns)
api.add_namespace(daliy_activation_ns)
api.add_namespace(employee_ns)
api.add_namespace(category_ns)
api.add_namespace(scan_ns)


#!/usr/bin/env python

import time
import sys
import numpy as np
import glob
import skimage
import pika
import cPickle

from skimage.viewer import ImageViewer
from matplotlib import pyplot as plt

subscribe_topic = sys.argv[1]
print('Listening on topic: %s' % subscribe_topic)

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

channel.exchange_declare(exchange=subscribe_topic,
                         exchange_type='x-lvc')

result = channel.queue_declare(exclusive=True, auto_delete=True) # exclusive is required for LVC exchange to work
queue_name = result.method.queue
# queue_name = subscribe_topic

channel.queue_bind(exchange=subscribe_topic,
                   queue=queue_name,
                   routing_key=subscribe_topic)

plt.ion()

def display_image(im):
  im = cPickle.loads(im)
  plt.clf()
  plt.imshow(im)
  plt.pause(0.001)
  plt.show(block=False)

# # CHECK LAST MESSAGE
# method_frame, header_frame, body = channel.basic_get(queue=queue_name)
# print(" [lvc] %s" % method_frame)
# if method_frame:
#   display_image(body)

# LISTEN FOR NEW MESSAGES
def callback(chan, method, properties, body):
  print("\n [x] ")
  display_image(body)
  chan.basic_ack(method.delivery_tag)
  print(" [ack] ")

channel.basic_consume(callback,
                      queue=queue_name,
                      no_ack=False)

print(' Waiting...')
channel.start_consuming()


from __future__ import print_function
########################################
#           CartoonSet dataset         #
# https://google.github.io/cartoonset/ #
########################################
import base64
import csv
import os
import numpy as np
from matplotlib import image as mpimg
from .tfrecords_utils import *
import tensorflow as tf

def get_image_alpha_crop(image_path):
    """Given the path to an image with four channels (eg .png),
        returns the bounding box englobing all its visible pixels."""
    img = mpimg.imread(image_path)
    assert len(img.shape) == 3
    assert img.shape[-1] == 4
    w, h, _ = img.shape
    transparent = np.where(img[:, :, -1] > 0.)
    xmin = np.amin(transparent[0])
    ymin = np.amin(transparent[1])
    xmax = np.amax(transparent[0])
    ymax = np.amax(transparent[1])
    bbox = np.array([xmin / w, ymin / h, xmax / w, ymax / h], dtype=np.float32)
    return img, bbox

"""Define features to be stored in the TFRecords"""
CartoonSetFeatures = Features([
    ('image', FeatureType.BYTES, FeatureLength.FIXED, (), None),
    ('bounding_box', FeatureType.FLOAT, FeatureLength.FIXED, (4,), None),
    ('chin_length', FeatureType.INT, FeatureLength.FIXED, (), None),
    ('eye_angle', FeatureType.INT, FeatureLength.FIXED, (), None),
    ('eye_color', FeatureType.INT, FeatureLength.FIXED, (), None),
    ('eye_eyebrow_distance', FeatureType.INT, FeatureLength.FIXED, (), None),
    ('eye_lashes', FeatureType.INT, FeatureLength.FIXED, (), None),
    ('eye_lid', FeatureType.INT, FeatureLength.FIXED, (), None),
    ('eye_slant', FeatureType.INT, FeatureLength.FIXED, (), None),
    ('eyebrow_shape', FeatureType.INT, FeatureLength.FIXED, (), None),
    ('eyebrow_thickness', FeatureType.INT, FeatureLength.FIXED, (), None),
    ('eyebrow_weight', FeatureType.INT, FeatureLength.FIXED, (), None),
    ('eyebrow_width', FeatureType.INT, FeatureLength.FIXED, (), None),
    ('face_color', FeatureType.INT, FeatureLength.FIXED, (), None),
    ('face_shape', FeatureType.INT, FeatureLength.FIXED, (), None),
    ('facial_hair', FeatureType.INT, FeatureLength.FIXED, (), None),
    ('glasses', FeatureType.INT, FeatureLength.FIXED, (), None),
    ('glasses_color', FeatureType.INT, FeatureLength.FIXED, (), None),
    ('hair', FeatureType.INT, FeatureLength.FIXED, (), None),
    ('hair_color', FeatureType.INT, FeatureLength.FIXED, (), None)
                                ])

class CartoonSetConverter(Converter):
    features = CartoonSetFeatures

    def __init__(self, data_dir):
        """Initialize the object for the CartoonSet dataset in `data_dir`"""
        self.data_dir = data_dir
        self.folders = [x for x in sorted(os.listdir(data_dir)) if x.isdigit()]
        if len(self.folders) == 0:
            self.folders = ['']
        print('Found %d data folders:' % len(self.folders))
        print('\n'.join('  %s' % os.path.join(data_dir, x) for x in self.folders))

    def convert(self,
                tfrecords_path,
                train_split=[0, 1, 2, 3, 4, 5, 6],
                val_split=[7],
                test_split=[8, 9],
                compression_type=None,
                save_image_in_records=False):
        """Convert the dataset in TFRecords saved in the given `tfrecords_path`"""
        # assert sets do not overlap
        set_train_split = set(train_split)
        set_val_split = set(val_split)
        set_test_split = set(test_split)
        assert not len(set_train_split.intersection(set_val_split))
        assert not len(set_train_split.intersection(set_test_split))
        assert not len(set_val_split.intersection(set_test_split))

        # parse
        for name, split in [['train', train_split], ['val', val_split], ['test', test_split]]:
            if not(len(split)):
                print('No split selected for %s data' % name)
                continue
            writer_path = '%s_%s' % (tfrecords_path, name)
            writer = self.init_writer(writer_path, compression_type=compression_type)
            # For each directory
            print('\nLoad', name)
            for i, s in enumerate(split):
                if s >= len(self.folders)  or s < 0:
                    continue

                # for each image
                folder = self.folders[s]
                image_files = [x for x in os.listdir(os.path.join(self.data_dir, folder)) if x.endswith('.png')]
                for j, image_path in enumerate(image_files):
                    print('\r  folder %d/%d: image %d/%d' % (i + 1, len(split), j + 1, len(image_files)), end='')
                    # Image
                    full_image_path = os.path.join(self.data_dir, folder, image_path)
                    if save_image_in_records:
                        img, bbox = get_image_alpha_crop(full_image_path)
                        img = (img[:, :, :3] * 255.).astype(np.uint8).tostring()
                    else:
                        _, bbox = get_image_alpha_crop(full_image_path)
                        img = base64.b64encode(os.path.join(folder, image_path).encode('utf-8'))

                    # Attributes
                    csv_file = os.path.join(self.data_dir, folder, '%s.csv' % image_path.rsplit('.', 1)[0])
                    with open(csv_file, 'r') as f:
                        reader = csv.reader(f)
                        attributes = {row[0]: [int(row[1])] for row in reader}
                        attributes = [attributes[k] for k in sorted(attributes.keys())]

                    # Write
                    writer.write(self.create_example_proto([img],
                                                           bbox.flatten(),
                                                           *attributes))

            # End
            writer.close()
            print('\nWrote %s in file %s (%.2fMB)' % (
                name, writer_path, os.path.getsize(writer_path) / 1e6))

class CartoonSetLoader(Loader):
    features = CartoonSetFeatures
    attributes_values_list = [('chin_length', 3), ('eye_angle', 3), ('eye_color', 5), ('eye_eyebrow_distance', 3),
                              ('eye_lashes', 2), ('eye_lid', 2), ('eye_slant', 3), ('eyebrow_shape', 14),
                              ('eyebrow_thickness', 4), ('eyebrow_weight', 2),  ('eyebrow_width', 3),
                              ('face_color', 11), ('face_shape', 7), ('facial_hair', 15), ('glasses', 12),
                              ('glasses_color', 7), ('hair', 111), ('hair_color', 10)]

    def __init__(self,
                 save_image_in_records=False,
                 image_dir='',
                 crop_images=False,
                 keep_crop_aspect_ratio=True,
                 image_size=None,
                 one_hot_attributes=False,
                 verbose=False):
        """Init a Loader object.

        Args:
            `save_image_in_records` (bool): If True, the image was saved in the record, otherwise only the image path was.
            `image_dir` (str): If save_image_in_records is False, append this string to the image_path saved in the record.
            `crop_images` (bool): If True, the image is cropped to the bounding box (remove transparent pixels)
            `keep_crop_aspect_ratio` (bool): If True, the cropping operation additionally pads the image to preserve
                the original ratio if necessary
            `image_size` (int): If given, resize the image to the given size
            `one_hot_attributes`: If True, the attributes are one-hot encoded, otherwise there are discrete values
        """
        self.save_image_in_records = save_image_in_records
        self.image_dir = image_dir
        self.crop_images = crop_images
        self.keep_crop_aspect_ratio = keep_crop_aspect_ratio
        self.image_size = image_size
        self.one_hot_attributes = one_hot_attributes
        self.verbose = verbose

    def parsing_fn(self, example_proto):
        """tf.data.Dataset parsing function."""
        # Parse
        parsed_features = self.raw_parsing_fn(example_proto)
        # Reshape
        if self.save_image_in_records:
            image = decode_raw_image(parsed_features['image'], (500, 500, 3), image_size=None)
        else:
            filename = tf.decode_base64(parsed_features['image'])
            parsed_features['image_path'] = tf.identity(filename, name='image_path')
            image = decode_relative_image(filename, self.image_dir, image_size=None)
        # Crop image to bounding box
        if self.crop_images:
            bounding_box = parsed_features['bounding_box']
            if self.keep_crop_aspect_ratio:
                bounding_box = make_square_bounding_box(bounding_box, mode='max')
            image = tf.image.crop_and_resize(
                tf.expand_dims(image, axis=0), tf.expand_dims(bounding_box, axis=0), [0], (500, 500))[0]
            del parsed_features['bounding_box']
        # Resize image after cropping
        if self.image_size is not None:
            image = tf.image.resize_images(image, (self.image_size, self.image_size))
        parsed_features['image'] = tf.identity(image, name='image')
        # One-hot encode each attribute
        if self.one_hot_attributes:
            for key, num_values in self.attributes_values_list:
                parsed_features[key] = tf.one_hot(parsed_features[key], num_values, axis=-1, name='one_hot_%s' % key)
        # Return
        if self.verbose: print_records(parsed_features)
        return parsed_features


from flask import Flask, jsonify

app = Flask(__name__)

provinces = [
    {
        'province_id': "1",
        'province': "BALE",
        "chefLieu": "BOROMO",
        "departements": "Boromo, Bagassi, Fara, Pa, Pompoï, Poura, Siby, Oury, Yaho",
        "population": "169 543"
    },
    {
        "province_id": "2",
        "province": "BAM",
        "chefLieu": "Kongoussi",
        "departements": "Kongoussi, Bourzanga, Guibaré, Nasséré, Tikaré, Sabcé, Rollo, Rouko, Zitenga",
        "population": "212 295"
    },
    {
        "province_id": "3",
        "province": "BANWA",
        "chefLieu": "Solenzo",
        "departements": "Solenzo, Balavé, Kouka, Tansila, Sami, Sanaba",
        "population": "214 234"
    },
    {
        "province_id": "4",
        "province": "BAZEGA",
        "chefLieu": "Kombissiri",
        "departements": "Kombissiri, Doulougou, Ipelcé, Gaongo, Kayao,Toécé, Saponé.",
        "population": "214 450"
    },
    {
        "province_id": "5",
        "province": "BOUGOURIBA",
        "chefLieu": "Diébougou",
        "departements": "Diébougou, Dolo, Tiankoura, Bonddigui, Nioroniorro, Oronkua",
        "population": "76 444"
    },
    {
        "province_id": "6",
        "province": "BOULGOU",
        "chefLieu": "Tenkodogo",
        "departements": "Tenkodogo, Bané, Bagré, Béguédo, Bittou, Boussouma, Bissiga, Garango, Komtoéga, Niagho, "
                        "Zabré, Zoaga, Zonsé",
        "population": "415 414"
    },
    {
        "province_id": "7",
        "province": "BOUKIEMDE",
        "chefLieu": "Koudougou",
        "departements": "Koudougou, Bingo, Imasgo, Kindi, Kokologo, Nanoro, Niandiala, Pella, Poa, Ramongo, Sabou, "
                        "Siglé, Sourgou, Thiou, Soaw",
        "population": "421 083"
    },
    {
        "province_id": "8",
        "province": " COMOE",
        "chefLieu": "Banfora",
        "departements": "Banfora, Bérégadougou, Mangodara, Moussodougou, Niangoloko, Ouo, Sidéradougou, "
                        "Soubakaniédougou, Tiéfora",
        "population": "240 942"
    },
    {
        "province_id": "9",
        "province": "GANZOURGOU",
        "chefLieu": "Zorgho",
        "departements": "Zorgho, Boudry, Kogo, Méguet, Mogtédo, Salogo, Zam, Zoungou",
        "population": "257 707"
    },
    {
        "province_id": "10",
        "province": "GNAGNA",
        "chefLieu": "Bogandé",
        "departements": "Bogandé, Coalla, Bilanga, Liptougou, Mani, Pièla, Thion",
        "population": "307 386"
    }
]

@app.route('/')
def index():
    return 'Ma première application REST avec Flask!'

# fonction pour lister toutes les provinces

@app.route('/provinces', methods=['GET'])
def get_province():
    return jsonify({'Provinces': provinces})

# fonction pour lister une province avec son id
@app.route("/provinces/<int:province_id>", methods=['GET'])
def get_province_id(province_id):
    return jsonify({'province': provinces[province_id]})

# Fonction de création d'une province
@app.route("/provinces", methods=['POST'])
def create_province():
    province = {
        "province_id": "11",
        "province": "GOURMA",
        "chefLieu": "Fad N’Gourma",
        "departements": "Fada N’Gourma, Diabo, Diapangou, Matiacoali, Tibga, Yamba",
        "population": "221 956"
    }
    provinces.append(province)
    return jsonify({'Crée': province})

# Fonction de mise à jour d'une province
@app.route("/provinces/<int:province_id>", methods=['PUT'])
def update_province(province_id):
    provinces[province_id]['province'] = "Test"
    return jsonify({'province': provinces[province_id]})

# Fonction de suppression d'une province
@app.route("/provinces/<int:province_id>", methods=['DELETE'])
def delete_province(province_id):
    provinces.remove(provinces[province_id])
    return jsonify({'result': True})

if __name__ == '__main__':
    app.run(debug=True)


from thermo import acentric
import logging
import re
import keywords
import copy
import express

keywords = keywords.all
################################################################
# 1. Acentric Module
#       These perform the acentric module tasks.
################################################################
def casrn(expression, op):
    CASRN = keywords[1][0]
    # Check if omega is in same expression. (keywords 2,0)
    if expression.find(keywords[3][0]) != -1 \
            or expression.find(keywords[5][0]) != -1:
        return expression
    else:
        # Extract the CASRN function:
        casrn = expression.find(CASRN)
        end = keywords[1][2]
        start = keywords[1][1]
        delimeter = keywords[1][3]
        startindex = expression.find(start, casrn)
        endindex = expression.find(end, startindex + 1)
        element = copy.copy(expression[casrn:endindex])
        expression.replace(element, "")

# Kind of a useless function.
def omegacasrn(expression, op):
    # Check for CASRN (keywords 1, 0)
    CASRN = keywords[1][0]
    omegas = op[0]
    if expression.find(str(CASRN)) == -1:
        logging.warning('Error, omega function requires CASRN variable.')
        return expression
    else:
        # Extract the CASRN function:
        casrn = expression.find(CASRN)
        end = keywords[1][2]
        start = keywords[1][1]
        delimeter = keywords[1][3]
        startindex = expression.find(start, casrn)
        endindex = expression.find(end, startindex + 1)
        arg = copy.copy(expression[startindex + 1:endindex])
    # Extract the omega function
    om = expression.find(omegas)
    e = op[2]
    s = op[1]
    d = op[3]
    si = expression.find(s, om)
    ei = expression.find(e, si + 1)
    # Execute the function omega(CASRN='args')
    returnval = acentric.omega(CASRN=arg)
    express = copy.copy(expression[om:ei + 1])
    expression = expression[:om] + str(returnval) + s[ei + 1:]
    return expression

def omegamix(expression, op):
    arg = express.extractargs(expression, op)
    arrays = express.extractarrays(arg)
    if len(arrays) != 2:
        logging.warning("Too Many/Few Arguments for omega_mixture{}")
        return expression
    try:
        return acentric.omega_mixture(arrays[0], arrays[1])
    except:
        return express.cacfailure(expression,op)

def LKomega(expression, op):
    # Get LKomega syntax from csv.
    # Extract Extract args
    arg = express.extractargs(expression, op)
    args = arg.split(op[3])  # 3 Args
    if len(args) != 3:
        return expression
    try:
        return acentric.LK_omega(float(args[0]), float(args[1]), float(args[2]))
    except:
        return express.cacfailure(expression, op)

#Calculation of the stiel polar factor...
def StielPol(expression, op):
    arg = express.extractargs(expression, op)
    casrn = express.extractargs(arg, keywords[1])
    array = re.findall(r"[-+]?\d*\.\d+|\d+", arg)
    if(len(array) != 6):
        logging.warning("Error, Too many/few arguments")
        return expression
    try:
        return acentric.StielPolar(float(array[0]), float(array[1]), float(array[2]), CASRN=casrn)
    except:
        return express.cacfailure(expression, op)


from ..common.common_imports import *
from ..common.files import *
from dataclasses import dataclass
import os
from pathlib import Path
from evdspy.EVDSlocal.messages.error_classes import BucketFromSeriesFolderCreateError
from evdspy.EVDSlocal.config.config import *
from ..initial.start_args import Args
from ..components.options_class import Options, load_options, SingletonOptions
from evdspy.EVDSlocal.initial.start_options import default_data_folder_name
from evdspy.EVDSlocal.common.folder_name_checks import check_remove_back_slash
from dataclasses import dataclass
import os
from pathlib import Path

from evdspy.EVDSlocal.components.api_params import FrequencyEnum, Frequency, AggregationEnum, Aggregations, \
    get_enum_with_value, FormulasEnum, Formulas

# ------------------------------------------------------------------------------
# /*
#               BucketFromSeriesFile
#
# ------------------------------------------------------------------------------
# */
from evdspy.EVDSlocal.components.request_or_cache import RequestOrCacheResultInfo

@dataclass
class BucketFromSeriesFile:
    folder_name: any
    subject: str
    frequency: str
    formulas: str
    aggregateType: str
    prefix: str = Default_Prefix_
    NullType: bool = False
    abs_path: any = None
    xls_name_path: str = "test"
    request_or_cache_resultInfo_: field(default_factory=RequestOrCacheResultInfo) = False

    def __post_init__(self):
        """ Check 2 => after writing avoid \foldername\foldername instead of foldername\foldername
            we previously checked while getting input from user but
            we are checking if user added any absolute path by mistake
        """

        def get_defaults(item, default_value):
            if item == "":
                return default_value
            return item

        # self.frequency = get_defaults(str(self.frequency), default_freq)
        # self.formulas = get_defaults(self.formulas, default_formulas_)
        # self.aggregateType = get_defaults(self.aggregateType, default_AggregateType_)
        from evdspy.EVDSlocal.setup_project.user_series_config import default_freq, default_formulas_, \
            default_AggregateType_

        self.frequency: str = get_defaults(str(self.frequency), default_freq)
        self.frequency = get_enum_with_value(self.frequency, FrequencyEnum, FrequencyEnum.default)

        self.formulas: str = get_defaults(self.formulas, default_formulas_)
        self.formulas: str = get_enum_with_value(self.formulas, FormulasEnum, FormulasEnum.default)

        self.aggregateType = get_defaults(self.aggregateType, default_AggregateType_)
        self.aggregateType: str = get_enum_with_value(self.aggregateType, AggregationEnum, AggregationEnum.default)

        self.folder_name = check_remove_back_slash(self.folder_name)
        self.abs_path = self.get_folder_name(self.folder_name)
        self.xls_name_path = str(Path(self.abs_path) / (self.prefix + '_' + self.subject))

    def __str__(self):
        s = f"""
frequency : {self.frequency}
formulas : {self.formulas}
aggregateType : {self.aggregateType}
folder_name : {self.folder_name}
abs_path : {self.abs_path}
xls_name_path : {self.xls_name_path}

"""
        return s

    def get_folder_name(self, folder_name: str):
        def adapt_folder_name(relative_folder, reduce=False):
            if reduce:
                relative_folder = Path(folder_name).stem
            abs_modified = Path() / default_data_folder_name / relative_folder
            return abs_modified

        if folder_name is None:
            return ""
        if Avoid_Absolute_Paths_ and SingletonOptions().options_.avoid_absolute_paths_user:
            """ Namespace Protection is on """
            r"""
            This is extra protection to prevent mismatching of unwanted paths and populating some files replacing
            old files, if the user is sure absolute paths are correct they can modify
                Avoid_Absolute_Paths_ variable in start_options.py file
                avoids : C:Users\Userx\XdataFolderName
                changes paths to correct form : ...WorkingDirectory..\SeriesData\XdataFolderName
            """
            reduce = False
            if Path(folder_name).is_absolute():
                reduce = True
            return adapt_folder_name(folder_name, reduce=reduce)
        """ Namespace Protection turned off by user """
        print("Avoid_Absolute_Paths_ was cancelled")

        if self.create_folder():
            """ now we will return absolute path if we can successfully create the folder"""
            return self.abs_path
        return adapt_folder_name(self.abs_path, reduce=True)

    def create_folder(self):
        if is_dir(self.abs_path):
            return True
        try:
            os.makedirs(self.abs_path)
            return True
        except:
            if not config.current_mode_is_test:
                print(f"could not create folder {self.abs_path} ")
                # raise BucketFromSeriesFolderCreateError
            return False

from evdspy.EVDSlocal.setup_project.user_series_config import default_freq, default_formulas_, \
    default_AggregateType_

null_BucketFromSeriesFile = BucketFromSeriesFile(folder_name=None,
                                                 subject="test",
                                                 frequency=default_freq,
                                                 formulas=default_formulas_,
                                                 aggregateType=default_AggregateType_,
                                                 prefix="EV_",
                                                 NullType=True)


import sys
import argparse
import logging
import os
import subprocess
from datetime import timedelta
from typing import List
import traceback

# from common import run_command_in_shell, CCM_CLUSTER_IP_PREFIX, CCM_CLUSTER_NODES
from run import Run
from email_sender import create_report, get_driver_origin_remote, send_mail

logging.basicConfig(level=logging.INFO)

class EmptyTestResult(Exception):
    pass

def main(arguments: argparse.Namespace):
    status = 0
    results = dict()
    # TODO: move docker configure to rust-driver-matrix-test.jenkinsfile
    # Start docker configure
    # run_command_in_shell(driver_repo_path=arguments.rust_driver_git,
    #                      cmd=f"pip3 install https://github.com/scylladb/scylla-ccm/archive/master.zip")
    # run_command_in_shell(driver_repo_path=arguments.rust_driver_git,
    #                      cmd=f"ln -s /usr/local/bin/ccm /bin/ccm")
    # run_command_in_shell(driver_repo_path=arguments.rust_driver_git,
    #                      cmd=f"ccm create -i {CCM_CLUSTER_IP_PREFIX}. -n {CCM_CLUSTER_NODES} --scylla -v "
    #                          f"{arguments.scylla_version} scylla-cluster")
    # run_command_in_shell(driver_repo_path=arguments.rust_driver_git,
    #                      cmd=f"ccm start")
    # Finish docker configure

    for driver_version in arguments.versions:
        results[driver_version] = dict()
        for test in arguments.tests:
            logging.info('=== RUST DRIVER VERSION %s. TEST: %s ===', driver_version, test)
            try:
                report = Run(rust_driver_git=arguments.rust_driver_git,
                             tag=driver_version,
                             test=test,
                             scylla_version=arguments.scylla_version).call_test_func()

                if not report:
                    raise EmptyTestResult(f"No result for test '{test}' and driver version {driver_version}")

                logging.info("=== RUST DRIVER MATRIX RESULTS FOR DRIVER VERSION %s ===", driver_version)
                logging.info("\n".join(f"{key}: {value}" for key, value in report.summary.items()))
                if report.is_failed:
                    status = 1
                results[driver_version][test] = report.summary
                results[driver_version][test]['time'] = \
                    str(timedelta(seconds=results[driver_version][test]['testsuite_summary']['time']))[:-3]
            except Exception:
                logging.exception(f"{driver_version} failed")
                status = 1
                exc_type, exc_value, exc_traceback = sys.exc_info()
                results[driver_version] = dict(exception=traceback.format_exception(exc_type, exc_value, exc_traceback))

    if arguments.recipients:
        email_report = create_report(results=results)
        email_report['driver_remote'] = get_driver_origin_remote(arguments.rust_driver_git)
        email_report['status'] = "SUCCESS" if status == 0 else "FAILED"
        send_mail(arguments.recipients, email_report)

    quit(status)

def extract_n_latest_repo_tags(repo_directory: str, major_versions: List[str], latest_tags_size: int = 2) -> List[str]:
    major_versions = sorted(major_versions, key=lambda major_ver: major_ver)
    commands = [f"cd {repo_directory}", "git checkout .", ]
    # if not os.environ.get("DEV_MODE", False):
    #     commands.append("git fetch -p --all")
    commands.append("git tag --sort=-creatordate | grep v0\.")

    selected_tags = {}
    ignore_tags = set()
    result = []
    commands_in_line = "\n".join(commands)
    try:
        lines = subprocess.check_output(commands_in_line, shell=True, stderr=subprocess.STDOUT).decode().splitlines()
    except subprocess.CalledProcessError as e:
        raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))

    for repo_tag in lines:
        if "." in repo_tag:
            version = tuple(repo_tag.split(".", maxsplit=2)[:2])
            if version not in ignore_tags:
                ignore_tags.add(version)
                selected_tags.setdefault(version, []).append(repo_tag)

    for major_version in major_versions:
        if len(selected_tags[major_version]) < latest_tags_size:
            raise ValueError(f"There are no '{latest_tags_size}' different versions that start with the major version"
                             f" '{major_version}'")
        result.extend(selected_tags[major_version][:latest_tags_size])
    return result

def get_arguments() -> argparse.Namespace:
    versions = ['v0.8.2', 'v0.7.0']
    parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('rust_driver_git', help='folder with git repository of rust-driver')
    parser.add_argument('--versions', default=versions,
                        help='rust-driver versions to test, default={}'.format(','.join(versions)))
    parser.add_argument('--tests', choices=['rust', 'serverless', 'tls'], nargs='*', type=str,
                        help='tests to run')
    parser.add_argument('--scylla-version', help="relocatable scylla version to use",
                        default=os.environ.get('SCYLLA_VERSION', None)),
    parser.add_argument('--version-size', help='The number of the latest versions that will test.'
                                               'The version is filtered by the major and minor values.'
                                               'For example, the user selects the 2 latest versions for version 4.'
                                               'The values to be returned are: 4.9.0-scylla-1 and 4.8.0-scylla-0',
                        type=int, default=None, nargs='?')
    parser.add_argument('--recipients', help="whom to send mail at the end of the run",  nargs='+', default=None)
    arguments = parser.parse_args()
    versions = arguments.versions
    if not isinstance(versions, list):
        versions = versions.split(',')

    arguments.versions = versions

    if arguments.version_size:
        arguments.versions = extract_n_latest_repo_tags(repo_directory=arguments.rust_driver_git,
                                                        major_versions=list({tuple(v.split('.', maxsplit=2)[:2]) for v in versions}),
                                                        latest_tags_size=arguments.version_size)

    return arguments

if __name__ == '__main__':
    main(get_arguments())


from typing import List, Optional, Tuple

import torch
from torch import nn

import transformers
from transformers.models.llama.modeling_llama import apply_rotary_pos_emb
import torch
from torch import nn
import torch.nn.functional as F
import math
import transformers
from transformers.models.llama.modeling_llama import apply_rotary_pos_emb

def forward_2(
        self,
        hidden_states: torch.Tensor,
        attention_mask: Optional[torch.Tensor] = None,
        position_ids: Optional[torch.LongTensor] = None,
        past_key_value: Optional[Tuple[torch.Tensor]] = None,
        output_attentions: bool = False,
        use_cache: bool = False,
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
    bsz, q_len, _ = hidden_states.size()

    query_states = self.q_proj(hidden_states).view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
    key_states = self.k_proj(hidden_states).view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
    value_states = self.v_proj(hidden_states).view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)

    kv_seq_len = key_states.shape[-2]
    if past_key_value is not None:
        kv_seq_len += past_key_value[0].shape[-2]
    cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
    query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)

    assert not output_attentions, "output_attentions is not supported"
    assert not use_cache, "use_cache is not supported"
    assert past_key_value is None, "past_key_value is not supported"

    if past_key_value is not None:
        # reuse k, v, self_attention
        key_states = torch.cat([past_key_value[0], key_states], dim=2)
        value_states = torch.cat([past_key_value[1], value_states], dim=2)

    past_key_value = (key_states, value_states) if use_cache else None
    attn_output= F.scaled_dot_product_attention(query_states,key_states,value_states,dropout_p=0.0, is_causal=True)
    attn_weights = None

    if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim):
        raise ValueError(
            f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is"
            f" {attn_output.size()}"
        )

    attn_output = attn_output.transpose(1, 2)
    attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)

    attn_output = self.o_proj(attn_output)

    if not output_attentions:
        attn_weights = None

    return attn_output, attn_weights, past_key_value

def _prepare_decoder_attention_mask(self, attention_mask, input_shape,
                                    inputs_embeds, past_key_values_length):
    # [bsz, seq_len]
    return attention_mask

def replace_llama_attn_with_flash_attn():
    transformers.models.llama.modeling_llama.LlamaModel._prepare_decoder_attention_mask = _prepare_decoder_attention_mask
    transformers.models.llama.modeling_llama.LlamaAttention.forward = forward_2


import os
import json
import time
import yaml
import argparse
from imp import reload

import parse_json
import query as query
import utils.gpt_utils as gu
from gpt_request import gpt_request

def parse_args():
    description = "EVLM_gpt_process"
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument('idx', type=int, help='Index for data entry')
    parser.add_argument('process_path', type=str, default="/PATH/TO/CONFIG", help='path for loading process config')
    return parser.parse_args()

def gpt_process(args):
    idx = args.idx
    process_config = args.process_path
    process_cfg = yaml.load(open(process_config, "r"), Loader=yaml.FullLoader)
    sub_num = process_cfg["process"]["sub_num"]
    log_path = process_cfg["process"]["log_path"]
    data_path = process_cfg["process"]["data_path"]
    task_path = process_cfg["process"]["task_path"]

    with open(task_path) as f:
        data = json.load(f)

    EVLM_name = sorted(list(data))[idx]
    if "train" in data[EVLM_name]['split']: # check if it is training data
        task_data = data[EVLM_name] # all configs for this task
        gpt_task_name = task_data['detailed_name'] # detailed task name for data collection with GPT-4
        save_path = gu.f_mkdir(os.path.join(data_path, gpt_task_name))

        # main task loop
        main_task_flag = False
        subtask_iter = 1
        gpt_query = query.Query()
        for i in range(1, sub_num): # max subtask numbers set to sub_num
            gu.f_mkdir(os.path.join(save_path, f"subtask_{i}")) # pre-build all the subtasks

        while True:
            # make the directory
            sub_save_path = gu.f_mkdir(os.path.join(save_path, f"subtask_{subtask_iter}"))

            # init pipeline for each subtask
            while True:
                data = gu.get_finished_task(log_path)
                if gpt_task_name in data.keys():
                    return 0
                if os.path.exists(os.path.join(sub_save_path, 'task1.json')):
                    break
                time.sleep(1)
            while True:
                statinfo = os.stat(os.path.join(sub_save_path, 'task1.json'))
                if statinfo.st_size > 0:
                    break
            human_info = parse_json.parse_json(path=os.path.join(sub_save_path, "task1.json"))

            data = gu.get_finished_task(log_path)
            if gpt_task_name in data.keys():
                return 0

            # subtask loop, when a subtask is finished, close the loop
            while True:
                system_message = gpt_query.render_system_message()
                human_message = gpt_query.render_human_message(human_info)
                content = system_message.content + "\n\n" + human_message.content
                gu.save_input(sub_save_path, human_message.content)
                print("start query")

                # query process
                succuss = True
                while succuss:
                    try:
                        response = gpt_request(content)
                        succuss = False
                    except Exception as e:
                        print(f"Error: {e}")
                        if "exceeded" in str(e):
                            print("Sleeping for 3 seconds")
                            succuss = True
                            time.sleep(3)
                        else:
                            succuss = True
                            response = {"error_message": str(e)}
                            print(response)

                try:
                    answer = gpt_query.process_ai_message(response)
                except Exception as e:
                    answer = str(e)
                    print(answer)

                gu.save_response(sub_save_path, answer)

                while True:
                    data = gu.get_finished_task(log_path)
                    if gpt_task_name in data.keys():
                        return 0
                    if os.path.exists(os.path.join(sub_save_path, 'feedback.json')):
                        break
                    time.sleep(1)

                while True:
                    feedbackinfo = os.stat(os.path.join(sub_save_path, 'feedback.json'))
                    if feedbackinfo.st_size > 0:
                        break

                data = gu.get_finished_task(log_path)
                if gpt_task_name in data.keys():
                    return 0

                with open(os.path.join(sub_save_path, 'feedback.json')) as f:
                    data = json.load(f)

                main_task_flag = data['main_succeed']
                if data['critic'] == 'succeed':
                    print('Task succeed!')
                    gpt_query.record_history(subtask=data['subtask'], code=data['code'], error=data['error'])
                    break
                else:
                    if data['reset']:
                        gpt_query.record_history(subtask=answer['subtask'], code=answer['code'], error=data['error'])
                        break
                    else:
                        gpt_query.record_history(subtask=answer['subtask'], code=answer['code'], error=data['error'])
                        break

            # reset parameters
            subtask_iter += 1

            #write json
            if subtask_iter > sub_num - 1:
                print(f"already attempt {subtask_iter} time, it is too long!")
                return 0

            if main_task_flag:
                return 0

if __name__ == "__main__":
    args = parse_args()
    gpt_process(args)


from Ui import Ui_LoginWin, Ui_LeaderWin
from PyQt5 import QtWidgets

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    LoginWin = QtWidgets.QMainWindow()
    #LeaderWin=QtWidgets.QMainWindow()

    ui_login = Ui_LoginWin()
    ui_login.setupUi(LoginWin)

    #LeaderWin.show()
    LoginWin.show()
    sys.exit(app.exec_())


from datetime import datetime

from meetings import db

"""
For sake of implementation these are persistency-centered models: they contain all the information but have no method
and perform no "action". All the behavior is coded inside so.called "routes", that act de-facto like controllers.
"""

class Person(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(150), unique=True, nullable=False)
    available = db.Column(db.Boolean, nullable=False, default=lambda: True)

    def to_dict(self):
        return {
            'id': self.id,
            'username': self.username,
            'available': self.available
        }

invitation = db.Table('invitation',
                     db.Column('person_id', db.Integer, db.ForeignKey('person.id'), primary_key=True),
                     db.Column('meeting_id', db.Integer, db.ForeignKey('meeting.id'), primary_key=True)
                     )

class Meeting(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    creation_ts = db.Column(db.DateTime, nullable=False, default=datetime.now)
    start_ts = db.Column(db.DateTime, nullable=True)
    stop_ts = db.Column(db.DateTime, nullable=True)
    presenter_id = db.Column(db.Integer, db.ForeignKey('person.id'), nullable=True)
    status = db.Column(db.String, nullable=False, default='created')

    # relations are handy to retrieve the whole related object/set of related object without querying directly for them
    presenter = db.relationship('Person', primaryjoin='Meeting.presenter_id == Person.id')
    participants = db.relationship('Person', secondary=invitation, lazy='subquery',
                                   backref=db.backref('meetings', lazy=True))

class Event(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    ts = db.Column(db.DateTime, nullable=False, default=datetime.now)
    event_code = db.Column(db.Integer, nullable=False)
    person_id = db.Column(db.Integer, db.ForeignKey('person.id'))
    meeting_id = db.Column(db.Integer, db.ForeignKey('meeting.id'))

    person = db.relationship('Person', primaryjoin='Event.person_id == Person.id')
    meeting = db.relationship('Meeting', primaryjoin='Event.meeting_id == Meeting.id')

    """  for simplicity and extendability no enum but int - there should be a static table with definitions - but for a
         demo hardcoded values are enough.
         0 - meeting created
         1 - meeting start
         2 - meeting finish
         3 - person added
         4 - person present
         5 - person absent
         6 - person invited
         7 - person excluded
         """

def seed(n_users=10):
    """
    Convenience method generating users. Max 49 unique names are available.
    :return:
    """
    from random import randint, random

    if n_users > 49 or n_users <= 0:
        raise ValueError("The number of users to seed should be greater than 0 and lower than 50")

    names = ['Jane', 'Jack', 'John', 'Jill', 'Jade', 'Jim', 'Jes']
    surnames = ['Doe', 'Dam', 'Der', 'Dew', 'Dat', 'Dow', 'Dyl']
    ppl = 0
    while ppl < n_users:
        name = names[randint(0, 6)] + ' ' + surnames[randint(0, 6)]
        if Person.query.filter_by(username=name).first():
            continue
        new_user = Person(username=name, available=(random() < 0.7))
        db.session.add(new_user)
        db.session.commit()
        ppl += 1


import sys, heapq
import numpy as np
import pymysql as mdb
import json
from graph_to_json import graphToDict

NULL_HASH = 64*'0'

class PriorityDict():
	def __init__(self):
		self.heap = [] # for keys of self.elements
		self.elements = {}
	def add(self,element):
		priority = element.blockNumber
		key = (element.transactionHash,element.outputIndex)
		if key not in self.elements:
			heapq.heappush(self.heap,(priority,key))
			self.elements[key] = element
	def pop(self):
		priority,key = heapq.heappop(self.heap)
		return self.elements[key]
	def __len__(self):
		return len(self.heap)
	def __getitem__(self,output):
		return self.elements[(output.transactionHash,output.outputIndex)]

class Output:
	def __init__(self,parameters,taint=0):
		self.amount = 0.0
		self.taint = 0.0
		self.blockNumber = parameters[0]
		self.outputIndex = parameters[1]
		self.spendHash = parameters[2]
		self.transactionHash = parameters[3]
		self.value = parameters[4]
	def __hash__(self):
		return hash((self.outputIndex,self.transactionHash))
	def __eq__(self,other):
		return hash(self) == hash(other)

con = mdb.connect('localhost','root','','bitcoin')
cur = con.cursor()

def getTxInputValue(transaction_hash):
	command = 'SELECT SUM(value) FROM outputs WHERE spendHash = "%s"'
	command = command % (transaction_hash)
	cur.execute(command)
	return cur.fetchone()[0]

def getTxOutputValue(transaction_hash):
	command = 'SELECT SUM(value) FROM outputs WHERE transactionHash = "%s"'
	command = command % (transaction_hash)
	cur.execute(command)
	return cur.fetchone()[0]

def getSourceOutputs(source_address):
	command = 'SELECT blockNumber, outputIndex, spendHash, transactionHash, value '
	command += 'FROM outputs WHERE address = "%s"' %source_address
	cur.execute(command)
	sources = PriorityDict()
	for entry in cur.fetchall():
		output = Output(entry)
		output.amount = output.value
		output.taint = 1.0
		sources.add(output)
	return sources

def getLinkedOutputs(output):
	command = 'SELECT blockNumber,outputIndex,spendHash,transactionHash,value '
	command += 'FROM outputs WHERE transactionHash = "%s"'
	command = command % output.spendHash
	cur.execute(command)
	return [Output(entry) for entry in cur.fetchall()]

#try:
address = sys.argv[1]
#except IndexError:
#	address = '1eHhgW6vquBYhwMPhQ668HPjxTtpvZGPC'#'17SkEw2md5avVNyYgj6RiXuQKNwkXaxFyQ'
	#address = '17SkEw2md5avVNyYgj6RiXuQKNwkXaxFyQ'

tainteds = getSourceOutputs(address)

total_amount = 0
for tainted in tainteds.elements.values():
	tainteds[tainted].amount = tainted.value
	total_amount += tainted.value
unspent_outputs = []

graph = []

while len(tainteds) > 0:
	output = tainteds.pop()
	if output.amount < total_amount / 100 or output.amount / output.value < .1:
		continue
	elif output.spendHash == NULL_HASH:
		unspent_outputs.append(output)
	else:
		entry = [output.transactionHash]
		entry.append(output.spendHash)
		entry.append(output.amount)
		entry.append(output.amount/output.value)
		graph.append(entry)
		txOutputValue = float(getTxOutputValue(output.spendHash))
		for linked_output in getLinkedOutputs(output):
			tainteds.add(linked_output)
		for linked_output in getLinkedOutputs(output):
			weight = linked_output.value / txOutputValue
			increment = weight * tainteds[output].amount
			tainteds[linked_output].amount += increment

json_output = open('/Users/mason/Desktop/app/static/data/output.json','w')
json.dump(graphToDict(graph),json_output)


from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton

choice = InlineKeyboardMarkup(row_width=2)

film_100 = InlineKeyboardButton(text="Топ 100 фильмов от Кинопоиска", url='https://www.kinopoisk.ru/lists/movies/top_100_by_total_film/?utm_referrer=yandex.ru')
film_fav = InlineKeyboardButton(text="Любимый фильм создателя бота", url='https://www.kinopoisk.ru/film/45185/')
cancel_button = InlineKeyboardButton(text="Отмена", callback_data="cancel")

choice.insert(film_100)
choice.insert(film_fav)
choice.insert(cancel_button)


import sys
import json
import base64
import os
import math
from shutil import copyfile

if len(sys.argv) > 2:
    input_path = sys.argv[1]
    out_path = sys.argv[2]
    try:
        jsonFile = open(input_path,'r')
        values = json.load(jsonFile)
        assets= values["assets"]

        #create train and test dir in outpath
        train_dir = out_path + "/train"
        test_dir = out_path + "/test"

        #get training index
        training_index = math.ceil(75 * ( len(assets) /100 ))
        print("Training IndeX: ",training_index , " - ",len(assets))
        try:
            if not os.path.exists(out_path):
                os.mkdir(out_path)
            os.mkdir(train_dir)
            os.mkdir(test_dir)
        except OSError:
            print ("Creation of the directory %s failed" % out_path)

        for key,value in enumerate(assets):
            data = {}
            info = assets[value]["asset"]
            regions = assets[value]["regions"]
            imagePath = info["path"].replace("file:","")

            if len(regions) > 0:

                #Create each image jsonfile

                data["version"] = "4.5.6"
                data["flags"] = {}
                data["shapes"] = []
                data["imagePath"] = info["name"]
                data["imageHeight"] = info["size"]["width"]
                data["imageWidth"] = info["size"]["height"]

                #set Region
                for region in regions:
                    region_item = {}
                    region_item["points"] = []
                    region_item["label"] =   region["tags"][0] if len(region["tags"]) > 0 else ""
                    region_item["group_id"] = None
                    region_item["shape_type"] = "polygon"
                    region_item["flags"] = {}

                    for point in region["points"]:
                        item = [point["x"],point["y"]]
                        region_item["points"].append(item)

                    data["shapes"].append(region_item)
                #image Data
                with open(imagePath, "rb") as image_file:
                    encoded_string = base64.b64encode(image_file.read())
                    imageData = encoded_string.decode("utf-8")
                    data["imageData"] = imageData

                #write json file
                fileName = ""
                if key < training_index:
                    if os.path.exists(train_dir):
                        fileName = train_dir + "/" + info["name"].replace(".jpg",".json")
                        copyImageFileName = train_dir + "/" + info["name"]
                        copyfile(imagePath,copyImageFileName)
                else:
                    if os.path.exists(test_dir):
                        fileName = test_dir + "/" + info["name"].replace(".jpg",".json")
                        copyImageFileName = test_dir + "/" + info["name"]
                        copyfile(imagePath,copyImageFileName)

                # Serializing json
                json_object = json.dumps(data, indent = 4)
                with open(fileName, "w") as outfile:
                    outfile.write(json_object)
                print("Write To ",fileName)

        jsonFile.close()
    except IOError:
        print("Error occured when opeing file or json parse error.")

else:
    print("Set Input and Output Path.")


from distutils.log import debug
from flask import Flask, render_template, url_for
app = Flask(__name__)

posts = [
    {
        'author' : 'Akshit Chugh',
        'title' : 'Blog Post One',
        'content' : 'I detect little communism',
        'date_posted' : 'April 17, 2022'
    },
    {
        'author' : 'John Doe',
        'title' : 'Blog Post Two',
        'content' : 'Ain\'t he right?',
        'date_posted' : 'April 18, 2022'
    }

]

@app.route("/")
@app.route("/home")
def home():
    return render_template('home.html', posts =  posts)

@app.route("/about")
def about():
    return render_template('about.html', title = 'About')

if __name__ == '__main__':
    app.run(debug = True)


from os.path import expanduser
from pathlib import Path
from typing import Optional, Union

from dotenv import dotenv_values, find_dotenv

def config_to_bool(config_value: Optional[str]):
    """Turn a config value into a bool."""
    if not config_value or config_value.lower() in ["false", "0", "no"]:
        return False
    else:
        assert config_value.lower() in [
            "true",
            1,
            "yes",
        ], f"Invalid boolean config value: {config_value}"
        return True

def load_config():
    """Load the configuration files and convert known values"""
    config_dict: dict[str, Union[None, str, int, Path]] = {
        **dotenv_values(find_dotenv(".env")),
        **dotenv_values(find_dotenv(".env.local")),
    }

    bool_config_values = ["remove_duplicate_bi_encoder_results"]
    integer_config_values = [
        "num_processes",
        "top_k",
        "batch_size",
        "max_paragraphs_per_page",
        "max_sentences_per_paragraph",
    ]
    path_config_values = ["data_path", "crawl_result_file"]

    for key in bool_config_values:
        config_dict[key] = config_to_bool(config_dict[key])
    for key in integer_config_values:
        config_dict[key] = int(config_dict[key])
    for key in path_config_values:
        config_dict[key] = Path(expanduser(config_dict[key]))
    return config_dict


import json
import urllib.parse
import boto3
import os

def lambda_handler(event, context):
    # Get the bucket name and file/key name
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')

    try:

        # print(json.dumps(event))

        # Read the file
        print('############# Loading function ################')
        s3 = boto3.client('s3')
        response = s3.get_object(Bucket=bucket, Key=key)

        text = response["Body"].read().decode()
        print('############# Body in the file ################')
        print(text)

        print('############# Reversed words ################')
        body = text.split()
        for i in range(len(body)):
            body[i] = body[i][::-1]

        # creating a string from the list
        reversedWords = (" ").join(body)
        print(reversedWords)

        finalWrite = '############# Loading function ################\n' + \
                     '############# Body in the file ################\n' + text + \
                     '\n############# Reversed words   ################\n' + reversedWords + \
                     '\n############# End of filed     ################\n'

        # writing to the tmp storage
        with open("/tmp/output.txt", "w") as myfile:
            myfile.write(finalWrite)

        # check if the output file got created
        print(os.listdir('/tmp/'))

        # Reading the output file
        with open("/tmp/output.txt", "r") as myfile:
            response = myfile.read()
            print(response)

        return 'Success!'

    except Exception as e:
        print(e)
        raise e


# V0

# V1
# https://blog.csdn.net/danspace1/article/details/87854254
# http://bookshadow.com/weblog/2016/11/13/leetcode-4sum-ii/
class Solution(object):
    def fourSumCount(self, A, B, C, D):
        """
        :type A: List[int]
        :type B: List[int]
        :type C: List[int]
        :type D: List[int]
        :rtype: int
        """
        ans = 0
        cnt = collections.defaultdict(int)
        for a in A:
            for b in B:
                cnt[a + b] += 1
        for c in C:
            for d in D:
                ans += cnt[-(c + d)]
        return ans

# V1'
# https://www.jiuzhang.com/solution/4sum-ii/#tag-highlight-lang-python
class Solution:
    """
    @param A: a list
    @param B: a list
    @param C: a list
    @param D: a list
    @return: how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero
    """
    def fourSumCount(self, A, B, C, D):
        # Write your code here
        map = {}
        ans = 0
        for i in range(len(A)):
            for j in range(len(B)):
                sum = A[i] + B[j]
                if sum in map:
                    map[sum] += 1
                else:
                    map[sum] = 1
        for i in range(len(C)):
            for j in range(len(D)):
                sum = -(C[i] + D[j])
                if sum in map:
                    ans += map[sum]
        return ans

# V2
# Time:  O(n^2)
# Space: O(n^2)
import collections
class Solution(object):
    def fourSumCount(self, A, B, C, D):
        """
        :type A: List[int]
        :type B: List[int]
        :type C: List[int]
        :type D: List[int]
        :rtype: int
        """
        A_B_sum = collections.Counter(a+b for a in A for b in B)
        return sum(A_B_sum[-c-d] for c in C for d in D)


from django.db import models

# Create your models here.
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.forms import ModelForm
from tinymce.models import HTMLField

class Headquarter(models.Model):
    """
    Model for School grads
    """

    # Fields
    name = models.CharField(
        max_length=30,
        verbose_name="نام ستاد",
    )

    # Metadata
    class Meta:
        verbose_name = 'نام ستاد'
        verbose_name_plural = 'نام ستاد ها'

    # Methods
    def __str__(self):
        return self.name

# Major Section
class Queue(models.Model):
    """
    Model for School major
    """

    # Fields
    name = models.CharField(
        max_length=30,
        verbose_name="نام صف",
    )

    # Metadata
    class Meta:
        verbose_name = 'صف'
        verbose_name_plural = 'صف ها'

    # Methods
    def __str__(self):
        return self.name

class Manager(models.Model):
    """
    Model for administrator classes
    """

    Manager_STATUS = (
        ('فعال', 'فعال'),
        ('غیر فعال', 'غیر فعال'),
    )

    # Fields
    persian_name = models.CharField(
        max_length=35,
        verbose_name="نام مدیریت",
    )
    code = models.CharField(
        max_length=10,
        null=False,
        blank=False,
        verbose_name="کد مدیریت",
    )
    english_name = models.CharField(
        max_length=35,
        verbose_name="نام انگلیسی مدیریت",
    )

    order = models.IntegerField(
        max_length=35,
        verbose_name="ترتیب",
    )

    manager_status = models.CharField(
        choices=Manager_STATUS,
        max_length=15,
        null=True,
        blank=True,
        verbose_name="وضعیت مدیر",
    )

    queue = models.ForeignKey(
        Queue,
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="queue",
        verbose_name="صف",
    )
    headquarter = models.ForeignKey(
        Headquarter,
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="headquarter",
        verbose_name="ستاد",
    )

    # Metadata
    class Meta:
        verbose_name = 'مدیریت'
        verbose_name_plural = 'مدیران'

    # Methods
    def __str__(self):
        return self.persian_name

class Company(models.Model):
    """
    Model for company classes
    """

    Company_STATUS = (
        ('فعال', 'فعال'),
        ('غیر فعال', 'غیر فعال'),
    )

    # Fields
    title = models.CharField(
        max_length=35,
        verbose_name="نام شرکت",
    )
    code = models.CharField(
        max_length=10,
        null=False,
        blank=False,
        verbose_name="کد شرکت",
    )
    english_title = models.CharField(
        max_length=35,
        verbose_name="نام انگلیسی شرکت",
    )

    company_status = models.CharField(
        choices=Company_STATUS,
        max_length=15,
        null=True,
        blank=True,
        verbose_name="وضعیت شرکت",
    )

    content = HTMLField(blank=True, null=True)

    # Metadata
    class Meta:
        verbose_name = 'شرکت'
        verbose_name_plural = 'شرکت ها'

    # Methods
    def __str__(self):
        return self.title

class Department(models.Model):
    """
    Model for department classes
    """

    Department_STATUS = (
        ('فعال', 'فعال'),
        ('غیر فعال', 'غیر فعال'),
    )

    Department_TYPE = (
        ('صف', 'صف'),
        ('ستاد', 'ستاد'),
    )

    # Fields
    title = models.CharField(
        max_length=35,
        verbose_name="نام معاونت",
    )
    code = models.CharField(
        max_length=10,
        null=False,
        blank=False,
        verbose_name="کد معاونت",
    )
    english_title = models.CharField(
        max_length=35,
        verbose_name="نام انگلیسی معاونت",
    )

    department_status = models.CharField(
        choices=Department_STATUS,
        max_length=15,
        null=True,
        blank=True,
        verbose_name="وضعیت معاونت",
    )

    department_type = models.CharField(
        choices=Department_TYPE,
        max_length=15,
        null=True,
        blank=True,
        verbose_name="نوع معاونت",
    )

    order = models.IntegerField(
        max_length=35,
        verbose_name="ترتیب معاونت",
    )

    company = models.ForeignKey(Company, on_delete=models.CASCADE,blank=True,null=True)

    # Metadata
    class Meta:
        verbose_name = 'معاونت'
        verbose_name_plural = 'معاونت ها'

    # Methods
    def __str__(self):
        return self.title

class OU(models.Model):
    """
    Model for OU classes
    """

    OU_STATUS = (
        ('فعال', 'فعال'),
        ('غیر فعال', 'غیر فعال'),
    )

    # Fields
    title = models.CharField(
        max_length=35,
        verbose_name="نام واحد سازمانی",
    )
    code = models.CharField(
        max_length=10,
        null=False,
        blank=False,
        verbose_name="کد واحد سازمانی",
    )
    english_title = models.CharField(
        max_length=35,
        verbose_name="نام انگلیسی واحد سازمانی",
    )

    ou_status = models.CharField(
        choices=OU_STATUS,
        max_length=15,
        null=True,
        blank=True,
        verbose_name="وضعیت واحد سازمانی",
    )

    order = models.IntegerField(
        max_length=35,
        verbose_name="ترتیب واخد سازمانی",
    )

    department = models.ForeignKey(Department, on_delete=models.CASCADE, blank=True, null=True)

    # Metadata
    class Meta:
        verbose_name = 'واحد سازمانی'
        verbose_name_plural = 'واحد های سازمانی'

    # Methods
    def __str__(self):
        return self.title

class Level(models.Model):
    """
    Model for level classes
    """

    Position_STATUS = (
        ('فعال', 'فعال'),
        ('غیر فعال', 'غیر فعال'),
    )

    # Fields
    title = models.CharField(
        max_length=35,
        verbose_name="نام دسته سمت",
    )
    code = models.CharField(
        max_length=10,
        null=False,
        blank=False,
        verbose_name="کد دسته سمت",
    )

    english_title = models.CharField(
        max_length=35,
        verbose_name="نام انگلیسی دسته سمت",
    )

    position_status = models.CharField(
        choices=Position_STATUS,
        max_length=15,
        null=True,
        blank=True,
        verbose_name="وضعیت دسته سمت",
    )

    grade = models.IntegerField(
        max_length=35,
        verbose_name="ترتیب دسته سمت",
    )

    # Metadata
    class Meta:
        verbose_name = 'دسته سمت'
        verbose_name_plural = 'دسته های سمت'

    # Methods
    def __str__(self):
        return self.title

class Position(models.Model):
    """
    Model for position classes
    """

    Position_STATUS = (
        ('فعال', 'فعال'),
        ('غیر فعال', 'غیر فعال'),
    )

    Position_TYPE = (
        ('ساختاری', 'ساختاری'),
        ('غیر ساختاری', 'غیر ساختاری'),
    )

    # Fields
    title = models.CharField(
        max_length=35,
        verbose_name="نام سمت",
    )
    code = models.CharField(
        max_length=10,
        null=False,
        blank=False,
        verbose_name="کد سمت",
    )

    serial = models.CharField(
        max_length=10,
        null=False,
        blank=False,
        verbose_name="سریال سمت",
    )

    english_title = models.CharField(
        max_length=35,
        verbose_name="نام انگلیسی سمت",
    )

    position_status = models.CharField(
        choices=Position_STATUS,
        max_length=15,
        null=True,
        blank=True,
        verbose_name="وضعیت سمت",
    )

    position_type = models.CharField(
        choices=Position_TYPE,
        max_length=15,
        null=True,
        blank=True,
        verbose_name="نوع سمت",
    )

    order = models.IntegerField(
        max_length=35,
        verbose_name="ترتیب سمت",
    )

    ou = models.ForeignKey(OU, on_delete=models.CASCADE, blank=True, null=True)
    report_to = models.ForeignKey('self', on_delete=models.CASCADE, blank=True, null=True)
    deputy = models.ForeignKey(Department, on_delete=models.CASCADE, blank=True, null=True)
    level = models.ForeignKey(Level, on_delete=models.CASCADE, blank=True, null=True)

    # Metadata
    class Meta:
        verbose_name = 'سمت'
        verbose_name_plural = 'سمت ها'

    # Methods
    def __str__(self):
        return self.title


'''
从有道词典下载更多例句
'''
from urllib import request
import json, re
import requests
from lxml import etree
import time

class Youdao:
    def __init__(self):
        self.url_1 = 'http://fanyi.youdao.com/openapi.do'
        self.key = '993123434'  #有道API key
        self.keyfrom = 'pdblog' #有道keyfrom

        self.headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    # "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36",
    "Connection": "keep-alive",
    "referer": "http://dict.youdao.com/"
}
        self.url_2 = 'http://dict.youdao.com/w/eng/****/#keyfrom=dict2.index'

    def get_url_1(self, word):
        url = self.url_1 + '?keyfrom=' + self.keyfrom + '&key='+self.key + '&type=data&doctype=json&version=1.1&q=' + word
        result = request.urlopen(url).read().decode('utf-8')
        self.res = json.loads(result)
        cadidates = []
        try:
            for dic in self.res['web']:
                if word in dic['key']:
                    s = dic['key'].replace(word, '****').lower()
                    s = s.replace('****', word)
                    cadidates.append(s)
        except:
            cadidates = []
        return cadidates

    def get_url_2(self, word):
        url = self.url_2.replace('****', word)
        response = requests.get(url=url, headers=self.headers)
        selector = etree.HTML(response.text)
        cadidates = []
        try:
            for n in ['1','2','3']:
                content = selector.xpath('//*[@id="bilingual"]/ul/li[%s]/p[1]'%n)
                if content:
                    sentence = []
                    for i in content[0]:
                        sentence.append(i.xpath('string(.)'))
                    if word in ' '.join(sentence):
                        s = ' '.join(sentence).replace(word, '****')
                        s = re.sub('t[ ’\']+s', 'ts', s)
                        s = re.sub('["…‘“”,.:?]', ' ', s)
                        s = ' '.join(s.lower().split()).replace('****', word)
                        cadidates.append(s)
        except:
            cadidates = []
        return cadidates

if __name__ == '__main__':
    youdao = Youdao()
    with open('./tmp_data/word_dict.json', encoding='utf-8') as f:
        vocab_dict = json.load(f)
    with open('./tmp_data/wordnet', encoding='utf-8') as f:
        wordnet = json.load(f)
    for w in vocab_dict:
        if w in wordnet and len(wordnet[w]["examples"])<=3:
            print(w)
            downloads = youdao.get_url_1(w)+youdao.get_url_2(w)
            examples = []
            try:
                for sent in downloads:
                    sent += ' <SEP>'
                    if len(sent.split())>2:
                        idx = sent.split().index(w)
                        examples.append([sent, idx])
                wordnet[w]["examples"].extend(examples)
            except:
                examples = []
            print(examples)
            time.sleep(3)
    with open('./tmp_data/wordnet', 'w', encoding='utf-8') as f:
        json.dump(wordnet, f, indent=2)


from django.db.models import Q
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from django.contrib.auth import get_user_model

from .models import Trip, User
from .serializers import LogInSerializer, NestedTripSerializer, DeleteTripSerializer, UserSerializer

# Access user info from request.user. Example: request.user.group, request.user.id

@api_view(['POST'])
def signUpUser(request):
    serializer = UserSerializer(data=request.data)
    if serializer.is_valid(raise_exception=True):
        serializer.save()
        return(Response(status=200))
    return(Response(status=400))

@api_view(['POST'])
def loginUser(request):
    #Serialize the data that was sent.
    serializer = LogInSerializer(data=request.data)
    #The serializer will verify if the user exists or not and if the data sent was valid.
    if serializer.is_valid(raise_exception=True):
        token = serializer.validated_data
        #If the data is valid the serializer will create a token that we then send back to the client that stores the users data.
        return(Response(token, status=200))
    return(Response(status=400))

@permission_classes([IsAuthenticated])
@api_view(['GET'])
def getTrips(request):
    user = request.user
    #We first check if the user exists.
    if user.id == None:
        return(Response(status=200))
    #If they do exist then check if they are a driver or a rider.
    if user.group == 'driver':
        #If they are a driver then return all the trips where they are the driver or the status of the trip is REQUESTED.
        qs = Trip.objects.filter(Q(status=Trip.REQUESTED) | Q(driver=user))
        #Serialize the data and send back to the user.
        serializer = NestedTripSerializer(qs, many=True)
        return(Response(serializer.data, status=200))
    if user.group == 'rider':
        #If they are a rider then send back all the trips where they are the rider.
        qs = Trip.objects.filter(rider=user)
        #Serialize the data and send back.
        serializer = NestedTripSerializer(qs, many=True)
        return(Response(serializer.data, status=200))
    return(Response(status=400))

@permission_classes([IsAuthenticated])
@api_view(['GET'])
def getTrip(request, trip_id):
    #We first extract the trip id from the url and access it through trip_id
    qs = Trip.objects.filter(id=trip_id)
    #If that id does not exist then return a 404 error
    if not qs.exists():
        return Response(status=404)
    #If it does then serialize the trip data and send back to the user.
    #We need to specify qs.first() because qs is an array. We will get an error if we don't pick out an element from that array.
    #So we pick out one element and send that to be serialized and then we send the data to the user.
    serializer = NestedTripSerializer(qs.first())
    return Response(serializer.data, status=200)

@permission_classes([IsAuthenticated])
@api_view(['DELETE'])
def deleteTrip(request, trip_id, *args, **kwargs):
    #Get the user data that submitted the request
    user = request.user
    #Get the trip from the database only if the requester is the one that created the trip
    qs = Trip.objects.filter(Q(id=trip_id) & (Q(rider=user.id)))
    if not qs.exists():
        return(Response({"Trip cannot be found"}, status=404))
    #If the user is authorized then move on to verify that the submitted data is correct.
    serializer = DeleteTripSerializer(data=request.data)
    #Check if the submitted data is in the format that is required.
    if serializer.is_valid(raise_exception=True):
        data = serializer.validated_data
        status = data.get('status')
        #Last step is to check that the status of the trip is COMPLETED.
        if status != 'COMPLETED':
            return(Response({"Cannot remove uncompleted trips"}, status=403))
        #Delete the trip and send a 200 response
        qs.delete()
        return(Response({"Trip removed"}, status=200))
    return(Response(status=400))


import tkinter as tk #imports tkinter to create GUI
from tkinter import * #imports everything in tkinter
from datetime import date #imports date so it can be added to shipment log
today = date.today()
d = today.strftime("%b-%d-%Y") #creates a variable of the date from the system in Jan-1-2020 format

def chart(pieces,order): #creates a chart for conversions based on piece count
    Names = [] #creates a list to hold variable names
    count = 1
    counter1 = 1
    counter2 = 0
    pieces2 = pieces
    for x in range(pieces*4): #creates the list of variable names neeeded to create the chart
        Names += ["e"+str(count)]
        count = count + 1
    def convertustometric():#function to take user input and convert it from US to metric. It displays the conversions and writes it to the shipment log
        counter3 = 0
        counter4 = 1
        counter5 = 0
        Names2 = []
        for x in range(pieces):
            Names2 += [float(Names[0+counter3].get()) * .0254]
            Names2 += [float(Names[1+counter3].get()) * .0254]
            Names2 += [float(Names[2+counter3].get()) * .0254]
            Names2 += [float(Names[3+counter3].get()) * .45359237]
            file1 = open("shipment_log.txt","a")
            file1.write("Order Number {} Date {} Piece {} measures {} x {} x {} meters and {} kgs\n".format(order,d,counter4,Names2[0+counter3],Names2[1+counter3],Names2[2+counter3],Names2[3+counter3]))
            file1.close()
            counter3 = counter3 + 4
            counter4 = counter4 + 1
        for x in range(pieces):
            Label(Converter,text="{} meters".format(Names2[0+counter5])).grid(row=3 + counter5,column=2)
            Label(Converter,text="{} meters".format(Names2[1+counter5])).grid(row=4 + counter5,column=2)
            Label(Converter,text="{} meters".format(Names2[2+counter5])).grid(row=5 + counter5,column=2)
            Label(Converter,text="{} KG".format(Names2[3+counter5])).grid(row=6 + counter5,column=2)
            counter5 = counter5 + 4

    def convertmetrictous():#function to take user input and convert it from metric to US. It displays the conversions and writes it to the shipment log
        counter6 = 0
        counter7 = 1
        counter8 = 0
        Names3 = []
        for x in range(pieces):
            Names3 += [float(Names[0+counter6].get()) * 39.37]
            Names3 += [float(Names[1+counter6].get()) * 39.37]
            Names3 += [float(Names[2+counter6].get()) * 39.37]
            Names3 += [float(Names[3+counter6].get()) * 2.205]
            file1 = open("shipment_log.txt","a")
            file1.write("Order Number {} Date {} Piece {} measures {} x {} x {} meters and {} kgs\n".format(order,d,counter7,Names3[0+counter6],Names3[1+counter6],Names3[2+counter6],Names3[3+counter6]))
            file1.close()
            counter6 = counter6 + 4
            counter7 = counter7 + 1
        for x in range(pieces):
            Label(Converter,text="{} meters".format(Names3[0+counter8])).grid(row=3 + counter8,column=2)
            Label(Converter,text="{} meters".format(Names3[1+counter8])).grid(row=4 + counter8,column=2)
            Label(Converter,text="{} meters".format(Names3[2+counter8])).grid(row=5 + counter8,column=2)
            Label(Converter,text="{} KG".format(Names3[3+counter8])).grid(row=6 + counter8,column=2)
            counter8 = counter8 + 4

    while pieces2 > 0:#creates the chart to enter measurmentrs based on piece count. Uses list created earlier so each text entry box has a unique name.
        Label(Converter,text="piece {} length".format(counter1)).grid(row=3 + counter2,column=0)
        Label(Converter,text="piece {} width".format(counter1)).grid(row=4 + counter2,column=0)
        Label(Converter,text="piece {} heigth".format(counter1)).grid(row=5 + counter2,column=0)
        Label(Converter,text="piece {} weigth".format(counter1)).grid(row=6 + counter2,column=0)
        length= DoubleVar()
        width= DoubleVar()
        heigth= DoubleVar()
        weigth= DoubleVar()
        Names[0+counter2] = Entry(Converter,textvariable=length)
        Names[1+counter2] = Entry(Converter,textvariable=width)
        Names[2+counter2] = Entry(Converter,textvariable=heigth)
        Names[3+counter2] = Entry(Converter,textvariable=weigth)
        Names[0+counter2].grid(row=3 + counter2,column=1)
        Names[1+counter2].grid(row=4 + counter2,column=1)
        Names[2+counter2].grid(row=5 + counter2,column=1)
        Names[3+counter2].grid(row=6 + counter2,column=1)
        counter1 = counter1 + 1
        counter2 = counter2 + 4
        pieces2 = pieces2 - 1
        ustometric = Button(Converter,text="Convert US to Metric",command= lambda: convertustometric()).grid(row=2,column=2)
        metrictous = Button(Converter,text="Convert Metric to US",command= lambda: convertmetrictous()).grid(row=2,column=0)
    return;

Converter=tk.Tk() #creates the main window
Converter.title("Measurement Converter") #labels the main window
x=Label(Converter,text="Order Number").grid(row=0) #creates a label for the ordernumber entry box and places it
y=Label(Converter,text="Number of pieces").grid(row=1) #creates a label for the pieces entry box and places it
order_number = IntVar() #creates a variable type for the order number
pieces = IntVar() #creates a variable type for the piece count
entry1 = Entry(Converter,textvariable=order_number) #creates a text entry box that corresponds to the order number variable
entry2 = Entry(Converter,textvariable=pieces) #creates a text entry box that corresponds to the piece count
entry1.grid(row=0,column=1) #places the order number entry box
entry2.grid(row=1,column=1) #places the piece count entry box

next = Button(Converter,text="next", command= lambda: chart(int(pieces.get()),int(order_number.get()))).grid(row=2,column=1)
#this is a button that calls the chart funtion when clicked
Converter.mainloop()


"""Support for LG WebOS TV notification service."""
import logging

from aiowebostv import WebOsTvPairError

from homeassistant.components.notify import ATTR_DATA, BaseNotificationService
from homeassistant.const import CONF_ICON, CONF_NAME

from .const import ATTR_CONFIG_ENTRY_ID, DATA_CONFIG_ENTRY, DOMAIN, WEBOSTV_EXCEPTIONS

_LOGGER = logging.getLogger(__name__)

async def async_get_service(hass, _config, discovery_info=None):
    """Return the notify service."""

    if discovery_info is None:
        return None

    name = discovery_info.get(CONF_NAME)
    client = hass.data[DOMAIN][DATA_CONFIG_ENTRY][
        discovery_info[ATTR_CONFIG_ENTRY_ID]
    ].client

    return LgWebOSNotificationService(client, name)

class LgWebOSNotificationService(BaseNotificationService):
    """Implement the notification service for LG WebOS TV."""

    def __init__(self, client, name):
        """Initialize the service."""
        self._name = name
        self._client = client

    async def async_send_message(self, message="", **kwargs):
        """Send a message to the tv."""
        try:
            if not self._client.is_connected():
                await self._client.connect()

            data = kwargs.get(ATTR_DATA)
            icon_path = data.get(CONF_ICON, "") if data else None
            await self._client.send_message(message, icon_path=icon_path)
        except WebOsTvPairError:
            _LOGGER.error("Pairing with TV failed")
        except FileNotFoundError:
            _LOGGER.error("Icon %s not found", icon_path)
        except WEBOSTV_EXCEPTIONS:
            _LOGGER.error("TV unreachable")


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

import click

from nettool.nettest import NetTest as ntest

class ArgumentValidation(object):
    @classmethod
    def _type_validation(cls, validation_type, value):
        try:
            getattr(ntest.validate, validation_type)(value, raise_exception=True)
        except ValueError as e:
            error = unicode(e)
            raise click.BadParameter(error)
        return value

    @classmethod
    def _format_error(cls, error, valid_options):
        if valid_options:
            valid = u'\n\t\t'.join(valid_options)
            message = u'{}\n\tThe valid options are:\n\t\t{}'.format(error, valid)
        else:
            message = u'{}. There are no valid options.'.format(error)
        return message

    @classmethod
    def _type_validation_with_suggestions(cls, validation_class, value):
        try:
            validation_class.valid(value, raise_exception=True)
        except ValueError as e:
            error = unicode(e)
            message = cls._format_error(error, validation_class.valid_entries)
            raise click.BadParameter(message)
        return value

    @classmethod
    def hostname(cls, ctx, param, value):
        return cls._type_validation('hostname', value)

    @classmethod
    def ip(cls, ctx, param, value):
        return cls._type_validation('ip', value)


import numpy as np
import pandas as pd
import os
import time
import random
import matplotlib.pyplot as plt

from glob import glob

import PIL
import cv2

def hog(image_name):
    hog = cv2.HOGDescriptor()
    hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
    #image_path = glob.glob('data/*.jpg')
    #print(image_path)

    #for image_name in image_path:

    img = cv2.imread(image_name)

    if img.shape[1] < 400:
        (height, width) = img.shape[:2]
        ratio = float(height)/float(width)
        img = cv2.resize(img,(400, 350))

    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    rects,weights = hog.detectMultiScale(img_gray, winStride =(2,2), padding=(10,10), scale = 1.02)

    for i, (x,y,w,h) in enumerate(rects):
        if weights[i] < 0.1:
            continue
        elif weights[i] > 0.1 and weights[i] < 0.3:
            cv2.rectangle(img, (x,y), (x+w, y+h), (0,0,255),2)

        elif weights[i] > 0.3 and weights[i] < 0.7:
            cv2.rectangle(img, (x,y), (x+w, y+h), (50,122,255),2)

        elif weights[i] > 0.7:
            cv2.rectangle(img, (x,y), (x+w, y+h), (0,255,0),2)

    cv2.imshow('Image', img)
    cv2.waitKey(0)