base_time = self.last_run<EOL>if self.last_run == HAS_NOT_RUN:<EOL><INDENT>if self.wait_for_schedule is False:<EOL><INDENT>self.next_run = timezone.now()<EOL>self.wait_for_schedule = False <EOL>self.save()<EOL>return<EOL><DEDENT>else:<EOL><INDENT>base_time = timezone.now()<EOL><DEDENT><DEDENT>self.next_run = croniter(self.schedule, base_time).get_next(datetime)<EOL>self.save()<EOL>


Channel(RUN_TASK_CHANNEL).send({'<STR_LIT:id>':self.pk, '<STR_LIT>': timestamp.timestamp()})<EOL>

Internal instance method to submit this task for running immediately.
        Does not handle any iteration, end-date, etc., processing.


the_callable = self.func_from_info()<EOL>try:<EOL><INDENT>task_message = dict(<EOL>task=self,<EOL>channel_message=message,<EOL>)<EOL>the_callable(task_message)<EOL><DEDENT>finally:<EOL><INDENT>if self.end_running < self.next_run:<EOL><INDENT>self.enabled=False<EOL>Channel(KILL_TASK_CHANNEL).send({'<STR_LIT:id>': self.pk})<EOL>return<EOL><DEDENT>if self.iterations == <NUM_LIT:0>:<EOL><INDENT>return<EOL><DEDENT>else:<EOL><INDENT>self.iterations -= <NUM_LIT:1><EOL>if self.iterations == <NUM_LIT:0>:<EOL><INDENT>self.enabled = False<EOL>Channel(KILL_TASK_CHANNEL).send({'<STR_LIT:id>':self.pk})<EOL><DEDENT>self.save()<EOL><DEDENT><DEDENT>

Internal instance method run by worker process to actually run the task callable.


now = timezone.now()<EOL>self.last_run = now<EOL>self.calc_next_run()<EOL>self.save()<EOL>self.submit(now)<EOL>

Instance method to run this task immediately.


@classmethod<EOL><INDENT>def run_iterations(cls, the_callable, iterations=<NUM_LIT:1>, label=None, schedule='<STR_LIT>', userdata = None, run_immediately=False, delay_until=None):<DEDENT>

task = task_with_callable(the_callable, label=label, schedule=schedule, userdata=userdata)<EOL>task.iterations = iterations<EOL>if delay_until is not None:<EOL><INDENT>if isinstance(delay_until, datetime):<EOL><INDENT>if delay_until > timezone.now():<EOL><INDENT>task.start_running = delay_until<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>if run_immediately:<EOL><INDENT>task.next_run = timezone.now()<EOL><DEDENT>else:<EOL><INDENT>task.calc_next_run()<EOL><DEDENT>task.save()<EOL>

Class method to run a callable with a specified number of iterations


@classmethod<EOL><INDENT>def run_once(cls, the_callable, userdata=None, delay_until=None):<DEDENT>

cls.run_iterations(the_callable, userdata=userdata, run_immediately=True, delay_until=delay_until)<EOL>

Class method to run a one-shot task, immediately.


then = arrow.get(message['<STR_LIT:time>'])<EOL>now = arrow.get()<EOL>if (now - then) > timezone.timedelta(seconds=(TICK_FREQ+<NUM_LIT:1>)):<EOL><INDENT>pass <EOL><DEDENT>else:<EOL><INDENT>Task.run_tasks()<EOL><DEDENT>

Internal ``CLOCK_CHANNEL`` consumer to process task runs


task = Task.objects.get(pk=message['<STR_LIT:id>'])<EOL>if task.allow_overlap:<EOL><INDENT>task.run(message)<EOL><DEDENT>else:<EOL><INDENT>if not task.running:<EOL><INDENT>task.running = True<EOL>task.save()<EOL>try:<EOL><INDENT>task.run(message)<EOL><DEDENT>finally:<EOL><INDENT>task.running = False<EOL>task.save()<EOL><DEDENT><DEDENT><DEDENT>

Internal ``RUN_TASK`` consumer to run the task's callable


task = Task.objects.get(pk=message['<STR_LIT:id>'])<EOL>task.delete()<EOL>

Internal ``KILL_TASK`` consumer to remove retired tasks


r.send(stream=stream)<EOL>return r.response<EOL>

Just sends the request using its send method and returns its response.


def map(requests, stream=True, pool=None, size=<NUM_LIT:1>, exception_handler=None):

pool = pool if pool else Pool(size)<EOL>requests = list(requests)<EOL>requests = pool.map(send, requests)<EOL>ret = []<EOL>for request in requests:<EOL><INDENT>if request.response is not None:<EOL><INDENT>ret.append(request.response)<EOL><DEDENT>elif exception_handler and hasattr(request, '<STR_LIT>'):<EOL><INDENT>ret.append(exception_handler(request, request.exception))<EOL><DEDENT>else:<EOL><INDENT>ret.append(None)<EOL><DEDENT><DEDENT>if not pool:<EOL><INDENT>pool.close()<EOL><DEDENT>return ret<EOL>

Concurrently converts a list of Requests to Responses.

    :param requests: a collection of Request objects.
    :param stream: If False, the content will not be downloaded immediately.
    :param size: Specifies the number of workers to run at a time. If 1, no parallel processing.
    :param exception_handler: Callback function, called when exception occured. Params: Request, Exception


def imap(requests, stream=True, pool=None, size=<NUM_LIT:2>, exception_handler=None):

def send(r):<EOL><INDENT>return r.send(stream=stream)<EOL><DEDENT>pool = pool if pool else Pool(size)<EOL>for request in pool.imap(send, requests):<EOL><INDENT>if request.response is not None:<EOL><INDENT>yield request.response<EOL><DEDENT>elif exception_handler:<EOL><INDENT>exception_handler(request, request.exception)<EOL><DEDENT><DEDENT>if not pool:<EOL><INDENT>pool.close()<EOL><DEDENT>

Concurrently converts a generator object of Requests to
    a generator of Responses.

    :param requests: a generator of Request objects.
    :param stream: If False, the content will not be downloaded immediately.
    :param size: Specifies the number of requests to make at a time. default is 2
    :param exception_handler: Callback function, called when exception occured. Params: Request, Exception


def imap_unordered(requests, stream=True, pool=None, size=<NUM_LIT:2>, exception_handler=None):

def send(r):<EOL><INDENT>return r.send(stream=stream)<EOL><DEDENT>pool = pool if pool else Pool(size)<EOL>with contextlib.closing(Pool(size)) as pool:<EOL><INDENT>for request in pool.imap_unordered(send, requests):<EOL><INDENT>if request.response is not None:<EOL><INDENT>yield request.response<EOL><DEDENT>elif exception_handler:<EOL><INDENT>exception_handler(request, request.exception)<EOL><DEDENT><DEDENT><DEDENT>if not pool:<EOL><INDENT>pool.close()<EOL><DEDENT>


merged_kwargs = {}<EOL>merged_kwargs.update(self.kwargs)<EOL>merged_kwargs.update(kwargs)<EOL>try:<EOL><INDENT>self.response = self.session.request(<EOL>self.method, self.url, **merged_kwargs)<EOL><DEDENT>except Exception as e:<EOL><INDENT>self.exception = e<EOL>self.traceback = traceback.format_exc()<EOL><DEDENT>return self<EOL>

Prepares request based on parameter passed to constructor and optional
``kwargs```. Then sends request and saves response to :attr:`response`.

:returns: ``Response``


try:<EOL><INDENT>width = int(os.environ['<STR_LIT>'])<EOL><DEDENT>except (KeyError, ValueError):<EOL><INDENT>width = get_terminal_size().columns<EOL><DEDENT>return width or <NUM_LIT><EOL>

Figure out a reasonable default with. Use os.environ['COLUMNS'] if possible,
    and failing that use 80.


def columnize(array, displaywidth=<NUM_LIT>, colsep = '<STR_LIT:U+0020>',<EOL>arrange_vertical=True, ljust=True, lineprefix='<STR_LIT>',<EOL>opts={}):

if not isinstance(array, (list, tuple)):<EOL><INDENT>raise TypeError((<EOL>'<STR_LIT>'))<EOL><DEDENT>o = {}<EOL>if len(opts.keys()) > <NUM_LIT:0>:<EOL><INDENT>for key in default_opts.keys():<EOL><INDENT>o[key] = get_option(key, opts)<EOL>pass<EOL><DEDENT>if o['<STR_LIT>']:<EOL><INDENT>o['<STR_LIT>'] = '<STR_LIT:[>'<EOL>o['<STR_LIT>']   = '<STR_LIT:U+0020>'<EOL>o['<STR_LIT>']   = "<STR_LIT>"<EOL>o['<STR_LIT>'] = "<STR_LIT>"<EOL>o['<STR_LIT>']       = '<STR_LIT:U+002CU+0020>'<EOL>o['<STR_LIT>'] = False<EOL>pass<EOL><DEDENT><DEDENT>else:<EOL><INDENT>o = default_opts.copy()<EOL>o['<STR_LIT>']     = displaywidth<EOL>o['<STR_LIT>']           = colsep<EOL>o['<STR_LIT>'] = arrange_vertical<EOL>o['<STR_LIT>']            = ljust<EOL>o['<STR_LIT>']       = lineprefix<EOL>pass<EOL><DEDENT>if o['<STR_LIT>']:<EOL><INDENT>array = [(o['<STR_LIT>'] % i) for i in array]<EOL><DEDENT>else:<EOL><INDENT>array = [str(i) for i in array]<EOL>pass<EOL><DEDENT>size = len(array)<EOL>if <NUM_LIT:0> == size:<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT>elif size == <NUM_LIT:1>:<EOL><INDENT>return '<STR_LIT>' % (o['<STR_LIT>'], str(array[<NUM_LIT:0>]),<EOL>o['<STR_LIT>'])<EOL><DEDENT>o['<STR_LIT>'] = max(<NUM_LIT:4>, o['<STR_LIT>'] - len(o['<STR_LIT>']))<EOL>if o['<STR_LIT>']:<EOL><INDENT>array_index = lambda nrows, row, col: nrows*col + row<EOL>for nrows in range(<NUM_LIT:1>, size+<NUM_LIT:1>):<EOL><INDENT>ncols = (size+nrows-<NUM_LIT:1>) // nrows<EOL>colwidths = []<EOL>totwidth = -len(o['<STR_LIT>'])<EOL>for col in range(ncols):<EOL><INDENT>colwidth = <NUM_LIT:0><EOL>for row in range(nrows):<EOL><INDENT>i = array_index(nrows, row, col)<EOL>if i >= size: break<EOL>x = array[i]<EOL>colwidth = max(colwidth, len(x))<EOL>pass<EOL><DEDENT>colwidths.append(colwidth)<EOL>totwidth += colwidth + len(o['<STR_LIT>'])<EOL>if totwidth > o['<STR_LIT>']:<EOL><INDENT>break<EOL><DEDENT>pass<EOL><DEDENT>if totwidth <= o['<STR_LIT>']:<EOL><INDENT>break<EOL><DEDENT>pass<EOL><DEDENT>s = '<STR_LIT>'<EOL>for row in range(nrows):<EOL><INDENT>texts = []<EOL>for col in range(ncols):<EOL><INDENT>i = row + nrows*col<EOL>if i >= size:<EOL><INDENT>x = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>x = array[i]<EOL><DEDENT>texts.append(x)<EOL><DEDENT>while texts and not texts[-<NUM_LIT:1>]:<EOL><INDENT>del texts[-<NUM_LIT:1>]<EOL><DEDENT>for col in range(len(texts)):<EOL><INDENT>if o['<STR_LIT>']:<EOL><INDENT>texts[col] = texts[col].ljust(colwidths[col])<EOL><DEDENT>else:<EOL><INDENT>texts[col] = texts[col].rjust(colwidths[col])<EOL>pass<EOL><DEDENT>pass<EOL><DEDENT>s += "<STR_LIT>" % (o['<STR_LIT>'], str(o['<STR_LIT>'].join(texts)),<EOL>o['<STR_LIT>'])<EOL>pass<EOL><DEDENT>return s<EOL><DEDENT>else:<EOL><INDENT>array_index = lambda ncols, row, col: ncols*(row-<NUM_LIT:1>) + col<EOL>colwidths = []<EOL>for ncols in range(size, <NUM_LIT:0>, -<NUM_LIT:1>):<EOL><INDENT>min_rows = (size+ncols-<NUM_LIT:1>) // ncols<EOL>nrows = min_rows -<NUM_LIT:1><EOL>while nrows < size:<EOL><INDENT>nrows += <NUM_LIT:1><EOL>rounded_size = nrows * ncols<EOL>colwidths = []<EOL>totwidth  = -len(o['<STR_LIT>'])<EOL>for col in range(ncols):<EOL><INDENT>colwidth  = <NUM_LIT:0><EOL>for row in range(<NUM_LIT:1>, nrows+<NUM_LIT:1>):<EOL><INDENT>i = array_index(ncols, row, col)<EOL>if i >= rounded_size: break<EOL>elif i < size:<EOL><INDENT>x = array[i]<EOL>colwidth = max(colwidth, len(x))<EOL>pass<EOL><DEDENT>pass<EOL><DEDENT>colwidths.append(colwidth)<EOL>totwidth += colwidth + len(o['<STR_LIT>'])<EOL>if totwidth >= o['<STR_LIT>']:<EOL><INDENT>break<EOL><DEDENT>pass<EOL><DEDENT>if totwidth <= o['<STR_LIT>'] and i >= rounded_size-<NUM_LIT:1>:<EOL><INDENT>nrows  = row<EOL>break<EOL><DEDENT>elif totwidth >= o['<STR_LIT>']:<EOL><INDENT>break<EOL><DEDENT>pass<EOL><DEDENT>if totwidth <= o['<STR_LIT>'] and i >= rounded_size-<NUM_LIT:1>:<EOL><INDENT>break<EOL><DEDENT>pass<EOL><DEDENT>s = '<STR_LIT>'<EOL>if len(o['<STR_LIT>']) != <NUM_LIT:0>:<EOL><INDENT>prefix = o['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>prefix = o['<STR_LIT>']<EOL>pass<EOL><DEDENT>for row in range(<NUM_LIT:1>, nrows+<NUM_LIT:1>):<EOL><INDENT>texts = []<EOL>for col in range(ncols):<EOL><INDENT>i = array_index(ncols, row, col)<EOL>if i >= size:<EOL><INDENT>break<EOL><DEDENT>else: x = array[i]<EOL>texts.append(x)<EOL>pass<EOL><DEDENT>for col in range(len(texts)):<EOL><INDENT>if o['<STR_LIT>']:<EOL><INDENT>texts[col] = texts[col].ljust(colwidths[col])<EOL><DEDENT>else:<EOL><INDENT>texts[col] = texts[col].rjust(colwidths[col])<EOL>pass<EOL><DEDENT>pass<EOL><DEDENT>s += "<STR_LIT>" % (prefix, str(o['<STR_LIT>'].join(texts)),<EOL>o['<STR_LIT>'])<EOL>prefix = o['<STR_LIT>']<EOL>pass<EOL><DEDENT>if o['<STR_LIT>']:<EOL><INDENT>colsep = o['<STR_LIT>'].rstrip()<EOL>colsep_pos = -(len(colsep)+<NUM_LIT:1>)<EOL>if s[colsep_pos:] == colsep + "<STR_LIT:\n>":<EOL><INDENT>s = s[:colsep_pos] + o['<STR_LIT>'] + "<STR_LIT:\n>"<EOL>pass<EOL><DEDENT>pass<EOL><DEDENT>else:<EOL><INDENT>s += o['<STR_LIT>']<EOL>pass<EOL><DEDENT>return s<EOL><DEDENT>pass<EOL>

Return a list of strings as a compact set of columns arranged
    horizontally or vertically.

    For example, for a line width of 4 characters (arranged vertically):
        ['1', '2,', '3', '4'] => '1  3\n2  4\n'

    or arranged horizontally:
        ['1', '2,', '3', '4'] => '1  2\n3  4\n'

    Each column is only as wide as necessary.  By default, columns are
    separated by two spaces - one was not legible enough. Set "colsep"
    to adjust the string separate columns. Set `displaywidth' to set
    the line width.

    Normally, consecutive items go down from the top to bottom from
    the left-most column to the right-most. If "arrange_vertical" is
    set false, consecutive items will go across, left to right, top to
    bottom.


