import os, sys
import argparse
import json
import socket
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from parameter.private_config import *
from datetime import datetime

class Parameter:
    def __init__(self, config_path=None, debug=False, information=None):
        self.base_path = self.get_base_path()
        self.debug = debug
        self.experiment_target = EXPERIMENT_TARGET
        self.DEFAULT_CONFIGS = global_configs()
        self.arg_names = []
        self.host_name = 'localhost'
        self.ip = '127.0.0.1'
        self.exec_time = datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
        self.commit_id = self.get_commit_id()
        self.log_func = None
        self.json_name = 'parameter.json'
        self.txt_name = 'parameter.txt'
        self.information = information
        if config_path:
            self.config_path = config_path
        else:
            self.info('use default config path')
            self.config_path = osp.join(get_base_path(), 'parameter')
        self.info(f'json path is {os.path.join(self.config_path, self.json_name)}, '
                  f'txt path is {os.path.join(self.config_path, self.txt_name)}')
        if config_path:
            self.load_config()
        else:
            self.args = self.parse()
            self.apply_vars(self.args)

    def set_log_func(self, log_func):
        self.log_func = log_func

    def info(self, info):
        if self.log_func is not None:
            self.log_func(info)
        else:
            print(info)

    @staticmethod
    def get_base_path():
        return get_base_path()

    def set_config_path(self, config_path):
        self.config_path = config_path

    @staticmethod
    def important_configs():
        res = ['env_name', 'use_true_parameter', 'use_rmdm',
               'use_uposi', 'share_ep', "rnn_fix_length",
               'uniform_sample', 'enhance_ep', 'bottle_neck', 'stop_pg_for_ep', "ep_dim", 'seed']
        return res

    def apply_vars(self, args):
        for name in self.arg_names:
            setattr(self, name, getattr(args, name))

    def make_dict(self):
        res = {}
        for name in self.arg_names:
            res[name] = getattr(self, name)
        res['description'] = self.experiment_target
        res['exec_time'] = self.exec_time
        res['commit_id'] = self.commit_id
        return res

    def parse(self):
        parser = argparse.ArgumentParser(description=EXPERIMENT_TARGET)

        self.env_name = "Hopper-v2"
        parser.add_argument('--env_name', default=self.env_name, metavar='G',
                            help='name of the environment to run')
        self.register_param('env_name')

        self.model_path = ""
        parser.add_argument('--model_path', metavar='G',
                            help='path of pre-trained model')
        self.register_param("model_path")

        self.render = False
        parser.add_argument('--render', action='store_true', default=self.render,
                            help='render the environment')
        self.register_param("render")

        self.log_std = -1.5
        parser.add_argument('--log_std', type=float, default=self.log_std, metavar='G',
                            help='log std for the policy (default: -0.0)')
        self.register_param('log_std')

        self.gamma = 0.99
        parser.add_argument('--gamma', type=float, default=self.gamma, metavar='G',
                            help='discount factor (default: 0.99)')
        self.register_param('gamma')

        self.learning_rate = 3e-4
        parser.add_argument('--learning_rate', type=float, default=self.learning_rate, metavar='G',
                            help='learning rate (default: 3e-4)')
        self.register_param('learning_rate')

        self.value_learning_rate = 1e-3
        parser.add_argument('--value_learning_rate', type=float, default=self.value_learning_rate, metavar='G',
                            help='learning rate (default: 1e-3)')
        self.register_param('value_learning_rate')

        self.num_threads = 4
        parser.add_argument('--num_threads', type=int, default=self.num_threads, metavar='N',
                            help='number of threads for agent (default: 1)')
        self.register_param('num_threads')

        self.seed = 1
        parser.add_argument('--seed', type=int, default=self.seed, metavar='N',
                            help='random seed (default: 1)')
        self.register_param('seed')

        self.random_num = 4000
        parser.add_argument('--random_num', type=int, default=self.random_num, metavar='N',
                            help='sample random_num fully random samples,')
        self.register_param('random_num')

        self.start_train_num = 20000
        parser.add_argument('--start_train_num', type=int, default=self.start_train_num, metavar='N',
                            help='after reach start_train_num, training start')
        self.register_param('start_train_num')

        self.test_sample_num = 4000
        parser.add_argument('--test_sample_num', type=int, default=self.test_sample_num, metavar='N',
                            help='sample num in test phase')
        self.register_param('test_sample_num')

        self.sac_update_time = 1000
        parser.add_argument('--sac_update_time', type=int, default=self.sac_update_time, metavar='N',
                            help='update time after sampling a batch data')
        self.register_param('sac_update_time')

        self.sac_replay_size = 1e6
        parser.add_argument('--sac_replay_size', type=int, default=self.sac_replay_size, metavar='N',
                            help='update time after sampling a batch data')
        self.register_param('sac_replay_size')

        self.min_batch_size = 1200
        parser.add_argument('--min_batch_size', type=int, default=self.min_batch_size, metavar='N',
                            help='minimal sample number per iteration')
        self.register_param('min_batch_size')

        if FC_MODE:
            self.sac_mini_batch_size = 256
            parser.add_argument('--sac_mini_batch_size', type=int, default=self.sac_mini_batch_size, metavar='N',
                                help='update time after sampling a batch data')
            self.register_param('sac_mini_batch_size')

            self.sac_inner_iter_num = 1
            parser.add_argument('--sac_inner_iter_num', type=int, default=self.sac_inner_iter_num, metavar='N',
                                help='after sample several trajectories from replay buffer, '
                                     'sac_inner_iter_num mini-batch will be sampled from the batch, '
                                     'and model will be optimized for sac_inner_iter_num times.')
            self.register_param('sac_inner_iter_num')
        else:
            self.sac_mini_batch_size = 256
            parser.add_argument('--sac_mini_batch_size', type=int, default=self.sac_mini_batch_size, metavar='N',
                                help='sac_mini_batch_size trajectories will be sampled from the replay buffer.')
            self.register_param('sac_mini_batch_size')

            self.sac_inner_iter_num = 1
            parser.add_argument('--sac_inner_iter_num', type=int, default=self.sac_inner_iter_num, metavar='N',
                                help='after sample several trajectories from replay buffer, '
                                     'sac_inner_iter_num mini-batch will be sampled from the batch, '
                                     'and model will be optimized for sac_inner_iter_num times.')
            self.register_param('sac_inner_iter_num')

        self.rnn_sample_max_batch_size = 3e5
        parser.add_argument('--rnn_sample_max_batch_size', type=int, default=self.rnn_sample_max_batch_size, metavar='N',
                            help='max point num sampled from replay buffer per time')
        self.register_param('rnn_sample_max_batch_size')

        self.rnn_slice_num = 16
        parser.add_argument('--rnn_slice_num', type=int, default=self.rnn_slice_num, metavar='N',
                            help='gradient clip steps')
        self.register_param('rnn_slice_num')

        self.sac_tau = 0.995
        parser.add_argument('--sac_tau', type=float, default=self.sac_tau, metavar='N',
                            help='ratio of coping value net to target value net')
        self.register_param('sac_tau')

        self.sac_alpha = 0.2
        parser.add_argument('--sac_alpha', type=float, default=self.sac_alpha, metavar='N',
                            help='sac temperature coefficient')
        self.register_param('sac_alpha')

        self.reward_scale = 1.0
        parser.add_argument('--reward_scale', type=float, default=self.reward_scale, metavar='N',
                            help='sac temperature coefficient')
        self.register_param('reward_scale')

        self.max_iter_num = 10000
        parser.add_argument('--max_iter_num', type=int, default=self.max_iter_num, metavar='N',
                            help='maximal number of main iterations (default: 500)')
        self.register_param('max_iter_num')

        self.save_model_interval = 5
        parser.add_argument('--save_model_interval', type=int, default=self.save_model_interval, metavar='N',
                            help="interval between saving model (default: 5, means don't save)")
        self.register_param('save_model_interval')

        self.std_learnable = 1
        parser.add_argument('--std_learnable', type=int, default=self.std_learnable, metavar='N',
                            help="standard dev can be learned")
        self.register_param('std_learnable')

        self.update_interval = 1
        parser.add_argument('--update_interval', type=int, default=self.update_interval, metavar='N',
                            help="standard dev can be learned")
        self.register_param('update_interval')

        self.ep_pretrain_path_suffix = 'None'   # '-use_rmdm-rnn_len_32-ep_dim_2-1-debug'
        parser.add_argument('--ep_pretrain_path_suffix', type=str, default=self.ep_pretrain_path_suffix, metavar='N',
                            help="environment probing pretrain model path")
        self.register_param('ep_pretrain_path_suffix')

        self.name_suffix = 'None'  # '-use_rmdm-rnn_len_32-ep_dim_2-1-debug'
        parser.add_argument('--name_suffix', type=str, default=self.name_suffix, metavar='N',
                            help="name suffix of the experiment")
        self.register_param('name_suffix')

        self.ep_apply_tau = 0.99
        parser.add_argument('--ep_apply_tau', type=float, default=self.ep_apply_tau, metavar='N',
                            help="tau used to apply ep")
        self.register_param('ep_apply_tau')

        self.target_entropy_ratio = 1.5
        parser.add_argument('--target_entropy_ratio', type=float, default=self.target_entropy_ratio, metavar='N',
                            help="target entropy")
        self.register_param('target_entropy_ratio')

        self.history_length = 0
        parser.add_argument('--history_length', type=int, default=self.history_length, metavar='N',
                            help="interval between saving model (default: 0, means don't save)")
        self.register_param('history_length')

        self.task_num = 0
        parser.add_argument('--task_num', type=int, default=self.task_num, metavar='N',
                            help="interval between saving model (default: 0, means don't save)")
        self.register_param('task_num')

        self.test_task_num = 0
        parser.add_argument('--test_task_num', type=int, default=self.test_task_num, metavar='N',
                            help="number of tasks for testing")
        self.register_param('test_task_num')

        self.use_true_parameter = False
        parser.add_argument('--use_true_parameter', action='store_true')
        self.register_param("use_true_parameter")

        self.bottle_neck = False
        parser.add_argument('--bottle_neck', action='store_true')
        self.register_param("bottle_neck")

        self.transition_learn_aux = False
        parser.add_argument('--transition_learn_aux', action='store_true')
        self.register_param("transition_learn_aux")

        self.bottle_sigma = 1e-2
        parser.add_argument('--bottle_sigma', type=float, default=self.bottle_sigma, metavar='N',
                            help="std of the noise injected to ep while inference (information bottleneck)")
        self.register_param('bottle_sigma')

        self.l2_norm_for_ep = 0.0
        parser.add_argument('--l2_norm_for_ep', type=float, default=self.l2_norm_for_ep, metavar='N',
                            help="L2 norm added to EP module")
        self.register_param('l2_norm_for_ep')

        self.policy_max_gradient = 10
        parser.add_argument('--policy_max_gradient', type=float, default=self.policy_max_gradient, metavar='N',
                            help="maximum gradient of policy")
        self.register_param('policy_max_gradient')

        self.use_rmdm = False
        parser.add_argument('--use_rmdm', action='store_true',
                            help="use Relational Matrix Determinant Maximization or not")
        self.register_param('use_rmdm')

        self.use_uposi = False
        parser.add_argument('--use_uposi', action='store_true')
        self.register_param('use_uposi')

        self.uniform_sample = False
        parser.add_argument('--uniform_sample', action='store_true')
        self.register_param('uniform_sample')

        self.share_ep = False
        parser.add_argument('--share_ep', action='store_true')
        self.register_param('share_ep')

        self.enhance_ep = False
        parser.add_argument('--enhance_ep', action='store_true')
        self.register_param('enhance_ep')

        self.stop_pg_for_ep = False
        parser.add_argument('--stop_pg_for_ep', action='store_true')
        self.register_param('stop_pg_for_ep')

        self.use_contrastive = False
        parser.add_argument('--use_contrastive', action='store_true')
        self.register_param('use_contrastive')

        self.rmdm_update_interval = -1
        parser.add_argument('--rmdm_update_interval', type=int, default=self.rmdm_update_interval, metavar='N',
                            help="update interval of rmdm")
        self.register_param('rmdm_update_interval')

        self.rnn_fix_length = 0
        parser.add_argument('--rnn_fix_length', type=int, default=self.rnn_fix_length, metavar='N',
                            help="fix the rnn memory length to rnn_fix_length")
        self.register_param('rnn_fix_length')

        self.minimal_repre_rp_size = 1e5
        parser.add_argument('--minimal_repre_rp_size', type=float, default=self.minimal_repre_rp_size, metavar='N',
                            help="after minimal_repre_rp_size, start training EP module")
        self.register_param('minimal_repre_rp_size')

        # self.ep_start_num = 150000
        self.ep_start_num = 0
        parser.add_argument('--ep_start_num', type=int, default=self.ep_start_num, metavar='N',
                            help="only when the size of the replay buffer is larger than ep_start_num"
                                 ", ep can be learned")
        self.register_param('ep_start_num')

        self.kernel_type = 'rbf_element_wise'
        parser.add_argument('--kernel_type', default=self.kernel_type, metavar='G',
                            help='kernel type for DPP loss computing (rbf/rbf_element_wise/inner)')
        self.register_param('kernel_type')

        self.rmdm_ratio = 1.0
        parser.add_argument('--rmdm_ratio', type=float, default=self.rmdm_ratio, metavar='N',
                            help="gradient ratio of rmdm")
        self.register_param('rmdm_ratio')

        self.test_variable = 1.0
        parser.add_argument('--test_variable', type=float, default=self.test_variable, metavar='N',
                            help="variable for testing variable")
        self.register_param('test_variable')

        self.rmdm_tau = 0.995
        parser.add_argument('--rmdm_tau', type=float, default=self.rmdm_tau, metavar='N',
                            help="smoothing ratio of the representation")
        self.register_param('rmdm_tau')

        self.repre_loss_factor = 1.0
        parser.add_argument('--repre_loss_factor', type=float, default=self.repre_loss_factor, metavar='N',
                            help="size of the representation loss")
        self.register_param('repre_loss_factor')

        self.ep_smooth_factor = 0.0
        parser.add_argument('--ep_smooth_factor', type=float, default=self.ep_smooth_factor, metavar='N',
                            help="smooth  factor for ep module, 0.0 for apply concurrently")
        self.register_param('ep_smooth_factor')

        self.rbf_radius = 80.0
        parser.add_argument('--rbf_radius', type=float, default=self.rbf_radius, metavar='N',
                            help="radius of the rbf kerel")
        self.register_param('rbf_radius')

        self.env_default_change_range = 3.0
        parser.add_argument('--env_default_change_range', type=float, default=self.env_default_change_range, metavar='N',
                            help="environment default change range")
        self.register_param('env_default_change_range')

        self.env_ood_change_range = 4.0
        parser.add_argument('--env_ood_change_range', type=float, default=self.env_ood_change_range,
                            metavar='N',
                            help="environment OOD change range")
        self.register_param('env_ood_change_range')

        self.consistency_loss_weight = 50.0
        parser.add_argument('--consistency_loss_weight', type=float, default=self.consistency_loss_weight, metavar='N',
                            help="loss ratio of the consistency loss")
        self.register_param('consistency_loss_weight')

        self.diversity_loss_weight = 0.025
        parser.add_argument('--diversity_loss_weight', type=float, default=self.diversity_loss_weight, metavar='N',
                            help="loss ratio of the DPP loss")
        self.register_param('diversity_loss_weight')

        self.varying_params = ['gravity', 'body_mass']
        parser.add_argument('--varying_params', nargs='+', type=str, default=self.varying_params)
        self.register_param('varying_params')

        self.up_hidden_size = [128, 64]
        parser.add_argument('--up_hidden_size', nargs='+', type=int, default=self.up_hidden_size,
                            help="architecture of the hidden layers of Universe Policy")
        self.register_param('up_hidden_size')

        self.up_activations = ['leaky_relu', 'leaky_relu', 'linear']
        parser.add_argument('--up_activations', nargs='+', type=str,
                            default=self.up_activations,
                            help="activation of each layer of Universe Policy")
        self.register_param('up_activations')

        self.up_layer_type = ['fc', 'fc', 'fc']
        parser.add_argument('--up_layer_type', nargs='+', type=str,
                            default=self.up_layer_type,
                            help="net type of Universe Policy")
        self.register_param('up_layer_type')

        # self.ep_hidden_size = [128, 64, 32] # [128, 64, 32]
        self.ep_hidden_size = [128, 64] # [256, 128]
        parser.add_argument('--ep_hidden_size', nargs='+', type=int, default=self.ep_hidden_size,
                            help="architecture of the hidden layers of Environment Probing Net")
        self.register_param('ep_hidden_size')

        if FC_MODE:
            self.ep_activations = ['leaky_relu', 'leaky_relu', 'leaky_relu', 'tanh']
            parser.add_argument('--ep_activations', nargs='+', type=str,
                                default=self.ep_activations,
                                help="activation of each layer of Environment Probing Net")
            self.register_param('ep_activations')

            self.ep_layer_type = ['fc', 'fc', 'fc', 'fc']
            parser.add_argument('--ep_layer_type', nargs='+', type=str,
                                default=self.ep_layer_type,
                                help="net type of Environment Probing Net")
            self.register_param('ep_layer_type')
        else:
            # original RNN architecture
            self.ep_activations = ['leaky_relu', 'linear', 'tanh']
            parser.add_argument('--ep_activations', nargs='+', type=str,
                                default=self.ep_activations,
                                help="activation of each layer of Environment Probing Net")
            self.register_param('ep_activations')
            self.ep_layer_type = ['fc', 'gru', 'fc']
            parser.add_argument('--ep_layer_type', nargs='+', type=str,
                                default=self.ep_layer_type,
                                help="net type of Environment Probing Net")
            self.register_param('ep_layer_type')
            # fc architecture
            # self.ep_activations = ['leaky_relu', 'tanh', 'tanh']
            # parser.add_argument('--ep_activations', nargs='+', type=str,
            #                     default=self.ep_activations,
            #                     help="activation of each layer of Environment Probing Net")
            # self.register_param('ep_activations')
            # self.ep_layer_type = ['fc', 'fc', 'fc']
            # parser.add_argument('--ep_layer_type', nargs='+', type=str,
            #                     default=self.ep_layer_type,
            #                     help="net type of Environment Probing Net")
            # self.register_param('ep_layer_type')

        self.ep_dim = 2
        parser.add_argument('--ep_dim', type=int, default=self.ep_dim, metavar='N',
                            help="dimension of environment features")
        self.register_param('ep_dim')

        self.value_hidden_size = [128, 64]
        parser.add_argument('--value_hidden_size', nargs='+', type=int, default=self.value_hidden_size,
                            help="architecture of the hidden layers of value")
        self.register_param('value_hidden_size')

        self.value_activations = ['leaky_relu', 'leaky_relu', 'linear']
        parser.add_argument('--value_activations', nargs='+', type=str,
                            default=self.value_activations,
                            help="activation of each layer of value")
        self.register_param('value_activations')

        self.value_layer_type = ['fc', 'fc', 'fc']
        parser.add_argument('--value_layer_type', nargs='+', type=str,
                            default=self.value_layer_type,
                            help="net type of value")
        self.register_param('value_layer_type')

        return parser.parse_args()

    def register_param(self, name):
        self.arg_names.append(name)

    def get_experiment_description(self):
        description = f"本机{self.host_name}, ip为{self.ip}\n"
        description += f"目前实验目的为{self.experiment_target}\n"
        description += f"实验简称: {self.short_name}\n"
        description += f"commit id: {self.commit_id}\n"
        vars = ''
        important_config = self.important_configs()
        for name in self.arg_names:
            if name in important_config:
                vars += f'**{name}**: {getattr(self, name)}\n'
            else:
                vars += f'{name}: {getattr(self, name)}\n'
        for name in self.DEFAULT_CONFIGS:
            vars += f'{name}: {self.DEFAULT_CONFIGS[name]}\n'
        return description + vars

    def __str__(self):
        return self.get_experiment_description()

    def clear_local_file(self):
        cmd = f'rm -f {os.path.join(self.config_path, self.json_name)} {os.path.join(self.config_path, self.txt_name)}'
        system(cmd)

    def save_config(self):
        self.info(f'save json config to {os.path.join(self.config_path, self.json_name)}')
        if not os.path.exists(self.config_path):
            os.makedirs(self.config_path)
        with open(os.path.join(self.config_path, self.json_name), 'w') as f:
            things = self.make_dict()
            ser = json.dumps(things)
            f.write(ser)
        self.info(f'save readable config to {os.path.join(self.config_path, self.txt_name)}')
        with open(os.path.join(self.config_path, self.txt_name), 'w') as f:
            print(self, file=f)

    def load_config(self):
        self.info(f'load json config from {os.path.join(self.config_path, self.json_name)}')
        with open(os.path.join(self.config_path, self.json_name), 'r') as f:
            ser = json.load(f)
        for k, v in ser.items():
            if not k == 'description':
                setattr(self, k, v)
                self.register_param(k)
        self.experiment_target = ser['description']

    @property
    def differences(self):
        if not os.path.exists(os.path.join(self.config_path, self.json_name)):
            return None
        with open(os.path.join(self.config_path, self.json_name), 'r') as f:
            ser = json.load(f)
        differences = []
        for k, v in ser.items():
            if not hasattr(self, k):
                differences.append(k)
            else:
                v2 = getattr(self, k)
                if not v2 == v:
                    differences.append(k)
        return differences

    def check_identity(self, need_decription=False, need_exec_time=False):
        if not os.path.exists(os.path.join(self.config_path, self.json_name)):
            self.info(f'{os.path.join(self.config_path, self.json_name)} not exists')
            return False
        with open(os.path.join(self.config_path, self.json_name), 'r') as f:
            ser = json.load(f)
        flag = True
        for k, v in ser.items():
            if not k == 'description' and not k == 'exec_time':
                if not hasattr(self, k):
                    flag = False
                    return flag
                v2 = getattr(self, k)
                if not v2 == v:
                    flag = False
                    return flag
        if need_decription:
            if not self.experiment_target == ser['description']:
                flag = False
                return flag
        if need_exec_time:
            if not self.exec_time == ser['exec_time']:
                flag = False
                return flag
        return flag

    @property
    def short_name(self):
        name = ''
        for item in self.important_configs():
            value = getattr(self, item)
            if value:
                if item == 'env_name':
                    name += value
                elif item == 'seed':
                    name += f'-{value}'
                elif item == 'rnn_fix_length':
                    name += f"-rnn_len_{value}"
                elif item == 'ep_dim':
                    name += f"-ep_dim_{value}"
                else:
                    name += f'-{item}'
        if self.debug:
            name += '-debug'
        if self.information is not None:
            name += '-{}'.format(self.information)
        if hasattr(self, 'name_suffix') and not self.name_suffix == 'None':
            name += f'_{self.name_suffix}'
        elif not len(SHORT_NAME_SUFFIX) == 0:
            name += f'_{SHORT_NAME_SUFFIX}'
        return name

    def get_commit_id(self):
        base_path = get_base_path()
        cmd = f'cd {base_path} && git log'
        commit_id = None
        try:
            with os.popen(cmd) as f:
                line = f.readline()
                words = line.split(' ')
                commit_id = words[-1][:-1]
        except Exception as e:
            self.info(f'Error occurs while fetching commit id!!! {e}')
        return commit_id

