from pytorch_transformers.modeling_bert import BertConfig
from pytorch_transformers import optimization

import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

from datasets import Datasets
from model import BertForEmotionClassification
from optim import layerwise_decay_optimizer

from sklearn.metrics import classification_report, confusion_matrix
from tqdm import tqdm
import time
import pickle
import utils
import logging
import argparse
import random
import os
import warnings

warnings.filterwarnings('ignore')
logger = utils.get_logger('BERT Classification')
logger.setLevel(logging.INFO)

class ClassificationBatchFunction:
    # batch function for pytorch dataloader
    def __init__(self, max_len, pad_idx, cls_idx, sep_idx):
        self.max_len = max_len
        self.pad_idx = pad_idx
        self.cls_idx = cls_idx
        self.sep_idx = sep_idx

    def __call__(self, batch):
        tokens, label = list(zip(*batch))

        # Get max length from batch
        max_len = min(self.max_len, max([len(i) for i in tokens]))
        tokens = torch.tensor([self.pad([self.cls_idx] + t + [self.sep_idx], max_len) for t in tokens])
        masks = torch.ones_like(tokens).masked_fill(tokens == self.pad_idx, 0)

        return tokens, masks, torch.tensor(label)

    def pad(self, sample, max_len):
        diff = max_len - len(sample)
        if diff > 0:
            sample += [self.pad_idx] * diff
        else:
            sample = sample[-max_len:]
        return sample

def train(args):
    set_seed(args)
    args.train_batch_size = args.train_batch_size*args.n_gpu
    args.eval_batch_size = args.eval_batch_size*args.n_gpu

    # Set device
    if args.device == 'cuda':
        device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
        logger.info('use cuda')
    else:
        device = torch.device('cpu')
        logger.info('use cpu')

    # Set label list for classification
    if args.num_label == 'multi':
        label_list = ['공포', '놀람', '분노', '슬픔', '중립', '행복', '혐오']
    elif args.num_label == 'binary':
        label_list = ['긍정', '부정']
    logger.info('use {} labels for training'.format(len(label_list)))

    # Load pretrained model and model configuration
    pretrained_path = os.path.join('./pretrained_model/', args.pretrained_type)
    if args.pretrained_model_path is None:
        # Use pretrained bert model(etri/skt)
        pretrained_model_path = os.path.join(pretrained_path, 'pytorch_model.bin')
    else:
        # Use further-pretrained bert model
        pretrained_model_path = args.pretrained_model_path
    logger.info('Pretrain Model : {}'.format(pretrained_model_path))
    pretrained = torch.load(pretrained_model_path)

    # weight
    if args.pretrained_type == 'skt' and 'bert.' not in list(pretrained.keys())[0]:
        logger.info('modify parameter names')
        # Change parameter name for consistency
        new_keys_ = ['bert.' + k for k in pretrained.keys()]
        old_values_ = pretrained.values()
        pretrained = {k: v for k, v in zip(new_keys_, old_values_)}

    # bulid model
    bert_config = BertConfig(os.path.join(pretrained_path + '/bert_config.json'))
    bert_config.num_labels = len(label_list)
    model = BertForEmotionClassification(bert_config).to(device)

    # assigning weight
    model.load_state_dict(pretrained, strict=False)

    # Load Datasets
    tr_set = Datasets(file_path=args.train_data_path,
                      label_list=label_list,
                      pretrained_type=args.pretrained_type,
                      max_len=args.max_len)
    dev_set = Datasets(file_path=args.dev_data_path,
                       label_list=label_list,
                       pretrained_type=args.pretrained_type,
                       max_len=args.max_len)

    # Use custom batch function
    collate_fn = ClassificationBatchFunction(args.max_len, tr_set.pad_idx, tr_set.cls_idx, tr_set.sep_idx)

    tr_loader = DataLoader(dataset=tr_set,
                           batch_size=args.train_batch_size,
                           shuffle=True,
                           num_workers=8,
                           pin_memory=True,
                           collate_fn=collate_fn)

    dev_loader = DataLoader(dataset=dev_set,
                            batch_size=args.eval_batch_size,
                            num_workers=8,
                            pin_memory=True,
                            drop_last=False,
                            collate_fn=collate_fn)

    # optimizer
    optimizer = layerwise_decay_optimizer(model=model, lr=args.learning_rate, layerwise_decay=args.layerwise_decay)

    # lr scheduler
    t_total = len(tr_loader) // args.gradient_accumulation_steps * args.epochs
    warmup_steps = int(t_total * args.warmup_percent)
    logger.info('total training steps : {}, lr warmup steps : {}'.format(t_total, warmup_steps))
    # Use gradual warmup and linear decay
    scheduler = optimization.WarmupLinearSchedule(optimizer, warmup_steps=warmup_steps, t_total=t_total)

    # for low-precision training
    if args.fp16:
        try:
            from apex import amp
            logger.info('Use fp16')
        except ImportError:
            raise ImportError("Please install apex from https://www.github.com/nvidia/apex to use fp16 training.")
        model, optimizer = amp.initialize(model, optimizer, opt_level=args.fp16_opt_level, verbosity=0)

    # tensorboard setting
    save_path = "./model_saved_finetuning/lr{},batch{},total{},warmup{},len{},{}".format(
        args.learning_rate, args.train_batch_size * args.gradient_accumulation_steps, t_total,
        args.warmup_percent, args.max_len, args.pretrained_type)

    if not os.path.isdir(save_path):
        os.makedirs(save_path)
    writer = SummaryWriter(save_path)

    # Save best model results with resultwriter
    result_writer = utils.ResultWriter("./model_saved_finetuning/results.csv")
    model.zero_grad()

    best_val_loss = 1e+9
    global_step = 0

    train_loss, train_acc, train_f1 = 0, 0, 0
    logging_loss, logging_acc, logging_f1 = 0, 0, 0

    logger.info('***** Training starts *****')
    total_result = []
    for epoch in tqdm(range(args.epochs), desc='epochs'):
        for step, batch in tqdm(enumerate(tr_loader), desc='steps', total=len(tr_loader)):
            model.train()
            x_train, mask_train, y_train = map(lambda x: x.to(device), batch)

            inputs = {
                'input_ids': x_train,
                'attention_mask': mask_train,
                'classification_label': y_train,
            }

            output, loss = model(**inputs)
            y_max = output.max(dim=1)[1]

            cr = classification_report(y_train.tolist(),
                                       y_max.tolist(),
                                       labels=list(range(len(label_list))),
                                       target_names=label_list,
                                       output_dict=True)
            # Get accuracy(micro f1)
            if 'micro avg' not in cr.keys():
                batch_acc = list(cr.items())[len(label_list)][1]
            else:
                # If at least one of labels does not exists in mini-batch, use micro average instead
                batch_acc = cr['micro avg']['f1-score']
            # macro f1
            batch_macro_f1 = cr['macro avg']['f1-score']

            # accumulate measures
            grad_accu = args.gradient_accumulation_steps
            if grad_accu > 1:
                loss /= grad_accu
                batch_acc /= grad_accu
                batch_macro_f1 /= grad_accu

            if args.fp16:
                with amp.scale_loss(loss, optimizer) as scaled_loss:
                    scaled_loss.backward()
            else:
                if args.n_gpu > 1:
                    loss = loss.mean()
                    loss.backward()
                else:
                    loss.backward()

            train_loss += loss.item()
            train_acc += batch_acc
            train_f1 += batch_macro_f1

            if (global_step + 1) % grad_accu == 0:
                if args.fp16:
                    torch.nn.utils.clip_grad_norm_(amp.master_params(optimizer), args.grad_clip_norm)
                else:
                    torch.nn.utils.clip_grad_norm_(model.parameters(), args.grad_clip_norm)

                optimizer.step()
                scheduler.step()
                model.zero_grad()
                global_step += 1

                if global_step % args.logging_step == 0:
                    acc_ = (train_acc - logging_acc) / args.logging_step
                    f1_ = (train_f1 - logging_f1) / args.logging_step
                    loss_ = (train_loss - logging_loss) / args.logging_step
                    writer.add_scalars('loss', {'train': loss_}, global_step)
                    writer.add_scalars('acc', {'train': acc_}, global_step)
                    writer.add_scalars('macro_f1', {'train': f1_}, global_step)

                    logger.info('[{}/{}], trn loss : {:.3f}, trn acc : {:.3f}, macro f1 : {:.3f}'.format(
                        global_step, t_total, loss_, acc_, f1_
                    ))
                    logging_acc, logging_f1, logging_loss = train_acc, train_f1, train_loss

                    # Get f1 score for each label
                    f1_results = [(l, r['f1-score']) for i, (l, r) in enumerate(cr.items()) if i < len(label_list)]
                    f1_log = "\n".join(["{} : {}".format(l, f) for l, f in f1_results])
                    logger.info("\n\n***f1-score***\n" + f1_log + "\n\n***confusion matrix***\n{}".format(
                        confusion_matrix(y_train.tolist(), y_max.tolist())))

        # Validation
        val_loss, val_acc, val_macro_f1, _ = evaluate(args, dev_loader, model, device)
        val_result = '[{}/{}] val loss : {:.3f}, val acc : {:.3f}. val macro f1 : {:.3f}'.format(
            global_step, t_total, val_loss, val_acc, val_macro_f1
        )

        writer.add_scalars('loss', {'val': val_loss}, global_step)
        writer.add_scalars('acc', {'val': val_acc}, global_step)
        writer.add_scalars('macro_f1', {'val': val_macro_f1}, global_step)
        logger.info(val_result)
        total_result.append(val_result)

        if val_loss < best_val_loss:
            # Save model checkpoints
            torch.save(model.state_dict(), os.path.join(save_path, 'best_model.bin'))
            torch.save(args, os.path.join(save_path, 'training_args.bin'))
            logger.info('Saving model checkpoint to %s', save_path)
            best_val_loss = val_loss
            best_val_acc = val_acc
            best_val_macro_f1 = val_macro_f1

    # Save results in 'model_saved_finetuning/results.csv'
    results = {
        'val_loss': best_val_loss,
        'val_acc': best_val_acc,
        'val_macro_f1' : best_val_macro_f1,
        'save_dir': save_path,
        'pretrained_path': pretrained_path,
    }
    result_writer.update(args, **results)
    return global_step, loss_, acc_, best_val_loss, best_val_acc, total_result

def evaluate(args, dataloader, model, device, objective='classification'):

    if args.num_label == 'multi':
        label_list = ['공포', '놀람', '분노', '슬픔', '중립', '행복', '혐오']
    elif args.num_label == 'binary':
        label_list = ['긍정', '부정']

    val_loss, val_acc, val_f1 = 0, 0, 0
    total_y = []
    total_y_hat = []

    for val_step, batch in enumerate(dataloader):
        model.eval()

        x_dev, mask_dev, y_dev = map(lambda x: x.to(device), batch)
        total_y += y_dev.tolist()

        inputs = {
            'input_ids': x_dev,
            'attention_mask': mask_dev,
            'classification_label': y_dev,
        }
        with torch.no_grad():
            output, loss = model(**inputs)
            if args.n_gpu > 1:
                loss = loss.mean()
            y_max = output.max(dim=1)[1]
            total_y_hat += y_max.tolist()
            val_loss += loss.item()

    # f1-score 계산
    dev_cr = classification_report(total_y,
                                   total_y_hat,
                                   labels=list(range(len(label_list))),
                                   target_names=label_list,
                                   output_dict=True)

    # Get accuracy(micro f1)
    if 'micro avg' not in dev_cr.keys():
        val_acc = list(dev_cr.items())[len(label_list)][1]
    else:
        # If at least one of labels does not exists in mini-batch, use micro average instead
        val_acc = dev_cr['micro avg']['f1-score']
    # macro f1
    val_macro_f1 = dev_cr['macro avg']['f1-score']

    logger.info('***** Evaluation Results *****')
    f1_results = [(l, r['f1-score']) for i, (l, r) in enumerate(dev_cr.items()) if i < len(label_list)]
    f1_log = "\n".join(["{} : {}".format(l, f) for l, f in f1_results])
    cm = confusion_matrix(total_y, total_y_hat)
    logger.info("\n***f1-score***\n" + f1_log + "\n***confusion matrix***\n{}".format(cm))

    val_loss /= (val_step + 1)
    return val_loss, val_acc, val_macro_f1, (total_y_hat, cm)

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