if not saver_lib.checkpoint_exists(input_checkpoint):<EOL><INDENT>print("<STR_LIT>".format(input_checkpoint))<EOL>exit(-<NUM_LIT:1>)<EOL><DEDENT>

Check if input_checkpoint is a valid path or path prefix.


saver = tf.train.import_meta_graph('<STR_LIT>'.format(input_checkpoint))<EOL>saver.restore(sess, input_checkpoint)<EOL>return saver<EOL>

Return a TensorFlow saver from a checkpoint containing the metagraph.


if isinstance(output_node_names, string_types):<EOL><INDENT>return [output_node_names]<EOL><DEDENT>else:<EOL><INDENT>return output_node_names<EOL><DEDENT>

Return a list of containing output_node_names if it's a string, otherwise return just output_node_names.


def caffe_to_tensorflow_session(caffe_def_path, caffemodel_path, inputs, graph_name='<STR_LIT>',<EOL>conversion_out_dir_path=None, use_padding_same=False):

try:<EOL><INDENT>from caffeflow import convert<EOL><DEDENT>except ImportError:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>with (dummy_context_mgr(conversion_out_dir_path) or util.TemporaryDirectory()) as dir_path:<EOL><INDENT>params_values_output_path = os.path.join(dir_path, '<STR_LIT>')<EOL>network_output_path = os.path.join(dir_path, '<STR_LIT>')<EOL>convert.convert(caffe_def_path, caffemodel_path, params_values_output_path, network_output_path, False,<EOL>use_padding_same=use_padding_same)<EOL>network_module = imp.load_source('<STR_LIT>', network_output_path)<EOL>network_class = getattr(network_module, graph_name)<EOL>network = network_class(inputs)<EOL>sess = tf.Session()<EOL>network.load(params_values_output_path, sess)<EOL>return sess<EOL><DEDENT>

Create a TensorFlow Session from a Caffe model.


def freeze(caffe_def_path, caffemodel_path, inputs, output_file_path, output_node_names, graph_name='<STR_LIT>',<EOL>conversion_out_dir_path=None, checkpoint_out_path=None, use_padding_same=False):

with caffe_to_tensorflow_session(caffe_def_path, caffemodel_path, inputs, graph_name=graph_name,<EOL>conversion_out_dir_path=conversion_out_dir_path,<EOL>use_padding_same=use_padding_same) as sess:<EOL><INDENT>saver = tf.train.Saver()<EOL>with (dummy_context_mgr(checkpoint_out_path) or util.TemporaryDirectory()) as temp_dir_path:<EOL><INDENT>checkpoint_path = checkpoint_out_path or os.path.join(temp_dir_path, '<STR_LIT>')<EOL>saver.save(sess, checkpoint_path)<EOL>output_node_names = util.output_node_names_string_as_list(output_node_names)<EOL>tf_freeze.freeze_from_checkpoint(checkpoint_path, output_file_path, output_node_names)<EOL><DEDENT><DEDENT>

Freeze and shrink the graph based on a Caffe model, the input tensors and the output node names.


def save_graph_only(caffe_def_path, caffemodel_path, inputs, output_file_path, output_node_names, graph_name='<STR_LIT>',<EOL>use_padding_same=False):

with caffe_to_tensorflow_session(caffe_def_path, caffemodel_path, inputs, graph_name=graph_name,<EOL>use_padding_same=use_padding_same) as sess:<EOL><INDENT>tf_freeze.save_graph_only(sess, output_file_path, output_node_names)<EOL><DEDENT>

Save a small version of the graph based on a Caffe model, the input tensors and the output node names.


def save_weights(caffe_def_path, caffemodel_path, inputs, output_path, graph_name='<STR_LIT>', conv_var_names=None,<EOL>conv_transpose_var_names=None, use_padding_same=False):

with caffe_to_tensorflow_session(caffe_def_path, caffemodel_path, inputs, graph_name=graph_name,<EOL>use_padding_same=use_padding_same) as sess:<EOL><INDENT>tf_freeze.save_weights(sess, output_path, conv_var_names=conv_var_names,<EOL>conv_transpose_var_names=conv_transpose_var_names)<EOL><DEDENT>

Save the weights of the trainable variables, each one in a different file in output_path.


def freeze_from_checkpoint(input_checkpoint, output_file_path, output_node_names):

Freeze and shrink the graph based on a checkpoint and the output node names.


def freeze(sess, output_file_path, output_node_names):

with TemporaryDirectory() as temp_dir_name:<EOL><INDENT>checkpoint_path = os.path.join(temp_dir_name, '<STR_LIT>')<EOL>tf.train.Saver().save(sess, checkpoint_path)<EOL>freeze_from_checkpoint(checkpoint_path, output_file_path, output_node_names)<EOL><DEDENT>

Freeze and shrink the graph based on a session and the output node names.


def save_graph_only(sess, output_file_path, output_node_names, as_text=False):

for node in sess.graph_def.node:<EOL><INDENT>node.device = '<STR_LIT>'<EOL><DEDENT>graph_def = graph_util.extract_sub_graph(sess.graph_def, output_node_names)<EOL>output_dir, output_filename = os.path.split(output_file_path)<EOL>graph_io.write_graph(graph_def, output_dir, output_filename, as_text=as_text)<EOL>

Save a small version of the graph based on a session and the output node names.


def save_graph_only_from_checkpoint(input_checkpoint, output_file_path, output_node_names, as_text=False):

check_input_checkpoint(input_checkpoint)<EOL>output_node_names = output_node_names_string_as_list(output_node_names)<EOL>with tf.Session() as sess:<EOL><INDENT>restore_from_checkpoint(sess, input_checkpoint)<EOL>save_graph_only(sess, output_file_path, output_node_names, as_text=as_text)<EOL><DEDENT>

Save a small version of the graph based on a checkpoint and the output node names.


def save_weights(sess, output_path, conv_var_names=None, conv_transpose_var_names=None):

if not conv_var_names:<EOL><INDENT>conv_var_names = []<EOL><DEDENT>if not conv_transpose_var_names:<EOL><INDENT>conv_transpose_var_names = []<EOL><DEDENT>for var in tf.trainable_variables():<EOL><INDENT>filename = '<STR_LIT>'.format(output_path, var.name.replace('<STR_LIT::>', '<STR_LIT:->').replace('<STR_LIT:/>', '<STR_LIT:->'))<EOL>if var.name in conv_var_names:<EOL><INDENT>var = tf.transpose(var, perm=[<NUM_LIT:3>, <NUM_LIT:0>, <NUM_LIT:1>, <NUM_LIT:2>])<EOL><DEDENT>elif var.name in conv_transpose_var_names:<EOL><INDENT>var = tf.transpose(var, perm=[<NUM_LIT:3>, <NUM_LIT:1>, <NUM_LIT:0>, <NUM_LIT:2>])<EOL><DEDENT>value = sess.run(var)<EOL>with open(filename, '<STR_LIT:w>') as file_:<EOL><INDENT>value.tofile(file_)<EOL><DEDENT><DEDENT>


def save_weights_from_checkpoint(input_checkpoint, output_path, conv_var_names=None, conv_transpose_var_names=None):

check_input_checkpoint(input_checkpoint)<EOL>with tf.Session() as sess:<EOL><INDENT>restore_from_checkpoint(sess, input_checkpoint)<EOL>save_weights(sess, output_path, conv_var_names=conv_var_names,<EOL>conv_transpose_var_names=conv_transpose_var_names)<EOL><DEDENT>

Save the weights of the trainable variables given a checkpoint, each one in a different file in output_path.


def __init__(self, tie_break=<NUM_LIT:1>, default_label=<NUM_LIT:0>):

self.tie_break = tie_break<EOL>self.default_label = default_label<EOL>self.overall_mean_trait_value = <NUM_LIT:0.><EOL>self.feature_map = None<EOL>

Sets up the Continuous MDR algorithm for feature construction.

        Parameters
        ----------
        tie_break: int (default: 1)
            Default label in case there's a tie in a set of feature pair values
        default_label: int (default: 0)
            Default label in case there's no data for a set of feature pair values

        Returns
        -------
        None


self.feature_map = defaultdict(lambda: self.default_label)<EOL>self.overall_mean_trait_value = np.mean(targets)<EOL>self.mdr_matrix_values = defaultdict(list)<EOL>for row_i in range(features.shape[<NUM_LIT:0>]):<EOL><INDENT>feature_instance = tuple(features[row_i])<EOL>self.mdr_matrix_values[feature_instance].append(targets[row_i])<EOL><DEDENT>for feature_instance in self.mdr_matrix_values:<EOL><INDENT>grid_mean_trait_value = np.mean(self.mdr_matrix_values[feature_instance])<EOL>if grid_mean_trait_value > self.overall_mean_trait_value: <EOL><INDENT>self.feature_map[feature_instance] = <NUM_LIT:1><EOL><DEDENT>elif grid_mean_trait_value == self.overall_mean_trait_value:<EOL><INDENT>self.feature_map[feature_instance] = self.tie_break<EOL><DEDENT>else:<EOL><INDENT>self.feature_map[feature_instance] = <NUM_LIT:0><EOL><DEDENT><DEDENT>self.feature_map = dict(self.feature_map)<EOL>self.mdr_matrix_values = dict(self.mdr_matrix_values)<EOL>return self<EOL>

Constructs the Continuous MDR feature map from the provided training data.

        Parameters
        ----------
        features: array-like {n_samples, n_features}
            Feature matrix
        targets: array-like {n_samples}
            List of target values for prediction

        Returns
        -------
        self: A copy of the fitted model


new_feature = np.zeros(features.shape[<NUM_LIT:0>], dtype=np.int)<EOL>for row_i in range(features.shape[<NUM_LIT:0>]):<EOL><INDENT>feature_instance = tuple(features[row_i])<EOL>if feature_instance in self.feature_map:<EOL><INDENT>new_feature[row_i] = self.feature_map[feature_instance]<EOL><DEDENT>else:<EOL><INDENT>new_feature[row_i] = self.default_label<EOL><DEDENT><DEDENT>return new_feature.reshape(features.shape[<NUM_LIT:0>], <NUM_LIT:1>)<EOL>

Uses the Continuous MDR feature map to construct a new feature from the provided features.

        Parameters
        ----------
        features: array-like {n_samples, n_features}
            Feature matrix to transform

        Returns
        ----------
        array-like: {n_samples}
            Constructed feature from the provided feature matrix
            The constructed feature will be a binary variable, taking the values 0 and 1


def fit_transform(self, features, targets):

self.fit(features, targets)<EOL>return self.transform(features)<EOL>

Convenience function that fits the provided data then constructs a new feature from the provided features.

        Parameters
        ----------
        features: array-like {n_samples, n_features}
            Feature matrix
        targets: array-like {n_samples}
            List of true target values

        Returns
        ----------
        array-like: {n_samples}
            Constructed features from the provided feature matrix


if self.feature_map is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>group_0_trait_values = []<EOL>group_1_trait_values = []<EOL>for feature_instance in self.feature_map:<EOL><INDENT>if self.feature_map[feature_instance] == <NUM_LIT:0>:<EOL><INDENT>group_0_trait_values.extend(self.mdr_matrix_values[feature_instance])<EOL><DEDENT>else:<EOL><INDENT>group_1_trait_values.extend(self.mdr_matrix_values[feature_instance])<EOL><DEDENT><DEDENT>return abs(ttest_ind(group_0_trait_values, group_1_trait_values).statistic)<EOL>

Estimates the quality of the ContinuousMDR model using a t-statistic.

        Parameters
        ----------
        features: array-like {n_samples, n_features}
            Feature matrix to predict from
        targets: array-like {n_samples}
            List of true target values

        Returns
        -------
        quality_score: float
            The estimated quality of the Continuous MDR model


return scipy.stats.entropy(list(Counter(X).values()), base=base)<EOL>

