from aghast.ast import ImportModuleSection, ImportStatement, SingleImport
from aghast.param import x
from aghast.parser import AbstractParser
from aghast.tokens import Identifier, Indent, Keywords, Symbols, Whitespace

class ImportParser(AbstractParser):
    def __iter__(self):
        token = self.token_stream.peek(Keywords('import'), Keywords('from'))
        self.tokens.append(self.token_stream.next())

        needs_module_section = False
        if token == Keywords('from'):
            needs_module_section = True

        self.token_stream.peek(Whitespace(x))
        self.tokens.append(self.token_stream.next())

        if needs_module_section:
            module_section = yield from ModuleSection(self.token_stream)

        token = self.token_stream.peek(Keywords('import'))
        self.tokens.append(self.token_stream.next())

        imports = yield from ImportSection(self.token_stream)

        return ImportStatement(
            tokens=self.tokens,
            module_section=module_section,
            imports=imports,
        )

class ModuleSection(AbstractParser):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.local = False
        self.local_path_index = 0

        self.seen_first_module = False
        self.last_token = None
        self.modules = []

    def __iter__(self):
        token = self.token_stream.peek()

        if token == Symbols.PERIOD:
            self.local = True
            token = self.token_stream.next()

        while self.token_stream:
            token = self.token_stream.peek(Whitespace(x), Identifier(x), Symbols.PERIOD)

            if token == Whitespace(x):
                # So the module section is done now!
                self.token_stream.next()
                yield ImportModuleSection(
                    self.tokens,
                    self.local,
                    self.local_path_index,
                    self.modules,
                )
                break

            if token == Symbols.PERIOD:
                if self.seen_first_module and self.last_token == Symbols.PERIOD:
                    raise ValueError('period invalid here')

                if not self.seen_first_module:
                    self.local = True
                    self.local_path_index -= 1

            elif token == Identifier(x):
                self.seen_first_module = True
                self.modules.append(token)

            self.tokens.append(self.token_stream.next())

class ImportSection(AbstractParser):
    """Parse the import section of an import statement.

    e.g. from a import <this section>

    TODO:
    * as
    * like all the edge cases, currently only handles the one happy path.
    """
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.require_close_parens = False
        self.need_comma = False
        self.imports = []

    def __iter__(self):
        token = self.token_stream.peek()
        if token == Whitespace(x):
            self.token_stream.next()

        token = self.token_stream.peek()
        if token == Symbols.PAREN_OPEN:
            self.require_close_parens = True
            self.token_stream.next()

        while self.token_stream:
            token = self.token_stream.peek(
                Identifier(x), Symbols.COMMA, Whitespace(x), Indent(x), Symbols.PAREN_CLOSE,
            )
            if token == Whitespace(x):
                self.tokens.append(self.token_stream.next())
                continue

            if token == Indent(x):
                if not self.require_close_parens:
                    break

                self.tokens.append(self.token_stream.next())
                continue

            if token == Identifier(x):
                self.imports.append(token)
                self.tokens.append(self.token_stream.next())
                self.need_comma = True
                continue

            if token == Symbols.COMMA:
                self.tokens.append(self.token_stream.next())
                self.need_comma = False
                continue

            if token == Symbols.PAREN_CLOSE:
                self.tokens.append(self.token_stream.next())
                if self.require_close_parens:
                    break
                raise ValueError('wat')

        yield [
            SingleImport(self.tokens, import_name, alias=None)
            for import_name in self.imports
        ]


"""
Environment

"""
# numpy
import numpy as np
# pytorch
import torch as T
# misc
import pdb # use with set_trace() for the debugger

class CliffEnv():
    # constructor
    def __init__(self, params):
        # parameters and spaces
        self.params = params
        self.spaces = {'t_space' : np.arange(params["T"]),
                      'pos_space' : np.linspace(-1, 12, 51),
                      'u_space' : np.linspace(-params["max_u"], params["max_u"], 41)}
        self.device = T.device('cuda:0' if T.cuda.is_available() else 'cpu')

    # initialization of the environment with its true initial state
    def reset(self, Nsims=1):
        pos = T.zeros(Nsims)

        return pos

    # initialization of the environment with multiple random states
    def random_reset(self, time, Nsims=1):
        temp = time*(time <= self.params["T"]/2) + self.params["T"]/2*(time > self.params["T"]/2)
        pos = temp*(self.params["max_u"]+2.3*self.params["sigma"])*T.rand(Nsims, device=self.device)

        return pos

    # simulation engine
    def step(self, pos_t, time_t, u_t):
        sizes = pos_t.shape

        pos_tp1 = pos_t + u_t
        time_tp1 = time_t + 1

        r = - self.params["C_time"]*T.ones(sizes) \
            - self.params["C_cliff"]*((pos_tp1 < self.params["cliff"]) & (time_tp1 != self.params["T"]-1)) \
            - self.params["C_move"](u_t)

        return pos_tp1, time_tp1, -r

    # terminal penalty on the inventory
    def get_final_cost(self, pos_T):
        r = -self.params["C_terminal"](pos_T)

        return -r


"""
多头版的 GeneralMemoryBank
"""
import torch
from torch import nn

from finedial.modules.memory_bank.GeneralMemoryBank import GeneralMemoryBankAgent

class MultiHeadGeneralMemoryBankAgent(nn.Module):
    memory_bank: object
    memory_bank_length: object
    memory_bank_value: object

    def __init__(self, agent_name, attn_params, attn_input_param, head_names, placeholder_mode):
        num_heads = len(head_names)
        assert num_heads > 1, 'head number must larger than 1'
        super(MultiHeadGeneralMemoryBankAgent, self).__init__()
        heads = [GeneralMemoryBankAgent(agent_name+'_'+head_name, attn_params, attn_input_param, placeholder_mode)
                 for head_name in head_names]
        self.heads = nn.ModuleDict()
        self.copy_mode = False
        self.placeholder_mode = False
        for head_name, head in zip(head_names, heads):
            self.heads[head_name] = head
            self.copy_mode = head.copy_mode
            self.placeholder_mode = head.placeholder_mode

    def init_memory_bank(self, memory_bank, memory_bank_value, memory_bank_length, time_major=False):
        for head_name in self.heads:
            head = self.heads[head_name]
            head.init_memory_bank(memory_bank, memory_bank_value, memory_bank_length, time_major)

    def create_beams(self, beam_width):
        for head_name in self.heads:
            head = self.heads[head_name]
            head.create_beams(beam_width)

    def get_output_dim(self):
        """
        Attention的输出维度
        """
        res = 0
        for head_name in self.heads:
            head = self.heads[head_name]
            res += head.get_output_dim()
        return res

    def get_gen_mode_name(self):
        """
        当前的Memory是否可以进行拷贝
        """
        res = []
        for head_name in self.heads:
            head = self.heads[head_name]
            res += head.get_gen_mode_name()
        return res

    def get_gen_mode_num(self):
        """
        当前的Memory是否可以进行拷贝
        """
        res = 0
        for head_name in self.heads:
            head = self.heads[head_name]
            res += head.get_gen_mode_num()
        return res

    def access(self, query, output_distribution=False):
        """
            Args:
                query : [batch, query_dim]
                output_distribution :
            Returns:
                attention_vector:
        """
        res = []
        distributions = []
        for head_name in self.heads:
            head = self.heads[head_name]
            if output_distribution:
                tmp = head.access(query, output_distribution)
                res.append(tmp[0])
                distributions.append(tmp[1])
            else:
                res.append(head.access(query))

        if output_distribution:
            return torch.cat(res, -1), distributions
        else:
            return torch.cat(res, -1)

    def generate(self, query, pad_to_max=True):
        res = []
        for head_name in self.heads:
            head = self.heads[head_name]
            res.append(head.generate(query, pad_to_max))
        return res

    def create_vocab_predictor(self, input_size, hidden_size, fixed_vocab_size, dropout, vocab_predictor_mode):
        for head in self.heads.values():
            head.create_vocab_predictor(input_size, hidden_size, fixed_vocab_size, dropout, vocab_predictor_mode)

    def generate_vocab(self, query, state, dynamic_projections=None, pad_to_max=True):
        res = []
        for head in self.heads.values():
            tmp = head.generate_vocab(query, state, dynamic_projections, pad_to_max)
            res += tmp
        return res


import numpy as np
import re

space = "\t"