def main():
    parser = argparse.ArgumentParser()

    # Pretrained model Parameters
    parser.add_argument("--pretrained_type", default='etri', type=str,
                        help="type of pretrained model (skt, etri)")
    parser.add_argument("--pretrained_model_path", required=False,
                        help="path of pretrained model (If you wnat to use further-pretrained model)")

    # Train Parameters
    parser.add_argument("--train_batch_size", default=100, type=int,
                        help="batch size")
    parser.add_argument("--eval_batch_size", default=100, type=int,
                        help="batch size for validation")
    parser.add_argument("--layerwise_decay", action="store_true",
                        help="Whether to use layerwise decay")
    parser.add_argument("--learning_rate", default=2e-5, type=float,
                        help="The initial learning rate for Adam")
    parser.add_argument("--epochs", default=50, type=int,
                        help="total epochs")
    parser.add_argument("--gradient_accumulation_steps", default=1, type=int,
                        help="gradient accumulation steps for large batch training")
    parser.add_argument("--warmup_percent", default=0.1, type=float,
                        help="gradient warmup percentage")
    parser.add_argument("--grad_clip_norm", default=1.0, type=float,
                        help="batch size")

    # Other Parameters
    parser.add_argument("--logging_step", default=2, type=int,
                        help="logging step for training loss and acc")
    parser.add_argument("--device", default='cuda', type=str,
                        help="Whether to use cpu or cuda")
    parser.add_argument("--fp16", action="store_true",
                        help="Whether to use nvidia mixed precision training")
    parser.add_argument('--fp16_opt_level', type=str, default='O1',
                        help="For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3']."
                        "See details at https://nvidia.github.io/apex/amp.html")
    parser.add_argument("--seed", default=0, type=int,
                        help="Random seed(default=0)")
    parser.add_argument("--n_gpu", default=1, type=int,
                        help="n_gpu")
    # Data Parameters
    parser.add_argument("--train_data_path", default='../split_datasets_vat/train.pkl', type=str,
                        help="train data path")
    parser.add_argument("--dev_data_path", default='../split_datasets_vat/dev.pkl', type=str,
                        help="dev data path")
    parser.add_argument("--num_label", default='multi', type=str,
                        help="Number of labels in datastes(binary or multi)")
    parser.add_argument("--max_len", default=64, type=int,
                        help="Maximum sequence length")

    args = parser.parse_args()
    set_seed(args)

    t = time.time()
    global_step, train_loss, train_acc, best_val_loss, best_val_acc, total_result = train(args)
    elapsed = time.time() - t

    logger.info('***** Training done *****')
    logger.info('total validation results : \n'+'\n'.join(total_result))
    logger.info('elapsed time: %.3f Hours' % (elapsed / 3600.))
    logger.info('best acc in test: %.4f' % best_val_acc)
    logger.info('best loss in test: %.4f' % best_val_loss)

if __name__ == '__main__':
    main()


import json
from django.shortcuts import render
from django.http import JsonResponse
from common.utils import common_response, logger, page_calculate
from common.models import User, Grade
from knowledges.models import Knowledge
from knowledges.serializers.knowledge import knowledge_filter, knowledge_data

# 教程列表页面
def knowledge_list_page(request):
    if request.method == "GET":
        knowledges = [{"id": k.id, "name": k.title} for k in knowledge_filter()]
        datas = {
            "knowledges": knowledges
        }
        return render(request, "knowledge/knowledge_list.html", datas)

# 教程列表
def knowledge_list(request):
    if request.method == "GET":
        user = request.user
        filter_name = request.GET.get('filter_name')
        filter_knowledge = request.GET.get('filter_knowledge')
        query = knowledge_filter()
        if filter_knowledge:
            query = query.filter(id=filter_knowledge)
        if filter_name:
            query = query.filter(title__icontains=filter_name)
        data_dict = {}
        try:
            logger.info(query.count())
            data_dict, datas = page_calculate(request_data=request.GET, data=query)
            result = []
            for data in datas:
                logger.info(data.id)
                res = knowledge_data(knowledge=data)
                result.append(res)
            data_dict['rows'] = result
        except Exception as ex:
            logger.exception(ex)
        return JsonResponse(data_dict)

# 教程删除
def knowledge_delete(request):
    req = json.loads(request.body)
    knowledge_id = req.get('id')
    knowledge_filter(id=knowledge_id).delete()
    return common_response()


"""Training reserves serializers."""

# Django
from django.core.validators import RegexValidator
from django.utils import timezone

# Django REST Framework
from rest_framework import serializers

# Models
from users.models import User
from bookings.models import TrainingReserve

# Serializers
from users.serializers import UserModelSerializer

class TrainingReserveModelSerializer(serializers.ModelSerializer):
    """Appointment model serializer."""

    user = UserModelSerializer(read_only=True)

    class Meta:
        """Meta class."""
        model = TrainingReserve
        fields = ('__all__')
        read_only_fields = ['user']

class CreateTrainingReserveSerializer(serializers.Serializer):
    """Create a training reserve."""

    identification_regex = RegexValidator(
        regex=r"\d{6,10}$",
        message='Identification number must be entered in the format: 199999999. Up to 11 digits allowed'
    )
    identification_number = serializers.CharField(validators=[identification_regex])
    date = serializers.DateTimeField()

    def validate(self, data):
        """Verify that the user has an active membership,
        and the reserve. """
        user = User.objects.get(
            identification_number=data['identification_number'],
            is_verified=True
        )
        profile = user.profile
        reserves = TrainingReserve.objects.filter(date=data['date']).count()

        try:
            user_reserve = TrainingReserve.objects.get(user=user)
            if user_reserve:
                raise serializers.ValidationError('You already have a reserve.')
        except TrainingReserve.DoesNotExist:
            if profile.is_active == False:
                raise serializers.ValidationError('The user does not have an active membership.')
            if data['date'] <= timezone.now():
                raise serializers.ValidationError('Time not available')
            # Aforo max 26 por hora debido a la pandemia
            if reserves == 26:
                raise serializers.ValidationError('The capacity at this time is full.')
            else:
                return data

    def create(self, validated_data):
        """Create a training reserve."""
        user = User.objects.get(
            identification_number=validated_data.pop('identification_number')
        )
        reserve = TrainingReserve.objects.create(
            user=user,
            date=validated_data['date']
        )
        return reserve


import os
import shlex
import subprocess
import sys

db = []
dbSet = set()

# 读文件名列表
def read_file_db(db_path):
    if os.path.isfile(db_path):
        with open(db_path, 'r+') as f:
            for line in f:
                _line = line.strip()
                db.append(_line)
                dbSet.add(_line)

# 保存文件名列表
def save_file_db(db_path):
    with open(db_path, 'w') as f:
        for filename in db:
            f.write(filename + '\n')

# 反汇编
def process(target_dir, save_to):
    for fpath, dirs, fs in os.walk(target_dir):
        total = len(fs)
        count = 0
        for f in fs:
            count = count + 1
            print('{0}/{1}'.format(count, total))
            if f.endswith('.exe') or f.endswith('.dll'):
                filename = os.path.join(fpath, f)
                if filename in dbSet:
                    print('Skip ' + filename)
                else:
                    print(filename)
                    db.append(filename)
                    dbSet.add(filename)
                    line_no = len(db)
                    cmd = '"D:\\IDA\\IDA 7.0\\ida.exe" -c -A -S"D:\\Work\\GetData\\dump_ret.idc ' \
                          + save_to + ' ' + str(line_no) + '" "' + filename + '"'
                    subprocess.run(shlex.split(cmd))

if __name__ == '__main__':
    # if len(sys.argv) != 4:
    #     print("Usage: %s D:\\sample\\xp d:\\xp.bin d:\\files.txt" % sys.argv[0])
    # else:
    #     db_path = sys.argv[3]
    #     read_file_db(db_path)
    #     process(sys.argv[1], sys.argv[2])
    #     save_file_db(db_path)
    db_path = r'D:\Work\Bin_Path\Xp.txt'
    read_file_db(db_path)
    process(r'D:\AAAA\sample\train2\xp', r'D:\Work\Bin_Location\Xp.bin')
    save_file_db(db_path)


import socket
import threading
import os
import time
from socket import *
import easygui
from Client_details import ClientD
from threading import Thread
from Massage import Massage
from server_gui import ServerGUI

