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()


#Kwamae Delva
# code to read in and plot Flux vs Velocity & Wavelength for JO825 for 4 ions
#Future steps: add title to all plots, add rest of galaxies and ions

import os
import numpy as np
from astropy.io import fits
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from matplotlib.ticker import AutoMinorLocator
from astropy import constants as const
from astropy import units as u

dir = os.environ['SDSSDIR']
file = dir + 'spec-0761-54524-0409.fits'
hdulist = fits.open(file)
hdulist.info()
coeff0 = hdulist[0].header['COEFF0']
coeff1 = hdulist[0].header['COEFF1']

flux = hdulist[1].data

npix = len(flux)
index = np.arange(npix)
wavelength = 10.**(coeff0 + coeff1*index)

# speed of light in Angstroms per second
c = c = const.c.to('AA/s')
minorLocator = AutoMinorLocator()

wavelength_of_interest = ('Fe2600', 'MgII2796', 'MgII2803', 'MgI2852')
z = .60329
rest_wavelength = np.array([2600, 2796, 2803, 2852])

# Observed Wavelength
obs_wavelength = np.array([])
for i in range(0,len(rest_wavelength)):
    wavelength_calc = rest_wavelength[i] * (1 + z)
    obs_wavelength = np.append(obs_wavelength, wavelength_calc)

#Observed Velocity
for i in range(0,len(rest_wavelength)):
    velocity_calc = (obs_wavelength- rest_wavelength[i]) * c / rest_wavelength[i]
obs_velocity = np.array([velocity_calc.to('km/s')])

#velocity conversion
for i in range(0, len(wavelength)):
    vel_big = ((wavelength*(1+z)) - wavelength[i]) * c / wavelength[i]

# convert to [km / s]
vel_big_graph = np.array([vel_big.to('km/s')])

# define a function to plot an absorption line profile
def sdss_plot_wavelength(wavelengths, name):

    # define parameters for the x and y axes
    ax.set_xlim(xmin, xmax)
    ax.xaxis.set_minor_locator(minorLocator)
    ax.tick_params(axis='x', labelsize=7)
    ax.set_ylim(0., 15.)
    # make the plot
    ax.plot(wavelength, flux)
    # include the name of the line
    plt.text(xmin+0.03*(xmax-xmin), 0.15, name)
    # mark the approximate centroid velocity
    plt.axvline(x=wavelengths, ymin=0., ymax = 15., linewidth=1, color='k', linestyle='dotted')
    plt.axvline(x=wavelengths + 30., ymin=0., ymax = 15., linewidth=0.5, color='k')
    plt.axvline(x=wavelengths - 30., ymin=0., ymax = 15., linewidth=0.5, color='k')

def sdss_plot_velocity(wave, interest_wave, flux, name, target):

    # define the velocity scale
    vel_aas = (wave/(1+z) - interest_wave) * c / interest_wave
    # convert to [km / s]
    vel_kms = vel_aas.to('km/s')
    #define parameters for the x and y axes
    ax.set_xlim(xmin, xmax)
    ax.xaxis.set_minor_locator(minorLocator)
    ax.tick_params(axis='x', labelsize=7)
    ax.set_ylim(0., 15.)
    # make the plot
    ax.plot(vel_kms, flux)
    # include the name of the line
    plt.text(xmin+0.03*(xmax-xmin), 0.15, name)
    # mark the approximate velocities of interest
    plt.axvline(x=target, ymin=0., ymax = 15., linewidth=1, color='k', linestyle='dotted')
    plt.axvline(x=target +30., ymin=0., ymax = 15., linewidth=0.5, color='k')
    plt.axvline(x=target -30., ymin=0., ymax = 15., linewidth=0.5, color='k')

filename = 'J0826_sdss.pdf'
with PdfPages(filename) as pdf:

#Big Graph

    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    ax.plot(wavelength, flux)
    ax.set_ylim(0., 15.)
    for i in range(0, len(obs_wavelength)):
        # mark the observed wavelengths of interest
        plt.axvline(x=obs_wavelength[i], ymin=0., ymax = 15., linewidth=1, color='k', linestyle='dotted')
        plt.axvline(x=obs_wavelength[i]+30., ymin=0., ymax = 15., linewidth=0.5, color='k')
        plt.axvline(x=obs_wavelength[i]-30., ymin=0., ymax = 15., linewidth=0.5, color='k')
    plt.xlabel('Wavelength',fontsize=14)
    plt.ylabel('Flux',fontsize=14)
    pdf.savefig()
    plt.close()

#Big Graph Based on Velocity

    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    ax.plot(vel_big_graph[0], flux)
    ax.set_ylim(0., 15.)
    for i in range(0, len(obs_velocity)):
        # mark the observed wavelengths of interest
        plt.axvline(x=obs_velocity[i].all(), ymin=0., ymax = 15., linewidth=1, color='k', linestyle='dotted')
        plt.axvline(x=(obs_velocity[i]+30.).all(), ymin=0., ymax = 15., linewidth=0.5, color='k')
        plt.axvline(x=(obs_velocity[i]-30.).all(), ymin=0., ymax = 15., linewidth=0.5, color='k')
    plt.xlabel('Velocity (observed)',fontsize=14)
    plt.ylabel('Flux',fontsize=14)
    pdf.savefig()
    plt.close()

#Graphs of Wavelengths of Interest

    for i in range(0, len(wavelength_of_interest)):
        fig = plt.figure()
        ax = fig.add_subplot(1,1,1)

        xmin = obs_wavelength[i] - 100
        xmax = obs_wavelength[i] + 100
        ax.plot(wavelength, flux)
        plt.xlabel('Wavelength',fontsize=14)
        plt.ylabel('Flux',fontsize=14)
        ax.set_ylim(0., 15.)
        plt.suptitle(wavelength_of_interest[i])
        sdss_plot_wavelength(obs_wavelength[i], wavelength_of_interest[i])

        pdf.savefig()
        plt.close()

#Graphs of the Velocities of the Wavelengths of Interest

    for i in range(0, len(wavelength_of_interest)):
        fig = plt.figure()
        ax = fig.add_subplot(1,1,1)

        xmin = -3000
        xmax = 500

        plt.xlabel('Velocity km/s (observed)',fontsize=14)
        plt.ylabel('Flux',fontsize=14)

        plt.suptitle(wavelength_of_interest[i])
        sdss_plot_velocity(wavelength,rest_wavelength[i], flux, wavelength_of_interest[i], -1600.)

        pdf.savefig()
        plt.close()

    os.system("open %s &" % filename)


from util import *
from torch.nn import Module
from model.layers import *

class Encoder(Module):
    def __init__(self, layer, N):
        super(Encoder, self).__init__()
        self.layers = clones(layer, N)
        self.norm = nn.LayerNorm(layer.size, eps=1e-6)

    def forward(self, x, mask):
        for layer in self.layers:
            x = layer(x, mask)
        return self.norm(x)

class Decoder(Module):
    def __init__(self, layer, N):
        super(Decoder, self).__init__()
        self.layers = clones(layer, N)
        self.norm = nn.LayerNorm(layer.size, eps=1e-6)

    def forward(self, tgt, memory, src_mask, tgt_mask):

        for layer in self.layers:
            x = layer(tgt, memory, tgt_mask, src_mask)
        return self.norm(x)