if __name__ == '__main__':
    parameter = Parameter()
    parameter.get_commit_id()
    print(parameter)


from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from time import sleep

ser = Service("C:\Program Files (x86)\chromedriver.exe")
op = webdriver.ChromeOptions()

text = input("Search Google or Type URL: ")

class GoogleBot:

	def __init__(self, text):
		self.s = webdriver.Chrome(service=ser, options=op)
		self.s.get("https://www.google.com/search?q=" + text + "&start")

GoogleBot(text)


import re

from Acquisition import aq_inner
from Products.CMFCore.utils import getToolByName
from Products.Five.browser import BrowserView
from Products.PythonScripts.standard import url_quote

INCLUDE = [
    ".*jquery",
    "d3.js",
    "bika.lims.graphics.range.js",
    "senaite.impress.*.js",  # append all senaite.impress javascripts
]

class JavaScriptsView(BrowserView):
    """Helper View to inject uncooked JS from portal_javascripts

    Most of it copied from Products.ResourceRegistries
    https://github.com/plone/Products.ResourceRegistries/blob/master/Products/ResourceRegistries/browser/scripts.pt
    """

    def registry(self):
        return getToolByName(aq_inner(self.context), "portal_javascripts")

    def skinname(self):
        return aq_inner(self.context).getCurrentSkinName()

    def scripts(self):
        registry = self.registry()
        registry_url = registry.absolute_url()
        skinname = url_quote(self.skinname())
        # get the uncooked resources
        scripts = registry.getResources()
        result = []

        for script in scripts:
            # only consider enabled JS resources
            if not script.getEnabled():
                continue
            # Only include JS where the regular expression matches
            script_id = script.getId()
            if not any(map(lambda rx: re.findall(rx, script_id), INCLUDE)):
                continue

            if script.isExternalResource():
                src = "%s" % (script.getId(),)
            else:
                src = "%s/%s/%s" % (registry_url, skinname, script.getId())
            data = {
                "src": src,
            }
            result.append(data)
        return result