class Server:
    def __init__(self):
        self.serverPort = 50001
        self.serverSocket = socket(AF_INET, SOCK_STREAM)
        self.serverSocket.bind(('', self.serverPort))
        self.serverSocket.listen(1000)
        self.udpPort = 50002
        self.udpSocket = socket(AF_INET, SOCK_DGRAM)
        self.udpSocket.bind(('', self.udpPort))
        self.udpPort2 = 50003
        self.udpSocket2 = socket(AF_INET, SOCK_DGRAM)
        self.udpSocket2.bind(('', self.udpPort2))
        print("the server is ready to receive")
        self.port_dict = {}
        self.name_dict = {}
        self.first_port = 55000
        self.ack_list = []
        self.serverSocket.settimeout(3)
        self.udpSocket.settimeout(0.2)
        self.file_list = ["1mb.txt", "project.pdf", "HelloWorld.html", "C++.xlsx"]

    # this function works as the listener to the clients
    def listen(self, socket, name, lock):
        while True:
            if name not in self.name_dict.keys():  # user is disconnected
                break
            lock.acquire()
            try:
                data = socket.recv(1024).decode()
            except Exception:
                lock.release()
                continue
            list1 = data.split()
            if list1[0] == "disconnect":
                self.disconnect(list1[1])
                break
            if list1[0] == "get_files":
                files = self.get_files()
                sock = self.name_dict[list1[1]].socket
                sock.send(bytes(str(files).encode()))
                lock.release()
                continue
            if list1[0] == "file":
                self.send_file(list1[1], name)
                lock.release()
                continue
            if list1[0] == "get_list":
                name_list = self.get_list()
                sock = self.name_dict[list1[1]].socket
                sock.send(bytes(str(name_list).encode()))
                lock.release()
                continue
            text = ""  # this is a message that needs to be sent
            for i in range(4, len(list1)):
                text = text + list1[i] + " "
            message = Massage(list1[1], text, list1[3])
            self.send_message(message)
            lock.release()

    def get_files(self):
        return self.file_list

    # this function return the list of all the users.
    def get_list(self) -> []:
        name_list = []
        for i in self.name_dict.keys():
            name_list.append(i)
        return name_list

    # this function get a message and sent it to the destination.
    def send_message(self, message):
        dest = message.dest
        if dest == "all":
            sentence = repr(message)
            for i in self.name_dict.values():  # send to everyone
                sock = i.socket
                sock.send(bytes(sentence.encode()))
            src = message.src
            if src != "server":  # let the sender know that his message was sent successfully
                sock = self.name_dict[src].socket
                sentence2 = "the message sent"
                sock.send(bytes(sentence2.encode()))
            return
        if dest not in self.name_dict.keys():
            src = message.src
            sock = self.name_dict[src].socket
            sentence = "this destination does not exist"
            sock.send(bytes(sentence.encode()))
            return
        sock = self.name_dict[dest].socket
        sentence = repr(message)
        sock.send(bytes(sentence.encode()))
        src = message.src
        if src != "server":
            sock = self.name_dict[src].socket
            sentence2 = "the message sent"  # let the sender know that his message was sent successfully
            sock.send(bytes(sentence2.encode()))

    def send_file(self, file, name):
        while True:
            try:
                connection, address = self.udpSocket.recvfrom(1024)  # receive client's socket details
            except Exception:
                continue
            if connection.decode() != "":
                break
        if file not in self.file_list:  # check if file exist
            self.udpSocket.sendto("this file not exist".encode(), address)
            return
        SEPARATOR = "<SEPARATOR>"
        sent = 0
        wnd_size = 1
        PACKET_SIZE = 63000
        file_size = os.path.getsize("files/" + file)
        packet_list = []
        end_wnd = 0
        with open("files/" + file, "rb") as f:
            for i in range(file_size):  # separate the file to packets
                outputdata = f.read(PACKET_SIZE)
                if not outputdata:
                    break
                packet_list.append(outputdata)
            ssthresh = len(packet_list)
            while sent < len(packet_list)/2:  # send 50% of the file
                threading.Thread(target=self.ack_listener, args=[wnd_size, sent]).start()  # follow the acks
                for j in range(wnd_size):
                    try:
                        self.udpSocket.sendto(packet_list[sent] + SEPARATOR.encode() + str(sent).encode(),
                                          address)
                    except Exception:
                        j -= 1
                        continue
                    sent += 1
                time.sleep(0.2)
                if -1 in self.ack_list:  # timeout
                    sent = self.ack_list.index(-1) + end_wnd
                    ssthresh = wnd_size
                    wnd_size = 1
                elif -2 in self.ack_list:  # wrong sequence
                    sent = self.ack_list.index(-2) + end_wnd
                    ssthresh = wnd_size
                    wnd_size = max(1, int(wnd_size/2))
                else:
                    end_wnd += wnd_size
                    if (wnd_size*2) < ssthresh:  # slow start
                        wnd_size *= 2
                    else:  # congestion avoidance
                        wnd_size += 1
            self.udpSocket2.sendto('sent 50%, you like to proceed?'.encode(),
                                  address)
            while True:
                try:
                    answer, address = self.udpSocket2.recvfrom(1024)
                    break
                except Exception:
                    continue
            answer2 = answer.decode()
            if answer2 != "yes":
                self.udpSocket.sendto("stop".encode(), address)
                return
            wnd_size = 1
            while sent < len(packet_list):  # send the rest of the file
                threading.Thread(target=self.ack_listener, args=[wnd_size, sent]).start()
                for j in range(wnd_size):
                    try:
                        self.udpSocket.sendto(packet_list[sent] + SEPARATOR.encode() + str(sent).encode(),
                                          address)
                    except Exception:
                        j -= 1
                        continue
                    sent += 1
                    if sent >= len(packet_list):
                        break
                time.sleep(0.2)
                if -1 in self.ack_list:
                    sent = self.ack_list.index(-1) + end_wnd
                    ssthresh = wnd_size
                    wnd_size = 1
                elif -2 in self.ack_list:
                    sent = self.ack_list.index(-2) + end_wnd
                    ssthresh = wnd_size
                    wnd_size = max(1, int(wnd_size/2))
                else:
                    end_wnd += wnd_size
                    if (wnd_size * 2) < ssthresh:
                        if (wnd_size*2 + sent) > len(packet_list):
                            wnd_size = len(packet_list) - sent
                        else:
                            wnd_size *= 2
                    else:
                        wnd_size += 1
            self.udpSocket.sendto("the file sent successfully".encode(),
                                  address)

    # listen to acks from the client
    def ack_listener(self, size, sent):
        self.ack_list = [0] * size
        acked = sent + 1
        for i in range(size):
            try:
                ack, address = self.udpSocket.recvfrom(1024)
                self.ack_list[i] = int(ack.decode())
                if self.ack_list[i] != acked:
                    self.ack_list[i] = -2
                    return
                acked += 1
            except Exception:  # timeout
                self.ack_list[i] = -1
                return

    # this function disconnect the client
    def disconnect(self, name):
        message1 = Massage("server", "you are disconnected", name)
        self.send_message(message1)
        port = self.name_dict[name].port
        self.name_dict[name].socket.close()
        self.name_dict.pop(name)
        self.port_dict[port] = None
        message = Massage("server", name + " disconnected")
        self.send_message(message)

def start(gui: ServerGUI):
    while True:
        if gui.button_start.is_pressed:
            server = Server()
            for i in range(16):
                server.port_dict[server.first_port + i] = None  # init all the ports
            while gui.button_start.is_pressed:
                freePort = 0
                for i in server.port_dict.keys():  # check if there is a free port
                    if server.port_dict[i] is None:
                        freePort = i
                        break
                if freePort == 0:
                    print("connection was not accepted")
                    continue
                try:
                    connectionSocket, addr = server.serverSocket.accept()
                except Exception:
                    continue
                name = connectionSocket.recv(1024).decode()
                if name in server.name_dict.keys():
                    sentence = "this name is taken, try again"
                    connectionSocket.send(bytes(sentence.encode()))
                    continue
                client = ClientD(connectionSocket, addr, name, freePort)
                server.port_dict[freePort] = client
                server.name_dict[name] = client
                lock = threading.Lock()
                # open thread that listens to this client
                thread = threading.Thread(target=server.listen, args=[connectionSocket, name, lock]).start()
                sentence = "connection received"
                connectionSocket.send(bytes(sentence.encode()))
                message = Massage("server", name + " is connected")
                server.send_message(message)
            for i in server.name_dict.keys():
                massage =Massage("server", "server is disconnected, please press \"log out\"", i)
                server.send_message(massage)
            time.sleep(3)
            server.serverSocket.close()
            print("the server is closed")
            exit(0)

if __name__ == '__main__':
    gui = ServerGUI()
    server_thread = threading.Thread(target=start, args=[gui]).start()
    gui.display()


# Author : Shivam Chauhan
# Date   : March 6, 2019

# This scripts takes search matter as input and open up the best
# matched links(on the first page usually) in new tabs in the browser
# automatically.

from bs4 import BeautifulSoup
import requests
import webbrowser

content = input("Enter the content to be searched : ") # Get the content
urlAddress = 'http://google.com/search?q=' + content  # Set up Url
try:
	res = requests.get(urlAddress) # Get request object
	res.raise_for_status() # Check the status
	webbrowser.open(urlAddress) # Open browser's windows
	soup = BeautifulSoup(res.text, 'html.parser')
	linkElements = soup.select('.r a')
	numOpen = min(5, len(linkElements))
	for i in range(numOpen): # Opening up of tabs
		webbrowser.open('http://google.com' + linkElements[i].get('href'))
except Exception as err:
	print("Oops!!, you entered a wrong URL " )


"""
Default settings for the ``mezzanine.blog`` app. Each of these can be
overridden in your project's settings module, just like regular
Django settings. The ``editable`` argument for each controls whether
the setting is editable via Django's admin.

Thought should be given to how a setting is actually used before
making it editable, as it may be inappropriate - for example settings
that are only read during startup shouldn't be editable, since changing
them would require an application reload.
"""
from django.conf import settings
from django.utils.translation import gettext_lazy as _

from mezzanine.conf import register_setting

register_setting(
    name="BLOG_USE_FEATURED_IMAGE",
    description=_("Enable featured images in blog posts"),
    editable=False,
    default=False,
)

_BLOG_URLS_DATE_FORMAT = ""
if getattr(settings, "BLOG_URLS_USE_DATE", False):
    _BLOG_URLS_DATE_FORMAT = "day"
    from warnings import warn

    warn(
        "BLOG_URLS_USE_DATE setting is deprecated, please use the "
        "BLOG_URLS_DATE_FORMAT setting with a value of 'year', 'month', "
        "or 'day'."
    )

register_setting(
    name="BLOG_URLS_DATE_FORMAT",
    label=_("Blog post URL date format"),
    description=_(
        "A string containing the value ``year``, ``month``, or "
        "``day``, which controls the granularity of the date portion in the "
        "URL for each blog post. Eg: ``year`` will define URLs in the format "
        "/blog/yyyy/slug/, while ``day`` will define URLs with the format "
        "/blog/yyyy/mm/dd/slug/. An empty string means the URLs will only "
        "use the slug, and not contain any portion of the date at all."
    ),
    editable=False,
    default=_BLOG_URLS_DATE_FORMAT,
)

register_setting(
    name="BLOG_POST_PER_PAGE",
    label=_("Blog posts per page"),
    description=_("Number of blog posts shown on a blog listing page."),
    editable=True,
    default=5,
)

register_setting(
    name="BLOG_RSS_LIMIT",
    label=_("Blog posts RSS limit"),
    description=_(
        "Number of most recent blog posts shown in the RSS feed. "
        "Set to ``None`` to display all blog posts in the RSS feed."
    ),
    editable=False,
    default=20,
)

register_setting(
    name="BLOG_SLUG",
    description=_("Slug of the page object for the blog."),
    editable=False,
    default="blog",
)


import asyncio
import html
import json
import os
import re
from time import perf_counter

import aiohttp
import requests
import sqlalchemy as db
from dotenv import load_dotenv

load_dotenv()

print("RUNING")

url: str = os.getenv('DATABASE_URL')

engine = db.create_engine(url)
meta_data = db.MetaData(engine)
db.MetaData.reflect(meta_data)
vapo = meta_data.tables['Vapo']
tag_plus = meta_data.tables['TagPlus']

get_creds = (
    db.select(tag_plus).where(tag_plus.c.id == 1)
)

access_creds = engine.execute(get_creds).fetchone()

access_token = access_creds[2]
refresh_token = access_creds[1]

def check_tokens():
    global access_token
    global refresh_token

    if requests.get(f"https://api.tagplus.com.br/produtos?access_token={access_token}").status_code in range(400, 499):
        res = requests.post('https://api.tagplus.com.br/oauth2/token', data={
            'grant_type': 'refresh_token',
            'refresh_token': refresh_token,
            'client_secret': os.getenv('CLIENT_SECRET'),
            'client_id': os.getenv('CLIENT_ID')
        })
        if res.status_code in range(200, 299):
            res = res.json()
            refresh_token = res['refresh_token']
            access_token = res['access_token']

            update_tokens = (
                db.update(tag_plus).where(tag_plus.c.id == 1).values(accessToken=access_token,
                                                                     refreshToken=refresh_token)
            )
            engine.execute(update_tokens)
        else:
            print('COULD NOT GET ACCESS TOKEN')
            exit()

check_tokens()

urlc = f'https://api.tagplus.com.br/categorias?access_token={access_token}'
urlp = f'https://api.tagplus.com.br/produtos?access_token={access_token}&categoria='

session = requests.session()
data = session.get(urlc).json()
parsedData = []

for item in data:
    if not item["categoria_mae"]:
        if item['id'] == 68:
            continue
        parsedData.append(
            {
                'id': item['id'],
                'name': item['descricao'],
                'slug': item['descricao'],
                'productCount': len(session.get(f'{urlp}{item["id"]}').json()),
                'children': [],
                "icon": '',
                'image': {
                    'thumbnail': '/assets/images/categories/' + item['descricao'].lower() + '.jpg',
                    'original': '/assets/images/categories/' + item['descricao'].lower() + '.jpg',
                }
            }
        )
        print(f"CATEGORY DONE - {item['id']}")

for item in data:
    if item['categoria_mae']:
        for cate in parsedData:
            if item['categoria_mae']['id'] == 68:
                continue
            if cate['id'] == item['categoria_mae']['id']:
                cate['children'].append(
                    {
                        "id": item['id'],
                        "name": item['descricao'],
                        "slug": item['descricao']
                    }
                )
                print(f"SUBCATEGORY DONE - {item['categoria_mae']['id']}")
                break

updateCategories = (
    db.update(vapo).where(vapo.c.id == 1).values(categories=json.dumps(parsedData))
)
engine.execute(updateCategories)

def format_description(text):
    text = html.unescape(text).replace(u'\xa0', u' ').replace('"', '').replace("N'", '').replace('\n', '').replace('\t',
                                                                                '')
    text = text.split(" ")
    text = ' '.join(text)
    return text