class Encoder_Decoder(nn.Module):
    def __init__(self, encoder_layer, decoder_layer, N):
        super(Encoder_Decoder, self).__init__()
        self.encoder_layers = clones(encoder_layer, N)
        self.decoder_layers = clones(decoder_layer, N)
        self.norm = nn.LayerNorm(encoder_layer.size, eps=1e-6)
        self.N = N

    def forward(self, src, src_mask, tgt, tgt_mask):
        for n in range(self.N):
            src = self.encoder_layers[n](src, src_mask)
            tgt = self.decoder_layers[n](tgt, src, src_mask, tgt_mask)
        return self.norm(tgt)


# Was going to be used for renaming mxd's by adding a standard text bit that could be removed later. The purpose
#   was to copy the originals, which had save issues and other issues, put in a new location. These were to be used
#   in testing and analysis and manipulation and then were to replace the originals.
# We changed our angle of attack and this was never carried out

import os
import shutil

old_dir_root = r"D:\MXD"
new_dir_root = r"D:\MXD\iMAP_3_copies"
directory_names = None
mxd_paths_dict = {}
for dir, dirnames, files in os.walk(r"D:\MXD"):
    directory_names = dirnames
    break

for name in directory_names:
    newpath = os.path.join(new_dir_root,name)
    if os.path.exists(newpath):
        pass
    else:
        os.mkdir(newpath)

for name in directory_names:
    oldpath = os.path.join(old_dir_root,name)
    for dir, dirnames, files in os.walk(oldpath):
        for file in files:
            if file[-3:] == "mxd":
                new_filename = file
                temp = os.path.join(os.path.join(new_dir_root,name), new_filename)
                mxd_paths_dict[os.path.join(os.path.join(old_dir_root,name), file)] = temp

for key, value in mxd_paths_dict.items():
    shutil.copy(key,value)


#!/usr/bin/env python
# СМОДЗУ
# Михаил Рыбаков <rma.10@uni-dubna.ru>
# 17.11.2015

import numpy
import math
import matplotlib.pyplot as plt

# Распределение Пуассона

lam = 5
size = 1000

print('Input lamda = {}, N = {}'.format(lam, size))

dist = numpy.random.poisson(lam, size)

count, bins, ignored = plt.hist(dist)

# вычисление оценки lambda
lam_est = 1 / size * sum(dist)

print('Lambda est = {}'.format(lam_est))

plt.show()


import os
import datetime
import webapp2

from google.appengine.ext import ndb
from google.appengine.ext.webapp import template

from core import utils

class Warning(ndb.Model):
  message = ndb.StringProperty()
  file = ndb.StringProperty()
  time = ndb.DateTimeProperty()

class WarningsPage(webapp2.RequestHandler):
  def get(self):
    warnings = Warning.query().order(-Warning.time).fetch(15)

    for item in warnings:
      delta_milliseconds = utils.delta_milliseconds(item.time)
      item.relative_time = utils.get_relative_time(delta_milliseconds)

    template_values = {
      'warnings_list': warnings
    }

    template_path = os.path.join(os.path.dirname(__file__), '../templates/admin/warnings.min.html')
    self.response.out.write(template.render(template_path, template_values))

app = webapp2.WSGIApplication([
    ('/admin/warnings.html', WarningsPage),
], debug=True)


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

APPNAME = 'kodo_relay_simulations'
VERSION = '1.0.0'

def recurse_helper(ctx, name):
    if not ctx.has_dependency_path(name):
        ctx.fatal('Load a tool to find %s as system dependency' % name)
    else:
        p = ctx.dependency_path(name)
        ctx.recurse([p])

def options(opt):

    import waflib.extras.wurf_dependency_bundle as bundle
    import waflib.extras.wurf_dependency_resolve as resolve
    import waflib.extras.wurf_configure_output

    bundle.add_dependency(opt,
        resolve.ResolveGitMajorVersion(
            name = 'waf-tools',
            git_repository = 'github.com/steinwurf/external-waf-tools.git',
            major_version = 2))

    bundle.add_dependency(opt,
        resolve.ResolveGitMajorVersion(
            name = 'gtest',
            git_repository = 'github.com/steinwurf/external-gtest.git',
            major_version = 2))

    bundle.add_dependency(opt,
        resolve.ResolveGitMajorVersion(
            name = 'boost',
            git_repository = 'github.com/steinwurf/external-boost-light.git',
            major_version = 1))

    bundle.add_dependency(opt,
        resolve.ResolveGitMajorVersion(
            name = 'sak',
            git_repository = 'github.com/steinwurf/sak.git',
            major_version = 10))

    bundle.add_dependency(opt,
        resolve.ResolveGitMajorVersion(
            name = 'fifi',
            git_repository = 'github.com/steinwurf/fifi.git',
            major_version = 10))

    bundle.add_dependency(opt,
        resolve.ResolveGitMajorVersion(
            name = 'gauge',
            git_repository = 'github.com/steinwurf/cxx-gauge.git',
            major_version = 7))

    bundle.add_dependency(opt,
        resolve.ResolveGitMajorVersion(
            name = 'tables',
            git_repository = 'github.com/steinwurf/tables.git',
            major_version = 4))

    bundle.add_dependency(opt,
        resolve.ResolveGitMajorVersion(
             name = 'kodo',
             git_repository = 'github.com/steinwurf/kodo.git',
             major_version = 15))

    opt.load('wurf_dependency_bundle')
    opt.load('wurf_dependency_resolve')
    opt.load('wurf_tools')

def configure(conf):

    if conf.is_toplevel():

        conf.load('wurf_dependency_bundle')
        conf.load('wurf_tools')

        conf.load_external_tool('mkspec', 'wurf_cxx_mkspec_tool')
        conf.load_external_tool('runners', 'wurf_runner')
        conf.load_external_tool('install_path', 'wurf_install_path')
        conf.load_external_tool('project_gen', 'wurf_project_generator')

        recurse_helper(conf, 'boost')
        recurse_helper(conf, 'gtest')
        recurse_helper(conf, 'sak')
        recurse_helper(conf, 'fifi')
        recurse_helper(conf, 'gauge')
        recurse_helper(conf, 'tables')
        recurse_helper(conf, 'kodo')

def build(bld):

    if bld.is_toplevel():

        bld.load('wurf_dependency_bundle')

        recurse_helper(bld, 'boost')
        recurse_helper(bld, 'gtest')
        recurse_helper(bld, 'sak')
        recurse_helper(bld, 'fifi')
        recurse_helper(bld, 'gauge')
        recurse_helper(bld, 'tables')
        recurse_helper(bld, 'kodo')

    bld.recurse('relay_simulations')


#! /usr/bin/python3

import requests
import lxml.html as lh
import csv
import pandas as pd

source = requests.get("https://www.worldometers.info/coronavirus/")
doc = lh.fromstring(source.content)
tr_elements = doc.xpath('//tr')
Cases_Map={}
i=0
# Since out first row is the header, data is stored on the second row onwards
for j in range(1, int(len(tr_elements)/2)):
    # T is our j'th row
    z=['World','Total:','','Africa','Oceania','South America','Asia','Europe','North America','Country,Other','Asia']
    T = tr_elements[j]
    try:
        if (T[0].text_content().replace('\n', "").strip() not in z):
            Cases_Map[T[0].text_content()]=int(T[1].text_content().replace(',',""))
    except:
        pass