from typing import Tuple, Callable

import tkinter as tk
from tkinter import ttk

import win32gui

from magnifier.util import Arguments, get_icon_path
from .start_magnifier import start_magnifier

def _create_select_delay_section(root):
    text = tk.StringVar()
    text.set('Select capture interval delay (milliseconds) (20):')

    select_delay_duration_label = ttk.Label(root, textvariable=text)
    select_delay_duration_label.pack(fill='x', padx=5, pady=5)

    def on_magnification_changed(new_value):
        text.set(f'Select capture interval delay (milliseconds) ({round(float(new_value), 1)}):')

    delay_level_slider = ttk.Scale(root, from_=1, to=1000, orient=tk.HORIZONTAL, command=on_magnification_changed)
    delay_level_slider.set(20)
    delay_level_slider.pack(fill='x', padx=5, pady=5)

    return delay_level_slider

def _create_select_resampling_filter(root):
    select_resampling_filter_label = ttk.Label(root, text='Select a resampling filter (default is BICUBIC):')
    select_resampling_filter_label.pack(fill='x', padx=5, pady=5)

    resampling_filter_selector = ttk.Combobox(root)
    resampling_filter_selector['values'] = ['NEAREST (Quickest Lowest Quality)', 'BOX', 'BILINEAR (DEFAULT)', 'HAMMING', 'BICUBIC', 'LANCZOS (Slowest, Highest Quality)']
    resampling_filter_selector.current(2)
    resampling_filter_selector.pack(fill='x', padx=5, pady=5)

    return resampling_filter_selector