data = []
length = 2000

def check_category(cid):
    for c_item in parsedData:
        if len(c_item['children']) >= 1:
            for child in c_item['children']:
                if child['id'] == cid:
                    return {
                        "id": c_item['id'],
                        "name": c_item['name'],
                        "sub_category_id": child['id'],
                        "sub_category_name": child['name']
                    }
    for c_item in parsedData:
        if c_item['id'] == cid:
            return {
                "id": c_item['id'],
                "name": c_item['name'],
                "sub_category_id": None,
                "sub_category_name": None
            }

def adjust_price(price, sale):
    if sale == 0:
        return price
    return sale

async def fetch(s, url):
    async with s.get(f"https://api.tagplus.com.br/produtos/{url}?access_token={access_token}&fields="
                     f"nome,descricao,descricao_longa,codigo,estoque,categoria,valor_venda_varejo,valor_oferta,"
                     f"data_criacao,imagens,imagem_principal") as res:
        if res.status != 200:
            return
        parsed = await res.json()
        if not parsed:
            return
        if not parsed['estoque']['qtd_revenda'] or parsed['estoque']['qtd_revenda'] <= 0:
            return
        img = ''
        if 'imagem_principal' in parsed and parsed['imagem_principal'] is not None and 'url' in parsed[
            'imagem_principal']:
            img = parsed['imagem_principal']['url']
        gallery = parsed['imagens']
        if img in gallery:
            gallery.remove(img)
        product = {
            "id": parsed['id'],
            "code": parsed['codigo'],
            "name": parsed['descricao'],
            "description": format_description(re.sub("\(.*?\)|\<.*?\>", "", parsed['descricao_longa'])),
            "slug": parsed['descricao'],
            "quantity": parsed['estoque']['qtd_revenda'],
            "category": check_category(parsed['categoria']['id']),
            "price": parsed['valor_venda_varejo'],
            "sale_price": adjust_price(parsed['valor_venda_varejo'], parsed['valor_oferta']),
            "image": img,
            "gallery": gallery,
            "created_on": parsed['data_criacao']
        }
        print(f"PRODUCT DONE - {product['id']}")
        data.append(product)

async def fetch_all(s, urls):
    tasks = []
    for url in urls:
        task = asyncio.create_task(fetch(s, url))
        tasks.append(task)
    res = await asyncio.gather(*tasks)
    return res

async def main():
    urls = range(1, length)
    async with aiohttp.ClientSession() as session:
        await fetch_all(session, urls)
    update_products = (
        db.update(vapo).where(vapo.c.id == 1).values(products=json.dumps(data))
    )
    engine.execute(update_products)

if __name__ == '__main__':
    start = perf_counter()
    asyncio.run(main())
    stop = perf_counter()
    print("time taken:", stop - start)

print('ENDING')


#!/usr/bin/env python3
# coding: utf-8

import json
import nrn_validation as V

V.override_defaults_from_args()

# dendrite geometry: 200 µm long, diameter 1 µm to 0.4 µm.
geom = [(0, 1.0), (200, 0.4)]

model = V.VModel()
model.add_soma(12.6157)
model.add_dendrite("taper", geom)
model.add_iclamp(5, 80, 0.3, to="taper")

data = V.run_nrn_sim(100, report_dt=10, model="ball_and_taper")
print(json.dumps(data))
V.nrn_stop()


def average(array):
    # your code goes here
    arr = set(array)
    totl = sum(arr)
    noofval = len(arr)
    return float(totl/noofval)

if __name__ == '__main__':
    n = int(input())
    arr = list(map(int, input().split()))
    result = average(arr)
    print(result)

hackerrank_python_getting_started/Sets/Day38 -Introduction to Set.py


from datetime import datetime, timedelta
from Modules.FlightConstants import FlightConstants

class FlightFinder():
    # AIRLINE: Get airline(s) to fly from
    def get_airline_codes(airlines, airline_choices):
        if airlines: return airlines
        elif airline_choices: return FlightConstants.airlines[airline_choices]
        return FlightConstants.airlines[FlightConstants.ALL_AIRLINES]

    # AIRPORTS: Get all airports that match the first given parameter
    def get_airports(airports, coordinates, address, region, distance, default):
        if airports:
            desired_airports = [FlightConstants.get_airport_from_code(airport) for airport in airports]
        elif coordinates:
            latitude, longitude = coordinates
            desired_airports = FlightFinder.get_all_airports_within_circle(latitude, longitude, distance)
        elif address:
            from geopy.geocoders import Nominatim
            geo = Nominatim(user_agent="my_request").geocode(address)
            latitude, longitude = geo.latitude, geo.longitude

            desired_airports = FlightFinder.get_all_airports_within_circle(latitude, longitude, distance)
        elif region:
            desired_airports = FlightConstants.regions[region]
        else:
            desired_airports = default

        return desired_airports

    def get_airports_nearme(distance, debug=False):
        import geocoder
        latlng = geocoder.ip('me').latlng
        if latlng == None:
            if debug:
                print("You are not connected to the internet; defaulting the the coordinates for PHX airport")
                latlng = (33.433333, -112.033333)
            else:
                exit("You are not connected to the internet; please reconnect and try again")
        latitude, longitude = latlng

        return FlightFinder.get_all_airports_within_circle(latitude, longitude, distance)

    def get_airports_anywhere(distance):
        return FlightFinder.get_all_airports_within_circle(0, 0, float('inf'))

    # DATES: Get start and end date for desired flights
    def get_start_end_date(startdate, length):
        try:
            start = datetime.strptime(startdate, '%m-%d-%Y')
        except:
            start = datetime.strptime(startdate, '%m/%d/%Y')
        return start, start + timedelta(days=length)

    def get_all_airports_within_circle(latitude, longitude, radius):
        if radius == float('inf'): return FlightConstants.get_all_airports()

        origin = (latitude, longitude)
        from geopy.distance import geodesic

        dist_airports = [(geodesic(origin, a.coordinates).miles, a) for a in FlightConstants.get_all_airports()]
        airports_in_range = filter(lambda a: a[0] <= radius, dist_airports)
        airports_in_range_sorted = sorted(airports_in_range, key=lambda a: a[0])
        return [a for dist, a in airports_in_range_sorted]

    def get_all_airports_with_airlines(airlines):
        all_airports = FlightConstants.get_all_airports()
        airline_airports = set()
        for airline in airlines:
            for a1, a2 in FlightConstants.city_to_city_by_airline[airline]:
                airline_airports.add(a1)
                airline_airports.add(a2)

        return [airport for airport in all_airports if airport.airport_code in airline_airports]

    def flight_exists(airline, origin, destination):
        origin_to_destination = FlightConstants.city_to_city_by_airline[airline]
        for o1, d1 in origin_to_destination:
            if o1 == origin and d1 == destination: return True
        return False


import math
import re
from functools import reduce
from operator import mul

import utils

def parse_img(img_part: str):
    lines = img_part.splitlines()
    return (utils.ints(lines[0])[0], lines[1:])

all_pics = utils.get_list_data_from_file('day20.txt', parser=parse_img, sep='\n\n')

MONSTER= [
    '                  # ',
    '#    ##    ##    ###',
    ' #  #  #  #  #  #   '
]

monster_pattern1 = r'(?=(#....##....##....###))'
monster_pattern2 = r'(?=(.#..#..#..#..#..#))'

class Tile:
    def __init__(self, id, img, neighbors) -> None:
        self.id = id
        self.img = img
        self.neighbors = neighbors
    @property
    def left_border(self):
        return ''.join(item[0] for item in self.img)
    @property
    def right_border(self):
        return ''.join(item[-1] for item in self.img)
    @property
    def up_border(self):
        return self.img[0]
    @property
    def down_border(self):
        return self.img[-1]

    @property
    def borders(self):
        return [self.up_border, self.down_border, self.left_border, self.right_border]

    def is_neighbor(self, tile) -> bool:
        if tile.id == self.id:
            return False
        for border in self.borders:
            for border2 in tile.borders:
                if border == border2 or border == border2[::-1]:
                    return True
        return False

    def is_right_neighbor(self, tile) -> bool:
        if tile.id == self.id:
            return False
        if self.right_border == tile.left_border:
            return True

    def update_neighbors(self, all_pics):
        for pic in all_pics:
            tile = Tile(pic[0], pic[1], [])
            if self.is_neighbor(tile):
                self.neighbors.append(tile.id)
    @property
    def neighbors_count(self):
        return len(self.neighbors)

    def _rotate(self):
        tmp_list = []
        row_cnt = len(self.img)
        col_cnt = len(self.img[0])
        for i in range(col_cnt):
            temp_col = []
            for j in range(row_cnt):
                temp_col.append(self.img[j][i])
            tmp_list.append(''.join(temp_col[::-1]))
        return Tile(self.id, tmp_list, self.neighbors)

    def _flip(self):
        return Tile(self.id, self.img[::-1], self.neighbors)

    def all_form(self):
        tiles = []
        tile = self
        for i in range(4):
            tiles.append(tile)
            tile = tile._rotate()
        tile = self._flip()
        for i in range(4):
            tiles.append(tile)
            tile = tile._rotate()
        return tiles

    def show(self):
        print(self.id)
        for row in self.img:
            print(row)
        print(f'neighbors id: {self.neighbors}')

tiles_dict = {}
for pic in all_pics:
    tile = Tile(pic[0], pic[1], [])
    tile.update_neighbors(all_pics)
    tiles_dict[tile.id] = tile

def part1(all_tiles):
    angles_ids = [ tile.id for tile in all_tiles if tile.neighbors_count == 2]
    return reduce(mul, angles_ids, 1)

def get_first_row(tile, tiles_dict, n):
    row = [tile]
    for i in range(n):
        find = False
        tile = row[-1]
        for neighbor_id in tile.neighbors:
            n_tile = tiles_dict[neighbor_id]
            for ntf in n_tile.all_form():
                if tile.right_border == ntf.left_border:
                    row.append(ntf)
                    find = True
                    break
            if find:
                break
    return row

def get_next_row(row, tiles_dict):
    next_row = []
    for tile in row:
        find = False
        for neighbor_id in tile.neighbors:
            n_tile = tiles_dict[neighbor_id]
            for ntf in n_tile.all_form():
                if tile.down_border == ntf.up_border:
                    next_row.append(ntf)
                    find = True
                    break
            if find:
                break
    return next_row

def build_final_piles(first_pile, tiles_dict, n):
    for tile in first_pile.all_form():
        final_piles = []
        row = get_first_row(tile, tiles_dict, n-1)
        if len(row) < n:
            continue
        final_piles.append(row)
        for i in range(n-1):
            next_row = get_next_row(row, tiles_dict)
            if len(next_row) < n:
                break
            final_piles.append(next_row)
            row = next_row
        if len(final_piles) == n:
            return final_piles

def remove_borders(piles):
    whole_pic = []
    for row in piles:
        new_row = []
        for pile in row:
            pile_img = []
            for sub_img in pile.img[1:-1]:
                pile_img.append(sub_img[1:-1])
            new_row.append(pile_img)
        for item in zip(*new_row):
            whole_pic.append(''.join(item))
    return whole_pic

def get_whole_pic(tiles_dict, n):
    angles = [tile for tile  in tiles_dict.values() if tile.neighbors_count == 2]
    first_pile = angles[0]
    final_piles = build_final_piles(first_pile, tiles_dict, n)
    return remove_borders(final_piles)

def get_monster_cnt(image):
    cnt = 0
    for i in range(1, len(image)-1):
        second_finds = set(m.start(0) for m in re.finditer(monster_pattern1, image[i]))
        if len(second_finds) > 0:
            third_finds = set(m.start(0) for m in re.finditer(monster_pattern2, image[i+1]))
            both_math_set = second_finds & third_finds
            if len(both_math_set) > 0:
                for j in both_math_set:
                    if image[i-1][j+18] == '#':
                        cnt += 1
    return cnt

