import random

import matplotlib.pyplot as plt
import numpy as np
import torch
from torch.utils.data import Dataset
from torchvision import datasets

def set_seed(seed):
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    np.random.seed(seed)
    random.seed(seed)

set_seed(0)

class CIFAR10FromTxt(Dataset):
    def __init__(self, txt_path, root, train, transform, download, need_idx):
        self.need_idx =  need_idx
        self.base_dataset = datasets.CIFAR10(root, train, transform, download=download)
        self.raw_idxs = []
        self.targets = []
        self.txt_path = txt_path

        with open(self.txt_path, 'r') as f:
            lines = f.readlines()
            for line in lines:
                line = line.strip('\n')
                line = line.split(' ')
                raw_idx = int(line[0][8:])
                label = int(line[1][6:])
                self.raw_idxs.append(raw_idx)
                self.targets.append(label)

    def __len__(self):
        return len(self.raw_idxs)

    def __getitem__(self, index):
        raw_idx, label = self.raw_idxs[index], self.targets[index]
        img, _ = self.base_dataset.__getitem__(raw_idx)
        if self.need_idx:
            return img, label, raw_idx
        else:
            return img, label

class CIFAR10Noisy(Dataset):
    def __init__(self, root, train, transform=None, download=True, noise_type='symmetric', noise_rate=0.2,
                 need_idx=True):
        self.base_dataset = datasets.CIFAR10(root, train, transform, download=download)
        self.noise_type = noise_type
        self.noise_rate = noise_rate
        self.noisy_sample_idx = []
        self.clean_sample_idx = []
        self.need_idx = need_idx
        self.base_transform = transform
        # add label noise
        if self.noise_type == 'symmetric':
            self.uniform(noise_rate, 10)
        elif self.noise_type == 'asymmetric':
            self.flip(noise_rate, 10)

    def to_txt(self, txt_path):
        with open(txt_path, 'w') as f:
            for i in range(self.base_dataset.__len__()):
                l = self.base_dataset.targets[i]
                f.write('raw_idx:' + str(i) + ' label:' + str(l) + '\n')

    def uniform(self, noise_rate: float, num_classes: int):
        """Add symmetric noise"""

        # noise transition matrix
        ntm = noise_rate * np.full((num_classes, num_classes), 1 / (num_classes - 1))
        np.fill_diagonal(ntm, 1 - noise_rate)

        figure = plt.figure()
        axes = figure.add_subplot(111)
        # plt.matshow(ntm, cmap='viridis')
        # caxes = axes.matshow(ntm, interpolation='nearest',cmap=plt.cm.Blues)
        caxes = axes.matshow(ntm, interpolation='nearest', cmap="viridis")
        # axes.set_xticklabels([''] + alphabets)
        # axes.set_yticklabels([''] + alphabets)
        cbar = figure.colorbar(caxes, )
        plt.show()

        sample_indices = np.arange(len(self.base_dataset))

        # generate noisy label by noise transition matrix
        for i in sample_indices:
            label = np.random.choice(num_classes, p=ntm[self.base_dataset.targets[i]])
            if label != self.base_dataset.targets[i]:
                self.noisy_sample_idx.append(i)
            self.base_dataset.targets[i] = label

        self.noisy_sample_idx = np.array(self.noisy_sample_idx)
        self.clean_sample_idx = np.setdiff1d(sample_indices, self.noisy_sample_idx)

        print('Noise type: Symmetric')
        print('Noise rate:', noise_rate)
        print('Noise transition matrix:\n', ntm)
        print('Clean samples:', len(self.clean_sample_idx), 'Noisy samples:', len(self.noisy_sample_idx))

    def flip(self, noise_rate: float, num_classes: int):
        """Add asymmetric noise"""

        # noise transition matrix
        ntm = np.eye(num_classes) * (1 - noise_rate)

        d = {9: 1, 2: 0, 3: 5, 5: 3, 4: 7}  # truck->automobile, bird->airplane, cat->dog, dog->cat, deer->horse
        for raw_class, new_class in d.items():
            ntm[raw_class][new_class] = noise_rate

        for i in [0, 1, 6, 7, 8]:
            ntm[i][i] = 1

        figure = plt.figure()
        axes = figure.add_subplot(111)
        # plt.matshow(ntm, cmap='viridis')

        caxes = axes.matshow(ntm, interpolation='nearest', cmap=plt.cm.Blues)
        # axes.set_xticklabels([''] + alphabets)
        # axes.set_yticklabels([''] + alphabets)
        figure.colorbar(caxes)
        plt.show()

        sample_indices = np.arange(len(self.base_dataset))

        # generate noisy label by noise transition matrix
        for i in sample_indices:
            label = np.random.choice(num_classes, p=ntm[self.base_dataset.targets[i]])
            if label != self.base_dataset.targets[i]:
                self.noisy_sample_idx.append(i)
            self.base_dataset.targets[i] = label

        self.noisy_sample_idx = np.array(self.noisy_sample_idx)
        self.clean_sample_idx = np.setdiff1d(sample_indices, self.noisy_sample_idx)

        print('Noise type: Asymmetric')
        print('Noise rate:', noise_rate)
        print('Noise transition matrix:\n', ntm)
        print('Clean samples:', len(self.clean_sample_idx), 'Noisy samples:', len(self.noisy_sample_idx))

    def __len__(self):
        return self.base_dataset.__len__()

    def __getitem__(self, index):
        if self.need_idx:
            return self.base_dataset.__getitem__(index), index
        else:
            return self.base_dataset.__getitem__(index)


import sys, getopt
from configparser import ConfigParser
import requests
import time
import os
import json

def send_json(json_list):
    """
    This function will send the json to the server
    """
    print('Sending JSON to server...')
    config_object = ConfigParser()
    config_object.read("config.ini")
    api = config_object["API"]
    url = api["url"]
    headers = {'accept': 'application/json', 'X-Scope-OrgID': 'docker',  'Content-Type': 'application/json'}
    for record in json_list:
        data = json.dumps({"streams": [{"stream": {"Aplication": "Malware",
        "fecha_hora": record["Fecha y hora"],
        "alerta": record["Alerta/Anomalia"],
        "usuario": record["Usuario"],
        "IPOrigen": record["IPsource"],
        "IPDestino": record["IPdestination"],
        "Actividad": record["Actividad"],
        "Descripcion": record["Descripcion"],
        "Permisos": record["Permisos"]},
        "values":[[str(time.time_ns()), "{} {} {} -> {} {}".format(record["Fecha y hora"],
        record["Alerta/Anomalia"], record["IPsource"], record["IPdestination"],
        record["Descripcion"])]]}]}, indent = 4)
        r = requests.post(url, headers=headers, data=data)
    print('All logs sent.')

def suricata_jsonfylogs():
    """
    This function will convert all suricata logs to json using the eve.json file
    """
    print('Converting all suricata logs to JSON...')
    # log format: Fecha y hora, Alerta/Anomalia, Usuario, IPsource, IPdestination, Actividad, Descripcion, Permisos "signature"
    # "signature" is the desired field in the eve.json file
    json_list = []
    config_object = ConfigParser()
    config_object.read("config.ini")
    suricatapath = config_object["MAINPATH"]["suricata_path"]
    last_valid_line = config_object["LINE"]["last_line"]
    flag = False
    if(os.path.basename(suricatapath) == 'suricata1.log'):
        print('Suricata path is valid.')
        f = open(suricatapath, "r")
        line = f.readline()
        if(not config_object["LINE"]["last_line"] == 'Nothing'):
            while line:
                if (not line.find(last_valid_line) == -1):
                    flag = True
                    line = f.readline()
                    break
                line = f.readline()
        if(not flag):
            f.seek(0)
            line = f.readline()
        while line:
            if(not line.find('{') == -1):
                index = line.index('{')
                record = json.loads(line[index:])
                last_valid_line = line[0:index]
                print(last_valid_line)
                if(record['event_type'] == 'alert'):
                    json_list.append({"Fecha y hora": record['timestamp'],
                    "Alerta/Anomalia": record['event_type'], "Usuario": "Suricata",
                    "IPsource": record['src_ip'], "IPdestination": record['dest_ip'],
                    "Actividad": record['alert']['action'], "Descripcion": record['alert']['signature'], "Permisos": "None"})
            line = f.readline()
        if(not last_valid_line == 'Nothing'):
            config_object['LINE'] = {
                'last_line': last_valid_line
            }
            with open('config.ini', 'w') as conf:
                config_object.write(conf)
        f.close()

    with open('suricata.json', 'w') as f:
        json.dump(json_list, f, ensure_ascii=False, indent = 4)
    if(len(json_list) > 0):
        send_json(json_list)
    else:
        print("The log file is empty")

def checK_configfile(mode, arg):
    """
    This function will check if the config file exists and if it has the correct configuration
    """
    options = {'API': 'url', 'MAINPATH': 'suricata_path'}
    if(verify_configfile()):
        config_object = ConfigParser()
        config_object.read("config.ini")
        config_option = config_object[mode]
        if(config_option[options[mode]] != arg):
            print('Warning: Config file does not match with the config provided.')
            print('Changing configuration...')
            change_configfile(mode, arg)
            print('Configuration changed.')
    else:
        if not os.path.exists('config.ini'):
            print('Warning: Config file does not exist , creating a new file.')
            open('config.ini', 'w').close()
        config_object = ConfigParser()
        config_object.read("config.ini")
        config_object[mode] = {
            options[mode]: arg
        }
        with open('config.ini', 'w') as conf:
            config_object.write(conf)

def change_configfile(mode, new_option):
    """
    This function will change the path in the config file
    """
    options = {'API': 'url', 'MAINPATH': 'suricata_path'}
    config_object = ConfigParser()
    config_object.read("config.ini")
    config_object[mode] = {
        options[mode]: new_option
    }
    with open('config.ini', 'w') as conf:
        config_object.write(conf)

def verify_configfile():
    """
    This function will verify if the config file exists and if it has the path to the logs
    """
    exists = True
    if os.path.exists('config.ini'):
        config_file = ConfigParser()
        config_file.read('config.ini')
        sections = config_file.sections()
        if len(sections) == 0 or 'MAINPATH' not in sections or 'API' not in sections:
            exists = False
    else:
        print('Config file does not exist.')
        exists = False
    return exists

def main(argv):
    try:
        opts, args = getopt.getopt(argv,"ha:p:",["help","api=","path="])
    except getopt.GetoptError:
        print('Invalid arguments.')
        print('Usage: jsonfylogs.py [-a | --api]  [-p | --path <path>]')
        sys.exit(2)
    if len(opts) != 0:
        for opt, arg in opts:
            if opt in ("-h", "--help"):
                print('Usage: jsonfylogs.py [-a | --api]  [-p | --path <path>]')
                sys.exit()
            elif opt in ("-a", "--api"):
                # If this option is selected, is necesary to have in the config file the url of the api
                checK_configfile('API', arg)
            elif opt in ("-p", "--path"):
                # This option will be used to specify the path to the logs
                checK_configfile('MAINPATH', arg)
            else:
                print('Invalid arguments.')
                print('Usage: jsonfylogs.py [-a | --api]  [-p | --path <path>]')
                sys.exit(2)
    else:
        if os.path.exists('config.ini'):
            if(verify_configfile()):
                suricata_jsonfylogs()
            else:
                print('Configuration file is not valid.')
                print('Run the script with the -p and -a option and specify the path to the logs and the URL for the API.')
        else:
            print('Config file does not exist, run the script with the -p and -a option and specify the path to the logs.')
            print('Usage: jsonfylogs.py [-a | --api]  [-p | --path <path>]')
            sys.exit(2)

if __name__ == "__main__":
    main(sys.argv[1:])


import time

from django.conf import settings