def _create_select_window_title_section(root):
    window_titles = []

    def add_title(hwnd, extra):
        if win32gui.IsWindowVisible(hwnd):
            window_titles.append(win32gui.GetWindowText(hwnd))

    win32gui.EnumWindows(add_title, None)

    window_titles = [title for title in window_titles if title is not None and title.strip() != '']
    window_titles.sort()

    select_window_label = ttk.Label(root, text='Select a target application to magnify:')
    select_window_label.pack(fill='x', padx=5, pady=5)

    window_title_selector = ttk.Combobox(root)
    window_title_selector['values'] = [title for title in window_titles if title is not None and title.strip() != '']
    window_title_selector.pack(fill='x', padx=5, pady=5)

    return window_title_selector

_CHECKBOX_DEFINITIONS = [
    (0, 'Capture mouse cursor when cursor is over target window'),
    (0, 'Make magnifier appear on top of other windows'),
    (0, 'Activate target window when magnifier is selected'),
    (0, 'Use screen capture mode (needed to properly magnify OpenGL and DirectX apps)')
]

def _create_check_mapper_function(root) -> Callable[[Tuple[int, str]], tk.StringVar]:
    def _create_checkbox(checkbox_definition: Tuple[int, str]) -> tk.StringVar:
        control = tk.StringVar(value=str(checkbox_definition[0]))
        check = ttk.Checkbutton(root, text=checkbox_definition[1], variable=control)
        check.pack(fill='x', padx=5, pady=5)
        return control
    return _create_checkbox

def _parse_resampling_filter_option(sampler_option: str) -> str:
    sampler_option = sampler_option.strip()
    if sampler_option == '':
        return 'BICUBIC'

    if ' ' in sampler_option:
        return sampler_option.split(' ')[0].strip()

    return sampler_option

def present_magnifier_options():
    root = tk.Tk()
    icon_path = get_icon_path()
    if icon_path is not None:
        root.iconphoto(False, tk.PhotoImage(file=icon_path))
    root.geometry('470x350')
    root.resizable(False, False)
    root.title('Window Magnifier Options')

    window_title_selector = _create_select_window_title_section(root)
    delay_level_slider = _create_select_delay_section(root)
    sample_selector = _create_select_resampling_filter(root)

    checkbox_mapper_func = _create_check_mapper_function(root)
    capture_cursor_check, always_on_top_check, refocus_to_target_check, screen_capture_check =\
        list(map(checkbox_mapper_func, _CHECKBOX_DEFINITIONS))

    def confirm_clicked():
        window_title = window_title_selector.get()
        capture_delay_interval = round(float(delay_level_slider.get()) / 1000.0, 5)
        resampling_filter = _parse_resampling_filter_option(sample_selector.get())
        capture_cursor = capture_cursor_check.get() == '1'
        always_on_top = always_on_top_check.get() == '1'
        refocus_to_target = refocus_to_target_check.get() == '1'
        use_screen_capture = screen_capture_check.get() == '1'
        if window_title.strip() == '':
            return
        arguments = Arguments(window_title, capture_delay_interval, resampling_filter, capture_cursor, always_on_top,
                              refocus_to_target, use_screen_capture)
        print(str(arguments))
        root.destroy()
        start_magnifier(arguments)

    confirm_button = ttk.Button(root, text="Magnify", command=confirm_clicked)
    confirm_button.pack(fill='x', padx=5, pady=5)

    root.mainloop()


#!/usr/bin/env python3

import sys

delay_value_list = sys.argv[1:]
char_delay = 10
delay_value_pairs = zip(delay_value_list[0::2], delay_value_list[1::2])

output = []

for pair in delay_value_pairs:
    delay, value = pair
    output.extend([1] * int(delay))
    for char in value:
        output.append(0)
        output.extend([int(x) for x in list(bin(ord(char))[2:].rjust(8, '0'))[::-1]])
        output.extend([1] * char_delay)

for bit in output:
    sys.stdout.write(chr(bit))
sys.stdout.flush()


from benchmark import BaseBench, metric_angle, metric_chrom_distance
from algorithms.shades_of_gray import AverageRGB, MaxRGB, PNorm
from algorithms.mixed_illuminant_kmeans import MixedKmeans
from algorithms.single_approx import BestSingleApprox
from images import SpectralImage, RGBImage

from joblib import Parallel, delayed

test_dir = 'fixtures/test'
output_dir = 'dist'
groundtruth_dir = 'fixtures/ground_truth'

algs: [BaseBench] = [BestSingleApprox]

def parallel_process(algorithm):
    alg_ins = algorithm(
        test_dir, f'{output_dir}/{algorithm.NAME}', groundtruth_dir)
    alg_ins.run()
    while alg_ins.has_next():
        alg_ins.get_next_refl()
        print(f'working on {alg_ins.curr_img_name}')
        angular_err = alg_ins.get_error(metric_angle)
        distance_err = alg_ins.get_error(metric_chrom_distance)
        alg_ins.draw_heatmap(angular_err, f'{alg_ins.curr_img_name}_ang')
        alg_ins.draw_heatmap(distance_err, f'{alg_ins.curr_img_name}_dis')

# Parallel(n_jobs=4)(delayed(parallel_process)(algorithm) for algorithm in algs)

parallel_process(BestSingleApprox)


#code

def CheckPalindrome(S):

    str1 = ""
    for i in range(len(S)):

        if str(S[i]).isalnum():
            str1 = str1 + str(S[i])

    str2 = ""
    for i in range(len(str1)-1, -1, -1):

        str2 = str2 + str(str1[i])

    if str1.lower() == str2.lower():
        return "YES"
    else:
        return "NO"

def main():
    t = int(input ())
    for tc in range (t):
        S = str(input ())
        print(CheckPalindrome(S))

if __name__=="__main__":
    main()