def part2(tiles_dict):
    n = int(math.sqrt(len(tiles_dict)))
    whole_pic = get_whole_pic(tiles_dict, n)
    whole_pic_tile = Tile(0, whole_pic, [])
    all_images = [tile.img for tile in whole_pic_tile.all_form()]
    sea_monsters_img = None
    sea_monsters_cnt = 0
    for image in all_images:
        sea_monsters_cnt = get_monster_cnt(image)
        if sea_monsters_cnt > 0:
            sea_monsters_img = image
            break
    monster_hash_cnt = sum(row.count('#') for row in MONSTER)
    image_hash_cnt = sum(row.count('#') for row in sea_monsters_img)
    return image_hash_cnt - monster_hash_cnt*sea_monsters_cnt

print(part1(tiles_dict.values()))
print(part2(tiles_dict))


from audioop import bias
from enum import auto
import sys
sys.path.append(".")
from agent.DeepScalper.model import Net
import numpy as np
import torch
from torch import nn
model_a = torch.load('/mnt/d/TradeMaster-1/result/AT/trained_model/best_model/best_model.pth').cpu()
model_a.eval()

torch.set_printoptions(threshold=np.inf)
# Display all model layer weights
# txt=""
# for name, para in model_a.named_parameters():
#     print('{}: {}'.format(name, para)+"\n")
#     print('{}: {}'.format(name, para.shape)+"\n")
#     txt+='{}: {}'.format(name, para)+"\n"
# f = open("/mnt/d/TradeMaster-1/result/AT/trained_model/parameter_printer/weights.txt",'a')
# f.write(txt)
# f.close()
paras=[]
names=[]
for name, para in model_a.named_parameters():
    para=para.reshape(-1)
    paras.append(para.detach().numpy().tolist())
    names.append(name)

# paras=torch.cat(paras,dim=1).reshape(-1)
# bias weights 放到一起 weights为偶数 bias为奇数

print(names)
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
# colors = [plt.cm.Spectral(i/float(len(paras)-1)) for i in range(len(paras))]
# n, bins, patches = plt.hist(paras, 10, stacked=True, density=False, color=colors[:len(paras)])
# plt.legend({group:col for group, col in zip(names, colors[:len(paras)])},loc='lower center',ncol=int(len(paras)/2),fontsize=8,bbox_to_anchor=(0.25, 1., 0.5, 0.5))
# plt.savefig("/mnt/d/TradeMaster-1/result/AT/trained_model/parameter_printer/single_layer_distribution.pdf")
from matplotlib.ticker import FuncFormatter

linear_weights=paras[0:len(paras):2]
linear_name=names[0:len(paras):2]
bias_weights=paras[1:len(paras):2]
bias_name=names[1:len(paras):2]
colors = [plt.cm.Spectral(i/float(len(linear_weights)-1)) for i in range(len(linear_weights))]
colors=["pink",plt.cm.Spectral(1/float(len(linear_weights)-1)),"#c7fdb5","#cea2fd"]
# n, bins, patches=plt.hist(linear_weights,10, stacked=True, density=False, color=colors[:len(linear_weights)],edgecolor='black',cumulative=False)

n, bins, patches=plt.hist(bias_weights,10, stacked=True, density=True, color=colors[:len(bias_weights)],edgecolor='black',cumulative=False)
print(bins[-1]-bins[0])
def pencetail(weights,bins):
    weights_sum=0
    for single_layer_weights in weights:
        for weight in single_layer_weights:
            weights_sum=weights_sum+1
    percentages=[]
    for i in range(len(bins)-1):
        percentage=0
        for single_layer_weights in weights:
            for weight in single_layer_weights:
                if (weight>=bins[i] and weight <bins[i+1]):
                    percentage=percentage+1
        percentage=percentage/weights_sum
        percentages.append(percentage)
    return percentages

percentages=pencetail(bias_weights,bins)
for i in range(10):
    plt.text(bins[i]+(bins[1]-bins[0])/2,percentages[i]*38+0.01, str(round(percentages[i]*100,2)), ha='center', va= 'bottom')
def to_percent(y,position):
    return str(round(y*(bins[-1]-bins[0])*10,2))+"%"#这里可以用round（）函数设置取几位小数
fomatter=FuncFormatter(to_percent)
plt.gca().yaxis.set_major_formatter(fomatter)
plt.legend({group:col for group, col in zip(bias_name, colors[:len(bias_name)])},loc='lower center',
ncol=int(len(paras)/2),fontsize=8,bbox_to_anchor=(0.25, 1., 0.5, 0.5))
plt.ylabel("Percentage")
plt.xlabel("Value of bias")
plt.savefig("/mnt/d/TradeMaster-1/result/AT/trained_model/parameter_printer/single_layer_bias_distribution.pdf")

# weights 图像
# n, bins, patches=plt.hist(linear_weights,10, stacked=True, density=True, color=colors[:len(linear_weights)],edgecolor='black',cumulative=False)
# def pencetail(weights,bins):
#     weights_sum=0
#     for single_layer_weights in weights:
#         for weight in single_layer_weights:
#             weights_sum=weights_sum+1
#     percentages=[]
#     for i in range(len(bins)-1):
#         percentage=0
#         for single_layer_weights in weights:
#             for weight in single_layer_weights:
#                 if (weight>=bins[i] and weight <bins[i+1]):
#                     percentage=percentage+1
#         percentage=percentage/weights_sum
#         percentages.append(percentage)
#     return percentages
# percentages=pencetail(linear_weights,bins)
# for i in range(10):
#     plt.text(bins[i]+(bins[1]-bins[0])/2,percentages[i]*10+0.1, round(percentages[i]*100,2), ha='center', va= 'bottom')
#     # plt.text(bins[i]+(bins[1]-bins[0])/2, n[i,:].sum()*1.01, int(n[i,:].sum()), ha='center', va= 'bottom')
# def to_percent(y,position):
#     return str(10*y)+"%"#这里可以用round（）函数设置取几位小数
# fomatter=FuncFormatter(to_percent)
# plt.gca().yaxis.set_major_formatter(fomatter)
# plt.legend({group:col for group, col in zip(linear_name, colors[:len(linear_name)])},loc='lower center',
# ncol=int(len(paras)/2),fontsize=8,bbox_to_anchor=(0.25, 1., 0.5, 0.5))
# plt.ylabel("Percentage")
# plt.xlabel("Value of weights")

# plt.savefig("/mnt/d/TradeMaster-1/result/AT/trained_model/parameter_printer/single_layer_weights_distribution.pdf")

# kde graph
# plt.figure(figsize=(16,10))
# sns.kdeplot(paras, shade=True, color="g", label="parameters", alpha=.7)
# plt.title('Density Plot of parameters in deepscalper', fontsize=22)
# plt.legend()
# plt.show()
# plt.savefig("/mnt/d/TradeMaster-1/result/AT/trained_model/parameter_printer/all_layer_distribution.pdf")


# 네이버 검색 API 예제 - 블로그 검색
import os
import sys
import urllib.request

#client_id = "YOUR_CLIENT_ID"
#client_secret = "YOUR_CLIENT_SECRET"
client_id = "0At29T7vkcGGyngOl5BY"
client_secret = "DJShkQKQXe"

encText = urllib.parse.quote("Chat GPT")  #  " "에 검색할 입력값
url = "https://openapi.naver.com/v1/search/news?query=" + encText # JSON 결과 뉴스검색
# url = "https://openapi.naver.com/v1/search/blog.xml?query=" + encText # XML 결과
request = urllib.request.Request(url)
request.add_header("X-Naver-Client-Id",client_id)
request.add_header("X-Naver-Client-Secret",client_secret)
response = urllib.request.urlopen(request)
rescode = response.getcode()
if(rescode==200):
    response_body = response.read()
    print(response_body.decode('utf-8'))
else:
    print("Error Code:" + rescode)

# 출처 : https://developers.naver.com/docs/serviceapi/search/blog/blog.md


#!/usr/bin/env python
# coding: utf-8

# In[1]:

import pandas as pd
import numpy as np
import librosa
from pathlib import Path
import soundfile as sf
from tqdm import tqdm
from pqdm.processes import pqdm

train = pd.read_csv('../input/train_metadata.csv')
train

def ogg2np(ebird, filename):
    input_path = Path('../input/')
    output_path = Path('../data/')
    input_path = input_path / 'train_short_audio' / ebird / filename
    filename = '.'.join(filename.split('.')[:-1])
    output_path = output_path
    record, sr = sf.read(input_path)
    record = librosa.to_mono(record)
    record = record.astype('float32')
    length = record.shape[0]
    period = int(np.ceil(5 * sr))
    if period == 0:
        filename = '%s_%d.npy' % (filename, 0)
        np.save(output_path / filename, record)
    else:
        for i in range(int(np.ceil(length/period))):
            filename_i = '%s_%d.npy' % (filename, i)
            record_i = record[i*period : (i+3)*period]
            np.save(output_path / filename_i, record_i)
    return length, sr

res = pqdm(zip(train.primary_label, train.filename), ogg2np, n_jobs=8, argument_type='args')

train['sr'] = [r[1] for r in res]
train['length'] = [r[0] for r in res]
train['duration'] = train['length'] / train['sr']

train['filename'] = [fname[:-4]+'.npy' for fname in train.filename]

train.to_csv('../input/train_001.csv', index=False)

train_ff1010 = pd.read_csv('../input/freefield1010/ff1010bird_metadata.csv')
train_ff1010

train_ff1010 = train_ff1010.sort_values(by='itemid').reset_index(drop=True)
train_ff1010

def get_clip(itemid):
    data_path = Path('../input/freefield1010/wav')
    path = data_path / ('%d.wav' % itemid)
    clip, sr_native = librosa.load(path, sr=None, mono=True, dtype=np.float32)
    sr = 32000
    if sr_native != 0:
        clip = librosa.resample(clip, sr_native, sr, res_type='kaiser_best')
    else:
        print('null sr_native')
    return clip, sr, sr_native

train_ff1010 = train_ff1010[train_ff1010.hasbird == 0].reset_index(drop=True)
train_ff1010

def work_sub(itemid):
    output_path = Path('../data/')
    clip, sr, sr_native = get_clip(itemid)
    clip = clip.astype('float32')
    length = clip.shape[0]
    filename = 'ff1010_%d_0.npy' % (itemid)
    np.save(output_path / filename, clip)

    return sr, sr_native, length

res = pqdm(train_ff1010.itemid, work_sub, n_jobs=8)

train_ff1010['primary_label'] = ''
train_ff1010['secondary_labels'] = None
train_ff1010['sr'] = [r[0] for r in res]
train_ff1010['sr_native'] = [r[1] for r in res]
train_ff1010['length'] = [r[2] for r in res]
train_ff1010['duration'] = train_ff1010['length'] / 32000
train_ff1010['filename'] = ['ff1010_%d_0.npy' % (itemid) for itemid in train_ff1010['itemid']]
train_ff1010

train_ff1010['secondary_labels'] = [[]] * len(train_ff1010)

columns = ['duration', 'length', 'primary_label', 'secondary_labels', 'filename']
train_ff1010[columns].to_csv('../input/train_ff1010.csv', index=False)


'''fonty.lib.telemetry'''
import sys
import json
import platform
import threading
from enum import Enum
from datetime import datetime
from typing import Tuple

import requests
from fonty.version import __version__
from fonty.lib.json_encoder import FontyJSONEncoder
from fonty.lib.constants import TELEMETRY_ENDPOINT, JSON_DUMP_OPTS
from fonty.lib.config import CommonConfiguration