#2019/8/18 修正4 16 18 19这4个特征
class ConnectionFeatures(object):
    def __init__(self, key, tuple_list, tcp_list, label, delta_time, sub_set):
        self.key = key
        self.tuple_list = tuple_list
        self.tcp_list = tcp_list
        self.label = label
        self.delta_time = 0
        self.delta_time = delta_time

        self.record_num = 0
        self.cert_dic = {}
        self.cert_length_dic = {}
        self.duration_list = []
        self.origin_bytes_total = 0
        self.destination_bytes_total = 0
        self.origin_pkts_total = 0
        self.destination_pkts_total = 0
        self.established_num = 0
        self.timestamp_list = []
        self._1nd_list = []
        self._2nd_list = []
        self.tls_ssl_num = 0
        self.tls_num = 0
        self.SNI_num = 0
        self.SNI_as_IP_num = 0
        self.SNI_not_as_IP_num = 0
        self.SNI_as_IP = -1
        self.has_cert_num = 0
        self.self_signed_num = 0
        self.certPath_length_list = []
        self.valid_length_list = []
        self.valid_num = 0
        self.valid_ratio_list = []
        self.SANdns_num_list = []
        self.x509_line_num = 0
        self.SNI_in_SANdns_list = []
        self.CN_in_SANdns_list = []

        self.pre_process()

    def pre_process(self):
        self.record_num = len(self.tuple_list) + len(self.tcp_list)
        for item in self.tuple_list:
            ssl_line = item["ssl"]
            ssl_elements = ssl_line.split(space)
            version = ssl_elements[6]
            if "TLS" in version or "SSL" in version:
                self.tls_ssl_num += 1
                if "TLS" in version:
                    self.tls_num += 1
            SNI = ssl_elements[9]
            if SNI != "-":
                self.SNI_num += 1
                if len(re.findall(r'\d+\.\d+\.\d+\.\d+', SNI)) != 0:
                    dstIP = self.key.split("/")[1]
                    if dstIP == SNI:
                        self.SNI_as_IP = 1
                        self.SNI_as_IP_num += 1
                    else:
                        self.SNI_as_IP = 0.5
                        self.SNI_not_as_IP_num += 1

            certPath = ssl_elements[14]
            if certPath == "-":
                self.certPath_length_list.append(0)
            else:
                self.has_cert_num += 1
                cert_uids = certPath.split(",")
                self.certPath_length_list.append(len(cert_uids))

            if 'signed certificate in certificate' in ssl_line:
                self.self_signed_num += 1
            #
            conn_line = item["conn"]
            conn_elements = conn_line.split(space)
            duration = conn_elements[8]
            if duration == "-":
                duration = "0"
            self.duration_list.append(float(duration))
            origin_bytes, destination_bytes = conn_elements[9], conn_elements[10]
            if origin_bytes == "-":
                origin_bytes = "0"
            self.origin_bytes_total += int(origin_bytes)
            if destination_bytes == "-":
                destination_bytes = "0"
            self.destination_bytes_total += int(destination_bytes)
            established_state = conn_elements[11]
            if established_state in ['SF', 'S1', 'S2', 'S3', 'RSTO', 'RSTR']:
                self.established_num += 1
            origin_pkts, destination_pkts = conn_elements[16], conn_elements[18]
            if origin_pkts == "-":
                origin_pkts = "0"
            self.origin_pkts_total += int(origin_pkts)
            if destination_pkts == "-":
                destination_pkts = "0"
            self.destination_pkts_total += int(destination_pkts)
            self.timestamp_list.append(float(conn_elements[0]))
            #
            if item["x509"]:
                self.x509_line_num += 1
                x509_elements = item["x509"].split(space)
                cert_sertial, cert_length = x509_elements[3], x509_elements[11]
                try:
                    if self.cert_dic[cert_sertial]:
                        self.cert_dic[cert_sertial] += 1
                except:
                    self.cert_dic[cert_sertial] = 1
                try:
                    if self.cert_length_dic[cert_length]:
                        self.cert_length_dic[cert_length] += 1
                except:
                    self.cert_length_dic[cert_length] = 1
                if x509_elements[6] != "-" and x509_elements[7] != "-":
                    from_, to_ = float(x509_elements[6]), float(x509_elements[7])
                    valid = round((to_ - from_) / (3600 * 24), 2)
                    self.valid_length_list.append(valid)
                    if self.delta_time == "":
                        self.delta_time = "0"
                    timestamp = float(x509_elements[0]) + float(self.delta_time)
                    if (timestamp < to_) and (timestamp > from_):
                        self.valid_num += 1
                    ratio = (timestamp - from_) / (to_ - from_)
                    self.valid_ratio_list.append(ratio)
                if x509_elements[14] is "-":
                    self.SANdns_num_list.append(0)
                else:
                    num = len(x509_elements[14].split(","))
                    self.SANdns_num_list.append(num)
                    if SNI != "-":
                        if SNI in x509_elements[14]:
                            self.SNI_in_SANdns_list.append(1)
                        else:
                            self.SNI_in_SANdns_list.append(0)
                    if x509_elements[4] != "-":
                        CN = x509_elements[4].split(",")[0][3:].replace("*", "")
                        if CN in x509_elements[14]:
                            self.CN_in_SANdns_list.append(1)
                        else:
                            self.CN_in_SANdns_list.append(0)

        for item in self.tcp_list:
            tcp_elements = item.split(space)
            duration = tcp_elements[8]
            if duration == "-":
                duration = "0"
            self.duration_list.append(float(duration))
            origin_bytes, destination_bytes = tcp_elements[9], tcp_elements[10]
            if origin_bytes == "-":
                origin_bytes = "0"
            self.origin_bytes_total += int(origin_bytes)
            if destination_bytes == "-":
                destination_bytes = "0"
            self.destination_bytes_total += int(destination_bytes)
            established_state = tcp_elements[11]
            if established_state in ['SF', 'S1', 'S2', 'S3', 'RSTO', 'RSTR']:
                self.established_num += 1
            origin_pkts, destination_pkts = tcp_elements[16], tcp_elements[18]
            if origin_pkts == "-":
                origin_pkts = "0"
            self.origin_pkts_total += int(origin_pkts)
            if destination_pkts == "-":
                destination_pkts = "0"
            self.destination_pkts_total += int(destination_pkts)
            self.timestamp_list.append(float(tcp_elements[0]))
    #1
    def get_record_num(self):
        return self.record_num
    # 2
    def get_duration_mean(self):
        return np.mean(self.duration_list)
    # 3
    def get_duration_std(self):
        return np.std(self.duration_list)
    # 4
    def get_ratio_duraion_exceed(self):
        duration_max = np.mean(self.duration_list) + np.std(self.duration_list)
        duration_min = np.mean(self.duration_list) - np.std(self.duration_list)
        num = 0
        for duration in self.duration_list:
            if duration > duration_max or duration < duration_min:
                num += 1
        return float(num / self.get_record_num())
    # 5
    def get_origin_bytes(self):
        return self.origin_bytes_total
    # 6
    def get_destination_bytes(self):
        return self.destination_bytes_total
    # 7
    def get_ratio_bytes(self):
        return self.destination_bytes_total/(self.origin_bytes_total+self.destination_bytes_total)
    # 8
    def get_ratio_established(self):
        return float(self.established_num/self.get_record_num())
    # 9
    def get_origin_pkts(self):
        return self.origin_pkts_total
    # 10
    def get_destination_pkts(self):
        return self.destination_pkts_total
    # 11
    def get_2nd_mean(self):
        if self.get_record_num() <= 2:
            return -1
        self.timestamp_list.sort()
        i, j = 1, 1
        while i < self.get_record_num():
            self._1nd_list.append(self.timestamp_list[i] - self.timestamp_list[i - 1])
            i = i + 1
        while j < (self.get_record_num() - 1):
            self._2nd_list.append(self._1nd_list[j] - self._1nd_list[j - 1])
            j = j + 1
        return np.mean(self._2nd_list)
    # 12
    def get_2nd_std(self):
        if self.get_record_num() <= 2:
            return -1
        return np.std(self._2nd_list)
    # 13
    def get_ratio_tcp(self):
        return float(len(self.tcp_list) / self.get_record_num())
    # 14
    def get_ratio_tls(self):
        if self.tls_ssl_num == 0:
            return -1
        return float(self.tls_num / self.tls_ssl_num)
    # 15
    def get_ratio_SNI(self):
        return float(self.SNI_num / len(self.tuple_list))
    # 16
    def get_SNI_as_IP(self):
        if (self.SNI_num != 0) and (self.SNI_as_IP != 0.5) and (self.SNI_as_IP != 1):
            self.SNI_as_IP = 0
        if (self.SNI_as_IP_num != 0) and (self.SNI_not_as_IP_num != 0):
            self.SNI_as_IP = 0.75
        return self.SNI_as_IP
    # 17
    def get_certPath_length_mean(self):
        if self.has_cert_num == 0:
            return -1
        return np.mean(self.certPath_length_list)
    # 18
    def get_ratio_self_signed(self):
        if self.has_cert_num == 0:
            return -1
        return float(self.self_signed_num / len(self.tuple_list))
    # 19
    def get_key_length_mean(self):
        if len(self.cert_length_dic.keys()) == 0:
            return -1
        length, num = 0, 0
        for key in self.cert_length_dic:
            num += int(self.cert_length_dic[key])
            length += int(key) * int(self.cert_length_dic[key])
        return length/num
    # 20
    def get_valid_length_mean(self):
        if len(self.valid_length_list) == 0:
            return -1
        return np.mean(self.valid_length_list)
    # 21
    def get_valid_length_std(self):
        if len(self.valid_length_list) == 0:
            return -1
        return np.std(self.valid_length_list)
    # 22
    def get_ratio_valid(self):
        if len(self.valid_length_list) == 0:
            return -1
        return float(self.valid_num / len(self.valid_length_list))
    # 23
    def get_valid_ratio_mean(self):
        if len(self.valid_ratio_list) == 0:
            return -1
        return np.mean(self.valid_ratio_list)
    # 24
    def get_cert_num(self):
        return len(self.cert_dic.keys())
    # 25
    def get_SANdns_mean(self):
        if self.x509_line_num == 0:
            return -1
        return np.mean(self.SANdns_num_list)
    # 26
    def get_x509_ratio(self):
        return float(self.x509_line_num / len(self.tuple_list))
    # 27
    def get_SNI_in_SANdns(self):
        if len(self.SNI_in_SANdns_list) == 0:
            return -1
        for val in self.SNI_in_SANdns_list:
            if val == 0:
                return 0
        return 1
    # 28
    def get_CN_in_SANdns(self):
        if len(self.CN_in_SANdns_list) == 0:
            return -1
        for val in self.CN_in_SANdns_list:
            if val == 0:
                return 0
        return 1

    def get_label(self):
        if self.label == "malware":
            return 1
        else:
            return 0
    def get_result_line(self):
        return str(self.get_record_num()) + space + str(self.get_duration_mean()) + space + str(self.get_duration_std()) + space + \
               str(self.get_ratio_duraion_exceed()) + space + str(self.get_origin_bytes()) + space + \
               str(self.get_destination_bytes()) + space + str(self.get_ratio_bytes()) + space + \
               str(self.get_ratio_established()) + space + str(self.get_origin_pkts()) + space + \
               str(self.get_destination_pkts()) + space + str(self.get_2nd_mean()) + space + str(self.get_2nd_std()) + space + \
               str(self.get_ratio_tcp()) + space + str(self.get_ratio_tls()) + space + str(self.get_ratio_SNI()) + space + \
               str(self.get_SNI_as_IP()) + space + str(self.get_certPath_length_mean()) + space + \
               str(self.get_ratio_self_signed()) + space + str(self.get_key_length_mean()) + space + \
               str(self.get_valid_length_mean()) + space + str(self.get_valid_length_std()) + space + \
               str(self.get_ratio_valid()) + space + str(self.get_valid_ratio_mean()) + space + str(self.get_cert_num()) + space + \
               str(self.get_SANdns_mean()) + space + str(self.get_x509_ratio()) + space + str(self.get_SNI_in_SANdns()) + space + \
               str(self.get_CN_in_SANdns()) + space + str(self.get_label()) + "\n"