from flask import Blueprint
from controllers.general_controller import health
from controllers.image_controller import classify, get_all_imagens, list_classficiations_user, get_image, delete_classification, get_classification_by_hash
from controllers.user_controller import create_user, login, get_all

general_bp = Blueprint("general", __name__)
image_bp = Blueprint("image", __name__, url_prefix="/image")
user_bp = Blueprint("user", __name__, url_prefix="/user")

# User routes
user_bp.route("/login", methods=["POST"])(login)
user_bp.route("/get_all", methods=["GET"])(get_all)
# user_bp.route('/', methods=['GET'])(get_user)
user_bp.route("/", methods=["POST"])(create_user)

# Image routes
image_bp.route("/classify", methods=["POST"])(classify)
image_bp.route("/classify/get_all", methods=["GET"])(get_all_imagens)
image_bp.route("/classifications/<int:id>", methods=["GET"])(list_classficiations_user)
image_bp.route("/<string:hash>", methods=["GET"])(get_image)
image_bp.route("/<string:hash>/<int:user_id>", methods=["DELETE"])(delete_classification)
image_bp.route("/classification/hash/<string:hash>", methods=["GET"])(get_classification_by_hash)

# General
general_bp.route("/health", methods=["GET"])(health)


import os
from dotenv import load_dotenv
from google_images_search import GoogleImagesSearch

def get_artwork_image_url(query):
    load_dotenv()
    # Googles programmable search engine API key
    search_engine_key = os.getenv("PROGRAMMABLE_SEARCH_ENGINE_API_KEY")
    # Search Engine ID from google's programmable search engine controlpanel
    search_engine_id = os.getenv("SEARCH_ENGINE_ID")

    gis = GoogleImagesSearch(search_engine_key, search_engine_id)

    search_params = {
        "q": query,
        "num": 1,  # Number of images to fetch
        "safe": "high",  # Safety level (options: high, medium, off)
        "fileType": "jpg|png",  # Limit search to JPEG and PNG files
        "size": "medium",  # Filter for large images
        "rights": (  # Creative Commons licenses
            # "cc_publicdomain|"
            # "cc_attribute|"
            # "cc_sharealike|"
            "cc_noncommercial|"
            # "cc_nonderived"
        ),
    }

    gis.search(search_params=search_params)
    first_result = gis.results()[0]

    print("result.url", first_result.url)

    return first_result.url if first_result else None


# %%
import itertools
import numpy as np
import pandas as pd
import pickle
from scipy.stats import norm
import seaborn as sns

# ONLY SET UP FOR 1QB, 2RB, 2WR, 1TE, 1FLEX(RB,WR,TE) AS OF NOW
# HAVE NOT ATTEMPTED TWO FLEX POSITIONS, NOT SURE IF ITERTOOLS.PRODUCT WILL WORK

class Draft_Setup:

    def __init__(self, n_QB, n_RB, n_WR, n_TE, n_FLEX, flex_type, draft_picks, year):
        self.n_QB = n_QB
        self.n_RB = n_RB
        self.n_WR = n_WR
        self.n_TE = n_TE
        self.n_FLEX = n_FLEX
        # Either: Standard (RB, WR, TE), Super Flex (RB, WR, TE, QB), RB/WR or None
        self.flex_type = flex_type
        self.draft_picks = draft_picks
        self.year = year

    def initialize_player_data(self):
        '''

        Parameters
        ----------
        year : TYPE
            DESCRIPTION.

        Returns
        -------
        season_projections : Dataframe of all players with the following information:
            FPTS: Projected fantasy points for the season
            ADP Mean: Average draft position mean
            ADP Std: Average draft positition standard deviation
            WAR: Wins above replacement based on the projected fantasy points

        '''

        # Load the season projection data
        season_projections = pd.read_csv(
            './data/{year}/season_projections.csv'.format(year=self.year))

        # Load the linear regression models that define the wins above replacement
        # for each player based on their position
        WAR_file = open('./war_linear_models/PPR.pickle', 'rb')
        WAR_linear_models = pickle.load(WAR_file)

        def apply_WAR_model(points, position):
            '''

            Parameters
            ----------
            points : Projected fantasy points for the season to be input
            into the linear regression model.
            position : Players position that will be used to look up the
            linear regression model to calculate the WAR.

            Returns
            -------
            WAR : The players projected wins above replacement based on the
            projected season fantasy points.

            '''
            WAR = float(WAR_linear_models[position].predict([[points]]))
            return WAR

        # Apply the calculate_WAR function for each player to determine the predicted
        # wins above replacement.
        # Must be done iterating over rows (unless there's an easier options)
        season_projections['WAR'] = season_projections[['FPTS', 'Pos']].apply(
            lambda row: apply_WAR_model(*row), axis=1)

        return season_projections

    def position_combinations(self):
        '''

        Returns
        -------
        all_position_combinations :

        '''

        # Setup the list of starting positions and the number of starters (excluding K, and DST or IDP)
        positions = {
            'QB': self.n_QB,
            'RB': self.n_RB,
            'WR': self.n_WR,
            'TE': self.n_TE,
            'FLEX': self.n_FLEX
        }

        combined_positions = []
        for key, value in positions.items():
            for i in range(value):
                combined_positions.append(key)

        # Setup the list of flex positions based on the user input
        if self.flex_type == 'Standard':
            flex_positions = ['RB', 'WR', 'TE']
        elif self.flex_type == 'Super Flex':
            flex_positions = ['QB', 'RB', 'WR', 'TE']
        elif self.flex_type == 'RB/WR':
            flex_positions = ['RB', 'WR']
        elif self.flex_type == 'None':
            pass

        # Initial list of of draft combinations with the FLEX "position"
        initial_draft_combinations = (
            set(itertools.permutations(combined_positions)))

        # Skip all the extra steps if there are no FLEX "positions"
        if self.flex_type != 'None':
            '''
            Map the flex_positions list to every location where the FLEX "position" occurs
            Convert each non-Flex position to it's own list to properly use itertools.product in the next step
            Use itertools.product to get all lists from list containing flex players list

            EXAMPLE:
            Intial list: ['QB','WR','TE','FLEX','RB','RB','WR']
            List with flex replaced: ['QB','WR','TE',['RB','WR','TE'],'RB','RB','WR']
            Lists after itertools.product:
                    ['QB','WR','TE','RB','RB','RB','WR']
                    ['QB','WR','TE','WR','RB','RB','WR']
                    ['QB','WR','TE','TE','RB','RB','WR']
            '''
            modified_draft_combinations = [list(map(lambda x: [x] if x != 'FLEX' else flex_positions, i))
                                           for i in initial_draft_combinations]

            modified_draft_combinations = [list(itertools.product(*modified_draft_combinations[i]))
                                           for i in range(len(modified_draft_combinations))]

            # The modified_draft_combinations is a list of lists of lists
            # Reappend the list so it is a list of lists instead
            draft_combinations_combined = []
            for outside_list in modified_draft_combinations:
                for i in range(len(outside_list)):
                    draft_combinations_combined.append(outside_list[i])

            # Convert the list of lists into a dataframe
            all_position_combinations = pd.DataFrame(
                set(draft_combinations_combined))
        else:
            all_position_combinations = pd.DataFrame(
                (initial_draft_combinations))

        return all_position_combinations