class TelemetryEventTypes(Enum):
    '''An enum of possible telemetry event types.'''
    FONTY_SETUP = 'FONTY_SETUP'
    FONT_INSTALL = 'FONT_INSTALL'
    FONT_UNINSTALL = 'FONT_UNINSTALL'
    FONT_LIST = 'FONT_LIST'
    FONT_LIST_REBUILD = 'FONT_LIST_REBUILD'
    FONT_CONVERT = 'FONT_CONVERT'
    SOURCE_LIST = 'SOURCE_LIST'
    SOURCE_ADD = 'SOURCE_ADD'
    SOURCE_REMOVE = 'SOURCE_REMOVE'
    SOURCE_UPDATE = 'SOURCE_UPDATE'

class TelemetryEvent:
    '''The TelemetryEvent class describes a fonty telemetry event.

    By default, fonty sends some light usage data to better understand (1) how
    users use fonty and (2) also to identify interesting font usage statistics.
    It can however be disabled by turning off the `telemetry` setting in the
    fonty configuration file.
    '''

    #: The event type of this telemetry event.
    event_type: TelemetryEventTypes

    #: The timestamp of this telemetry event.
    timestamp: datetime

    #: The fonty version currently being used.
    fonty_version: str

    #: The operating system family of the current environment.
    os_family: str

    #: The operating system version of the current environment.
    os_version: str

    #: The current Python version.
    python_version: str

    #: The time it took (in seconds) to execute this event.
    execution_time: float

    #: The status code of the current command. 0 means success, >1 means error.
    status_code: int

    #: The additional data that is relevant to this telemetry event.
    data: dict

    def __init__(
        self,
        status_code: int,
        event_type: TelemetryEventTypes,
        execution_time: float = None,
        data: dict = None
    ) -> None:
        self.event_type = event_type
        self.timestamp = datetime.now()
        self.fonty_version = __version__
        self.python_version = '{major}.{minor}.{micro}'.format(
            major=sys.version_info.major,
            minor=sys.version_info.minor,
            micro=sys.version_info.micro
        )
        self.os_family, self.os_version = TelemetryEvent._get_os_info()
        self.execution_time = execution_time
        self.status_code = status_code
        self.data = data

    def send(self, force=False, asynchronous=True) -> None:
        '''Sends the telemetry data to the central logging server.'''
        if not CommonConfiguration.telemetry and not force:
            return

        # Create payload
        d = {
            'timestamp': self.timestamp,
            'status_code': self.status_code,
            'event_type': self.event_type.value,
            'execution_time': self.execution_time,
            'fonty_version': self.fonty_version,
            'os_family': self.os_family,
            'os_version': self.os_version,
            'python_version': self.python_version,
            'data': self.data
        }

        # Send request
        if asynchronous:
            threading.Thread(target=self._send_request, args=(d,)).start()
        else:
            self._send_request(d)

    def _send_request(self, d):
        try:
            requests.post(
                url=TELEMETRY_ENDPOINT,
                data=json.dumps(d, cls=FontyJSONEncoder, **JSON_DUMP_OPTS),
                headers={'Content-Type': 'application/json'}
            )
        except: # pylint: disable=W0702
            pass

    @staticmethod
    def _get_os_info() -> Tuple[str, str]:
        '''Gets the current operating system information and returns it as a
           tuple of (family, version).
        '''
        family = platform.system()
        version = ''

        if family.lower() == 'darwin':
            version = platform.mac_ver()[0]
        elif family.lower() == 'windows':
            win = sys.getwindowsversion() # pylint: disable=E1101
            version = '{}.{}-{}-{}'.format(win.major, win.minor, win.build, win.platform)
        elif family.lower() == 'linux':
            import distro
            dist = distro.linux_distribution(full_distribution_name=False)
            version = '{}-{}'.format(dist[0], dist[1])
        else:
            version = platform.platform()

        return family, version


from tensorflow.python.framework import ops
from tensorflow.python.ops import gen_rnn_ops

def _block_lstm_grad(op, *grads):
  """Gradient for the BlockLSTM op."""
  seq_len_max, x, cs_prev, h_prev, w, wci, wcf, wco, b = op.inputs
  i, cs, f, o, ci, co, h = op.outputs
  _, cs_grad, _, _, _, _, h_grad = grads
  (x_grad, cs_prev_grad, h_prev_grad, w_grad, wci_grad, wcf_grad, wco_grad,
   b_grad) = gen_rnn_ops.block_lstm_grad(
       seq_len_max=seq_len_max,
       x=x,
       cs_prev=cs_prev,
       h_prev=h_prev,
       w=w,
       wci=wci,
       wcf=wcf,
       wco=wco,
       b=b,
       i=i,
       cs=cs,
       f=f,
       o=o,
       ci=ci,
       co=co,
       h=h,
       cs_grad=cs_grad,
       h_grad=h_grad,
       use_peephole=op.get_attr("use_peephole"))
  return (None, x_grad, cs_prev_grad, h_prev_grad, w_grad, wci_grad, wcf_grad,
          wco_grad, b_grad)

ops.RegisterGradient("BlockLSTM")(_block_lstm_grad)
ops.RegisterGradient("BlockLSTMV2")(_block_lstm_grad)


from typing import List

class Solution:
    def contain_most_water(self, arr: List[int]):
        max_area = 0
        curr_area = 0
        i = 0
        j = len(arr) - 1
        while i < j:
            curr_area = min(arr[i], arr[j]) * (j - i)
            if curr_area > max_area:
                max_area = curr_area
            if arr[i] < arr[j]:
                i = i + 1
            else:
                j = j - 1
        print(max_area)

s = Solution()
s.contain_most_water([1,8,6,2,5,4,8,3,7])

# https://leetcode.com/problems/container-with-most-water/

Python Leetcode Algorithms/1. Array and String/5. contains_most_water.py


#!/usr/bin/env python

from nornir import InitNornir
from nornir.core.filter import F

def main():
    nr = InitNornir()
    nr = nr.filter(F(groups__contains="sea") | F(groups__contains="sfo"))
    print(nr.inventory.hosts)

if __name__ == "__main__":
    main()


import os
import platform
import sys
try:
	import termios
	import tty
except ImportError:
	import msvcrt

from .ansi_escapes import *
from .colorama import just_fix_windows_console

if platform.system() == "Windows":
	just_fix_windows_console()

class Shell():
	offset = 0
	line = 0

	def __init__(self, stdin = sys.stdin, stdout = sys.stdout):
		self.stdin = stdin
		self.stdout = stdout
		self.eof_when_enter = False
		self.interactables = []

	def read(self, count = 1):
		return self.stdin.read(count)

	def readline(self, count = 1):
		return self.stdin.readline(count)

	def input_raw(self, count = 1):
		try:
			fd = self.stdin.fileno()
			term_attrs = termios.tcgetattr(fd)
		except NameError:
			pass
		try:
			try:
				tty.setraw(fd)
				key = self.stdin.read(count)
			except NameError:
				key = msvcrt.getwch()
				count -= 1
				while count > 0:
					key += msvcrt.getwch()
					count -= 1
		finally:
			try:
				termios.tcsetattr(fd, termios.TCSADRAIN, term_attrs)
			except NameError:
				pass
		return key

	def input(self, count = 1):
		key = self.input_raw(count)
		if key == "\x03" or key == "\x1a": # Ctrl+C or Ctrl+Z
			raise KeyboardInterrupt()
		return key

	def inputline(self, count = 1):
		buffer = ""
		while count > 0:
			key = self.input()
			if ord(key) in {10, 13}: # Enter
				if count > 1:
					buffer += "\n"
				count -= 1
			else:
				buffer += key
		return buffer

	def write(self, value):
		self.stdout.write(value)
		value = str(value)
		self.line += value.count("\n")
		try:
			where = value.rindex("\n") + 1
			self.offset = len(value[where:])
		except ValueError:
			self.offset += len(value)

	def up(self, count):
		self.write(cursor_up(count))

	def down(self, count):
		self.write(cursor_down(count))

	def right(self, count):
		self.write(cursor_forward(count))

	def left(self, count):
		self.write(cursor_backward(count))

	def clear(self):
		if self.line > 0:
			self.stdout.write(erase_lines(self.line + 1))
			self.line = 0
		self.offset = 0

	def get_interactable(self, criteria):
		try:
			return self.interactables[criteria]
		except IndexError:
			pass
		except TypeError:
			pass
		for interactable in self.interactables:
			if interactable.key == criteria:
				return interactable
		raise ValueError(criteria)

	def observe(self, key):
		if self.eof_when_enter and ord(key) in {10, 13}: # Enter
			raise EOFError()
		return False

	def draw(self, interactable):
		return interactable.render(self, self.offset, self.line)

	def touch(self, interactable, key):
		return interactable.observe_key(key)

	def render(self):
		self.clear()
		self.write("\n")
		for interactable in self.interactables:
			self.draw(interactable)

	def enter(self):
		self.hide_cursor()
		self.render()

	def loop(self):
		self.enter()
		while True:
			try:
				key = self.input(1)
				observed = False
				for interactable in self.interactables:
					observed = self.touch(interactable, key) or observed
				if not observed:
					observed = self.observe(key)
				if observed:
					self.render()
			except EOFError:
				break
			except KeyboardInterrupt as err:
				self.leave()
				raise err
		self.leave()

	def leave(self):
		self.show_cursor()

	def hide_cursor(self):
		self.write(CURSOR_HIDE)

	def show_cursor(self):
		self.write(CURSOR_SHOW)

	class Interactable():
		def __init__(self, key):
			self.key = key

		def observe_key(self, what):
			return False

		def render(self, shell, offset, line):
			pass

		def lines(self, shell):
			return 0

class InteractiveShell(Shell):
	page_buffer_offset = 0
	global_buffer_offset = 0
	page = 1

	def __init__(self, stdin = sys.stdin, stdout = sys.stdout, infinite_scroll = False, lines_per_page = 6, implicit_page_indicator = False):
		Shell.__init__(self, stdin, stdout)
		self.infinite_scroll = infinite_scroll
		self.lines_per_page = lines_per_page
		self.implicit_page_indicator = implicit_page_indicator

	def observe(self, raw):
		observed = Shell.observe(self, raw)
		if raw != "\x1b" and raw != "\xe0" and raw != "\x00":
			return observed
		key = self.input_raw(1)
		if raw == "\xe0" or raw == "\x00": # Windows
			if key == "M": # Right
				self.turn_forward()
			elif key == "K": # Left
				self.turn_backward()
			else:
				return observed
			return True
		if key != "[": # Unix
			return observed
		joy = self.input_raw(1)
		if joy == "C": # Right
			self.turn_forward()
		elif joy == "D": # Left
			self.turn_backward()
		else:
			return observed
		return True

	def turn_forward(self):
		if self.global_buffer_offset + self.page_buffer_offset >= len(self.interactables):
			if self.infinite_scroll:
				self.global_buffer_offset = self.page_buffer_offset = 0
				self.page = 1
			return

		self.global_buffer_offset += self.page_buffer_offset
		self.page_buffer_offset = 0
		self.page += 1

	def turn_backward(self):
		index = self.global_buffer_offset
		if index == 0:
			if self.infinite_scroll:
				page = 1
				page_occupied_lines = 0
				page_buffer_offset = 0

				while index < len(self.interactables):
					lines = self.interactables[index].lines(self)
					if lines > self.lines_per_page or lines < 0:
						raise BufferError()

					if page_occupied_lines + lines > self.lines_per_page:
						page_occupied_lines = page_buffer_offset = 0
						page += 1
						continue

					page_occupied_lines += lines
					page_buffer_offset += 1
					index += 1

				self.global_buffer_offset = index - page_buffer_offset
				self.page_buffer_offset = 0
				self.page = page
			return

		page_occupied_lines = 0
		while index > 0:
			lines = self.interactables[index - 1].lines(self)
			if lines > self.lines_per_page or lines < 0:
				raise BufferError()

			if page_occupied_lines + lines > self.lines_per_page:
				break

			page_occupied_lines += lines
			index -= 1

		self.global_buffer_offset = index
		self.page_buffer_offset = 0
		self.page -= 1

	def draw(self, interactable, page, page_occupied_lines):
		if isinstance(interactable, InteractiveShell.Interactable):
			return interactable.render(self, self.offset, self.line, page, self.page_buffer_offset, page_occupied_lines)
		return Shell.draw(self, interactable)

	def write_implicit_indicator(self):
		if self.implicit_page_indicator and (self.global_buffer_offset > 0 or self.global_buffer_offset + self.page_buffer_offset < len(self.interactables)):
			self.write(
				"\n" * (self.lines_per_page + 1 - self.line) +
				(".." if self.global_buffer_offset > 0 else " " * 2)
				+ " " * 45 +
				(".." if self.global_buffer_offset + self.page_buffer_offset < len(self.interactables) else " " * 2)
				+ "\n"
			)

	def render(self):
		self.clear()
		if len(self.interactables) == 0:
			return
		if self.global_buffer_offset >= len(self.interactables):
			raise IndexError()
		self.write("\n")

		page_occupied_lines = 0
		self.page_buffer_offset = 0

		while self.global_buffer_offset + self.page_buffer_offset < len(self.interactables):
			interactable = self.interactables[self.global_buffer_offset + self.page_buffer_offset]
			lines = interactable.lines(self)
			if lines > self.lines_per_page or lines < 0:
				raise BufferError()
			if page_occupied_lines + lines > self.lines_per_page:
				break
			self.draw(interactable, self.page, page_occupied_lines)
			page_occupied_lines += lines
			self.page_buffer_offset += 1

		self.write_implicit_indicator()

	def enter(self):
		self.global_buffer_offset = self.page_buffer_offset = 0
		self.page = 1
		Shell.enter(self)

	def leave(self):
		self.clear()
		Shell.leave(self)

	class Interactable(Shell.Interactable):
		def __init__(self, key):
			Shell.Interactable.__init__(self, key)

		def render(self, shell, offset, line, page = 0, index = -1, lines_before = -1):
			pass