Cases_Map.pop('World',None)
Cases_Map.pop('Total:',None)
Cases_Map.pop('\n\n',None)
Cases_Map.pop('\nAfrica\n',None)
Cases_Map.pop('\nOceania\n',None)
Cases_Map.pop('\nSouth America\n',None)
Cases_Map.pop('\nAsia\n',None)
Cases_Map.pop('\nEurope\n',None)
Cases_Map.pop('\nNorth America\n',None)
Cases_Map=sorted(Cases_Map.items(), key = lambda x : x[1],reverse=True)
Cases_Map=dict(Cases_Map)
try:
    with open('cases.csv', 'w', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(['Rank','Country', 'Cases'])
        i=0
        for key, value in Cases_Map.items():
            i=i+1
            writer.writerow([i,key, value])
except IOError:
    print("I/O error")

active=pd.read_csv('cases.csv',index_col=[0,1,2],encoding = "ISO-8859-1")
active.to_html('cases.html')


import os
def rename_files():
    #(1) get fie names from a folder
    file_list = os.listdir(r"/home/mappuji/Documents/Nanodegree/Full Stack Developer/Projects/Prank/prank/prank")
    print(file_list)
    saved_path = os.getcwd()
    print("Current Working Directory is "+saved_path)
    os.chdir(r"/home/mappuji/Documents/Nanodegree/Full Stack Developer/Projects/Prank/prank/prank")

    #(2) for each file, rename filename
    for file_name in file_list:
        os.rename(file_name, file_name.translate(None, "0123456789"))
        print(file_name)
rename_files()


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

"""
Crawls the information of repositories that fork CPython with multi-threading.

This crawlers uses GitHub API.

Implementation according to:
https://zhuanlan.zhihu.com/p/36285110
"""

__author__ = 'Ziang Lu'

import functools
import threading
import time
from queue import Empty, Queue
from threading import Thread
from typing import Callable, Generator

import requests
from bs4 import BeautifulSoup

def run_time(func: Callable) -> Callable:
    """
    Decorator to time the given function.
    :param func: function
    :return: function
    """
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        start = time.time()
        func(*args, **kwargs)
        end = time.time()
        print(f'Finished in {end - start} seconds.')
    return wrapper

class Spider:
    __slots__ = [
        '_n_thread', '_task_queue', '_finished_first_level', '_data'
    ]

    _START_URL = 'https://movie.douban.com/top250'
    _TITLE_PROMPT = 'v:itemreviewed'
    _TIME_PROMPT = 'v:initialReleaseData'
    _DURATION_PROMPT = 'v:runtime'

    @staticmethod
    def _get_property(soup: BeautifulSoup, prompt: str) -> str:
        """
        Gets a movie property from the given soup.
        :param soup: BeautifulSoup
        :param prompt: str
        :return: str
        """
        elem = soup.find('span', property=prompt)
        if elem:
            return elem.text
        else:
            return ''

    def __init__(self, n_thread: int):
        """
        Constructor with parameter
        :param n_thread: int
        """
        self._task_queue = Queue()
        self._n_thread = n_thread
        self._finished_first_level = False
        self._data = []

    @run_time
    def run(self) -> None:
        """
        Runs the crawler.
        :return: None
        """
        threads = []

        # Fire a starting thread to generate the starting task and put it to the
        # queue
        start_th = Thread(target=self.start)
        start_th.start()
        threads.append(start_th)

        # Fire some threads to fetch the tasks from the queue and do the tasks
        for _ in range(self._n_thread):
            th = Thread(target=self._thread_do_tasks)
            th.start()
            threads.append(th)

        for th in threads:
            th.join()

        print(self._data)
        print('Data crawling finished.')

    def start(self) -> None:
        """
        Generates the starting task and put it to the queue.
        :return: None
        """
        for task in self._gen_starting_task():
            self._task_queue.put(task)

    def _gen_starting_task(self) -> Generator:
        """
        Generates the starting task.
        :return: Generator object
        """
        yield self._START_URL, self._parse_first_level

    def _parse_first_level(self, url: str) -> Generator:
        """
        Task to parse a first-level page.
        :param url: str
        :return: Generator object
        """
        r = requests.get(url)
        soup = BeautifulSoup(r.text, 'lxml')

        movie_elems = soup.find_all('div', class_='item')
        for movie_elem in movie_elems:
            second_level_url = movie_elem.find('div', class_='hd').a['href']
            yield second_level_url, self._parse_second_level

        next_elem = soup.find('span', class_='next').a
        if next_elem:
            next_url = self._START_URL + next_elem['href']
            yield next_url, self._parse_first_level
        else:
            self._finished_first_level = True

    def _parse_second_level(self, url: str) -> Generator:
        """
        Task to parse a second-level page.
        :param url: str
        :return: Generator object
        """
        r = requests.get(url)
        soup = BeautifulSoup(r.text, 'lxml')

        title = self._get_property(soup, self._TITLE_PROMPT)
        time_ = self._get_property(soup, self._TIME_PROMPT)
        duration = self._get_property(soup, self._DURATION_PROMPT)
        yield {
            'title': title,
            'time': time_,
            'duration': duration
        }

    def _thread_do_tasks(self) -> None:
        """
        Thread function to fetch tasks from the queue and do the tasks.
        :return: None
        """
        while not self._finished_first_level or not self._task_queue.empty():
            try:
                # Take out a task from the queue
                url, func = self._task_queue.get(timeout=3)  # Add timeout argument to ensure that every thread will eventually terminate
                print(f'Crawling ... {url}')
                # Do the current task, generating some results
                for result in func(url):
                    if isinstance(result, tuple):
                        # Meaning that the current task was parsing a
                        # first-level page, generating new tasks to parse
                        # second-level pages
                        new_task = result
                        self._task_queue.put(new_task)
                    elif isinstance(result, dict):
                        # Meaning that the current task was parsing a
                        # second-level page, generating a movie info
                        movie_info = result
                        self._data.append(movie_info)
                time.sleep(1)  # Sleep 1 second to avoid IP being blocked
            except Empty:
                print(f'{threading.current_thread().name}: Timeout occurred.')
        print(f'{threading.current_thread().name} finished.')

def main():
    Spider(10).run()

if __name__ == '__main__':
    main()


#!/usr/bin/env python3

import re
from collections import namedtuple

PUZZLE_PARSING = re.compile(r"(\w+) can fly (\d+) km/s for (\d+) seconds, but then must rest for (\d+) seconds")

RacePos = namedtuple(
    'RacePos',
    ['distance', 'sprinting_time', 'resting_time']
)
Participant = namedtuple(
    'Participant',
    ['name', 'speed', 'sprint_time', 'resting_time']
)

class Position:
    def __init__(self, participant):
        self.participant = participant
        self.points = 0

    def name(self):
        return self.participant.name

    def distance(self, ticks):
        cycle_time = self.participant.sprint_time + self.participant.resting_time
        cycles, remain = divmod(ticks, cycle_time)
        dist = cycles * self.participant.speed * self.participant.sprint_time
        if remain > self.participant.sprint_time:
            dist += self.participant.speed * self.participant.sprint_time
        else:
            dist += remain * self.participant.speed
        return dist

    def from_specification(spec):
        return Position(
            Participant(
                spec[0],
                int(spec[1]),
                int(spec[2]),
                int(spec[3])
            )
        )

def find_winner_at(participants, no_of_ticks):
    """
    :param participants:
    :param no_of_ticks:
    :return:

    >>> find_winner_at([("Comet", 14, 10, 127), ("Dancer", 16, 11, 162)], 1000)
    'Comet'
    """
    race = [Position.from_specification(x) for x in participants]
    position = sorted([(x.name(), x.distance(no_of_ticks)) for x in race], key=lambda p: p[1], reverse=True)
    print(position)
    winner, *loser = position
    return winner

def main():
    # import doctest
    # doctest.testmod()
    puzzle = [PUZZLE_PARSING.match(x).groups() for x in open('inputs/day_14.txt').read().strip().split("\n")]
    winner = find_winner_at(puzzle, 2503)
    print(winner)

    # Part 2:
    racers = [Position.from_specification(x) for x in puzzle]

    for i in range(1, 2503):
        dists = sorted([(r, r.distance(i)) for r in racers], key=lambda x: x[1], reverse=True)
        dists[0][0].points = dists[0][0].points + 1

    winner, *losers = sorted(racers, key=lambda x: x.points, reverse=True)
    print(winner.name(), winner.points)

if __name__ == '__main__':
    main()


__author__ = 'zhangxianghui'

import tensorflow as tf
import numpy as np
from numpy import linalg as la
from matplotlib import pyplot as plt
import xlrd

def getDataFromXlsx(file='',sheet=0,index=1,dose=1,scope=[2,82]):
    xls=xlrd.open_workbook(file)
    table=xls.sheets()[sheet]
    _data=np.array([])
    for i in range(table.nrows):
        if table.row_values(i)[index]==dose:
            _data=np.append(_data,table.row_values(i)[scope[0]:scope[1]])
    return _data.reshape((_data.size/(scope[1]-scope[0]),scope[1]-scope[0]))

def mean_filt(array,filter=[1,1,1]):
    l_filter=len(filter)
    l_array=len(array)
    Narray=np.append(array,[array[l_array-1] for k in range(l_filter-1)])
    res=np.zeros(l_array)
    for i in range(l_array):
        temp=[Narray[i+j] for j in range(l_filter)]
        res[i]=np.dot(temp,filter)
    return res

def L2_norm(x):
    norm=la.norm(x)
    return x/norm

_gfp=getDataFromXlsx('pulser_with_response.xlsx',0,1,1,[2,82])
_mcherry=getDataFromXlsx('pulser_with_response.xlsx',0,1,1,[82,162])
for i in range(16):
    _gfp[i]=L2_norm(mean_filt(_gfp[i]))
    _mcherry[i]=L2_norm(mean_filt(_mcherry[i]))

# train_gfp=np.array(_gfp[0:15])
# train_mcherry=np.array(_mcherry[0:15])
# test_gfp=np.array(_gfp[15])
# test_mcherry=np.array(_mcherry[15])
train_gfp=np.array(_gfp[1:16])
train_mcherry=np.array(_mcherry[1:16])
test_gfp=np.array(_gfp[0])
test_mcherry=np.array(_mcherry[0])

_x=tf.placeholder(tf.float32,[None,80])
_y=tf.placeholder(tf.float32,[None,80])

weight1=tf.Variable(tf.random_normal([80,160]))
bias1=tf.Variable(tf.zeros([1,160])+0.1)
output1=tf.nn.softmax(tf.matmul(_x,weight1)+bias1)

weight2=tf.Variable(tf.random_normal([160,160]))
bias2=tf.Variable(tf.zeros([1,160])+0.1)
output2=tf.nn.softmax(tf.matmul(output1,weight2)+bias2)

weight3=tf.Variable(tf.random_normal([160,80]))
bias3=tf.Variable(tf.zeros([1,80])+0.1)
output3=tf.nn.softmax(tf.matmul(output2,weight3)+bias3)

loss=tf.reduce_mean(tf.reduce_sum(tf.square(_y-output3),reduction_indices=[1]))

# optimizer=tf.train.GradientDescentOptimizer(0.1).minimize(loss)
optimizer=tf.train.AdamOptimizer(0.1,beta1=0.999).minimize(loss)

init=tf.initialize_all_variables()
sess=tf.Session()
sess.run(init)

for i in range(1000):
    _,loss_value=sess.run([optimizer,loss],feed_dict={_x:train_gfp,_y:train_mcherry})
    if(i%50==0):
        print(loss_value)

result_mcherry=sess.run(output3,feed_dict={_x:test_gfp.reshape((1,80))})

t=[i for i in range(80)]
plt.figure('gfp train set')
for i in range(15):
    plt.subplot(4,4,i+1)
    plt.plot(t,train_gfp[i])
plt.figure('mcherry train set')
for i in range(15):
    plt.subplot(4,4,i+1)
    plt.plot(t,train_mcherry[i])

plt.figure('gfp test set')
plt.plot(t,test_gfp)
plt.figure('mcherry test set')
plt.plot(t,test_mcherry)

plt.figure('mcherry test result')
plt.plot(t,result_mcherry[0])
plt.show()
plt.close()


def IntCodeComputer(noun, verb, initialMemoryState):
    intCodeProgram = initialMemoryState.copy()
    intCodeProgram[1] = noun
    intCodeProgram[2] = verb
    intructionPointer = 0
    haltNotReached = True
    while haltNotReached:
        opCode = intCodeProgram[intructionPointer]
        if opCode == 1:
            firstOperandIndex = intCodeProgram[intructionPointer + 1]
            secondOperandIndex = intCodeProgram[intructionPointer + 2]
            resultIndex = intCodeProgram[intructionPointer + 3]
            intCodeProgram[resultIndex] = intCodeProgram[firstOperandIndex] + intCodeProgram[secondOperandIndex]

            intructionPointer += 4
        elif opCode == 2:
            firstOperandIndex = intCodeProgram[intructionPointer + 1]
            secondOperandIndex = intCodeProgram[intructionPointer + 2]
            resultIndex = intCodeProgram[intructionPointer + 3]
            intCodeProgram[resultIndex] = intCodeProgram[firstOperandIndex] * intCodeProgram[secondOperandIndex]

            intructionPointer += 4
        elif opCode == 99:
            haltNotReached = False
        else:
            print(f"Invalid opcode {opCode}")
    return intCodeProgram[0]

def GetIntCodeProgramFromFile():
    f = open("input.txt", "r")
    _input = f.read().strip()
    intCodeProgram = list(map(lambda x: int(x), _input.split(",")))
    return intCodeProgram

def Part1():
    intCodeProgram = GetIntCodeProgramFromFile()
    output = IntCodeComputer(12, 2, intCodeProgram)
    print(output)

def Part2():
    intCodeProgram = GetIntCodeProgramFromFile()
    for noun in range(0, 100):
        for verb in range(0, 100):
            print(f"Noun {noun} Verb {verb}")
            output = IntCodeComputer(noun, verb, intCodeProgram)
            if output == 19690720:
                print("Output found")
                print(f"Result {100 * noun + verb}")
                return

Part1()
Part2()


# -*- coding: utf8 -*-
from tower import ugettext_lazy as _

# From page 10 of the Mozilla Exporter API docs v1.0.0
BANGO_OUTPAYMENT_CURRENCIES = [
    ('USD', _(u'US Dollars')),
    ('GBP', _(u'Pounds Sterling')),
    ('EUR', _(u'Euros')),
]

BANGO_CURRENCIES = [
    ('AUD', _(u'Australian Dollars')),
    ('CAD', _(u'Canadian Dollars')),
    ('CHF', _(u'Swiss Francs')),
    ('COP', _(u'Colombian Pesos')),
    ('DKK', _(u'Danish Krone')),
    ('EGP', _(u'Egyptian Pound')),
    ('EUR', _(u'Euros')),
    ('GBP', _(u'Pounds Sterling')),
    ('IDR', _(u'Indonesian Rupiah')),
    ('MXN', _(u'Mexican Pesos')),
    ('MYR', _(u'Malaysian Ringgits')),
    ('NOK', _(u'Norwegian Krone')),
    ('NZD', _(u'New Zealand Dollars')),
    ('PHP', _(u'Philippine Peso')),
    ('PLN', _(u'Polish Złoty')),
    ('QAR', _(u'Qatar Riyal')),
    ('SEK', _(u'Swedish Kronor')),
    ('SGD', _(u'Singapore Dollars')),
    ('THB', _(u'Thai Baht')),
    ('USD', _(u'US Dollars')),
    ('ZAR', _(u'South African Rand')),
]

BANGO_CURRENCIES_KEYS = [x[0] for x in BANGO_CURRENCIES]

BANGO_COUNTRIES = [
    ('AFG', _(u'Afghanistan')),
    ('ALA', _(u'Åland Islands')),
    ('ALB', _(u'Albania')),
    ('DZA', _(u'Algeria')),
    ('ASM', _(u'American Samoa')),
    ('AND', _(u'Andorra')),
    ('AGO', _(u'Angola')),
    ('AIA', _(u'Anguilla')),
    ('ATA', _(u'Antarctica')),
    ('ATG', _(u'Antigua and Barbuda')),
    ('ARG', _(u'Argentina')),
    ('ARM', _(u'Armenia')),
    ('ABW', _(u'Aruba')),
    ('AUS', _(u'Australia')),
    ('AUT', _(u'Austria')),
    ('AZE', _(u'Azerbaijan')),
    ('BHS', _(u'Bahamas')),
    ('BHR', _(u'Bahrain')),
    ('BGD', _(u'Bangladesh')),
    ('BRB', _(u'Barbados')),
    ('BLR', _(u'Belarus')),
    ('BEL', _(u'Belgium')),
    ('BLZ', _(u'Belize')),
    ('BEN', _(u'Benin')),
    ('BMU', _(u'Bermuda')),
    ('BTN', _(u'Bhutan')),
    ('BOL', _(u'Bolivia')),
    ('BES', _(u'Bonaire, Saint Eustatius and Saba')),
    ('BIH', _(u'Bosnia and Herzegovina')),
    ('BWA', _(u'Botswana')),
    ('BVT', _(u'Bouvet Island')),
    ('BRA', _(u'Brazil')),
    ('IOT', _(u'British Indian Ocean Territory')),
    ('BRN', _(u'Brunei Darussalam')),
    ('BGR', _(u'Bulgaria')),
    ('BFA', _(u'Burkina Faso')),
    ('BDI', _(u'Burundi')),
    ('KHM', _(u'Cambodia')),
    ('CMR', _(u'Cameroon')),
    ('CAN', _(u'Canada')),
    ('CPV', _(u'Cape Verde')),
    ('CYM', _(u'Cayman Islands')),
    ('CAF', _(u'Central African Republic')),
    ('TCD', _(u'Chad')),
    ('CHL', _(u'Chile')),
    ('CHN', _(u'China')),
    ('CXR', _(u'Christmas Island')),
    ('CCK', _(u'Cocos (Keeling) Islands')),
    ('COL', _(u'Colombia')),
    ('COM', _(u'Comoros')),
    ('COG', _(u'Congo')),
    ('COD', _(u'Congo, Democratic Republic')),
    ('COK', _(u'Cook Islands')),
    ('CRI', _(u'Costa Rica')),
    ('CIV', _(u"Côte d'Ivoire")),
    ('HRV', _(u'Croatia')),
    ('CUB', _(u'Cuba')),
    ('CUW', _(u'Curaçao')),
    ('CYP', _(u'Cyprus')),
    ('CZE', _(u'Czech Republic')),
    ('DNK', _(u'Denmark Do')),
    ('DJI', _(u'Djibouti')),
    ('DMA', _(u'Dominica')),
    ('DOM', _(u'Dominican Republic')),
    ('ECU', _(u'Ecuador')),
    ('EGY', _(u'Egypt')),
    ('SLV', _(u'El Salvador')),
    ('GNQ', _(u'Equatorial Guinea')),
    ('ERI', _(u'Eritrea')),
    ('EST', _(u'Estonia')),
    ('ETH', _(u'Ethiopia')),
    ('FLK', _(u'Falkland Islands (Malvinas)')),
    ('FRO', _(u'Faroe Islands')),
    ('FJI', _(u'Fiji')),
    ('FIN', _(u'Finland')),
    ('FRA', _(u'France')),
    ('GUF', _(u'French Guiana')),
    ('PYF', _(u'French Polynesia')),
    ('ATF', _(u'French Southern Territories')),
    ('GAB', _(u'Gabon')),
    ('GMB', _(u'Gambia')),
    ('GEO', _(u'Georgia')),
    ('DEU', _(u'Germany')),
    ('GHA', _(u'Ghana')),
    ('GIB', _(u'Gibraltar')),
    ('GRC', _(u'Greece')),
    ('GRL', _(u'Greenland')),
    ('GRD', _(u'Grenada')),
    ('GLP', _(u'Guadeloupe')),
    ('GUM', _(u'Guam')),
    ('GTM', _(u'Guatemala')),
    ('GGY', _(u'Guernsey')),
    ('GIN', _(u'Guinea')),
    ('GNB', _(u'Guinea-Bissau')),
    ('GUY', _(u'Guyana')),
    ('HTI', _(u'Haiti')),
    ('HMD', _(u'Heard and McDonald Islands')),
    ('VAT', _(u'Holy See (Vatican City State)')),
    ('HND', _(u'Honduras')),
    ('HKG', _(u'Hong Kong')),
    ('HUN', _(u'Hungary')),
    ('ISL', _(u'Iceland')),
    ('IND', _(u'India')),
    ('IDN', _(u'Indonesia')),
    ('IRN', _(u'Iran, Islamic Republic of')),
    ('IRQ', _(u'Iraq')),
    ('IRL', _(u'Ireland')),
    ('IMN', _(u'Isle of Man')),
    ('ISR', _(u'Israel')),
    ('ITA', _(u'Italy')),
    ('JAM', _(u'Jamaica')),
    ('JPN', _(u'Japan')),
    ('JEY', _(u'Jersey')),
    ('JOR', _(u'Jordan')),
    ('KAZ', _(u'Kazakhstan')),
    ('KEN', _(u'Kenya')),
    ('KIR', _(u'Kiribati')),
    ('PRK', _(u"Korea, Democratic People's Rep")),
    ('KOR', _(u'Korea, Republic of')),
    ('KOS', _(u'Kosovo')),
    ('KWT', _(u'Kuwait')),
    ('KGZ', _(u'Kyrgyzstan')),
    ('LAO', _(u"Lao People's Democratic Rep")),
    ('LVA', _(u'Latvia')),
    ('LBN', _(u'Lebanon')),
    ('LSO', _(u'Lesotho')),
    ('LBR', _(u'Liberia')),
    ('LBY', _(u'Libyan Arab Jamahiriya')),
    ('LIE', _(u'Liechtenstei')),
    ('LTU', _(u'Lithuania')),
    ('LUX', _(u'Luxembourg')),
    ('MAC', _(u'Macao')),
    ('MKD', _(u'Macedonia, Former Yugoslav Rep')),
    ('MDG', _(u'Madagascar')),
    ('MWI', _(u'Malawi')),
    ('MYS', _(u'Malaysia')),
    ('MDV', _(u'Maldives')),
    ('MLI', _(u'Mali')),
    ('MLT', _(u'Malta')),
    ('MHL', _(u'Marshall Islands')),
    ('MTQ', _(u'Martinique')),
    ('MRT', _(u'Mauritania')),
    ('MUS', _(u'Mauritius')),
    ('MYT', _(u'Mayotte')),
    ('MEX', _(u'Mexico')),
    ('FSM', _(u'Micronesia, Federated States of')),
    ('MDA', _(u'Moldova, Republic of')),
    ('MCO', _(u'Monaco')),
    ('MNG', _(u'Mongolia')),
    ('MNE', _(u'Montenegro')),
    ('MSR', _(u'Montserrat')),
    ('MAR', _(u'Morocco')),
    ('MOZ', _(u'Mozambique')),
    ('MMR', _(u'Myanmar')),
    ('NAM', _(u'Namibia')),
    ('NRU', _(u'Nauru')),
    ('NPL', _(u'Nepal')),
    ('NLD', _(u'Netherlands')),
    ('NCL', _(u'New Caledonia')),
    ('NZL', _(u'New Zealand')),
    ('NIC', _(u'Nicaragua')),
    ('NER', _(u'Niger')),
    ('NGA', _(u'Nigeria')),
    ('NIU', _(u'Niue')),
    ('NFK', _(u'Norfolk Island')),
    ('MNP', _(u'Northern Mariana Islands')),
    ('NOR', _(u'Norway')),
    ('OMN', _(u'Oman')),
    ('PAK', _(u'Pakistan')),
    ('PLW', _(u'Palau')),
    ('PSE', _(u'Palestinian Territory, Occupied')),
    ('PAN', _(u'Panama')),
    ('PNG', _(u'Papua New Guinea')),
    ('PRY', _(u'Paraguay')),
    ('PER', _(u'Peru')),
    ('PHL', _(u'Philippines')),
    ('PCN', _(u'Pitcairn')),
    ('POL', _(u'Poland')),
    ('PRT', _(u'Portugal')),
    ('PRI', _(u'Puerto Rico')),
    ('QAT', _(u'Qatar')),
    ('REU', _(u'Réunion')),
    ('ROU', _(u'Romania')),
    ('RUS', _(u'Russian Federation')),
    ('RWA', _(u'Rwanda')),
    ('BLM', _(u'Saint Barthélemy')),
    ('SHN', _(u'Saint Helena')),
    ('KNA', _(u'Saint Kitts and Nevis')),
    ('LCA', _(u'Saint Lucia')),
    ('MAF', _(u'Saint Martin')),
    ('SPM', _(u'Saint Pierre and Miquelon')),
    ('VCT', _(u'Saint Vincent and the Grenadines')),
    ('WSM', _(u'Samoa')),
    ('SMR', _(u'San Marino')),
    ('STP', _(u'Sao Tome and Principe')),
    ('SAU', _(u'Saudi Arabia')),
    ('SEN', _(u'Senega')),
    ('SRB', _(u'Serbia')),
    ('SCG', _(u'Serbia and Montenegro')),
    ('SYC', _(u'Seychelles')),
    ('SLE', _(u'Sierra Leone')),
    ('SGP', _(u'Singapore')),
    ('SXM', _(u'Sint Maarten (Dutch part)')),
    ('SVK', _(u'Slovakia')),
    ('SVN', _(u'Slovenia')),
    ('SLB', _(u'Solomon Islands')),
    ('SOM', _(u'Somalia')),
    ('ZAF', _(u'South Africa')),
    ('SGS', _(u'South Georgia and the South Sandwich Islands')),
    ('SSD', _(u'South Sudan')),
    ('ESP', _(u'Spain')),
    ('LKA', _(u'Sri Lanka')),
    ('SDN', _(u'Sudan')),
    ('SUR', _(u'Suriname')),
    ('SJM', _(u'Svalbard and Jan Mayen')),
    ('SWZ', _(u'Swaziland')),
    ('SWE', _(u'Sweden')),
    ('CHE', _(u'Switzerland')),
    ('SYR', _(u'Syrian Arab Republic')),
    ('TWN', _(u'Taiwan, Province of China')),
    ('TJK', _(u'Tajikistan')),
    ('TZA', _(u'Tanzania, United Republic of')),
    ('THA', _(u'Thailand')),
    ('TLS', _(u'Timor-Leste')),
    ('TGO', _(u'Togo')),
    ('TKL', _(u'Tokelau')),
    ('TON', _(u'Tonga')),
    ('TTO', _(u'Trinidad and Tobago')),
    ('TUN', _(u'Tunisia')),
    ('TUR', _(u'Turkey')),
    ('TKM', _(u'Turkmenistan')),
    ('TCA', _(u'Turks and Caicos Islands')),
    ('TUV', _(u'Tuvalu')),
    ('UGA', _(u'Uganda')),
    ('UKR', _(u'Ukraine')),
    ('ARE', _(u'United Arab Emirates')),
    ('GBR', _(u'United Kingdom')),
    ('USA', _(u'United States')),
    ('UMI', _(u'United States Minor Outlying Islands')),
    ('URY', _(u'Uruguay')),
    ('UZB', _(u'Uzbekistan')),
    ('VUT', _(u'Vanuatu')),
    ('VEN', _(u'Venezuela, Bolivarian Republic of')),
    ('VNM', _(u'Vietnam')),
    ('VGB', _(u'Virgin Islands, British')),
    ('VIR', _(u'Virgin Islands, U.S.')),
    ('WLF', _(u'Wallis and Futuna')),
    ('ESH', _(u'Western Sahara')),
    ('YEM', _(u'Yemen')),
    ('ZMB', _(u'Zambia')),
    ('ZWE', _(u'Zimbabwe')),
]


class Ship:
    static_val = 'static!'
    def __init__(self, name = 'Корабель', water_capacity = 1000, amount_containers = 100, weight = 10000,
                 type_name = 'Крейсери', length = 200):
        self.name = name
        self.water_capacity = water_capacity
        self.amount_containers = amount_containers
        self.weight = weight
        self.type_name = type_name
        self.lenght = length
    def __del__(self):
        print('Деструктор')
    def ShipInfo(self):
        print('Назва: ' + self.name + '\nВодоємність: ' + str(self.water_capacity) + '\nКількість контейнерів: ' + str(self.amount_containers) +
              '\nВага: ' + str(self.weight) + '\nКлас: ' + self.type_name + '\nДовжина: ' + str(self.lenght) + '\n')
    @staticmethod
    def Get_static_val():
        return self.static_val

def main():
    ship1 = Ship('Корабель1', 2000, 200, 15000, 'Лінкор', 250)
    ship2 = Ship('Корабель2', 2000)
    ship3 = Ship('Корабель3', 2000, 200, 15000)
    ship1.ShipInfo();
    ship2.ShipInfo();
    ship3.ShipInfo();

if __name__ == '__main__':
    main()


from collections import deque

from Aircraft import Aircraft

# structure used to represent a departure queue
class TakeOffQueue(deque):
    def __init__(self):
        deque.__init__(self)

        self.currentTime = 0
        self.nextAvailable = 0
        self.lastPlane = Aircraft('nil', 0, 0, 0, 0)
        self.totalFuelBurned = 0
        self.totalDelayCost = 0

    def print(self):
        print("next available " + str(self.nextAvailable))
        for plane in self:
            plane.printAircraft()
        print("\n")

    # depart aircraft if available, otherwise update time
    def update(self, newTime):
        self.currentTime = newTime
        self.nextAvailable = max(self.nextAvailable, self.currentTime)
        if len(self) > 0 and self.nextAvailable <= self.currentTime:
            self.lastPlane = self.popleft()
            res = self.lastPlane.depart(self.nextAvailable, self)
            self.nextAvailable = self.nextAvailable + self.lastPlane.delayToOthers
            return res + self.update(newTime)
        return 0, 0

    # insert new plane into queue at appropriate index
    def insertIntoTakeoffQueue(self, plane):
        res = self.update(plane.arrivalTime)
        self.insert(plane.findIndex(self), plane)
        return res

# takeoff queue without re-ordering
class DefaultTakeOffQueue(TakeOffQueue):
    def insertIntoTakeoffQueue(self, plane):
        self.append(plane)
        return self.update(plane.arrivalTime)


#coding=utf-8
__author__ = 'wanglu'

APP_KEY = "wl_flask"

IS_USE_MONGO = False
IS_USE_MYSQL = False

# version
VERSION_ID = ''

STATIC_URL="/static"

#数据库配置
SQLALCHEMY_DATABASE_URI="mysql+pymysql://root:root@localhost:3306/test?charset=utf8"

# MongoDB settings
MONGODB_SETTINGS = {
    'db': 'test',
    'host': '10.128.8.10',
    'port': 27017
}

#UEditor
UEDITOR_URL_PREFIX = "http://localhost:8000/ndb/ueditor/"
UEDITOR_UPLOAD_FOLDER = "./upload/ueditor/"
ALLOWED_EXTENSIONS =['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif',"json"]

#日志配置
formatter = '[%(asctime)s][%(filename)s:%(lineno)s][%(levelname)s][%(message)s]'

LOGGING_CONFIG = {
    'version': 1,
    'disable_existing_loggers': False,  # this fixes the problem

    'formatters': {
        'standard': {
            'format': formatter,
        },
    },
    'handlers': {
        'default': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'standard'
        },
        "debug_file_handler": {
            "class": "logging.handlers.TimedRotatingFileHandler",
            "level": "DEBUG",
            "formatter": "standard",
            "filename": "./logs/debug.log",
            "when": "D",
            "interval": 1,
            #"maxBytes": 1048576000,
            "backupCount": 30,
            "encoding": "utf8"
        },
    },
    'loggers': {
        '': {
            'handlers': ['default', 'debug_file_handler'],
            'level': 'DEBUG',
            'propagate': False
        },
        'requests': {
            'handlers': ['default', 'debug_file_handler'],
            'level': 'ERROR',
            'propagate': False,
        },
        'flask':{
            'handlers': ['default', 'debug_file_handler'],
            'level': 'ERROR',
            'propagate': False,
        }
    }
}