def build_photo_url(photo_id, pic_version="thumb", pic_type="photos", cdn=False):
    # identifier = "!"
    if not cdn:
        if photo_id:
            return "".join([
                settings.MEDIA_URL, pic_type, "/", pic_version,
                "/", photo_id[:2], "/", photo_id, ".jpg"
            ])
        else:
            return "".join(["/static/", "images/", "user-default.jpg"])
    else:
        if photo_id:
            return "".join([
                settings.SERVERHOST, "/media/", pic_type, "/", pic_version,
                "/", photo_id[:2], "/", photo_id , ".jpg"
            ])
        else:
            return "".join([settings.SERVERHOST, "/static/", "images/", "user-default", ".jpg"])

def parse_timestamps(timestamps):
    t = time.localtime(timestamps)
    time_str = time.strftime(r'%Y-%m-%d %H:%M:%S', t)
    return time_str


from typing import List, Tuple

class Day4:
    def __init__(self):
        self.input = []
        self.bingo_numbers = []
        self.bingo_boards = []
        self.bingo_status = []
        self.load_input()
        print(self.bingo_boards)
        self.solve_part_1()
        self.solve_part_2()

    def load_input(self):
        f = open("../inputs/day4.txt", "r")
        txt = f.read()
        lines = txt.splitlines()
        self.bingo_numbers = list(map(int, lines[0].split(',')))
        self.bingo_boards, self.bingo_status = Day4.parse_bingo_boards(lines)

    def solve_part_1(self):
        print("p1 ====================")
        winning_board: List[Tuple[int, int]]
        winning_num: int
        winning_board, winning_num = Day4.find_winning_board(self.bingo_numbers, self.bingo_boards)
        unused_nums = sum(map(lambda n: n[0] if n[1] == 0 else 0, winning_board))
        print(f'unused({unused_nums}) * winning({winning_num}) = {unused_nums*winning_num}')
        return

    def solve_part_2(self):
        print("p2 ====================")
        return

    @staticmethod
    def find_winning_board(numbers, boards):
        for num in numbers:
            for board in boards:
                if Day4.play_board_turn(board, num):
                    return board, num

    @staticmethod
    def parse_bingo_boards(raw_input: List[str], start_line=2):
        boards = []
        status_boards = []
        board = []
        status = [0]*25
        for line in raw_input[start_line:]:
            if line.strip() == '':
                boards.append(board)
                board = []
            else:
                board.extend(list(map(int, line.split())))
        # capture the last board
        boards.append(board)

        for x in range(len(boards)):
            status_boards.append(status)
        return boards, status_boards

    @staticmethod
    def play_board_turn(board: List[List[int, int]], number):
        if number not in board:
            return False
        else:
            for board_pos in board:
                if board_pos[0] == number:
                    board_pos[1] = 1
                    # check victory
                    Day4.check_win(board, number)

    @staticmethod
    def check_win(board: List[List[int, int]], winning_num):
        # win conditions: horizontal match
        if board[0][1] and board[1][1] and board[2][1] and board[3][1] and board[4][1]:
            return True
        elif board[5][1] and board[6][1] and board[7][1] and board[8][1] and board[9][1]:
            return True
        elif board[10][1] and board[11][1] and board[12][1] and board[13][1] and board[14][1]:
            return True
        elif board[15][1] and board[16][1] and board[17][1] and board[18][1] and board[19][1]:
            return True
        elif board[20][1] and board[21][1] and board[22][1] and board[23][1] and board[24][1]:
            return True

        # win conditions: vertical match
        for i in range(5):
            if sum(map(lambda x: x,board[0:5][1])) == 5:
                # do stuff
                break

        return False

Day4()


# -*- coding: utf-8 -*-
import scrapy
import time
import re
import json
from china.items import ChinaIntroItem_shenzhenL
from china.items import ChinaIntroItem_shanghaiL

class CodeSpider(scrapy.Spider):
    name = 'china_updateAll_l'
    allowed_domains = ['sse.com.cn', 'szse.cn']
    #shanghaiL 无
    #shenzhenL
    type_list_shenzhenL = ["tab2", "tab3", "tab4"]
    url1_shenzhenL = "http://www.szse.cn/api/report/ShowReport/data?SHOWTYPE=JSON&CATALOGID=1110x&TABKEY="
    url2_shenzhenL = "&PAGENO="
    url3_shenzhenL = "&random=0.8452698015484685"

    def start_requests(self):
        #shanghaiL
        url_shanghaiL = "http://query.sse.com.cn/security/stock/downloadStockListFile.do?csrcCode=&stockCode=&areaName=&stockType=1"
        yield scrapy.Request(url_shanghaiL, callback=self.parse_shanghaiL)
        #shenzhenL
        for temp_shenzhenL in self.type_list_shenzhenL:
            num_shenzhenL = 1
            if temp_shenzhenL == "tab2":
                plate_type = "主版"#1
            elif temp_shenzhenL == "tab3":
                plate_type = "中小企业版"#2
            else:
                plate_type = "创业版"#3
            link_shenzhenL = self.url1_shenzhenL + temp_shenzhenL + self.url2_shenzhenL
            url_shenzhenL = self.url1_shenzhenL + temp_shenzhenL + self.url2_shenzhenL + str(num_shenzhenL) + self.url3_shenzhenL
            yield scrapy.FormRequest(url_shenzhenL, callback=self.parse_shenzhenL, meta={"link": link_shenzhenL, "plate_type": plate_type, "num": num_shenzhenL})

    def parse_shanghaiL(self, response):
        data = response.text
        item = ChinaIntroItem_shanghaiL()
        item["data"] = data
        item["doc_source_url"] = None
        yield item
        # with open("/data/OPDCMS/china/listed_company_update/company_list/shanghai_" + self.create_time_shanghaiL + ".txt", "a") as f:
        #     f.write(data)

    def parse_shenzhenL(self, response):
        num = response.meta["num"]
        plate_type = response.meta["plate_type"]
        link = response.meta["link"]
        data = response.body
        if plate_type == "主版":
            data_list = json.loads(data)[1]["data"]
        elif plate_type == "中小企业版":
            data_list = json.loads(data)[2]["data"]
        else:
            data_list = json.loads(data)[3]["data"]
        if len(data_list) != 0:
            for temp_shenzhenL in data_list:
                item = ChinaIntroItem_shenzhenL()
                item["plate_type"] = plate_type
                item["code"] = temp_shenzhenL["zqdm"]
                item["company_short_name_zh"] = re.search(">(.*?)", str(temp_shenzhenL["gsjc"])).group(1)
                item["name_origin"] = temp_shenzhenL["gsqc"]
                item["security_code"] = temp_shenzhenL["sshymc"]
                item["website_url"] = temp_shenzhenL["http"]
                item["gmt_create"] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
                item["user_create"] = "zx"
                item["doc_source_url"] = None
                yield item
                num += 1
                url = link + str(num) + self.url3_shenzhenL
                yield scrapy.Request(url, callback=self.parse_shenzhenL, meta={"link": link, "plate_type": plate_type, "num": num})


import random
import os
from collections import defaultdict
from ducktape.utils.util import wait_until
from ducktape.mark import matrix
from rptest.clients.types import TopicSpec
from rptest.clients.rpk import RpkTool
from rptest.services.admin import Admin
from rptest.clients.default import DefaultClient
from rptest.services.cluster import cluster
from rptest.tests.redpanda_test import RedpandaTest
from rptest.utils.mode_checks import cleanup_on_early_exit

class RackAwarePlacementTest(RedpandaTest):
    def __init__(self, test_context):
        super(RackAwarePlacementTest,
              self).__init__(test_context=test_context,
                             num_brokers=6,
                             extra_rp_conf={
                                 'enable_rack_awareness': True,
                                 'segment_fallocation_step': 1024,
                             })

    def setUp(self):
        # Delay startup, so that the test case can configure redpanda
        # based on test parameters before starting it.
        pass

    def _get_partition_count(self, topic):
        try:
            m = self.client().describe_topic(topic)
            return len(m.partitions)
        except Exception as err:
            self.logger.info(f"describe_topic error: {err}")
            return 0

    def _create_topic(self, topic):
        """Create topic and wait until all partitions are created.
        """
        self.client().create_topic(topic)

        wait_until(
            lambda: self._get_partition_count(topic.name
                                              ) == topic.partition_count,
            timeout_sec=60,
            backoff_sec=1,
            err_msg=
            f"Not all partitions are created in time, expected {topic.partition_count} partitions, found {self._get_partition_count(topic.name)}"
        )

    def _validate_placement(self,
                            topic,
                            rack_layout,
                            num_replicas,
                            ids_mapping={}):
        """Validate the replica placement. The method uses provided
        rack layout and number of replicas for the partitions.
        The validation is done by examining existing replica placemnt
        against the rack layout. The validation succedes if every replica
        is placed on a different rack or if there is not enough racks on
        every available rack.
        """
        num_racks = len(set(rack_layout))
        m = self.client().describe_topic(topic.name)
        replicas = defaultdict(list)
        for part in m.partitions:
            for broker in part.replicas:
                # broker ids are 1-based
                id = ids_mapping[broker] if broker in ids_mapping else broker
                rack = rack_layout[id - 1]
                replicas[part.id].append(rack)

        for part_id, racks in replicas.items():
            expected_racks = min(num_racks, num_replicas)
            self.logger.info(
                f"partition {part_id} is replicated to racks {racks}, expected {expected_racks} racks, total racks {num_racks}"
            )
            assert len(set(racks)) == min(num_racks, num_replicas)

    @cluster(num_nodes=6)
    @matrix(rack_layout_str=['ABCDEF', 'xxYYzz', 'ooooFF'],
            num_partitions=[50, 400],
            replication_factor=[3, 5],
            num_topics=[2])
    def test_replica_placement(self, rack_layout_str, num_partitions,
                               replication_factor, num_topics):
        """
        Test replica placement. The test case creates a set of brokers with
        initialized 'rack' parameter in the configuration. The 'rack' property
        is defined by the 'rack_layout' parameter. Then it creates a topic with
        pre-defined number of partitions and replication factor and validates
        the replica placement.

        The validation procedure checks that:
        - either every replica lives on a different rack;
        - or every rack is used to place one or more replicas of the partition
          (if replication factor is larger than number of racks)

        ----
        @param rack_layout_str is a 6-char string with all rack names combined into a signle string.
                               Every character of the string corresponds to one broker. Every
                               rack id has single character.
        @param num_partitions defines number of partitions that needs to be created.
        @param replication_factor defines recplication factor of all partitions.
        """

        # https://github.com/redpanda-data/redpanda/issues/11276
        if self.debug_mode and num_partitions == 400:
            self.logger.info(
                "Disabling test in debug mode due to slowness/timeouts with large number of partitions."
            )
            cleanup_on_early_exit(self)
            return

        rack_layout = [str(i) for i in rack_layout_str]
        assert len(rack_layout) == 6

        for ix, node in enumerate(self.redpanda.nodes):
            extra_node_conf = {
                # We're introducing two racks, small and large.
                # The small rack has only one node and the
                # large one has four nodes.
                'rack': rack_layout[ix],
                # This parameter enables rack awareness
                'enable_rack_awareness': True,
                'segment_fallocation_step': 4096,
            }
            self.redpanda.set_extra_node_conf(node, extra_node_conf)

        self.redpanda.start()

        if num_topics * num_partitions * replication_factor > 3000:
            # Combination of 2 topics, 5 replicas and 400 partitions can't be run
            # reliably on both debug and release due to ducktape limitations.
            self.logger.info(
                f"combination of num_partitions: {num_partitions}, replication_factor: {replication_factor}, and num_topics: {num_topics} is excluded"
            )
            return

        topics = []
        for tix in range(0, num_topics):
            topic = TopicSpec(partition_count=num_partitions,
                              replication_factor=replication_factor)
            topics.append(topic)
            self._create_topic(topic)

        for topic in topics:
            self._validate_placement(topic, rack_layout, replication_factor)

    def _partition_count(self):
        # use smaller number of partitions for debug builds
        if os.environ.get('BUILD_TYPE', None) == 'debug':
            return random.randint(20, 40)

        return random.randint(200, 300)

    @cluster(num_nodes=6)
    @matrix(rack_layout=['ABCDEF', 'xxYYzz', 'ooooFF'])
    def test_rack_awareness_after_node_operations(self, rack_layout):
        replication_factor = 3

        for ix, node in enumerate(self.redpanda.nodes):
            self.redpanda.set_extra_node_conf(
                node,
                {
                    "rack": rack_layout[ix],
                    'enable_rack_awareness': True,
                    # make fallocation step small to spare the disk space
                    'segment_fallocation_step': 4096,
                })

        self.redpanda.start()
        self._client = DefaultClient(self.redpanda)

        topic = TopicSpec(partition_count=self._partition_count(),
                          replication_factor=replication_factor)
        self.client().create_topic(topic)
        self._validate_placement(topic, rack_layout, replication_factor)

        # decommission one of the nodes
        admin = Admin(self.redpanda)

        brokers = admin.get_brokers()
        to_decommission = random.choice(brokers)['node_id']

        admin.decommission_broker(to_decommission)

        def node_is_removed():
            for n in self.redpanda.nodes:
                if self.redpanda.idx(n) == to_decommission:
                    continue
                brokers = admin.get_brokers(node=n)
                for b in brokers:
                    if b['node_id'] == to_decommission:
                        return False
            return True

        # wait until node is removed and validate rack placement
        wait_until(node_is_removed, 90, 2)

        self._validate_placement(topic, rack_layout, replication_factor)

        # stop the node and add it back with different id
        to_stop = self.redpanda.get_node(to_decommission)
        self.redpanda.stop_node(to_stop)
        self.redpanda.clean_node(node=to_stop, preserve_logs=True)

        new_node_id = 123

        def seed_servers_for(idx):
            seeds = map(
                lambda n: {
                    "address": n.account.hostname,
                    "port": 33145
                }, self.redpanda.nodes)

            return list(
                filter(
                    lambda n: n['address'] != self.redpanda.get_node(idx).
                    account.hostname, seeds))

        # add a node back with different id but the same rack
        # change the seed server list to prevent node from forming new cluster
        self.redpanda.start_node(
            to_stop,
            override_cfg_params={
                "node_id": new_node_id,
                "seed_servers": seed_servers_for(to_decommission),
                "rack": rack_layout[to_decommission - 1],
                'enable_rack_awareness': True,
                # make fallocation step small to spare the disk space
                'segment_fallocation_step': 4096,
            })

        rpk = RpkTool(self.redpanda)

        def new_node_has_partitions():
            for p in rpk.describe_topic(topic.name):
                for r in p.replicas:
                    if r == 123:
                        return True

            return False

        wait_until(new_node_has_partitions, 90, 2)

        def no_partitions_moving():
            for n in self.redpanda.nodes:
                r = admin.list_reconfigurations(node=n)
                if len(r) > 0:
                    return False

            return True

        wait_until(no_partitions_moving, 90, 1)

        self._validate_placement(topic,
                                 rack_layout,
                                 replication_factor,
                                 ids_mapping={new_node_id: to_decommission})

    @cluster(num_nodes=6)
    def test_node_config_update(self):
        """
        * Create a cluster from nodes with no rack id configured
        * edit node configs adding rack ids
        * restart the nodes
        * check that rack aware placement works
        """
        rack_layout = 'AABBCC'
        replication_factor = 3
        num_partitions = 10

        self.redpanda.start()

        self.redpanda.stop()
        for ix, node in enumerate(self.redpanda.nodes):
            self.redpanda.start_node(
                node, override_cfg_params={"rack": rack_layout[ix]})

        admin = Admin(self.redpanda)

        def rack_ids_updated():
            for n in self.redpanda.nodes:
                if any('rack' not in b for b in admin.get_brokers(n)):
                    return False
            return True

        wait_until(rack_ids_updated,
                   timeout_sec=30,
                   backoff_sec=1,
                   err_msg="node configurations didn't get updated")

        topic = TopicSpec(partition_count=num_partitions,
                          replication_factor=replication_factor)
        self._create_topic(topic)
        self._validate_placement(topic, rack_layout, replication_factor)