"""
service: python_script.notify
data:
  title: "Notification title"
  message: "Notification message"
  tag: "unique-tag"
  image: "https://test.xy/image.png"
  color: "red"
  url: "https://example.com"
  priority: "high"
  group: "group-1"
  actions:
    - title: "Action title"
      action: "ACTION"
  services:
    - service: notify.mobile_app_iphone
      type: "ios"
    - service: notify.mobile_app_oneplus
      type: "android"
    - service: persistent_notification.create
      type: "frontend"
    - service: notify.sms
      type: "sms"
      recipient: "+48123456789"
"""
ATTR_ACTIONS = "actions"
ATTR_ANDROID = "android"
ATTR_BADGE = "badge"
ATTR_APNS_COLLAPSE_ID = "apns-collapse-id"
ATTR_APNS_HEADERS = "apns_headers"
ATTR_ATTACHMENT = "attachment"
ATTR_CLICK_ACTION = "clickAction"
ATTR_COLOR = "color"
ATTR_DATA = "data"
ATTR_ENTITY_ID = "entity_id"
ATTR_FALSE = False
ATTR_FRONTEND = "frontend"
ATTR_HIDE_THUMBNAIL = "hide-thumbnail"
ATTR_GROUP = "group"
ATTR_IMAGE = "image"
ATTR_IOS = "ios"
ATTR_MESSAGE = "message"
ATTR_NOTIFICATION_ID = "notification_id"
ATTR_PRIORITY = "priority"
ATTR_PUSH = "push"
ATTR_RECIPIENT = "recipient"
ATTR_SMS = "sms"
ATTR_SERVICE = "service"
ATTR_SOUND = "sound"
ATTR_TAG = "tag"
ATTR_THREAD_ID = "thread-id"
ATTR_TITLE = "title"
ATTR_TTL = "ttl"
ATTR_TYPE = "type"
ATTR_URL = "url"

CONF_ACTIONS = "actions"
CONF_COLOR = "color"
CONF_DEVELOP = "develop"
CONF_ENTITY_ID = "entity_id"
CONF_GROUP = "group"
CONF_IMAGE = "image"
CONF_MESSAGE = "message"
CONF_PRIORITY = "priority"
CONF_RECIPIENT = "recipient"
CONF_SERVICES = "services"
CONF_TAG = "tag"
CONF_TITLE = "title"
CONF_URL = "url"

PRIORITY_NORMAL = "normal"
PRIORITY_HIGH = "high"

PRIORITIES = [PRIORITY_NORMAL, PRIORITY_HIGH]

title = data.get(CONF_TITLE)
message = data.get(CONF_MESSAGE)
if not title:
    raise ValueError("`title` argument is empty")
if not message:
    raise ValueError("`message` argument is empty")

services = data.get(CONF_SERVICES)
if not services:
    raise ValueError("Argument `services` should be a list")
if len(services) < 1:
    raise ValueError("Argument `services` should be a list")

priority = PRIORITY_NORMAL
if data.get(CONF_PRIORITY):
    priority = data.get(CONF_PRIORITY)
if not priority in PRIORITIES:
    raise ValueError("Wrong value for `priority` argument")

actions = data.get(CONF_ACTIONS)
group = data.get(CONF_GROUP)
image = data.get(CONF_IMAGE)
entity_id = data.get(CONF_ENTITY_ID)
tag = data.get(CONF_TAG)
url = data.get(CONF_URL)
color = data.get(CONF_COLOR)

develop = False
if data.get(CONF_DEVELOP) == True:
    develop = data.get(CONF_DEVELOP)

for item in services:
    if item[ATTR_TYPE] == ATTR_IOS:
        service_data = {ATTR_TITLE: title, ATTR_MESSAGE: message}
        service_data[ATTR_DATA] = {}
        service_data[ATTR_DATA][ATTR_PUSH] = {}
        service_data[ATTR_DATA][ATTR_PUSH][ATTR_BADGE] = 1
        service_data[ATTR_DATA][ATTR_PUSH][ATTR_SOUND] = "Warsaw_Haptic.caf"

        if actions:
            service_data[ATTR_DATA][ATTR_ACTIONS] = actions
        if image:
            service_data[ATTR_DATA][ATTR_ATTACHMENT] = {}
            service_data[ATTR_DATA][ATTR_ATTACHMENT][ATTR_URL] = image
            service_data[ATTR_DATA][ATTR_ATTACHMENT][ATTR_HIDE_THUMBNAIL] = ATTR_FALSE
        if group:
            service_data[ATTR_DATA][ATTR_PUSH][ATTR_THREAD_ID] = group
        if tag:
            service_data[ATTR_DATA][ATTR_APNS_HEADERS] = {}
            service_data[ATTR_DATA][ATTR_APNS_HEADERS][ATTR_APNS_COLLAPSE_ID] = tag
        if url:
            service_data[ATTR_DATA][ATTR_URL] = url
        if entity_id:
            service_data[ATTR_DATA][ATTR_ENTITY_ID] = entity_id

        logger.debug(f"service: {item[ATTR_SERVICE]}, data: {service_data}")

        if not develop:
            hass.services.call(
                item[ATTR_SERVICE].split(".")[0],
                item[ATTR_SERVICE].split(".")[1],
                service_data,
                False,
            )

    if item[ATTR_TYPE] == ATTR_ANDROID:
        service_data = {ATTR_TITLE: title, ATTR_MESSAGE: message}
        if priority == PRIORITY_HIGH:
            if not service_data.get(ATTR_DATA):
                service_data[ATTR_DATA] = {}
            service_data[ATTR_DATA][ATTR_PRIORITY] = priority
            service_data[ATTR_DATA][ATTR_TTL] = 0
        if actions:
            if not service_data.get(ATTR_DATA):
                service_data[ATTR_DATA] = {}
            service_data[ATTR_DATA][ATTR_ACTIONS] = actions
        if group:
            if not service_data.get(ATTR_DATA):
                service_data[ATTR_DATA] = {}
            service_data[ATTR_DATA][ATTR_GROUP] = group
        if tag:
            if not service_data.get(ATTR_DATA):
                service_data[ATTR_DATA] = {}
            service_data[ATTR_DATA][ATTR_TAG] = tag
        if url:
            if not service_data.get(ATTR_DATA):
                service_data[ATTR_DATA] = {}
            service_data[ATTR_DATA][ATTR_CLICK_ACTION] = url
        if color:
            if not service_data.get(ATTR_DATA):
                service_data[ATTR_DATA] = {}
            service_data[ATTR_DATA][ATTR_COLOR] = color
        if image:
            if not service_data.get(ATTR_DATA):
                service_data[ATTR_DATA] = {}
            service_data[ATTR_DATA][ATTR_IMAGE] = image

        logger.debug(f"service: {item[ATTR_SERVICE]}, data: {service_data}")

        if not develop:
            hass.services.call(
                item[ATTR_SERVICE].split(".")[0],
                item[ATTR_SERVICE].split(".")[1],
                service_data,
                False,
            )

    if item[ATTR_TYPE] == ATTR_FRONTEND:
        if image:
            message = message + " "
            message = message + image
        if url:
            message = message + " "
            message = message + url
        service_data = {ATTR_TITLE: title, ATTR_MESSAGE: message}
        if tag:
            service_data[ATTR_NOTIFICATION_ID] = tag

        logger.debug(f"service: {item[ATTR_SERVICE]}, data: {service_data}")

        if not develop:
            hass.services.call(
                item[ATTR_SERVICE].split(".")[0],
                item[ATTR_SERVICE].split(".")[1],
                service_data,
                False,
            )

    if item[ATTR_TYPE] == ATTR_SMS:
        recipient = item.get(CONF_RECIPIENT)
        if not recipient:
            raise ValueError("`recipient` argument is empty")
        if image:
            message = message + " "
            message = message + image
        if url:
            message = message + " "
            message = message + url
        service_data = {ATTR_MESSAGE: message, ATTR_RECIPIENT: recipient}

        logger.debug(f"service: {item[ATTR_SERVICE]}, data: {service_data}")

        if not develop:
            hass.services.call(
                item[ATTR_SERVICE].split(".")[0],
                item[ATTR_SERVICE].split(".")[1],
                service_data,
                False,
            )


import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import Adam

import pytorch_lightning as pl

from modules.PositionalEncoding import PositionalEncoding, PositionalEncoding2d, A2DPE, ExperimentalPositionalEncoding
from modules.TransformerEncoderLayer2d import TransformerEncoderLayer2d
from modules.Transformer import TransformerDecoder, TransformerDecoderLayer
from modules.ResNet import ResNet
from metrics import exact_match, exact_match_alphanum, exact_match_alpha, char_error_rate, word_error_rate

from argparse import Namespace
from datetime import datetime
import sys