def positional_value_by_round(active_pick, draft_picks, undrafted_players):
    '''

    Parameters
    ----------
    active_pick : Integer of the active pick in the draft
    draft_picks : List of the managers draft picks
    undrafted_players : Dataframe of all undrafted players

    Returns
    -------
    position_value_by_round : Dictionary of dictionaries
    The nested dictionary contains the positional values for each position per round. These
    values are the probabilistic wins above replacement for the position in the specific
    round. The outer dictionary key is the round number (e.g. 1, 2, 3, etc.).
    These values will be used to check all possible draft combinations to determine
    the most optimal player to draft.

    '''

    def grouped_probability(df, i):

        # Sort the WAR value descending in order to classify the order of "best" players
        df.sort_values(by='WAR', ascending=False)

        # Calculate the probability that each player is available at the specified draft pick
        df['p_Available'] = 1-norm(df.loc[:,'ADP Avg'], df.loc[:,'ADP Std']).cdf(i)

        # Calculate the probability that each player is NOT available at the specified draft pick
        df['p_Not Available'] = norm(df['ADP Avg'], df['ADP Std']).cdf(i)

        # Calculate the probability that this player is the best available player at their position
        # First, take the cumulative product of the p_Not Available for all players better than
        # the player in question. This is accomplished by shifting the values down 1 since
        # cumprod() also includes the the row we are analyzing.
        # Then multiply the player in questions p_Available by the cumulative product previously calculated.
        df['p_Best Available'] = (
            (df['p_Not Available'].cumprod()).shift(1))*df['p_Available']

        # Since the dataframes were shifted, the top player in each dataframe will have an nan value.
        # Replace the nan value with this players p_Available since they are the best available.
        df['p_Best Available'].fillna(df['p_Available'], inplace=True)

        # Determine the adjusted player WAR based on the p_Best Available
        # dWAR = dynamic Wins Above Replacement
        df['dWAR'] = df['WAR']*df['p_Best Available']

        return df

    position_value_by_round = {}
    round_counter = 1
    for i in draft_picks:

        # Only consider future rounds.
        if i > active_pick:

            undrafted_players = undrafted_players.groupby(
                by='Pos').apply(grouped_probability, i=i)
            positional_value = undrafted_players.groupby('Pos').sum()['dWAR']
            position_value_by_round[round_counter] = positional_value.to_dict()
        else:
            pass

        round_counter = round_counter+1

    return position_value_by_round

def remaining_draft_value(active_pick, draft_picks, position_value_by_round, position_combinations):
    '''

    Parameters
    ----------
    active_pick : Integer of the active pick in the draft
    draft_picks : List of the managers draft picks
    position_value_by_round : Dictionary of dictionaries with values for each position in each remaining round
    undrafted_players : Dataframe of all undrafted players

    Returns
    -------
    combined_position_value : Dictionary
        Combining all positional values by round for the remaining draft combinations. Dictionary has
        a single value for each position. This is then added to all undrafted players WAR value in the main()
        function based on their position.

    '''

    round_counter = 1
    temp_df = pd.DataFrame()
    for i in draft_picks:
        # Only consider future rounds.
        if i > active_pick:
            temp_df[round_counter] = position_combinations.loc[:,
                                                               round_counter-1].map(position_value_by_round[round_counter])
        # If pick has already occured, drop that rounds column from the combinations
        else:
            pass
        round_counter = 1+round_counter

    position_combinations['Total'] = temp_df.sum(axis=1)
    position_combinations.sort_values(
        by='Total', ascending=False, inplace=True)

    current_round = draft_picks.index(active_pick)
    combined_position_value = position_combinations.groupby(
        position_combinations.iloc[:, current_round])['Total'].max()
    combined_position_value = combined_position_value.to_dict()
    return combined_position_value

def start_draft():
    draft_picks = [3, 22, 27, 46, 51, 70, 75]
    Draft = Draft_Setup(1, 2, 2, 1, 1, 'Standard', draft_picks, 2021)
    all_players = Draft.initialize_player_data()
    all_position_combinations = Draft.position_combinations()

    drafted_players = []
    my_team = []
    active_pick = 1
    remaining_draft_combinations = all_position_combinations.copy()
    for j in range(1, 76):
        undrafted_players = all_players[~all_players['Player'].isin(
            drafted_players)]

        if j in draft_picks:

            # Calculate the positional value by round
            position_value_by_round = positional_value_by_round(
                active_pick, draft_picks, undrafted_players)

            # Calculate combined value of all future rounds by position
            combined_position_value = remaining_draft_value(
                active_pick, draft_picks, position_value_by_round, remaining_draft_combinations)

            # Calculated the dynamic WAR for each player
            undrafted_players.loc[:,'dWAR'] = undrafted_players['Pos'].map(
                combined_position_value)+undrafted_players.loc[:,'WAR']
            undrafted_players.sort_values(
                by='dWAR', ascending=False, inplace=True)

            # Get the managers selection
            cols_to_display = ['Player', 'Team', 'Pos', 'FPTS']
            print(undrafted_players[cols_to_display].head(10))
            player_selected = int(input('Enter the player index : '))

            # Add the selected player to the drafted_players list and the my_team list
            drafted_players.append(
                undrafted_players.loc[player_selected, 'Player'])
            my_team.append(undrafted_players.loc[player_selected, 'Player'])

            # Drop all rows without the players position in the selected round from
            # the remaining_draft_combinations dataframe
            drafted_position = undrafted_players.loc[player_selected, 'Pos']
            column_lookup = draft_picks.index(j)

            # Remove all draft combinations that didn't include the position selected this round
            remaining_draft_combinations = remaining_draft_combinations[remaining_draft_combinations.iloc[:, column_lookup].isin([drafted_position])]
            print(remaining_draft_combinations.head(10))

            active_pick = active_pick+1
        else:
            undrafted_players.sort_values(
                by='ADP Avg', ascending=True, inplace=True)

            # Add the selected player to the drafted_players list
            # cols_to_display=['Player','Team','Pos','FPTS']
            # print(undrafted_players[cols_to_display].head(10))
            #player_selected=int(input('Enter the player index : '))
            undrafted_players.sort_values(
                by='ADP Avg', ascending=True, inplace=True)
            player_selected = undrafted_players.iloc[0, undrafted_players.columns.get_loc(
                'Player')]

            # Add the selected player to the drafted_players list
            # loc[player_selected,'Player'])
            drafted_players.append(player_selected)
            active_pick = active_pick+1

start_draft()

#%%

# Incorporate injury risk by position into the value check
# for example, take another RB even though they'll be on your bench or take a WR that will be in your starting lineup

# %%


from fnmatch import fnmatch
from logging import getLogger
from typing import Optional, Set, Type

# FIXME import from typing when support for 3.7 is removed
from typing_extensions import final

from opentelemetry.metrics import Instrument
from opentelemetry.sdk.metrics._internal.aggregation import (
    Aggregation,
    DefaultAggregation,
)

_logger = getLogger(__name__)

class View:
    """
    A `View` configuration parameters can be used for the following
    purposes:

    1. Match instruments: When an instrument matches a view, measurements
       received by that instrument will be processed.
    2. Customize metric streams: A metric stream is identified by a match
       between a view and an instrument and a set of attributes. The metric
       stream can be customized by certain attributes of the corresponding view.

    The attributes documented next serve one of the previous two purposes.

    Args:
        instrument_type: This is an instrument matching attribute: the class the
            instrument must be to match the view.

        instrument_name: This is an instrument matching attribute: the name the
            instrument must have to match the view. Wild card characters are supported. Wild
            card characters should not be used with this attribute if the view has also a
            ``name`` defined.

        meter_name: This is an instrument matching attribute: the name the
            instrument meter must have to match the view.

        meter_version: This is an instrument matching attribute: the version
            the instrument meter must have to match the view.

        meter_schema_url: This is an instrument matching attribute: the schema
            URL the instrument meter must have to match the view.

        name: This is a metric stream customizing attribute: the name of the
            metric stream. If `None`, the name of the instrument will be used.

        description: This is a metric stream customizing attribute: the
            description of the metric stream. If `None`, the description of the instrument will
            be used.

        attribute_keys: This is a metric stream customizing attribute: this is
            a set of attribute keys. If not `None` then only the measurement attributes that
            are in ``attribute_keys`` will be used to identify the metric stream.

        aggregation: This is a metric stream customizing attribute: the
            aggregation instance to use when data is aggregated for the
            corresponding metrics stream. If `None` an instance of
            `DefaultAggregation` will be used.

        instrument_unit: This is an instrument matching attribute: the unit the
            instrument must have to match the view.

    This class is not intended to be subclassed by the user.
    """

    _default_aggregation = DefaultAggregation()

    def __init__(
        self,
        instrument_type: Optional[Type[Instrument]] = None,
        instrument_name: Optional[str] = None,
        meter_name: Optional[str] = None,
        meter_version: Optional[str] = None,
        meter_schema_url: Optional[str] = None,
        name: Optional[str] = None,
        description: Optional[str] = None,
        attribute_keys: Optional[Set[str]] = None,
        aggregation: Optional[Aggregation] = None,
        instrument_unit: Optional[str] = None,
    ):
        if (
            instrument_type
            is instrument_name
            is instrument_unit
            is meter_name
            is meter_version
            is meter_schema_url
            is None
        ):
            raise Exception(
                "Some instrument selection "
                f"criteria must be provided for View {name}"
            )

        if (
            name is not None
            and instrument_name is not None
            and ("*" in instrument_name or "?" in instrument_name)
        ):

            raise Exception(
                f"View {name} declared with wildcard "
                "characters in instrument_name"
            )

        # _name, _description, _aggregation and _attribute_keys will be
        # accessed when instantiating a _ViewInstrumentMatch.
        self._name = name
        self._instrument_type = instrument_type
        self._instrument_name = instrument_name
        self._instrument_unit = instrument_unit
        self._meter_name = meter_name
        self._meter_version = meter_version
        self._meter_schema_url = meter_schema_url

        self._description = description
        self._attribute_keys = attribute_keys
        self._aggregation = aggregation or self._default_aggregation

    # pylint: disable=too-many-return-statements
    # pylint: disable=too-many-branches
    @final
    def _match(self, instrument: Instrument) -> bool:

        if self._instrument_type is not None:
            if not isinstance(instrument, self._instrument_type):
                return False

        if self._instrument_name is not None:
            if not fnmatch(instrument.name, self._instrument_name):
                return False

        if self._instrument_unit is not None:
            if not fnmatch(instrument.unit, self._instrument_unit):
                return False

        if self._meter_name is not None:
            if instrument.instrumentation_scope.name != self._meter_name:
                return False

        if self._meter_version is not None:
            if instrument.instrumentation_scope.version != self._meter_version:
                return False

        if self._meter_schema_url is not None:
            if (
                instrument.instrumentation_scope.schema_url
                != self._meter_schema_url
            ):
                return False

        return True


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