def qs(aList, start, end):
    if start >= end:
        return aList
    pivotValue = partition(aList, start, end)
    qs(aList, start, pivotValue)
    qs(aList, pivotValue + 1, end)

def partition(A, start, end):
    left = start
    right = end
    pivot = A[start]
    while left < right:
        while A[left] < pivot:
            left += 1
        while A[right] > pivot:
            right -= 1
        if left < right:
            # Swapping
            A[left], A[right] = A[right], A[left]
    return right

def visualQS(aList, start, end, indentLevel):
    print(":   " * indentLevel +"|-----------NewCall-----------\n" + ":   " * indentLevel + "|")
    print(":   " * indentLevel + "|Current List: {}  Starting: {}   End: {}".format(aList, start, end))
    if start >= end:
        print(":   " * indentLevel +"|Start ({}) >= End ({})".format(start, end))
        print(":   " * indentLevel +"|-----------EndCall-----------\n" + ":   " * indentLevel + "|")
        return aList
    pivotIndex = partition(aList, start, end)
    print(":   " * indentLevel +"|Moving list after partition: {}".format(aList))
    print(":   " * indentLevel +"|Less than: {}".format(aList[start:pivotIndex]))
    visualQS(aList, start, pivotIndex, indentLevel + 1)
    print(":   " * indentLevel +"|Greater than: {}".format(aList[pivotIndex+1:end]))
    visualQS(aList, pivotIndex + 1, end, indentLevel + 1)
    print(":   " * indentLevel +"|-----------EndCall-----------\n" + ":   " * indentLevel + "|")