class SATRNModel(pl.LightningModule):
    def __init__(self, hparams, tokenizer):
        super().__init__()
        self.save_hyperparameters(hparams)
        self.tokenizer = tokenizer

        self.metrics = {'acc': exact_match, 'acc_alnum': exact_match_alphanum, 'acc_alpha': exact_match_alpha, 'cer': char_error_rate, 'wer': word_error_rate}

        #Shallow CNN
        if self.hparams.backbone == 'simple':
            self.shallow_conv = nn.Sequential(nn.Conv2d(1 if self.hparams.grayscale else 3, self.hparams.d_model, 3, padding=1),
                                            nn.MaxPool2d(kernel_size=2, stride=2),
                                            nn.ReLU(),
                                            nn.Dropout(self.hparams.dropout),
                                            nn.Conv2d(self.hparams.d_model, self.hparams.d_model, 3, padding=1),
                                            nn.MaxPool2d(kernel_size=2, stride=2))
        elif self.hparams.backbone == 'resnet':
            self.shallow_conv = ResNet(1 if self.hparams.grayscale else 3, self.hparams.d_model, dropout=self.hparams.dropout)

        #Encoder
        if 'positional_enc' not in self.hparams or self.hparams.positional_enc == 'simple':
            self.encoder_pe = PositionalEncoding2d(self.hparams.d_model, self.hparams.dropout)
        elif self.hparams.positional_enc == 'a2dpe':
            self.encoder_pe = A2DPE(self.hparams.d_model, self.hparams.dropout)
        elif self.hparams.positional_enc == 'experimental':
            self.encoder_pe = ExperimentalPositionalEncoding(self.hparams.d_model, self.hparams.dropout)

        encoder_layers = TransformerEncoderLayer2d(self.hparams.d_model, self.hparams.nhead, self.hparams.d_hidden, dropout=self.hparams.dropout)
        self.encoder = nn.TransformerEncoder(encoder_layers, self.hparams.nlayers_encoder)

        #Decoder
        self.decoder_emb = nn.Embedding(self.hparams.vocab_size, self.hparams.d_model)
        self.decoder_pe = PositionalEncoding(self.hparams.d_model, self.hparams.dropout)
        decoder_layers = TransformerDecoderLayer(self.hparams.d_model, self.hparams.nhead, self.hparams.d_hidden, dropout=self.hparams.dropout)
        self.decoder = TransformerDecoder(decoder_layers, self.hparams.nlayers_decoder)
        self.lin_out = nn.Linear(self.hparams.d_model, self.hparams.vocab_size)
        return

    def generate_square_subsequent_mask(self, L: int):
        mask = (torch.triu(torch.ones(L, L)) == 1).transpose(0, 1)
        mask = mask.float().masked_fill(mask == 0, float('-inf')).masked_fill(mask == 1, float(0.0))
        return mask

    def run_encoder(self, img, img_padding_mask=None):
        """
        Shape:
            img: N x C x H x W
            img_padding_mask: N x H x W
        """
        img = self.shallow_conv(img)
        if img_padding_mask is not None:
            img_padding_mask = img_padding_mask[:, 0::4, 0::4]

        img = img.permute(2, 3, 0, 1) # -> H x W x N x C
        img = self.encoder_pe(img)
        img = self.encoder(img, mask=None, src_key_padding_mask=img_padding_mask)

        (H, W, N, _) = img.size()

        img = img.reshape(H*W, N, self.hparams.d_model)
        if img_padding_mask is not None:
            img_padding_mask = img_padding_mask.reshape(N, H*W)

        return img, img_padding_mask

    def run_decoder(self, memory, dec_input, attn_mask=None, memory_padding_mask=None, dec_input_padding_mask=None, return_attn=False):
        """
        Shape:
            memory: H*W x N x C
            dec_input: L x N
            attn_mask: L x L
            memory_padding_mask: N x H*W
            dec_input_padding_mask: N x L
        """

        dec_input = self.decoder_emb(dec_input) # -> L x N x C
        dec_input = self.decoder_pe(dec_input)

        if return_attn:
            logits, attn_weights = self.decoder(dec_input, memory, tgt_mask=attn_mask, memory_key_padding_mask=memory_padding_mask, tgt_key_padding_mask=dec_input_padding_mask, return_attn=True) # -> L x N x C
        else:
            logits = self.decoder(dec_input, memory, tgt_mask=attn_mask, memory_key_padding_mask=memory_padding_mask, tgt_key_padding_mask=dec_input_padding_mask) # -> L x N x C

        logits = self.lin_out(logits)
        return (logits, attn_weights) if return_attn else logits

    def forward(self, img, tgt, img_padding_mask=None, tgt_padding_mask=None):
        """
        Shape:
            img: N x C x H x W
            tgt: N x L
            img_padding_mask: N x H x W
            tgt_padding_mask: N x L
        """
        memory, memory_padding_mask = self.run_encoder(img, img_padding_mask)

        tgt = tgt.transpose(0, 1) # -> L x N
        (L, N) = tgt.size()

        dec_input = torch.cat([self.hparams.go_token_idx * torch.ones(1, N).type_as(tgt), tgt[:-1, :]], dim=0)
        if tgt_padding_mask is not None:
            dec_input_padding_mask = torch.cat([tgt_padding_mask[0:1, :], tgt_padding_mask[:-1, :]], dim=0)
        else:
            dec_input_padding_mask = None
        attn_mask = self.generate_square_subsequent_mask(L).to(img.device) # -> L x L

        logits = self.run_decoder(memory, dec_input, attn_mask, memory_padding_mask, dec_input_padding_mask)

        logits = logits.transpose(0, 1) # -> N x L x V
        return logits

    def generate(self, img, img_padding_mask, max_length=50):
        """
        Shape:
            img: N x C x H x W
            img_padding_mask: N x H x W
        """
        N, _, H, W = img.size()
        memory, memory_padding_mask = self.run_encoder(img, img_padding_mask)

        termination_check = torch.zeros(N)
        dec_input = self.hparams.go_token_idx * torch.ones((1, N), dtype=int).long()
        if img.is_cuda:
            termination_check = termination_check.cuda()
            dec_input = dec_input.cuda()
        attn_mask = self.generate_square_subsequent_mask(1)
        length = 0

        while torch.sum(termination_check) != termination_check.size(0) and length < max_length:
            logits, attn_weights = self.run_decoder(memory, dec_input, attn_mask, memory_padding_mask, None, return_attn=True)
            pred = logits.argmax(dim=2)

            termination_check = torch.max(termination_check, pred[-1, :] == self.hparams.end_token_idx)
            dec_input = torch.cat([self.hparams.go_token_idx * torch.ones(1, N).type_as(dec_input), pred], dim=0)
            attn_mask = self.generate_square_subsequent_mask(dec_input.size(0))
            length += 1

        layers, _, L, _ = attn_weights.size()
        attn_weights = attn_weights.contiguous().view(layers, N, L, H//4, W//4)
        return pred.transpose(0, 1), attn_weights

    def configure_optimizers(self):
        return Adam(self.parameters(), lr=self.hparams.lr)

    def training_step(self, batch, batch_idx):
        logits = self(**batch)
        logits = logits.reshape(-1, logits.size(2))
        tgt = batch['tgt'].view(-1)

        loss = F.cross_entropy(logits, tgt)

        self.log('loss', loss, on_step=True, on_epoch=True, prog_bar=False, logger=True, sync_dist=True)
        return loss

    def validation_step(self, batch, batch_idx):
        with torch.no_grad():
            logits = self(**batch)
            pred = logits.argmax(dim=2)
            tgt = batch['tgt']

            tgt_words = self.tokenizer.decode(tgt.cpu().numpy())
            pred_words = self.tokenizer.decode(pred.cpu().numpy())
            assert len(tgt_words) == len(pred_words)

            out_dict = {}
            for metric_name, metric in self.metrics.items():
                out_dict[metric_name] = sum([metric(pred_words[i], tgt_words[i]) for i in range(len(tgt_words)) if len(tgt_words[i]) != 0])

            out_dict['nb_ex'] = len([words for words in tgt_words if len(words) != 0])

        return out_dict

    def validation_epoch_end(self, outputs):
        print("---VAL-RESULTS---")
        nb_ex = sum([out['nb_ex'] for out in outputs])

        for metric_name in self.metrics:
            metric_value = sum([out[metric_name] for out in outputs])
            metric_value /= float(nb_ex)
            print("{}: {}".format(metric_name, metric_value))
            self.log('val_{}'.format(metric_name), metric_value, sync_dist=True)
        return

    def test_step(self, batch, batch_idx):
        with torch.no_grad():
            logits = self(**batch)
            pred = logits.argmax(dim=2)
            tgt = batch['tgt']

            tgt_words = self.tokenizer.decode(tgt.cpu().numpy())
            pred_words = self.tokenizer.decode(pred.cpu().numpy())
            assert len(tgt_words) == len(pred_words)

            out_dict = {}
            for metric_name, metric in self.metrics.items():
                out_dict[metric_name] = sum([metric(pred_words[i], tgt_words[i]) for i in range(len(tgt_words)) if len(tgt_words[i]) != 0])

            out_dict['nb_ex'] = len([words for words in tgt_words if len(words) != 0])

        return out_dict

    def test_epoch_end(self, outputs):
        print("---TEST-RESULTS---")
        nb_ex = sum([out['nb_ex'] for out in outputs])

        for metric_name in self.metrics:
            metric_value = sum([out[metric_name] for out in outputs])
            metric_value /= float(nb_ex)
            print("{}: {}".format(metric_name, metric_value))
            self.log('test_{}'.format(metric_name), metric_value, sync_dist=True)
        return


from sklearn.model_selection import train_test_split
import pandas as pd
import os
import numpy as np

# ===================================================================== #
# ========================  Combine Data ======================== #
# ===================================================================== #

# NOTE: The reason that I am using pandas even thought is not necessrary,
# I would like to improve my skill on pandas.

def get_dir_names(FLAGS):
    """Obtain directory names where data are saved.
    Returns:
       List of folders names
    """
    folders = []
    root_dir, dirs, files = next(os.walk('./data'))
    for name in dirs:
        folders.append(os.path.join(root_dir, name))

    folders = np.array(folders)
    folders_include = FLAGS.folders_include.split(',')
    # add the root to the names of the folders
    folders_include = np.core.defchararray.add(root_dir + '/',folders_include)
    return  np.extract(np.isin(folders , folders_include),folders)

def combine_data(folders,rec_data=False):
    """combine all driving logs files into one.
    Arguments:
        folders: List of folders names
    Returns:
       Path of the new driving log file.
    """
    driving_log_file = "./data/combine_driving_log.csv"
    if rec_data:
        list_ = []
        headers = ['center', 'left', 'right',
                   'steering', 'throttle', 'break', 'speed']
        for folder in folders:
            df = pd.read_csv(folder + "/driving_log.csv",
                             header=None, names=headers)
            # get the relative path of the images

            def get_image_path(row):
                for image in headers[:3]:
                    row[image] = folder + '/' + \
                        '/'.join(row[image].split('/')[-2:])

                return row

            df = df.apply(get_image_path, axis=1)
            list_.append(df)

        pd.concat(list_).to_csv(driving_log_file, index=False)

    return driving_log_file

# ===================================================================== #
# ============================ split Data ============================= #
# ===================================================================== #

# @return pandas DataFrame
def split_data(path_to_csv):
    # read  driveing log csv file
    data = pd.read_csv(path_to_csv)
    return train_test_split(data, test_size=0.2)


from tkinter import *

WIDTH=400
HEIGHT=300

root=Tk()
root.title("Mon arc !")
cnv = Canvas(root, width=WIDTH, height=HEIGHT, bg='ivory')
cnv.pack()
A = (50, 50)
B = (250, 250)

cnv.create_oval(A,B)
#Pour ligne 28 :
"""
* option extent : la totalité de l'angle (OA, OB), end dégrés
* option start : l'angle en degrés (u, OA) où représente un demi-droite horizontale
dirigée vers l'Est et A le point de départ sur l'ellipse.

"""
cnv.create_arc(A,B, outline="red",extent=210,start=-120, fill="light blue",width=4)

root.mainloop()

Tkinter/IV - Les canevas/16 - Créer un arc/index.py


# Write RAW output to text file

import sys
import serial
import time
from pick import pick
import serial.tools.list_ports
import re

def use_regex(input):
    pattern = re.compile(r"[0-9]*\.[0-9]+")
    return pattern.search(input, re.IGNORECASE)

comlist = serial.tools.list_ports.comports()

port = []
name = []
for element in comlist:

    port.append(element.device)  # Device : COM name full length
    name.append(element.description)  # Description : Full description of port

indicator: str = "=>"

title = "Select COM port :"

# Exit with error code 1 if no ports found
if len(port) <= 0:
    print("No device connected")
    sys.exit(1)

options = []

for i in range(0, len(port)):
    options.append(port[i] + " : " + name[i])

option, index = pick(options, title, indicator)

ser = serial.Serial(port[index], 115200, timeout=1)
time.sleep(1)

# Print all serial port data
def print_serial_data():
    while(True):
        line = ser.readline()
        if line:
            string = line.decode()
            print(string)

# One time function - Stackoverflow
def run_once(f):
    def wrapper(*args, **kwargs):
        if not wrapper.has_run:
            wrapper.has_run = True
            return f(*args, **kwargs)
    wrapper.has_run = False
    return wrapper

@run_once
def my_function():
    ser.write("AT+RUNIMPULSE".encode())
    # Enter Key
    ser.write("\r".encode())

action = run_once(my_function)

val = input("Enter value : ")

while(True):
    line = ser.readline()
    action()
    if line:
        string = line.decode()
        # change "none: 	" to any matching string
        if val in string:
            num = float(use_regex(string).group(0))
            # add custom condition here
            print(num)
            f = open("data.txt", "a")
            f.write(str(num) + "\n")
            f.close()

ser.close()


import re

class Header:
    def __init__(self, header):
        self.header = header
    def text(self):
        return self.header

class Container:
    def append(self, matcher):
        raise Exception('Not implemented.')

class Action:
    def __init__(self, client, matcher):
        self.client = client
        self.matcher = matcher
    def execute(self):
        raise Exception('Not implemented.')

class MessageAction(Action):
    pass

class ResultMessageAction(MessageAction):
    def __init__(self, client, valid):
        MessageAction.__init__(self, client, None)
        self.valid = valid

RESULT_OK = ResultMessageAction(None, True)
RESULT_NOK = ResultMessageAction(None, False)

class SampleMessageAction(MessageAction,Container):
    def __init__(self, client, matcher):
        MessageAction.__init__(self, client, matcher)
        self.nSamples,samples = self.convert(matcher)
        self.samples = []
        self.samples.append(samples)
    def execute(self):
        watchers = self.caller.getWatchers(self.client.id)
        self.caller.message(self.client.id, RESULT_OK)
        for watcher in watchers:
            self.caller.message(watcher, self)
    def text(self):
        fmt = '! {0} {2} {1} '+' '.join(['{'+str(i+3)+'}' for i in range(self.nSamples) ])
        return fmt.format(self.client.id, self.nSamples, *self.samples)
    def convert(self, matcher):
        nSamples = int(self.matcher.group(2))
        samples = [ int(self.matcher.group(1)) ]
        for i in self.matcher.group(3).split():
            samples.append(float(i))
        if len(samples)-1 != nSamples:
            raise ValueError('Inconsistent packet count.')
        return (nSamples, tuple(samples))
    def append(self, matcher):
        nSamples,samples = self.convert(matcher)
        if nSamples != self.nSamples:
            raise ValueError("Wrong value")
        self.samples.append(samples)

class RoleMessageAction(MessageAction):
    def __init__(self, client, role):
        MessageAction.__init__(self, client, None)
        self.role = role
    def text(self):
        return str(self.role)

class HeaderMessageAction(MessageAction):
    def __init__(self, client, header):
        MessageAction.__init__(self, client, None)
        self.header = Header(header)
    def text(self):
        return self.header.text()

class StatusMessageAction(MessageAction):
    def __init__(self, client, clients):
        MessageAction.__init__(self, client, None)
        self.clients = clients
    def text(self):
        nClients = len(self.clients)
        retarr = [ '{0} clients connected'.format(nClients) ]
        for key,val in self.clients.items():
            retarr.append('{0}:{1}'.format(key, val.getRole()))
        return '\r\n'.join(retarr)

class CommandAction(Action):
    pass

class ResultCommandAction(CommandAction):
    def __init__(self, client, status):
        CommandAction.__init__(self, client, False)
        self.status = status
    def execute(self):
        self.caller.message(self.client.id, self.status)

class StatusCommandAction(CommandAction):
    def execute(self):
        clients = self.caller.getClients()
        status = StatusMessageAction(self.client, clients)
        self.caller.message(self.client.id, [RESULT_OK, status])

class GetheaderCommandAction(CommandAction):
    def __init__(self, client, matcher):
        CommandAction.__init__(self, client, matcher)
        self.id = int(self.matcher.group(1))
    def execute(self):
        header = HeaderMessageAction(self.client, client.getHeader(self.id))
        self.caller.message(self.client.id, [ RESULT_OK, header ])

class SetheaderCommandAction(CommandAction):
    def __init__(self, client, matcher):
        CommandAction.__init__(self, client, matcher)
        self.header = Header(self.matcher.group(1))
    def execute(self):
        self.client.setHeader(self.header)
        self.caller.message(self.client.id, RESULT_OK)

class EEGCommandAction(CommandAction):
    def execute(self):
        self.client.setRole('EEG')
        self.caller.message(self.client.id, RESULT_OK)

class DisplayCommandAction(CommandAction):
    def execute(self):
        self.client.setRole('Display')
        self.caller.message(self.client.id, RESULT_OK)

class RoleCommandAction(CommandAction):
    def execute(self):
        role = RoleMessageAction(self.client, self.client.getRole())
        self.caller.message(self.client.id, [ RESULT_OK, role ])

class WatchCommandAction(CommandAction):
    def __init__(self, client, matcher):
        CommandAction.__init__(self, client, matcher)
        self.id = int(self.matcher.group(1))
    def execute(self):
        try:
            if self.caller.getRole(self.id) != 'EEG':
                raise Exception('Role "EEG" is required.')
            self.client.setWatch(self.id)
            self.caller.message(self.client.id, RESULT_OK)
        except:
            self.caller.message(self.client.id, RESULT_NOK)
            raise

class UnwatchCommandAction(CommandAction):
    def __init__(self, client, matcher):
        CommandAction.__init__(self, client, matcher)
        self.id = int(self.matcher.group(1))
    def execute(self):
        try:
            if self.caller.getRole(self.id) != 'EEG':
                raise Exception('Role "EEG" is required.')
            self.client.setUnwatch(self.id)
            self.caller.message(self.client.id, RESULT_OK)
        except:
            self.caller.message(self.client.id, RESULT_NOK)
            raise

class WatchallCommandAction(CommandAction):
    def execute(self):
        try:
            if self.caller.getRole(self.id) != 'EEG':
                raise Exception('Role "EEG" is required.')
            self.client.setWatch()
            self.caller.message(self.client.id, RESULT_OK)
        except:
            self.caller.message(self.client.id, RESULT_NOK)
            raise

class UnwatchallCommandAction(CommandAction):
    def execute(self):
        try:
            if self.caller.getRole(self.id) != 'EEG':
                raise Exception('Role "EEG" is required.')
            self.client.setUnwatch()
            self.caller.message(self.client.id, RESULT_OK)
        except:
            self.caller.message(self.client.id, RESULT_NOK)
            raise

map = [
       (re.compile(r'^!\s+([0-9]+)\s+([0-9]+)\s+(.*)$'), SampleMessageAction),
       (re.compile(r'^status$'), StatusCommandAction),
       (re.compile(r'^getheader\s+([0-9]+)$'), GetheaderCommandAction),
       (re.compile(r'^setheader\s(.+)$'), SetheaderCommandAction),
       (re.compile(r'^eeg$'), EEGCommandAction),
       (re.compile(r'^display$'), DisplayCommandAction),
       (re.compile(r'^role$'), RoleCommandAction),
       (re.compile(r'^watch\s([0-9]+)$'), WatchCommandAction),
       (re.compile(r'^unwatch\s([0-9]+)$'), UnwatchCommandAction),
       (re.compile(r'^watch\s+$'), WatchallCommandAction),
       (re.compile(r'^unwatch\s+$'), UnwatchallCommandAction)
]

class Parser(object):
    def __init__(self, caller):
        self.caller = caller
    def parse(msg, client = None, container = None):
        for pattern, Class in map:
            matcher = pattern.match(msg)
            if matcher is not None:
                if container is not None and isinstance(container, Container):
                    container.append(matcher)
                    return container
                else:
                    if client is None:
                        raise ValueError('One of the "client" or "container" is supposed to be present.')
                    return Class(client, matcher)
        return ResultCommandAction(client, RESULT_NOK)


# python

import lx, lxifc, lxu.command

FORMS = [
    {
        "label":"Unit Switcher Pie Menu",
		"recommended": "Alt+7",
        "hash":"rb_unit_switcher:sheet"
    }, {
        "label":"Seletion Sets Pie Menu",
        "hash":"rb_select_set:sheet"
    }, {
        "label":"Packaging Thicken Pie Menu",
        "hash":"rb_packaging_thicken:sheet"
    }, {
        "label":"UV Unwrapping Pie Menu",
        "hash":"rb_uv_unwrapping:sheet"
    }
]

def list_commands():
    fcl = []
    for n, form in enumerate(sorted(FORMS, key=lambda k: k['label']) ):
        fcl.append("redbeard.labeledPopover {%s} {%s}" % (form["hash"], form["label"]))
        fcl.append("redbeard.labeledMapKey {%s} {%s}" % (form["hash"], form["label"]))

        if n < len(FORMS)-1:
            fcl.append('- ')

    return fcl

class CommandListClass(lxifc.UIValueHints):
    def __init__(self, items):
        self._items = items

    def uiv_Flags(self):
        return lx.symbol.fVALHINT_FORM_COMMAND_LIST

    def uiv_FormCommandListCount(self):
        return len(self._items)

    def uiv_FormCommandListByIndex(self,index):
        return self._items[index]

class CommandClass(lxu.command.BasicCommand):
    def __init__(self):
        lxu.command.BasicCommand.__init__(self)

        self.dyna_Add('query', lx.symbol.sTYPE_INTEGER)
        self.basic_SetFlags(0, lx.symbol.fCMDARG_QUERY)

    def arg_UIValueHints(self, index):
        if index == 0:
            return CommandListClass(list_commands())

    def cmd_Execute(self,flags):
        pass

    def cmd_Query(self,index,vaQuery):
        pass

lx.bless(CommandClass, "rb.mapping_FCL")


## package for dim reduction of data

from sklearn.decomposition import PCA,KernelPCA
import numpy as np
from mb_pandas.src.dfload import load_any_df
import pandas as pd
from mb_utils.src.logging import logger
import ast
from scipy.spatial import distance as dist_scipy
from sklearn.manifold import TSNE
import umap

__all__ = ['distance_scipy','get_data','get_avg_per_label','get_distance_mean']

def distance_scipy(event1,event2):
    """
    Distance between two points
    Inputs:
        event1 : list of coordinates of the points
        event2 : list of coordinates of the points
    Returns:
        distance between the points
    """
    return dist_scipy.euclidean(event1,event2)

def get_data(df_np,label,ids=None,dim_type= 'pca',dim_comp =2, logger=None):
    """
    Get the data from the file
    Inputs:
        df_np : numpy array (numpy array) or list
        label : label of the data (list)
        ids : ids of the data (list)
        dim_type : dimensionality reduction type. Options are 'pca','tsne' and 'umap'. Default is 'pca'
        dim_comp : number of pca components , also can be tsne or umap components. Default is 2
        logger : logger
    Returns:
        data : dataframe
    """

    assert type(df_np) == np.ndarray or type(df_np) == list, 'df_np should be numpy array or list'
    assert dim_type in ['pca','tsne','umap'] , 'dim_type should be pca, tsne or umap'
    assert type(dim_comp) == int, 'dim_comp should be integer'
    assert type(label) == list, 'label should be list'

    if type(df_np) == np.ndarray:
        data = list(df_np)
    else:
        data = df_np

    if dim_type == 'pca':
        pca = PCA(n_components=dim_comp)
        pca_emb = pca.fit_transform(data)
    elif dim_type == 'tsne':
        tsne = TSNE(n_components=dim_comp, perplexity=15, random_state=42, init='random', learning_rate=200)
        pca_emb = tsne.fit_transform(np.array(data))
        #pca_emb = TSNE(n_components=dim_comp).fit_transform(list(data['embedding']))
    elif dim_type == 'umap':
        pca_emb = umap.UMAP(n_components=dim_comp).fit_transform(data)
    if logger:
        logger.info(pca_emb[:3])
    temp_pca = list(pca_emb)

    data_pd = pd.DataFrame()
    data_pd['original_embedding'] = data
    data_pd['embedding'] = temp_pca
    data_pd['taxcode'] = label
    if ids:
        data['event_id'] = ids

    if logger:
        df_unique_taxcode = data['taxcode'].unique()
        logger.info('Number of unique taxcodes {}'.format(len(df_unique_taxcode)))

    return data

def get_avg_per_label(df,logger=None):
    """
    Get the average of the data per label
    Inputs:
        df : dataframe
        logger : logger
    Returns:
        df_avg : dataframe
    """
    assert type(df) == pd.DataFrame, 'df should be pandas dataframe'
    assert 'taxcode' in df.columns, 'taxcode should be in the dataframe'
    assert 'embedding' in df.columns, 'embedding should be in the dataframe'

    df_unique_taxcode = df['taxcode'].unique()

    pca_mean = {}
    for taxcode in df_unique_taxcode:
        df_temp = df[df['taxcode']==taxcode]
        pca_mean[taxcode]  = np.mean(df_temp['pca_res'],axis=0)
    if logger:
        logger.info('Length of PCA avg dict : {}'.format(str(len(pca_mean))))
    return pca_mean

def get_distance_mean(dm1,dm2,dm_mean=False,logger=None):
    """
    Get the distance of the data from the average of the data per label
    Inputs:
        dm1 : dataframe , one whos avg. might me needed
        dm2 : dataframe
        logger : logger
    Returns:
        df : dataframe
    """

    assert type(dm1) == pd.DataFrame, 'dm1 should be pandas dataframe'
    assert type(dm2) == pd.DataFrame, 'dm2 should be pandas dataframe'
    assert 'taxcode' in dm1.columns, 'taxcode should be in the dataframe'
    assert 'embedding' in dm1.columns, 'embedding should be in the dataframe'
    assert 'taxcode' in dm2.columns, 'taxcode should be in the dataframe'
    assert 'embedding' in dm2.columns, 'embedding should be in the dataframe'

    df = pd.DataFrame()
    df['event_id'] = dm2['event_id']
    df['taxcode'] = dm2['taxcode']
    if dm_mean == True:
        pca_mean = get_avg_per_label(dm1,logger=logger)

    dist = []
    dist_1 = []
    for i in range(len(dm2)):
        dist_all = []
        for j in range(len(dm1)):
            if dm_mean == True:
                dist_all.append(distance_scipy(dm2['embedding'][i],pca_mean[dm1['taxcode'][j]]))
            else:
                dist.append(distance_scipy(dm2['embedding'][i],dm1['embedding'][j]))
        dist_1.append(dist_all)
    if sum(dist_all)==0:
        dist_f = dist_all
    else:
        dist_f = dist
    df['distance'] = dist_f

    if logger:
        logger.info('Length of df : {}'.format(str(len(df))))
    return df


import gym, os
import matplotlib.pyplot as plt
import pickle as pkl
import numpy as np
import featurized_env
import featurized_env.humanoid

plot_counter = 0

def sanitize_features(features):
    for i in range(0, 15, 3):
        features[:, i], features[:, i+1], features[:, i+2] = features[:, i+2].copy(), features[:, i].copy(), features[:, i+1].copy()
    return features

def plot_old_features(features, title=None):
    global plot_counter
    plot_counter += 1
    plt.figure(plot_counter)

    def label(i):
        parts = ["head", "left_hand", "right_hand", "left_foot", "right_foot"]
        xyz = "xyz"
        return "%s\n%s" % (parts[i // 3], xyz[i % 3])

    block_size = 40
    violins = []
    print(features.shape)
    for i in range(15):
        violins.append(features[:, i])
        # for j in range(0, features.shape[0], block_size):
        #     plt.plot(list(range(j, j+block_size)),
        #              list(features[j:j+block_size, i].mean() for x in range(block_size)), label=label(i))
    plt.violinplot(violins, showmeans=False, showmedians=True)
    plt.gca().set_xticks(range(1, 16))
    plt.gca().set_xticklabels([label(i) for i in range(15)])
    plt.grid()

    if title is not None:
        plt.title(title)

    plt.legend()

def plot_features(features, title=None):
    global plot_counter
    plot_counter += 1
    plt.figure(plot_counter)

    def label(i):
        parts = ["head", "left_hand", "right_hand", "left_foot", "right_foot", "delta_root"]
        xyz = "xyz"
        return "%s\n%s" % (parts[i // 3], xyz[i % 3])

    violins = []
    print(features.shape)
    for i in range(18):
        violins.append(features[i, :])
        # for j in range(0, features.shape[0], block_size):
        #     plt.plot(list(range(j, j+block_size)),
        #              list(features[j:j+block_size, i].mean() for x in range(block_size)), label=label(i))
    plt.violinplot(violins, showmeans=False, showmedians=True)
    plt.gca().set_xticks(range(1, 19))
    plt.gca().set_xticklabels([label(i) for i in range(18)])
    plt.grid()

    if title is not None:
        plt.title(title)

    plt.legend()

def load_pickled_features(pkl_file):
    with open(pkl_file, "rb") as f:
        traj_data = pkl.load(f)
        features = []
        for traj in traj_data:
            features.append(traj["features"])
        return np.array([v for ob in features for v in ob])

def load_pickled_features_buggy(pkl_file):
    with open(pkl_file, "rb") as f:
        traj_data = pkl.load(f)
        features = []
        for traj in traj_data:
            features.append(traj["features"])
        return np.array(features)

def load_pickled_positions(pkl_file):
    with open(pkl_file, "rb") as f:
        traj_data = pkl.load(f)
        positions = []
        for traj in traj_data:
            positions.append(traj["positions"])
        return [v for ob in positions for v in ob]

def plot_positions(positions, keys=None, title=None):
    if keys is None:
        keys = ["root"]
    global plot_counter
    plot_counter += 1
    plt.figure(plot_counter)
    violins = []
    xyz = "xyz"
    for key in keys:
        violinx, violiny, violinz = [], [], []
        for t, p in enumerate(positions):
            violinx.append(np.ravel(p[key])[0])
            violiny.append(np.ravel(p[key])[1])
            violinz.append(np.ravel(p[key])[2])
        violins.append(violinx)
        violins.append(violiny)
        violins.append(violinz)

        # for j in range(0, features.shape[0], block_size):
        #     plt.plot(list(range(j, j+block_size)),
        #              list(features[j:j+block_size, i].mean() for x in range(block_size)), label=label(i))
    plt.violinplot(violins, showmeans=False, showmedians=True)
    plt.gca().set_xticks(range(1, len(keys) * 3 + 1))

    def label(i):
        return "%s\n%s" % (keys[i // 3], xyz[i % 3])

    plt.gca().set_xticklabels([label(i) for i in range(len(keys) * 3)])
    plt.grid()

    if title is not None:
        plt.title(title)

    plt.legend()

def plot_time_positions(positions, keys=None, title=None):
    if keys is None:
        keys = ["root"]
    global plot_counter
    plot_counter += 1
    plt.figure(plot_counter)
    violins = []
    xyz = "xyz"
    for key in keys:
        violinx, violiny, violinz = [], [], []
        for t, p in enumerate(positions):
            violinx.append(np.ravel(p[key])[0])
            violiny.append(np.ravel(p[key])[1])
            violinz.append(np.ravel(p[key])[2])
        plt.plot(violinx, label="%s\n%s" % (key, "x"))
        plt.plot(violiny, label="%s\n%s" % (key, "y"))
        plt.plot(violinz, label="%s\n%s" % (key, "z"))

    plt.grid()

    if title is not None:
        plt.title(title)

    plt.legend()

def tabulate_features(features, title=None, scaling=10):
    target = np.zeros((features.shape[0] * scaling, features.shape[1]))
    for i in range(features.shape[0]):
        target[i*scaling:(i+1)*scaling,:] = features[i, :]
    plt.matshow(target)

    if title is not None:
        plt.title(title)

if __name__ == "__main__":
    env = gym.make("HumanoidFeaturized-v1")

    trpo_rollout_path = os.path.expanduser("~/gail-tf/rollout/stochastic.trpo.HumanoidFeaturized.0.00.pkl")
    mocap_rollout_path = os.path.expanduser("~/gail-tf/rollout/mocap_trajectories.pkl")

    trpo_features = load_pickled_features(trpo_rollout_path)[:60]
    mocap_features = load_pickled_features(mocap_rollout_path)[:60]

    plot_features(trpo_features.T, title="TRPO")
    plot_features(mocap_features.T, title="Mocap")

    trpo_positions = load_pickled_positions(trpo_rollout_path)[:2000]
    mocap_positions = load_pickled_positions(mocap_rollout_path)[:2000]

    plot_time_positions(trpo_positions, keys=["root"], title="TRPO")
    plot_time_positions(mocap_positions, keys=["root"], title="Mocap")

    tabulate_features(trpo_features.T, title="TRPO")
    tabulate_features(mocap_features.T, title="Mocap")

    plt.show()


class Solution:
    # @param    A       a list of integers
    # @param    elem    an integer, value need to be removed
    # @return an integer
    def removeElement(self, A, elem):
        i = 0
        for num in A:
            if num != elem:
                A[i] = num
                i += 1
        return i


# Group 23

# Code to implement the perceptron network on season by season

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import model_selection
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
'''with open('C:/Users/user/Downloads/indian-premier-league-csv-dataset/data/season9.csv','w') as f:
    for l in lines:
         f.write('('+l+'),\n')'''
         #shuffle=True, random_state=None,

class linear_network:
    def __init__(self,eta=0.01,n_iter=1000):  # new
        self.eta = eta
        self.n_iter = n_iter

    def initializeWeights(self,data):
        self.w=np.zeros(1 + data.shape[1]) # +1 has been done to include bias weight

    def train(self,data,target):
        for epoch in range(self.n_iter):
            for x, y in zip(data, target):
                delta= self.eta * (y - self.predict(x))
                self.w[1:] += delta * x
                self.w[0] += delta
        return self

    def netvalue(self, X):
        """Calculate net input"""
        return np.dot(X, self.w[1:]) + self.w[0]

    def predict(self, X):
        """Return class label after unit step"""
        return np.where(self.netvalue(X) >= 0.0, 1, -1)

    def test(self,data,target):
        y=np.dot(data,self.w[1:])+self.w[0]
        output=np.where(y>=0.0,1,-1)
        print(accuracy_score(target,output))
        print(confusion_matrix(target,output))
        print(classification_report(target,output))
        self.accuracy(target,output)

    def accuracy(self,target,output):
        correct=0
        n=len(target)
        for i in range(n):
            if(output[i]==target[i]):
                correct+=1
        print( correct / float(len(target)) * 100)

df_train=pd.read_csv('C:/Users/user/Downloads/indian-premier-league-csv-dataset/season1.csv')
df_test=pd.read_csv('C:/Users/user/Downloads/indian-premier-league-csv-dataset/season8.csv')

df_train['Average_runs']=pd.to_numeric(df_train['Average_runs'], errors='coerce').fillna(0.0).astype(np.float)
df_train['Average_wickets']=pd.to_numeric(df_train['Average_wickets'], errors='coerce').fillna(0.0).astype(np.float)
df_train['Performance_rating']=pd.to_numeric(df_train['Performance_rating'], errors='coerce').fillna(-1.0).astype(np.float)
df_train['Player_Id']=pd.to_numeric(df_train['Player_Id'], errors='coerce').fillna(0).astype(np.int64)
df_train['Runs_Scored']=pd.to_numeric(df_train['Runs_Scored'], errors='coerce').fillna(0).astype(np.int64)
df_train['Wickets_Taken']=pd.to_numeric(df_train['Wickets_Taken'], errors='coerce').fillna(0).astype(np.int64)
df_train['Outs']=pd.to_numeric(df_train['Outs'], errors='coerce').fillna(0).astype(np.int64)
df_train['Matches_Played']=pd.to_numeric(df_train['Matches_Played'], errors='coerce').fillna(0).astype(np.int64)

df_test['Average_runs']=pd.to_numeric(df_test['Average_runs'], errors='coerce').fillna(0.0).astype(np.float)
df_test['Average_wickets']=pd.to_numeric(df_test['Average_wickets'], errors='coerce').fillna(0.0).astype(np.float)
df_test['Performance_rating']=pd.to_numeric(df_test['Performance_rating'], errors='coerce').fillna(-1.0).astype(np.float)
df_test['Player_Id']=pd.to_numeric(df_test['Player_Id'], errors='coerce').fillna(0).astype(np.int64)
df_test['Runs_Scored']=pd.to_numeric(df_test['Runs_Scored'], errors='coerce').fillna(0).astype(np.int64)
df_test['Wickets_Taken']=pd.to_numeric(df_test['Wickets_Taken'], errors='coerce').fillna(0).astype(np.int64)
df_test['Outs']=pd.to_numeric(df_test['Outs'], errors='coerce').fillna(0).astype(np.int64)
df_test['Matches_Played']=pd.to_numeric(df_test['Matches_Played'], errors='coerce').fillna(0).astype(np.int64)

X = df_train.iloc[1:,1:-1].values
y=df_train.iloc[1:,-1].values

X1 = df_test.iloc[1:,1:-1].values
y1=df_test.iloc[1:,-1].values

ppn = linear_network(0.01,100)
ppn.initializeWeights(X)
ppn.train(X, y)

ppn.test(X1,y1)


class Solution(object):
    def maxProduct(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # Time O(n)
        # Memory O(1)
        currentMin = 1
        currentMax = 1
        result = nums[0]
        for num in nums:
            temp = num * currentMax # currentMax could change, so we use a temporary variable.
            currentMax = max(num * currentMax, num * currentMin, num)
            currentMin = min(temp, num * currentMin, num)
            result = max(result, currentMax)
        return result


from __future__ import annotations  # for python 3.8
from enum import Enum
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import QTreeWidgetItem, QLabel, QHBoxLayout, QVBoxLayout, QGridLayout, QWidget

from agstoolbox.at_icons import ags_editor_as_pixmap
from agstoolbox.at_tasks import do_download_managed
from agstoolbox.core.ags.ags_editor import LocalAgsEditor
from agstoolbox.core.ags.ags_local_run import start_ags_editor
from agstoolbox.core.utils.open_in_browser import open_in_browser
from agstoolbox.wdgts_utils.ags_local_extra import ags_editor_folder_in_explorer
from agstoolbox.core.gh.release import Release
from agstoolbox.core.utils.time import s_ago
from agstoolbox.wdgts_utils.action_utils import DefaultMenuQAction

class ToolType(Enum):
    HEADER = 16
    MANAGED_TOOL = 1
    AVAILABLE_TO_DOWNLOAD = 2
    EXTERNALLY_INSTALLED_TOOL = 4

    MANAGED_TOOL_HEADER = 17
    AVAILABLE_TO_DOWNLOAD_HEADER = 18
    EXTERNALLY_INSTALLED_TOOL_HEADER = 20

class TreeItemTool(QTreeWidgetItem):
    tool_type: ToolType = None

    def __init__(self, parent: QTreeWidgetItem | None, tool_type: ToolType):
        QTreeWidgetItem.__init__(self, parent)
        self.tool_type = tool_type

class EditorImage(QLabel):
    def __init__(self, parent: QWidget = None):
        QLabel.__init__(self, parent)

        pxmap = ags_editor_as_pixmap()

        self.setPixmap(pxmap.scaled(
            32, 32,
            QtCore.Qt.AspectRatioMode.KeepAspectRatio,
            QtCore.Qt.TransformationMode.SmoothTransformation))
        self.setMaximumSize(32, 32)
        self.setMinimumSize(32, 32)

class TreeItemTool_Header(TreeItemTool):
    name = None

    def __init__(self, name: str, tool_type: ToolType):
        TreeItemTool.__init__(self, None, tool_type)
        self.name = name
        self.setWhatsThis(0, name)
        self.tool_type = tool_type
        self.setText(0, name)

    def __lt__(self, other):
        return False  # keeps unaltered when sorting

    def clear(self):
        for i in range(self.childCount()):
            self.removeChild(self.child(0))

# Tool Download Item
class TreeItemTool_Download_Widget(QWidget):
    release = None
    # we need to store the thread or the garbage collector will remove it!
    thread_download = None

    def __init__(self, release: Release, parent: QWidget = None):
        QWidget.__init__(self, parent)
        self.release = release

        self.icon_img = EditorImage()
        self.labelName = QLabel(self.release.name)
        self.labelName.setWordWrap(True)

        smaller_font = QtGui.QFont(
            self.labelName.font().family(),
            self.labelName.font().pointSize() * 0.90,
        )

        smallest_font = QtGui.QFont(
            self.labelName.font().family(),
            self.labelName.font().pointSize() * 0.75,
        )

        self.labelTime = QLabel(s_ago(self.release.published_at_timestamp))
        self.labelTime.setFont(smaller_font)
        self.labelTime.setAlignment(QtCore.Qt.AlignmentFlag.AlignRight)

        self.labelDir = QLabel(self.release.url)
        self.labelDir.setFont(smallest_font)
        self.labelDir.setDisabled(True)

        top_hbox = QHBoxLayout()
        bottom_hbox = QHBoxLayout()

        right_vbox = QVBoxLayout()

        vbox = QVBoxLayout()

        vbox.addLayout(top_hbox)
        vbox.addLayout(bottom_hbox)
        top_hbox.addWidget(self.labelName)
        right_vbox.addWidget(self.labelTime)
        top_hbox.addLayout(right_vbox)
        bottom_hbox.addWidget(self.labelDir)

        main_qgrid = QGridLayout()
        main_qgrid.addWidget(self.icon_img)
        main_qgrid.addLayout(vbox, 0, 1)

        self.setLayout(main_qgrid)

    def install(self):
        if self.thread_download is not None:
            return
        self.thread_download = do_download_managed(self.release, self.download_ended)
        self.setEnabled(False)

    def show_gh_rel_page(self):
        open_in_browser(self.release.html_url)

    def mouseDoubleClickEvent(self, event):
        self.install()

    def download_ended(self):
        self.thread_download = None
        self.parent().parent().tools_schd_update_managed()
        self.setEnabled(True)

    def contextMenuEvent(self, event):
        menu = QtWidgets.QMenu(self)
        install_action = DefaultMenuQAction(menu, "Install as managed Editor")
        show_gh_rel_page_action = menu.addAction("Show GitHub Release Page")
        action = menu.exec(self.mapToGlobal(event.pos()))
        if action == install_action:
            self.install()
        elif action == show_gh_rel_page_action:
            self.show_gh_rel_page()

# tool available locally for use
# either managed or unmanaged (externally installed)
class TreeItemTool_Local_Widget(QWidget):
    ags_editor = None

    def __init__(self, ags_editor: LocalAgsEditor, tool_type: ToolType, parent: QWidget = None):
        QWidget.__init__(self, parent)
        self.ags_editor = ags_editor

        self.icon_img = EditorImage()
        self.labelName = QLabel(self.ags_editor.name)
        self.labelName.setWordWrap(True)

        smaller_font = QtGui.QFont(
            self.labelName.font().family(),
            self.labelName.font().pointSize() * 0.90,
        )

        smallest_font = QtGui.QFont(
            self.labelName.font().family(),
            self.labelName.font().pointSize() * 0.75,
        )

        self.labelVersion = QLabel(self.ags_editor.version.as_str)
        self.labelVersion.setFont(smaller_font)
        self.labelVersion.setAlignment(QtCore.Qt.AlignmentFlag.AlignRight)

        self.labelTime = QLabel(s_ago(self.ags_editor.last_modified))
        self.labelTime.setFont(smaller_font)
        self.labelTime.setAlignment(QtCore.Qt.AlignmentFlag.AlignRight)

        self.labelDir = QLabel(self.ags_editor.path)
        self.labelDir.setFont(smallest_font)
        self.labelDir.setDisabled(True)

        top_hbox = QHBoxLayout()
        bottom_hbox = QHBoxLayout()

        right_vbox = QVBoxLayout()

        vbox = QVBoxLayout()

        vbox.addLayout(top_hbox)
        vbox.addLayout(bottom_hbox)
        top_hbox.addWidget(self.labelName)
        right_vbox.addWidget(self.labelTime)
        right_vbox.addWidget(self.labelVersion)
        top_hbox.addLayout(right_vbox)
        bottom_hbox.addWidget(self.labelDir)

        main_qgrid = QGridLayout()
        main_qgrid.addWidget(self.icon_img)
        main_qgrid.addLayout(vbox, 0, 1)

        self.setLayout(main_qgrid)

    def open_editor(self):
        start_ags_editor(self.ags_editor)

    def mouseDoubleClickEvent(self, event):
        self.open_editor()

    def contextMenuEvent(self, event):
        menu = QtWidgets.QMenu(self)
        open_editor_action = DefaultMenuQAction(menu, "Open Editor")
        open_folder_action = menu.addAction("Open Folder in File Explorer")
        action = menu.exec(self.mapToGlobal(event.pos()))
        if action == open_editor_action:
            self.open_editor()
        elif action == open_folder_action:
            ags_editor_folder_in_explorer(self.ags_editor)

##################################################################################################
# item widgets

class TreeItemTool_Managed(TreeItemTool):
    def __init__(self, parent: QTreeWidgetItem, ags_editor: LocalAgsEditor):
        TreeItemTool.__init__(self, parent, ToolType.MANAGED_TOOL)
        self.itm_wdgt = TreeItemTool_Local_Widget(ags_editor, self.tool_type)

    def __lt__(self, other):
        return self.itm_wdgt.ags_editor.version.as_int < other.itm_wdgt.ags_editor.version.as_int

    def updateInTree(self):
        self.treeWidget().setItemWidget(self, 0, self.itm_wdgt)

class TreeItemTool_ExternallyInstalled(TreeItemTool):
    def __init__(self, parent: QTreeWidgetItem, ags_editor: LocalAgsEditor):
        TreeItemTool.__init__(self, parent, ToolType.EXTERNALLY_INSTALLED_TOOL)
        self.itm_wdgt = TreeItemTool_Local_Widget(ags_editor, self.tool_type)

    def __lt__(self, other):
        return self.itm_wdgt.ags_editor.version.as_int < other.itm_wdgt.ags_editor.version.as_int

    def updateInTree(self):
        self.treeWidget().setItemWidget(self, 0, self.itm_wdgt)

class TreeItemTool_Download(TreeItemTool):
    def __init__(self, parent: QTreeWidgetItem, release: Release):
        TreeItemTool.__init__(self, parent, ToolType.AVAILABLE_TO_DOWNLOAD)
        self.itm_wdgt = TreeItemTool_Download_Widget(release)

    def updateInTree(self):
        self.treeWidget().setItemWidget(self, 0, self.itm_wdgt)


from telegram import Update
from telegram.ext import Updater,CommandHandler,MessageHandler,Filters,CallbackContext
from telegram import Bot,ReplyKeyboardMarkup,Update
import text

def start(update:Update,context:CallbackContext):
    author = update.message.from_user.first_name
    reply = "Hello "+"{}".format(author)+"!"
    update.message.reply_text(reply + text.welcome_text)

def help(update:Update,context:CallbackContext):
    author = update.message.from_user.first_name
    reply = "Hello "+"{}".format(author)+" This is an Information bot!"
    update.message.reply_text(reply)

def main():
    updater = Updater("5089790707:AAHytQvl34MDIXsyo1Ne5PldiwcR7PNvhnk", use_context=True)
    dispatcher = updater.dispatcher
    dispatcher.add_handler(CommandHandler('start',start))
    dispatcher.add_handler(CommandHandler('help',help))
    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()


import requests
from bs4 import BeautifulSoup
import httpx
import asyncio
import time

def fetch_html_content(url):
    response = requests.get(url)
    return response.text

def find_matching_domains(td_texts, domain):
    printed_strings = set()
    for td_text in td_texts:
        if domain in td_text.lower() and td_text.count('.') == 2 and td_text not in printed_strings:
            printed_strings.add(td_text)
    return printed_strings

def prepare_urls(printed_strings):

    urls = set()
    for x in printed_strings:
        urls.add("http://" + x)
        urls.add("https://" + x)
    return urls

async def sc_check(urls):

    check_urls = list(urls)
    start_time = time.time()

    for x in check_urls:
        try:
            async with httpx.AsyncClient(verify=True, timeout=5.0) as client:
                check = await client.get(x)
                print(f"{x}: {check.status_code}")

        except httpx.RequestError as e:
            print(f"{x}: Error - {e}")

    print("\n", "Process finished --- %s seconds ---" % (time.time() - start_time))

    return x, check.status_code

def main():

    domain = input("What is the domain? Example: google.nl: ")
    url = "https://crt.sh/?q=" + domain

    try:
        response = fetch_html_content(url)
        soup = BeautifulSoup(response, "html.parser")
        td_texts = [td.get_text() for td in soup.find_all("td")]
        printed_strings = find_matching_domains(td_texts, domain)
        urls = prepare_urls(printed_strings)
        asyncio.run(sc_check(urls))

    except requests.RequestException as e:
        print("Error fetching or parsing data:", e)
    except KeyboardInterrupt:
        print("Operation interrupted by user.")

if __name__ == "__main__":
    main()


# BOJ 1085 직사각형에서 탈출

# 한수는 지금 (x, y)에 있다. 직사각형의 왼쪽 아래 꼭짓점은 (0, 0)에 있고, 오른쪽 위 꼭짓점은 (w, h)에 있다.
# 직사각형의 경계선까지 가는 거리의 최솟값을 구하는 프로그램을 작성하시오.

# 첫째 줄에 x, y, w, h가 주어진다.
# 첫째 줄에 문제의 정답을 출력한다.

import sys

x,y,w,h = map(int, sys.stdin.readline().split())

w_min = min(x, w-x)
h_min = min(y, h-y)

result = min(w_min, h_min)
print(result)


from typing import List

class Solution:
    def leftRightDifference(self, nums: List[int]) -> List[int]:

        left_sum = [0 for _ in range(len(nums))]
        running_total = 0
        for idx in range(1, len(nums)):
            left_sum[idx] = nums[idx - 1] + running_total
            running_total = left_sum[idx]

        right_sum = [0 for _ in range(len(nums))]
        running_total = 0
        for idx in range(len(nums) - 2, -1, -1):
            right_sum[idx] = nums[idx + 1] + running_total
            running_total = right_sum[idx]

        return [
            abs(x - y) for x, y in zip(left_sum, right_sum)
        ]

if __name__ == '__main__':
    Solution().leftRightDifference([10, 4, 8, 3])


import argparse
import os
import json
import matplotlib.pyplot as plt

def get_output_filename(inp_fname):
    return inp_fname.replace('output', 'graph.png')

def get_x_value(graph_type, x):
    if graph_type == "trials":
        return int(x[11:-1])
    elif graph_type == "initpolicy":
        return int(x[15:-2])
    elif graph_type == "maxiter":
        return int(x[12: -1])
    elif graph_type == "revert":
        return int(x[11:-1])
    elif graph_type == "target":
        return int(x[11:-1])

def get_json_name(graph_type):
    return '{}_test_results.json'.format(graph_type)

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("graph_type", help="type of graphs (eg., cweights, trials etc)", type=str)
    parser.add_argument("inp_file", help="input file whose data should be plotted", type=str)
    args = parser.parse_args()

    with open(args.inp_file, mode='r', encoding='utf-8') as json_file:
        data = json.load(json_file)

    data_to_plot = list()
    for k in data.keys():
        print(k)
        data_to_plot.append(data[k])

    # for a in data_to_plot:
    #     print(a)
    #     print()

    # fig = plt.figure(1)
    # ax = fig.add_subplot(111)
    # bp = ax.boxplot(data_to_plot)
    # plt.show()
    plt.boxplot(data_to_plot)
    plt.show()

    # with open(args.inp_file, mode='r', encoding='utf-8') as input_file:
    #     file_content = input_file.readlines()

    # x_vals = list()
    # y_vals = list()
    # for line in file_content:
    #     x, y = line.strip().split(' ')
    #     x_vals.append(get_x_value(args.graph_type, x))
    #     # x_vals.append(x)
    #     y_vals.append(y)
    #
    # for a, b in zip(x_vals, y_vals):
    #     print(a, b)
    #
    # plt.plot(x_vals, y_vals, marker='o')
    # plt.xlabel('{} setting'.format(args.inp_file))
    # plt.ylabel('runtime (millisecond)')
    # fig = plt.gcf()
    # plt.show()
    # # fig.savefig('{}.png'.format(args.inp_file), dpi=100)
    # fig.savefig(get_output_filename(args.inp_file))

if __name__ == '__main__':
    main()