Calculates the entropy, H(X), in the given base

    Parameters
    ----------
    X: array-like (# samples)
        An array of values for which to compute the entropy
    base: integer (default: 2)
        The base in which to calculate entropy

    Returns
    ----------
    entropy: float
        The entropy calculated according to the equation H(X) = -sum(p_x * log p_x) for all states of X


def joint_entropy(X, Y, base=<NUM_LIT:2>):

X_Y = ['<STR_LIT>'.format(x, y) for x, y in zip(X, Y)]<EOL>return entropy(X_Y, base=base)<EOL>

Calculates the joint entropy, H(X,Y), in the given base

    Parameters
    ----------
    X: array-like (# samples)
        An array of values for which to compute the joint entropy
    Y: array-like (# samples)
        An array of values for which to compute the joint entropy
    base: integer (default: 2)
        The base in which to calculate joint entropy

    Returns
    ----------
    joint_entropy: float
        The joint entropy calculated according to the equation H(X,Y) = -sum(p_xy * log p_xy) for all combined states of X and Y


def conditional_entropy(X, Y, base=<NUM_LIT:2>):

return joint_entropy(X, Y, base=base) - entropy(Y, base=base)<EOL>

Calculates the conditional entropy, H(X|Y), in the given base

    Parameters
    ----------
    X: array-like (# samples)
        An array of values for which to compute the conditional entropy
    Y: array-like (# samples)
        An array of values for which to compute the conditional entropy
    base: integer (default: 2)
        The base in which to calculate conditional entropy

    Returns
    ----------
    conditional_entropy: float
        The conditional entropy calculated according to the equation H(X|Y) = H(X,Y) - H(Y)


def mutual_information(X, Y, base=<NUM_LIT:2>):

return entropy(Y, base=base) - conditional_entropy(Y, X, base=base)<EOL>

Calculates the mutual information between two variables, I(X;Y), in the given base

    Parameters
    ----------
    X: array-like (# samples)
        An array of values for which to compute the mutual information
    Y: array-like (# samples)
        An array of values for which to compute the mutual information
    base: integer (default: 2)
        The base in which to calculate mutual information

    Returns
    ----------
    mutual_information: float
        The mutual information calculated according to the equation I(X;Y) = H(Y) - H(Y|X)


def two_way_information_gain(X, Y, Z, base=<NUM_LIT:2>):

X_Y = ['<STR_LIT>'.format(x, y) for x, y in zip(X, Y)]<EOL>return (mutual_information(X_Y, Z, base=base) -<EOL>mutual_information(X, Z, base=base) -<EOL>mutual_information(Y, Z, base=base))<EOL>

Calculates the two-way information gain between three variables, I(X;Y;Z), in the given base

    IG(X;Y;Z) indicates the information gained about variable Z by the joint variable X_Y, after removing
    the information that X and Y have about Z individually. Thus, two-way information gain measures the
    synergistic predictive value of variables X and Y about variable Z.

    Parameters
    ----------
    X: array-like (# samples)
        An array of values for which to compute the 2-way information gain
    Y: array-like (# samples)
        An array of values for which to compute the 2-way information gain
    Z: array-like (# samples)
        An array of outcome values for which to compute the 2-way information gain
    base: integer (default: 2)
        The base in which to calculate 2-way information

    Returns
    ----------
    mutual_information: float
        The information gain calculated according to the equation IG(X;Y;Z) = I(X,Y;Z) - I(X;Z) - I(Y;Z)


def three_way_information_gain(W, X, Y, Z, base=<NUM_LIT:2>):

W_X_Y = ['<STR_LIT>'.format(w, x, y) for w, x, y in zip(W, X, Y)]<EOL>return (mutual_information(W_X_Y, Z, base=base) -<EOL>two_way_information_gain(W, X, Z, base=base) -<EOL>two_way_information_gain(W, Y, Z, base=base) -<EOL>two_way_information_gain(X, Y, Z, base=base) -<EOL>mutual_information(W, Z, base=base) -<EOL>mutual_information(X, Z, base=base) -<EOL>mutual_information(Y, Z, base=base))<EOL>

Calculates the three-way information gain between three variables, I(W;X;Y;Z), in the given base

    IG(W;X;Y;Z) indicates the information gained about variable Z by the joint variable W_X_Y, after removing
    the information that W, X, and Y have about Z individually and jointly in pairs. Thus, 3-way information gain
    measures the synergistic predictive value of variables W, X, and Y about variable Z.

    Parameters
    ----------
    W: array-like (# samples)
        An array of values for which to compute the 3-way information gain
    X: array-like (# samples)
        An array of values for which to compute the 3-way information gain
    Y: array-like (# samples)
        An array of values for which to compute the 3-way information gain
    Z: array-like (# samples)
        An array of outcome values for which to compute the 3-way information gain
    base: integer (default: 2)
        The base in which to calculate 3-way information

    Returns
    ----------
    mutual_information: float
        The information gain calculated according to the equation:
            IG(W;X;Y;Z) = I(W,X,Y;Z) - IG(W;X;Z) - IG(W;Y;Z) - IG(X;Y;Z) - I(W;Z) - I(X;Z) - I(Y;Z)


return MDR().fit_predict(np.column_stack((X, Y)), labels)<EOL>

Fits a MDR model to variables X and Y with the given labels, then returns the resulting predictions

    This is a convenience method that should only be used internally.

    Parameters
    ----------
    X: array-like (# samples)
        An array of values corresponding to one feature in the MDR model
    Y: array-like (# samples)
        An array of values corresponding to one feature in the MDR model
    labels: array-like (# samples)
        The class labels corresponding to features X and Y

    Returns
    ----------
    predictions: array-like (# samples)
        The predictions from the fitted MDR model


def mdr_entropy(X, Y, labels, base=<NUM_LIT:2>):

return entropy(_mdr_predict(X, Y, labels), base=base)<EOL>

Calculates the MDR entropy, H(XY), in the given base

    MDR entropy is calculated by combining variables X and Y into a single MDR model then calculating
    the entropy of the resulting model's predictions.

    Parameters
    ----------
    X: array-like (# samples)
        An array of values corresponding to one feature in the MDR model
    Y: array-like (# samples)
        An array of values corresponding to one feature in the MDR model
    labels: array-like (# samples)
        The class labels corresponding to features X and Y
    base: integer (default: 2)
        The base in which to calculate MDR entropy

    Returns
    ----------
    mdr_entropy: float
        The MDR entropy calculated according to the equation H(XY) = -sum(p_xy * log p_xy) for all output states of the MDR model


def mdr_conditional_entropy(X, Y, labels, base=<NUM_LIT:2>):

return conditional_entropy(_mdr_predict(X, Y, labels), labels, base=base)<EOL>

Calculates the MDR conditional entropy, H(XY|labels), in the given base

    MDR conditional entropy is calculated by combining variables X and Y into a single MDR model then calculating
    the entropy of the resulting model's predictions conditional on the provided labels.

    Parameters
    ----------
    X: array-like (# samples)
        An array of values corresponding to one feature in the MDR model
    Y: array-like (# samples)
        An array of values corresponding to one feature in the MDR model
    labels: array-like (# samples)
        The class labels corresponding to features X and Y
    base: integer (default: 2)
        The base in which to calculate MDR conditional entropy

    Returns
    ----------
    mdr_conditional_entropy: float
        The MDR conditional entropy calculated according to the equation H(XY|labels) = H(XY,labels) - H(labels)


def mdr_mutual_information(X, Y, labels, base=<NUM_LIT:2>):

return mutual_information(_mdr_predict(X, Y, labels), labels, base=base)<EOL>

Calculates the MDR mutual information, I(XY;labels), in the given base

    MDR mutual information is calculated by combining variables X and Y into a single MDR model then calculating
    the mutual information between the resulting model's predictions and the labels.

    Parameters
    ----------
    X: array-like (# samples)
        An array of values corresponding to one feature in the MDR model
    Y: array-like (# samples)
        An array of values corresponding to one feature in the MDR model
    labels: array-like (# samples)
        The class labels corresponding to features X and Y
    base: integer (default: 2)
        The base in which to calculate MDR mutual information

    Returns
    ----------
    mdr_mutual_information: float
        The MDR mutual information calculated according to the equation I(XY;labels) = H(labels) - H(labels|XY)


def n_way_models(mdr_instance, X, y, n=[<NUM_LIT:2>], feature_names=None):

if feature_names is None:<EOL><INDENT>feature_names = list(range(X.shape[<NUM_LIT:1>]))<EOL><DEDENT>for cur_n in n:<EOL><INDENT>for features in itertools.combinations(range(X.shape[<NUM_LIT:1>]), cur_n):<EOL><INDENT>mdr_model = copy.deepcopy(mdr_instance)<EOL>mdr_model.fit(X[:, features], y)<EOL>mdr_model_score = mdr_model.score(X[:, features], y)<EOL>model_features = [feature_names[feature] for feature in features]<EOL>yield mdr_model, mdr_model_score, model_features<EOL><DEDENT><DEDENT>

Fits a MDR model to all n-way combinations of the features in X.

    Note that this function performs an exhaustive search through all feature combinations and can be computationally expensive.

    Parameters
    ----------
    mdr_instance: object
        An instance of the MDR type to use.
    X: array-like (# rows, # features)
        NumPy matrix containing the features
    y: array-like (# rows, 1)
        NumPy matrix containing the target values
    n: list (default: [2])
        The maximum size(s) of the MDR model to generate.
        e.g., if n == [3], all 3-way models will be generated.
    feature_names: list (default: None)
        The corresponding names of the features in X.
        If None, then the features will be named according to their order.

    Returns
    ----------
    (fitted_model, fitted_model_score, fitted_model_features): tuple of (list, list, list)
        fitted_model contains the MDR model fitted to the data.
        fitted_model_score contains the training scores corresponding to the fitted MDR model.
        fitted_model_features contains a list of the names of the features that were used in the corresponding model.


var1_levels = list(set([variables[<NUM_LIT:0>] for variables in mdr_instance.feature_map]))<EOL>var2_levels = list(set([variables[<NUM_LIT:1>] for variables in mdr_instance.feature_map]))<EOL>max_count = np.array(list(mdr_instance.class_count_matrix.values())).flatten().max()<EOL>"""<STR_LIT>"""<EOL>fig, splots = plt.subplots(ncols=len(var1_levels), nrows=len(var2_levels), sharey=True, sharex=True)<EOL>fig.set_figwidth(<NUM_LIT:6>)<EOL>fig.set_figheight(<NUM_LIT:6>)<EOL>for (var1, var2) in itertools.product(var1_levels, var2_levels):<EOL><INDENT>class_counts = mdr_instance.class_count_matrix[(var1, var2)]<EOL>splot = splots[var2_levels.index(var2)][var1_levels.index(var1)]<EOL>splot.set_yticks([])<EOL>splot.set_xticks([])<EOL>splot.set_ylim(<NUM_LIT:0>, max_count * <NUM_LIT>)<EOL>splot.set_xlim(-<NUM_LIT:0.5>, <NUM_LIT>)<EOL>if var2_levels.index(var2) == <NUM_LIT:0>:<EOL><INDENT>splot.set_title('<STR_LIT>'.format(var1), fontsize=<NUM_LIT:12>)<EOL><DEDENT>if var1_levels.index(var1) == <NUM_LIT:0>:<EOL><INDENT>splot.set_ylabel('<STR_LIT>'.format(var2), fontsize=<NUM_LIT:12>)<EOL><DEDENT>bars = splot.bar(left=range(class_counts.shape[<NUM_LIT:0>]),<EOL>height=class_counts, width=<NUM_LIT:0.5>,<EOL>color='<STR_LIT>', align='<STR_LIT>')<EOL>bgcolor = '<STR_LIT>' if mdr_instance.feature_map[(var1, var2)] == <NUM_LIT:0> else '<STR_LIT>'<EOL>splot.set_axis_bgcolor(bgcolor)<EOL>for index, bar in enumerate(bars):<EOL><INDENT>splot.text(index, class_counts[index] + (max_count * <NUM_LIT:0.1>), class_counts[index], ha='<STR_LIT>')<EOL><DEDENT><DEDENT>fig.tight_layout()<EOL>return fig<EOL>

Visualizes the MDR grid of a given fitted MDR instance. Only works for 2-way MDR models.

    This function is currently incomplete.

    Parameters
    ----------
    mdr_instance: object
        A fitted instance of the MDR type to visualize.

    Returns
    ----------
    fig: matplotlib.figure
        Figure object for the visualized MDR grid.


def __init__(self, n_estimators=<NUM_LIT:100>, tie_break=<NUM_LIT:1>, default_label=<NUM_LIT:0>, random_state=None):

self.n_estimators = n_estimators<EOL>self.tie_break = tie_break<EOL>self.default_label = default_label<EOL>self.random_state = random_state<EOL>self.feature_map = defaultdict(lambda: default_label)<EOL>self.ensemble = BaggingClassifier(base_estimator=MDR(tie_break=tie_break, default_label=default_label),<EOL>n_estimators=n_estimators, random_state=random_state)<EOL>

Sets up the MDR ensemble

        Parameters
        ----------
        n_estimators: int (default: 100)
            Number of MDR models to include in the ensemble
        tie_break: int (default: 1)
            Default label in case there's a tie in a set of feature pair values
        default_label: int (default: 0)
            Default label in case there's no data for a set of feature pair values
        random_state: int, RandomState instance or None (default: None)
            If int, random_state is the seed used by the random number generator;
            If RandomState instance, random_state is the random number generator;
            If None, the random number generator is the RandomState instance used by np.random.

        Returns
        -------
        None


self.ensemble.fit(features, classes)<EOL>unique_rows = list(set([tuple(row) for row in features]))<EOL>for row in unique_rows:<EOL><INDENT>self.feature_map[row] = self.ensemble.predict([row])[<NUM_LIT:0>]<EOL><DEDENT>

Constructs the MDR ensemble from the provided training data

        Parameters
        ----------
        features: array-like {n_samples, n_features}
            Feature matrix
        classes: array-like {n_samples}
            List of class labels for prediction

        Returns
        -------
        None


return self.ensemble.predict(features)<EOL>

Uses the MDR ensemble to construct a new feature from the provided features

        Parameters
        ----------
        features: array-like {n_samples, n_features}
            Feature matrix to transform

        Returns
        ----------
        array-like: {n_samples}
            Constructed features from the provided feature matrix


def fit_predict(self, features, classes):

self.ensemble.fit(features, classes)<EOL>return self.ensemble.predict(features)<EOL>

Convenience function that fits the provided data then constructs a new feature from the provided features

        Parameters
        ----------
        features: array-like {n_samples, n_features}
            Feature matrix
        classes: array-like {n_samples}
            List of true class labels

        Returns
        ----------
        array-like: {n_samples}
            Constructed features from the provided feature matrix


def score(self, features, classes, scoring_function=None, **scoring_function_kwargs):

new_feature = self.ensemble.predict(features)<EOL>if scoring_function is None:<EOL><INDENT>return accuracy_score(classes, new_feature)<EOL><DEDENT>else:<EOL><INDENT>return scoring_function(classes, new_feature, **scoring_function_kwargs)<EOL><DEDENT>

Estimates the accuracy of the predictions from the MDR ensemble

        Parameters
        ----------
        features: array-like {n_samples, n_features}
            Feature matrix to predict from
        classes: array-like {n_samples}
            List of true class labels

        Returns
        -------
        accuracy_score: float
            The estimated accuracy based on the constructed feature


self.tie_break = tie_break<EOL>self.default_label = default_label<EOL>self.class_count_matrix = None<EOL>self.feature_map = None<EOL>

Sets up the MDR algorithm for feature construction.

        Parameters
        ----------
        tie_break: int (default: 1)
            Default label in case there's a tie in a set of feature pair values
        default_label: int (default: 0)
            Default label in case there's no data for a set of feature pair values

        Returns
        -------
        None


unique_labels = sorted(np.unique(class_labels))<EOL>if len(unique_labels) != <NUM_LIT:2>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>self.class_count_matrix = defaultdict(lambda: defaultdict(int))<EOL>for row_i in range(features.shape[<NUM_LIT:0>]):<EOL><INDENT>feature_instance = tuple(features[row_i])<EOL>self.class_count_matrix[feature_instance][class_labels[row_i]] += <NUM_LIT:1><EOL><DEDENT>self.class_count_matrix = dict(self.class_count_matrix)<EOL>overall_class_fraction = float(sum(class_labels == unique_labels[<NUM_LIT:0>])) / class_labels.size<EOL>self.feature_map = {}<EOL>for feature_instance in self.class_count_matrix:<EOL><INDENT>counts = self.class_count_matrix[feature_instance]<EOL>fraction = float(counts[unique_labels[<NUM_LIT:0>]]) / np.sum(list(counts.values()))<EOL>if fraction > overall_class_fraction:<EOL><INDENT>self.feature_map[feature_instance] = unique_labels[<NUM_LIT:0>]<EOL><DEDENT>elif fraction == overall_class_fraction:<EOL><INDENT>self.feature_map[feature_instance] = self.tie_break<EOL><DEDENT>else:<EOL><INDENT>self.feature_map[feature_instance] = unique_labels[<NUM_LIT:1>]<EOL><DEDENT><DEDENT>return self<EOL>

Constructs the MDR feature map from the provided training data.

        Parameters
        ----------
        features: array-like {n_samples, n_features}
            Feature matrix
        class_labels: array-like {n_samples}
            List of true class labels

        Returns
        -------
        self: A copy of the fitted model


if self.feature_map is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>new_feature = np.zeros(features.shape[<NUM_LIT:0>], dtype=np.int)<EOL>for row_i in range(features.shape[<NUM_LIT:0>]):<EOL><INDENT>feature_instance = tuple(features[row_i])<EOL>if feature_instance in self.feature_map:<EOL><INDENT>new_feature[row_i] = self.feature_map[feature_instance]<EOL><DEDENT>else:<EOL><INDENT>new_feature[row_i] = self.default_label<EOL><DEDENT><DEDENT>return new_feature.reshape(features.shape[<NUM_LIT:0>], <NUM_LIT:1>)<EOL>

Uses the MDR feature map to construct a new feature from the provided features.

        Parameters
        ----------
        features: array-like {n_samples, n_features}
            Feature matrix to transform

        Returns
        ----------
        array-like: {n_samples, 1}
            Constructed features from the provided feature matrix


def fit_transform(self, features, class_labels):

self.fit(features, class_labels)<EOL>return self.transform(features)<EOL>

Convenience function that fits the provided data then constructs a new feature from the provided features.

        Parameters
        ----------
        features: array-like {n_samples, n_features}
            Feature matrix
        class_labels: array-like {n_samples}
            List of true class labels

        Returns
        ----------
        array-like: {n_samples, 1}
            Constructed features from the provided feature matrix


if self.feature_map is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>new_feature = np.zeros(features.shape[<NUM_LIT:0>], dtype=np.int)<EOL>for row_i in range(features.shape[<NUM_LIT:0>]):<EOL><INDENT>feature_instance = tuple(features[row_i])<EOL>if feature_instance in self.feature_map:<EOL><INDENT>new_feature[row_i] = self.feature_map[feature_instance]<EOL><DEDENT>else:<EOL><INDENT>new_feature[row_i] = self.default_label<EOL><DEDENT><DEDENT>return new_feature<EOL>

Uses the MDR feature map to construct predictions from the provided features.

        Parameters
        ----------
        features: array-like {n_samples, n_features}
            Feature matrix to transform

        Returns
        ----------
        array-like: {n_samples}
            Constructed features from the provided feature matrix


def fit_predict(self, features, class_labels):

self.fit(features, class_labels)<EOL>return self.predict(features)<EOL>

Convenience function that fits the provided data then constructs predictions from the provided features.

        Parameters
        ----------
        features: array-like {n_samples, n_features}
            Feature matrix
        class_labels: array-like {n_samples}
            List of true class labels

        Returns
        ----------
        array-like: {n_samples}
            Constructed features from the provided feature matrix


def score(self, features, class_labels, scoring_function=None, **scoring_function_kwargs):

if self.feature_map is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>new_feature = self.predict(features)<EOL>if scoring_function is None:<EOL><INDENT>return accuracy_score(class_labels, new_feature)<EOL><DEDENT>else:<EOL><INDENT>return scoring_function(class_labels, new_feature, **scoring_function_kwargs)<EOL><DEDENT>

Estimates the accuracy of the predictions from the constructed feature.

        Parameters
        ----------
        features: array-like {n_samples, n_features}
            Feature matrix to predict from
        class_labels: array-like {n_samples}
            List of true class labels

        Returns
        -------
        accuracy_score: float
            The estimated accuracy based on the constructed feature


return json.dumps(record, sort_keys=True,<EOL>indent=<NUM_LIT:2>, separators=('<STR_LIT:U+002C>', '<STR_LIT>'))<EOL>

Serialize object to JSON.

        :param pid: Persistent Identifier.
        :param record: The :class:`invenio_records.api.Record` instance.


def only_authenticated_users(record, *args, **kwargs):

def can(self):<EOL><INDENT>return current_user.is_authenticated<EOL><DEDENT>return type('<STR_LIT>', (), {'<STR_LIT>': can})()<EOL>


pass<EOL>


with app.app_context():<EOL><INDENT>rec_uuid = uuid.uuid4()<EOL>pid1 = PersistentIdentifier.create(<EOL>'<STR_LIT>', '<STR_LIT:1>', object_type='<STR_LIT>', object_uuid=rec_uuid,<EOL>status=PIDStatus.REGISTERED)<EOL>Record.create({'<STR_LIT:title>': '<STR_LIT>', '<STR_LIT>': <NUM_LIT:1>}, id_=rec_uuid)<EOL>rec_uuid = uuid.uuid4()<EOL>pid = PersistentIdentifier.create(<EOL>'<STR_LIT>', '<STR_LIT:2>', object_type='<STR_LIT>', object_uuid=rec_uuid,<EOL>status=PIDStatus.REGISTERED)<EOL>pid.delete()<EOL>Record.create({'<STR_LIT:title>': '<STR_LIT>'}, id_=rec_uuid)<EOL>PersistentIdentifier.create(<EOL>'<STR_LIT>', '<STR_LIT:3>', status=PIDStatus.DELETED)<EOL>PersistentIdentifier.create(<EOL>'<STR_LIT>', '<STR_LIT:4>', status=PIDStatus.REGISTERED)<EOL>pid = PersistentIdentifier.create(<EOL>'<STR_LIT>', '<STR_LIT:5>', status=PIDStatus.REGISTERED)<EOL>pid.redirect(pid1)<EOL>rec_uuid = uuid.uuid4()<EOL>Record.create({'<STR_LIT:title>': '<STR_LIT>', }, id_=rec_uuid)<EOL>doi = PersistentIdentifier.create(<EOL>'<STR_LIT>', '<STR_LIT>', status=PIDStatus.REGISTERED,<EOL>object_type='<STR_LIT>', object_uuid=rec_uuid)<EOL>pid = PersistentIdentifier.create(<EOL>'<STR_LIT>', '<STR_LIT>', status=PIDStatus.REGISTERED)<EOL>pid.redirect(doi)<EOL>PersistentIdentifier.create(<EOL>'<STR_LIT>', '<STR_LIT>', status=PIDStatus.RESERVED)<EOL>db.session.commit()<EOL><DEDENT>


def custom_view(pid, record, template=None, **kwargs):

return '<STR_LIT>'.format(pid.pid_value, kwargs.get('<STR_LIT:filename>'))<EOL>


def custom_get_and_post(pid, record, template=None):

return '<STR_LIT>'.format(pid.pid_value, request.method)<EOL>

Custom view function for testing GET/POST.


current_dir = os.getcwd()<EOL>project_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))<EOL>exampleappdir = os.path.join(project_dir, '<STR_LIT>')<EOL>os.chdir(exampleappdir)<EOL>cmd = '<STR_LIT>'.format(app_name)<EOL>exit_status = subprocess.call(cmd, shell=True)<EOL>assert exit_status == <NUM_LIT:0><EOL>cmd = '<STR_LIT>'.format(app_name)<EOL>webapp = subprocess.Popen(cmd, stdout=subprocess.PIPE,<EOL>preexec_fn=os.setsid, shell=True)<EOL>time.sleep(<NUM_LIT:10>)<EOL>yield webapp<EOL>os.killpg(webapp.pid, signal.SIGTERM)<EOL>cmd = '<STR_LIT>'.format(app_name)<EOL>subprocess.call(cmd, shell=True)<EOL>os.chdir(current_dir)<EOL>


def my_permission_factory(record, *args, **kwargs):

def can(self):<EOL><INDENT>rec = Record.get_record(record.id)<EOL>return rec.get('<STR_LIT>', '<STR_LIT>') == '<STR_LIT>'<EOL><DEDENT>return type('<STR_LIT>', (), {'<STR_LIT>': can})()<EOL>


import uuid<EOL>from invenio_records.api import Record<EOL>from invenio_pidstore.models import PersistentIdentifier, PIDStatus<EOL>with db.session.begin_nested():<EOL><INDENT>pid1 = PersistentIdentifier.create(<EOL>'<STR_LIT>', '<STR_LIT:1>', object_type='<STR_LIT>', object_uuid=rec1_uuid,<EOL>status=PIDStatus.REGISTERED)<EOL>Record.create({<EOL>'<STR_LIT:title>': '<STR_LIT>',<EOL>'<STR_LIT>': [<EOL>{'<STR_LIT:name>': '<STR_LIT>'},<EOL>{'<STR_LIT:name>': '<STR_LIT>'},<EOL>],<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': ['<STR_LIT>', '<STR_LIT>'],<EOL>}, id_=rec1_uuid)<EOL>PersistentIdentifier.create(<EOL>'<STR_LIT>', '<STR_LIT:2>', object_type='<STR_LIT>', object_uuid=rec2_uuid,<EOL>status=PIDStatus.REGISTERED)<EOL>Record.create({<EOL>'<STR_LIT:title>': '<STR_LIT>',<EOL>'<STR_LIT>': [<EOL>{'<STR_LIT:name>': '<STR_LIT>'},<EOL>{'<STR_LIT:name>': '<STR_LIT>'},<EOL>],<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': ['<STR_LIT>', '<STR_LIT>'],<EOL>}, id_=rec2_uuid)<EOL>rec3_uuid = uuid.uuid4()<EOL>pid = PersistentIdentifier.create(<EOL>'<STR_LIT>', '<STR_LIT:3>', object_type='<STR_LIT>', object_uuid=rec3_uuid,<EOL>status=PIDStatus.REGISTERED)<EOL>pid.delete()<EOL>Record.create({'<STR_LIT:title>': '<STR_LIT>'}, id_=rec3_uuid)<EOL>PersistentIdentifier.create(<EOL>'<STR_LIT>', '<STR_LIT:4>', status=PIDStatus.DELETED)<EOL>PersistentIdentifier.create(<EOL>'<STR_LIT>', '<STR_LIT:5>', status=PIDStatus.REGISTERED)<EOL>pid = PersistentIdentifier.create(<EOL>'<STR_LIT>', '<STR_LIT>', status=PIDStatus.REGISTERED)<EOL>pid.redirect(pid1)<EOL>doi = PersistentIdentifier.create(<EOL>'<STR_LIT>', '<STR_LIT>', status=PIDStatus.REGISTERED)<EOL>pid = PersistentIdentifier.create(<EOL>'<STR_LIT>', '<STR_LIT>', status=PIDStatus.REGISTERED)<EOL>pid.redirect(doi)<EOL>PersistentIdentifier.create(<EOL>'<STR_LIT>', '<STR_LIT>', status=PIDStatus.RESERVED)<EOL><DEDENT>db.session.commit()<EOL>


return create_blueprint(app.config.get('<STR_LIT>'))<EOL>

Create Invenio-Records-UI blueprint from a Flask application.

    .. note::

        This function assumes that the application has loaded all extensions
        that want to register REST endpoints via the ``RECORDS_UI_ENDPOINTS``
        configuration variable.

    :params app: A Flask application.
    :returns: Configured blueprint.


blueprint = Blueprint(<EOL>'<STR_LIT>',<EOL>__name__,<EOL>url_prefix='<STR_LIT>',<EOL>template_folder='<STR_LIT>',<EOL>static_folder='<STR_LIT>',<EOL>)<EOL>@blueprint.errorhandler(PIDDeletedError)<EOL>def tombstone_errorhandler(error):<EOL><INDENT>return render_template(<EOL>current_app.config['<STR_LIT>'],<EOL>pid=error.pid,<EOL>record=error.record or {},<EOL>), <NUM_LIT><EOL><DEDENT>@blueprint.context_processor<EOL>def inject_export_formats():<EOL><INDENT>return dict(<EOL>export_formats=(<EOL>current_app.extensions['<STR_LIT>'].export_formats)<EOL>)<EOL><DEDENT>for endpoint, options in (endpoints or {}).items():<EOL><INDENT>blueprint.add_url_rule(**create_url_rule(endpoint, **options))<EOL><DEDENT>return blueprint<EOL>

Create Invenio-Records-UI blueprint.

    The factory installs one URL route per endpoint defined, and adds an
    error handler for rendering tombstones.

    :param endpoints: Dictionary of endpoints to be installed. See usage
        documentation for further details.
    :returns: The initialized blueprint.


def create_url_rule(endpoint, route=None, pid_type=None, template=None,<EOL>permission_factory_imp=None, view_imp=None,<EOL>record_class=None, methods=None):

assert route<EOL>assert pid_type<EOL>permission_factory = import_string(permission_factory_imp) ifpermission_factory_imp else None<EOL>view_method = import_string(view_imp) if view_imp else default_view_method<EOL>record_class = import_string(record_class) if record_class else Record<EOL>methods = methods or ['<STR_LIT:GET>']<EOL>view_func = partial(<EOL>record_view,<EOL>resolver=Resolver(pid_type=pid_type, object_type='<STR_LIT>',<EOL>getter=record_class.get_record),<EOL>template=template or '<STR_LIT>',<EOL>permission_factory=permission_factory,<EOL>view_method=view_method)<EOL>view_func.__module__ = record_view.__module__<EOL>view_func.__name__ = record_view.__name__<EOL>return dict(<EOL>endpoint=endpoint,<EOL>rule=route,<EOL>view_func=view_func,<EOL>methods=methods,<EOL>)<EOL>

Create Werkzeug URL rule for a specific endpoint.

    The method takes care of creating a persistent identifier resolver
    for the given persistent identifier type.

    :param endpoint: Name of endpoint.
    :param route: URL route (must include ``<pid_value>`` pattern). Required.
    :param pid_type: Persistent identifier type for endpoint. Required.
    :param template: Template to render.
        (Default: ``invenio_records_ui/detail.html``)
    :param permission_factory_imp: Import path to factory that creates a
        permission object for a given record.
    :param view_imp: Import path to view function. (Default: ``None``)
    :param record_class: Name of the record API class.
    :param methods: Method allowed for the endpoint.
    :returns: A dictionary that can be passed as keywords arguments to
        ``Blueprint.add_url_rule``.


def record_view(pid_value=None, resolver=None, template=None,<EOL>permission_factory=None, view_method=None, **kwargs):

try:<EOL><INDENT>pid, record = resolver.resolve(pid_value)<EOL><DEDENT>except (PIDDoesNotExistError, PIDUnregistered):<EOL><INDENT>abort(<NUM_LIT>)<EOL><DEDENT>except PIDMissingObjectError as e:<EOL><INDENT>current_app.logger.exception(<EOL>"<STR_LIT>".format(e.pid),<EOL>extra={'<STR_LIT>': e.pid})<EOL>abort(<NUM_LIT>)<EOL><DEDENT>except PIDRedirectedError as e:<EOL><INDENT>try:<EOL><INDENT>return redirect(url_for(<EOL>'<STR_LIT>'.format(e.destination_pid.pid_type),<EOL>pid_value=e.destination_pid.pid_value))<EOL><DEDENT>except BuildError:<EOL><INDENT>current_app.logger.exception(<EOL>"<STR_LIT>".format(<EOL>e.destination_pid.pid_type),<EOL>extra={<EOL>'<STR_LIT>': e.pid,<EOL>'<STR_LIT>': e.destination_pid,<EOL>})<EOL>abort(<NUM_LIT>)<EOL><DEDENT><DEDENT>permission_factory = permission_factory or current_permission_factory<EOL>if permission_factory:<EOL><INDENT>if not permission_factory(record).can():<EOL><INDENT>from flask_login import current_user<EOL>if not current_user.is_authenticated:<EOL><INDENT>return redirect(url_for(<EOL>current_app.config['<STR_LIT>'],<EOL>next=request.url))<EOL><DEDENT>abort(<NUM_LIT>)<EOL><DEDENT><DEDENT>return view_method(pid, record, template=template, **kwargs)<EOL>

Display record view.

    The two parameters ``resolver`` and ``template`` should not be included
    in the URL rule, but instead set by creating a partially evaluated function
    of the view.

    The template being rendered is passed two variables in the template
    context:

    - ``pid``
    - ``record``.

    Procedure followed:

    #. PID and record are resolved.

    #. Permission are checked.

    #. ``view_method`` is called.

    :param pid_value: Persistent identifier value.
    :param resolver: An instance of a persistent identifier resolver. A
        persistent identifier resolver takes care of resolving persistent
        identifiers into internal objects.
    :param template: Template to render.
    :param permission_factory: Permission factory called to check if user has
        enough power to execute the action.
    :param view_method: Function that is called.
    :returns: Tuple (pid object, record object).


def default_view_method(pid, record, template=None, **kwargs):

record_viewed.send(<EOL>current_app._get_current_object(),<EOL>pid=pid,<EOL>record=record,<EOL>)<EOL>return render_template(<EOL>template,<EOL>pid=pid,<EOL>record=record,<EOL>)<EOL>

r"""Display default view.

    Sends record_viewed signal and renders template.

    :param pid: PID object.
    :param record: Record object.
    :param template: Template to render.
    :param \*\*kwargs: Additional view arguments based on URL rule.
    :returns: The rendered template.


def export(pid, record, template=None, **kwargs):

formats = current_app.config.get('<STR_LIT>', {}).get(<EOL>pid.pid_type)<EOL>fmt = formats.get(request.view_args.get('<STR_LIT>'))<EOL>if fmt is False:<EOL><INDENT>abort(<NUM_LIT>)<EOL><DEDENT>elif fmt is None:<EOL><INDENT>abort(<NUM_LIT>)<EOL><DEDENT>else:<EOL><INDENT>serializer = obj_or_import_string(fmt['<STR_LIT>'])<EOL>data = serializer.serialize(pid, record)<EOL>if isinstance(data, six.binary_type):<EOL><INDENT>data = data.decode('<STR_LIT:utf8>')<EOL><DEDENT>return render_template(<EOL>template, pid=pid, record=record, data=data,<EOL>format_title=fmt['<STR_LIT:title>'],<EOL>)<EOL><DEDENT>

r"""Record serialization view.

    Serializes record with given format and renders record export template.

    :param pid: PID object.
    :param record: Record object.
    :param template: Template to render.
    :param \*\*kwargs: Additional view arguments based on URL rule.
    :return: The rendered template.


self.app = app<EOL>self._permission_factory = None<EOL>self._export_formats = {}<EOL>

Initialize state.

        :param app: The Flask application.


if pid_type not in self._export_formats:<EOL><INDENT>fmts = self.app.config.get('<STR_LIT>', {}).get(<EOL>pid_type, {})<EOL>self._export_formats[pid_type] = sorted(<EOL>[(k, v) for k, v in fmts.items() if v],<EOL>key=lambda x: x[<NUM_LIT:1>]['<STR_LIT>'],<EOL>)<EOL><DEDENT>return self._export_formats[pid_type]<EOL>


if self._permission_factory is None:<EOL><INDENT>imp = self.app.config['<STR_LIT>']<EOL>self._permission_factory = obj_or_import_string(imp)<EOL><DEDENT>return self._permission_factory<EOL>


if app:<EOL><INDENT>self.init_app(app)<EOL><DEDENT>

Extension initialization.

        :param app: The Flask application. (Default: ``None``)


self.init_config(app)<EOL>app.extensions['<STR_LIT>'] = _RecordUIState(app)<EOL>

Flask application initialization.

        :param app: The Flask application.


app.config.setdefault(<EOL>'<STR_LIT>',<EOL>app.config.get('<STR_LIT>', '<STR_LIT>')<EOL>)<EOL>for k in dir(config):<EOL><INDENT>if k.startswith('<STR_LIT>'):<EOL><INDENT>app.config.setdefault(k, getattr(config, k))<EOL><DEDENT><DEDENT>

Initialize configuration on application.

        :param app: The Flask application.


if isinstance(value, six.string_types):<EOL><INDENT>return import_string(value)<EOL><DEDENT>elif value:<EOL><INDENT>return value<EOL><DEDENT>return default<EOL>

Import string or return object.

    :params value: Import path or class object to instantiate.
    :params default: Default object to return if the import fails.
    :returns: The imported object.


config_option_help = "<STR_LIT>"<EOL>parser = OptionParser()<EOL>parser.add_option("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>",<EOL>type="<STR_LIT:string>", dest="<STR_LIT>", help="<STR_LIT>")<EOL>parser.add_option("<STR_LIT:-c>", "<STR_LIT>", action="<STR_LIT:store>",<EOL>type="<STR_LIT:string>", dest="<STR_LIT>", help=config_option_help)<EOL>parser.add_option("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>",<EOL>type="<STR_LIT:string>", dest="<STR_LIT>", help="<STR_LIT>")<EOL>parser.add_option("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>",<EOL>type="<STR_LIT:string>", dest="<STR_LIT>", help="<STR_LIT>")<EOL>parser.add_option("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>",<EOL>type="<STR_LIT:string>", dest="<STR_LIT>", help="<STR_LIT>")<EOL>options, args = parser.parse_args()<EOL>if options.config:<EOL><INDENT>if options.config == "<STR_LIT>":<EOL><INDENT>config_option_list = '<STR_LIT>'<EOL>config_sections = config.sections()<EOL>for section in config_sections:<EOL><INDENT>config_option_list = config_option_list+section+"<STR_LIT:\n>"<EOL>section_items = config.items(section)<EOL>for item in section_items:<EOL><INDENT>config_option_list = config_option_list +"<STR_LIT:U+0020>"+item[<NUM_LIT:0>]+"<STR_LIT:U+0020>"+item[<NUM_LIT:1>]+"<STR_LIT:\n>"<EOL><DEDENT><DEDENT>print(config_option_list)<EOL>quit()<EOL><DEDENT><DEDENT>def add_notes(note_name, existing_tags):<EOL><INDENT>call([editor, environ["<STR_LIT>"] + "<STR_LIT>"+note_name+"<STR_LIT>"])<EOL>definedtags = input("<STR_LIT>").split("<STR_LIT:U+0020>")<EOL>definedtags.append(note_name)<EOL>print(definedtags)<EOL>print(existing_tags)<EOL>definedtags = list(set(definedtags)-set(existing_tags))<EOL>print(definedtags)<EOL>if len(definedtags) > <NUM_LIT:0>:<EOL><INDENT>modify_tags_xml(note_name, definedtags, files,<EOL>rootfiles, tags, roottags, tree, TAGS_XML_DIR)<EOL><DEDENT><DEDENT>def get_tags_from_file(note_name):<EOL><INDENT>fil = get_file_from_files(note_name)<EOL>filetags = fil.iter('<STR_LIT>')<EOL>filetaglist = []<EOL>for tag in filetags:<EOL><INDENT>filetaglist.append(tag.text)<EOL><DEDENT>return filetaglist<EOL><DEDENT>if options.addfile:<EOL><INDENT>existing_tags = []<EOL>if isFile(options.addfile, files):<EOL><INDENT>existing_tags = get_tags_from_file(options.addfile)<EOL>input("<STR_LIT>"+"<STR_LIT:U+0020>".join(existing_tags) +<EOL>"<STR_LIT>")<EOL><DEDENT>add_notes(options.addfile, existing_tags)<EOL>quit()<EOL><DEDENT>if options.editfile:<EOL><INDENT>if isFile(options.editfile, files):<EOL><INDENT>add_notes(note_name, [])<EOL><DEDENT>else:<EOL><INDENT>input(<EOL>"<STR_LIT>")<EOL>add_notes(note_name)<EOL><DEDENT><DEDENT>if options.remove:<EOL><INDENT>pass<EOL><DEDENT>if len(args) != <NUM_LIT:1>:<EOL><INDENT>print("<STR_LIT>")<EOL>quit()<EOL><DEDENT>_key_File = "<STR_LIT>"<EOL>_key_Results = "<STR_LIT>"<EOL>table = {_key_Results: []}<EOL>for tag in tags:<EOL><INDENT>if tag.attrib["<STR_LIT:value>"] == args[<NUM_LIT:0>]:<EOL><INDENT>fileelements = tag.iter("<STR_LIT:file>")<EOL>for fileelement in fileelements:<EOL><INDENT>f = open(<EOL>environ["<STR_LIT>"] + "<STR_LIT>" + fileelement.text+"<STR_LIT>", "<STR_LIT:r>")<EOL>table[_key_Results].append(<EOL>f.read()+"<STR_LIT>"+fileelement.text+"<STR_LIT>")<EOL>f.close()<EOL><DEDENT><DEDENT><DEDENT>print(tabulate(table, headers=[], tablefmt="<STR_LIT>"))<EOL>


config = configparser.RawConfigParser()<EOL>config.read(environ["<STR_LIT>"] + "<STR_LIT>")<EOL>editor = config.get("<STR_LIT>", "<STR_LIT:path>")<EOL>if not editor:<EOL><INDENT>print("<STR_LIT>")<EOL>quit()<EOL><DEDENT>config_option_help = "<STR_LIT>"<EOL>parser = OptionParser()<EOL>parser.add_option("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>",<EOL>type="<STR_LIT:string>", dest="<STR_LIT>", help="<STR_LIT>")<EOL>parser.add_option("<STR_LIT:-c>", "<STR_LIT>", action="<STR_LIT:store>",<EOL>type="<STR_LIT:string>", dest="<STR_LIT>", help=config_option_help)<EOL>parser.add_option("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>",<EOL>type="<STR_LIT:string>", dest="<STR_LIT>", help="<STR_LIT>")<EOL>parser.add_option("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>",<EOL>type="<STR_LIT:string>", dest="<STR_LIT>", help="<STR_LIT>")<EOL>parser.add_option("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>",<EOL>type="<STR_LIT:string>", dest="<STR_LIT>", help="<STR_LIT>")<EOL>options, args = parser.parse_args()<EOL>tree = ET.parse(environ["<STR_LIT>"] + "<STR_LIT>")<EOL>root = tree.getroot()<EOL>roottags = root.find('<STR_LIT>')<EOL>tags = roottags.iter('<STR_LIT>')<EOL>rootfiles = root.find('<STR_LIT>')<EOL>files = rootfiles.iter('<STR_LIT:file>')<EOL>if options.config:<EOL><INDENT>if options.config == "<STR_LIT>":<EOL><INDENT>config_option_list = '<STR_LIT>'<EOL>config_sections = config.sections()<EOL>for section in config_sections:<EOL><INDENT>config_option_list = config_option_list+section+"<STR_LIT:\n>"<EOL>section_items = config.items(section)<EOL>for item in section_items:<EOL><INDENT>config_option_list = config_option_list +"<STR_LIT:U+0020>"+item[<NUM_LIT:0>]+"<STR_LIT:U+0020>"+item[<NUM_LIT:1>]+"<STR_LIT:\n>"<EOL><DEDENT><DEDENT>print(config_option_list)<EOL>quit()<EOL><DEDENT><DEDENT>def add_notes(note_name, existing_tags):<EOL><INDENT>call([editor, environ["<STR_LIT>"] + "<STR_LIT>"+note_name+"<STR_LIT>"])<EOL>definedtags = input(<EOL>"<STR_LIT>").split("<STR_LIT:U+0020>")<EOL>definedtags.append(note_name)<EOL>print(definedtags)<EOL>print(existing_tags)<EOL>definedtags = list(set(definedtags)-set(existing_tags))<EOL>print(definedtags)<EOL>quit()<EOL>if len(definedtags) > <NUM_LIT:0>:<EOL><INDENT>modify_tags_xml(note_name, definedtags)<EOL><DEDENT><DEDENT>def get_tags_from_file(note_name):<EOL><INDENT>fil = get_file_from_files(note_name)<EOL>filetags = fil.iter('<STR_LIT>')<EOL>filetaglist = []<EOL>for tag in filetags:<EOL><INDENT>filetaglist.append(tag.text)<EOL><DEDENT>return filetaglist<EOL><DEDENT>if options.addfile:<EOL><INDENT>if isFile(options.addfile):<EOL><INDENT>existing_tags = get_tags_from_file(options.addfile)<EOL>input("<STR_LIT>"+"<STR_LIT:U+0020>".join(existing_tags) +<EOL>"<STR_LIT>")<EOL><DEDENT>add_notes(options.addfile, existing_tags)<EOL>quit()<EOL><DEDENT>if options.editfile:<EOL><INDENT>if isFile(options.editfile):<EOL><INDENT>add_notes(note_name, [])<EOL><DEDENT>else:<EOL><INDENT>input(<EOL>"<STR_LIT>")<EOL>add_notes(note_name)<EOL><DEDENT><DEDENT>if options.remove:<EOL><INDENT>pass<EOL><DEDENT>if len(args) != <NUM_LIT:1>:<EOL><INDENT>print("<STR_LIT>")<EOL>quit()<EOL><DEDENT>table = {"<STR_LIT>": [], "<STR_LIT>": []}<EOL>for tag in tags:<EOL><INDENT>if tag.attrib["<STR_LIT:value>"] == args[<NUM_LIT:0>]:<EOL><INDENT>fileelements = tag.iter("<STR_LIT:file>")<EOL>for fileelement in fileelements:<EOL><INDENT>f = open(<EOL>environ["<STR_LIT>"] + "<STR_LIT>" + fileelement.text+"<STR_LIT>", "<STR_LIT:r>")<EOL>table["<STR_LIT>"].append(fileelement.text)<EOL>table["<STR_LIT>"].append(f.read())<EOL>f.close()<EOL><DEDENT><DEDENT><DEDENT>print(tabulate(table, headers="<STR_LIT>", tablefmt="<STR_LIT>"))<EOL>


head, tail = path.split(name)<EOL>if not tail:<EOL><INDENT>head, tail = path.split(head)<EOL><DEDENT>if head and tail and not path.exists(head):<EOL><INDENT>try:<EOL><INDENT>makedirs(head, mode)<EOL><DEDENT>except OSError as e:<EOL><INDENT>if e.errno != errno.EEXIST:<EOL><INDENT>raise<EOL><DEDENT><DEDENT>if tail == curdir:           <EOL><INDENT>return<EOL><DEDENT><DEDENT>mkdir(name, mode)<EOL>

makedirs(path [, mode=0777])

    Super-mkdir; create a leaf directory and all intermediate ones.
    Works like mkdir, except that any intermediate path segment (not
    just the rightmost) will be created if it does not exist.  This is
    recursive.


rmdir(name)<EOL>head, tail = path.split(name)<EOL>if not tail:<EOL><INDENT>head, tail = path.split(head)<EOL><DEDENT>while head and tail:<EOL><INDENT>try:<EOL><INDENT>rmdir(head)<EOL><DEDENT>except error:<EOL><INDENT>break<EOL><DEDENT>head, tail = path.split(head)<EOL><DEDENT>

removedirs(path)

    Super-rmdir; remove a leaf directory and all empty intermediate
    ones.  Works like rmdir except that, if the leaf directory is
    successfully removed, directories corresponding to rightmost path
    segments will be pruned away until either the whole path is
    consumed or an error occurs.  Errors during this latter phase are
    ignored -- they generally mean that a directory was not empty.


head, tail = path.split(new)<EOL>if head and tail and not path.exists(head):<EOL><INDENT>makedirs(head)<EOL><DEDENT>rename(old, new)<EOL>head, tail = path.split(old)<EOL>if head and tail:<EOL><INDENT>try:<EOL><INDENT>removedirs(head)<EOL><DEDENT>except error:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>

renames(old, new)

    Super-rename; create directories as necessary and delete any left
    empty.  Works like rename, except creation of any intermediate
    directories needed to make the new pathname good is attempted
    first.  After the rename, directories corresponding to rightmost
    path segments of the old name will be pruned until either the
    whole path is consumed or a nonempty directory is found.

    Note: this function can fail with the new directory structure made
    if you lack permissions needed to unlink the leaf directory or
    file.


def walk(top, topdown=True, onerror=None, followlinks=False):

islink, join, isdir = path.islink, path.join, path.isdir<EOL>try:<EOL><INDENT>names = listdir(top)<EOL><DEDENT>except error as err:<EOL><INDENT>if onerror is not None:<EOL><INDENT>onerror(err)<EOL><DEDENT>return<EOL><DEDENT>dirs, nondirs = [], []<EOL>for name in names:<EOL><INDENT>if isdir(join(top, name)):<EOL><INDENT>dirs.append(name)<EOL><DEDENT>else:<EOL><INDENT>nondirs.append(name)<EOL><DEDENT><DEDENT>if topdown:<EOL><INDENT>yield top, dirs, nondirs<EOL><DEDENT>for name in dirs:<EOL><INDENT>new_path = join(top, name)<EOL>if followlinks or not islink(new_path):<EOL><INDENT>for x in walk(new_path, topdown, onerror, followlinks):<EOL><INDENT>yield x<EOL><DEDENT><DEDENT><DEDENT>if not topdown:<EOL><INDENT>yield top, dirs, nondirs<EOL><DEDENT>

Directory tree generator.

    For each directory in the directory tree rooted at top (including top
    itself, but excluding '.' and '..'), yields a 3-tuple

        dirpath, dirnames, filenames

    dirpath is a string, the path to the directory.  dirnames is a list of
    the names of the subdirectories in dirpath (excluding '.' and '..').
    filenames is a list of the names of the non-directory files in dirpath.
    Note that the names in the lists are just names, with no path components.
    To get a full path (which begins with top) to a file or directory in
    dirpath, do os.path.join(dirpath, name).

    If optional arg 'topdown' is true or not specified, the triple for a
    directory is generated before the triples for any of its subdirectories
    (directories are generated top down).  If topdown is false, the triple
    for a directory is generated after the triples for all of its
    subdirectories (directories are generated bottom up).

    When topdown is true, the caller can modify the dirnames list in-place
    (e.g., via del or slice assignment), and walk will only recurse into the
    subdirectories whose names remain in dirnames; this can be used to prune the
    search, or to impose a specific order of visiting.  Modifying dirnames when
    topdown is false is ineffective, since the directories in dirnames have
    already been generated by the time dirnames itself is generated. No matter
    the value of topdown, the list of subdirectories is retrieved before the
    tuples for the directory and its subdirectories are generated.

    By default errors from the os.listdir() call are ignored.  If
    optional arg 'onerror' is specified, it should be a function; it
    will be called with one argument, an os.error instance.  It can
    report the error to continue with the walk, or raise the exception
    to abort the walk.  Note that the filename is available as the
    filename attribute of the exception object.

    By default, os.walk does not follow symbolic links to subdirectories on
    systems that support them.  In order to get this functionality, set the
    optional argument 'followlinks' to true.

    Caution:  if you pass a relative pathname for top, don't change the
    current working directory between resumptions of walk.  walk never
    changes the current directory, and assumes that the client doesn't
    either.

    Example:

    import os
    from os.path import join, getsize
    for root, dirs, files in os.walk('python/Lib/email'):
        print root, "consumes",
        print sum([getsize(join(root, name)) for name in files]),
        print "bytes in", len(files), "non-directory files"
        if 'CVS' in dirs:
            dirs.remove('CVS')  # don't visit CVS directories


execv(file, args)<EOL>

execl(file, *args)

    Execute the executable file with argument list args, replacing the
    current process.


env = args[-<NUM_LIT:1>]<EOL>execve(file, args[:-<NUM_LIT:1>], env)<EOL>

execle(file, *args, env)

    Execute the executable file with argument list args and
    environment env, replacing the current process.


execvp(file, args)<EOL>

execlp(file, *args)

    Execute the executable file (which is searched for along $PATH)
    with argument list args, replacing the current process.


env = args[-<NUM_LIT:1>]<EOL>execvpe(file, args[:-<NUM_LIT:1>], env)<EOL>

execlpe(file, *args, env)

    Execute the executable file (which is searched for along $PATH)
    with argument list args and environment env, replacing the current
    process.


_execvpe(file, args)<EOL>

execvp(file, args)

    Execute the executable file (which is searched for along $PATH)
    with argument list args, replacing the current process.
    args may be a list or tuple of strings.


_execvpe(file, args, env)<EOL>

execvpe(file, args, env)

    Execute the executable file (which is searched for along $PATH)
    with argument list args and environment env , replacing the
    current process.
    args may be a list or tuple of strings.


return environ.get(key, default)<EOL>

Get an environment variable, return None if it doesn't exist.
    The optional second argument can specify an alternate default.


def format(percent, value, grouping=False, monetary=False, *additional):

<EOL>match = _percent_re.match(percent)<EOL>if not match or len(match.group())!= len(percent):<EOL><INDENT>raise ValueError(("<STR_LIT>"<EOL>"<STR_LIT>") % repr(percent))<EOL><DEDENT>return _format(percent, value, grouping, monetary, *additional)<EOL>

Returns the locale-aware substitution of a %? specifier
    (percent).

    additional is for format strings which contain one or more
    '*' modifiers.


def format_string(f, val, grouping=False):

percents = list(_percent_re.finditer(f))<EOL>new_f = _percent_re.sub('<STR_LIT:%s>', f)<EOL>if isinstance(val, collections.abc.Mapping):<EOL><INDENT>new_val = []<EOL>for perc in percents:<EOL><INDENT>if perc.group()[-<NUM_LIT:1>]=='<STR_LIT:%>':<EOL><INDENT>new_val.append('<STR_LIT:%>')<EOL><DEDENT>else:<EOL><INDENT>new_val.append(format(perc.group(), val, grouping))<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>if not isinstance(val, tuple):<EOL><INDENT>val = (val,)<EOL><DEDENT>new_val = []<EOL>i = <NUM_LIT:0><EOL>for perc in percents:<EOL><INDENT>if perc.group()[-<NUM_LIT:1>]=='<STR_LIT:%>':<EOL><INDENT>new_val.append('<STR_LIT:%>')<EOL><DEDENT>else:<EOL><INDENT>starcount = perc.group('<STR_LIT>').count('<STR_LIT:*>')<EOL>new_val.append(_format(perc.group(),<EOL>val[i],<EOL>grouping,<EOL>False,<EOL>*val[i+<NUM_LIT:1>:i+<NUM_LIT:1>+starcount]))<EOL>i += (<NUM_LIT:1> + starcount)<EOL><DEDENT><DEDENT><DEDENT>val = tuple(new_val)<EOL>return new_f % val<EOL>

Formats a string in the same way that the % formatting would use,
    but takes the current locale into account.
    Grouping is applied if the third parameter is true.


def currency(val, symbol=True, grouping=False, international=False):

conv = localeconv()<EOL>digits = conv[international and '<STR_LIT>' or '<STR_LIT>']<EOL>if digits == <NUM_LIT>:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>s = format('<STR_LIT>' % digits, abs(val), grouping, monetary=True)<EOL>s = '<STR_LIT:<>' + s + '<STR_LIT:>>'<EOL>if symbol:<EOL><INDENT>smb = conv[international and '<STR_LIT>' or '<STR_LIT>']<EOL>precedes = conv[val<<NUM_LIT:0> and '<STR_LIT>' or '<STR_LIT>']<EOL>separated = conv[val<<NUM_LIT:0> and '<STR_LIT>' or '<STR_LIT>']<EOL>if precedes:<EOL><INDENT>s = smb + (separated and '<STR_LIT:U+0020>' or '<STR_LIT>') + s<EOL><DEDENT>else:<EOL><INDENT>s = s + (separated and '<STR_LIT:U+0020>' or '<STR_LIT>') + smb<EOL><DEDENT><DEDENT>sign_pos = conv[val<<NUM_LIT:0> and '<STR_LIT>' or '<STR_LIT>']<EOL>sign = conv[val<<NUM_LIT:0> and '<STR_LIT>' or '<STR_LIT>']<EOL>if sign_pos == <NUM_LIT:0>:<EOL><INDENT>s = '<STR_LIT:(>' + s + '<STR_LIT:)>'<EOL><DEDENT>elif sign_pos == <NUM_LIT:1>:<EOL><INDENT>s = sign + s<EOL><DEDENT>elif sign_pos == <NUM_LIT:2>:<EOL><INDENT>s = s + sign<EOL><DEDENT>elif sign_pos == <NUM_LIT:3>:<EOL><INDENT>s = s.replace('<STR_LIT:<>', sign)<EOL><DEDENT>elif sign_pos == <NUM_LIT:4>:<EOL><INDENT>s = s.replace('<STR_LIT:>>', sign)<EOL><DEDENT>else:<EOL><INDENT>s = sign + s<EOL><DEDENT>return s.replace('<STR_LIT:<>', '<STR_LIT>').replace('<STR_LIT:>>', '<STR_LIT>')<EOL>

Formats val according to the currency settings
    in the current locale.


return format("<STR_LIT>", val)<EOL>

Convert float to string, taking the locale into account.


<EOL>ts = localeconv()['<STR_LIT>']<EOL>if ts:<EOL><INDENT>string = string.replace(ts, '<STR_LIT>')<EOL><DEDENT>dd = localeconv()['<STR_LIT>']<EOL>if dd:<EOL><INDENT>string = string.replace(dd, '<STR_LIT:.>')<EOL><DEDENT>return func(string)<EOL>

Parses a string as a float according to the locale settings.


return atof(str, int)<EOL>

Converts a string to an integer according to the locale settings.


<EOL>if isinstance(localename, _unicode):<EOL><INDENT>localename = localename.encode('<STR_LIT:ascii>')<EOL><DEDENT>code = localename.translate(_ascii_lower_map)<EOL>if '<STR_LIT::>' in code:<EOL><INDENT>code = code.replace('<STR_LIT::>', '<STR_LIT:.>')<EOL><DEDENT>if '<STR_LIT:@>' in code:<EOL><INDENT>code, modifier = code.split('<STR_LIT:@>', <NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>modifier = '<STR_LIT>'<EOL><DEDENT>if '<STR_LIT:.>' in code:<EOL><INDENT>langname, encoding = code.split('<STR_LIT:.>')[:<NUM_LIT:2>]<EOL><DEDENT>else:<EOL><INDENT>langname = code<EOL>encoding = '<STR_LIT>'<EOL><DEDENT>lang_enc = langname<EOL>if encoding:<EOL><INDENT>norm_encoding = encoding.replace('<STR_LIT:->', '<STR_LIT>')<EOL>norm_encoding = norm_encoding.replace('<STR_LIT:_>', '<STR_LIT>')<EOL>lang_enc += '<STR_LIT:.>' + norm_encoding<EOL><DEDENT>lookup_name = lang_enc<EOL>if modifier:<EOL><INDENT>lookup_name += '<STR_LIT:@>' + modifier<EOL><DEDENT>code = locale_alias.get(lookup_name, None)<EOL>if code is not None:<EOL><INDENT>return code<EOL><DEDENT>if modifier:<EOL><INDENT>code = locale_alias.get(lang_enc, None)<EOL>if code is not None:<EOL><INDENT>if '<STR_LIT:@>' not in code:<EOL><INDENT>return code + '<STR_LIT:@>' + modifier<EOL><DEDENT>if code.split('<STR_LIT:@>', <NUM_LIT:1>)[<NUM_LIT:1>].translate(_ascii_lower_map) == modifier:<EOL><INDENT>return code<EOL><DEDENT><DEDENT><DEDENT>if encoding:<EOL><INDENT>lookup_name = langname<EOL>if modifier:<EOL><INDENT>lookup_name += '<STR_LIT:@>' + modifier<EOL><DEDENT>code = locale_alias.get(lookup_name, None)<EOL>if code is not None:<EOL><INDENT>if '<STR_LIT:@>' not in code:<EOL><INDENT>return _replace_encoding(code, encoding)<EOL><DEDENT>code, modifier = code.split('<STR_LIT:@>', <NUM_LIT:1>)<EOL>return _replace_encoding(code, encoding) + '<STR_LIT:@>' + modifier<EOL><DEDENT>if modifier:<EOL><INDENT>code = locale_alias.get(langname, None)<EOL>if code is not None:<EOL><INDENT>if '<STR_LIT:@>' not in code:<EOL><INDENT>return _replace_encoding(code, encoding) + '<STR_LIT:@>' + modifier<EOL><DEDENT>code, defmod = code.split('<STR_LIT:@>', <NUM_LIT:1>)<EOL>if defmod.translate(_ascii_lower_map) == modifier:<EOL><INDENT>return _replace_encoding(code, encoding) + '<STR_LIT:@>' + defmod<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return localename<EOL>

Returns a normalized locale code for the given locale
        name.

        The returned locale code is formatted for use with
        setlocale().

        If normalization fails, the original name is returned
        unchanged.

        If the given encoding is not known, the function defaults to
        the default encoding for the locale code just like setlocale()
        does.


code = normalize(localename)<EOL>if '<STR_LIT:@>' in code:<EOL><INDENT>code, modifier = code.split('<STR_LIT:@>', <NUM_LIT:1>)<EOL>if modifier == '<STR_LIT>' and '<STR_LIT:.>' not in code:<EOL><INDENT>return code, '<STR_LIT>'<EOL><DEDENT><DEDENT>if '<STR_LIT:.>' in code:<EOL><INDENT>return tuple(code.split('<STR_LIT:.>')[:<NUM_LIT:2>])<EOL><DEDENT>elif code == '<STR_LIT:C>':<EOL><INDENT>return None, None<EOL><DEDENT>raise ValueError('<STR_LIT>' % localename)<EOL>

Parses the locale code for localename and returns the
        result as tuple (language code, encoding).

        The localename is normalized and passed through the locale
        alias engine. A ValueError is raised in case the locale name
        cannot be parsed.

        The language code corresponds to RFC 1766.  code and encoding
        can be None in case the values cannot be determined or are
        unknown to this implementation.


language, encoding = localetuple<EOL>if language is None:<EOL><INDENT>language = '<STR_LIT:C>'<EOL><DEDENT>if encoding is None:<EOL><INDENT>return language<EOL><DEDENT>else:<EOL><INDENT>return language + '<STR_LIT:.>' + encoding<EOL><DEDENT>

Builds a locale code from the given tuple (language code,
        encoding).

        No aliasing or normalizing takes place.


def getdefaultlocale(envvars=('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>')):

try:<EOL><INDENT>import _locale<EOL>code, encoding = _locale._getdefaultlocale()<EOL><DEDENT>except (ImportError, AttributeError):<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>if sys.platform == "<STR_LIT:win32>" and code and code[:<NUM_LIT:2>] == "<STR_LIT>":<EOL><INDENT>code = windows_locale.get(int(code, <NUM_LIT:0>))<EOL><DEDENT>return code, encoding<EOL><DEDENT>import os<EOL>lookup = os.environ.get<EOL>for variable in envvars:<EOL><INDENT>localename = lookup(variable,None)<EOL>if localename:<EOL><INDENT>if variable == '<STR_LIT>':<EOL><INDENT>localename = localename.split('<STR_LIT::>')[<NUM_LIT:0>]<EOL><DEDENT>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>localename = '<STR_LIT:C>'<EOL><DEDENT>return _parse_localename(localename)<EOL>

Tries to determine the default locale settings and returns
        them as tuple (language code, encoding).

        According to POSIX, a program which has not called
        setlocale(LC_ALL, "") runs using the portable 'C' locale.
        Calling setlocale(LC_ALL, "") lets it use the default locale as
        defined by the LANG variable. Since we don't want to interfere
        with the current locale setting we thus emulate the behavior
        in the way described above.

        To maintain compatibility with other platforms, not only the
        LANG variable is tested, but a list of variables given as
        envvars parameter. The first found to be defined will be
        used. envvars defaults to the search path used in GNU gettext;
        it must always contain the variable name 'LANG'.

        Except for the code 'C', the language code corresponds to RFC
        1766.  code and encoding can be None in case the values cannot
        be determined.


localename = _setlocale(category)<EOL>if category == LC_ALL and '<STR_LIT:;>' in localename:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>return _parse_localename(localename)<EOL>

Returns the current setting for the given locale category as
        tuple (language code, encoding).

        category may be one of the LC_* value except LC_ALL. It
        defaults to LC_CTYPE.

        Except for the code 'C', the language code corresponds to RFC
        1766.  code and encoding can be None in case the values cannot
        be determined.


if locale and not isinstance(locale, (_str, _unicode)):<EOL><INDENT>locale = normalize(_build_localename(locale))<EOL><DEDENT>return _setlocale(category, locale)<EOL>

Set the locale for the given category.  The locale can be
        a string, an iterable of two strings (language code and encoding),
        or None.

        Iterables are converted to strings using the locale aliasing
        engine.  Locale strings are passed directly to the C lib.

        category may be given as one of the LC_* values.


_setlocale(category, _build_localename(getdefaultlocale()))<EOL>

Sets the locale for category to the default setting.

        The default setting is determined by calling
        getdefaultlocale(). category defaults to LC_ALL.


categories = {}<EOL>def _init_categories(categories=categories):<EOL><INDENT>for k,v in list(globals().items()):<EOL><INDENT>if k[:<NUM_LIT:3>] == '<STR_LIT>':<EOL><INDENT>categories[k] = v<EOL><DEDENT><DEDENT><DEDENT>_init_categories()<EOL>del categories['<STR_LIT>']<EOL>print('<STR_LIT>')<EOL>print('<STR_LIT:->'*<NUM_LIT>)<EOL>lang, enc = getdefaultlocale()<EOL>print('<STR_LIT>', lang or '<STR_LIT>')<EOL>print('<STR_LIT>', enc or '<STR_LIT>')<EOL>print()<EOL>print('<STR_LIT>')<EOL>print('<STR_LIT:->'*<NUM_LIT>)<EOL>for name,category in list(categories.items()):<EOL><INDENT>print(name, '<STR_LIT>')<EOL>lang, enc = getlocale(category)<EOL>print('<STR_LIT>', lang or '<STR_LIT>')<EOL>print('<STR_LIT>', enc or '<STR_LIT>')<EOL>print()<EOL><DEDENT>print()<EOL>print('<STR_LIT>')<EOL>print('<STR_LIT:->'*<NUM_LIT>)<EOL>resetlocale()<EOL>for name,category in list(categories.items()):<EOL><INDENT>print(name, '<STR_LIT>')<EOL>lang, enc = getlocale(category)<EOL>print('<STR_LIT>', lang or '<STR_LIT>')<EOL>print('<STR_LIT>', enc or '<STR_LIT>')<EOL>print()<EOL><DEDENT>try:<EOL><INDENT>setlocale(LC_ALL, "<STR_LIT>")<EOL><DEDENT>except:<EOL><INDENT>print('<STR_LIT>')<EOL>print('<STR_LIT>')<EOL>print('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>print()<EOL>print('<STR_LIT>')<EOL>print('<STR_LIT:->'*<NUM_LIT>)<EOL>for name,category in list(categories.items()):<EOL><INDENT>print(name, '<STR_LIT>')<EOL>lang, enc = getlocale(category)<EOL>print('<STR_LIT>', lang or '<STR_LIT>')<EOL>print('<STR_LIT>', enc or '<STR_LIT>')<EOL>print()<EOL><DEDENT><DEDENT>


for m in sys.modules.values():<EOL><INDENT>if ((_is_jython and not isinstance(m, ModuleType)) or<EOL>hasattr(m, '<STR_LIT>')):<EOL><INDENT>continue<EOL><DEDENT>f = getattr(m, '<STR_LIT>', None)<EOL>if f is None:<EOL><INDENT>continue<EOL><DEDENT>m.__file__ = os.path.abspath(f)<EOL><DEDENT>

Set all module' __file__ attribute to an absolute path


<EOL>L = []<EOL>known_paths = set()<EOL>for dir in sys.path:<EOL><INDENT>dir, dircase = makepath(dir)<EOL>if not dircase in known_paths:<EOL><INDENT>L.append(dir)<EOL>known_paths.add(dircase)<EOL><DEDENT><DEDENT>sys.path[:] = L<EOL>return known_paths<EOL>

Remove duplicate entries from sys.path along with making them
    absolute


from distutils.util import get_platform<EOL>s = "<STR_LIT>" % (get_platform(), sys.version)<EOL>if hasattr(sys, '<STR_LIT>'):<EOL><INDENT>s += '<STR_LIT>'<EOL><DEDENT>s = os.path.join(os.path.dirname(sys.path[-<NUM_LIT:1>]), s)<EOL>sys.path.append(s)<EOL>

Append ./build/lib.<platform> in case we're running in the build dir
    (especially for Guido :-)


d = set()<EOL>for dir in sys.path:<EOL><INDENT>try:<EOL><INDENT>if os.path.isdir(dir):<EOL><INDENT>dir, dircase = makepath(dir)<EOL>d.add(dircase)<EOL><DEDENT><DEDENT>except TypeError:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>return d<EOL>

Return a set containing all existing directory entries from sys.path


def addpackage(sitedir, name, known_paths):

if known_paths is None:<EOL><INDENT>_init_pathinfo()<EOL>reset = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>reset = <NUM_LIT:0><EOL><DEDENT>fullname = os.path.join(sitedir, name)<EOL>try:<EOL><INDENT>f = open(fullname, "<STR_LIT>")<EOL><DEDENT>except IOError:<EOL><INDENT>return<EOL><DEDENT>try:<EOL><INDENT>for line in f:<EOL><INDENT>if line.startswith("<STR_LIT:#>"):<EOL><INDENT>continue<EOL><DEDENT>if line.startswith("<STR_LIT>"):<EOL><INDENT>exec(line)<EOL>continue<EOL><DEDENT>line = line.rstrip()<EOL>dir, dircase = makepath(sitedir, line)<EOL>if not dircase in known_paths and os.path.exists(dir):<EOL><INDENT>sys.path.append(dir)<EOL>known_paths.add(dircase)<EOL><DEDENT><DEDENT><DEDENT>finally:<EOL><INDENT>f.close()<EOL><DEDENT>if reset:<EOL><INDENT>known_paths = None<EOL><DEDENT>return known_paths<EOL>

Add a new path to known_paths by combining sitedir and 'name' or execute
    sitedir if it starts with 'import


if known_paths is None:<EOL><INDENT>known_paths = _init_pathinfo()<EOL>reset = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>reset = <NUM_LIT:0><EOL><DEDENT>sitedir, sitedircase = makepath(sitedir)<EOL>if not sitedircase in known_paths:<EOL><INDENT>sys.path.append(sitedir)        <EOL><DEDENT>try:<EOL><INDENT>names = os.listdir(sitedir)<EOL><DEDENT>except os.error:<EOL><INDENT>return<EOL><DEDENT>names.sort()<EOL>for name in names:<EOL><INDENT>if name.endswith(os.extsep + "<STR_LIT>"):<EOL><INDENT>addpackage(sitedir, name, known_paths)<EOL><DEDENT><DEDENT>if reset:<EOL><INDENT>known_paths = None<EOL><DEDENT>return known_paths<EOL>

Add 'sitedir' argument to sys.path if missing and handle .pth files in
    'sitedir


def addsitepackages(known_paths, sys_prefix=sys.prefix, exec_prefix=sys.exec_prefix):

prefixes = [os.path.join(sys_prefix, "<STR_LIT>"), sys_prefix]<EOL>if exec_prefix != sys_prefix:<EOL><INDENT>prefixes.append(os.path.join(exec_prefix, "<STR_LIT>"))<EOL><DEDENT>for prefix in prefixes:<EOL><INDENT>if prefix:<EOL><INDENT>if sys.platform in ('<STR_LIT>', '<STR_LIT>') or _is_jython:<EOL><INDENT>sitedirs = [os.path.join(prefix, "<STR_LIT>", "<STR_LIT>")]<EOL><DEDENT>elif _is_pypy:<EOL><INDENT>sitedirs = [os.path.join(prefix, '<STR_LIT>')]<EOL><DEDENT>elif sys.platform == '<STR_LIT>' and prefix == sys_prefix:<EOL><INDENT>if prefix.startswith("<STR_LIT>"): <EOL><INDENT>sitedirs = [os.path.join("<STR_LIT>", sys.version[:<NUM_LIT:3>], "<STR_LIT>"),<EOL>os.path.join(prefix, "<STR_LIT>", "<STR_LIT>", "<STR_LIT>")]<EOL><DEDENT>else: <EOL><INDENT>sitedirs = [os.path.join(prefix, "<STR_LIT>",<EOL>"<STR_LIT>" + sys.version[:<NUM_LIT:3>], "<STR_LIT>")]<EOL><DEDENT><DEDENT>elif os.sep == '<STR_LIT:/>':<EOL><INDENT>sitedirs = [os.path.join(prefix,<EOL>"<STR_LIT>",<EOL>"<STR_LIT>" + sys.version[:<NUM_LIT:3>],<EOL>"<STR_LIT>"),<EOL>os.path.join(prefix, "<STR_LIT>", "<STR_LIT>"),<EOL>os.path.join(prefix, "<STR_LIT>" + sys.version[:<NUM_LIT:3>], "<STR_LIT>")]<EOL>lib64_dir = os.path.join(prefix, "<STR_LIT>", "<STR_LIT>" + sys.version[:<NUM_LIT:3>], "<STR_LIT>")<EOL>if (os.path.exists(lib64_dir) and<EOL>os.path.realpath(lib64_dir) not in [os.path.realpath(p) for p in sitedirs]):<EOL><INDENT>if _is_64bit:<EOL><INDENT>sitedirs.insert(<NUM_LIT:0>, lib64_dir)<EOL><DEDENT>else:<EOL><INDENT>sitedirs.append(lib64_dir)<EOL><DEDENT><DEDENT>try:<EOL><INDENT>sys.getobjects<EOL>sitedirs.insert(<NUM_LIT:0>, os.path.join(sitedirs[<NUM_LIT:0>], '<STR_LIT>'))<EOL><DEDENT>except AttributeError:<EOL><INDENT>pass<EOL><DEDENT>sitedirs.append(os.path.join(prefix, "<STR_LIT>",<EOL>"<STR_LIT>" + sys.version[:<NUM_LIT:3>],<EOL>"<STR_LIT>"))<EOL>if sys.version[<NUM_LIT:0>] == '<STR_LIT:2>':<EOL><INDENT>sitedirs.append(os.path.join(prefix, "<STR_LIT>",<EOL>"<STR_LIT>" + sys.version[:<NUM_LIT:3>],<EOL>"<STR_LIT>"))<EOL><DEDENT>else:<EOL><INDENT>sitedirs.append(os.path.join(prefix, "<STR_LIT>",<EOL>"<STR_LIT>" + sys.version[<NUM_LIT:0>],<EOL>"<STR_LIT>"))<EOL><DEDENT>sitedirs.append(os.path.join(prefix, "<STR_LIT>", "<STR_LIT>"))<EOL><DEDENT>else:<EOL><INDENT>sitedirs = [prefix, os.path.join(prefix, "<STR_LIT>", "<STR_LIT>")]<EOL><DEDENT>if sys.platform == '<STR_LIT>':<EOL><INDENT>if '<STR_LIT>' in prefix:<EOL><INDENT>home = os.environ.get('<STR_LIT>')<EOL>if home:<EOL><INDENT>sitedirs.append(<EOL>os.path.join(home,<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>sys.version[:<NUM_LIT:3>],<EOL>'<STR_LIT>'))<EOL><DEDENT><DEDENT><DEDENT>for sitedir in sitedirs:<EOL><INDENT>if os.path.isdir(sitedir):<EOL><INDENT>addsitedir(sitedir, known_paths)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return None<EOL>

Add site-packages (and possibly site-python) to sys.path


if hasattr(sys, '<STR_LIT>') and getattr(sys.flags, '<STR_LIT>', False):<EOL><INDENT>return False<EOL><DEDENT>if hasattr(os, "<STR_LIT>") and hasattr(os, "<STR_LIT>"):<EOL><INDENT>if os.geteuid() != os.getuid():<EOL><INDENT>return None<EOL><DEDENT><DEDENT>if hasattr(os, "<STR_LIT>") and hasattr(os, "<STR_LIT>"):<EOL><INDENT>if os.getegid() != os.getgid():<EOL><INDENT>return None<EOL><DEDENT><DEDENT>return True<EOL>

Check if user site directory is safe for inclusion

    The function tests for the command line flag (including environment var),
    process uid/gid equal to effective uid/gid.

    None: Disabled for security reasons
    False: Disabled by user (command line option)
    True: Safe and enabled


global USER_BASE, USER_SITE, ENABLE_USER_SITE<EOL>env_base = os.environ.get("<STR_LIT>", None)<EOL>def joinuser(*args):<EOL><INDENT>return os.path.expanduser(os.path.join(*args))<EOL><DEDENT>if os.name == "<STR_LIT>":<EOL><INDENT>base = os.environ.get("<STR_LIT>") or "<STR_LIT>"<EOL>if env_base:<EOL><INDENT>USER_BASE = env_base<EOL><DEDENT>else:<EOL><INDENT>USER_BASE = joinuser(base, "<STR_LIT>")<EOL><DEDENT>USER_SITE = os.path.join(USER_BASE,<EOL>"<STR_LIT>" + sys.version[<NUM_LIT:0>] + sys.version[<NUM_LIT:2>],<EOL>"<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>if env_base:<EOL><INDENT>USER_BASE = env_base<EOL><DEDENT>else:<EOL><INDENT>USER_BASE = joinuser("<STR_LIT>", "<STR_LIT>")<EOL><DEDENT>USER_SITE = os.path.join(USER_BASE, "<STR_LIT>",<EOL>"<STR_LIT>" + sys.version[:<NUM_LIT:3>],<EOL>"<STR_LIT>")<EOL><DEDENT>if ENABLE_USER_SITE and os.path.isdir(USER_SITE):<EOL><INDENT>addsitedir(USER_SITE, known_paths)<EOL><DEDENT>if ENABLE_USER_SITE:<EOL><INDENT>for dist_libdir in ("<STR_LIT>", "<STR_LIT>"):<EOL><INDENT>user_site = os.path.join(USER_BASE, dist_libdir,<EOL>"<STR_LIT>" + sys.version[:<NUM_LIT:3>],<EOL>"<STR_LIT>")<EOL>if os.path.isdir(user_site):<EOL><INDENT>addsitedir(user_site, known_paths)<EOL><DEDENT><DEDENT><DEDENT>return known_paths<EOL>

Add a per user site-package to sys.path

    Each user has its own python directory with site-packages in the
    home directory.

    USER_BASE is the root directory for all Python versions

    USER_SITE is the user specific site-packages directory

    USER_SITE/.. can be used for data.


dllpath = os.path.join(sys.prefix, "<STR_LIT>", "<STR_LIT>")<EOL>libpath = os.environ['<STR_LIT>'].split('<STR_LIT:;>')<EOL>if libpath[-<NUM_LIT:1>]:<EOL><INDENT>libpath.append(dllpath)<EOL><DEDENT>else:<EOL><INDENT>libpath[-<NUM_LIT:1>] = dllpath<EOL><DEDENT>os.environ['<STR_LIT>'] = '<STR_LIT:;>'.join(libpath)<EOL>

The OS/2 EMX port has optional extension modules that do double duty
    as DLLs (and must use the .DLL file extension) for other extensions.
    The library search path needs to be amended so these will be found
    during module import.  Use BEGINLIBPATH so that these are at the start
    of the library search path.


if os.sep == '<STR_LIT::>':<EOL><INDENT>eof = '<STR_LIT>'<EOL><DEDENT>elif os.sep == '<STR_LIT:\\>':<EOL><INDENT>eof = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>eof = '<STR_LIT>'<EOL><DEDENT>class Quitter(object):<EOL><INDENT>def __init__(self, name):<EOL><INDENT>self.name = name<EOL><DEDENT>def __repr__(self):<EOL><INDENT>return '<STR_LIT>' % (self.name, eof)<EOL><DEDENT>def __call__(self, code=None):<EOL><INDENT>try:<EOL><INDENT>sys.stdin.close()<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT>raise SystemExit(code)<EOL><DEDENT><DEDENT>builtins.quit = Quitter('<STR_LIT>')<EOL>builtins.exit = Quitter('<STR_LIT>')<EOL>

Define new built-ins 'quit' and 'exit'.
    These are simply strings that display a hint on how to exit.


builtins.copyright = _Printer("<STR_LIT>", sys.copyright)<EOL>if _is_jython:<EOL><INDENT>builtins.credits = _Printer(<EOL>"<STR_LIT>",<EOL>"<STR_LIT>")<EOL><DEDENT>elif _is_pypy:<EOL><INDENT>builtins.credits = _Printer(<EOL>"<STR_LIT>",<EOL>"<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>builtins.credits = _Printer("<STR_LIT>", """<STR_LIT>""")<EOL><DEDENT>here = os.path.dirname(os.__file__)<EOL>builtins.license = _Printer(<EOL>"<STR_LIT>", "<STR_LIT>" % sys.version,<EOL>["<STR_LIT>", "<STR_LIT>"],<EOL>[os.path.join(here, os.pardir), here, os.curdir])<EOL>

Set 'copyright' and 'credits' in __builtin__


if sys.platform == '<STR_LIT:win32>':<EOL><INDENT>import locale, codecs<EOL>enc = locale.getdefaultlocale()[<NUM_LIT:1>]<EOL>if enc.startswith('<STR_LIT>'):            <EOL><INDENT>try:<EOL><INDENT>codecs.lookup(enc)<EOL><DEDENT>except LookupError:<EOL><INDENT>import encodings<EOL>encodings._cache[enc] = encodings._unknown<EOL>encodings.aliases.aliases[enc] = '<STR_LIT>'<EOL><DEDENT><DEDENT><DEDENT>

On Windows, some default encodings are not provided by Python,
    while they are always available as "mbcs" in each locale. Make
    them usable by aliasing to "mbcs" in such a case.


encoding = "<STR_LIT:ascii>" <EOL>if <NUM_LIT:0>:<EOL><INDENT>import locale<EOL>loc = locale.getdefaultlocale()<EOL>if loc[<NUM_LIT:1>]:<EOL><INDENT>encoding = loc[<NUM_LIT:1>]<EOL><DEDENT><DEDENT>if <NUM_LIT:0>:<EOL><INDENT>encoding = "<STR_LIT>"<EOL><DEDENT>if encoding != "<STR_LIT:ascii>":<EOL><INDENT>sys.setdefaultencoding(encoding)<EOL><DEDENT>

Set the string encoding used by the Unicode implementation.  The
    default is 'ascii', but if you're willing to experiment, you can
    change this.


try:<EOL><INDENT>import sitecustomize<EOL><DEDENT>except ImportError:<EOL><INDENT>pass<EOL><DEDENT>

Run custom site specific code, if available.


egginsert = getattr(sys, '<STR_LIT>', <NUM_LIT:0>)<EOL>for i, path in enumerate(sys.path):<EOL><INDENT>if i > egginsert and path.startswith(sys.prefix):<EOL><INDENT>egginsert = i<EOL><DEDENT><DEDENT>sys.__egginsert = egginsert + <NUM_LIT:1><EOL>

Force easy_installed eggs in the global environment to get placed
in sys.path after all packages inside the virtualenv.  This
maintains the "least surprise" result that packages in the
virtualenv always mask global packages, never the other way
around.


paths = []<EOL>classpaths = []<EOL>for path in sys.path:<EOL><INDENT>if path == '<STR_LIT>' or path.startswith('<STR_LIT>'):<EOL><INDENT>classpaths.append(path)<EOL><DEDENT>else:<EOL><INDENT>paths.append(path)<EOL><DEDENT><DEDENT>sys.path = paths<EOL>sys.path.extend(classpaths)<EOL>

Adjust the special classpath sys.path entries for Jython. These
    entries should follow the base virtualenv lib directories.


try:<EOL><INDENT>import usercustomize<EOL><DEDENT>except ImportError:<EOL><INDENT>pass<EOL><DEDENT>

Run custom user specific code, if available.


<EOL>names = cls.__dict__.get("<STR_LIT>")<EOL>if names is not None:<EOL><INDENT>return names<EOL><DEDENT>names = []<EOL>if not hasattr(cls, "<STR_LIT>"):<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>for c in cls.__mro__:<EOL><INDENT>if "<STR_LIT>" in c.__dict__:<EOL><INDENT>slots = c.__dict__['<STR_LIT>']<EOL>if isinstance(slots, str):<EOL><INDENT>slots = (slots,)<EOL><DEDENT>for name in slots:<EOL><INDENT>if name in ("<STR_LIT>", "<STR_LIT>"):<EOL><INDENT>continue<EOL><DEDENT>elif name.startswith('<STR_LIT>') and not name.endswith('<STR_LIT>'):<EOL><INDENT>stripped = c.__name__.lstrip('<STR_LIT:_>')<EOL>if stripped:<EOL><INDENT>names.append('<STR_LIT>' % (stripped, name))<EOL><DEDENT>else:<EOL><INDENT>names.append(name)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>names.append(name)<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>try:<EOL><INDENT>cls.__slotnames__ = names<EOL><DEDENT>except:<EOL><INDENT>pass <EOL><DEDENT>return names<EOL>

Return a list of slot names for a given class.

    This needs to find slots defined by the class and its bases, so we
    can't simply return the __slots__ attribute.  We must walk down
    the Method Resolution Order and concatenate the __slots__ of each
    class found there.  (This assumes classes don't modify their
    __slots__ attribute to misrepresent their slots after the class is
    defined.)