if __name__ == "__main__":
    # theList = [10, 8, -2,  6, 11, 4]
    # qs(theList, 0, len(theList) -1 )
    # print(theList)

    newList= [54, 26, 93, 17, 77, 31, 44, 55, 20]
    print(newList)
    print()
    visualQS(newList, 0, len(newList) - 1, 0)
    print(newList)


class Solution:
    def getMorsePattern(self, word):
        morseList = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
        morse = ""
        for char in word:
            charNum = ord(char) - ord('a')
            morse += morseList[charNum]
        return morse

    def uniqueMorseRepresentations(self, words):
        """
        :type words: List[str]
        :rtype: int
        """

        morsePatterns = set()

        for word in words:
            morsePatterns.add(self.getMorsePattern(word))

        return len(morsePatterns)


# Copyright (C) 2017 Google Inc.
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>

"""Contains WithAction mixin.

A mixin for processing actions on an object in the scope of put request .
"""

from collections import namedtuple, defaultdict

from ggrc import db
from ggrc.login import get_current_user
from ggrc.models.comment import Comment
from ggrc.models.document import Document
from ggrc.models.snapshot import Snapshot
from ggrc.models.exceptions import ValidationError
from ggrc.models.reflection import ApiAttributes
from ggrc.models.reflection import Attribute
from ggrc.models.relationship import Relationship

