import os.path
import sys
sys.path.append('.')
from data.base_dataset import BaseDataset, get_transform
from data.image_folder import make_dataset
import random
import numpy as np
from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
from util import custom_transforms
import torchvision.transforms as transforms
import pdb
import torch
import cv2
import json
import copy

from util.smart_crop_transforms import RandomCropDramaticlly
from util import augment

class RetrievalWorkshopBaselineDataset(BaseDataset):
    def __init__(self, opt):
        BaseDataset.__init__(self, opt)

        self.sketch_file = []
        self.sketch_label = []

        self.sketch_path = os.path.join(opt.dataroot, 'sketch')
        self.render_path = os.path.join(opt.dataroot, 'render5')
        self.label_model = json.load(open(os.path.join(opt.dataroot, 'data_info','label_model.json'),'r'))
        self.level_model = json.load(open(os.path.join(opt.dataroot, 'data_info','level_model.json'),'r'))

        cls = sorted(os.listdir(self.sketch_path))
        for k,c in enumerate(cls):
            tr = os.path.join(self.sketch_path, c, 'train')
            fs = [os.path.join(tr,i) for i in os.listdir(tr)]
            self.sketch_file.append(np.array(fs))
            self.sketch_label.append(np.array([k for _ in range(len(fs))]))

        self.sketch_file = np.array(self.sketch_file).reshape(-1)
        self.sketch_label = np.array(self.sketch_label).reshape(-1)

        self.data_size = len(self.sketch_label)

        self.sketch_transform = transforms.Compose([
                transforms.RandomRotation(degrees=8),#, fill=234),
                transforms.Resize(256),
                transforms.RandomHorizontalFlip(p=0.5),
                #transforms.RandomResizedCrop((256, 256), scale=(0.8, 1.0), ratio=(1.0, 1.0)),
                #transforms.ColorJitter(brightness=0.15, contrast=0.15, saturation=0.15, hue=0.15),
                transforms.ToTensor(),
                transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
            ])

        # render
        self.render_transform = transforms.Compose([
                transforms.RandomRotation(degrees=8),#, fill=234),
                transforms.Resize(256),
                transforms.RandomHorizontalFlip(p=0.5),
                #transforms.RandomResizedCrop((256, 256), scale=(0.8, 1.0), ratio=(1.0, 1.0)),
                #transforms.ColorJitter(brightness=0.15, contrast=0.15, saturation=0.15, hue=0.15),
                transforms.ToTensor(),
                transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
            ])

    def __getitem__(self, index):
        """
        Get training samples

        query_img: random choosed rgb sample image;
        positive_img: rendered no-texture sample image, same as query_img;
        negative_img: rendered no-texture sample image, random choosed from leve3 or leve2 dict
        """
        sketch_cls = self.sketch_label[index]

        # 单个正负样本，----扩充为多个正负样本
        # 选择正样本
        ps = self.level_model['%d'%sketch_cls]
        positive_id = ps[random.choice(range(len(ps)))]

        # choose negative id
        available = list(range(90))
        available.remove(sketch_cls)
        choice_level = random.choice(available)
        ns = self.level_model['%d'%choice_level]
        negative_id = ns[random.choice(range(len(ns)))]

        query_img = self._load_query_image_notexture(self.sketch_file[index])
        positive_img = self._load_pool_image_notexture(positive_id)
        negative_img = self._load_pool_image_notexture(negative_id)

        query_label = torch.Tensor([sketch_cls]).long()
        positive_label = torch.Tensor([sketch_cls]).long() #self.model_label[positive_id]
        negative_label = torch.Tensor([choice_level]).long() #self.model_label[negative_id]

        return {'query_img':query_img, 'positive_img':positive_img, 'negative_img':negative_img, 'query_label':query_label, 'positive_label':positive_label, 'negative_label':negative_label}
        #return {'query_img':query_img, 'positive_img':positive_img, 'negative_img':negative_img, 'center_label':center_label, 'cate_label':cate_label}

    def __len__(self):
        return self.data_size

    def _load_query_image(self, shape_id, image_name):

        prob = random.random()

        syn_id = '{0:03d}'.format(int(random.randint(0, 30)))
        if prob > 0.8:
            img_file = os.path.join(self.bicycle_image_path,  shape_id, 'image_' + syn_id + '.jpg')
        else:
            img_file = os.path.join(self.real_image_path, image_name)

        print('query: ', img_file)

        img = Image.open(img_file).convert('RGB')
        trans_img = self.query_transform(img)

        #if random.random() > 0.5:
        #    return self.query_random_erase(trans_img)
        #else:
        return trans_img

    def _load_query_image_notexture(self, image_file):

        img = Image.open(image_file).convert('RGB')
        trans_img = self.sketch_transform(img)
        return trans_img

    def _load_pool_image_notexture(self, shape_id):
        syn_id = [30,60,90,120,150]
        img_file = [os.path.join(self.render_path, '%04d'%shape_id, 'image_' + '%03d'%syn_id[i] + '.png') for i in range(5)]
        #img_file= [os.path.join(self.bicycle_image_path,  '%07d.png'%(int(shape_id)*5+i)) for i in range(5)]
        trans_img = []
        for i in img_file:
            img = Image.open(i).convert('RGB')
            trans_img.append(self.render_transform(img))

        #img = Image.open(img_file).convert('RGB')
        #trans_img = self.pool_transform(img)

        return torch.stack(trans_img)

class UnNormalize(object):
    def __init__(self, mean, std):
        self.mean = mean
        self.std = std

    def __call__(self, tensor):
        for t, m, s in zip(tensor, self.mean, self.std):
            t.mul_(s).add_(m)
        return tensor

if __name__ == '__main__':
    from options.train_options import TrainOptions
    opt = TrainOptions().parse()
        # hard-code some parameters for test
    opt.level2_dic = 'level2_dic_v1.npy'
    opt.level3_dic = 'level3_dic_v1.npy'
    opt.dataset_name = '3d_validation_set.npy'
    opt.inplanes = 64
    opt.reverse = True
    opt.pose_num = 12
    opt.num_threads = 0   # test code only supports num_threads = 1
    opt.batch_size = 1    # test code only supports batch_size = 1
    opt.serial_batches = True  # disable data shuffling; comment this line if results on randomly chosen images are needed.
    opt.no_flip = True    # no flip; comment this line if results on flipped images are needed.
    opt.display_id = -1   # no visdom display; the test code saves the results to a HTML file.

    data = RetrievalWorkshopBaselineDataset(opt)
    for i in data:
        print([i[k].shape for n,k in enumerate(i) if n<3 ])


import logging
import json
import zlib
import traceback
import struct
import random
import socket
from logging.handlers import DatagramHandler

WAN_CHUNK, LAN_CHUNK = 1420, 8154

class GELFHandler(DatagramHandler):
    def __init__(self, host, port, chunk_size=WAN_CHUNK):
        self.chunk_size = chunk_size
        DatagramHandler.__init__(self, host, port)

    def send(self, s):
        if len(s) < self.chunk_size:
            DatagramHandler.send(self, s)
        else:
            for chunk in ChunkedGELF(s, self.chunk_size):
                DatagramHandler.send(self, chunk)

    def makePickle(self, record):
        message_dict = self.make_message_dict(record)
        return zlib.compress(json.dumps(message_dict))

    def convert_level_to_syslog(self, level):
        return {
            logging.CRITICAL: 2,
            logging.ERROR: 3,
            logging.WARNING: 4,
            logging.INFO: 6,
            logging.DEBUG: 7,
        }.get(level, level)

    def get_full_message(self, exc_info):
        return traceback.format_exc(exc_info) if exc_info else ''

    def make_message_dict(self, record):
        d = {
            'version': "1.0",
            'host': socket.gethostname(),
            'short_message': record.getMessage(),
            'full_message': self.get_full_message(record.exc_info),
            'timestamp': record.created,
            'level': self.convert_level_to_syslog(record.levelno),
            'facility': record.name,
            'file': record.pathname,
            'line': record.lineno,
            '_function': record.funcName,
            '_pid': record.process,
            '_thread_name': record.threadName,
        }
        # record.processName was added in Python 2.6.2
        if hasattr(record, 'processName'):
            d['_process_name'] = record.processName
        return d

class ChunkedGELF(object):
    def __init__(self, message, size):
        self.message = message
        self.size = size
        self.pieces = struct.pack('>H', (len(message) / size) + 1)
        self.id = struct.pack('Q', random.randint(0, 0xFFFFFFFFFFFFFFFF)) * 4

    def message_chunks(self):
        return (self.message[i:i+self.size] for i
                    in range(0, len(self.message), self.size))

    def encode(self, sequence, chunk):
        return ''.join([
            '\x1e\x0f',
            self.id,
            struct.pack('>H', sequence),
            self.pieces,
            chunk
        ])

    def __iter__(self):
        for sequence, chunk in enumerate(self.message_chunks()):
            yield self.encode(sequence, chunk)


import sys
import re
import os
import logging
import logging.handlers
from random import randint, sample, shuffle
import Queue
import multiprocessing
import socket
from requests import Session
from subprocess import Popen, PIPE, check_output
import time
import datetime
import inspect
# from pysnmp.hlapi import *

try:
    from logging import NullHandler
except ImportError:
    class NullHandler(logging.Handler):
        def emit(self, record):
            pass

log = logging.getLogger(__name__)
log.addHandler(NullHandler())
check_crash_file = r'check_mdm_crash.pl.crash'
default_timeout = 60
global_config = None
cur_abs_path = os.path.split(inspect.getfile(inspect.currentframe()))[0]
global_config_file = os.path.abspath(os.path.join(cur_abs_path, '..', 'ac.ini'))
config_file = None
IP_ADDR = None
global_regex = re.compile(r'^\s*\[Global_Var\]\s*')
ini_item_regex = re.compile(r'^\s*(\w+)\s*=(.*)')

def init():
    global config_file, global_config, IP_ADDR
    global_config = read_config(global_config_file)
    # print "Get global config: {}".format(global_config)
    ip_prefix_list = [global_config['global_var']['ip_prefix']]
    for index in range(1,20):
        ip_prefix = 'ip_prefix{}'.format(index)
        if ip_prefix in global_config['global_var']:
            ip_prefix_list.append(global_config['global_var'][ip_prefix])
    # print "Got IP prefix list {}".format(ip_prefix_list)
    IP_ADDR = get_ip(*ip_prefix_list)
    if IP_ADDR is None:
        raise ValueError("Failed to get IP address with ip prefix list {}!".format(ip_prefix_list))
    config_file = get_config_file(IP_ADDR)
    if config_file is None:
        raise ValueError("Failed to get local config file name which matched ip address {}!".format(IP_ADDR))

def test_case():
    log.debug("Do nothing!")

def add_stderr_logger(level=logging.DEBUG, logger_name=__name__):
    # This method needs to be in this __init__.py to get the __name__ correct
    logger = logging.getLogger(logger_name)
    handler = logging.StreamHandler()
    handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(name)s %(lineno)d %(message)s'))
    logger.addHandler(handler)
    logger.setLevel(level)
    # logger.debug('Added a stderr logging handler to logger: %s' % logger_name)
    return logger

def add_file_logger(filename='test_case_py.log', level=logging.DEBUG, logger_name=__name__):
    # This method needs to be in this __init__.py to get the __name__ correct
    logger = logging.getLogger(logger_name)
    handler = logging.FileHandler(filename=filename, mode='a')
    handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(name)s %(lineno)d %(message)s'))
    handler.setLevel(level)
    logger.addHandler(handler)
    # logger.debug('Added a file %s logging handler to logger: %s' % (filename, logger_name))
    return logger