from collections import deque
n, m, v = map(int, input().split())

data = [[] for x in range(n+1)]
# DFS 전용 체크 visited
visited = [False] * (n+1)
# BFS 전용 체크 visited1
visited2 = [False] * (n+1)

for i in range(m):
    a, b = map(int, input().split())
    data[a].append(b)
    data[b].append(a)

for i in range(len(data)):
    data[i].sort()

def dfs(graph, v, visited):
    visited[v] = True
    print(v, end=' ')
    for i in graph[v]:
        if not visited[i]:
            dfs(graph, i, visited)

def bfs(graph, v, visited):
    queue = deque([v])
    visited[v] = True
    while queue:
        v = queue.popleft()
        print(v, end=' ')
        for i in graph[v]:
            if not visited[i]:
                queue.append(i)
                visited[i] = True

dfs(data, v, visited)
print()
bfs(data, v, visited2)


from multiprocessing import Process, current_process
from time import sleep
import numpy as np

import allogger

def worker_process(id):
    if id % 2 == 0:
        logger = allogger.get_logger(f'worker')
    else:
        logger = allogger.get_logger(scope=f'worker', logdir=f'/tmp/allogger/multiprocessing/{current_process().name}')

    for i in range(10):
        logger.log(i, 'value')
        sleep(np.random.uniform(1, 5))

    if id % 2 != 0:
        allogger.close()

def main():
    allogger.basic_configure('/tmp/allogger/multiprocessing', ['tensorboard'])
    logger = allogger.get_logger('main')

    for i in range(10):
        logger.log(i, 'value')
        sleep(np.random.uniform(1, 5))

    workers = []
    for i in range(20):
        worker = Process(target=worker_process,
                                         args=(i,))
        workers.append(worker)
        worker.start()
    for w in workers:
        w.join()

    allogger.close()

if __name__ == '__main__':
    main()


import torch
import torch.nn as nn
from torchvision import models
from REGISTRY import double_class_methods

def construct_model(architecture, method, num_classes, in_channels=1):

    if architecture == "densenet121":
        model = models.densenet121(pretrained=True)
        num_ftrs = model.classifier.in_features
        if method in double_class_methods:
            model.classifier = nn.Linear(num_ftrs, 2 * num_classes)
        else:
            model.classifier = nn.Linear(num_ftrs, num_classes)

    elif architecture == "shortdensenet121":

        class ShortNet(nn.Module):
            def __init__(self, base_model):
              super(ShortNet, self).__init__()
              self.base_model = base_model
              self.global_pool = nn.AvgPool2d(28)
              self.classifier = nn.Linear(512, 14)

            def forward(self, x):
              x = self.base_model.features.conv0(x)
              x = self.base_model.features.norm0(x)
              x = self.base_model.features.relu0(x)
              x = self.base_model.features.pool0(x);
              x = self.base_model.features.denseblock1(x);
              x = self.base_model.features.transition1(x);
              x = self.base_model.features.denseblock2(x);
              x = self.base_model.features.transition2.norm(x);
              x = self.global_pool(x)
              x = x.squeeze(-1).squeeze(-1)
              x = self.classifier(x)
              return x

        model = models.densenet121(pretrained=True)
        model = ShortNet(model)
        num_ftrs = model.classifier.in_features
        if method in double_class_methods:
            model.classifier = nn.Linear(num_ftrs, 2 * num_classes)
        else:
            model.classifier = nn.Linear(num_ftrs, num_classes)

    elif architecture == "resnet50":
        model = models.resnet50(pretrained=True)
        num_ftrs = model.classifier.in_features
        if method in double_class_methods:
            model.classifier = nn.Linear(num_ftrs, 2 * num_classes)
        else:
            model.classifier = nn.Linear(num_ftrs, num_classes)

    else:
        print("Unknown architecture. Aborting...")
        return

    model.architecture = architecture
    model.num_classes = num_classes
    model.method = method
    model.in_channels = in_channels

    return model

def load_model(checkpoint_path):
    checkpoint = torch.load(checkpoint_path);
    architecture = checkpoint['architecture'];
    num_classes = checkpoint['num_classes'];
    train_method = checkpoint['train_method'];
    model_state_dict = checkpoint['model_state_dict'];
    optimizer_state_dict = checkpoint['optimizer_state_dict'];
    starting_epoch = checkpoint['epoch'] if 'epoch'in checkpoint else 0;
    best_acc = checkpoint['best_acc'] if 'best_acc' in checkpoint else 0.0;
    in_channels = checkpoint['in_channels'] if 'in_channels' in checkpoint else 1;

    model = construct_model(architecture, train_method, num_classes, in_channels);
    model.load_state_dict(model_state_dict);
    print("Existing model was trained using {0}".format(train_method))

    return model, starting_epoch, best_acc, train_method

def put_model_to_device(model):
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    model = model.to(device)
    return model

def get_num_pool_ops(architecture):
    if architecture == 'vnet':
        num_pool_ops = 4
    else:
        num_pool_ops = 3
    return num_pool_ops


import cv2
from directkeys import PressKey, ReleaseKey
from grabscreen import grab_screen
import numpy as np
import pyautogui
from sklearn.cluster import KMeans
import threading
import time

uhd_x = 640
uhd_y = 312
BOX = (uhd_x, uhd_y, uhd_x + 640, uhd_y + 480)

hood_y = 410
horizon_y = 175
side_y = 50
keytime = 0.1

STRAIGHT = 0x11
RIGHT = 0x20
LEFT = 0x1E

VERTICES = np.array([[4, horizon_y + side_y],
                     [220, horizon_y], [580, horizon_y],
                     [800, horizon_y + side_y],
                     [800, hood_y], [4, hood_y]])

for i in range(3, 0, -1):
    time.sleep(.4)
    print(i)

def t_key(key_a, key_b, key_c):
    PressKey(key_a)
    ReleaseKey(key_b)
    ReleaseKey(key_c)
    time.sleep(keytime)

def straight():
    print("pressing up")
    thread_straight = threading.Thread(target=t_key,
                                       args=(STRAIGHT, RIGHT, LEFT))
    thread_straight.start()

def right():
    print("pressing r")
    thread_right = threading.Thread(target=t_key,
                                    args=(RIGHT, STRAIGHT, LEFT))
    thread_right.start()

def left():
    print("pressing l")
    thread_left = threading.Thread(target=t_key,
                                   args=(LEFT, STRAIGHT, RIGHT))
    thread_left.start()

def slope(line):
    try:
        y = line[1] - line[3]
        x = line[0] - line[2]
        slope = np.divide(y, x)
    except ZeroDivisionError:
        slope = 100000
    finally:
        return slope

def drive(m=None):
    sign = np.sum(np.sign(m))
    if sign == -2:
        right()
    elif sign == 2:
        left()
    else:
        straight()

def draw_lines(img, lines):
    try:
        m = []
        for coords in lines:
            m.append(slope(coords))
            coords = np.array(coords, dtype='uint32')
            cv2.line(img,
                     (coords[0], coords[1]),
                     (coords[2], coords[3]),
                     [255, 255, 255], 10)
    except TypeError as e:
        print('draw lines error: {}'.format(e))
    else:
        pass
        drive(m)

def roi(img, vertices):
    mask = np.zeros_like(img)
    cv2.fillPoly(mask, vertices, 255)
    masked = cv2.bitwise_and(img, mask)
    return masked