class WithAction(object):
  """Mixin for add/remove map/unmap actions processing"""

  _api_attrs = ApiAttributes(
      Attribute("actions", create=False, update=True, read=False)
  )
  _operation_order = [
      "add_related",
      "remove_related",
  ]
  _object_map = {
      "Document": Document,
      "Comment": Comment,
      "Snapshot": Snapshot,
  }
  _actions = None
  _added = None  # collect added objects for signals sending
  _deleted = None  # collect deleted objects fro signals sending
  _relationships_map = None

  def actions(self, value):
    """Save actions for further processing"""
    if value:
      self._actions = value.get("actions")

  def _validate_actions(self):
    """Validate operation types"""
    invalid_actions = ",".join(set(self._actions) - set(self._operation_order))
    if invalid_actions:
      raise ValueError("Invalid actions found: {}".format(invalid_actions))

  def _build_relationships_map(self):
    """Build relationships map"""
    self._relationships_map = {
        (rel.destination_type, rel.destination_id): rel
        for rel in self.related_destinations
    }
    self._relationships_map.update({
        (rel.source_type, rel.source_id): rel
        for rel in self.related_sources
    })

  def _process_operation(self, operation):
    """Process operation actions"""
    for action in self._actions[operation]:
      # get object class
      obj_type = action.get("type")
      if not obj_type:
        raise ValidationError('type is not defined')
      obj_class = self._object_map.get(obj_type)
      if not obj_class:
        raise ValueError('Invalid action type: {type}'.format(type=obj_type))

      # get handler class
      action_type = '{type}Action'.format(type=obj_type)
      action_class = getattr(self, action_type, None)
      if not action_class:
        raise ValueError('Invalid action type: {type}'.format(type=obj_type))

      # process action
      # pylint: disable=not-callable
      added, deleted = getattr(action_class(), operation)(self, action)

      # collect added/deleted objects
      self._added.extend(added)
      self._deleted.extend(deleted)

  def process_actions(self):
    """Process actions"""
    if not self._actions:
      return {}, []

    self._validate_actions()

    self._added = []
    self._deleted = []

    for operation in self._operation_order:
      if operation not in self._actions:
        continue

      if not self._actions[operation]:
        continue

      self._build_relationships_map()
      self._process_operation(operation)

    # collect added/deleted objects for signals sending
    added = defaultdict(list)
    for obj in self._added:
      added[obj.__class__].append(obj)

    return added, self._deleted

  class BaseAction(object):
    """Base action"""

    AddRelated = namedtuple("AddRelated", ["id", "type"])
    MapRelated = namedtuple("MapRelated", ["id", "type"])
    RemoveRelated = namedtuple("RemoveRelated", ["id", "type"])

    def add_related(self, parent, _action):
      """Add/map object to parent"""
      added = []
      if _action.get("id"):
        action = self._validate(_action, self.MapRelated)
        obj = self._get(action)
      else:
        action = self._validate(_action, self.AddRelated)
        obj = self._create(parent, action)
        added.append(obj)

      rel = Relationship(source=parent,
                         destination=obj,
                         context=parent.context)
      added.append(rel)
      return added, []

    @staticmethod
    def _validate(_action, ntuple):
      try:
        return ntuple(**_action)
      except TypeError:
        raise ValidationError("Missed action parameters")

    # pylint: disable=unused-argument,no-self-use
    def _create(self, parent, action):
      raise ValidationError("Can't create {type} object".format(
          type=action.type))

    def _get(self, action):
      """Get object specified in action"""
      if not action.id:
        raise ValueError("id is not defined")
      # pylint: disable=protected-access
      obj_class = WithAction._object_map[action.type]
      obj = obj_class.query.get(action.id)
      if not obj:
        raise ValueError(
            'Object not found: {type} {id}'.format(type=action.type,
                                                   id=action.id))
      return obj

    def remove_related(self, parent, _action):
      """Remove relationship"""
      action = self._validate(_action, self.RemoveRelated)
      deleted = []
      obj = self._get(action)
      # pylint: disable=protected-access
      rel = parent._relationships_map.get((obj.type, obj.id))
      if rel:
        db.session.delete(rel)
        deleted.append(rel)
      return [], deleted

  class DocumentAction(BaseAction):
    """Document action"""

    AddRelated = namedtuple("AddRelated", ["id",
                                           "type",
                                           "document_type",
                                           "link",
                                           "title"])

    def _create(self, parent, action):
      obj = Document(link=action.link,
                     title=action.title,
                     document_type=action.document_type,
                     context=parent.context)
      return obj

  class CommentAction(BaseAction):
    """Comment action"""

    AddRelated = namedtuple("AddRelated", ["id",
                                           "type",
                                           "description",
                                           "custom_attribute_definition_id"])

    def _create(self, parent, action):
      # get assignee type
      current_user = get_current_user()
      # pylint: disable=protected-access
      rel = parent._relationships_map.get((current_user.type, current_user.id))
      if rel:
        assignee_type = rel.attrs["AssigneeType"]
      else:
        assignee_type = None
      # create object
      cad_id = action.custom_attribute_definition_id
      if not cad_id:
        obj = Comment(description=action.description,
                      assignee_type=assignee_type,
                      context=parent.context)
      else:
        obj = Comment(description=action.description,
                      custom_attribute_definition_id=cad_id,
                      assignee_type=assignee_type,
                      context=parent.context)

      return obj

  class SnapshotAction(BaseAction):
    """Snapshot action"""


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 ])