class SelectiveShell(InteractiveShell):
	page_cursor_offset = -1
	pending_hover_offset = 0
	blocked_in_page = False

	def __init__(self, stdin = sys.stdin, stdout = sys.stdout, infinite_scroll = False, lines_per_page = 6, implicit_page_indicator = False):
		InteractiveShell.__init__(self, stdin, stdout, infinite_scroll, lines_per_page, implicit_page_indicator)
		self.eof_when_enter = True

	def turn_backward(self):
		if not self.blocked_in_page:
			InteractiveShell.turn_backward(self)
		self.page_cursor_offset += 1
		self.pending_hover_offset = -2

	def turn_forward(self):
		if not self.blocked_in_page:
			InteractiveShell.turn_forward(self)
		self.page_cursor_offset -= 1
		self.pending_hover_offset = 2

	def turn_up(self):
		if self.page_cursor_offset > 0 or self.infinite_scroll or self.global_buffer_offset > 0:
			self.pending_hover_offset = -1 if not self.blocked_in_page else -2

	def turn_down(self):
		if (self.page_cursor_offset < self.page_buffer_offset and self.which() < len(self.interactables) - 1) or self.infinite_scroll:
			self.pending_hover_offset = 1 if not self.blocked_in_page else 2

	def hover_previous(self):
		cursor_offset = min(self.page_cursor_offset, self.page_buffer_offset) - 1
		while cursor_offset >= 0:
			try:
				interactable = self.interactables[self.global_buffer_offset + cursor_offset]
			except IndexError:
				cursor_offset -= 1
				continue
			if isinstance(interactable, SelectiveShell.Selectable) and interactable.hoverable():
				self.page_cursor_offset = cursor_offset
				return True
			cursor_offset -= 1
		return False

	def hover_next(self):
		cursor_offset = max(self.page_cursor_offset, -1) + 1
		while cursor_offset < self.page_buffer_offset:
			try:
				interactable = self.interactables[self.global_buffer_offset + cursor_offset]
			except IndexError:
				cursor_offset += 1
				continue
			if isinstance(interactable, SelectiveShell.Selectable) and interactable.hoverable():
				self.page_cursor_offset = cursor_offset
				return True
			cursor_offset += 1
		return False

	def touch(self, interactable, key):
		if isinstance(interactable, SelectiveShell.Selectable):
			try:
				return interactable.observe_key(key, self.interactables.index(interactable) == self.which())
			except ValueError:
				pass
		return InteractiveShell.touch(self, interactable, key)

	def render(self):
		InteractiveShell.render(self)

		# Changes cursor location to previous one if possible
		if self.pending_hover_offset != 0:
			if self.pending_hover_offset <= -1:
				if not self.hover_previous():

					if self.pending_hover_offset == -1:
						if self.infinite_scroll or self.global_buffer_offset > 0:
							self.page_cursor_offset = self.lines_per_page
							InteractiveShell.turn_backward(self)
							InteractiveShell.render(self)
							if not self.hover_previous():
								self.page_cursor_offset = -1

					elif not self.hover_next() and not self.hovered():
						self.page_cursor_offset = -1

				InteractiveShell.render(self)

			elif self.pending_hover_offset >= 1:
				if not self.hover_next():

					if self.pending_hover_offset == 1:
						self.page_cursor_offset = -1
						InteractiveShell.turn_forward(self)
						InteractiveShell.render(self)
						if not self.hover_next():
							self.page_cursor_offset = -1

					elif not self.hover_previous() and not self.hovered():
						self.page_cursor_offset = -1

				InteractiveShell.render(self)

			self.pending_hover_offset = 0

	def observe(self, raw):
		if self.eof_when_enter and ord(raw) in {10, 13}: # Enter
			if self.which() == -1:
				return False
			raise EOFError()
		observed = Shell.observe(self, raw)
		if raw != "\x1b" and raw != "\xe0" and raw != "\x00":
			return observed
		key = self.input_raw(1)
		if raw == "\xe0" or raw == "\x00": # Windows
			if key == "H": # Up
				self.turn_up()
			elif key == "P": # Down
				self.turn_down()
			elif key == "M": # Right
				self.turn_forward()
			elif key == "K": # Left
				self.turn_backward()
			else:
				return observed
			return True
		if key != "[":
			return observed
		joy = self.input_raw(1) # Unix
		if joy == "A": # Up
			self.turn_up()
		elif joy == "B": # Down
			self.turn_down()
		elif joy == "C": # Right
			self.turn_forward()
		elif joy == "D": # Left
			self.turn_backward()
		else:
			return observed
		return True

	def draw(self, interactable, page, page_occupied_lines):
		if isinstance(interactable, SelectiveShell.Selectable):
			return interactable.render(self, self.offset, self.line, page, self.page_buffer_offset, page_occupied_lines, self.page_cursor_offset == self.page_buffer_offset)
		return InteractiveShell.draw(self, interactable, page, page_occupied_lines)

	def enter(self):
		self.page_cursor_offset = -1
		self.pending_hover_offset = 2
		InteractiveShell.enter(self)

	def hovered(self):
		interactable = self.get_interactable(self.which())
		return interactable.hoverable() if isinstance(interactable, SelectiveShell.Selectable) else False

	def which(self):
		return self.global_buffer_offset + self.page_cursor_offset if self.page_cursor_offset != -1 else -1

	def what(self):
		try:
			return self.interactables[self.which()].key
		except IndexError:
			return None

	class Selectable(InteractiveShell.Interactable):
		def __init__(self, key):
			InteractiveShell.Interactable.__init__(self, key)

		def render(self, shell, offset, line, page = 0, index = -1, lines_before = -1, at_cursor = None):
			pass

		def hoverable(self):
			return True

		def placeholder(self):
			pass

		def observe_key(self, what, at_cursor = None):
			return False

class Separator(Shell.Interactable):
	def __init__(self, key = "separator", size = 1):
		Shell.Interactable.__init__(self, key)
		self.size = size

	def render(self, shell, offset, line):
		shell.write("\n" * self.size)

	def lines(self, shell):
		return self.size

class Notice(Shell.Interactable):
	def __init__(self, key, text = None):
		Shell.Interactable.__init__(self, key)
		self.text = text if text is not None else key

	def render(self, shell, offset, line):
		shell.write(str(self.text) + "\n")

	def lines(self, shell):
		return str(self.text).count("\n") + 1

class Entry(SelectiveShell.Selectable):
	def __init__(self, key, text = None, arrow = "> "):
		SelectiveShell.Selectable.__init__(self, key)
		self.text = text if text is not None else key
		self.arrow = arrow

	def get_arrow(self, at_cursor = None):
		return "" if at_cursor is None else \
			str(self.arrow) if at_cursor else " " * len(self.arrow)

	def render(self, shell, offset, line, page = 0, index = -1, lines_before = -1, at_cursor = None):
		shell.write(self.get_arrow(at_cursor) + str(self.text) + "\n")

	def placeholder(self):
		return str(self.text).partition("\n")[0]

	def lines(self, shell):
		return str(self.text).count("\n") + 1

class Switch(Entry):
	def __init__(self, key, text = None, checked = False, arrow = "> ", checked_arrow = "* ", hover_arrow = ">*"):
		Entry.__init__(self, key, text, arrow)
		self.checked_arrow = checked_arrow
		self.checked = checked
		self.hover_arrow = hover_arrow

	def get_arrow(self, at_cursor = None):
		return str(self.hover_arrow) if at_cursor and self.checked else \
			str(self.arrow) if at_cursor else str(self.checked_arrow) if self.checked else " " * len(self.arrow)

	def observe_key(self, what, at_cursor = None):
		if at_cursor and ord(what) in {10, 13}:
			self.checked = not self.checked
			return True
		return Entry.observe_key(self, what)

class Input(Entry):
	def __init__(self, key, hint = None, text = "", arrow = "> ", template = None, maximum_length = 40):
		Entry.__init__(self, key, text, arrow)
		self.hovered = False
		self.hint = hint
		self.template = template
		self.maximum_length = maximum_length

	def render(self, shell, offset, line, page = 0, index = -1, lines_before = -1, at_cursor = None):
		shell.write(self.get_arrow(at_cursor) + (str(self.hint) if self.hint is not None else "") +
			("" if self.hovered else "\x1b[2m") + (self.text if len(self.text) > 0 or self.hovered else \
				"..." if self.template is None else self.template) + ("\x1b[7m " if self.hovered else "") + "\x1b[0m\n")

	def read(self):
		return self.template if not self.hovered and len(self.text) == 0 and self.template is not None else self.text

	def observe_key(self, what, at_cursor = None):
		if at_cursor:
			if ord(what) in {10, 13}:
				self.hovered = not self.hovered
				return True
			if self.hovered:
				if what in ("\x7f", "\x08"):
					if len(self.text) > 0:
						self.text = self.text[::-1][1:][::-1]
					else:
						self.hovered = False
				elif what.isprintable() and len(self.text + what) <= self.maximum_length:
					self.text += what
				return True
		return Entry.observe_key(self, what)

class Progress(Shell.Interactable):
	def __init__(self, key = "progress", progress = 0, weight = 49, text = None):
		Shell.Interactable.__init__(self, key)
		self.progress = progress
		self.weight = weight
		self.text = text

	def render(self, shell, offset, line):
		text = (str(self.text) if self.text is not None else str(int(self.progress * 100)) + "%").center(self.weight)
		size = int(self.weight * self.progress)
		shell.write("\x1b[7m" + text[:size] + "\x1b[2m" + text[size:self.weight] + "\x1b[0m\n")

	def seek(self, progress, text = None):
		self.progress = progress
		if text is not None:
			self.text = text

	def lines(self, shell):
		return (str(self.text).count("\n") if self.text is not None else 0) + 1

class Interrupt(InteractiveShell.Interactable):
	def __init__(self, key = "interrupt", occupied_page = True):
		InteractiveShell.Interactable.__init__(self, key)
		self.ocuppied_page = occupied_page

	def render(self, shell, offset, line, page = 0, index = -1, lines_before = -1):
		raise EOFError()

	def lines(self, shell):
		return shell.lines_per_page if self.ocuppied_page else 0