def process_img(original_img):

    processed_img = cv2.Canny(original_img,
                              threshold1=100, threshold2=300)

    processed_img = roi(processed_img, [VERTICES])

    # processed_img = cv2.GaussianBlur(processed_img, (5, 5), 0)
    lines = cv2.HoughLinesP(processed_img, 1,
                            np.pi / 180, 180, np.array([]), 120, 20)

    # draw_lines(processed_img, nlines)

    try:
        nlines = np.array([l[0] for l in lines])
        kmeans = KMeans(n_clusters=2, random_state=0).fit(nlines)
        draw_lines(processed_img, kmeans.cluster_centers_)
    except (ValueError, TypeError) as e:
        print('Kmeans error: {}'.format(e))

    return processed_img

def main():
    while True:
        ti = time.time()
        screen = grab_screen(region=BOX)
        cv2.imshow('window', cv2.cvtColor(screen,
                                          cv2.COLOR_BGR2RGB))

        new_screen = process_img(screen)
        cv2.imshow('window2', new_screen)

        print('{:.2f} FPS'.format(1 / (time.time() - ti)))
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

if __name__ == '__main__':
    main()


'''
下载斗罗大第一集
流程：
    1.拿到710-1-1.html页面源代码
    2.从代码中提取m3u8的url
    3.下载m3u8
    4.读取m3u8文件，下载视频
    5.合并视频
'''

import requests
import re
import asyncio
import aiohttp
import aiofiles
import os

# 1.拿到710-1-1.html页面源代码
# 2.从代码中提取m3u8的url

# https://v.v1kd.com/20211117/ttxgRz2V/index.m3u8（第一个m3u8）
# https://v.v1kd.com/20211117/ttxgRz2V/1000kb/hls/index.m3u8（第二个m3u8）