def enable_log(log_level, logger_name, log_file):
    add_stderr_logger(log_level)
    add_stderr_logger(log_level, logger_name)
    file_path, file_name = os.path.split(log_file)
    file_name, file_ext = os.path.splitext(file_name)
    log_file_name = "{}_{}.log".format(file_name, datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S"))
    add_file_logger(log_file_name, log_level)
    add_file_logger(log_file_name, log_level, logger_name)

def update_test(is_crash=0):
    config = merge_config()
    sess = Session()
    url = "{}/project/td".format(config['tbd_server'])
    post_params={
        'host_name': socket.gethostname(),
        'ip_addr': IP_ADDR,
        'tc_name': config['tc_name'],
        'test_client': config['monitor_client'],
        'build_verion': config['build_version'],
        'is_crash': is_crash,
        'ta_name': None,
        'tc_result': None,
        'ta_result': None,
    }
    resp = sess.post(url, data=post_params)
    log.debug("Post to {} with parameters {}".format(url, post_params))
    try:
        result = resp.json()
        if result['code']:
            log.error("update test data failed: {}!".format(result['result']))
        else:
            result = result['result']
            log.debug("update test data successfully: {}".format(result))
    except Exception as err:
        log.error("Failed to parse json result: {}!".format(err))

def check_crash(crash_info):
    if os.path.isfile(crash_info):
        log.debug("Found crash info file {}, and try to copy log files!".format(crash_info))
        crash_path = None
        try:
            with open(crash_info, 'r') as fh:
                crash_path = fh.readline()
                crash_path = crash_path.strip()
                log.debug("Got crash path '{}' from crash info files!".format(crash_path))
        except Exception as err:
            log.error("Failed to open {}: {}!".format(crash_info, err))
        if crash_path and os.path.isdir(crash_path):
            update_test(1)
            log.debug("Found crash dir, try to copy log files into it!")
            cmd(r"copy /y *.log {}".format(crash_path), timeout=300, skip_check=1)
            cmd("del /s /q *.log", timeout=300, skip_check=1)
            cmd(r"copy /y *.log.* {}".format(crash_path), timeout=300, skip_check=1)
            cmd("del /s /q *.log.*", timeout=300, skip_check=1)
            cmd("copy /y log\\*.* {}".format(crash_path), timeout=300, skip_check=1)
            cmd("del /s /q log\\*.*", timeout=300, skip_check=1)
            log.debug("Copy log files done, restart test case!".format(crash_info))
        else:
            log.debug("Not found crash dir, so just restart test case!")
        log.debug("Delete crash info file {} before exit.".format(crash_info))
        os.unlink(crash_info)
        sys.exit()

def merge_config():
    local_config = read_config(config_file)
    # print "Get local_config: {}".format(local_config)
    config = global_config['global_var'].copy()
    # print "Copy global config: {}".format(config)
    config.update(local_config['global_var'])
    # print "updated local_config: {}".format(config)
    if 'build_file' in config and os.path.isfile(config['build_file']):
        build_config = get_build_config(config['build_file'])
        config.update(build_config)
    config['ip_addr'] = IP_ADDR
    return config

def subs_var(string, config=None):
    if config is None:
        config = merge_config()
    for k in sorted(config.keys(), reverse=True):
        v = config[k]
        # log.debug("Try to replace ${} with {} in {}.".format(k, v, string))
        string = string.replace('$'+k, v)
    return string

def get_build_config(build_config):
    tbd_regex = re.compile(r'TBD_Server\s*\s*=\s*(.+)')
    project_regex = re.compile(r'Project_Name\s*\s*=\s*(.+)')
    build_regex = re.compile(r'Build_Version\s*\s*=\s*(.+)')
    meta_regex = re.compile(r'ACS_MetaBuild\s*\s*=\s*(.+)')
    crash_path_regex = re.compile(r'APS_USB_Upload_Root_Directory\s*\s*=\s*(.+)')
    match_regex = (tbd_regex, project_regex, build_regex, meta_regex, crash_path_regex)
    match_result = {}
    try:
        with open(build_config, 'r') as fh:
            while True:
                line = fh.readline()
                if not line:
                    break
                for regex in match_regex:
                    m = regex.match(line)
                    if m:
                        if regex == tbd_regex:
                            match_result['tbd_server'] = m.group(1)
                        elif regex == project_regex:
                            match_result['project_name'] = m.group(1)
                        elif regex == build_regex:
                            match_result['build_version'] = m.group(1)
                        elif regex == meta_regex:
                            match_result['meta_path'] = m.group(1)
                        elif regex == crash_path_regex:
                            match_result['crash_path'] = m.group(1)
                            if not os.path.isdir(match_result['crash_path']):
                                os.makedirs(match_result['crash_path'])
                        break
    except Exception as err:
        raise ValueError("Failed to open {} to read: {}".format(build_config, err))
    # if not (match_result.get('tbd_server', None) and match_result.get('project_name', None) and match_result.get('build_version', None) \
            # and match_result.get('meta_path', None) and match_result.get('crash_path', None))
        # raise ValueError("Need mandatory configuration in {}".format(config_file))
    return match_result

def read_config(conf_file):
    global_var = False
    config = {'global_var': {}}
    # log.debug("Try to open config file {}".format(conf_file))
    try:
        with open(conf_file, 'r') as fh:
            while True:
                line = fh.readline()
                if not line:
                    break
                if global_regex.match(line):
                    global_var = True
                    continue
                if global_var:
                    m = ini_item_regex.match(line)
                    if m:
                        (k, v) = (m.group(1), m.group(2))
                        v = v.strip()
                        config['global_var'][k] = v
                        continue
    except Exception as err:
        log.error("Failed to open config '{}' to read: {}".format(conf_file, err))
    # log.debug("read config: {}".format(config))
    return config

def cmd(command, timeout=default_timeout, skip_check=0):
    command = subs_var(command)
    if not skip_check:
        check_crash(check_crash_file)
    log.debug("cmd '{}'({}) begin!".format(command, timeout))
    try:
        output, err = Runner.fork(command, timeout=timeout)
    except Exception as err:
        log.error("cmd '{}' exception: {}!".format(command, err))
        return
    if err:
        log.debug("cmd '{}' done with stderr: {}!".format(command, err))
    if output:
        log.debug("cmd '{}' done with stdout: {}!".format(command, output))
    if not (err or output):
        log.debug("cmd '{}' done without output/error!".format(command))
    return (output, err)

def get_ip(*ip_prefix_list):
    ip_output = None
    local_ip = None
    try:
        ip_output = check_output('ipconfig')
    except Exception as err:
        log.error("Failed to run ipconfig to get ip: {}".format(err))
        return None
    for ip_prefix in ip_prefix_list:
        ip_regex = re.compile(r'IPv4\s+Address.*({}\S*)'.format(ip_prefix.replace('.', r'\.')))
        for m in ip_regex.finditer(ip_output):
            if local_ip:
                log.debug("Find more than one IP address: {}, {}, ignore the later one!".format(local_ip, m.group(1)))
            else:
                local_ip = m.group(1)
        if local_ip:
            break
        else:
            log.error("Failed to find any IP address with prefix '{}'!".format(ip_prefix))
    return local_ip

def get_config_file(local_ip):
    ip_index_regex = re.compile(r'^(\d+)\.(\d+)\.(\d+)\.(\d+)$')
    ip_index = None
    config_file = None
    if local_ip:
        m = ip_index_regex.search(local_ip)
        if m:
            ip_parts = m.groups()
    if ip_parts:
        for index in range(0,4):
            ip_index = '.'.join(ip_parts[index:4])
            config_file = "ac_{}.ini".format(ip_index)
            log.debug("Get ip index {}, so try to find config {}.".format(ip_index, config_file))
            config_file = os.path.abspath(os.path.join(cur_abs_path, '..', config_file))
            # print "Try to find local config file {}".format(config_file)
            if os.path.isfile(config_file):
                log.debug("Find user config file {}. so use it!".format(config_file))
                break
            else:
                config_file = None
    else:
        log.error("Cannot get ip index, so failed to get config file!")
    return config_file

def sleep(sec, max_sec=None):
    sleep_sec = sec
    if max_sec is not None:
        sleep_sec = randint(sec, max_sec)
    log.debug("Sleep {} seconds...".format(sleep_sec))
    time.sleep(sleep_sec)

def arrange_internet_group(client_for_ap, aps_internet, internet_server_set):
    internet_group = {}
    if aps_internet and internet_server_set:
        aps_internet_num = len(aps_internet)
        internet_server_num = len(internet_server_set)
        aps_internet_sample = None
        internet_server_sample = sample(internet_server_set, internet_server_num)
        if aps_internet_num > internet_server_num:
            aps_internet_sample = sample(aps_internet, internet_server_num)
        else:
            aps_internet_sample = sample(aps_internet, aps_internet_num)
        index = 0
        for ap in aps_internet_sample:
            use_internet_server = internet_server_sample[index]
            if ap in client_for_ap:
                internet_group[use_internet_server] = client_for_ap[ap]
            index += 1
    return internet_group

def arrange_traffic_group(client_for_ap, aps_pair=None):
    traffic_group = []
    used_ap = {}
    if aps_pair:
        for pair in aps_pair:
            combine_clients = []
            for ap in pair:
                if ap in client_for_ap:
                    combine_clients.extend(client_for_ap[ap])
                    used_ap[ap] = 1
            if combine_clients:
                shuffle(combine_clients)
                traffic_group.append(combine_clients)
    for ap, ap_clients in client_for_ap.items():
        if ap not in used_ap:
            traffic_group.append(ap_clients)
    log.debug("Got traffic group: {}".format(traffic_group))
    return traffic_group

def allocate_random_client(aps_set, clients_set):
    ap_num = len(aps_set)
    client_num = len(clients_set)
    group_num = min(ap_num, client_num/2)
    if group_num <= 0:
        raise ValueError("Group number is {}, your ap or client number is not reasonable!".format(group_num))
    log.debug("Test {} APs set: {}!".format(ap_num, aps_set))
    log.debug("Test {} clients set: {}!".format(client_num, clients_set))
    log.debug("It will use {} group to connect different APs!".format(group_num))
    extra_client_num = client_num - group_num * 2
    random_ap_list = sample(aps_set, group_num)
    random_client_num_for_ap = {ap:2 for ap in random_ap_list}
    random_client_for_ap = {}
    if extra_client_num > 0:
        log.debug("Clients number is enough and some groups would own more than 2 clients")
        for ap in random_ap_list:
            get_extra = randint(0, extra_client_num)
            random_client_num_for_ap[ap] += get_extra
            extra_client_num = extra_client_num - get_extra
            if extra_client_num <= 0:
                break
        if extra_client_num > 0:
            get_extra_bonus = randint(0, group_num - 1)
            random_client_num_for_ap[random_ap_list[get_extra_bonus]] += extra_client_num
    random_clients = clients_set[:]
    shuffle(clients_set)
    log.debug("shuffle clients to get random client list: {}".format(clients_set))
    client_offset = 0
    for ap in random_ap_list:
        client_start_index = client_offset
        client_offset = client_start_index + random_client_num_for_ap[ap]
        random_client_for_ap[ap] = random_clients[client_start_index:client_offset]
        log.debug("Get clients for {} from random duration [{},{}]: {}".format(ap, client_start_index, client_offset, random_client_for_ap[ap]))
    return random_client_for_ap

class Runner(object):
    default_timeout = default_timeout

    @classmethod
    def fork(cls, cmd, input=None, timeout=default_timeout):
        # log.debug("current pid {}".format(os.getpid()))
        q = multiprocessing.Queue()
        p = multiprocessing.Process(target=cls.popen, args=(q, cmd, input))
        p.start()
        # log.debug("fork child process pid {}".format(p.pid))
        # log.debug("join child process pid {} with timeout {}".format(p.pid, timeout))
        p.join(timeout)
        while p.is_alive():
            log.error("Process {} timeout! Terminating...".format(p.pid))
            p.terminate()
            p.join(1)
            raise ValueError("child process timeout {}".format(timeout))
        out, err = (None, None)
        try:
            out, err = q.get_nowait()
        except Queue.Empty:
            log.error("Cannot get stdout/stderr: no response until exit!")
        # log.debug("fork return output: {}".format(out))
        if err:
            log.error("fork return error: {}!".format(err))
        # log.debug("Process has been done!")
        return (out, err)

    @classmethod
    def popen(cls, q, cmd, input=None):
        p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate(input)
        if err:
            log = add_stderr_logger()
            log.error("popen error: {}".format(err))
        q.put((out, err))

init()

if __name__ == '__main__':
    add_stderr_logger()
    test_case()


from icrawler.builtin import GoogleImageCrawler
import argparse

def arg_parse():
    parser = argparse.ArgumentParser(description='CNN implemented with Keras')
    parser.add_argument('--key', dest='key', type=str)
    parser.add_argument('--dir', dest='dir', type=str)
    parser.add_argument("--N", dest="N", type=int)
    args = parser.parse_args()
    return args

args = arg_parse()

crawler = GoogleImageCrawler(storage={"root_dir": args.dir})
crawler.crawl(keyword=args.key, max_num=args.N)


##### Visual Search Demo - Image similarity with Inception.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os.path
import sys
import math
import numpy as np
import tensorflow as tf
from flask import Flask, request, render_template, jsonify
import json

app = Flask(__name__)
working_dir = os.getcwd()

# Returns a dictionary of feature representations for an input list of JPEG images,
# which is the next-to-last layer of the Inception network (trained on the
# ImageNet 2012 Challenge dataset). Tensorflow's Inception reference:
# https://tensorflow.org/tutorials/image_recognition/
def get_features():
  img_features = {}

  # Creates graph from saved graph_def.pb.
  graph_file = working_dir + '/inception/classify_image_graph_def.pb'
  with tf.gfile.FastGFile(graph_file, 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    _ = tf.import_graph_def(graph_def, name='')

  with tf.Session() as sess:
    # Runs the softmax tensor by feeding the image_data as input to the graph.
    # 'pool_3:0': A tensor containing the next-to-last layer containing 2048
    #  float description of the image.
    last_layer = sess.graph.get_tensor_by_name('pool_3:0')

    for file in os.listdir(working_dir + '/static/img'):
      if file.endswith(".jpg"):
        img_path = working_dir + '/static/img/' + file
        image_data = tf.gfile.FastGFile(img_path, 'rb').read()
        # 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG
        #   encoding of the image.
        features = sess.run(last_layer, {'DecodeJpeg/contents:0': image_data})
        img_features[file] = list([float(x) for x in features[0][0][0]])
  return img_features

# Serve the homepage, a static file
@app.route("/")
def index():
  return render_template('index.html')

@app.route('/get_img_features')
def get_img_features():
  img_features = get_features()
  print(type(img_features))
  print("server calculated features")
  return jsonify(img_features)

# Serve static files
@app.route('/<path:path>')
def static_proxy(path):
  # send_static_file will guess the correct MIME type
  return app.send_static_file(path)

# Run the server app
if __name__ == "__main__":
  app.debug = True
  app.run()
  app.run(debug = True)


from concurrent.futures import ThreadPoolExecutor, as_completed
from pprint import pprint
from datetime import datetime
import time
from itertools import repeat

import yaml
from netmiko import ConnectHandler

start_msg = '===> {} Connection to device: {}'
received_msg = '<=== {} Received result from device: {}'

class CiscoSSH:
    def __init__(self, **device_dict):
        self.device_dict = device_dict

    def __call__(self, command):
        print(start_msg.format(datetime.now().time(), self.device_dict['ip']))
        self.ssh = ConnectHandler(**self.device_dict)
        self.ssh.enable()
        result = self.ssh.send_command(command)
        print(received_msg.format(datetime.now().time(), self.device_dict['ip']))
        return result

def threads_conn(devices, command, limit=2):
    all_results = []
    with ThreadPoolExecutor(max_workers=limit) as executor:
        future_ssh = [
            executor.submit(device, command) for device in devices
        ]
        for f in as_completed(future_ssh):
            all_results.append(f.result())
    return all_results

if __name__ == '__main__':
    devices = yaml.load(open('devices.yaml'))
    dev_instances = [CiscoSSH(**params) for params in devices['routers']]
    all_done = threads_conn(dev_instances, command='sh clock')
    pprint(all_done)


from SPARQLWrapper import SPARQLWrapper, JSON
from requests import get
import numpy as np
import math
import json

__data_file_path = '/Users/timoon/Projects/spbstu-is/simple-recommendation-system/data.csv'
__day_context_file_path = '/Users/timoon/Projects/spbstu-is/simple-recommendation-system/context_day.csv'
__place_context_file_path = '/Users/timoon/Projects/spbstu-is/simple-recommendation-system/context_place.csv'

def getUserMovieMarkData():
    return np.genfromtxt(__data_file_path, delimiter=',', dtype=int, skip_header=1, usecols=(np.arange(1, 31, 1)))

def getUserMovieDayContextData():
    return np.genfromtxt(__day_context_file_path, delimiter=',', dtype=str, skip_header=1, usecols=(np.arange(1, 31, 1)))

def getUserMoviePlaceContextData():
    return np.genfromtxt(__place_context_file_path, delimiter=',', dtype=str, skip_header=1, usecols=(np.arange(1, 31, 1)))

def findCompareapleUsers(target_user_id):
    data = getUserMovieMarkData()

    target_user_marks = data[target_user_id]
    similarity = []

    for user_id, user_marks in enumerate(data):
        if user_id != target_user_id:
            target_user_sqr_sum = 0
            current_user_sqr_sum = 0
            compared_users_sum = 0

            for mark_id, mark in enumerate(user_marks):
                if target_user_marks[mark_id] != -1 and mark != -1:
                    target_user_sqr_sum += target_user_marks[mark_id] * target_user_marks[mark_id]
                    current_user_sqr_sum += mark * mark
                    compared_users_sum += target_user_marks[mark_id] * mark

            similarity_value = compared_users_sum / (math.sqrt(current_user_sqr_sum) * math.sqrt(target_user_sqr_sum))
            similarity.append((user_id, similarity_value))

    dtype = [('user_id', int), ('similarity_value', float)]
    arr_sim = np.array(similarity, dtype=dtype)
    sorted_sim = np.sort(arr_sim, order='similarity_value')
    return sorted_sim[-7:]

def processUserRatings(target_user_id):
    real_target_id = target_user_id - 1
    relevant_users = findCompareapleUsers(real_target_id)
    data = getUserMovieMarkData()

    target_user_avg_mark = my_avg(data[real_target_id])

    relevant_users_avg_mark = []

    for user in relevant_users:
        relevant_users_avg_mark.append(my_avg(data[user[0]]))

    processed_target_user_marks = []

    for movie_id in np.arange(0, 30, 1):
        if data[real_target_id][movie_id] == -1:
            sim_sum = 0
            pure_sim_sum = 0

            for relevant_id, user in enumerate(relevant_users):
                if data[user[0]][movie_id] != -1:
                    sim_sum += user[1] * (data[user[0]][movie_id] - relevant_users_avg_mark[relevant_id])
                    pure_sim_sum += user[1]

            processed_target_user_marks.append((movie_id + 1, target_user_avg_mark + (sim_sum / pure_sim_sum)))

    return np.round(processed_target_user_marks, 3)

def dayContextValue(movie_ids):
    mark_data = getUserMovieMarkData()
    day_context_data = getUserMovieDayContextData()

    movie_day_param = []

    for movie in movie_ids:
        movie_marks = np.array([], dtype=int)
        movie_holiday_marks = np.array([], dtype=int)

        for user_id, user_marks in enumerate(mark_data):
            if user_marks[int(movie[0]) - 1] != -1:
                movie_marks = np.append(movie_marks, user_marks[int(movie[0]) - 1])
                if day_context_data[user_id][int(movie[0]) - 1].lstrip() == 'Sat' or day_context_data[user_id][int(movie[0]) - 1].lstrip() == 'Sun':
                    movie_holiday_marks = np.append(movie_holiday_marks, user_marks[int(movie[0]) - 1])

        movie_day_param.append((int(movie[0]), np.divide(np.average(movie_marks), np.average(movie_holiday_marks))))

    return movie_day_param

def placeContextValue(movie_ids):
    mark_data = getUserMovieMarkData()
    place_context_data = getUserMoviePlaceContextData()

    movie_place_param = []

    for movie in movie_ids:
        movie_marks = np.array([], dtype=int)
        movie_home_marks = np.array([], dtype=int)

        for user_id, user_marks in enumerate(mark_data):
            if user_marks[int(movie[0]) - 1] != -1:
                movie_marks = np.append(movie_marks, user_marks[int(movie[0]) - 1])
                if place_context_data[user_id][int(movie[0]) - 1].lstrip() == 'h' or place_context_data[user_id][int(movie[0]) - 1].lstrip() == 'v':
                    movie_home_marks = np.append(movie_home_marks, user_marks[int(movie[0]) - 1])

        movie_place_param.append((int(movie[0]), np.divide(np.average(movie_marks), np.average(movie_home_marks))))

    return movie_place_param

def my_avg(lst):
    sumer = 0
    cnt = 0
    for val in lst:
        if val != -1:
            sumer += val
            cnt += 1

    return sumer / cnt

def personalRecommendation(target_user):
    unseen_movie_processed_ratings = processUserRatings(target_user)
    movie_place_val = placeContextValue(unseen_movie_processed_ratings)
    movie_holiday_val = dayContextValue(unseen_movie_processed_ratings)

    recommendation_ratings = []
    for rating_id, val in enumerate(unseen_movie_processed_ratings):
        recommendation_ratings.append((int(val[0]), val[1] * movie_place_val[rating_id][1] * movie_holiday_val[rating_id][1]))

    recommendations = np.array(recommendation_ratings, dtype=[('movie_id', int), ('similarity_value', float)])
    sorted = np.sort(recommendations, order='similarity_value')

    return sorted[-1]

def saveAnsvers(target_user):
    user_id = target_user
    recommendation = personalRecommendation(target_user)

    res_obj = {
        'user': target_user,
        '1': {

        }
    }

    for val in processUserRatings(target_user):
        res_obj['1']['movie ' + str(int(val[0]))] = val[1]

    res_obj['2'] = {
        'movie ' + str(recommendation[0]): np.round(recommendation[1], 3)
    }

    with open('/Users/timoon/Projects/spbstu-is/simple-recommendation-system/results.json', 'w') as file:
        json.dump(res_obj, file)
        file.close()

# My film 'id'
movie_id = 'Movie ' + str(personalRecommendation(1)[0])

# I get film name by id from file
__data_file_path = 'movie_names.csv'
movie_names = np.genfromtxt(__data_file_path, delimiter=',', dtype=str, skip_header=0, usecols=(np.arange(1, 1, 1)))
movie_name = movie_names[np.where(movie_names[:,0]==movie_id)][0][1].strip()

# Ask api for Q by provided name
resp = get('https://www.wikidata.org/w/api.php', {
        'action': 'wbgetentities',
        'titles': movie_name,
        'sites': 'enwiki',
        'props': '',
        'format': 'json'
    }).json()

wikiMovieCode = list(resp['entities'])[0]

# Prepare request to wikidata
sparql = SPARQLWrapper("https://query.wikidata.org/sparql")
sparql.setQuery(
"""
SELECT DISTINCT ?human ?humanLabel ?picture

WHERE {
?human wdt:P31 wd:Q5.
?human wdt:P106 wd:Q33999.

?human wdt:P18 ?picture.

wd:""" + wikiMovieCode + """ wdt:P161 ?human.

?human wdt:P569 ?date.
wd:""" + wikiMovieCode + """ wdt:P577 ?dateFilm.

FILTER (YEAR(?dateFilm) - YEAR(?date) > 40)

SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
"""
)

# Send it and get the response in json format
sparql.setReturnFormat(JSON)
results = sparql.query().convert()

# My result data container
res = {}

# Put results into it
for res_id, result in enumerate(results['results']['bindings']):
    res[res_id] = {'name': result['humanLabel']['value'], 'picture':result['picture']['value'] }

# Then create file based on this data
# ?????
# PROFIT!
with open('results.json', 'w') as file:
    json.dump(res, file)
    file.close()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Gathers information about system load (Unix) or CPU utilization (Windows).
"""

import collections
import os

from .. import _utils

def main(system, logger):
    """
    Gets the system load average for the last 1, 5, and 15 minutes (Linux and
    FreeBSD). Gets the current CPU usage (Windows).
    """
    result = collections.OrderedDict()

    if system == 'Windows':
        import psutil

        as_percent = psutil.cpu_percent(interval=0.1)
        as_decimal = _utils.percent_to_decimal(as_percent)
        result.update({'usage': as_decimal})

    else:  # `system in ('FreeBSD', 'Linux')`
        for (key, val) in zip(('1min', '5min', '15min'), os.getloadavg()):
            result.update({key: val})

    return result


import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import networkx as nx
from parse import *
from utils  import *
from pulp import *
from math import comb

graph, budget = read_input_file("inputs/small/small-1.in")
pos = nx.kamada_kawai_layout(graph)  # positions for all nodes
nx.draw(graph, pos, with_labels=True)
prob = LpProblem("proj",LpMaximize)

inputs = pd.read_csv("inputs/small/small-1.in", header = None)
n = int(list(inputs[0])[0])
s_max = float(list(inputs[0])[1])
K = 3

# Parsing input data
df = inputs
df = pd.DataFrame([x.split() for x in df[0].tolist() ])
df = df.loc[2:,:]
df["i"] = df.loc[:,0]
df["j"] = df.loc[:,1]
df["sadness"] = df.loc[:,3]
df["happiness"] = df.loc[:,2]
df = df[["i", "j", "sadness", "happiness"]]
i_id = [int(i) for i in list(df["i"])]
j_id = [int(i) for i in list(df["j"])]
sadness = [float(i) for i in list(df["sadness"])]*K
happiness = [float(i) for i in list(df["happiness"])]*K

sadness_1 = [float(i) for i in list(df["sadness"])]
happiness_1 = [float(i) for i in list(df["happiness"])]
len(sadness) == len(happiness)

prob = LpProblem("solver",LpMaximize)

# edges
d_e = {}
for k in range(K):
    for i in range(comb(n,2)):
        d_e["e_{0}_{1}_{2}".format(i_id[i],j_id[i],k)] = LpVariable("e_{0}_{1}_{2}".format(i_id[i],j_id[i],k), cat="Binary")
edges_1 = list(d_e.values())

# Create another edge array with different sort
edges_2 = [d_e.get(i) for i in sorted(d_e.keys(), key=lambda x: (x[2], x[4]))]

# Create another edge array for constraint 4
edges_3 = [d_e.get(i) for i in sorted(d_e.keys(), key=lambda x: (x[2], x[6]))]

# Check
len(edges_1) == comb(n,2)*K

# vertices
d_v = {}
for j in range(K):
    for i in range(n):
        d_v["v_{0}_{1}".format(i,j)] = LpVariable("v_{0}_{1}".format(i,j), cat="Binary")
vertices_1 = list(d_v.values())

# Create another edge array with different sort
vertices_2 = [d_v.get(i) for i in sorted(d_v.keys(), key=lambda x: x[2])]

# Check
len(vertices_1) == n*K

# Linearization of quadratic variables 1
v_list = np.array(np.array_split(vertices_1, K)).tolist()
v_big = []
for i in range(K):
    j = 0
    while j != n:
        for _ in range(j,n-1):
            v_big.append(v_list[i][j])
        j += 1

print(len(edges_1) == len(v_big))

# Linearization of quadratic variables 2
v_list_2 = [i[1:] for i in v_list]
v_big_2 = []
for h in range(K):
    j = 0
    while j != n:
        for i in range(j,n-1):
            v_big_2.append(v_list_2[h][i])
        j += 1
        i = j
print(len(edges_1) == len(v_big_2))

##LP Solver
# Objective function
prob += sum(edges_1[i] * happiness[i] for i in range(len(happiness)))

# Constraint 1
# Each rooms has sadness levels <= s_max / K
aa = []
h = 0
for i in range(K):
    for j in range(comb(n,2)):
        aa.append(edges_1[h] * sadness_1[j])
        h += 1
constraint_2 = [sum(aa[i:i+ comb(n,2)]) for i in range(0, len(aa), comb(n,2))]
for i in range(len(constraint_2)):
    prob += constraint_2[i] <= s_max / K
# Constraint 2
# Each person belongs to only one room
constraint_3 = [sum(vertices_2[i:i + K]) for i in range(0, len(vertices_2), K)]
for i in range(len(constraint_3)):
    prob += constraint_3[i] == 1
# Linearization of quadratic variables
# Constraint 3.1
for i in range(len(edges_1)):
    prob += edges_1[i] <= v_big[i]
# Constraint 3.2
for i in range(len(edges_1)):
    prob += edges_1[i] <= v_big_2[i]
# Constraint 3.3
for i in range(len(edges_1)):
    prob += edges_1[i] >= v_big[i] + v_big_2[i] - 1

prob.solve()

variables = [v.varValue for v in prob.variables()[len(prob.variables()) - n*K : len(prob.variables())]]
locations = list(np.mod(np.nonzero(variables)[0], K))
output_dict = dict(list(enumerate(locations)))
print(output_dict)
print(is_valid_solution(output_dict, graph, s_max, K))
# write_output_file(output_dict, "path")


from io import StringIO
from PIL import Image
import matplotlib
import plotly

# https://stackoverflow.com/questions/18897511/how-to-drawimage-a-matplotlib-figure-in-a-reportlab-canvas

def fig2img(fig):
    imgdata = StringIO()
    fig.savefig(imgdata, format='png')
    imgdata.seek(0)  # rewind the data

    return Image.open(imgdata)

def builder(path, ref, fig_dict = dict(), var_dict = dict()):
    SCALE = 2
    PAD = 0.3

    from pptx import Presentation
    from pptx.util import Inches
    import tempfile
    import copy
    import os

    prs = Presentation(ref)
    out = Presentation()

    out.slide_width = prs.slide_width
    out.slide_height = prs.slide_height

    for slide in prs.slides:
        blank_slide_layout = out.slide_layouts[6]
        o_slide = out.slides.add_slide(blank_slide_layout)

        for shape in slide.shapes:
            try:
                if shape.text[0] == "!" and shape.text[1] == "(" and shape.text[-1] == ")":
                    f = tempfile.NamedTemporaryFile(suffix=".png")
                    fig = fig_dict[shape.text[2:-1]]
                    if type(fig) == matplotlib.figure.Figure:
                        fig.set_size_inches(SCALE*shape.width/Inches(1), SCALE*shape.height/Inches(1))
                        fig.set_tight_layout({"pad" : PAD})
                        fig.savefig(f.name, format='png',dpi = 300)
                        pic = o_slide.shapes.add_picture(f.name, shape.left, shape.top, width=shape.width)
                        continue
                    elif type(fig) == plotly.graph_objs._figure.Figure:
                        fig.write_image(f.name)
                        pic = o_slide.shapes.add_picture(f.name, shape.left, shape.top, width=shape.width)
                        continue

                fvar = False
                otxt = ""
                nvar = ""
                i = 0

                while i < len(shape.text):
                    if fvar:
                        if shape.text[i] != "]":
                            nvar += shape.text[i]
                        else:
                            otxt += var_dict[nvar]
                            fvar = False
                            nvar = ""
                    elif shape.text[i] == "$" and shape.text[i+1] == "[":
                        fvar = True
                        nvar = ""
                        i+=1
                    else:
                        otxt += shape.text[i]
                    i+=1
                shape.text = otxt
                cp = copy.deepcopy(shape.element)
                o_slide.shapes._spTree.insert_element_before(cp, 'p:extLst')
                continue
            except:
                pass

            try:
                img = shape.image.blob
                f = tempfile.NamedTemporaryFile()
                f.write(img)
                o_slide.shapes.add_picture(f.name, shape.left, shape.top, shape.width, shape.height)
                continue
            except:
                pass

            o_slide.shapes._spTree.insert_element_before(copy.deepcopy(shape.element), 'p:extLst')
    out.save(path)


import torch.nn as nn

class LockedDropout(nn.Module):
    def __init__(self):
        super(LockedDropout, self).__init__()

    def forward(self, inputs, dropout=0.5):
        if not self.training or not dropout:
            return inputs
        m = inputs.data.new(1, inputs.size(1), inputs.size(2)).bernoulli_(1 - dropout)
        mask = m.div_(1 - dropout)
        mask = mask.expand_as(inputs)
        return mask * inputs


from django.contrib.auth.middleware import get_user

from django.shortcuts import render

from django.views import View

from rest_framework.views import APIView

from authentication.logics import *
from backend.config.client_response.response_status import ResponseStatusEnum
from common.exception.childf_exception import  exception_handler
from common.util.response_utils import ResponseUtils
from person.models import  Hamyar

from person.serializers import PersonSerializer, AdminSerializer, UserSerializer, HamyarSerializer, MadadjuSerializer

# Create your views here.

class RegisterAPIView(APIView):
    def post(self, request):
        try:
            register_user(request=request,data_dict=request.data)
            resp_data = "register successfully"
            resp_status = ResponseStatusEnum.ok_200
        except Exception as e:
            resp_data, resp_status = exception_handler(e)
        return ResponseUtils.jsonify(response_enum_obj=resp_status.value, data=resp_data)

class ActiveAccountAPIView(APIView):
    def post(self, request):
        try:
            resp_data = activate_user(request, request.data)
            resp_status = ResponseStatusEnum.ok_200
        except Exception as e:
            resp_data, resp_status = exception_handler(e)
        return ResponseUtils.jsonify(response_enum_obj=resp_status.value, data=resp_data)

class ForgetPassAPIView(APIView):
    def post(self,request):
        try:
            forget_password(request, request.data)
            resp_data = "new password send to email"
            resp_status = ResponseStatusEnum.ok_200
        except Exception as e:
            resp_data, resp_status = exception_handler(e)
        return ResponseUtils.jsonify(response_enum_obj=resp_status.value, data=resp_data)

class SendCodeAPIView(APIView):
    def post(self,request):
        try:
            send_verification_code(data_dict=request.data)
            resp_data = "send activation code"
            resp_status = ResponseStatusEnum.ok_200
        except Exception as e:
            resp_data, resp_status = exception_handler(e)
        return ResponseUtils.jsonify(response_enum_obj=resp_status.value, data=resp_data)

class LoginAPIView(APIView):
    def post(self, request):
        try:
            resp_data = UserSerializer(login_user(request, request.data)).data
            resp_status = ResponseStatusEnum.ok_200

        except Exception as e:
            resp_data,resp_status = exception_handler(e)
        return ResponseUtils.jsonify(response_enum_obj=resp_status.value,data=resp_data)

class UserAPIView(APIView):
    def get(self, request,username):
        try:

            resp_data = user_exist(username)
            resp_status = ResponseStatusEnum.ok_200

        except Exception as e:
            resp_data,resp_status = exception_handler(e)
        return ResponseUtils.jsonify(response_enum_obj=resp_status.value,data=resp_data)

class LogOutAPIView(APIView):
    def get(self, request):
        try:
            logout_user(request)
            resp_data = "logout successfully"
            resp_status=ResponseStatusEnum.ok_200
        except Exception as e:
            resp_data,resp_status = exception_handler(e)
        return ResponseUtils.jsonify(response_enum_obj=resp_status.value,data=resp_data)

#############################

class TestView(View):
    def get(self, request, *args,**kwargs):
          return render(request=request, template_name="test.html")

def test(user):
    return User.objects.get(username=user.username)

class TestAPIView(APIView):

    def get(self, request):
        madadju = Person.objects.all()
       # resp_data = PersonSerializer(Person.objects.filter(user__email__),many=True).data
        return ResponseUtils.jsonify(response_enum_obj=ResponseStatusEnum.ok_200.value, data=resp_data)

            #todo exception handling


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import uuid
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('Site', '0002_auto_20150801_1851'),
    ]

    operations = [
        migrations.CreateModel(
            name='TokenLogin',
            fields=[
                ('id_token', models.UUIDField(serialize=False, default=uuid.uuid4, primary_key=True, editable=False)),
                ('data_login', models.DateTimeField(auto_now_add=True)),
                ('fk_usuario', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
            ],
        ),
    ]


# coding=utf-8
# The MIT License (MIT)
#
# Copyright (c) 2015 Agustin Prats
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import xlwt
from xlwt import Font
from xlwt import XFStyle

class XlsExporter(object):

    @staticmethod
    def export(match_list, file_path):
        wb = xlwt.Workbook()
        ws = wb.add_sheet('Ranking')

        # writing header
        header = ['Ranking',
                  'Name',
                  'Rating',
                  'Reviews',
                  'Price',
                  'Cuisines',
                  'Edenred Name',
                  'Match',
                  'Edenred Address']
        font = Font()
        font.name = 'Arial'
        font.bold = True
        style = XFStyle()
        style.font = font
        for index in range(len(header)):
            ws.write(0, index, header[index], style)

        # writing restaurants
        for index in range(len(match_list)):
            match = match_list[index]
            ws.write(index+1, 0, int(match.restaurant1.ranking))
            ws.write(index+1, 1, xlwt.Formula('HYPERLINK("%s"; "%s")' % (match.restaurant1.url, match.restaurant1.name)))
            ws.write(index+1, 2, float(match.restaurant1.rating.replace(',', '.')))
            ws.write(index+1, 3, int(match.restaurant1.review_count))
            ws.write(index+1, 4, match.restaurant1.price)
            ws.write(index+1, 5, ', '.join(match.restaurant1.cuisines))
            ws.write(index+1, 6, match.restaurant2.name)
            ws.write(index+1, 7, int(100.0 * match.score))
            address = match.restaurant2.address + ', ' + match.restaurant2.postcode
            map_link = 'https://www.google.com/maps?f=q&source=s_q&hl=es&q=%s' % address
            ws.write(index+1, 8, xlwt.Formula('HYPERLINK("%s"; "%s")' % (map_link, address)))
        wb.save(file_path)


import math
import itertools

def digits(t, b): #assumes t < b
	mantissa = b
	while True:
		mantissa /= b
		digit = math.floor(t/mantissa)
		t -= digit * mantissa
		yield digit

template = "\draw[thick, {3}, {{[-)}}] ({0}, {2}) -- ({1}, {2});"
def pt(ints, y, colorfun):
    for ((a, b), d) in ints:
        print(template.format(a*10,b*10,y, colorfun(d)))

def intervals(t, b, depth):
    digs = list(itertools.islice(digits(t,b), depth))
    digs[-1] += 1
    x = 0
    for i, d in enumerate(digs):
        for k in range(d):
            yield ((x, x+b**(-i)), i)
            x += b**(-i)


import cv2
import datetime
cap = cv2.VideoCapture(0)
print("Original Height",cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print("Original Width",cap.get(cv2.CAP_PROP_FRAME_WIDTH))

cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240.0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320.0)

print("\nAfter modified HIGHT and WIGHT")
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))

while (cap.isOpened()):
    _, frame = cap.read()
    frame_flip = cv2.flip(frame, 1)
##    color = cv2.cvtColor(frame, cv2.IMREAD_COLOR )
    font = cv2.FONT_HERSHEY_COMPLEX_SMALL
    frame = cv2.putText(frame_flip,str(datetime.datetime.now()),(0,20),font,.5, (161,174,179),1,cv2.LINE_AA)
    cv2.imshow('Videos', frame)
    if cv2.waitKey(1) == ord('q'):
        break

cap.release() # None
cv2.destroyAllWindows() # destroy windows.


import os

from flask import Flask, render_template, redirect, request, url_for
from flask_pymongo import PyMongo
from bson.objectid import ObjectId

if os.path.exists("env.py"):
    import env

app = Flask(__name__)

#grab the enviornment variables
app.config["MONGO_URI"] = os.environ.get("MONGO_URI")
app.secret_key = os.environ.get("SECRET_KEY")

mongo = PyMongo(app)

# mongo db name is 'breastcancer'
# stories held in 'stories' collection
# information held in 'info' collection
storyData = mongo.db.stories
infoData = mongo.db.info

@app.route("/")
def index():
    return render_template("index.html", latestStory = storyData.find_one())

@app.route("/stories")
def stories():
    return render_template("stories.html", stories = storyData.find())

@app.route("/create_story")
def create_story():
    return render_template("create_story.html")

@app.route("/edit_story_page")
def edit_story_page():
    storyId=request.args.get("storyId", None)
    return render_template("edit_story_page.html", story=storyData.find_one({ '_id': ObjectId(storyId)}))

@app.route("/delete_story")
def delete_story():
    storyId=request.args.get("storyId", None)
    storyData.remove({ "_id": ObjectId(storyId) })
    return redirect(url_for("stories"))

@app.route("/publish_story", methods=["POST"])
def publish_story():
    storys=storyData
    storys.insert_one(request.form.to_dict())
    return redirect(url_for('stories'))

@app.route('/edit_story', methods=["POST"])
def edit_story():
    storyId=request.args.get('storyId', None)
    storys = storyData
    storys.update( {'_id': ObjectId(storyId)},
    {
        'title':request.form.get('title'),
        'text':request.form.get('text'),
    })
    return redirect(url_for('stories'))

# a page for viewing a single story on its own
# @app.route("/dedicated")
# def dedicated():
#    storyId = request.args.get('storyId', None)
#    return render_template("dedicated.html", story=storyData.find_one({ '_id': ObjectId(storyId) }))

# ------------------------------------------ #

# ------------------------------------------ #
@app.route("/speak")
def speak():
    return render_template("speak.html")
# ------------------------------------------ #

@app.route("/information")
def information():
    return render_template("information.html", allInfo = infoData.find())

@app.route("/edit_info_page")
def edit_info_page():
    infoId=request.args.get("infoId", None)
    return render_template("edit_info_page.html", info=infoData.find_one({ '_id': ObjectId(infoId)}))

@app.route('/edit_info', methods=["POST"])
def edit_info():
    infoId=request.args.get('infoId', None)
    infos = infoData
    infos.update( {'_id': ObjectId(infoId)},
    {
        'title':request.form.get('title'),
        'text':request.form.get('text'),
    })
    return redirect(url_for('information'))

@app.route("/new_info")
def new_info():
    return render_template("new_info.html")

@app.route("/publish_info", methods=["POST"])
def publish_info():
    infos=infoData
    infos.insert_one(request.form.to_dict())
    return redirect(url_for('information'))

@app.route("/delete_info")
def delete_info():
    infoId=request.args.get("infoId", None)
    infoData.remove({ "_id": ObjectId(infoId) })
    return redirect(url_for("information"))

# ------------------------------------------ #

@app.route("/login")
def login():
    return render_template("login.html")

# ------------------------------------------ #

# set to False (deploy) set to True (development)
if __name__ == '__main__':
    app.run(debug=True)


# coding: utf-8
from __future__ import unicode_literals

from .common import InfoExtractor
from ..utils import (
    ExtractorError,
    dict_get,
    float_or_none,
    int_or_none,
    remove_end,
    str_or_none,
    try_get,
    url_or_none,
    urljoin,
)

class GettrIE(InfoExtractor):
    _VALID_URL = r'https?://(www\.)?gettr\.com/post/(?P<id>[a-z0-9]+)'
    _MEDIA_BASE_URL = 'https://media.gettr.com/'

    _TESTS = [{
        'url': 'https://www.gettr.com/post/pcf6uv838f',
        'info_dict': {
            'id': 'pcf6uv838f',
            'title': 'md5:9086a646bbd06c41c4fe8e52b3c93454',
            'description': 'md5:be0577f1e4caadc06de4a002da2bf287',
            'ext': 'mp4',
            'uploader': 'EpochTV',
            'uploader_id': 'epochtv',
            'thumbnail': r're:^https?://.+/out\.jpg',
            'timestamp': 1632782451058,
            'duration': 58.5585,
        }
    }, {
        'url': 'https://gettr.com/post/p4iahp',
        'info_dict': {
            'id': 'p4iahp',
            'title': 'md5:b03c07883db6fbc1aab88877a6c3b149',
            'description': 'md5:741b7419d991c403196ed2ea7749a39d',
            'ext': 'mp4',
            'uploader': 'Neues Forum Freiheit',
            'uploader_id': 'nf_freiheit',
            'thumbnail': r're:^https?://.+/out\.jpg',
            'timestamp': 1626594455017,
            'duration': 23,
        }
    }]

    def _real_extract(self, url):
        post_id = self._match_id(url)
        webpage = self._download_webpage(url, post_id)

        api_data = self._download_json(
            'https://api.gettr.com/u/post/%s?incl="poststats|userinfo"' % post_id, post_id)

        post_data = try_get(api_data, lambda x: x['result']['data'])
        user_data = try_get(api_data, lambda x: x['result']['aux']['uinf'][post_data['uid']]) or {}

        if post_data.get('nfound'):
            raise ExtractorError(post_data.get('txt'), expected=True)

        title = description = str_or_none(
            post_data.get('txt') or self._og_search_description(webpage))

        uploader = str_or_none(
            user_data.get('nickname')
            or remove_end(self._og_search_title(webpage), ' on GETTR'))
        if uploader:
            title = '%s - %s' % (uploader, title)

        if not dict_get(post_data, ['vid', 'ovid']):
            raise ExtractorError('There\'s no video in this post.')

        vid = post_data.get('vid')
        ovid = post_data.get('ovid')

        formats = self._extract_m3u8_formats(
            urljoin(self._MEDIA_BASE_URL, vid), post_id, 'mp4',
            entry_protocol='m3u8_native', m3u8_id='hls') if vid else []

        if ovid:
            formats.append({
                'url': urljoin(self._MEDIA_BASE_URL, ovid),
                'format_id': 'ovid',
                'ext': 'mp4',
                'width': int_or_none(post_data.get('vid_wid')),
                'height': int_or_none(post_data.get('vid_hgt')),
                'source_preference': 1,
                'quality': 1,
            })

        self._sort_formats(formats)

        return {
            'id': post_id,
            'title': title,
            'description': description,
            'thumbnail': url_or_none(
                urljoin(self._MEDIA_BASE_URL, post_data.get('main'))
                or self._og_search_thumbnail(webpage)),
            'timestamp': int_or_none(post_data.get('cdate')),
            'uploader_id': str_or_none(
                dict_get(user_data, ['_id', 'username'])
                or post_data.get('uid')),
            'uploader': uploader,
            'formats': formats,
            'duration': float_or_none(post_data.get('vid_dur')),
            'tags': post_data.get('htgs'),
        }


"""
Core decoders that handle manipulation of standard USB packets.

Includes functionality for e.g. breaking packets into transfers / transactions.
"""

import bitstruct

from ..decoder import ViewSBDecoder, UnhandledPacket
from ..packet import USBPacket, MalformedPacket, USBTokenPacket, USBHandshakePacket, \
     USBDataPacket, USBTransaction

class USBPacketSpecializer(ViewSBDecoder):
    """
    Decoder that converts raw USB packets into specialized ones by PID group.
    e.g. can convert a raw USB packet into a TokenPacket, a DataPacket, or etc.
    """

    # Various USB packets size constant.
    TOKEN_PAYLOAD_LENGTH     = 2
    HANDSHAKE_PAYLOAD_LENGTH = 0

    CRC16_LENGTH             = 2
    MAXIMUM_PACKET_PAYLOAD   = 1026

    def can_handle_packet(self, packet):
        return type(packet) == USBPacket

    def _consume_token_packet(self, packet):
        """ Consumes a packet known to be a token packet. """

        fields = packet.__dict__

        # If our packet isn't the right length for a token, emit
        # a malformed packet.
        if len(packet.data) != self.TOKEN_PAYLOAD_LENGTH:
            self.emit_packet(MalformedPacket(**fields))
            return

        # Unpack the fields of the token.
        address, endpoint, crc5 = bitstruct.unpack('u7u4u5', packet.data)
        fields['device_address']  = address
        fields['endpoint_number'] = endpoint

        # Populate a USBTokenPacket with the relevant information...
        new_packet = USBTokenPacket(crc5=crc5, **fields)

        # ... and emit the new packet.
        self.emit_packet(new_packet)

    def _consume_handshake_packet(self, packet):
        """ Consumes a packet known to be a handshake packet. """

        new_packet = USBHandshakePacket(**packet.__dict__)
        self.emit_packet(new_packet)

    def _consume_data_packet(self, packet):
        """ Consumes a packet known to be a data packet. """

        fields = packet.__dict__

        # If our packet doesn't have at least the CRC length, it's malformed.
        # a malformed packet.
        if not (self.CRC16_LENGTH <= len(packet.data) < self.MAXIMUM_PACKET_PAYLOAD):
            self.emit_packet(MalformedPacket(**fields))
            return

        # Copy the original data, but extract the CRC16.
        crc16          = fields['data'][-2:]
        fields['data'] = packet.data[:-2]

        # Populate a USBDataPacket with the relevant information...
        new_packet = USBDataPacket(crc16=crc16, **fields)

        # ... and emit the new packet.
        self.emit_packet(new_packet)

    def consume_packet(self, packet):

        # Convert the packet according to its group.
        if packet.pid.is_token():
            self._consume_token_packet(packet)
        elif packet.pid.is_handshake():
            self._consume_handshake_packet(packet)
        elif packet.pid.is_data():
            self._consume_data_packet(packet)

        # If we didn't have a strategy for consuming the given packet
        # fail out and don't consume the packet.
        else:
            raise UnhandledPacket()

class USBTransactionDecoder(ViewSBDecoder):
    """ Class that groups sequences of packets into Transactions. """

    def __init__(self, analyzer):

        # Start a new list of packets absorbed.
        self.packets_captured = []
        super().__init__(analyzer)

    def can_handle_packet(self, packet):
        return type(packet) in (USBTokenPacket, USBDataPacket, USBHandshakePacket)

    def _last_captured(self):
        if not self.packets_captured:
            return None
        else:
            return self.packets_captured[-1]

    def _first_captured(self):
        if not self.packets_captured:
            return None
        else:
            return self.packets_captured[0]

    def emit_transaction(self, sequence_error=False):

        fields = self._first_captured().__dict__.copy()

        fields['token']     = self._first_captured().pid
        fields['handshake'] = self._last_captured().pid

        if len(self.packets_captured) == 3:
            fields['data_pid'] = self.packets_captured[1].pid
            fields['data'] = self.packets_captured[1].data[:]
        else:
            fields['data_pid'] = None
            fields['data'] = None

        # Move the captured packets into our new transaction; and create a
        # new buffer for future fields.
        fields['subordinate_packets'] = self.packets_captured
        self.packets_captured = []

        self.emit_packet(USBTransaction(**fields))

    def consume_packet(self, packet):

        # Case 1: if this is a token packet, and we've not started a transaction, capture it.
        if isinstance(packet, USBTokenPacket) and not self.packets_captured:
            self.packets_captured.append(packet)

        # Case 2: if this is a data packet, and it's following a token packet, capture it.
        elif isinstance(packet, USBDataPacket) and isinstance(self._last_captured(), USBTokenPacket):
            self.packets_captured.append(packet)

        elif isinstance(packet, USBHandshakePacket) and isinstance(self._first_captured(), USBTokenPacket):
            self.packets_captured.append(packet)
            self.emit_transaction(sequence_error=False)

        else:
            self.packets_captured.append(packet)
            self.emit_transaction(sequence_error=True)


#!/usr/bin/env python

"""Update parameters which directly depends on the dataset.
"""

__author__ = 'Anna Kukleva'
__date__ = 'November 2018'

import os

from utils.arg_pars import opt
from utils.util_functions import update_opt_str
from utils.logging_setup import path_logger

def update():
    opt.dataset_root = '/media/data/kukleva/lab/Breakfast'

    data_subfolder = ['kinetics', 'data', 's1', 'video', 'OPN', 'videovector', 'videovector', 'videodarwin'][opt.data_type]
    opt.data = os.path.join(opt.dataset_root, 'feat', data_subfolder)

    opt.gt = os.path.join(opt.data, opt.gt)

    opt.ext = ['gz', 'gz', 'txt', 'avi', 'txt', 'txt', 'txt', 'txt'][opt.data_type]
    opt.feature_dim = [400, 64, 64, 0, 1024, 4096, 64, 128][opt.data_type]
    opt.subfolder = 'ascii'

    if opt.data_type in [5, 6]:
        # ascii_subsampled ascii_imgnet ascii_target ascii_context
        i = 0 if opt.data_type == 5 else 1
        opt.subfolder = ['ascii_%s', 'ascii_subsampled'][i]
        if i == 0:
            data_type = ['target', 'context'][0]
            resume_iter = [25, 50][1]
            opt.subfolder = opt.subfolder % data_type
            opt.prefix = opt.prefix + '.%s.%d' % (data_type, resume_iter)
        opt.frame_frequency = 5

    if opt.data_type in [4, 5, 6, 7]:
        exception_list = ['P03_webcam02_P03_tea',
                          'P26_webcam02_P26_cereals',
                          'P27_webcam02_P27_coffee',
                          'P34_cam01_P34_coffee',
                          'P51_webcam01_P51_coffee',
                          'P52_stereo01_P52_sandwich']

        for root, dirs, files in os.walk(os.path.join(opt.data, opt.subfolder)):
            if files:
                for filename in files:
                    if 'P51_webcam01_P51_coffee' in filename:
                        print('P51_webcam01_P51_coffee')
                    for exception in exception_list:
                        if exception in filename:
                            try:
                                os.remove(os.path.join(root, filename))
                                os.remove(os.path.join(opt.gt, filename))
                            except FileNotFoundError:
                                pass

    if opt.data_type == 6:
        opt.model_name = 'mlp'

    if opt.data_type in [0, 4, 5, 7]:
        opt.model_name = 'nothing'

    opt.bg = False
    opt.gr_lev = ''

    if opt.all:
        opt.subaction = 'all'

    update_opt_str()

    logger = path_logger()

    vars_iter = list(vars(opt))
    for arg in sorted(vars_iter):
        logger.debug('%s: %s' % (arg, getattr(opt, arg)))


# Copyright 2021 The KubeEdge Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Remodeling tasks based on their relationships

Parameters
----------
mappings ：all assigned tasks get from the `task_mining`
samples : input samples

Returns
-------
models : List of groups which including at least 1 task.
"""

from typing import List

import numpy as np
import pandas as pd

from sedna.datasources import BaseDataSource
from sedna.common.class_factory import ClassFactory, ClassType

__all__ = ('DefaultTaskRemodeling',)

@ClassFactory.register(ClassType.MTL)
class DefaultTaskRemodeling:
    """
    Assume that each task is independent of each other
    """

    def __init__(self, models: list, **kwargs):
        self.models = models

    def __call__(self, samples: BaseDataSource, mappings: List):
        """
        Grouping based on assigned tasks
        """
        mappings = np.array(mappings)
        data, models = [], []
        d_type = samples.data_type
        for m in np.unique(mappings):
            task_df = BaseDataSource(data_type=d_type)
            _inx = np.where(mappings == m)
            if isinstance(samples.x, pd.DataFrame):
                task_df.x = samples.x.iloc[_inx]
            else:
                task_df.x = np.array(samples.x)[_inx]
            if d_type != "test":
                if isinstance(samples.x, pd.DataFrame):
                    task_df.y = samples.y.iloc[_inx]
                else:
                    task_df.y = np.array(samples.y)[_inx]
            task_df.inx = _inx[0].tolist()
            if samples.meta_attr is not None:
                task_df.meta_attr = np.array(samples.meta_attr)[_inx]
            data.append(task_df)
            model = self.models[m] or self.models[0]
            models.append(model)
        return data, models


import abc
import csv
import enum
import logging
import re
from typing import ClassVar, Sequence

from taxes.receipts.models import PaymentMethod
from taxes.receipts.types import (
    RawTransaction,
    RawTransactionGenerator,
    TransactionType,
)
from taxes.receipts.util.datetime import parse_date
from taxes.receipts.util.currency import parse_amount

LOGGER = logging.getLogger(__name__)

class CommonColumn(enum.Enum):
    TRANSACTION_DATE = "transaction_date"
    AMOUNT = "amount"
    DESCRIPTION = "description"

class ParseException(Exception):
    def __init__(self, *args, line_number=None, **kwargs):
        self.line_number = line_number
        super().__init__(*args, **kwargs)

    def __repr__(self):
        prefix = f"{self.line_number} " if self.line_number else ""
        return prefix + super().__repr__()

class TextFileLineFilter(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def is_accepted(self, line):
        pass

class NonEmptyLinesFilter(TextFileLineFilter):
    def is_accepted(self, line):
        return line.strip() != ""

class SkipPatternsFilter(TextFileLineFilter):
    def __init__(self, patterns):
        self.patterns = [re.compile(pattern) for pattern in patterns]

    def is_accepted(self, line):
        for pattern in self.patterns:
            if pattern.match(line):
                return False
        return True

class TextFileIterator:
    def __init__(self, file):
        self.file = file
        self._line_num = 0

    def __iter__(self):
        self._line_num = 0
        for line in self.file.readlines():
            self._line_num += 1
            yield line

    @property
    def line_num(self):
        return self._line_num

class BaseTransactionParser(metaclass=abc.ABCMeta):
    QUOTE_CHAR: ClassVar[str] = '"'
    LINE_FILTERS: ClassVar[Sequence[TextFileLineFilter]] = []
    CSV_FIELDS: ClassVar[Sequence[str]]  # must be specified by derived class
    TRANSACTION_DATE_FORMAT: ClassVar[str] = "%m/%d/%Y"

    """
    Base class for parsing transaction files from financial institutions
    """

    def __init__(self, payment_method: PaymentMethod):
        self.payment_method = payment_method
        self._failures = 0
        if not self.CSV_FIELDS:
            raise RuntimeError("CSV_FIELDS not specified in derived class")

    def parse(self, filename: str) -> RawTransactionGenerator:
        self._failures = 0
        with open(filename, mode="r", encoding="utf-8-sig") as csv_file:
            raw_iter_lines = TextFileIterator(csv_file)
            iter_filtered_lines = filter(
                lambda line: all((f.is_accepted(line) for f in self.LINE_FILTERS)),
                raw_iter_lines,
            )
            iter_rows = csv.DictReader(
                iter_filtered_lines,
                fieldnames=self.CSV_FIELDS,
                quotechar=self.QUOTE_CHAR,
            )
            for row in iter_rows:
                try:
                    yield self.parse_row(row, raw_iter_lines.line_num)
                except Exception:
                    LOGGER.error(
                        "FAILURE on line %d of file %s",
                        raw_iter_lines.line_num,
                        filename,
                    )
                    self._failures += 1
                    raise

    @abc.abstractmethod
    def parse_row(self, row: dict, line_number: int) -> RawTransaction:
        pass

    @property
    def failures(self) -> int:
        return self._failures

    def _make_transaction(
        self, row: dict, line_number: int, misc: dict, amount: int = None
    ) -> RawTransaction:
        return RawTransaction(
            line_number=line_number,
            transaction_date=parse_date(
                row[CommonColumn.TRANSACTION_DATE.value], self.TRANSACTION_DATE_FORMAT
            ),
            amount=amount if amount else parse_amount(row[CommonColumn.AMOUNT.value]),
            currency=self.payment_method.currency,
            description=row[CommonColumn.DESCRIPTION.value].strip(),
            misc=misc,
            payment_method=self.payment_method,
        )

class BMOCSVBankAccountParser(BaseTransactionParser):
    QUOTE_CHAR = "'"
    LINE_FILTERS = [
        NonEmptyLinesFilter(),
        SkipPatternsFilter(
            [r"^Following data is valid as of.*", r"^First Bank Card.*"]
        ),
    ]
    CSV_FIELDS = [
        "card_number",
        "transaction_type",
        CommonColumn.TRANSACTION_DATE.value,
        CommonColumn.AMOUNT.value,
        "consolidated_description",
    ]
    TRANSACTION_DATE_FORMAT = "%Y%m%d"
    DESCRIPTION_PARSER = re.compile(r"^\[([A-Z]{2})\](.*)$")

    def parse_row(self, row: dict, line_number: int) -> RawTransaction:
        match = self.DESCRIPTION_PARSER.search(row["consolidated_description"])
        if not match:
            raise ParseException("Unable to parse line", line_number=line_number)
        transaction_code, description = match.groups()

        row[CommonColumn.DESCRIPTION.value] = description
        return self._make_transaction(
            row,
            line_number,
            {
                "last_4_digits": row["card_number"][-4:],
                "transaction_code": transaction_code,
            },
        )

class BMOCSVCreditParser(BaseTransactionParser):
    QUOTE_CHAR = "'"
    LINE_FILTERS = [
        NonEmptyLinesFilter(),
        SkipPatternsFilter([r"^Following data is valid as of.*", r"^Item #.*"]),
    ]
    CSV_FIELDS = [
        "item_number",
        "card_number",
        CommonColumn.TRANSACTION_DATE.value,
        "posting_date",
        CommonColumn.AMOUNT.value,
        CommonColumn.DESCRIPTION.value,
    ]
    TRANSACTION_DATE_FORMAT = "%Y%m%d"

    def parse_row(self, row: dict, line_number: int) -> RawTransaction:
        # all postive values are debited as expenses
        amount = -1 * parse_amount(row[CommonColumn.AMOUNT.value])

        return self._make_transaction(
            row, line_number, {"last_4_digits": row["card_number"][-4:],}, amount=amount
        )

class MBNAMastercardParser(BaseTransactionParser):
    LINE_FILTERS = [SkipPatternsFilter([r"^Posted Date,Payee.*"])]
    CSV_FIELDS = [
        CommonColumn.TRANSACTION_DATE.value,
        CommonColumn.DESCRIPTION.value,
        "address",
        CommonColumn.AMOUNT.value,
    ]
    TRANSACTION_DATE_FORMAT = "%m/%d/%Y"

    def parse_row(self, row: dict, line_number: int) -> RawTransaction:
        return self._make_transaction(row, line_number, {})

class CapitalOneMastercardParser(BaseTransactionParser):
    LINE_FILTERS = [SkipPatternsFilter([r"^Transaction Date.*"])]
    CSV_FIELDS = [
        CommonColumn.TRANSACTION_DATE.value,
        "posted_date",
        "card_number",
        CommonColumn.DESCRIPTION.value,
        "category",
        "debit",
        "credit",
    ]
    TRANSACTION_DATE_FORMAT = "%Y-%m-%d"
    CATEGORY_TO_TRANSACTION_TYPE = {
        "Car Rental": TransactionType.TRAVEL,
        "Dining": TransactionType.MEALS,
        "Entertainment": None,
        "Gas/Automotive": TransactionType.TRAVEL,
        "Health Care": None,
        "Insurance": TransactionType.INSURANCE,
        "Internet": None,
        "Merchandise": None,
        "Other Services": None,
        "Other Travel": TransactionType.TRAVEL,
        "Payment/Credit": None,
        "Phone/Cable": TransactionType.TELEPHONE,
    }

    def parse_row(self, row: dict, line_number: int) -> RawTransaction:
        debit_amount = parse_amount(row["debit"] or "0.0")
        credit_amount = parse_amount(row["credit"] or "0.0")
        raw_category = row["category"]
        if raw_category not in self.CATEGORY_TO_TRANSACTION_TYPE:
            raise ParseException(
                f"Unrecognized category: {raw_category}", line_number=line_number
            )

        return self._make_transaction(
            row,
            line_number,
            {
                "last_4_digits": row["card_number"],
                "raw_category": raw_category,
                "tax_transaction_type": self.CATEGORY_TO_TRANSACTION_TYPE.get(
                    raw_category
                ),
            },
            amount=credit_amount - debit_amount,
        )

class ChaseVisaParser(BaseTransactionParser):
    LINE_FILTERS = [SkipPatternsFilter([r"^Transaction Date,Post Date.*"])]
    CSV_FIELDS = [
        CommonColumn.TRANSACTION_DATE.value,
        "posted_date",
        CommonColumn.DESCRIPTION.value,
        "category",
        "type",
        CommonColumn.AMOUNT.value,
    ]
    CATEGORY_TO_TRANSACTION_TYPE = {
        "Automotive": TransactionType.TRAVEL,
        "Bills & Utilities": TransactionType.UTILITY,
        "Education": None,
        "Entertainment": None,
        "Food & Drink": TransactionType.MEALS,
        "Gas": TransactionType.TRAVEL,
        "Gifts & Donations": TransactionType.DONATION,
        "Groceries": TransactionType.MEALS,
        "Health & Wellness": None,
        "Home": None,
        "Personal": None,
        "Professional Services": TransactionType.PROFESSIONAL_SERVICES,
        "Shopping": None,
        "Travel": TransactionType.TRAVEL,
        "Fees & Adjustments": TransactionType.ADMINISTRATIVE,
    }

    def parse_row(self, row: dict, line_number: int) -> RawTransaction:
        raw_category = row["category"]
        if not raw_category:
            transaction_type = None
        elif raw_category not in self.CATEGORY_TO_TRANSACTION_TYPE:
            raise ParseException(
                f"Unrecognized category: {raw_category}", line_number=line_number
            )
        else:
            transaction_type = self.CATEGORY_TO_TRANSACTION_TYPE.get(raw_category)

        return self._make_transaction(
            row,
            line_number,
            {
                "raw_category": raw_category,
                "tax_transaction_type": transaction_type,
                "type": row["type"],
            },
        )

class WellsFargoParser(BaseTransactionParser):
    LINE_FILTERS = [
        NonEmptyLinesFilter(),
    ]
    CSV_FIELDS = [
        CommonColumn.TRANSACTION_DATE.value,
        CommonColumn.AMOUNT.value,
        "unknown_0",
        "check_number",
        CommonColumn.DESCRIPTION.value,
    ]

    def __init__(self, *args):
        super().__init__(*args)
        self.authorized_purchase_pattern = re.compile(
            r"^PURCHASE AUTHORIZED ON (?P<authorized_date>\d{2}/\d{2}) (?P<party>.+)$"
        )

    def parse_row(self, row: dict, line_number: int) -> RawTransaction:
        # extract real payee if preauthorized payment
        misc = {}
        preauthorized_match = self.authorized_purchase_pattern.match(
            row[CommonColumn.DESCRIPTION.value]
        )
        if preauthorized_match:
            row[CommonColumn.DESCRIPTION.value] = preauthorized_match.group("party")
            misc["authorized_purchase_on"] = preauthorized_match.group(
                "authorized_date"
            )

        return self._make_transaction(row, line_number, misc)

class SchwabRealizedGainsParser(BaseTransactionParser):
    LINE_FILTERS = [
        NonEmptyLinesFilter(),
        SkipPatternsFilter([r"^Realized Gain/Loss for.*"]),
        SkipPatternsFilter([r'^"Symbol","Name",.*']),
        SkipPatternsFilter([r'^"Total",.*']),
    ]
    # pylint:disable=no-member
    CSV_FIELDS = [
        CommonColumn.DESCRIPTION.value,
        "Name",
        CommonColumn.TRANSACTION_DATE.value,
        "Quantity",
        "Proceeds",
        "CostBasis",
        CommonColumn.AMOUNT.value,
        "Total Gain/Loss (%)",
        TransactionType.LONG_TERM_CAPITAL_GAINS.value,
        "Long Term Gain/Loss (%)",
        TransactionType.SHORT_TERM_CAPITAL_GAINS.value,
        "Short Term Gain/Loss (%)",
        "Wash Sale?",
        "Disallowed Loss",
        "Transaction Closed Date",
        "Transaction Cost Basis",
        "Total Transaction Gain/Loss ($)",
        "Total Transaction Gain/Loss (%)",
        "LT Transaction Gain/Loss ($)",
        "LT Transaction Gain/Loss (%)",
        "ST Transaction Gain/Loss ($)",
        "ST Transaction Gain/Loss (%)",
    ]
    # pylint:enable=no-member

    def parse_row(self, row: dict, line_number: int) -> RawTransaction:
        misc = {"tax_transaction_type": TransactionType.CAPITAL_GAINS}
        for gains_type in [
            TransactionType.SHORT_TERM_CAPITAL_GAINS,
            TransactionType.LONG_TERM_CAPITAL_GAINS,
        ]:
            gain_type = gains_type.value  # pylint:disable=no-member
            amount_str = row.get(gain_type, "").rstrip("--")
            if amount_str:
                misc[gain_type] = parse_amount(amount_str)

        return self._make_transaction(row, line_number, misc)


import pandas as pd
import numpy as np
import networkx as nx

# 顯示所有的行
pd.set_option('display.max_columns', None)
# 顯示所有的列
pd.set_option('display.max_rows', None)
# 顯示長度 ＝ 100 ,預設50
pd.set_option('max_colwidth',100)

arr = []
ev_msg_body = [429,1,2,429,1,2,3,429,1,2]
df = pd.DataFrame(columns=['switch_id', 'live_port', 'hw_addr'])
edges = pd.DataFrame({'source' : [0, 1],
                      'target' : [1, 2],
                      'weight' : [100, 50]})

a = pd.Series(1)
b = pd.Series(2)
c=[1,2,3]
d=[4,5,6]
i= 1

# df=df.append({'switch_id':1}, ignore_index=True)
# df=df.append({'live_port':4294967}, ignore_index=True)
# df=df.append({'hw_addr':'2e:f2:9f:aa:62:4d:'}, ignore_index=True)
# df=df.append({'live_port':1, 'hw_addr':'e2:64:98:d2:8b:d7'}, ignore_index=True)
# df=df.append({'live_port':2, 'hw_addr':'d2:03:07:ee:ad:a4:'}, ignore_index=True)
# df=df.append({'switch_id':3}, ignore_index=True)

df=df.append({'switch_id':1, 'live_port':4294967, 'hw_addr':'2e:f2:9f:aa:62:4d:'}, ignore_index=True)
df=df.append({'switch_id':1, 'live_port':1, 'hw_addr':'e2:64:98:d2:8b:d7'}, ignore_index=True)
df=df.append({'switch_id':1, 'live_port':2, 'hw_addr':'d2:03:07:ee:ad:a4:'}, ignore_index=True)

df=df.append({'switch_id':2, 'live_port':4294967, 'hw_addr':'de:7e:b4:a7:f0:42'}, ignore_index=True)
df=df.append({'live_port':1, 'hw_addr':'26:50:fb:81:1f:df'}, ignore_index=True)
df=df.append({'live_port':2, 'hw_addr':'56:7c:81:82:c1:07'}, ignore_index=True)
# df=df.append({'live_port':3, 'hw_addr':'12:fb:02:12:67:77'}, ignore_index=True)
# df=df.append({'live_port':4, 'hw_addr':'6a:ec:db:25:c0:f1'}, ignore_index=True)

# df=df.append({'switch_id':3, 'live_port':4294967, 'hw_addr':'7a:5f:99:46:04:4d'}, ignore_index=True)
# df=df.append({'live_port':1, 'hw_addr':'ee:fe:46:c4:29:01'}, ignore_index=True)
# df=df.append({'live_port':4, 'hw_addr':'8a:6d:2f:7e:a1:ac'}, ignore_index=True)
# df=df.append({'live_port':2, 'hw_addr':'9a:4c:7e:21:31:38'}, ignore_index=True)
# df=df.append({'live_port':3, 'hw_addr':'ae:1a:9b:8f:72:99'}, ignore_index=True)

# print(df)
# print(df.isnull().any(axis = 0))
df['switch_id'] = df['switch_id'].fillna(method='ffill')

# print(df[df['hw_addr'].isnull() & df['live_port'].notnull()])
df1 = df[df['hw_addr'].isnull() & df['live_port'].notnull()]
# print(df[df['hw_addr'].notnull() & df['live_port'].isnull()])
df2 = df[df['hw_addr'].notnull() & df['live_port'].isnull()]

df1.set_index(df2.index, inplace=True)
# print(df1)
# print(df1.index)
# print(df2)
# print(df2.index)

df2 = df2.combine_first(df1)
# print(df2.combine_first(df1))
df = df.combine_first(df2)
# print(df)
df = df.dropna(axis=0).reset_index().drop(columns='index')
# print(df)

# df=df.append({'switch_id':4, 'live_port':8}, ignore_index=True)
# df=df.append({'switch_id':5}, ignore_index=True)
# df=df.append({'switch_id':6, 'live_port':[7,8,9]}, ignore_index=True)
# df.iloc[-1,-1] = 666
# df.ix[:, 'live_port'] = 456
# print('df.loc[0,live_port]: ', df.loc[0,'live_port'])
# print('原本的dataframe: ')
# print(df)
#
# if df.isnull().loc[1, 'live_port']:
#     print('isnull: ', df.isnull().loc[1, 'live_port'])
#     # df.loc[2,'live_port'] = [frozenset(c)] # frozenset()存不可變序列
#     # df._set_value(1,'live_port', c) # 存可變序列
#     df.loc[i, 'live_port'] = [1,2,3]
#     df.loc[2, 'live_port'] = d
#
# # df = df['switch_id'].append([2])
# # df = df['switch_id'].append([3])
#
# print('後來的dataframe: ')
# print(df)

# print(edges)

import networkx as nx
net = nx.DiGraph()

src_dpid = 1
dst_dpid = 3
port = 2

links=[(src_dpid, dst_dpid, {'port': port})]
net.add_edges_from(links)

net.add_node('aa:bb:cc')
net.add_edge(1, 'aa:bb:cc', attr_dict={'port': 3})
net.add_edge('aa:bb:cc', 1)

# self.network.add_node(src)
# self.network.add_edge(dpid, src, attr_dict={'port': in_port})
# self.network.add_edge(src, dpid)
# print(net.edges)

tmp2_df = pd.DataFrame(columns=['request_sid', 'request_port', 'receive_sid', 'receive_port'])
tmp2_df=tmp2_df.append({'request_sid': 1, 'request_port': 2, 'receive_sid': 2, 'receive_port': 2}, ignore_index=True)
tmp2_df=tmp2_df.append({'request_sid': 2, 'request_port': 2, 'receive_sid': 1, 'receive_port': 2}, ignore_index=True)
# tmp2_df=tmp2_df.append({'request_sid': 3, 'request_port': 2, 'receive_sid': 2, 'receive_port': 2}, ignore_index=True)
# tmp2_df=tmp2_df.append({'request_sid': 3, 'request_port': 1, 'receive_sid': 1, 'receive_port': 3}, ignore_index=True)
# tmp2_df=tmp2_df.append({'request_sid': 2, 'request_port': 1, 'receive_sid': 1, 'receive_port': 2}, ignore_index=True)
# tmp2_df=tmp2_df.append({'request_sid': 3, 'request_port': 3, 'receive_sid': 4, 'receive_port': 4}, ignore_index=True)
# tmp2_df=tmp2_df.append({'request_sid': 1, 'request_port': 4, 'receive_sid': 4, 'receive_port': 3}, ignore_index=True)
# tmp2_df=tmp2_df.append({'request_sid': 3, 'request_port': 4, 'receive_sid': 5, 'receive_port': 3}, ignore_index=True)
# tmp2_df=tmp2_df.append({'request_sid': 2, 'request_port': 3, 'receive_sid': 5, 'receive_port': 2}, ignore_index=True)
# tmp2_df=tmp2_df.append({'request_sid': 4, 'request_port': 5, 'receive_sid': 5, 'receive_port': 4}, ignore_index=True)

print(df)
print(tmp2_df)
# print(df.info())
# print(tmp2_df.info())
# for row in tmp2_df.itertuples():
#     print(getattr(row, 'request_sid'))
    # if (df['switch_id'] == getattr(row, 'request_sid')) & (df['live_port'] == getattr(row, 'request_port')):
    # if (df.iloc[0] == getattr(row, 'request_sid')) & (df.iloc[1] == getattr(row, 'request_port')):
    # if (df.loc['switch_id'] == 1):
    #     print('test')
# print(tmp2_df.request_sid)
# print(tmp2_df[tmp2_df['request_sid'] == 1])
df3 = df.copy()
# for i, o in tmp2_df.request_sid, tmp2_df.request_port:
#     print(i, o)
#     print(df3[(df3['switch_id'] == i) & (df3['live_port'] == o)])

for i in range(0, len(tmp2_df), 1):
    print(df3[(df3['switch_id'] == tmp2_df.at[i, 'request_sid']) & (df3['live_port'] == tmp2_df.at[i, 'request_port'])])
    request_index = df3[(df3['switch_id'] == tmp2_df.at[i, 'request_sid']) & (df3['live_port'] == tmp2_df.at[i, 'request_port'])].index
    receive_index = df3[(df3['switch_id'] == tmp2_df.at[i, 'receive_sid']) & (df3['live_port'] == tmp2_df.at[i, 'receive_port'])].index
    controller_index = df3[(df3['live_port'] > 429)].index
    # print(type(index))
    df3.drop(index=request_index, inplace=True)
    df3.drop(index=receive_index, inplace=True)
    df3.drop(index=controller_index, inplace=True)
    df3.reset_index(drop=True, inplace=True)
print(df3)

# print(df[(df['switch_id'] == tmp2_df['request_sid'])])
# print(df.loc[(df['switch_id'] == 1) & (df['live_port'] == 1)])
# print(pd.concat([df, tmp2_df], axis=0))

# tmp  = dict({3: {3: [3], 1: [3, 1], 5: [3, 5], 4: [3, 4], 2: [3, 2]}, 1: {1: [1], 3: [1, 3], 4: [1, 4], 2: [1, 2], 5: [1, 3, 5]}, 5: {5: [5], 3: [5, 3], 4: [5, 4], 2: [5, 2], 1: [5, 3, 1]}, 4: {4: [4], 3: [4, 3], 5: [4, 5], 1: [4, 1], 2: [4, 3, 2]}, 2: {2: [2], 1: [2, 1], 3: [2, 3], 5: [2, 5], 4: [2, 1, 4]}}
# )
# print('原始networkx所有資料: ', tmp)
# print('資料的所有key: ', tmp.keys())
# print('資料的所有values: ', tmp.values())
# print('資料key = 3的所有 資料: ', tmp[3])
# print('資料key = 3的所有key: ', tmp[3].keys())
# print('資料key = 3的所有資料', tmp[3].values())

# tmp_df = pd.DataFrame(tmp[3].values())
# print(tmp_df)
# tmp_df = pd.DataFrame(tmp)
# print('原始資料塞進dataframe: \n', tmp_df)
# print(tmp_df.loc[3])
# for m in tmp_df.index:
#     for i in tmp_df.loc[m]:
#         if len(i) == 2:
#             print(type(i), i[0], i[1])
#             # print(tmp2_df[(tmp2_df['request_sid'] == i[0]) & (tmp2_df['receive_sid'] == i[1]) |
#             #      (tmp2_df['request_sid'] == i[1]) & (tmp2_df['receive_sid'] == i[0])].iloc[0, 1])
#             # print(tmp2_df[(tmp2_df['request_sid'] == i[0]) & (tmp2_df['receive_sid'] == i[1]) |
#             #               (tmp2_df['request_sid'] == i[1]) & (tmp2_df['receive_sid'] == i[0])].iloc[0, 3])
#         if len(i) == 3:
#             print(type(i), i[0], i[1], i[2])
#         #     print(tmp2_df[(tmp2_df['request_sid'] == i[0]) & (tmp2_df['receive_sid'] == i[1]) |
#         #          (tmp2_df['request_sid'] == i[1]) & (tmp2_df['receive_sid'] == i[0])].iloc[0, 3])

# print(tmp_df.loc[[3]])
# print(type(tmp_df.loc[3]), type(tmp_df.loc[[3]]))

# # 轉成上三角矩陣
# m,n = tmp_df.shape
# tmp_df[:] = np.where(np.arange(m)[:,None] >= np.arange(n),np.nan,tmp_df)
# # 轉成完整矩陣
# tmp_df = tmp_df.stack().reset_index()
# tmp_df.columns = ['start_sid', 'end_sid', 'link']
# print(tmp_df)
#
# for i in range(0, len(tmp_df), 1):
#     test = tmp_df.loc[i, 'link']
#     print('第一個for: ', tmp_df.loc[i, 'link'])
#     # print(test[n:n+2])
#     n = 2
#     for link in [test[i:i + n] for i in range(0, len(test), 1)]:
#         if len(link)%2 == 0:
#             print('第二個for: ', link)
#             print('第二個for: ', link[0], link[1])


import discord
from discord.ext import commands

class Edit:

    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def edit(self, ctx, *, username: str):
        await self.bot.user.edit(username=username)
        await ctx.send(f'Changed username to {username}.')

def setup(bot):
    bot.add_cog(Edit(bot))