class Debugger(SelectiveShell.Selectable):
	def __init__(self, key = "debugger"):
		SelectiveShell.Selectable.__init__(self, key)

	def render(self, shell, offset, line, page = 0, index = -1, lines_before = -1, at_cursor = None):
		shell.write(f"Page {page}:{shell.global_buffer_offset}, offset {offset}/{shell.page_buffer_offset}, cursor {shell.page_cursor_offset}\n")

	def hoverable(self):
		return False

def select_prompt(prompt = None, *variants, fallback = None, what_not_which = False):
	if prompt is not None:
		print(prompt, end="")
	shell = SelectiveShell(infinite_scroll=True, implicit_page_indicator=True)
	for variant in variants:
		shell.interactables.append(Entry(variant))
	try:
		shell.loop()
		result = shell.which()
	except KeyboardInterrupt:
		result = fallback
	try:
		interactable = shell.get_interactable(result)
	except ValueError:
		return None
	try:
		print((prompt + " " if prompt is not None else "") + "\x1b[2m" + interactable.placeholder() + "\x1b[0m")
	except ValueError:
		pass
	return result if not what_not_which else interactable.key if interactable is not None else None

if __name__ == "__main__":
	shell = Shell()
	while True:
		try:
			key = shell.input(1)
		except KeyboardInterrupt:
			break
		print(ord(key), ": ", str(key.encode("unicode-escape"))[2:][::-1][1:][::-1].replace("\\\\", "\\"), sep="")


import pandas as pd
import numpy as np

def evaluating_change_point(true, prediction, metric='nab', numenta_time=None):
    """
    true - both:
                list of pandas Series with binary int labels
                pandas Series with binary int labels
    prediction - both:
                      list of pandas Series with binary int labels
                      pandas Series with binary int labels
    metric: 'nab', 'binary' (FAR, MAR), 'average_delay'

    """

    def binary(true, prediction):
        """
        true - true binary series with 1 as anomalies
        prediction - trupredicted binary series with 1 as anomalies
        """
        def single_binary(true,prediction):
            true_ = true == 1
            prediction_ = prediction == 1
            TP = (true_ & prediction_).sum()
            TN = (~true_ & ~prediction_).sum()
            FP = (~true_ & prediction_).sum()
            FN = (true_ & ~prediction_).sum()
            return TP,TN,FP,FN

        if type(true) != type(list()):
            TP,TN,FP,FN = single_binary(true,prediction)
        else:
            TP,TN,FP,FN = 0,0,0,0
            for i in range(len(true)):
                TP_,TN_,FP_,FN_ = single_binary(true[i],prediction[i])
                TP,TN,FP,FN = TP+TP_,TN+TN_,FP+FP_,FN+FN_

        f1 = round(TP/(TP+(FN+FP)/2), 2)
        print(f'False Alarm Rate {round(FP/(FP+TN)*100,2)} %' )
        print(f'Missing Alarm Rate {round(FN/(FN+TP)*100,2)} %')
        print(f'F1 metric {f1}')
        return f1

    def average_delay(detecting_boundaries, prediction):

        def single_average_delay(detecting_boundaries, prediction):
            missing = 0
            detectHistory = []
            for couple in detecting_boundaries:
                t1 = couple[0]
                t2 = couple[1]
                if prediction[t1:t2].sum()==0:
                    missing+=1
                else:
                    detectHistory.append(prediction[prediction ==1][t1:t2].index[0]-t1)
            return missing, detectHistory

        if type(prediction) != type(list()):
            missing, detectHistory = single_average_delay(detecting_boundaries, prediction)
        else:
            missing, detectHistory = 0, []
            for i in range(len(prediction)):
                missing_, detectHistory_ = single_average_delay(detecting_boundaries[i], prediction[i])
                missing, detectHistory = missing+missing_, detectHistory+detectHistory_

        add = pd.Series(detectHistory).mean()
        print('Average delay', add)
        print(f'A number of missed CPs = {missing}')
        return add

    def evaluate_nab(detecting_boundaries, prediction, table_of_coef=None):
        """
        Scoring labeled time series by means of
        Numenta Anomaly Benchmark methodics

        Parameters
        ----------
        detecting_boundaries: list of list of two float values
            The list of lists of left and right boundary indices
            for scoring results of labeling
        prediction: pd.Series with timestamp indices, in which 1
            is change point, and 0 in other case.
        table_of_coef: pandas array (3x4) of float values
            Table of coefficients for NAB score function
            indeces: 'Standart','LowFP','LowFN'
            columns:'A_tp','A_fp','A_tn','A_fn'

        Returns
        -------
        Scores: numpy array, shape of 3, float
            Score for 'Standart','LowFP','LowFN' profile
        Scores_null: numpy array, shape 3, float
            Null score for 'Standart','LowFP','LowFN' profile
        Scores_perfect: numpy array, shape 3, float
            Perfect Score for 'Standart','LowFP','LowFN' profile
        """
        def single_evaluate_nab(detecting_boundaries, prediction, table_of_coef=None, name_of_dataset=None):
            if table_of_coef is None:
                table_of_coef = pd.DataFrame([[1.0,-0.11,1.0,-1.0],
                                     [1.0,-0.22,1.0,-1.0],
                                      [1.0,-0.11,1.0,-2.0]])
                table_of_coef.index = ['Standart','LowFP','LowFN']
                table_of_coef.index.name = "Metric"
                table_of_coef.columns = ['A_tp','A_fp','A_tn','A_fn']

            alist = detecting_boundaries.copy()
            prediction = prediction.copy()

            Scores, Scores_perfect, Scores_null=[], [], []
            for profile in ['Standart', 'LowFP', 'LowFN']:
                A_tp = table_of_coef['A_tp'][profile]
                A_fp = table_of_coef['A_fp'][profile]
                A_fn = table_of_coef['A_fn'][profile]
                def sigm_scale(y, A_tp, A_fp, window=1):
                    return (A_tp-A_fp)*(1/(1+np.exp(5*y/window))) + A_fp

                #First part
                score = 0
                if len(alist)>0:
                    score += prediction[:alist[0][0]].sum()*A_fp
                else:
                    score += prediction.sum()*A_fp
                #second part
                for i in range(len(alist)):
                    if i<=len(alist)-2:
                        win_space = prediction[alist[i][0]:alist[i+1][0]].copy()
                    else:
                        win_space = prediction[alist[i][0]:].copy()
                    win_fault = prediction[alist[i][0]:alist[i][1]]
                    slow_width = int(len(win_fault)/4)

                    if len(win_fault) + slow_width >= len(win_space):
                        print(f'Intersection of the windows of too wide widths for dataset {name_of_dataset}')
                        win_fault_slow = win_fault.copy()
                    else:
                        win_fault_slow= win_space[:len(win_fault)  +  slow_width]

                    win_fp = win_space[-len(win_fault_slow):]

                    if win_fault_slow.sum() == 0:
                        score+=A_fn
                    else:
                        #to get the first index
                        tr = pd.Series(win_fault_slow.values,index = range(-len(win_fault), len(win_fault_slow)-len(win_fault)))
                        tr_values= tr[tr==1].index[0]
                        tr_score = sigm_scale(tr_values, A_tp,A_fp,slow_width)
                        score += tr_score
                        score += win_fp.sum()*A_fp
                Scores.append(score)
                Scores_perfect.append(len(alist)*A_tp)
                Scores_null.append(len(alist)*A_fn)
            return np.array([np.array(Scores),np.array(Scores_null), np.array(Scores_perfect)])
       #======
        if type(prediction) != type(list()):
            matrix = single_evaluate_nab(detecting_boundaries, prediction, table_of_coef=table_of_coef)
        else:
            matrix = np.zeros((3,3))
            for i in range(len(prediction)):
                matrix_ = single_evaluate_nab(detecting_boundaries[i], prediction[i], table_of_coef=table_of_coef,name_of_dataset=i)
                matrix = matrix + matrix_

        results = {}
        desc = ['Standart', 'LowFP', 'LowFN']
        for t, profile_name in enumerate(desc):
            results[profile_name] = round(100*(matrix[0,t]-matrix[1,t])/(matrix[2,t]-matrix[1,t]), 2)
            print(profile_name,' - ', results[profile_name])

        return results

    #=========================================================================
    if type(true) != type(list()):
        true_items = true[true==1].index
    else:
        true_items = [true[i][true[i]==1].index for i in range(len(true))]

    if not metric=='binary':
        def single_detecting_boundaries(true, numenta_time, true_items):
            detecting_boundaries=[]
            td = pd.Timedelta(numenta_time) if numenta_time is not None else pd.Timedelta((true.index[-1]-true.index[0])/len(true_items))
            for val in true_items:
                detecting_boundaries.append([val, val + td])
            return detecting_boundaries

        if type(true) != type(list()):
            detecting_boundaries = single_detecting_boundaries(true=true, numenta_time=numenta_time, true_items=true_items)
        else:
            detecting_boundaries=[]
            for i in range(len(true)):
                detecting_boundaries.append(single_detecting_boundaries(true=true[i], numenta_time=numenta_time, true_items=true_items[i]))

    if metric== 'nab':
        return evaluate_nab(detecting_boundaries, prediction)
    elif metric=='average_delay':
        return average_delay(detecting_boundaries, prediction)
    elif metric== 'binary':
        return binary(true, prediction)


# gets credit card number
cc_num = int(input("Number: "))
cc_num_copy = cc_num
sum = 0
# print(cc_num)
num_digits = 0

while (cc_num > 0):
    # counts by two or one since ccnum is divided by 10 twice below
    if (cc_num > 9):
        num_digits = num_digits + 2
    else:
        num_digits = num_digits + 1

    sum = sum + (cc_num % 10)
    cc_num = cc_num // 10
    dummy = (cc_num % 10) * 2
    if (dummy < 10):  # if dummy is double digits, we need to split it into single digits
        sum = sum + dummy
    else:
     # splits into single digits
        sum = sum + (dummy % 10)
        dummy = dummy // 10
        sum = sum + dummy

    if (cc_num > 100 and cc_num < 1000):
        first_two = cc_num // 10  # /* first two digits of cc_num */
    elif (cc_num > 9):
        first_two = cc_num
    cc_num = cc_num // 10
    # print(cc_num)

# automatically determines if invalid based on sum
if (sum % 10 != 0):
    print("INVALID")

# first two is first two digits; numdigits is number of digits
elif (first_two >= 51 and first_two <= 55 and num_digits == 16):
    print("MASTERCARD")
elif ((first_two == 34 or first_two == 37) and num_digits == 15):
    print("AMEX")
elif (first_two >= 40 and first_two <= 49 and (num_digits == 13 or num_digits == 16)):
    print("VISA")
else:
    print("INVALID")


import os

import numpy as np
import pandas as pd

print(os.getcwd())
bc_data = pd.read_csv('bc_data.csv', header=0)  # 数据集中没有列名信息，所以header设置为0
bc_data.head()
print(bc_data.shape)  # 查看形状
print(bc_data.columns)  # 查看列名
print(bc_data.describe())  # 查看描述性统计信息
data = bc_data.drop(['id'], axis=1)
print(data.head())
x_data = data.drop(['diagnosis'], axis=1)
x_data.head()
y_data = np.ravel(data[['diagnosis']])  # np.ravel()
y_data[0:6]
from sklearn.model_selection import train_test_split

x_trainingSet, x_testSet = train_test_split(x_data)
y_trainingSet, y_testSet = train_test_split(y_data)
print(x_trainingSet.shape)
print(x_testSet.shape)
from sklearn.neighbors import KNeighborsClassifier

myModel = KNeighborsClassifier(algorithm='kd_tree')
myModel.fit(x_trainingSet, y_trainingSet)
y_predictSet = myModel.predict(x_testSet)
print(y_predictSet)
print(y_testSet)
from sklearn.metrics import accuracy_score

print(accuracy_score(y_testSet, y_predictSet))