def get_m3u8(url):
    resp = requests.get(url)
    text = resp.text
    resp.close()

    # 从JavaScript提取最好使用re
    obj = re.compile(r'
正在播放(?P<name>.*?)
.*?'
                     r'


# https://contest.yandex.ru/yacup/contest/42202/problems/A/
# Time Limit

from typing import List

with open("input.txt") as f_in:
    n, m, q = f_in.readline().replace('\n', '').split(' ')
    n = int(n)
    m = int(m)
    last_max = last_min = -1

    out: List[int] = []
    dcr: List[int] = [0]*n
    dcs: List[List[bool]] = [[True]*m for _ in range(n)]
    dcs_as: List[int] = [m]*n

    for cmd in f_in.readlines():
        cmd = cmd.replace('\n', '').split(' ')

        if cmd[0] == 'DISABLE':
            dc_i = int(cmd[1]) - 1
            s_i = int(cmd[2])-1
            if dcs[dc_i][s_i]:
                dcs[dc_i][s_i] = False
                dcs_as[dc_i] -= 1
                last_max = -1 if dc_i == last_max else last_max
                last_min = -1 if dc_i != last_min else last_min
                # if dc_i != last_min:
                #     last_min = last_min if dcs_as[dc_i] * dcr[dc_i] > dcs_as[last_min] * dcr[last_min] else dc_i

        if cmd[0] == 'RESET':
            dc_i = int(cmd[1]) - 1
            dcs[dc_i] = [True]*m
            dcr[dc_i] += 1
            dcs_as[dc_i] = m

            last_min = -1 if dc_i == last_min else last_min
            last_max = last_max if dc_i == last_max else -1

        if cmd[0] == 'GETMAX':
            if last_max != -1:
                out.append(last_max)
            else:
                dc_max_i = 0
                dc_max_value = dcs_as[0] * dcr[0]
                for i in range(1, n):
                    if dcs_as[i] * dcr[i] > dc_max_value:
                        dc_max_i = i
                        dc_max_value = dcs_as[i] * dcr[i]
                last_max = dc_max_i
                out.append(dc_max_i)

        if cmd[0] == 'GETMIN':
            if last_min != -1:
                out.append(last_min)
            else:
                dc_min_i = 0
                dc_min_value = sum(dcs[0]) * dcr[0]
                for i in range(1, n):
                    if dcs_as[i] * dcr[i] < dc_min_value:
                        dc_min_i = i
                        dc_min_value = dcs_as[i] * dcr[i]

                last_min = dc_min_i
                out.append(dc_min_i)

    with open('output.txt', 'w') as f_out:
        f_out.write('\n'.join([f"{o + 1}" for o in out]))


from tkinter import *
from tkinter.ttk import Combobox
import sqlite3
import math
import Languages as LG
import Number_format as Nf

class Sheet_SummaryMenu():
    def __init__(self, Multiple_Page, Invcal):
        self.Pagina_Multiple = Multiple_Page
        self.Invcal = Invcal

        self.Page = Frame(Multiple_Page)
        self.start = 0

        self.dict_actual = LG.create_language(language=LG.language_actual())
        self.dict_main = LG.create_language()

        self.Text1_Filter = Label(self.Page, text=LG.translate(self.dict_main, self.dict_actual,"text_54"))
        self.Text2_Filter = Label(self.Page, text=LG.translate(self.dict_main, self.dict_actual,"text_55"))

        self.Text1_Filter.place(x=130, y=35, anchor=E)
        self.Text2_Filter.place(x=130, y=70, anchor=E)

        self.List1_Filter_var = StringVar()
        self.List1_Filter = Combobox(self.Page,values= [LG.translate(self.dict_main, self.dict_actual,"type_domain_1"), LG.translate(self.dict_main, self.dict_actual,"type_domain_2")], textvariable=self.List1_Filter_var, state="readonly", width=15)
        self.List1_Filter.bind("<<ComboboxSelected>>", self.Update_List_Button)
        self.List1_Filter_var.set("-")
        self.List2_Filter_var = StringVar()
        self.List2_Filter = Combobox(self.Page,values= [LG.translate(self.dict_main, self.dict_actual,"status_1"), LG.translate(self.dict_main, self.dict_actual,"status_2")], textvariable=self.List2_Filter_var, state="readonly", width=15)
        self.List2_Filter.bind("<<ComboboxSelected>>", self.Update_List_Button)
        self.List2_Filter_var.set("-")

        self.List1_Filter.place(x=140, y=35, anchor=W)
        self.List2_Filter.place(x=140, y=70, anchor=W)

        self.Marco1a = Label(self.Page)
        self.Marco1a.config(relief="groove", width=36, height=10)
        self.Marco1a.place(x=430, y=20, anchor=N)

        self.Marco1b = Label(self.Page)
        self.Marco1b.config(relief="groove", width=36, height=10)
        self.Marco1b.place(x=713, y=20, anchor=N)

        self.Text1_Marco1a = Label(self.Page, text=LG.translate(self.dict_main, self.dict_actual,"text_57"))
        self.Text1_Marco1b = Label(self.Page, text=LG.translate(self.dict_main, self.dict_actual,"text_56"))

        self.Text1_Marco1a.place(x=430, y=30, anchor=N)
        self.Text1_Marco1b.place(x=713, y=30, anchor=N)

        self.Text_Image1_Marco1a_var = IntVar()
        self.Text_Image1_Marco1a = Label(self.Page, textvariable=self.Text_Image1_Marco1a_var)
        self.Text_Image1_Marco1a.place(x=360, y=85, anchor=N)
        self.Text_Image2_Marco1a_var = IntVar()
        self.Text_Image2_Marco1a = Label(self.Page, textvariable=self.Text_Image2_Marco1a_var)
        self.Text_Image2_Marco1a.place(x=430, y=85, anchor=N)
        self.Text_Image3_Marco1a_var = IntVar()
        self.Text_Image3_Marco1a = Label(self.Page, textvariable=self.Text_Image3_Marco1a_var)
        self.Text_Image3_Marco1a.place(x=500, y=85, anchor=N)
        self.Text_Image4_Marco1a_var = IntVar()
        self.Text_Image4_Marco1a = Label(self.Page, textvariable=self.Text_Image4_Marco1a_var)
        self.Text_Image4_Marco1a.place(x=360, y=140, anchor=N)
        self.Text_Image5_Marco1a_var = IntVar()
        self.Text_Image5_Marco1a = Label(self.Page, textvariable=self.Text_Image5_Marco1a_var)
        self.Text_Image5_Marco1a.place(x=430, y=140, anchor=N)
        self.Text_Image6_Marco1a_var = IntVar()
        self.Text_Image6_Marco1a = Label(self.Page, textvariable=self.Text_Image6_Marco1a_var)
        self.Text_Image6_Marco1a.place(x=500, y=140, anchor=N)

        self.Text_Image1_Marco1b_var = IntVar()
        self.Text_Image1_Marco1b = Label(self.Page, textvariable=self.Text_Image1_Marco1b_var)
        self.Text_Image1_Marco1b.place(x=643, y=85, anchor=N)
        self.Text_Image2_Marco1b_var = IntVar()
        self.Text_Image2_Marco1b = Label(self.Page, textvariable=self.Text_Image2_Marco1b_var)
        self.Text_Image2_Marco1b.place(x=713, y=85, anchor=N)
        self.Text_Image3_Marco1b_var = IntVar()
        self.Text_Image3_Marco1b = Label(self.Page, textvariable=self.Text_Image3_Marco1b_var)
        self.Text_Image3_Marco1b.place(x=783, y=85, anchor=N)
        self.Text_Image4_Marco1b_var = IntVar()
        self.Text_Image4_Marco1b = Label(self.Page, textvariable=self.Text_Image4_Marco1b_var)
        self.Text_Image4_Marco1b.place(x=643, y=140, anchor=N)
        self.Text_Image5_Marco1b_var = IntVar()
        self.Text_Image5_Marco1b = Label(self.Page, textvariable=self.Text_Image5_Marco1b_var)
        self.Text_Image5_Marco1b.place(x=713, y=140, anchor=N)
        self.Text_Image6_Marco1b_var = IntVar()
        self.Text_Image6_Marco1b = Label(self.Page, textvariable=self.Text_Image6_Marco1b_var)
        self.Text_Image6_Marco1b.place(x=783, y=140, anchor=N)

        self.Marco2 = Label(self.Page)
        self.Marco3 = Label(self.Page)
        self.Marco4 = Label(self.Page)

        self.Text1_Marco2 = Label(self.Page)
        self.Text1_Marco3 = Label(self.Page)
        self.Text1_Marco4 = Label(self.Page)

        self.Text1_Marco2.place(x=27, y=210, anchor=W)
        self.Text1_Marco3.place(x=310, y=210, anchor=W)
        self.Text1_Marco4.place(x=593, y=210, anchor=W)

        self.Text2_Marco2_var = StringVar()
        self.Text2_Marco2 = Label(self.Page, textvariable=self.Text2_Marco2_var, justify=LEFT)
        self.Text2_Marco3_var = StringVar()
        self.Text2_Marco3 = Label(self.Page, textvariable=self.Text2_Marco3_var, justify=LEFT)
        self.Text2_Marco4_var = StringVar()
        self.Text2_Marco4 = Label(self.Page, textvariable=self.Text2_Marco4_var, justify=LEFT)

        self.Text3_Marco2_var = StringVar()
        self.Text3_Marco2 = Label(self.Page, textvariable=self.Text3_Marco2_var)
        self.Text3_Marco3_var = StringVar()
        self.Text3_Marco3 = Label(self.Page, textvariable=self.Text3_Marco3_var)
        self.Text3_Marco4_var = StringVar()
        self.Text3_Marco4 = Label(self.Page, textvariable=self.Text3_Marco4_var)

        self.Text4_Marco2_var = StringVar()
        self.Text4_Marco2 = Label(self.Page, textvariable=self.Text4_Marco2_var)
        self.Text4_Marco3_var = StringVar()
        self.Text4_Marco3 = Label(self.Page, textvariable=self.Text4_Marco3_var)
        self.Text4_Marco4_var = StringVar()
        self.Text4_Marco4 = Label(self.Page, textvariable=self.Text4_Marco4_var)

        self.Text5_Marco2_var = StringVar()
        self.Text5_Marco2 = Label(self.Page, textvariable=self.Text5_Marco2_var)
        self.Text5_Marco3_var = StringVar()
        self.Text5_Marco3 = Label(self.Page, textvariable=self.Text5_Marco3_var)
        self.Text5_Marco4_var = StringVar()
        self.Text5_Marco4 = Label(self.Page, textvariable=self.Text5_Marco4_var)

        self.Load_Images()

        self.Image1_Marco2 = Label(self.Page, image=self.Image_Resin_var)
        self.Image2_Marco2 = Label(self.Page, image=self.Image_Resin_var)
        self.Image3_Marco2 = Label(self.Page, image=self.Image_Resin_var)
        self.Image4_Marco2 = Label(self.Page, image=self.Image_Mora_var)
        self.Image5_Marco2 = Label(self.Page, image=self.Image_Character_var)
        self.Image6_Marco2 = Label(self.Page, image=self.Image_Book_var)

        self.Image1_Marco3 = Label(self.Page, image=self.Image_Resin_var)
        self.Image2_Marco3 = Label(self.Page, image=self.Image_Resin_var)
        self.Image3_Marco3 = Label(self.Page, image=self.Image_Resin_var)
        self.Image4_Marco3 = Label(self.Page, image=self.Image_Mora_var)
        self.Image5_Marco3 = Label(self.Page, image=self.Image_Character_var)
        self.Image6_Marco3 = Label(self.Page, image=self.Image_Book_var)

        self.Image1_Marco4 = Label(self.Page, image=self.Image_Resin_var)
        self.Image2_Marco4 = Label(self.Page, image=self.Image_Resin_var)
        self.Image3_Marco4 = Label(self.Page, image=self.Image_Resin_var)
        self.Image4_Marco4 = Label(self.Page, image=self.Image_Mora_var)
        self.Image5_Marco4 = Label(self.Page, image=self.Image_Character_var)
        self.Image6_Marco4 = Label(self.Page, image=self.Image_Book_var)

        self.Summary_All()

    def Load_Images(self):

        self.Image_Card_var = LG.fastimport_imagech("card_character", "others", 3)
        self.Image_Resin_var = LG.fastimport_imagech("condensed_resin", "others", 3)
        self.Image_Mora_var = LG.fastimport_imagech("mora", "others", 3)
        self.Image_Character_var = LG.fastimport_imagech("xp_character", "others", 3)
        self.Image_Weapon_var = LG.fastimport_imagech("xp_weapon", "others", 3)
        self.Image_Mat_var = LG.fastimport_imagech("mat_domain_weapon_04", "weapon_asc", 3)
        self.Image_Book_var = LG.fastimport_imagech("mat_domain_book_01", "talent", 3)

        self.Image1_Marco1a = Label(self.Page, image=self.Image_Mora_var)
        self.Image2_Marco1a = Label(self.Page, image=self.Image_Character_var)
        self.Image3_Marco1a = Label(self.Page, text=LG.translate(self.dict_main, self.dict_actual,"text_58"))
        self.Image4_Marco1a = Label(self.Page, image=self.Image_Resin_var)
        self.Image5_Marco1a = Label(self.Page, image=self.Image_Resin_var)
        self.Image6_Marco1a = Label(self.Page, image=self.Image_Weapon_var)

        self.Image1_Marco1b = Label(self.Page, image=self.Image_Mora_var)
        self.Image2_Marco1b = Label(self.Page, image=self.Image_Character_var)
        self.Image3_Marco1b = Label(self.Page, text=LG.translate(self.dict_main, self.dict_actual,"text_58"))
        self.Image4_Marco1b = Label(self.Page, image=self.Image_Resin_var)
        self.Image5_Marco1b = Label(self.Page, image=self.Image_Resin_var)
        self.Image6_Marco1b = Label(self.Page, image=self.Image_Weapon_var)

        self.Image1_Marco1a.place(x=360,y=50, anchor=N)
        self.Image2_Marco1a.place(x=430,y=50, anchor=N)
        self.Image3_Marco1a.place(x=500,y=57, anchor=N)

        self.Image4_Marco1a.place(x=360,y=105, anchor=N)
        self.Image5_Marco1a.place(x=430,y=105, anchor=N)
        self.Image6_Marco1a.place(x=500,y=105, anchor=N)

        self.Image1_Marco1b.place(x=643,y=50, anchor=N)
        self.Image2_Marco1b.place(x=713,y=50, anchor=N)
        self.Image3_Marco1b.place(x=783,y=57, anchor=N)

        self.Image4_Marco1b.place(x=643,y=105, anchor=N)
        self.Image5_Marco1b.place(x=713,y=105, anchor=N)
        self.Image6_Marco1b.place(x=783,y=105, anchor=N)

    def Load_Images_Dynamic(self):

        self.Image1_Marco2.place(x=162,y=200)
        self.Image2_Marco2.place(x=197,y=200)
        self.Image3_Marco2.place(x=232,y=200)
        self.Image4_Marco2.place(x=162,y=230)
        self.Image5_Marco2.place(x=197,y=230)
        self.Image6_Marco2.place(x=232,y=230)

        self.Image1_Marco3.place(x=445,y=200)
        self.Image2_Marco3.place(x=480,y=200)
        self.Image3_Marco3.place(x=515,y=200)
        self.Image4_Marco3.place(x=445,y=230)
        self.Image5_Marco3.place(x=480,y=230)
        self.Image6_Marco3.place(x=515,y=230)

        self.Image1_Marco4.place(x=728,y=200)
        self.Image2_Marco4.place(x=763,y=200)
        self.Image3_Marco4.place(x=798,y=200)
        self.Image4_Marco4.place(x=728,y=230)
        self.Image5_Marco4.place(x=763,y=230)
        self.Image6_Marco4.place(x=798,y=230)

    def Summary_All(self):

        conexion1 = sqlite3.connect("DB SQL/genshin_db.db")
        query1 = conexion1.execute("select Build, sum(XP_Character), sum(XP_Weapon), sum(Mora_Ascension + Mora_Talent + Mora_Weapon_Ascension + XP_Character*0.2 + XP_Weapon*0.1) as Mora from user group by Build")
        conexion1.commit()

        query1_values = query1.fetchall()

        try:
            var1a=query1_values[0][3] + query1_values[1][3]
        except:
            try:
                var1a=query1_values[0][3]
            except:
                var1a=0

        try:
            var2a=query1_values[0][1] + query1_values[1][1]
        except:
            try:
                var2a=query1_values[0][1]
            except:
                var2a=0

        try:
            var3a=query1_values[0][2] + query1_values[1][2]
        except:
            try:
                var3a=query1_values[0][2]
            except:
                var3a=0

        try:
            var1b=query1_values[1][3]
        except:
            var1b=0

        try:
            var1b=query1_values[1][3]
        except:
            var1b=0

        try:
            var2b=query1_values[1][1]
        except:
            var2b=0

        try:
            var3b=query1_values[1][2]
        except:
            var3b=0

        # var2a=query1_values[0][1] + query1_values[1][1]
        # var3a=query1_values[0][2] + query1_values[1][2]

        # var1b=query1_values[1][3]
        # var2b=query1_values[1][1]
        # var3b=query1_values[1][2]

        self.Text_Image1_Marco1a_var.set(Nf.format_point(var1a))
        self.Text_Image2_Marco1a_var.set(Nf.format_point(var2a))
        self.Text_Image3_Marco1a_var.set(Nf.format_point(var3a))

        self.Text_Image4_Marco1a_var.set(Nf.format_point(math.ceil(var1a/120000)))
        self.Text_Image5_Marco1a_var.set(Nf.format_point(math.ceil(var2a/245000)))
        self.Text_Image6_Marco1a_var.set(Nf.format_point(math.ceil(var3a/10000)))

        self.Text_Image1_Marco1b_var.set(Nf.format_point(var1b))
        self.Text_Image2_Marco1b_var.set(Nf.format_point(var2b))
        self.Text_Image3_Marco1b_var.set(Nf.format_point(var3b))

        self.Text_Image4_Marco1b_var.set(Nf.format_point(math.ceil(var1b/120000)))
        self.Text_Image5_Marco1b_var.set(Nf.format_point(math.ceil(var2b/245000)))
        self.Text_Image6_Marco1b_var.set(Nf.format_point(math.ceil(var3b/10000)))

    def Load_Marco(self):

        self.Marco2.config(width=36, height=24, relief="groove")
        self.Marco3.config(width=36, height=24, relief="groove")
        self.Marco4.config(width=36, height=24, relief="groove")

        self.Marco2.place(x=147, y=190, anchor=N)
        self.Marco3.place(x=430, y=190, anchor=N)
        self.Marco4.place(x=713, y=190, anchor=N)

        self.Text1_Marco2.config(text=LG.translate(self.dict_main, self.dict_actual,"day_1"))
        self.Text1_Marco3.config(text=LG.translate(self.dict_main, self.dict_actual,"day_2"))
        self.Text1_Marco4.config(text=LG.translate(self.dict_main, self.dict_actual,"day_3"))

        self.Text2_Marco2.place(x=27, y=270)
        self.Text2_Marco3.place(x=310, y=270)
        self.Text2_Marco4.place(x=593, y=270)

        self.Text3_Marco2.place(x=152, y=270)
        self.Text3_Marco3.place(x=435, y=270)
        self.Text3_Marco4.place(x=718, y=270)

        self.Text4_Marco2.place(x=187, y=270)
        self.Text4_Marco3.place(x=470, y=270)
        self.Text4_Marco4.place(x=753, y=270)

        self.Text5_Marco2.place(x=222, y=270)
        self.Text5_Marco3.place(x=505, y=270)
        self.Text5_Marco4.place(x=788, y=270)

    def Summary_day(self):

        #Query Total
        build = "status_1"

        list_day1 = []
        list_day2 = []
        list_day3 = []

        list_unic_day1 = set()
        list_unic_day2 = set()
        list_unic_day3 = set()

        if LG.translate_inv(self.List2_Filter_var.get()) == "status_2":
            build= "status_2"

        var_book = 1

        for elem in self.query2_values:
            if build == "status_1":
                if elem[3] == 1:

                    list_day1.append([self.dict_actual[elem[0]], self.dict_actual[elem[2]], math.ceil(self.Invcal.Calculate_Value([elem[4], elem[5], elem[6]])/var_book), math.ceil(elem[7]/120000), math.ceil(elem[8]/245000)])
                    list_unic_day1.add(self.dict_actual[elem[2]])
                elif elem[3] == 2:
                    list_day2.append([self.dict_actual[elem[0]], self.dict_actual[elem[2]], math.ceil(self.Invcal.Calculate_Value([elem[4], elem[5], elem[6]])/var_book), math.ceil(elem[7]/120000), math.ceil(elem[8]/245000)])
                    list_unic_day2.add(self.dict_actual[elem[2]])
                elif elem[3] == 3:
                    list_day3.append([self.dict_actual[elem[0]], self.dict_actual[elem[2]], math.ceil(self.Invcal.Calculate_Value([elem[4], elem[5], elem[6]])/var_book), math.ceil(elem[7]/120000), math.ceil(elem[8]/245000)])
                    list_unic_day3.add(self.dict_actual[elem[2]])
            elif build == "status_2":
                if elem[3] == 1 and elem[1] == build:
                    list_day1.append([self.dict_actual[elem[0]], self.dict_actual[elem[2]], math.ceil(self.Invcal.Calculate_Value([elem[4], elem[5], elem[6]])/var_book), math.ceil(elem[7]/120000), math.ceil(elem[8]/245000)])
                    list_unic_day1.add(self.dict_actual[elem[2]])
                elif elem[3] == 2 and elem[1] == build:
                    list_day2.append([self.dict_actual[elem[0]], self.dict_actual[elem[2]], math.ceil(self.Invcal.Calculate_Value([elem[4], elem[5], elem[6]])/var_book), math.ceil(elem[7]/120000), math.ceil(elem[8]/245000)])
                    list_unic_day2.add(self.dict_actual[elem[2]])
                elif elem[3] == 3 and elem[1] == build:
                    list_day3.append([self.dict_actual[elem[0]], self.dict_actual[elem[2]], math.ceil(self.Invcal.Calculate_Value([elem[4], elem[5], elem[6]])/var_book), math.ceil(elem[7]/120000), math.ceil(elem[8]/245000)])
                    list_unic_day3.add(self.dict_actual[elem[2]])

        list_day1.sort(key=lambda x: x[1])
        list_day2.sort(key=lambda x: x[1])
        list_day3.sort(key=lambda x: x[1])

        list_unic_day1 = sorted(list_unic_day1)
        list_unic_day2 = sorted(list_unic_day2)
        list_unic_day3 = sorted(list_unic_day3)

        self.Text2_Marco2_var.set(self.List_Text(list_unic_day1, list_day1, "text", 0))
        self.Text2_Marco3_var.set(self.List_Text(list_unic_day2, list_day2, "text", 0))
        self.Text2_Marco4_var.set(self.List_Text(list_unic_day3, list_day3, "text", 0))

        self.Text3_Marco2_var.set(self.List_Text(list_unic_day1, list_day1, "number", 3))
        self.Text3_Marco3_var.set(self.List_Text(list_unic_day2, list_day2, "number", 3))
        self.Text3_Marco4_var.set(self.List_Text(list_unic_day3, list_day3, "number", 3))

        self.Text4_Marco2_var.set(self.List_Text(list_unic_day1, list_day1, "number", 4))
        self.Text4_Marco3_var.set(self.List_Text(list_unic_day2, list_day2, "number", 4))
        self.Text4_Marco4_var.set(self.List_Text(list_unic_day3, list_day3, "number", 4))

        self.Text5_Marco2_var.set(self.List_Text(list_unic_day1, list_day1, "number", 2))
        self.Text5_Marco3_var.set(self.List_Text(list_unic_day2, list_day2, "number", 2))
        self.Text5_Marco4_var.set(self.List_Text(list_unic_day3, list_day3, "number", 2))

    def List_Text(self, list1, list2, type_value, elem_list):
        text_day = ""
        for elem1 in list1:
            if type_value == "text":
                text_day += elem1 + "\n"
            elif type_value == "number":
                text_day += "\n"
            for elem2 in list2:
                if elem1 == elem2[1]:
                    text_day += f"      {elem2[elem_list]}\n"
        return text_day

    def Update_List_Button(self, event):
        self.Update_List(self.List1_Filter_var, self.List2_Filter_var)

    def Update_List(self, list1_var, list2_var):

        if list1_var.get() != "-" and list2_var.get() != "-":
            self.start = 1

            if LG.translate_inv(list1_var.get()) == "type_domain_1": #talent
                conexion2 = sqlite3.connect("DB SQL/genshin_db.db")
                query2 = conexion2.execute("""select v.Character, u.Build, v.n1, o.order2, sum(v.t2), sum(v.t3), sum(v.t4), (u.Mora_Ascension + u.Mora_Weapon_Ascension + u.Mora_Talent + u.XP_Character*0.2 + u.XP_Weapon*0.1) as mora, (u.XP_Character) as xp_ch from
                                            (select u.Character, b.tal_1_book_1 as n1, u.Tal1_B1_T2 as t2, u.Tal1_B1_T3 as t3, u.Tal1_B1_T4 as t4 from user as u join db_base as b on u.Character = b.id
                                            union all
                                            select u.Character, b.tal_1_book_2 as n1, u.Tal1_B2_T2 as t2, u.Tal1_B2_T3 as t3, u.Tal1_B2_T4 as t4 from user as u join db_base as b on u.Character = b.id
                                            union all
                                            select u.Character, b.tal_1_book_3 as n1, u.Tal1_B3_T2 as t2, u.Tal1_B3_T3 as t3, u.Tal1_B3_T4 as t4 from user as u join db_base as b on u.Character = b.id
                                            union all
                                            select u.Character, b.tal_2_book_1 as n1, u.Tal2_B1_T2 as t2, u.Tal2_B1_T3 as t3, u.Tal2_B1_T4 as t4 from user as u join db_base as b on u.Character = b.id
                                            union all
                                            select u.Character, b.tal_2_book_2 as n1, u.Tal2_B2_T2 as t2, u.Tal2_B2_T3 as t3, u.Tal2_B2_T4 as t4 from user as u join db_base as b on u.Character = b.id
                                            union all
                                            select u.Character, b.tal_2_book_3 as n1, u.Tal2_B3_T2 as t2, u.Tal2_B3_T3 as t3, u.Tal2_B3_T4 as t4 from user as u join db_base as b on u.Character = b.id
                                            union all
                                            select u.Character, b.tal_3_book_1 as n1, u.Tal3_B1_T2 as t2, u.Tal3_B1_T3 as t3, u.Tal3_B1_T4 as t4 from user as u join db_base as b on u.Character = b.id
                                            union all
                                            select u.Character, b.tal_3_book_2 as n1, u.Tal3_B2_T2 as t2, u.Tal3_B2_T3 as t3, u.Tal3_B2_T4 as t4 from user as u join db_base as b on u.Character = b.id
                                            union all
                                            select u.Character, b.tal_3_book_3 as n1, u.Tal3_B3_T2 as t2, u.Tal3_B3_T3 as t3, u.Tal3_B3_T4 as t4 from user as u join db_base as b on u.Character = b.id
                                            ) as v join user as u on v.Character = u.Character join db_order as o on o.id = v.n1
											where (v.t2 + v.t3 + v.t4) != 0
											group by v.Character, v.n1""")
                conexion2.commit()
            elif LG.translate_inv(list1_var.get()) == "type_domain_2": #mat_weapon
                conexion2 = sqlite3.connect("DB SQL/genshin_db.db")
                query2 = conexion2.execute("select u.Character, u.Build, w.mat_domain_weapon, o.order2, sum(u.Weapon_Material_Domain_Tier2) as matt2, sum(u.Weapon_Material_Domain_Tier3) as matt3, sum(u.Weapon_Material_Domain_Tier4) as matt4, sum(u.Weapon_Material_Domain_Tier5) as matt5, (u.Mora_Ascension + u.Mora_Weapon_Ascension + u.Mora_Talent + u.XP_Character*0.2 + u.XP_Weapon*0.1) as mora, (u.XP_Character) as XP_Character from user as u join db_weapon as w on u.weapon = w.id join db_order as o on o.id = w.mat_domain_weapon group by u.Character, u.Build, w.mat_domain_weapon, o.order2 HAVING (matt2 + matt3 + matt4 + matt5) != 0")
                conexion2.commit()

            self.query2_values = query2.fetchall()

            self.Summary_day()

        if self.start == 1:
            self.Load_Marco()
            self.Load_Images_Dynamic()


import logging
import json
import matching
import requests

optm_logger = logging.getLogger("optmRequest")

def optm_request(dstUrl, insRequest, orderId):
    optmRequest = f'/api/V1/{insRequest.requestCntrId}/{insRequest.requestZoneId}/' \
                  f'order/{insRequest.requestOrderDate}/{orderId}/request'
    url = dstUrl + optmRequest
    optionFile = "option.json"
    with open(optionFile, 'r', encoding='utf-8') as file:
        payload = json.load(file)
        payload = json.dumps(payload)
        optm_logger.info("Optimization Option read")

    headers = {'Content-Type': 'application/json; charset=utf-8',
               "CertNum": matching.getKey(insRequest.dstRequest, insRequest.dstUser)}

    response = requests.request("POST", url, headers=headers, data=payload)

    if response.status_code == 200:
        optm_logger.info("Complete Optimization Request")


from __future__ import annotations

from pathlib import Path

from click.testing import CliRunner, Result

from cdv.cmds.cli import cli

class TestCdvCommands:
    def test_encode_decode(self):
        runner = CliRunner()
        puzhash: str = "3d4237d9383a7b6e60d1bfe551139ec2d6e5468205bf179ed381e66bed7b9788"

        # Without prefix
        result: Result = runner.invoke(cli, ["encode", puzhash])
        address: str = "xch184pr0kfc8fakucx3hlj4zyu7cttw235zqkl308kns8nxhmtmj7yqxsnauc"
        assert result.exit_code == 0
        assert address in result.output
        result = runner.invoke(cli, ["decode", address])
        assert result.exit_code == 0
        assert puzhash in result.output

        # With prefix
        result = runner.invoke(cli, ["encode", puzhash, "--prefix", "txch"])
        test_address: str = "txch184pr0kfc8fakucx3hlj4zyu7cttw235zqkl308kns8nxhmtmj7yqth5tat"
        assert result.exit_code == 0
        assert test_address in result.output
        result = runner.invoke(cli, ["decode", test_address])
        assert result.exit_code == 0
        assert puzhash in result.output

    def test_hash(self):
        runner = CliRunner()
        str_msg: str = "chia"
        b_msg: str = "0xcafef00d"

        result: Result = runner.invoke(cli, ["hash", str_msg])
        assert result.exit_code == 0
        assert "3d4237d9383a7b6e60d1bfe551139ec2d6e5468205bf179ed381e66bed7b9788" in result.output
        result = runner.invoke(cli, ["hash", b_msg])
        assert result.exit_code == 0
        assert "8f6e594e007ca1a1676ef64469c58f7ece8cddc9deae0faf66fbce2466519ebd" in result.output

    def test_test(self):
        runner = CliRunner()
        with runner.isolated_filesystem():
            result: Result = runner.invoke(cli, ["test", "--init"])
            assert result.exit_code == 0
            assert Path("./tests").exists() and Path("./tests/test_skeleton.py").exists()

            # 2023-10-17 Commenting subsequent tests because pytest throws an import file mismatch error
            # It seems that the test_skeleton module is being imported from the test enviroment but is
            # also present in the tests directory, causing the mismatch.
            # This repo has a tool (build-init-files.py) for creating missing __init__ files which may
            # have been intended as a solution to this problem, but as of now it doesn't work.

            # result = runner.invoke(cli, ["test", "--discover"])
            # assert result.exit_code == 0
            # assert "TestSomething" in result.output

            # result = runner.invoke(cli, ["test"])
            # assert result.exit_code == 0
            # assert "test_skeleton.py ." in result.output


import json

class Models(object):
    def __init__(self, model_path):
        self.model_path = model_path
        self.models = self.load_models()

    def load_models(self):
        models = {}
        with open(self.model_path, encoding='utf-8') as f:
            while True:
                line = f.readline()
                if line:
                    model = json.loads(line.strip('\n'))
                    domain = model['domain']
                    model.pop('domain', None)
                    models.update({domain: model})
                else:
                    break
        return models


# Text Data Preprocessing Lib
from data_preprocessing import get_stem_words
import tensorflow
import random
import numpy as np
import pickle
import json
from nltk.stem import WordNetLemmatizer
import nltk
nltk.download('punkt')
nltk.download('wordnet')
lemmatizer = WordNetLemmatizer()

# words to be ignored/omitted while framing the dataset
ignore_words = ['?', '!', ',', '.', "'s", "'m"]

# Model Load Lib

# load the model
model = tensorflow.keras.models.load_model('./chatbot_model.h5')

# Load data files
intents = json.loads(open('./intents.json').read())
words = pickle.load(open('./words.pkl', 'rb'))
classes = pickle.load(open('./classes.pkl', 'rb'))

def preprocess_user_input(user_input):
    bag = [0] * len(words)

    # tokenize the user_input
    user_input_words = nltk.word_tokenize(user_input)

    # convert the user input into its root words: stemming
    user_input_words = [lemmatizer.lemmatize(
        word.lower()) for word in user_input_words]

    # Remove duplicacy and sort the user_input
    user_input_words = sorted(list(set(user_input_words)))

    # Input data encoding: Create BOW for user_input
    for w in user_input_words:
        for i, word in enumerate(words):
            if word == w:
                bag[i] = 1

    return np.array(bag)

def bot_class_prediction(user_input):
    inp = preprocess_user_input(user_input)
    inp = inp.reshape(1, len(inp))

    prediction = model.predict(inp)
    predicted_class_label = np.argmax(prediction[0])

    return predicted_class_label

def bot_response(user_input):
    predicted_class_label = bot_class_prediction(user_input)
    predicted_class = classes[predicted_class_label]

    for intent in intents['intents']:
        if intent['tag'] == predicted_class:
            bot_response = random.choice(intent['responses'])
            return bot_response

print("Hi, I am Stella. How can I help you?")

while True:
    # take input from the user
    user_input = input('Type your message here: ')

    response = bot_response(user_input)
    print("Bot Response: ", response)


from django.contrib.auth import get_user_model
from django.db.models import Q
User=get_user_model()

# rest_framework
from rest_framework import permissions, generics, mixins
from rest_framework.views import APIView
from rest_framework.response import Response

# relative import of serializer
from .serializers import UserModelSerializer, PersonalContactsSerializer

# importing Model
from data.models import PersonalContacts

# Search View Handler API ENDPOINT
class DataListSearchAPIView(APIView):
    def get(self,request,*args, **kwargs):
        if 'p' in request.GET:
            search_phone_number = request.GET['p']
            qs = User.objects.filter(phone_number__iexact=search_phone_number)
            if qs.count()==1:
                serializer = UserModelSerializer(qs, many=True)
                return Response(serializer.data)
            else:
                qs = PersonalContacts.objects.filter(phone_number__iexact=search_phone_number)
                print(qs)
                if qs.count() > 0:
                    serializer = PersonalContactsSerializer(qs, many=True)
                    data={
                        "message":"Number is not registered",
                        "data":serializer.data
                    }
                    return Response(data)
        if 'name' in request.GET:
            search_name = request.GET['name']
            user_qs = User.objects.filter(
                Q(name__startswith=search_name)|
                Q(name__icontains = search_name)
            ).distinct()
            p_qs = PersonalContacts.objects.filter(
                Q(name__startswith=search_name)|
                Q(name__icontains = search_name)
            ).distinct()
            serializer1 = UserModelSerializer(user_qs,many=True)
            serializer2 = PersonalContactsSerializer(p_qs, many=True)
            data={
                'registered_users':serializer1.data,
                'personal_contacts':serializer2.data
            }
            return Response(data)
        return Response({"data":"Search something by name (?name=)  or phone_number (?p=)"},status=200)

# detail view of User model
class UserDetailAPIView(APIView):

    def get(self,request,pk,*args,**kwargs):
        qs=User.objects.get(id=pk)
        serializer = UserModelSerializer(qs)

        # to display email or not
        # user is already registered
        # now check whether searching user is in this user's contact list
        check_qs = PersonalContacts.objects.filter(
            Q(user=qs) &
            Q(phone_number__icontains=request.user.phone_number)
        )
        if check_qs.count()>0:
            data={
                "data":serializer.data,
                "email":qs.email
            }
        else:
            data=serializer.data
        return Response(data)

    def post(self,request,pk,*args,**kwargs):
        q=User.objects.get(id=pk)
        q.spam_count+=1
        q.save()
        serializer = UserModelSerializer(q)
        data={
            "data":serializer.data,
            "message":"{} marked as spam.".format(q.name)
        }
        return Response(data,status=200)

# detail view of Personal contacts model
class PersonalContactsDetailAPIView(APIView):

    def get(self,request,pk,*args,**kwargs):
        qs=PersonalContacts.objects.get(id=pk)
        serializer = PersonalContactsSerializer(qs)
        return Response(serializer.data)

    def post(self,request,pk,*args,**kwargs):
        q=PersonalContacts.objects.get(id=pk)
        q.spam_count+=1
        q.save()
        serializer = PersonalContactsSerializer(q)
        data={
            "data":serializer.data,
            "message":"{} marked as spam.".format(q.name)
        }
        return Response(data,status=200)


#!/usr/bin/env python3
#coding=utf8
from __future__ import (division,absolute_import,print_function,unicode_literals)
import argparse, csv, sys,subprocess,time

def normal(srcs,dess):
    maxs=[0]*5
    for src in srcs:
        inFile=open(src)
        for row in csv.reader(inFile):
            for i in range(5):
                feat=float(row[i])
                if feat>maxs[i]:
                    maxs[i]=feat
        inFile.close()
    print(maxs)
    for src,des in zip(srcs,dess):
        outFile=open(des,'w')
        inFile=open(src)
        feats=['0']*5
        for row in csv.reader(inFile):
            for i in range(5):
                feats[i]=str(float(row[i])/maxs[i])
            outFile.write(','.join(feats)+'\n')
        inFile.close()
        outFile.close()

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('outPath', type=str)
    args = vars(parser.parse_args())
    outPath=args['outPath']

    normal([outPath+'new_train_nonormal.csv',outPath+'new_test_nonormal.csv'],[outPath+'new_train.csv',outPath+'new_test.csv'])

main()


# Exercise: Build a model that returns the log-softmax as the output and calculate the loss using the negative log
# likelihood loss. Note that for nn.LogSoftmax and F.log_softmax you'll need to set the dim keyword argument
# appropriately. dim=0 calculates softmax across the rows, so each column sums to 1, while dim=1 calculates across the
# columns so each row sums to 1. Think about what you want the output to be and choose dim appropriately.

import torch
from torch import nn
import torch.nn.functional as F
from torchvision import datasets, transforms

# Define a transform to normalize the data
transform = transforms.Compose([transforms.ToTensor(),
                                transforms.Normalize((0.5, ), (0.5, )),
                              ])

# Download and load the training data
trainset = datasets.MNIST('MNIST_data\\', download=True, train=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)

# Build a feed-forward network
model = nn.Sequential(nn.Linear(784, 128),
                      nn.ReLU(),
                      nn.Linear(128, 64),
                      nn.ReLU(),
                      nn.Linear(64, 10),
                      nn.LogSoftmax(dim=1)) # dim=1 logs corresponds to columns instead of rows

# Define the loss
criterion = nn.NLLLoss()

### Run this to check your work
# Get our data
images, labels = next(iter(trainloader))
# Flatten images
images = images.view(images.shape[0], -1)

# Forward pass, get our logits
log_ps = model(images)
# Calculate the loss with the logits and the labels
loss = criterion(log_ps, labels)

print(loss)


import random

from src.utils import read_data, write_data
from typing import List, Dict
import re
from tqdm import tqdm
from collections import Counter

def have_constant(target_template: List) -> bool:
    for val in target_template:
        if val.strip() == "1":
        # if val.strip() == "pi" or val.strip() == "PI":
            return True
    return False

def have_pi(target_template: List) -> bool:
    for val in target_template:
        if val.strip() == "PI":
        # if val.strip() == "pi" or val.strip() == "PI":
            return True
    return False

def have_square(target_template: List) -> bool:
    for val in target_template:
        if val.strip() == "^":
            return True
    return False

def count_variable(target_template: List) -> int:
    num_vars = set()
    for val in target_template:
        if val.strip().startswith("temp_"):
            num_vars.add(val.strip())
    return len(num_vars)

def have_multiple_m0(target_template: List):
    target_string = ' '.join(target_template)
    target_string = target_string.replace("()", "").replace("( )", "")
    target_string = re.sub(r"\(.*\)", "temp_m", target_string)
    target_template = target_string.split()
    high_priority_symbol_pos = []
    for idx, val in enumerate(target_template):
        if val in {"*", "/"}:
            high_priority_symbol_pos.append(idx)
    for prev, next in zip(high_priority_symbol_pos[:-1], high_priority_symbol_pos[1:]):
        if next - prev != 2:
            return True
    return False

def check_in_labels(current_tuple, labels):
    if current_tuple in labels:
        return current_tuple
    if current_tuple[-1] in {'+', '*'} and [current_tuple[1], current_tuple[0], current_tuple[-1]] in labels:
        return [current_tuple[1], current_tuple[0], current_tuple[-1]]
    return None

def get_labels(target_norm_post_template: List, target_template: List, remove_duplicate: bool = False):
    assert target_norm_post_template[:2] == ["x", "="]
    if len(target_norm_post_template) == 3:
        assert target_norm_post_template[2].startswith("temp_")
        target_norm_post_template.append("1")
        target_norm_post_template.append("*")
    stack = []
    pointer = 2
    labels = []
    both_m = False
    eq_2_m = {}
    got_duplicate = False
    while pointer != len(target_norm_post_template):
        stack.append(target_norm_post_template[pointer])
        if stack[-1] in {'+', '-', '*', '/', '^'}:
            if len(stack[-3:]) == 3:
                if stack[-3].startswith("m_") and stack[-2].startswith("m_"):
                    both_m = True
                if remove_duplicate:
                    checker = check_in_labels([stack[-3], stack[-2], stack[-1]], labels)
                    if checker:
                        got_duplicate = True
                        m_string = eq_2_m[' '.join(checker)]
                    else:
                        labels.append([stack[-3], stack[-2], stack[-1]])
                        m_string = f"m_{len(labels)}"
                        eq_2_m[' '.join([stack[-3], stack[-2], stack[-1]])] = m_string
                else:
                    labels.append([stack[-3], stack[-2], stack[-1]])
                    m_string = f"m_{len(labels)}"
                stack.pop()
                stack.pop()
                stack.pop()
                stack.append(m_string)
        pointer += 1
    for i, (left, right, op) in enumerate(labels):
        # left = left[-1:] if left.startswith("temp_") else left
        # right = right[-1:] if right.startswith("temp_") else right
        if left.startswith("m_") or right.startswith("m_"):
            if left.startswith("m_") and right.startswith("m_"):
                left_is_smaller = (ord(left[-1:]) - ord(right[-1:])) <= 0
                modified_op = op + "_rev" if op in {'-', '/', '^'} and (not left_is_smaller) else op
                labels[i] = [left, right, modified_op] if left_is_smaller else [right, left, modified_op]
            elif right.startswith("m_"):
                modified_op = op + "_rev" if op in {'-', '/', '^'} else op
                labels[i] = [right, left, modified_op]
        else:
            if left.startswith("temp_") or right.startswith("temp_"):
                if left.startswith("temp_") and right.startswith("temp_"):
                    left_is_smaller = (ord(left[-1:]) - ord(right[-1:])) <= 0
                    modified_op = op + "_rev" if op in {'-', '/', '^'} and (not left_is_smaller) else op
                    labels[i] = [left, right, modified_op] if left_is_smaller else [right, left, modified_op]
                elif right.startswith("temp_"):
                    modified_op = op + "_rev" if op in {'-', '/', '^'} else op
                    labels[i] = [right, left, modified_op]
                else:
                    assert right in {"1", "PI"}
            else:
                pass
                # raise NotImplementedError(f"all constant for label: {labels[i]}")

    for i, (left, right, op) in enumerate(labels):
        left = left[-1:] if left.startswith("temp_") else left
        right = right[-1:] if right.startswith("temp_") else right
        # if (left == "PI" or right == "PI"):# and op not in {'*', '/'}:
        #     print(labels[i])
        labels[i] = [left, right, op]

    max_temp_org = max([v for v in target_template if v.startswith("temp_")])
    max_temp_update = max([v for v in target_norm_post_template if v.startswith("temp_")])
    gap = ord(max_temp_org[-1]) - ord(max_temp_update[-1])
    if gap > 0:
        for i, (left, right, op) in enumerate(labels):
            left = chr(ord(left) + gap) if len(left) == 1 and ord(left) >= ord('a') and ord(left) <= ord('z') else left
            right = chr(ord(right) + gap) if len(right) == 1 and ord(right) >= ord('a') and ord(right) <= ord('z') else right
            labels[i] = [left, right, op]
    return labels, both_m, gap, got_duplicate

def check_intermediate_m_in_order(labels: List[List[str]]):
    current_m_idx = 0
    for idx, (left_var, right_var, op) in enumerate(labels):
        if left_var.startswith("m_"):
            # try:
            assert int(left_var[2:]) - current_m_idx == 1
            # except:
            #     print("not incremental")
            current_m_idx += 1
    return True

def process_obj(obj: Dict, remove_duplicate: bool = False):
    target_template = [val.strip() for val in obj["target_template"]]

    labels, have_both_m, gap, got_duplicate = get_labels(obj["target_norm_post_template"], obj["target_template"], remove_duplicate)
    type_str = "legal"

    # if count_variable(target_template) > 7: ## only 2 in test
    #     type_str = "variable more than 7"
    #     return type_str, labels, gap

    # if have_constant(target_template):
    #     type_str = "have constant"
    #     return type_str, labels
    #
    # if have_pi(target_template):
    #     type_str = "have pi"
    #     print(obj["equation"], obj["target_template"])
    #     return type_str, labels

    if have_square(target_template): ## only 1 in test
        type_str = "have square"
        return type_str, labels, gap, False

    # if have_both_m:
    #     type_str = "have both m0, m1"
    #     return type_str, labels, gap

    # have_same_variable = []
    # for idx, curr_labels in enumerate(labels):
    #     if curr_labels[0] == curr_labels[1]:
    #         have_same_variable.append(idx)
    # if len(have_same_variable) > 0:
    #     if len(have_same_variable) == 1 and have_same_variable[0] == 0:
    #         return "legal", labels, gap
    #     else:
    #         return "have same variable at multiple layer", labels, gap

    # if have_multiple_m0(target_template):
    #     type_str = "have mutiple m0"
    #     return type_str, labels, gap

    return type_str, labels, gap, got_duplicate

def main():
    remove_duplicate = True
    for in_file in ["train23k_processed.json", "valid23k_processed.json", "test23k_processed.json"]:
        print(f"working on... {in_file}")
        in_file = f"../data/math23k/{in_file}"
        if remove_duplicate:
            out_file = in_file.split(".json")[0] + "_nodup_more.json"
        else:
            out_file = in_file.split(".json")[0] + "_all.json"
        data = read_data(in_file)
        count = Counter()
        inst_num_with_gap = 0
        duplicate_num = 0
        for obj in tqdm(data, desc="processing data", total=len(data)):
            type_str, labels, gap, got_duplicate = process_obj(obj, remove_duplicate=remove_duplicate)
            if len(labels) == 0:
                assert len(obj["target_norm_post_template"]) == 3
                print("something", obj["num_list"], obj["equation"])
            if gap > 0:
                inst_num_with_gap += 1
            count[type_str] += 1
            obj["type_str"] = type_str
            obj["equation_layer"] = labels
            duplicate_num += 1 if got_duplicate else 0
            # if type_str == "legal":
            #     check_intermediate_m_in_order(labels)
        # write_data(file=out_file, data = data)

        print(inst_num_with_gap)
        print(f" duplication number: {duplicate_num}")
        for key in count:
            print(f"{key}, valid number: {count[key]}, total: {len(data)}, %: {count[key] * 1.0 / len(data) * 100:.2f}")

def get_five_folds():
    random.seed(42)
    import os
    all_data = []
    for in_file in ["train23k_processed_nodup.json", "valid23k_processed_nodup.json", "test23k_processed_nodup.json"]:
        print(f"working on... {in_file}")
        in_file = f"../data/math23k/{in_file}"
        all_data.append(read_data(in_file))
    all_data = all_data[0] + all_data[1] + all_data[2]
    random.shuffle(all_data)
    num_fold = 5
    fold_size = len(all_data) // num_fold
    output_folder= "math23k_five_fold"
    os.makedirs(f"../data/math23k_five_fold", exist_ok=True)
    for i in range(num_fold):
        if i == num_fold - 1:
            test_data = all_data[i * fold_size:]
            train_data = all_data[:i * fold_size]
        else:
            test_data = all_data[i * fold_size:(i + 1) * fold_size]
            train_data = all_data[:i * fold_size] + all_data[(i + 1) * fold_size:]
        size = len(train_data) + len(test_data)
        print(f"total size : {size}, train: {len(train_data)}, test: {len(test_data)}")
        write_data(file=f"../data/{output_folder}/train_{i}.json", data=train_data)
        write_data(file=f"../data/{output_folder}/test_{i}.json", data=test_data)

if __name__ == '__main__':
    # text = "a () * c"
    # print(re.sub(r"\(.*\)", "temp_m", text))
    # main()
    get_five_folds()
    # print(breakit('(((a+b)+a)+c)'))


import psycopg2

import warnings
warnings.filterwarnings('ignore')

import sys

# setting path
sys.path.append('.')

from connection import create_db_connection

def execute_sql(conn, sql):
    # Create a new cursor
    try:
        cur = conn.cursor()
        cur.execute(sql)
        updated_rows = cur.rowcount
        # Commit the changes to the database
        conn.commit()
        # Close cursor
        cur.close()
        return updated_rows
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
        #logging.error(error)
        return None

sql1 = """
        INSERT INTO public.hse_analytics_mape
        ( type_id,
          created_at,
          created_by)
        VALUES(1, '2022-12-02 00:00:00', 'PYTHON');

        INSERT INTO public.hse_analytics_mape
        ( type_id,
          created_at,
          created_by)
        VALUES(2, '2022-12-02 00:00:00', 'PYTHON');

       """

# Add the arguments to the parser
#ap = argparse.ArgumentParser()
#ap.add_argument("-y", "--year", required=True, help="Year data to truncate")

# Connect to database
# Exit program if not connected to database
conn = create_db_connection(section='postgresql_ml_hse_skk')
if conn == None:
    exit()
else:
    updated_rows = execute_sql(conn, sql1)
    if updated_rows != None:
        print("Initial rows has been inserted")


import argparse
import sys

parser = argparse.ArgumentParser(
        description="Train a RL net to play in an OpenAI Gym environment.",
        allow_abbrev=False
    )
parser.add_argument("-p", "--parameters", help="Path to JSON parameters file.")
parser.add_argument("--offline_train", action='store_true')
raw_args = sys.argv[1:]
args = parser.parse_args(raw_args)
print(raw_args)
print(args)


#
# @lc app=leetcode id=240 lang=python3
#
# [240] Search a 2D Matrix II
#

# @lc code=start
from typing import List

class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        m = len(matrix)
        n = len(matrix[0])

        r, c = 0, n - 1
        while r < m and c >= 0:
            if matrix[r][c] == target:
                return True
            if matrix[r][c] > target:
                c -= 1
            else:
                r += 1

        return False

# @lc code=end


import cv2
import numpy as np
import matplotlib.pyplot as plt
im=cv2.imread('ff.jpg')
h,w,c=im.shape

rim=np.zeros([h,w,3])
gim=np.zeros([h,w,3])
bim=np.zeros([h,w,3])

for i in range (h):
    for j in range(w):
        for k in range(3):
            rim[i][j][k]=im[i][j][2]
            gim[i][j][k]=im[i][j][1]
            bim[i][j][k]=im[i][j][0]

rim = rim.astype(np.uint8)
gim = gim.astype(np.uint8)
bim = bim.astype(np.uint8)

fig = plt.figure(figsize=(18, 18))
pltX = 1
pltY = 4
fig.add_subplot(pltX, pltY, 1)
plt.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.title(" ORIGINAL IMAGE ")

fig.add_subplot(pltX, pltY, 2)
plt.imshow(cv2.cvtColor(rim,cv2.COLOR_BGR2RGB ))
plt.axis('off')
plt.title(" RED CHANNEL IMAGE ")
fig.add_subplot(pltX, pltY, 3)
plt.imshow(cv2.cvtColor(gim,cv2.COLOR_BGR2RGB ))
plt.axis('off')
plt.title(" GREEN CHANNEL IMAGE ")
fig.add_subplot(pltX, pltY, 4)
plt.imshow(cv2.cvtColor(bim,cv2.COLOR_BGR2RGB ))
plt.axis('off')
plt.title(" BLUE CHANNEL IMAGE ")
fig.show()
fig.waitforbuttonpress()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 22 18:50:27 2018

@author: victor
"""
import numpy as np
import matplotlib.pyplot as plt

'''
DESCRIPTION

PrimeGen TAKES IN A POSITIVE NUMBER, n, AND GENERATES ALL THE PRIMES BETWEEN 1
AND n, INCLUDING n, AND CHECKS IF IT IS A PRIME USING PrimeCheck1. PrimeGen2
DOES THE SAME BUT WITH PrimeCheck2.

PrimeCheck1 TAKES IN A REAL NUMBER, someN, AND CHECKS IF IT IS A PRIME BY BRUTE
FORCE. PrimeCheck2 DOES THE SAME BUT UP TO THE SQUARE ROOT OF someN. HERE'S
THE PROOF:

https://stackoverflow.com/questions/5811151/why-do-we-check-up-to-the-
square-root-of-a-prime-number-to-determine-if-it-is-pr

PARAMETERS FOR...

PrimeCheck
    someN - LITERALLY SOME INTEGER. IT TAKES ANY INTEGER AND CHECKS WHETHER
    IT IS A PRIME OR NOT. SINCE WE ALREADY CHECK IF SAID NUMBER'S BIGGER THAN 1

    opt_p_check - THIS IS A BOOLEAN. IF IT'S TRUE, OR NOT IN AT ALL, THEN IT'LL
    USE THE OPTIMIZED METHOD. OTHERWISE, IT USES THE REGULAR METHOD.

PrimeGen
    n - THIS NUMBER IS OUR UP-TO VALUE TO CHECK FOR PRIMES.

    opt_p_check - THIS IS A BOOLEAN INPUT. TRUE BY DEFAULT, IT'LL USE
    PrimeCheck2. ELSE, IT'LL USE PrimeCheck1.

PrimePlot
    SAME AS PrimeGen

DESCRIPTION
'''

# HELPER FUNCTIONS

def PrimeCheck(someN, opt_p_check=True):
    if opt_p_check:
        up_to = int(np.sqrt(someN)) + 1
    else:
        up_to = someN
    for kk in range(2, up_to):
        if someN % kk == 0:
            return False
    return True

# HELPER FUNCTIONS

# TABLE OF CONTENTS --------------------------------------------------------- #
#   1
#       1.1
#       1.2

# TABLE OF CONTENTS --------------------------------------------------------- #

# CONTENT ------------------------------------------------------------------- #
#   1
#       1.1

# CONTENT ------------------------------------------------------------------- #

def PrimeGen(n, opt_p_check=True):
    if n < 2:
        strg = 'Prime numbers are natural numbers greater than 1.'
        print(strg)
        return 0
    if n == 2:
        return [2]
    primeLst = [2]
    for ii in range(3, n + 1, 2):
        if PrimeCheck(ii, opt_p_check):
            primeLst.append(ii)
    return primeLst

def PrimePlot(n, opt_p_check=True, rtrn_bool=False):
    primeLst = PrimeGen(n, opt_p_check)
#                        +--> FROM THE VALUE 1...
#                        |           +--> TO SIZE OF LIST...
#                       _|_ _________|________
    nthPrime = np.arange(1, len(primeLst) + 1)
    plt.plot(nthPrime, primeLst)
    plt.grid()
    plt.ylabel('Prime')
    plt.xlabel('Index of prime')
    plt.title('Prime plot')
    if rtrn_bool:
        return primeLst