import datetime

import mock

from google.appengine.ext import ndb

from dashboard.pinpoint.models.change import change_test
from dashboard.pinpoint.models import isolate
from dashboard.pinpoint import test

class IsolateTest(test.TestCase):

  def testPutAndGet(self):
    isolate_infos = (
        ('Mac Builder Perf', change_test.Change(1), 'target_name',
         'https://isolate.server', '7c7e90be'),
        ('Mac Builder Perf', change_test.Change(2), 'target_name',
         'https://isolate.server', '38e2f262'),
    )
    isolate.Put(isolate_infos)

    isolate_server, isolate_hash = isolate.Get('Mac Builder Perf',
                                               change_test.Change(1),
                                               'target_name')
    self.assertEqual(isolate_server, 'https://isolate.server')
    self.assertEqual(isolate_hash, '7c7e90be')

  def testUnknownIsolate(self):
    with self.assertRaises(KeyError):
      isolate.Get('Wrong Builder', change_test.Change(1), 'target_name')

  @mock.patch.object(isolate, 'datetime')
  def testExpiredIsolate(self, mock_datetime):
    isolate_infos = (('Mac Builder Perf', change_test.Change(1), 'target_name',
                      'https://isolate.server', '7c7e90be'),)
    isolate.Put(isolate_infos)

    # Teleport to the future after the isolate is expired.
    mock_datetime.datetime.utcnow.return_value = (
        datetime.datetime.utcnow() + isolate.ISOLATE_EXPIRY_DURATION +
        datetime.timedelta(days=1))
    mock_datetime.timedelta = datetime.timedelta

    with self.assertRaises(KeyError):
      isolate.Get('Mac Builder Perf', change_test.Change(1), 'target_name')

  def testDeleteExpiredIsolate(self):
    isolate_infos = (
        ('Mac Builder Perf', change_test.Change(0), 'target_name',
         'https://isolate.server', '123'),
        ('Mac Builder Perf', change_test.Change(1), 'target_name',
         'https://isolate.server', '456'),
    )
    isolate.Put(isolate_infos)

    cur = ndb.Key(
        'Isolate',
        isolate._Key('Mac Builder Perf', change_test.Change(0),
                     'target_name')).get()
    cur.created = datetime.datetime.utcnow() - datetime.timedelta(hours=1)
    cur.put()

    cur = ndb.Key(
        'Isolate',
        isolate._Key(isolate_infos[1][0], isolate_infos[1][1],
                     isolate_infos[1][2])).get()
    cur.created = datetime.datetime.utcnow() - (
        isolate.ISOLATE_EXPIRY_DURATION + datetime.timedelta(hours=1))
    cur.put()

    isolate.DeleteExpiredIsolates()

    q = isolate.Isolate.query()
    isolates = q.fetch()

    self.assertEqual(1, len(isolates))
    self.assertEqual('123', isolates[0].isolate_hash)


# -*- coding: utf-8 -*-
import logging
import os
import datetime
import time

class SingletonType(type):
    _instances = {}

    def __call__(cls, *args, **kwargs):
        print("call")
        print(cls._instances)
        if cls not in cls._instances:
            cls._instances[cls] = super(SingletonType, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

# python 3 style
class MyLogger(object, metaclass=SingletonType):
    _logger = None

    # def __call__():
    #     print("call MyLogger")

    def __init__(self):
        print("init")
        self._logger = logging.getLogger("crumbs")
        self._logger.setLevel(logging.DEBUG)
        formatter = logging.Formatter('%(asctime)s \t [%(levelname)s | %(filename)s:%(lineno)s] > %(message)s')

        now = datetime.datetime.now()
        dirname = "./log"

        if not os.path.isdir(dirname):
            os.mkdir(dirname)
        fileHandler = logging.FileHandler(dirname + "/log_" + now.strftime("%Y-%m-%d") + ".log")

        streamHandler = logging.StreamHandler()

        fileHandler.setFormatter(formatter)
        streamHandler.setFormatter(formatter)

        self._logger.addHandler(fileHandler)
        self._logger.addHandler(streamHandler)

        print("Generate new instance")

    def get_logger(self):
        return self._logger

class A:
    pass

# a simple usecase
if __name__ == "__main__":
    # MyLogger()
    # logger = MyLogger.__call__().get_logger()
    logger = MyLogger().get_logger()
    # logger = MyLogger()
    # print(type(logger))
    # print(type(type(logger)))
    # print(type(type(type(logger))))
    # print(type(MyLogger))
    # print(type(type(MyLogger)))
    # print(type(A))
    # a = A()
    # print(type(a))
    logger.info("Hello, Logger")
    logger.debug("bug occurred")
    logger2 = MyLogger.__call__().get_logger()
    print(logger is logger2)
    # logger()


from http.server import BaseHTTPRequestHandler, HTTPServer
import json
import argparse
import model.config as config
from gerbil.nn_processing import NNProcessing
from model.util import load_train_args
from gerbil.build_entity_universe import BuildEntityUniverse

class GetHandler(BaseHTTPRequestHandler):

    def do_POST(self):
        content_length = int(self.headers['Content-Length'])
        post_data = self.rfile.read(content_length)
        self.send_response(200)
        self.end_headers()

        if args.build_entity_universe:
            buildEntityUniverse.process(*read_json(post_data))
            response = []
        else:
            response = nnprocessing.process(*read_json(post_data))

        print("response in server.py code:\n", response)
        self.wfile.write(bytes(json.dumps(response), "utf-8"))
        return

def read_json(post_data):
    data = json.loads(post_data.decode("utf-8"))
    #print("received data:", data)
    text = data["text"]
    spans = [(int(j["start"]), int(j["length"])) for j in data["spans"]]
    return text, spans

def _parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--experiment_name", default="per_document_no_wikidump",
                        help="under folder data/tfrecords/")
    parser.add_argument("--training_name", default="doc_fixed_nowiki_evecsl2dropout")
    parser.add_argument("--all_spans_training", type=bool, default=False)
    parser.add_argument("--el_mode", dest='el_mode', action='store_true')
    parser.add_argument("--ed_mode", dest='el_mode', action='store_false')
    parser.set_defaults(el_mode=True)

    parser.add_argument("--running_mode", default=None, help="el_mode or ed_mode, so"
                                "we can restore an ed_mode model and run it for el")

    parser.add_argument("--lowercase_spans_pem", type=bool, default=False)

    parser.add_argument("--entity_extension", default=None, help="extension_entities or extension_entities_all etc")

    # those are for building the entity set
    parser.add_argument("--build_entity_universe", type=bool, default=False)
    parser.add_argument("--hardcoded_thr", type=float, default=None, help="0, 0.2")
    parser.add_argument("--el_with_stanfordner_and_our_ed", type=bool, default=False)

    parser.add_argument("--persons_coreference", type=bool, default=False)
    parser.add_argument("--persons_coreference_merge", type=bool, default=False)

    args = parser.parse_args()
    if args.persons_coreference_merge:
        args.persons_coreference = True
    print(args)
    if args.build_entity_universe:
        return args, None

    temp = "all_spans_" if args.all_spans_training else ""
    args.experiment_folder = config.base_folder+"data/tfrecords/" + args.experiment_name+"/"

    args.output_folder = config.base_folder+"data/tfrecords/" + \
                         args.experiment_name+"/{}training_folder/".format(temp) + \
                         args.training_name+"/"

    train_args = load_train_args(args.output_folder, "gerbil")
    train_args.entity_extension = args.entity_extension

    print(train_args)
    return args, train_args