# coding=utf-8
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
                        unicode_literals, with_statement)

import os
from textwrap import dedent

from pants.util.contextutil import temporary_dir

class ResolveJarsTestMixin(object):
  """Mixin for evaluating tasks which resolve their own source and javadoc jars (such as Export)."""

  def evaluate_subtask(self, targets, workdir, load_extra_confs, extra_args, expected_jars):
    """Evaluate the underlying task with the given target specs.

    :param targets: the list of targets.
    :param string workdir: the working directory to execute in.
    :param bool load_extra_confs: whether to attempt to download sources and javadocs.
    :param list extra_args: extra args to pass to the task.
    :param list expected_jars: list of jars that were expected to be resolved.
    """
    raise NotImplementedError()

  def _test_jar_lib_with_url(self, load_all):
    with self.temporary_workdir() as workdir:
      with self.temporary_sourcedir() as source_dir:
        with temporary_dir() as dist_dir:
          os.makedirs(os.path.join(source_dir, 'src'))
          with open(os.path.join(source_dir, 'src', 'BUILD.one'), 'w+') as f:
            f.write(dedent("""
              jvm_binary(name='synthetic',
                source='Main.java',
              )
            """))
          with open(os.path.join(source_dir, 'src', 'Main.java'), 'w+') as f:
            f.write(dedent("""
              public class Main {
                public static void main(String[] args) {
                  System.out.println("Hello.");
                }
              }
            """))
          with open(os.path.join(source_dir, 'src', 'Foo.java'), 'w+') as f:
            f.write(dedent("""
              public class Foo {
                public static void main(String[] args) {
                  Main.main(args);
                }
              }
            """))

          binary_target = '{}:synthetic'.format(os.path.join(source_dir, 'src'))
          pants_run = self.run_pants_with_workdir(['binary', binary_target,
                                                   '--pants-distdir={}'.format(dist_dir)], workdir)
          self.assert_success(pants_run)
          jar_path = os.path.realpath(os.path.join(dist_dir, 'synthetic.jar'))
          self.assertTrue(os.path.exists(jar_path), 'Synthetic binary was not created!')
          jar_url = 'file://{}'.format(os.path.abspath(jar_path))

          with open(os.path.join(source_dir, 'src', 'BUILD.two'), 'w+') as f:
            f.write(dedent("""
              jar_library(name='lib_with_url',
                jars=[
                  jar(org='org.pantsbuild', name='synthetic-test-jar', rev='1.2.3',
                  url='{jar_url}')
                ],
              )

              java_library(name='src',
                sources=['Foo.java'],
                dependencies=[':lib_with_url'],
              )
            """).format(jar_url=jar_url))

          spec_names = ['lib_with_url', 'src']

          targets = ['{0}:{1}'.format(os.path.join(source_dir, 'src'), name) for name in spec_names]

          with temporary_dir() as ivy_temp_dir:
            extra_args = ['--ivy-cache-dir={}'.format(ivy_temp_dir)]
            self.evaluate_subtask(targets, workdir, load_all, extra_args=extra_args,
                                  expected_jars=['org.pantsbuild:synthetic-test-jar:1.2.3'])

  def test_jar_lib_with_url_resolve_default(self):
    self._test_jar_lib_with_url(False)

  def test_jar_lib_with_url_resolve_all(self):
    self._test_jar_lib_with_url(True)