def terminate():
    tee.close()
    if args.build_entity_universe:
        buildEntityUniverse.flush_entity_universe()
    else:
        print("from_myspans_to_given_spans_map_errors:", nnprocessing.from_myspans_to_given_spans_map_errors)

if __name__ == "__main__":
    args, train_args = _parse_args()
    if args.build_entity_universe:
        buildEntityUniverse = BuildEntityUniverse()
    else:
        nnprocessing = NNProcessing(train_args, args)
    server = HTTPServer(('localhost', 5555), GetHandler)
    print('Starting server at http://localhost:5555')
    from model.util import Tee
    tee = Tee('server.txt', 'w')
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        terminate()
        exit(0)


# coding=UTF-8
data = []
count = 0 #計數
with open('reviews.txt', 'r') as f:
	for line in f:
		data.append(line)
		count += 1 # count = count + 1
		if count % 1000 == 0: # if count/1000 remainder is 0
			print(len(data)) #print when read every 1000 data

print('Finish reading', len(data), 'data') #print how many reviews

sum_len = 0 #目前的留言總長度
for d in data:
	sum_len = sum_len + len(d) #目前已累積的留言總長度加上下一筆的留言長度
print('The average length of review is', sum_len/len(data)) #需離開for loop才不會每一次加總的平均都算出來

#filter
new = []
for d in data:
	if len(d) < 100:
		new.append(d)
print('Totall', len(new), 'data length less than 100')
print('----------------------------')
print(new[0])
print('----------------------------')
print(new[1])


# -*- coding: utf-8 -*-
"""
This file contains analytic implementations of rotation methods.
"""

import numpy as np
import scipy as sp

def target_rotation(A, H, full_rank=False):
    r"""
    Analytically performs orthogonal rotations towards a target matrix,
    i.e., we minimize:

    .. math::
        \phi(L) =\frac{1}{2}\|AT-H\|^2.

    where :math:`T` is an orthogonal matrix. This problem is also known as
    an orthogonal Procrustes problem.

    Under the assumption that :math:`A^*H` has full rank, the analytical
    solution :math:`T` is given by:

    .. math::
        T = (A^*HH^*A)^{-\frac{1}{2}}A^*H,

    see Green (1952). In other cases the solution is given by :math:`T = UV`,
    where :math:`U` and :math:`V` result from the singular value decomposition
    of :math:`A^*H`:

    .. math::
        A^*H = U\Sigma V,

    see Schonemann (1966).

    Parameters
    ----------
    A : numpy matrix (default None)
        non rotated factors
    H : numpy matrix
        target matrix
    full_rank : bool (default FAlse)
        if set to true full rank is assumed

    Returns
    -------
    The matrix :math:`T`.

    References
    ----------
    [1] Green (1952, Psychometrika) - The orthogonal approximation of an
    oblique structure in factor analysis

    [2] Schonemann (1966) - A generalized solution of the orthogonal
    procrustes problem

    [3] Gower, Dijksterhuis (2004) - Procrustes problems
    """
    ATH = A.T.dot(H)
    if full_rank or np.linalg.matrix_rank(ATH) == A.shape[1]:
        T = sp.linalg.fractional_matrix_power(ATH.dot(ATH.T), -1/2).dot(ATH)
    else:
        U, D, V = np.linalg.svd(ATH, full_matrices=False)
        T = U.dot(V)
    return T

def procrustes(A, H):
    r"""
    Analytically solves the following Procrustes problem:

    .. math::
        \phi(L) =\frac{1}{2}\|AT-H\|^2.

    (With no further conditions on :math:`H`)

    Under the assumption that :math:`A^*H` has full rank, the analytical
    solution :math:`T` is given by:

    .. math::
        T = (A^*HH^*A)^{-\frac{1}{2}}A^*H,

    see Navarra, Simoncini (2010).

    Parameters
    ----------
    A : numpy matrix
        non rotated factors
    H : numpy matrix
        target matrix
    full_rank : bool (default False)
        if set to true full rank is assumed

    Returns
    -------
    The matrix :math:`T`.

    References
    ----------
    [1] Navarra, Simoncini (2010) - A guide to empirical orthogonal functions
    for climate data analysis
    """
    return np.linalg.inv(A.T.dot(A)).dot(A.T).dot(H)

def promax(A, k=2):
    r"""
    Performs promax rotation of the matrix :math:`A`.

    This method was not very clear to me from the literature, this
    implementation is as I understand it should work.

    Promax rotation is performed in the following steps:

    * Determine varimax rotated patterns :math:`V`.

    * Construct a rotation target matrix :math:`|V_{ij}|^k/V_{ij}`

    * Perform procrustes rotation towards the target to obtain T

    * Determine the patterns

    First, varimax rotation a target matrix :math:`H` is determined with
    orthogonal varimax rotation.
    Then, oblique target rotation is performed towards the target.

    Parameters
    ----------
    A : numpy matrix
        non rotated factors
    k : float
        parameter, should be positive

    References
    ----------
    [1] Browne (2001) - An overview of analytic rotation in exploratory
    factor analysis

    [2] Navarra, Simoncini (2010) - A guide to empirical orthogonal functions
    for climate data analysis
    """
    assert k > 0
    # define rotation target using varimax rotation:
    from ._wrappers import rotate_factors
    V, T = rotate_factors(A, 'varimax')
    H = np.abs(V)**k/V
    # solve procrustes problem
    S = procrustes(A, H)  # np.linalg.inv(A.T.dot(A)).dot(A.T).dot(H);
    # normalize
    d = np.sqrt(np.diag(np.linalg.inv(S.T.dot(S))))
    D = np.diag(d)
    T = np.linalg.inv(S.dot(D)).T
    return A.dot(T), T


from src import constructor

if __name__ == "__main__":
    course_id = 35816
    inputs = {
        "course_id": course_id,
        "store_dir": f"course-{course_id}",
        "target_website": "umass.moonami.com",
    }

    content = constructor(**inputs)
    content.extraction()
    content.construct_sections()


# -*- coding: utf-8 -*-
"""
Created on Fri Feb 23 17:11:55 2018

@author: Ian
"""
import numpy as np
import arm

def rung_kutta4t(obj, x1, u, dt):
    obj = arm.Arm()
    k1 = obj.xdot(x1, u)
    k2 = obj.xdot(x1 + dt / 2 * k1, u)
    k3 = obj.xdot(x1 + dt / 2 * k2, u)
    k4 = obj.xdot(x1 + dt * k3, u)

    x2 = x1 + dt / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
    return x2

def rung_kutta4(obj, x1, u, dt):
    k1 = obj.xdot(x1, u)
    k2 = obj.xdot(x1 + dt / 2 * k1, u)
    k3 = obj.xdot(x1 + dt / 2 * k2, u)
    k4 = obj.xdot(x1 + dt * k3, u)

    x2 = x1 + dt / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
    return x2

def close_enough(x, target):
    ptol = 0.1
    vtol = 0.2

    return (np.linalg.norm(x[0:2]) < ptol) and (np.linalg.norm(x[2:]) < vtol)

def reward(x, u, target):
    pos_pen = 10
    vel_pen = 0.01
    u_pen = 0.001
    cost = pos_pen * np.linalg.norm(x[0:2]-target[0:2]) + vel_pen * np.linalg.norm(x[2:]-target[2:]) + u_pen * np.linalg.norm(u)
    return -cost