def itertrain(self, train, valid=None, algo='<STR_LIT>', subalgo='<STR_LIT>',<EOL>save_every=<NUM_LIT:0>, save_progress=None, **kwargs):

if '<STR_LIT>' not in kwargs:<EOL><INDENT>kwargs['<STR_LIT>'] = self._rng<EOL><DEDENT>def create_dataset(data, **kwargs):<EOL><INDENT>name = kwargs.get('<STR_LIT:name>', '<STR_LIT>')<EOL>s = '<STR_LIT>'.format(name)<EOL>return downhill.Dataset(<EOL>data,<EOL>name=name,<EOL>batch_size=kwargs.get('<STR_LIT>', <NUM_LIT:32>),<EOL>iteration_size=kwargs.get('<STR_LIT>', kwargs.get(s)),<EOL>axis=kwargs.get('<STR_LIT>', <NUM_LIT:0>),<EOL>rng=kwargs['<STR_LIT>'])<EOL><DEDENT>if valid is None:<EOL><INDENT>valid = train<EOL><DEDENT>if not isinstance(valid, downhill.Dataset):<EOL><INDENT>valid = create_dataset(valid, name='<STR_LIT>', **kwargs)<EOL><DEDENT>if not isinstance(train, downhill.Dataset):<EOL><INDENT>train = create_dataset(train, name='<STR_LIT:train>', **kwargs)<EOL><DEDENT>if '<STR_LIT>' in kwargs:<EOL><INDENT>warnings.warn(<EOL>'<STR_LIT>',<EOL>DeprecationWarning)<EOL>algo = kwargs.pop('<STR_LIT>')<EOL>if isinstance(algo, (list, tuple)):<EOL><INDENT>algo = algo[<NUM_LIT:0>]<EOL><DEDENT><DEDENT>if isinstance(algo, util.basestring):<EOL><INDENT>algo = algo.lower()<EOL>if algo == '<STR_LIT>':<EOL><INDENT>algo = trainer.SampleTrainer(self)<EOL><DEDENT>elif algo.startswith('<STR_LIT>') or algo.startswith('<STR_LIT>'):<EOL><INDENT>algo = trainer.SupervisedPretrainer(subalgo, self)<EOL><DEDENT>elif algo.startswith('<STR_LIT>') or algo.startswith('<STR_LIT>'):<EOL><INDENT>algo = trainer.UnsupervisedPretrainer(subalgo, self)<EOL><DEDENT>else:<EOL><INDENT>algo = trainer.DownhillTrainer(algo, self)<EOL><DEDENT><DEDENT>def needs_saving(elapsed, iteration):<EOL><INDENT>if save_progress is None:<EOL><INDENT>return False<EOL><DEDENT>if isinstance(save_every, float):<EOL><INDENT>return elapsed > <NUM_LIT> * save_every<EOL><DEDENT>if isinstance(save_every, int):<EOL><INDENT>return iteration % save_every == <NUM_LIT:0><EOL><DEDENT>return False<EOL><DEDENT>start = time.time()<EOL>for i, monitors in enumerate(algo.itertrain(train, valid, **kwargs)):<EOL><INDENT>yield monitors<EOL>now = time.time()<EOL>if i and needs_saving(now - start, i):<EOL><INDENT>filename_or_handle = save_progress<EOL>if isinstance(filename_or_handle, util.basestring):<EOL><INDENT>filename_or_handle = save_progress.format(int(now))<EOL><DEDENT>self.save(filename_or_handle)<EOL>start = now<EOL><DEDENT><DEDENT>

Train our network, one batch at a time.

        This method yields a series of ``(train, valid)`` monitor pairs. The
        ``train`` value is a dictionary mapping names to monitor values
        evaluated on the training dataset. The ``valid`` value is also a
        dictionary mapping names to values, but these values are evaluated on
        the validation dataset.

        Because validation might not occur every training iteration, the
        validation monitors might be repeated for multiple training iterations.
        It is probably most helpful to think of the validation monitors as being
        the "most recent" values that have been computed.

        After training completes, the network attribute of this class will
        contain the trained network parameters.

        Parameters
        ----------
        train : :class:`Dataset <downhill.dataset.Dataset>` or list
            A dataset to use when training the network. If this is a
            ``downhill.Dataset`` instance, it will be used directly as the
            training datset. If it is a list of numpy arrays or a list of
            callables, it will be converted to a ``downhill.Dataset`` and then
            used as the training set.
        valid : :class:`Dataset <downhill.dataset.Dataset>` or list, optional
            If this is provided, it will be used as a validation dataset. If not
            provided, the training set will be used for validation. (This is not
            recommended!)
        algo : str, optional
            An optimization algorithm to use for training our network. If not
            provided, :class:`RMSProp <downhill.adaptive.RMSProp>` will be used.
        subalgo : str, optional
            An optimization algorithm to use for a trainer that requires a
            "sub-algorithm," sugh as an unsupervised pretrainer. Defaults to
            :class:`RMSProp <downhill.adaptive.RMSProp>`.
        save_every : int or float, optional
            If this is nonzero and ``save_progress`` is not None, then the model
            being trained will be saved periodically. If this is a float, it is
            treated as a number of minutes to wait between savings. If it is an
            int, it is treated as the number of training epochs to wait between
            savings. Defaults to 0.
        save_progress : str or file handle, optional
            If this is not None, and ``save_progress`` is nonzero, then save the
            model periodically during training. This parameter gives either (a)
            the full path of a file to save the model, or (b) a file-like object
            where the model should be saved. If it is a string and the given
            name contains a "{}" format specifier, it will be filled with the
            integer Unix timestamp at the time the model is saved. Defaults to
            None, which does not save models.

        Yields
        ------
        training : dict
            A dictionary of monitor values computed using the training dataset,
            at the conclusion of training. This dictionary will at least contain
            a 'loss' key that indicates the value of the loss function. Other
            keys may be available depending on the trainer being used.
        validation : dict
            A dictionary of monitor values computed using the validation
            dataset, at the conclusion of training.


def score(self, x, y, w=None, **kwargs):

u = y - self.predict(x, **kwargs)<EOL>v = y - y.mean()<EOL>if w is None:<EOL><INDENT>w = np.ones_like(u)<EOL><DEDENT>return <NUM_LIT:1> - (w * u * u).sum() / (w * v * v).sum()<EOL>

Compute R^2 coefficient of determination for a given labeled input.

        Parameters
        ----------
        x : ndarray (num-examples, num-inputs)
            An array containing data to be fed into the network. Multiple
            examples are arranged as rows in this array, with columns containing
            the variables for each example.
        y : ndarray (num-examples, num-outputs)
            An array containing expected target data for the network. Multiple
            examples are arranged as rows in this array, with columns containing
            the variables for each example.

        Returns
        -------
        r2 : float
            The R^2 correlation between the prediction of this netork and its
            target output.


def add(s):<EOL><INDENT>h.update(str(s).encode('<STR_LIT:utf-8>'))<EOL><DEDENT>h = hashlib.md5()<EOL>for l in self.layers:<EOL><INDENT>add('<STR_LIT>'.format(l.__class__.__name__, l.name, l.output_shape))<EOL><DEDENT>for l in self.losses:<EOL><INDENT>add('<STR_LIT>'.format(l.__class__.__name__, l.weight))<EOL><DEDENT>for r in regularizers:<EOL><INDENT>add('<STR_LIT>'.format(r.__class__.__name__, r.weight, r.pattern))<EOL><DEDENT>return h.hexdigest()<EOL>

Construct a string key for representing a computation graph.

        This key will be unique for a given (a) network topology, (b) set of
        losses, and (c) set of regularizers.

        Returns
        -------
        key : str
            A hash representing the computation graph for the current network.


monitors = None<EOL>for monitors in self.itertrain(*args, **kwargs):<EOL><INDENT>pass<EOL><DEDENT>return monitors<EOL>

Train the network until the trainer converges.

        All arguments are passed to :func:`itertrain`.

        Returns
        -------
        training : dict
            A dictionary of monitor values computed using the training dataset,
            at the conclusion of training. This dictionary will at least contain
            a 'loss' key that indicates the value of the loss function. Other
            keys may be available depending on the trainer being used.
        validation : dict
            A dictionary of monitor values computed using the validation
            dataset, at the conclusion of training.


assert not isinstance(cls, Network),'<STR_LIT>'<EOL>if isinstance(filename_or_handle, util.basestring):<EOL><INDENT>opener = gzip.open if filename_or_handle.lower().endswith('<STR_LIT>') else open<EOL>handle = opener(filename_or_handle, '<STR_LIT:rb>')<EOL><DEDENT>else:<EOL><INDENT>handle = filename_or_handle<EOL><DEDENT>model = pickle.load(handle)<EOL>if isinstance(filename_or_handle, util.basestring):<EOL><INDENT>handle.close()<EOL><DEDENT>util.log('<STR_LIT>', filename_or_handle)<EOL>return model<EOL>

Load a saved network from disk.

        Parameters
        ----------
        filename_or_handle : str or file handle
            Load the state of this network from a pickle file. If this parameter
            is a string, it names the file where the pickle will be saved. If it
            is a file-like object, this object will be used for reading the
            pickle. If the filename ends in ".gz" then the output will
            automatically be gunzipped.


regs = regularizers.from_kwargs(self, **kwargs)<EOL>outputs, _ = self.build_graph(regs)<EOL>monitors = [('<STR_LIT>', self.losses[<NUM_LIT:0>](outputs))]<EOL>def matching(pattern):<EOL><INDENT>'''<STR_LIT>'''<EOL>for name, expr in util.outputs_matching(outputs, pattern):<EOL><INDENT>yield name, expr<EOL><DEDENT>for name, expr in util.params_matching(self.layers, pattern):<EOL><INDENT>yield name, expr<EOL><DEDENT><DEDENT>def parse_levels(levels):<EOL><INDENT>'''<STR_LIT>'''<EOL>if isinstance(levels, dict):<EOL><INDENT>levels = levels.items()<EOL><DEDENT>if isinstance(levels, (int, float)):<EOL><INDENT>levels = [levels]<EOL><DEDENT>for level in levels:<EOL><INDENT>if isinstance(level, (tuple, list)):<EOL><INDENT>label, call = level<EOL>yield '<STR_LIT>'.format(label), call<EOL><DEDENT>if isinstance(level, (int, float)):<EOL><INDENT>def call(expr):<EOL><INDENT>return (expr < level).mean()<EOL><DEDENT>yield '<STR_LIT>'.format(level), call<EOL><DEDENT><DEDENT><DEDENT>inputs = kwargs.get('<STR_LIT>', {})<EOL>if isinstance(inputs, dict):<EOL><INDENT>inputs = inputs.items()<EOL><DEDENT>for pattern, levels in inputs:<EOL><INDENT>for name, expr in matching(pattern):<EOL><INDENT>for key, value in parse_levels(levels):<EOL><INDENT>monitors.append(('<STR_LIT>'.format(name, key), value(expr)))<EOL><DEDENT><DEDENT><DEDENT>return monitors<EOL>

Return expressions that should be computed to monitor training.

        Returns
        -------
        monitors : list of (name, expression) pairs
            A list of named monitor expressions to compute for this network.


output = outputs[self.output_name]<EOL>k = output.shape[-<NUM_LIT:1>]<EOL>n = TT.prod(output.shape) // k<EOL>prob = output.reshape((n, k))[TT.arange(n), self._target.reshape((n, ))]<EOL>nlp = -TT.log(TT.clip(prob, <NUM_LIT>, <NUM_LIT:1>))<EOL>if self._weights is not None:<EOL><INDENT>return (self._weights.reshape((n, )) * nlp).sum() / self._weights.sum()<EOL><DEDENT>return nlp.mean()<EOL>

Construct the computation graph for this loss function.

        Parameters
        ----------
        outputs : dict of Theano expressions
            A dictionary mapping network output names to Theano expressions
            representing the outputs of a computation graph.

        Returns
        -------
        loss : Theano expression
            The values of the loss given the network output.


return '<STR_LIT>'.join(self._rev_index[c] for c in enc)<EOL>

Encode a text string by replacing characters with alphabet index.

        Parameters
        ----------
        classes : list of int
            A sequence of alphabet index values to convert to text.

        Returns
        -------
        txt : str
            A string containing corresponding characters from the alphabet.


return list(self._fwd_index.get(c, <NUM_LIT:0>) for c in txt)<EOL>

Encode a text string by replacing characters with alphabet index.

        Parameters
        ----------
        txt : str
            A string to encode.

        Returns
        -------
        classes : list of int
            A sequence of alphabet index values corresponding to the given text.


def plot_layers(weights, tied_weights=False, channels=<NUM_LIT:1>):

if hasattr(weights[<NUM_LIT:0>], '<STR_LIT>'):<EOL><INDENT>weights = [w.get_value() for w in weights]<EOL><DEDENT>k = min(len(weights), <NUM_LIT:9>)<EOL>imgs = np.eye(weights[<NUM_LIT:0>].shape[<NUM_LIT:0>])<EOL>for i, weight in enumerate(weights[:-<NUM_LIT:1>]):<EOL><INDENT>imgs = np.dot(weight.T, imgs)<EOL>plot_images(imgs,<EOL><NUM_LIT:100> + <NUM_LIT:10> * k + i + <NUM_LIT:1>,<EOL>channels=channels,<EOL>title='<STR_LIT>'.format(i+<NUM_LIT:1>))<EOL><DEDENT>weight = weights[-<NUM_LIT:1>]<EOL>n = weight.shape[<NUM_LIT:1>] / channels<EOL>if int(np.sqrt(n)) ** <NUM_LIT:2> != n:<EOL><INDENT>return<EOL><DEDENT>if tied_weights:<EOL><INDENT>imgs = np.dot(weight.T, imgs)<EOL>plot_images(imgs,<EOL><NUM_LIT:100> + <NUM_LIT:10> * k + k,<EOL>channels=channels,<EOL>title='<STR_LIT>'.format(k))<EOL><DEDENT>else:<EOL><INDENT>plot_images(weight,<EOL><NUM_LIT:100> + <NUM_LIT:10> * k + k,<EOL>channels=channels,<EOL>title='<STR_LIT>')<EOL><DEDENT>

Create a plot of weights, visualized as "bottom-level" pixel arrays.


def readline(self, use_raw=None, prompt='<STR_LIT>'):

if self.closed: raise ValueError<EOL>if <NUM_LIT:0> == len(self.input):<EOL><INDENT>self.closed = True<EOL>raise EOFError<EOL><DEDENT>line = self.input[<NUM_LIT:0>]<EOL>del self.input[<NUM_LIT:0>]<EOL>return line<EOL>

Read a line of input. EOFError will be raised on EOF.

        Note that we don't support prompting


if isinstance(inp, list):<EOL><INDENT>self.input = inp<EOL><DEDENT>else:<EOL><INDENT>raise IOError("<STR_LIT>" % (type(inp), inp))<EOL><DEDENT>return<EOL>


if isinstance(output, types.Listype):<EOL><INDENT>self.output = output<EOL><DEDENT>else:<EOL><INDENT>raise IOError("<STR_LIT>" % (type(output),<EOL>output))<EOL><DEDENT>return<EOL>

Use this to set where to write to. output can be a
        file object or a string. This code raises IOError on error.

        If another file was previously open upon calling this open,
        that will be stacked and will come back into use after
        a close_write().


name2id = Mthread.map_thread_names()<EOL>for thread_id, f in list(sys._current_frames().items()):<EOL><INDENT>self.assertEqual(thread_id,<EOL>name2id[Mthread.id2thread_name(thread_id)])<EOL>self.assertNotEqual(f, Mthread.find_debugged_frame(f))<EOL>pass<EOL><DEDENT>

Helper for testing map_thread_names and id2thread


self.write(msg)<EOL>self.output.append('<STR_LIT>')<EOL>return<EOL>

used to write to a debugger that is connected to this
        server; Here, we use the null string '' as an indicator of a
        newline.


self.closed = True<EOL>return<EOL>


self.closed = True<EOL>return<EOL>

Nothing to do here. Interface is for compatibility


def readline(self, prompt='<STR_LIT>', use_raw=None):

line = self.input.readline()<EOL>if not line: raise EOFError<EOL>return line.rstrip("<STR_LIT:\n>")<EOL>

Read a line of input. Prompt and use_raw exist to be
        compatible with other input routines and are ignored.
        EOFError will be raised on EOF.


def run(self, cmd, start_opts=None, globals_=None, locals_=None):

if globals_ is None:<EOL><INDENT>globals_ = globals()<EOL><DEDENT>if locals_ is None:<EOL><INDENT>locals_ = globals_<EOL><DEDENT>if not isinstance(cmd, types.CodeType):<EOL><INDENT>self.eval_string = cmd<EOL>cmd = cmd+'<STR_LIT:\n>'<EOL>pass<EOL><DEDENT>retval = None<EOL>self.core.start(start_opts)<EOL>try:<EOL><INDENT>retval = eval(cmd, globals_, locals_)<EOL><DEDENT>except SyntaxError:<EOL><INDENT>try:<EOL><INDENT>exec(cmd, globals_, locals_)<EOL><DEDENT>except DebuggerQuit:<EOL><INDENT>pass<EOL><DEDENT>except DebuggerQuit:<EOL><INDENT>pass<EOL><DEDENT>pass<EOL><DEDENT>except DebuggerQuit:<EOL><INDENT>pass<EOL><DEDENT>finally:<EOL><INDENT>self.core.stop()<EOL><DEDENT>return retval<EOL>

Run debugger on string `cmd' using builtin function eval
        and if that builtin exec.  Arguments `globals_' and `locals_'
        are the dictionaries to use for local and global variables. By
        default, the value of globals is globals(), the current global
        variables. If `locals_' is not given, it becomes a copy of
        `globals_'.

        Debugger.core.start settings are passed via optional
        dictionary `start_opts'. Overall debugger settings are in
        Debugger.settings which changed after an instance is created
        . Also see `run_eval' if what you want to run is an
        run_eval'able expression have that result returned and
        `run_call' if you want to debug function run_call.


def run_exec(self, cmd, start_opts=None, globals_=None, locals_=None):

if globals_ is None:<EOL><INDENT>globals_ = globals()<EOL><DEDENT>if locals_ is None:<EOL><INDENT>locals_ = globals_<EOL><DEDENT>if not isinstance(cmd, types.CodeType):<EOL><INDENT>cmd = cmd+'<STR_LIT:\n>'<EOL>pass<EOL><DEDENT>self.core.start(start_opts)<EOL>try:<EOL><INDENT>exec(cmd, globals_, locals_)<EOL><DEDENT>except DebuggerQuit:<EOL><INDENT>pass<EOL><DEDENT>finally:<EOL><INDENT>self.core.stop()<EOL><DEDENT>return<EOL>

Run debugger on string `cmd' which will executed via the
        builtin function exec. Arguments `globals_' and `locals_' are
        the dictionaries to use for local and global variables. By
        default, the value of globals is globals(), the current global
        variables. If `locals_' is not given, it becomes a copy of
        `globals_'.

        Debugger.core.start settings are passed via optional
        dictionary `start_opts'. Overall debugger settings are in
        Debugger.settings which changed after an instance is created
        . Also see `run_eval' if what you want to run is an
        run_eval'able expression have that result returned and
        `run_call' if you want to debug function run_call.


def parse_list_cmd(proc, args, listsize=<NUM_LIT:10>):

text = proc.current_command[len(args[<NUM_LIT:0>])+<NUM_LIT:1>:].strip()<EOL>if text in frozenset(('<STR_LIT>', '<STR_LIT:.>', '<STR_LIT:+>', '<STR_LIT:->')):<EOL><INDENT>if text == '<STR_LIT:.>':<EOL><INDENT>location = resolve_location(proc, '<STR_LIT:.>')<EOL>return location.path, location.line_number, listsize<EOL><DEDENT>else:<EOL><INDENT>if proc.list_lineno is None:<EOL><INDENT>proc.errmsg("<STR_LIT>")<EOL>return INVALID_PARSE_LIST<EOL><DEDENT>filename = proc.list_filename<EOL>if text == '<STR_LIT:+>':<EOL><INDENT>first = max(<NUM_LIT:1>, proc.list_lineno + listsize)<EOL><DEDENT>elif text == '<STR_LIT:->':<EOL><INDENT>if proc.list_lineno == <NUM_LIT:1> + listsize:<EOL><INDENT>proc.errmsg("<STR_LIT>" % proc.list_filename)<EOL>return INVALID_PARSE_LIST<EOL><DEDENT>first = max(<NUM_LIT:1>, proc.list_lineno - (<NUM_LIT:2>*listsize) - <NUM_LIT:1>)<EOL><DEDENT>elif text == '<STR_LIT>':<EOL><INDENT>first = proc.list_lineno + <NUM_LIT:1><EOL><DEDENT><DEDENT>last = first + listsize - <NUM_LIT:1><EOL>return filename, first, last<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>list_range = build_range(text)<EOL><DEDENT>except LocationError as e:<EOL><INDENT>proc.errmsg("<STR_LIT>")<EOL>proc.errmsg(e.text)<EOL>proc.errmsg(e.text_cursor)<EOL>return INVALID_PARSE_LIST<EOL><DEDENT>except ScannerError as e:<EOL><INDENT>proc.errmsg("<STR_LIT>")<EOL>proc.errmsg(e.text)<EOL>proc.errmsg(e.text_cursor)<EOL>return INVALID_PARSE_LIST<EOL><DEDENT>if list_range.first is None:<EOL><INDENT>assert isinstance(list_range.last, Location)<EOL>location = resolve_location(proc, list_range.last)<EOL>if not location:<EOL><INDENT>return INVALID_PARSE_LIST<EOL><DEDENT>last     = location.line_number<EOL>first    = max(<NUM_LIT:1>, last - listsize)<EOL>return location.path, first, last<EOL><DEDENT>elif isinstance(list_range.first, int):<EOL><INDENT>first    = list_range.first<EOL>location = resolve_location(proc, list_range.last)<EOL>if not location:<EOL><INDENT>return INVALID_PARSE_LIST<EOL><DEDENT>filename = location.path<EOL>last     = location.line_number<EOL>if last < first:<EOL><INDENT>last = first + last<EOL><DEDENT>return location.path, first, last<EOL><DEDENT>else:<EOL><INDENT>assert isinstance(list_range.first, Location)<EOL>location = resolve_location(proc, list_range.first)<EOL>if not location:<EOL><INDENT>return INVALID_PARSE_LIST<EOL><DEDENT>first    = location.line_number<EOL>last     = list_range.last<EOL>if location.method:<EOL><INDENT>first -= listsize // <NUM_LIT:2><EOL><DEDENT>if isinstance(last, str):<EOL><INDENT>assert last[<NUM_LIT:0>] == '<STR_LIT:+>'<EOL>last = first + int(last[<NUM_LIT:1>:])<EOL><DEDENT>elif not last:<EOL><INDENT>last = first + listsize<EOL><DEDENT>elif last < first:<EOL><INDENT>last = first + last<EOL><DEDENT>return location.path, first, last<EOL><DEDENT>pass<EOL><DEDENT>return<EOL>

Parses arguments for the "list" command and returns the tuple:
    (filename, first line number, last line number)
    or sets these to None if there was some problem.


if len(args) <= <NUM_LIT:2>:<EOL><INDENT>doc = self.__doc__ or self.run.__doc__<EOL>if doc:<EOL><INDENT>self.rst_msg(doc.rstrip('<STR_LIT:\n>'))<EOL><DEDENT>else:<EOL><INDENT>self.proc.intf[-<NUM_LIT:1>].errmsg('<STR_LIT>' +<EOL>'<STR_LIT>' +<EOL>self.name)<EOL>pass<EOL><DEDENT>return<EOL><DEDENT>subcmd_name = args[<NUM_LIT:2>]<EOL>if '<STR_LIT:*>' == subcmd_name:<EOL><INDENT>self.section("<STR_LIT>" % self.name)<EOL>self.msg(self.columnize_commands(self.cmds.list()))<EOL>return<EOL><DEDENT>cmd = self.cmds.lookup(subcmd_name)<EOL>if cmd:<EOL><INDENT>doc = cmd.__doc__ or cmd.run.__doc__<EOL>if doc:<EOL><INDENT>self.proc.rst_msg(doc.rstrip('<STR_LIT:\n>'))<EOL><DEDENT>else:<EOL><INDENT>self.proc.intf[-<NUM_LIT:1>].errmsg('<STR_LIT>'<EOL>'<STR_LIT>' %<EOL>(subcmd_name, self.name))<EOL>pass<EOL><DEDENT><DEDENT>else:<EOL><INDENT>cmds = [c for c in self.cmds.list()<EOL>if re.match('<STR_LIT>' + subcmd_name, c) ]<EOL>if cmds == []:<EOL><INDENT>self.errmsg("<STR_LIT>"<EOL>"<STR_LIT>" % (self.name, subcmd_name))<EOL><DEDENT>else:<EOL><INDENT>self.section("<STR_LIT>" %<EOL>(self.name, subcmd_name,))<EOL>self.msg_nocr(self.columnize_commands(cmds))<EOL>pass<EOL><DEDENT>pass<EOL><DEDENT>return<EOL>

Give help for a command which has subcommands. This can be
        called in several ways:
            help cmd
            help cmd subcmd
            help cmd commands

        Our shtick is to give help for the overall command only if
        subcommand or 'commands' is not given. If a subcommand is given and
        found, then specific help for that is given. If 'commands' is given
        we will list the all the subcommands.


<EOL>cmd_instances     = []<EOL>class_prefix      = capitalize(name)  <EOL>module_dir        = '<STR_LIT>' % name<EOL>mod               = __import__(module_dir, None, None, ['<STR_LIT:*>'])<EOL>eval_cmd_template = '<STR_LIT>'<EOL>for module_name in mod.__modules__:<EOL><INDENT>import_name = module_dir + '<STR_LIT:.>' + module_name<EOL>try:<EOL><INDENT>command_mod = importlib.import_module(import_name)<EOL><DEDENT>except ImportError:<EOL><INDENT>print(("<STR_LIT>" %<EOL>(import_name, module_name, sys.exc_info()[<NUM_LIT:0>])))<EOL>continue<EOL><DEDENT>classnames = [ classname for classname, classvalue in<EOL>inspect.getmembers(command_mod, inspect.isclass)<EOL>if ('<STR_LIT>' != classname and<EOL>classname.startswith(class_prefix)) ]<EOL>for classname in classnames:<EOL><INDENT>eval_cmd = eval_cmd_template % classname<EOL>try:<EOL><INDENT>instance = eval(eval_cmd)<EOL>self.cmds.add(instance)<EOL><DEDENT>except:<EOL><INDENT>print("<STR_LIT>" % classname)<EOL>pass<EOL><DEDENT>pass<EOL><DEDENT>pass<EOL><DEDENT>return cmd_instances<EOL>

Create an instance of each of the debugger
        subcommands. Commands are found by importing files in the
        directory 'name' + 'sub'. Some files are excluded via an array set
        in __init__.  For each of the remaining files, we import them
        and scan for class names inside those files and for each class
        name, we will create an instance of that class. The set of
        DebuggerCommand class instances form set of possible debugger
        commands.


try:<EOL><INDENT>return(self.debugger.intf[-<NUM_LIT:1>].msg(msg))<EOL><DEDENT>except EOFError:<EOL><INDENT>pass<EOL><DEDENT>return None<EOL>

Convenience short-hand for self.debugger.intf[-1].msg


try:<EOL><INDENT>return(self.debugger.intf[-<NUM_LIT:1>].msg_nocr(msg))<EOL><DEDENT>except EOFError:<EOL><INDENT>pass<EOL><DEDENT>return None<EOL>

Convenience short-hand for self.debugger.intf[-1].msg_nocr


text = Mformat.rst_text(text,<EOL>'<STR_LIT>' == self.debugger.settings['<STR_LIT>'],<EOL>self.debugger.settings['<STR_LIT:width>'])<EOL>return self.msg(text)<EOL>

Convert ReStructuredText and run through msg()


mainfile = self.core.filename(None)<EOL>if self.core.is_running():<EOL><INDENT>curframe = self.proc.curframe<EOL>if curframe:<EOL><INDENT>line_no = inspect.getlineno(curframe)<EOL>offset  = curframe.f_lasti<EOL>self.msg("<STR_LIT>" % offset)<EOL>offset = max(offset, <NUM_LIT:0>)<EOL>code = curframe.f_code<EOL>co_code = code.co_code<EOL>disassemble_bytes(self.msg, self.msg_nocr,<EOL>co_code, offset, line_no, line_no-<NUM_LIT:1>, line_no+<NUM_LIT:1>,<EOL>constants=code.co_consts, cells=code.co_cellvars,<EOL>varnames=code.co_varnames, freevars=code.co_freevars,<EOL>linestarts=dict(findlinestarts(code)),<EOL>end_offset=offset+<NUM_LIT:10>)<EOL>pass<EOL><DEDENT>pass<EOL><DEDENT>else:<EOL><INDENT>if mainfile:<EOL><INDENT>part1 = "<STR_LIT>" % mainfile<EOL>msg   = "<STR_LIT>"<EOL>self.msg(Mmisc.wrapped_lines(part1, msg,<EOL>self.settings['<STR_LIT:width>']))<EOL><DEDENT>else:<EOL><INDENT>self.msg('<STR_LIT>')<EOL>pass<EOL><DEDENT>self.msg(self.core.execution_status)<EOL>pass<EOL><DEDENT>return False<EOL>


def show_category(self, category, args):

n2cmd = self.proc.commands<EOL>names = list(n2cmd.keys())<EOL>if len(args) == <NUM_LIT:1> and args[<NUM_LIT:0>] == '<STR_LIT:*>':<EOL><INDENT>self.section("<STR_LIT>" % category)<EOL>cmds = [cmd for cmd in names if category == n2cmd[cmd].category]<EOL>cmds.sort()<EOL>self.msg_nocr(self.columnize_commands(cmds))<EOL>return<EOL><DEDENT>self.msg("<STR_LIT>" % categories[category])<EOL>self.section("<STR_LIT>")<EOL>names.sort()<EOL>for name in names:  <EOL><INDENT>if category != n2cmd[name].category: continue<EOL>self.msg("<STR_LIT>" % (name, n2cmd[name].short_help,))<EOL>pass<EOL><DEDENT>return<EOL>

Show short help for all commands in `category'.


threading_list = threading.enumerate()<EOL>mythread =  threading.currentThread()<EOL>for t in threading_list:<EOL><INDENT>if t != mythread:<EOL><INDENT>ctype_async_raise(t, Mexcept.DebuggerQuit)<EOL>pass<EOL><DEDENT>pass<EOL><DEDENT>raise Mexcept.DebuggerQuit<EOL>

quit command when several threads are involved.


def find_and_set_debugged_frame(self, frame, thread_id):

thread = threading._active[thread_id]<EOL>thread_name = thread.getName()<EOL>if (not self.settings['<STR_LIT>'] and<EOL>thread_name == Mthread.current_thread_name()):<EOL><INDENT>newframe = Mthread.find_debugged_frame(frame)<EOL>if newframe is not None:  frame = newframe<EOL>pass<EOL><DEDENT>self.stack, self.curindex = Mcmdproc.get_stack(frame, None,<EOL>self.proc)<EOL>self.proc.stack, self.proc.curindex = self.stack, self.curindex<EOL>self.proc.frame_thread_name = thread_name<EOL>return<EOL>

The dance we have to do to set debugger frame state to
        *frame*, which is in the thread with id *thread_id*. We may
        need to the hide initial debugger frames.


return(self.debugger.intf[-<NUM_LIT:1>].confirm(msg, default))<EOL>

Convenience short-hand for self.debugger.intf.confirm


Convenience short-hand for self.proc.rst_msg(text)


def interact(banner=None, readfunc=None, my_locals=None, my_globals=None):

console = code.InteractiveConsole(my_locals, filename='<STR_LIT>')<EOL>console.runcode = lambda code_obj: runcode(console, code_obj)<EOL>setattr(console, '<STR_LIT>', my_globals)<EOL>if readfunc is not None:<EOL><INDENT>console.raw_input = readfunc<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>import readline<EOL><DEDENT>except ImportError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>console.interact(banner)<EOL>pass<EOL>

Almost a copy of code.interact
    Closely emulate the interactive Python interpreter.

    This is a backwards compatible interface to the InteractiveConsole
    class.  When readfunc is not specified, it attempts to import the
    readline module to enable GNU readline if it is available.

    Arguments (all optional, all default to None):

    banner -- passed to InteractiveConsole.interact()
    readfunc -- if not None, replaces InteractiveConsole.raw_input()
    local -- passed to InteractiveInterpreter.__init__()


print('<STR_LIT>')<EOL>self.proc.cmd_queue.append(string)<EOL>self.proc.process_command()<EOL>return<EOL>

Invoke a debugger command from inside a python shell called inside
        the debugger.


debug = not sys.flags.optimize if debug_override is None else debug_override<EOL>if debug:<EOL><INDENT>suffixes = DEBUG_BYTECODE_SUFFIXES<EOL><DEDENT>else:<EOL><INDENT>suffixes = OPTIMIZED_BYTECODE_SUFFIXES<EOL>pass<EOL><DEDENT>head, tail = os.path.split(path)<EOL>base_filename, sep, _ = tail.partition('<STR_LIT:.>')<EOL>if not hasattr(sys, '<STR_LIT>'):<EOL><INDENT>raise NotImplementedError('<STR_LIT>')<EOL><DEDENT>tag = sys.implementation.cache_tag<EOL>if tag is None:<EOL><INDENT>raise NotImplementedError('<STR_LIT>')<EOL><DEDENT>filename = '<STR_LIT>'.join([base_filename, sep, tag, suffixes[<NUM_LIT:0>]])<EOL>return os.path.join(head, _PYCACHE, filename)<EOL>

Given the path to a .py file, return the path to its .pyc/.pyo file.

    The .py file does not need to exist; this simply returns the path to the
    .pyc/.pyo file calculated as if the .py file were imported.  The extension
    will be .pyc unless sys.flags.optimize is non-zero, then it will be .pyo.

    If debug_override is not None, then it must be a boolean and is used in
    place of sys.flags.optimize.

    If sys.implementation.cache_tag is None then NotImplementedError is raised.


def console_runcode(code_obj):<EOL><INDENT>runcode(console, code_obj)<EOL><DEDENT>console = code.InteractiveConsole(my_locals, filename='<STR_LIT>')<EOL>console.runcode = console_runcode<EOL>setattr(console, '<STR_LIT>', my_globals)<EOL>if readfunc is not None:<EOL><INDENT>console.raw_input = readfunc<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>import readline<EOL><DEDENT>except ImportError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>console.interact(banner)<EOL>pass<EOL>


def event_processor(self, frame, event, arg):

out = self.debugger.intf[-<NUM_LIT:1>].output<EOL>lineno = frame.f_lineno<EOL>filename = self.core.canonic_filename(frame)<EOL>filename = self.core.filename(filename)<EOL>if not out:<EOL><INDENT>print("<STR_LIT>" % (event, filename, lineno))<EOL><DEDENT>else:<EOL><INDENT>out.write("<STR_LIT>" % (event, filename, lineno))<EOL>if arg is not None:<EOL><INDENT>out.writeline('<STR_LIT>' % repr(arg))<EOL><DEDENT>else:<EOL><INDENT>out.writeline('<STR_LIT>')<EOL>pass<EOL><DEDENT>pass<EOL><DEDENT>return self.event_processor<EOL>

A simple event processor that prints out events.


def complete_token_filtered(aliases, prefix, expanded):

complete_ary = list(aliases.keys())<EOL>results = [cmd for cmd in<EOL>complete_ary if cmd.startswith(prefix)] and not (<EOL>cmd in aliases and expanded not in aliases[cmd])<EOL>return sorted(results, key=lambda pair: pair[<NUM_LIT:0>])<EOL>

Find all starting matches in dictionary *aliases* that start
    with *prefix*, but filter out any matches already in
    *expanded*.


if not cmd.proc.curframe: return [None]<EOL>ns = cmd.proc.curframe.f_globals.copy()<EOL>ns.update(cmd.proc.curframe.f_locals)<EOL>if '<STR_LIT:.>' in prefix:<EOL><INDENT>dotted = prefix.split('<STR_LIT:.>')<EOL>try:<EOL><INDENT>obj = ns[dotted[<NUM_LIT:0>]]<EOL>for part in dotted[<NUM_LIT:1>:-<NUM_LIT:1>]:<EOL><INDENT>obj = getattr(obj, part)<EOL><DEDENT><DEDENT>except (KeyError, AttributeError):<EOL><INDENT>return []<EOL><DEDENT>pre_prefix = '<STR_LIT:.>'.join(dotted[:-<NUM_LIT:1>]) + '<STR_LIT:.>'<EOL>return [pre_prefix + n for n in dir(obj) if<EOL>n.startswith(dotted[-<NUM_LIT:1>])]<EOL><DEDENT>else:<EOL><INDENT>return Mcomplete.complete_token(ns.keys(), prefix)<EOL><DEDENT>


for subcmd_name in list(self.subcmds.keys()):<EOL><INDENT>if subcmd_name.startswith(subcmd_prefix)and len(subcmd_prefix) >=self.subcmds[subcmd_name].__class__.min_abbrev:<EOL><INDENT>return self.subcmds[subcmd_name]<EOL><DEDENT>pass<EOL><DEDENT>return None<EOL>


subcmd_name = subcmd_cb.name<EOL>self.subcmds[subcmd_name] = subcmd_cb<EOL>self.cmdlist.append(subcmd_name)<EOL>

Add subcmd to the available subcommands for this object.
        It will have the supplied docstring, and subcmd_cb will be called
        when we want to run the command. min_len is the minimum length
        allowed to abbreviate the command. in_list indicates with the
        show command will be run when giving a list of all sub commands
        of this object. Some commands have long output like "show commands"
        so we might not want to show that.


print(args)<EOL>subcmd_prefix = args[<NUM_LIT:0>]<EOL>if not subcmd_prefix or len(subcmd_prefix) == <NUM_LIT:0>:<EOL><INDENT>self.msg(self.doc)<EOL>self.msg(


if self.curframe:<EOL><INDENT>g = self.curframe.f_globals<EOL>l = self.curframe.f_locals<EOL><DEDENT>else:<EOL><INDENT>g = globals()<EOL>l = locals()<EOL>pass<EOL><DEDENT>try:<EOL><INDENT>val = int(eval(arg, g, l))<EOL><DEDENT>except (SyntaxError, NameError, ValueError, TypeError):<EOL><INDENT>return None<EOL><DEDENT>return val<EOL>

Eval arg and it is an integer return the value. Otherwise
        return None


def print_source_location_info(print_fn, filename, lineno, fn_name=None,<EOL>f_lasti=None, remapped_file=None):

if remapped_file:<EOL><INDENT>mess = '<STR_LIT>' % (remapped_file, lineno, filename)<EOL><DEDENT>else:<EOL><INDENT>mess = '<STR_LIT>' % (filename, lineno)<EOL><DEDENT>if f_lasti and f_lasti != -<NUM_LIT:1>:<EOL><INDENT>mess += '<STR_LIT>' % f_lasti<EOL>pass<EOL><DEDENT>mess += '<STR_LIT>'<EOL>if fn_name and fn_name != '<STR_LIT:?>':<EOL><INDENT>mess += "<STR_LIT>" % fn_name<EOL>pass<EOL><DEDENT>print_fn(mess)<EOL>return<EOL>

Print out a source location , e.g. the first line in
    line in:
        (/tmp.py:2 @21):  <module>
        L -- 2 import sys,os
        (trepan3k)


def get_int(self, arg, min_value=<NUM_LIT:0>, default=<NUM_LIT:1>, cmdname=None,<EOL>at_most=None):

if arg is None: return default<EOL>default = self.get_int_noerr(arg)<EOL>if default is None:<EOL><INDENT>if cmdname:<EOL><INDENT>self.errmsg(("<STR_LIT>"<EOL>+ "<STR_LIT>") % (cmdname, str(arg)))<EOL><DEDENT>else:<EOL><INDENT>self.errmsg('<STR_LIT>'<EOL>% str(arg))<EOL>pass<EOL><DEDENT>return None<EOL>pass<EOL><DEDENT>if default < min_value:<EOL><INDENT>if cmdname:<EOL><INDENT>self.errmsg(("<STR_LIT>" +<EOL>'<STR_LIT>')<EOL>% (cmdname, min_value, default))<EOL><DEDENT>else:<EOL><INDENT>self.errmsg(("<STR_LIT>" +<EOL>'<STR_LIT>')<EOL>% (min_value, default))<EOL>pass<EOL><DEDENT>return None<EOL><DEDENT>elif at_most and default > at_most:<EOL><INDENT>if cmdname:<EOL><INDENT>self.errmsg(("<STR_LIT>" +<EOL>'<STR_LIT>')<EOL>% (cmdname, at_most, default))<EOL><DEDENT>else:<EOL><INDENT>self.errmsg(("<STR_LIT>")<EOL>% (at_most, default))<EOL>pass<EOL><DEDENT>pass<EOL><DEDENT>return default<EOL>

If no argument use the default. If arg is a an integer between
        least min_value and at_most, use that. Otherwise report an error.
        If there's a stack frame use that in evaluation.


def print_source_line(msg, lineno, line, event_str=None):

<EOL>return msg('<STR_LIT>' % (event_str, lineno, line))<EOL>

Print out a source line of text , e.g. the second
    line in:
        (/tmp.py:2):  <module>
        L -- 2 import sys,os
        (trepan3k)

    We define this method
    specifically so it can be customized for such applications
    like ipython.


histfile = self.debugger.intf[-<NUM_LIT:1>].histfile<EOL>try:<EOL><INDENT>import readline<EOL>readline.read_history_file(histfile)<EOL><DEDENT>except IOError:<EOL><INDENT>pass<EOL><DEDENT>except ImportError:<EOL><INDENT>pass<EOL><DEDENT>return<EOL>

Read the command history file -- possibly.


val = show_onoff(obj.debugger.settings[obj.name])<EOL>if not what: what = obj.name<EOL>return obj.msg("<STR_LIT>" % (what, val))<EOL>

Generic subcommand showing a boolean-valued debugger setting.
    'obj' is generally a subcommand that has 'name' and
    'debugger.setting' attributes.


if not isinstance(b, bool):<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT>if b:<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT>return "<STR_LIT>"<EOL>

Return 'on' for True and 'off' for False, and ?? for anything
    else.


range_start  ::= opt_space range
range ::= location
range ::= location opt_space COMMA opt_space NUMBER
range ::= location opt_space COMMA opt_space OFFSET
range ::= COMMA opt_space location
range ::= location opt_space COMMA
range ::= location
range ::= DIRECTION


arange_start  ::= opt_space arange
arange ::= range
arange ::= addr_location opt_space COMMA opt_space NUMBER
arange ::= addr_location opt_space COMMA opt_space OFFSET
arange ::= addr_location opt_space COMMA opt_space ADDRESS
arange ::= location opt_space COMMA opt_space ADDRESS
arange ::= addr_location opt_space COMMA
arange ::= addr_location

# Unlike ranges, We don't allow ending at an address
# arange ::= COMMA opt_space addr_location

addr_location ::= location
addr_location ::= ADDRESS


<EOL>base = s[<NUM_LIT:1>:-<NUM_LIT:1>]<EOL>self.add_token('<STR_LIT>', base)<EOL>self.pos += len(s)<EOL>


<EOL>self.add_token('<STR_LIT>', s)<EOL>self.pos += len(s)<EOL>


self.add_token('<STR_LIT>', s)<EOL>self.pos += len(s)<EOL>pass<EOL>


pos = self.pos<EOL>self.add_token('<STR_LIT>', s)<EOL>self.pos = pos + len(s)<EOL>


<EOL>print_events = []<EOL>if opts.fntrace:   print_events = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>if opts.linetrace: print_events += ['<STR_LIT>']<EOL>if len(print_events):<EOL><INDENT>dbg.settings['<STR_LIT>'] = frozenset(print_events)<EOL>pass<EOL><DEDENT>for setting in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>dbg.settings[setting] = getattr(opts, setting)<EOL>pass<EOL><DEDENT>if getattr(opts, '<STR_LIT>'):<EOL><INDENT>dbg.settings['<STR_LIT>'] = opts.highlight<EOL><DEDENT>else:<EOL><INDENT>dbg.settings['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>dbg.settings['<STR_LIT>'] = None<EOL>if not opts.private:<EOL><INDENT>Mdebugger.debugger_obj = dbg<EOL>pass<EOL><DEDENT>if opts.post_mortem:<EOL><INDENT>Mapi.debugger_on_post_mortem()<EOL>pass<EOL><DEDENT>return<EOL>

Handle options (`opts') that feed into the debugger (`dbg')


def process_options(debugger_name, pkg_version, sys_argv, option_list=None):

usage_str="""<STR_LIT>"""<EOL>optparser = OptionParser(usage=usage_str, option_list=option_list,<EOL>version="<STR_LIT>" % pkg_version)<EOL>optparser.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>",<EOL>action="<STR_LIT:store_true>", default=False,<EOL>help="<STR_LIT>"<EOL>"<STR_LIT>")<EOL>optparser.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>",<EOL>action="<STR_LIT:store_true>", default=False,<EOL>help="<STR_LIT>"<EOL>"<STR_LIT>")<EOL>optparser.add_option("<STR_LIT>", dest="<STR_LIT>",<EOL>action="<STR_LIT:store_true>", default=False,<EOL>help="<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL>optparser.add_option("<STR_LIT>", dest="<STR_LIT>",<EOL>action='<STR_LIT:store_true>',<EOL>help="<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL>optparser.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>",<EOL>action="<STR_LIT:store>", type='<STR_LIT:string>', metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>optparser.add_option("<STR_LIT>", dest="<STR_LIT>",<EOL>action="<STR_LIT:store>", type='<STR_LIT:string>', metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>optparser.add_option("<STR_LIT>", dest="<STR_LIT>",<EOL>action="<STR_LIT:store_true>", default=True,<EOL>help="<STR_LIT>")<EOL>optparser.add_option("<STR_LIT>", dest="<STR_LIT>",<EOL>action="<STR_LIT:store_true>", default=False,<EOL>help="<STR_LIT>")<EOL>optparser.add_option("<STR_LIT>", dest="<STR_LIT>",<EOL>action="<STR_LIT:store_true>", default=True,<EOL>help="<STR_LIT>"<EOL>"<STR_LIT>")<EOL>optparser.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", type="<STR_LIT:string>",<EOL>help="<STR_LIT>" +<EOL>"<STR_LIT>")<EOL>optparser.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT:host>", default='<STR_LIT:127.0.0.1>',<EOL>action="<STR_LIT:store>", type='<STR_LIT:string>', metavar='<STR_LIT>',<EOL>help="<STR_LIT>"<EOL>"<STR_LIT>")<EOL>optparser.add_option("<STR_LIT>", dest="<STR_LIT>",<EOL>action="<STR_LIT:store>", type='<STR_LIT:string>',<EOL>metavar='<STR_LIT>',<EOL>default='<STR_LIT>',<EOL>help="<STR_LIT>"<EOL>"<STR_LIT>")<EOL>optparser.add_option("<STR_LIT>", dest="<STR_LIT>",<EOL>action='<STR_LIT:store_true>', default=False,<EOL>help="<STR_LIT>")<EOL>optparser.add_option("<STR_LIT>", dest="<STR_LIT>",<EOL>action="<STR_LIT:store_true>", default=True,<EOL>help="<STR_LIT>"<EOL>)<EOL>optparser.add_option("<STR_LIT>", dest="<STR_LIT>",<EOL>action="<STR_LIT>", default=True,<EOL>help="<STR_LIT>"<EOL>)<EOL>optparser.add_option("<STR_LIT>", dest="<STR_LIT>",<EOL>action='<STR_LIT:store_true>', default=True,<EOL>help=("<STR_LIT>"<EOL>"<STR_LIT>"))<EOL>optparser.add_option("<STR_LIT>", dest="<STR_LIT>",<EOL>action='<STR_LIT>', default=True,<EOL>help=("<STR_LIT>"<EOL>"<STR_LIT>"))<EOL>optparser.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>",<EOL>action="<STR_LIT:store_true>", default=False,<EOL>help=("<STR_LIT>"<EOL>"<STR_LIT>"))<EOL>optparser.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", metavar='<STR_LIT>',<EOL>action="<STR_LIT:store>", type='<STR_LIT:string>',<EOL>help=("<STR_LIT>"<EOL>"<STR_LIT>"))<EOL>optparser.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT:port>", default=<NUM_LIT>,<EOL>action="<STR_LIT:store>", type='<STR_LIT:int>',<EOL>help="<STR_LIT>"<EOL>"<STR_LIT>")<EOL>optparser.add_option("<STR_LIT>", dest="<STR_LIT>",<EOL>action='<STR_LIT:store_true>',<EOL>help="<STR_LIT>")<EOL>optparser.add_option("<STR_LIT>", dest="<STR_LIT>",<EOL>action="<STR_LIT:store_true>", default=False,<EOL>help="<STR_LIT>")<EOL>optparser.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT:target>",<EOL>help=("<STR_LIT>"<EOL>"<STR_LIT>")),<EOL>optparser.add_option("<STR_LIT>", dest='<STR_LIT>', action='<STR_LIT:store_true>',<EOL>default=False, help="<STR_LIT>")<EOL>optparser.add_option("<STR_LIT>", default=<NUM_LIT:0>, type="<STR_LIT:int>",<EOL>help="<STR_LIT>")<EOL>optparser.disable_interspersed_args()<EOL>sys.argv = list(sys_argv)<EOL>(opts, sys.argv) = optparser.parse_args()<EOL>dbg_opts = {'<STR_LIT>': opts.from_ipython}<EOL>dbg_initfiles = []<EOL>if not opts.noexecute:<EOL><INDENT>add_startup_file(dbg_initfiles)<EOL><DEDENT>if opts.command:<EOL><INDENT>dbg_initfiles.append(opts.command)<EOL>pass<EOL><DEDENT>dbg_opts['<STR_LIT>'] = {'<STR_LIT>': dbg_initfiles}<EOL>if opts.cd:<EOL><INDENT>os.chdir(opts.cd)<EOL>pass<EOL><DEDENT>if opts.output:<EOL><INDENT>try:<EOL><INDENT>dbg_opts['<STR_LIT>'] = Moutput.DebuggerUserOutput(opts.output)<EOL><DEDENT>except IOError:<EOL><INDENT>_, xxx_todo_changeme, _ = sys.exc_info()<EOL>(errno, strerror) = xxx_todo_changeme.args<EOL>print("<STR_LIT>" % opts.output)<EOL>print("<STR_LIT>" % (errno, strerror))<EOL><DEDENT>except:<EOL><INDENT>print("<STR_LIT>" %<EOL>opts.output)<EOL>print(sys.exc_info()[<NUM_LIT:0>])<EOL>sys.exit(<NUM_LIT:2>)<EOL>pass<EOL><DEDENT>pass<EOL><DEDENT>return opts, dbg_opts, sys.argv<EOL>

Handle debugger options. Set `option_list' if you are writing
    another main program and want to extend the existing set of debugger
    options.

    The options dicionary from optparser is returned. sys_argv is
    also updated.


def process_options(pkg_version, sys_argv, option_list=None):

usage_str="""<STR_LIT>"""<EOL>optparser = OptionParser(usage=usage_str, option_list=option_list,<EOL>version="<STR_LIT>" % pkg_version)<EOL>optparser.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT:host>", default='<STR_LIT:127.0.0.1>',<EOL>action="<STR_LIT:store>", type='<STR_LIT:string>', metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>optparser.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT:port>", default=<NUM_LIT>,<EOL>action="<STR_LIT:store>", type='<STR_LIT:int>', metavar='<STR_LIT>',<EOL>help="<STR_LIT>"<EOL>"<STR_LIT>")<EOL>optparser.add_option("<STR_LIT>", dest="<STR_LIT>", default=<NUM_LIT:0>,<EOL>action="<STR_LIT:store>", type='<STR_LIT:int>', metavar='<STR_LIT>',<EOL>help="<STR_LIT>"<EOL>"<STR_LIT>")<EOL>optparser.disable_interspersed_args()<EOL>sys.argv = list(sys_argv)<EOL>(opts, sys.argv) = optparser.parse_args()<EOL>return opts, sys.argv<EOL>

Handle debugger options. Set `option_list' if you are writing
    another main program and want to extend the existing set of debugger
    options.

    The options dicionary from opt_parser is return. sys_argv is
    also updated.


global __title__<EOL>orig_sys_argv = list(sys_argv)<EOL>opts, dbg_opts, sys_argv  = process_options(__title__, __version__,<EOL>sys_argv)<EOL>dbg_opts['<STR_LIT>'] = sys_argv<EOL>dbg_opts['<STR_LIT>']     = Mbullwinkle.BWInterface()<EOL>dbg_opts['<STR_LIT>']     = '<STR_LIT>'<EOL>if dbg is None:<EOL><INDENT>dbg = Mdebugger.Trepan(dbg_opts)<EOL>dbg.core.add_ignore(main)<EOL>pass<EOL><DEDENT>_postprocess_options(dbg, opts)<EOL>if len(sys_argv) == <NUM_LIT:0>:<EOL><INDENT>mainpyfile = None<EOL><DEDENT>else:<EOL><INDENT>mainpyfile = sys_argv[<NUM_LIT:0>]  <EOL>if not os.path.isfile(mainpyfile):<EOL><INDENT>mainpyfile=Mclifns.whence_file(mainpyfile)<EOL>is_readable = Mfile.readable(mainpyfile)<EOL>if is_readable is None:<EOL><INDENT>print("<STR_LIT>"<EOL>% (__title__, mainpyfile,))<EOL>sys.exit(<NUM_LIT:1>)<EOL><DEDENT>elif not is_readable:<EOL><INDENT>print("<STR_LIT>"<EOL>% (__title__, mainpyfile,))<EOL>sys.exit(<NUM_LIT:1>)<EOL>return<EOL><DEDENT><DEDENT>mainpyfile_noopt = Mfile.file_pyc2py(mainpyfile)<EOL>if mainpyfile != mainpyfile_nooptand Mfile.readable(mainpyfile_noopt):<EOL><INDENT>print("<STR_LIT>"<EOL>% __title__)<EOL>print("<STR_LIT>" % (<EOL>__title__, mainpyfile_noopt,))<EOL>mainpyfile = mainpyfile_noopt<EOL>pass<EOL><DEDENT>sys.path[<NUM_LIT:0>] = dbg.main_dirname = os.path.dirname(mainpyfile)<EOL><DEDENT>dbg.sig_received = False<EOL>while True:<EOL><INDENT>try:<EOL><INDENT>if dbg.program_sys_argv and mainpyfile:<EOL><INDENT>normal_termination = dbg.run_script(mainpyfile)<EOL>if not normal_termination: break<EOL><DEDENT>else:<EOL><INDENT>dbg.core.execution_status = '<STR_LIT>'<EOL>dbg.core.processor.process_commands()<EOL>pass<EOL><DEDENT>dbg.core.execution_status = '<STR_LIT>'<EOL>dbg.intf[-<NUM_LIT:1>].msg("<STR_LIT>")<EOL>dbg.core.processor.process_commands()<EOL><DEDENT>except Mexcept.DebuggerQuit:<EOL><INDENT>break<EOL><DEDENT>except Mexcept.DebuggerRestart:<EOL><INDENT>dbg.core.execution_status = '<STR_LIT>'<EOL>if dbg.program_sys_argv:<EOL><INDENT>sys.argv = list(dbg.program_sys_argv)<EOL>part1 = ('<STR_LIT>' %<EOL>dbg.core.filename(mainpyfile))<EOL>args  = '<STR_LIT:U+0020>'.join(dbg.program_sys_argv[<NUM_LIT:1>:])<EOL>dbg.intf[-<NUM_LIT:1>].msg(Mmisc.wrapped_lines(part1, args,<EOL>dbg.settings['<STR_LIT:width>']))<EOL><DEDENT>else: break<EOL><DEDENT>except SystemExit:<EOL><INDENT>break<EOL><DEDENT>pass<EOL><DEDENT>sys.argv = orig_sys_argv<EOL>return<EOL>

Routine which gets run if we were invoked directly


def post_mortem(exc=None, frameno=<NUM_LIT:1>, dbg=None):

if dbg is None:<EOL><INDENT>if Mdebugger.debugger_obj is None:<EOL><INDENT>Mdebugger.debugger_obj = Mdebugger.Trepan()<EOL>pass<EOL><DEDENT>dbg = Mdebugger.debugger_obj<EOL>pass<EOL><DEDENT>re_bogus_file = re.compile("<STR_LIT>")<EOL>if exc[<NUM_LIT:0>] is None:<EOL><INDENT>exc = get_last_or_frame_exception()<EOL>if exc[<NUM_LIT:0>] is None:<EOL><INDENT>print("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>return<EOL><DEDENT>pass<EOL><DEDENT>exc_type, exc_value, exc_tb = exc<EOL>dbg.core.execution_status = ('<STR_LIT>'<EOL>% exc_type)<EOL>if exc_tb is not None:<EOL><INDENT>while exc_tb.tb_next is not None:<EOL><INDENT>filename = exc_tb.tb_frame.f_code.co_filename<EOL>if (dbg.mainpyfile and <NUM_LIT:0> == len(dbg.mainpyfile)<EOL>and not re_bogus_file.match(filename)):<EOL><INDENT>dbg.mainpyfile = filename<EOL>pass<EOL><DEDENT>exc_tb = exc_tb.tb_next<EOL>pass<EOL><DEDENT>dbg.core.processor.curframe = exc_tb.tb_frame<EOL>pass<EOL><DEDENT>if <NUM_LIT:0> == len(dbg.program_sys_argv):<EOL><INDENT>dbg.program_sys_argv = list(sys.argv[<NUM_LIT:1>:])<EOL>dbg.program_sys_argv[:<NUM_LIT:0>] = [dbg.mainpyfile]<EOL><DEDENT>try:<EOL><INDENT>f = exc_tb.tb_frame<EOL>if f and f.f_lineno != exc_tb.tb_lineno : f = f.f_back<EOL>dbg.core.processor.event_processor(f, '<STR_LIT>', exc, '<STR_LIT>')<EOL><DEDENT>except DebuggerRestart:<EOL><INDENT>while True:<EOL><INDENT>sys.argv = list(dbg._program_sys_argv)<EOL>dbg.msg("<STR_LIT>"<EOL>% (dbg.filename(dbg.mainpyfile),<EOL>"<STR_LIT:U+0020>".join(dbg._program_sys_argv[<NUM_LIT:1>:])))<EOL>try:<EOL><INDENT>dbg.run_script(dbg.mainpyfile)<EOL><DEDENT>except DebuggerRestart:<EOL><INDENT>pass<EOL><DEDENT>pass<EOL><DEDENT><DEDENT>except DebuggerQuit:<EOL><INDENT>pass<EOL><DEDENT>return<EOL>

Enter debugger read loop after your program has crashed.

    exc is a triple like you get back from sys.exc_info.  If no exc
    parameter, is supplied, the values from sys.last_type,
    sys.last_value, sys.last_traceback are used. And if these don't
    exist either we'll assume that sys.exc_info() contains what we
    want and frameno is the index location of where we want to start.

    'frameno' specifies how many frames to ignore in the traceback.
    The default is 1, that is, we don't need to show the immediate
    call into post_mortem. If you have wrapper functions that call
    this one, you may want to increase frameno.


post_mortem(get_last_or_frame_exception(), frameno, dbg=dbg)<EOL>return<EOL>

Set up post-mortem debugging using the last traceback.  But if
    there is no traceback, we'll assume that sys.exc_info() contains
    what we want and frameno is the index location of where we want
    to start.

    'dbg', is an optional trepan.Trepan object.


return self.msg(msg)<EOL>

Common routine for reporting debugger error messages.


self.input.close()<EOL>self.output.close()<EOL>return<EOL>


def errmsg(self, msg, prefix="<STR_LIT>"):

<EOL>if not self.verbose:<EOL><INDENT>location = ("<STR_LIT>"<EOL>% (self.script_name, self.input_lineno))<EOL>msg = "<STR_LIT>" %(prefix, location, prefix, msg)<EOL><DEDENT>else:<EOL><INDENT>msg = "<STR_LIT>" %(prefix, msg)<EOL>pass<EOL><DEDENT>self.msg(msg)<EOL>if self.abort_on_error:<EOL><INDENT>raise EOFError<EOL><DEDENT>return<EOL>


while True:<EOL><INDENT>try:<EOL><INDENT>self.write_confirm(prompt, default)<EOL>reply = self.readline('<STR_LIT>').strip().lower()<EOL><DEDENT>except EOFError:<EOL><INDENT>return default<EOL><DEDENT>if reply in ('<STR_LIT:y>', '<STR_LIT:yes>'):<EOL><INDENT>return True<EOL><DEDENT>elif reply in ('<STR_LIT:n>', '<STR_LIT>'):<EOL><INDENT>return False<EOL><DEDENT>else:<EOL><INDENT>self.msg("<STR_LIT>")<EOL>pass<EOL><DEDENT>pass<EOL><DEDENT>return default<EOL>

Called when a dangerous action is about to be done to make sure
        it's okay. `prompt' is printed; user response is returned.


return self.inout.state<EOL>


def errmsg(self, str, prefix="<STR_LIT>"):

return self.msg("<STR_LIT>" %(prefix, str))<EOL>


self.inout.writeline(Mcomcodes.PRINT + msg)<EOL>return<EOL>

used to write to a debugger that is connected to this
        server; `str' written will have a newline added to it


return self.msg("<STR_LIT>" %(prefix, msg))<EOL>


try:<EOL><INDENT>self.input.close()<EOL>self.output.close()<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT>return<EOL>


try:<EOL><INDENT>midpoint = int(environ.get('<STR_LIT>', None))<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT>if not midpoint:<EOL><INDENT>term = environ.get('<STR_LIT>', None)<EOL>print("<STR_LIT>", midpoint, '<STR_LIT>',  (<NUM_LIT:16>*<NUM_LIT:5> + <NUM_LIT:16>*g + <NUM_LIT:16>*b))<EOL>midpoint = <NUM_LIT> if term and term == '<STR_LIT>' else <NUM_LIT><EOL><DEDENT>if ( (<NUM_LIT:16>*<NUM_LIT:5> + <NUM_LIT:16>*g + <NUM_LIT:16>*b) < midpoint ):<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT>

Pass as parameters R G B values in hex
    On return, variable is_dark_bg is set


term = environ.get('<STR_LIT>', None)<EOL>if term:<EOL><INDENT>if (term.startswith('<STR_LIT>',) or term.startswith('<STR_LIT>')<EOL>or term == '<STR_LIT>'):<EOL><INDENT>return False<EOL><DEDENT><DEDENT>return True<EOL>

Get bacground from
    default values based on the TERM environment variable


what = '<STR_LIT>'<EOL>if show_enabled:<EOL><INDENT>if self.enabled:<EOL><INDENT>what += '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>what += '<STR_LIT>'<EOL>pass<EOL><DEDENT>pass<EOL><DEDENT>if self.fmt:<EOL><INDENT>what += self.fmt + '<STR_LIT:U+0020>'<EOL>pass<EOL><DEDENT>what += self.arg<EOL>return '<STR_LIT>' % (self.number, what)<EOL>


found = False<EOL>s = []<EOL>for display in self.list:<EOL><INDENT>if not found:<EOL><INDENT>s.append("""<STR_LIT>""")<EOL>found = True<EOL>pass<EOL><DEDENT>s.append(display.format())<EOL><DEDENT>return s<EOL>

List all display items; return 0 if none


if not frame: return None<EOL>code = frame.f_code<EOL>return (code.co_name, code.co_filename, code.co_firstlineno)<EOL>

return suitable frame signature to key display expressions off of.


self.list = []<EOL>return<EOL>


return True if filename[-<NUM_LIT:4>:].lower() in ('<STR_LIT>', '<STR_LIT>') else False<EOL>

Given a file name, return True if the suffix is pyo or pyc (an
optimized bytecode file).


if sys.modules.get(name):<EOL><INDENT>return (sys.modules[name], sys.modules[name].__file__)<EOL><DEDENT>if os.path.isabs(name) and readable(name):<EOL><INDENT>return (None, name)<EOL><DEDENT>f = os.path.join(sys.path[<NUM_LIT:0>], name)<EOL>if readable(f):<EOL><INDENT>return (None, f)<EOL><DEDENT>root, ext = os.path.splitext(name)<EOL>if ext == '<STR_LIT>':<EOL><INDENT>name = name + '<STR_LIT>'<EOL>pass<EOL><DEDENT>if os.path.isabs(name):<EOL><INDENT>return (None, name)<EOL><DEDENT>for dirname in sys.path:<EOL><INDENT>while os.path.islink(dirname):<EOL><INDENT>dirname = os.readlink(dirname)<EOL>pass<EOL><DEDENT>fullname = os.path.join(dirname, name)<EOL>if readable(fullname):<EOL><INDENT>return (None, fullname)<EOL><DEDENT>pass<EOL><DEDENT>return (None, None)<EOL>

lookupmodule()->(module, file) translates a possibly incomplete
    file or module name into an absolute file name. None can be
    returned for either of the values positions of module or file when
    no or module or file is found.


if not b.funcname:<EOL><INDENT>if b.line != frame.f_lineno:<EOL><INDENT>return False<EOL><DEDENT>return True<EOL><DEDENT>if frame.f_code.co_name != b.funcname:<EOL><INDENT>return False<EOL><DEDENT>if not b.func_first_executable_line:<EOL><INDENT>b.func_first_executable_line = frame.f_lineno<EOL><DEDENT>if b.func_first_executable_line != frame.f_lineno:<EOL><INDENT>return False<EOL><DEDENT>return True<EOL>

Check whether we should break here because of `b.funcname`.


return self.canonic(frame.f_code.co_filename)<EOL>

Picks out the file name from `frame' and returns its
         canonic() value, a string.


<EOL>try:<EOL><INDENT>self.trace_hook_suspend = True<EOL>get_option = lambda key: Mmisc.option_set(opts, key,<EOL>default.START_OPTS)<EOL>add_hook_opts = get_option('<STR_LIT>')<EOL>if not tracer.is_started() or get_option('<STR_LIT>'):<EOL><INDENT>tracer_start_opts = default.START_OPTS.copy()<EOL>if opts:<EOL><INDENT>tracer_start_opts.update(opts.get('<STR_LIT>', {}))<EOL><DEDENT>tracer_start_opts['<STR_LIT>'] = self.trace_dispatch<EOL>tracer_start_opts['<STR_LIT>'] = add_hook_opts<EOL>tracer.start(tracer_start_opts)<EOL><DEDENT>elif not tracer.find_hook(self.trace_dispatch):<EOL><INDENT>tracer.add_hook(self.trace_dispatch, add_hook_opts)<EOL>pass<EOL><DEDENT>self.execution_status = '<STR_LIT>'<EOL><DEDENT>finally:<EOL><INDENT>self.trace_hook_suspend = False<EOL><DEDENT>return<EOL>

We've already created a debugger object, but here we start
        debugging in earnest. We can also turn off debugging (but have
        the hooks suspended or not) using 'stop'.

        'opts' is a hash of every known value you might want to set when
        starting the debugger. See START_OPTS of module default.


if filename is None:<EOL><INDENT>if self.debugger.mainpyfile:<EOL><INDENT>filename = self.debugger.mainpyfile<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT><DEDENT>if self.debugger.settings['<STR_LIT>']:<EOL><INDENT>return(os.path.basename(filename))<EOL><DEDENT>return filename<EOL>

Return filename or the basename of that depending on the
        basename setting


def is_stop_here(self, frame, event, arg):

<EOL>lineno = frame.f_lineno<EOL>filename = frame.f_code.co_filename<EOL>if self.different_line and event == '<STR_LIT>':<EOL><INDENT>if self.last_lineno == lineno and self.last_filename == filename:<EOL><INDENT>return False<EOL><DEDENT>pass<EOL><DEDENT>self.last_lineno   = lineno<EOL>self.last_filename = filename<EOL>if self.stop_level is not None:<EOL><INDENT>if frame != self.last_frame:<EOL><INDENT>self.last_level = Mstack.count_frames(frame)<EOL>self.last_frame = frame<EOL>pass<EOL><DEDENT>if self.last_level > self.stop_level:<EOL><INDENT>return False<EOL><DEDENT>elif self.last_level == self.stop_level andself.stop_on_finish and event in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>self.stop_level = None<EOL>self.stop_reason = "<STR_LIT>"<EOL>return True<EOL><DEDENT>pass<EOL><DEDENT>if self._is_step_next_stop(event):<EOL><INDENT>self.stop_reason = '<STR_LIT>'<EOL>return True<EOL><DEDENT>return False<EOL>

Does the magic to determine if we stop here and run a
        command processor or not. If so, return True and set
        self.stop_reason; if not, return False.

        Determining factors can be whether a breakpoint was
        encountered, whether we are stepping, next'ing, finish'ing,
        and, if so, whether there is an ignore counter.


if self.print_method:<EOL><INDENT>self.print_method('<STR_LIT>'<EOL>% self.signame)<EOL><DEDENT>if self.print_stack:<EOL><INDENT>import traceback<EOL>strings = traceback.format_stack(frame)<EOL>for s in strings:<EOL><INDENT>if s[-<NUM_LIT:1>] == '<STR_LIT:\n>': s = s[<NUM_LIT:0>:-<NUM_LIT:1>]<EOL>self.print_method(s)<EOL>pass<EOL><DEDENT>pass<EOL><DEDENT>if self.b_stop:<EOL><INDENT>core = self.dbgr.core<EOL>old_trace_hook_suspend = core.trace_hook_suspend<EOL>core.trace_hook_suspend = True<EOL>core.stop_reason = ('<STR_LIT>' %<EOL>(self.signame, signum))<EOL>core.processor.event_processor(frame, '<STR_LIT>', signum)<EOL>core.trace_hook_suspend = old_trace_hook_suspend<EOL>pass<EOL><DEDENT>if self.pass_along:<EOL><INDENT>if self.old_handler:<EOL><INDENT>self.old_handler(signum, frame)<EOL>pass<EOL><DEDENT>pass<EOL><DEDENT>return<EOL>

This method is called when a signal is received.


uname = name.upper()<EOL>if (uname.startswith('<STR_LIT>') and hasattr(signal, uname)):<EOL><INDENT>return getattr(signal, uname)<EOL><DEDENT>else:<EOL><INDENT>uname = "<STR_LIT>"+uname<EOL>if hasattr(signal, uname):<EOL><INDENT>return getattr(signal, uname)<EOL><DEDENT>return None<EOL><DEDENT>return<EOL>

Find the corresponding signal number for 'name'. Return None
    if 'name' is invalid.


def handle_ignore(self, signame, set_ignore):

self.handle_pass(signame, not set_ignore)<EOL>return set_ignore<EOL>

pass' and 'noignore' are synonyms. 'nopass and 'ignore' are
        synonyms.


def check_and_adjust_sighandler(self, signame, sigs):

signum = lookup_signum(signame)<EOL>try:<EOL><INDENT>old_handler = signal.getsignal(signum)<EOL><DEDENT>except ValueError:<EOL><INDENT>if signame in self.sigs:<EOL><INDENT>sigs.pop(signame)<EOL>pass<EOL><DEDENT>return None<EOL><DEDENT>if old_handler != self.sigs[signame].handle:<EOL><INDENT>if old_handler not in [signal.SIG_IGN, signal.SIG_DFL]:<EOL><INDENT>sigs[signame].old_handler = old_handler<EOL>pass<EOL><DEDENT>try:<EOL><INDENT>self._orig_set_signal(signum, self.sigs[signame].handle)<EOL><DEDENT>except ValueError:<EOL><INDENT>return False<EOL><DEDENT>except KeyError:<EOL><INDENT>return False<EOL><DEDENT>pass<EOL><DEDENT>return True<EOL>

Check to see if a single signal handler that we are interested
in has changed or has not been set initially. On return
self.sigs[signame] should have our signal handler. True is
returned if the same or adjusted, False or None if error or
not found.


def pprint_simple_array(val, displaywidth, msg_nocr, msg, lineprefix='<STR_LIT>'):

if type(val) != list:<EOL><INDENT>return False<EOL><DEDENT>numeric = True<EOL>for i in range(len(val)):<EOL><INDENT>if not (type(val[i]) in [bool, float, int]):<EOL><INDENT>numeric = False<EOL>if not (type(val[i]) in [bool, float, int, bytes]):<EOL><INDENT>return False<EOL><DEDENT>pass<EOL><DEDENT>pass<EOL><DEDENT>mess = columnize([repr(v) for v in val],<EOL>opts={"<STR_LIT>": True,<EOL>"<STR_LIT>": lineprefix,<EOL>"<STR_LIT>": int(displaywidth)-<NUM_LIT:3>,<EOL>'<STR_LIT>': not numeric})<EOL>msg_nocr(mess)<EOL>return True<EOL>

Try to pretty print a simple case where a list is not nested.
    Return True if we can do it and False if not.


def dis(msg, msg_nocr, section, errmsg, x=None, start_line=-<NUM_LIT:1>, end_line=None,<EOL>relative_pos = False, highlight='<STR_LIT>', start_offset=<NUM_LIT:0>, end_offset=None,<EOL>include_header=False):

lasti = -<NUM_LIT:1><EOL>if x is None:<EOL><INDENT>distb()<EOL>return None, None<EOL><DEDENT>if start_offset is None:<EOL><INDENT>start_offset = <NUM_LIT:0><EOL><DEDENT>mess = '<STR_LIT>'<EOL>if start_line > <NUM_LIT:1>:<EOL><INDENT>mess += "<STR_LIT>" % start_line<EOL><DEDENT>elif start_offset > <NUM_LIT:1>:<EOL><INDENT>mess = "<STR_LIT>" % start_offset<EOL><DEDENT>if end_line:<EOL><INDENT>mess += "<STR_LIT>" % end_line<EOL><DEDENT>elif end_offset:<EOL><INDENT>mess += "<STR_LIT>" % end_offset<EOL><DEDENT>sectioned = False<EOL>if hasattr(types, '<STR_LIT>') and isinstance(x, types.InstanceType):<EOL><INDENT>x = x.__class__<EOL><DEDENT>if inspect.ismethod(x):<EOL><INDENT>section("<STR_LIT>" % (x, mess))<EOL>sectioned = True<EOL>x = x.im_func<EOL><DEDENT>elif inspect.isfunction(x) or inspect.isgeneratorfunction(x):<EOL><INDENT>section("<STR_LIT>" % (x, mess))<EOL>x = x.func_code<EOL>sectioned = True<EOL><DEDENT>elif inspect.isgenerator(x):<EOL><INDENT>section("<STR_LIT>" % (x, mess))<EOL>frame = x.gi_frame<EOL>lasti = frame.f_last_i<EOL>x = x.gi_code<EOL>sectioned = True<EOL><DEDENT>elif inspect.isframe(x):<EOL><INDENT>section("<STR_LIT>" % (x, mess))<EOL>sectioned = True<EOL>if hasattr(x, '<STR_LIT>'):<EOL><INDENT>lasti = x.f_lasti<EOL>if lasti == -<NUM_LIT:1>: lasti = <NUM_LIT:0><EOL>pass<EOL><DEDENT>opc = get_opcode(PYTHON_VERSION, IS_PYPY)<EOL>x = x.f_code<EOL>if include_header:<EOL><INDENT>header_lines = Bytecode(x, opc).info().split("<STR_LIT:\n>")<EOL>header = '<STR_LIT:\n>'.join([format_token(Mformat.Comment, h) for h in header_lines])<EOL>msg(header)<EOL><DEDENT>pass<EOL><DEDENT>elif inspect.iscode(x):<EOL><INDENT>pass<EOL><DEDENT>if hasattr(x, '<STR_LIT>'):  <EOL><INDENT>items = sorted(x.__dict__.items())<EOL>for name, x1 in items:<EOL><INDENT>if isinstance(x1, _have_code):<EOL><INDENT>if not sectioned:<EOL><INDENT>section("<STR_LIT>" % x)<EOL><DEDENT>try:<EOL><INDENT>dis(msg, msg_nocr, section, errmsg, x1,<EOL>start_line=start_line, end_line=end_line,<EOL>relative_pos = relative_pos)<EOL>msg("<STR_LIT>")<EOL><DEDENT>except TypeError:<EOL><INDENT>_, msg, _ = sys.exc_info()<EOL>errmsg("<STR_LIT>", msg)<EOL>pass<EOL><DEDENT>pass<EOL><DEDENT>pass<EOL><DEDENT>pass<EOL><DEDENT>elif hasattr(x, '<STR_LIT>'):  <EOL><INDENT>if not sectioned:<EOL><INDENT>section("<STR_LIT>" % x)<EOL><DEDENT>return disassemble(msg, msg_nocr, section, x, lasti=lasti,<EOL>start_line=start_line, end_line=end_line,<EOL>relative_pos = relative_pos,<EOL>highlight = highlight,<EOL>start_offset = start_offset,<EOL>end_offset = end_offset)<EOL><DEDENT>elif isinstance(x, str):    <EOL><INDENT>return disassemble_string(msg, msg_nocr, x,)<EOL><DEDENT>else:<EOL><INDENT>errmsg("<STR_LIT>" %<EOL>type(x).__name__)<EOL><DEDENT>return None, None<EOL>

Disassemble classes, methods, functions, or code.

    With no argument, disassemble the last traceback.


return hasattr(frame, '<STR_LIT>') and get_call_function_name(frame) == '<STR_LIT>'<EOL>

Return True if we are looking at an exec statement


def print_stack_trace(proc_obj, count=None, color='<STR_LIT>', opts={}):

if count is None:<EOL><INDENT>n=len(proc_obj.stack)<EOL><DEDENT>else:<EOL><INDENT>n=min(len(proc_obj.stack), count)<EOL><DEDENT>try:<EOL><INDENT>for i in range(n):<EOL><INDENT>print_stack_entry(proc_obj, i, color=color, opts=opts)<EOL><DEDENT><DEDENT>except KeyboardInterrupt:<EOL><INDENT>pass<EOL><DEDENT>return<EOL>


n = len(code)<EOL>while offset < n:<EOL><INDENT>op = code[offset]<EOL>offset += <NUM_LIT:1><EOL>if op >= HAVE_ARGUMENT:<EOL><INDENT>offset += <NUM_LIT:2><EOL>pass<EOL><DEDENT>yield op, offset<EOL>pass<EOL><DEDENT>yield -<NUM_LIT:100>, -<NUM_LIT:1000><EOL>pass<EOL>

Return the next opcode and offset as a tuple. Tuple (-100,
    -1000) is returned when reaching the end.


return (line and _re_class.match(line)<EOL>and stmt_contains_opcode(frame.f_code, frame.f_lineno,<EOL>'<STR_LIT>'))<EOL>

Return True if we are looking at a class definition statement


self.output.write(msg)<EOL>return<EOL>

used to write to a debugger that is connected to this
        server; `str' written will not have a newline added to it


if hasattr(self.output, '<STR_LIT>'):<EOL><INDENT>self.output.writeline(msg)<EOL><DEDENT>elif hasattr(self.output, '<STR_LIT>'):<EOL><INDENT>self.output.writelines(msg + "<STR_LIT:\n>")<EOL>pass<EOL><DEDENT>return<EOL>

used to write to a debugger that is connected to this
        server; `str' written will have a newline added to it


self.commands = {}<EOL>for cmd_instance in self.cmd_instances:<EOL><INDENT>cmd_name = cmd_instance.name<EOL>self.commands[cmd_name] = cmd_instance<EOL>pass<EOL><DEDENT>return<EOL>


self.forget()<EOL>if self.settings('<STR_LIT>'):<EOL><INDENT>self.frame = inspect.currentframe()<EOL>pass<EOL><DEDENT>if self.event in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>exc_type, exc_value, exc_traceback = self.event_arg<EOL><DEDENT>else:<EOL><INDENT>_, _, exc_traceback = (None, None, None,)  <EOL>pass<EOL><DEDENT>if self.frame or exc_traceback:<EOL><INDENT>self.stack, self.curindex =get_stack(self.frame, exc_traceback, None, self)<EOL>self.curframe = self.stack[self.curindex][<NUM_LIT:0>]<EOL>self.thread_name = Mthread.current_thread_name()<EOL><DEDENT>else:<EOL><INDENT>self.stack = self.curframe =self.botframe = None<EOL>pass<EOL><DEDENT>if self.curframe:<EOL><INDENT>self.list_lineno =max(<NUM_LIT:1>, inspect.getlineno(self.curframe))<EOL><DEDENT>else:<EOL><INDENT>self.list_lineno = None<EOL>pass<EOL><DEDENT>return False<EOL>

Initialization done before entering the debugger-command
        loop. In particular we set up the call stack used for local
        variable lookup and frame/up/down commands.

        We return True if we should NOT enter the debugger-command
        loop.


raise NotImplementedError(NotImplementedMessage)<EOL>

The method that implements the debugger command.
        Help on the command comes from the docstring of this method.


self.debugger.core.stop()<EOL>self.debugger.core.execution_status = '<STR_LIT>'<EOL>self.proc.response['<STR_LIT>'] = '<STR_LIT>'<EOL>self.proc.response['<STR_LIT:name>']  = '<STR_LIT:status>'<EOL>self.proc.intf[-<NUM_LIT:1>].msg(self.proc.response)<EOL>raise Mexcept.DebuggerQuit<EOL>

quit command when there's just one thread.


def run_call(func, debug_opts=None, start_opts=None, *args, **kwds):

dbg = Mdebugger.Trepan(opts=debug_opts)<EOL>try:<EOL><INDENT>return dbg.run_call(func, start_opts, *args, **kwds)<EOL><DEDENT>except:<EOL><INDENT>Mpost_mortem.uncaught_exception(dbg)<EOL>pass<EOL><DEDENT>return<EOL>

Call the function (a function or method object, not a string)
    with the given arguments starting with the statement subsequent to
    the place that this appears in your program.

    When run_call() returns, it returns whatever the function call
    returned.  The debugger prompt appears as soon as the function is
    entered.


basename = osp.basename(filename)<EOL>if '<STR_LIT:.>' in basename:<EOL><INDENT>pos = basename.rfind('<STR_LIT:.>')<EOL>return basename[:pos]<EOL><DEDENT>else:<EOL><INDENT>return basename<EOL><DEDENT>return None<EOL>

Given a file name, extract the most likely module name.


if py_script.find(os.sep) != -<NUM_LIT:1>:<EOL><INDENT>return py_script<EOL><DEDENT>if dirnames is None:<EOL><INDENT>dirnames = os.environ['<STR_LIT>'].split(os.pathsep)<EOL><DEDENT>for dirname in dirnames:<EOL><INDENT>py_script_try = osp.join(dirname, py_script)<EOL>if osp.exists(py_script_try):<EOL><INDENT>return py_script_try<EOL><DEDENT><DEDENT>return py_script<EOL>

Do a shell-like path lookup for py_script and return the results.
    If we can't find anything return py_script


return(self.intf[-<NUM_LIT:1>].msg(msg))<EOL>

Convenience short-hand for self.debugger.intf[-1].msg


def grouper(iterable, n, fillvalue=None):

return list(zip_longest(*[iter(iterable)] * n, fillvalue=fillvalue))<EOL>

Group iterable by n elements.

    >>> for t in grouper('abcdefg', 3, fillvalue='x'):
    ...     print(''.join(t))
    abc
    def
    gxx


rates = self._rates[currency]<EOL>tmp = defaultdict(lambda: [None, None])<EOL>for date in sorted(rates):<EOL><INDENT>rate = rates[date]<EOL>if rate is not None:<EOL><INDENT>closest_rate = rate<EOL>dist = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>dist += <NUM_LIT:1><EOL>tmp[date][<NUM_LIT:0>] = closest_rate, dist<EOL><DEDENT><DEDENT>for date in sorted(rates, reverse=True):<EOL><INDENT>rate = rates[date]<EOL>if rate is not None:<EOL><INDENT>closest_rate = rate<EOL>dist = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>dist += <NUM_LIT:1><EOL>tmp[date][<NUM_LIT:1>] = closest_rate, dist<EOL><DEDENT><DEDENT>for date in sorted(tmp):<EOL><INDENT>(r0, d0), (r1, d1) = tmp[date]<EOL>rates[date] = (r0 * d1 + r1 * d0) / (d0 + d1)<EOL>if self.verbose:<EOL><INDENT>print(('<STR_LIT>'<EOL>'<STR_LIT>').format(currency, date, r0, d0, r1, d1))<EOL><DEDENT><DEDENT>

Fill missing rates of a currency.

        This is done by linear interpolation of the two closest available rates.

        :param str currency: The currency to fill missing rates for.


if currency == self.ref_currency:<EOL><INDENT>return <NUM_LIT:1.0><EOL><DEDENT>if date not in self._rates[currency]:<EOL><INDENT>first_date, last_date = self.bounds[currency]<EOL>if not self.fallback_on_wrong_date:<EOL><INDENT>raise RateNotFoundError('<STR_LIT>'.format(<EOL>date, currency, first_date, last_date))<EOL><DEDENT>if date < first_date:<EOL><INDENT>fallback_date = first_date<EOL><DEDENT>elif date > last_date:<EOL><INDENT>fallback_date = last_date<EOL><DEDENT>else:<EOL><INDENT>raise AssertionError('<STR_LIT>')<EOL><DEDENT>if self.verbose:<EOL><INDENT>print(r'<STR_LIT>'.format(<EOL>date, currency, first_date, last_date, fallback_date))<EOL><DEDENT>date = fallback_date<EOL><DEDENT>rate = self._rates[currency][date]<EOL>if rate is None:<EOL><INDENT>raise RateNotFoundError('<STR_LIT>'.format(currency, date))<EOL><DEDENT>return rate<EOL>

Get a rate for a given currency and date.

        :type date: datetime.date

        >>> from datetime import date
        >>> c = CurrencyConverter()
        >>> c._get_rate('USD', date=date(2014, 3, 28))
        1.375...
        >>> c._get_rate('BGN', date=date(2010, 11, 21))
        Traceback (most recent call last):
        RateNotFoundError: BGN has no rate for 2010-11-21


rates = self._rates[currency]<EOL>first_date, last_date = self.bounds[currency]<EOL>for date in list_dates_between(first_date, last_date):<EOL><INDENT>if date not in rates:<EOL><INDENT>rates[date] = None<EOL><DEDENT><DEDENT>if self.verbose:<EOL><INDENT>missing = len([r for r in itervalues(rates) if r is None])<EOL>if missing:<EOL><INDENT>print('<STR_LIT>'.format(<EOL>currency, missing, first_date, last_date,<EOL><NUM_LIT:1> + (last_date - first_date).days))<EOL><DEDENT><DEDENT>

Fill missing rates of a currency with the closest available ones.


args = value._proxy____args<EOL>kw = value._proxy____kw<EOL>if not kw and len(args) == <NUM_LIT:1> and isinstance(args[<NUM_LIT:0>], six.string_types):<EOL><INDENT>return LiteralStr(u'<STR_LIT>'.format(repr(value._proxy____args[<NUM_LIT:0>])))<EOL><DEDENT>return value<EOL>

Expand a _("TEST") call to something meaningful.


attrs = iter(object.__dict__.items())<EOL>if object.__class__:<EOL><INDENT>attrs = chain(attrs, iter(object.__class__.__dict__.items()))<EOL><DEDENT>is_model = isinstance(object, Model)<EOL>is_form = isinstance(object, BaseForm)<EOL>attrs = dict(<EOL>(k, v)<EOL>for k, v in attrs<EOL>if not k.startswith('<STR_LIT:_>')<EOL>and not getattr(v, '<STR_LIT>', False)<EOL>and not (is_model and k in ('<STR_LIT>', '<STR_LIT>'))<EOL>and not (is_form and k in ('<STR_LIT:Meta>',))<EOL>)<EOL>for member in dir(object):<EOL><INDENT>try:<EOL><INDENT>if member.startswith('<STR_LIT:_>') or not hasattr(object, member):<EOL><INDENT>continue<EOL><DEDENT><DEDENT>except HANDLED_EXCEPTIONS as e:<EOL><INDENT>attrs[member] = _format_exception(e)<EOL>continue<EOL><DEDENT>value = getattr(object, member)<EOL>if callable(value) or member in attrs or getattr(value, '<STR_LIT>', False):<EOL><INDENT>continue<EOL><DEDENT>attrs[member] = value<EOL><DEDENT>for name, value in list(attrs.items()):  <EOL><INDENT>if isinstance(value, property):<EOL><INDENT>attrs[name] = _try_call(lambda: getattr(object, name))<EOL><DEDENT>elif isinstance(value, types.FunctionType):<EOL><INDENT>if PY3:<EOL><INDENT>spec = inspect.getfullargspec(value)<EOL><DEDENT>else:<EOL><INDENT>spec = inspect.getargspec(value)<EOL><DEDENT>if len(spec.args) == <NUM_LIT:1> or len(spec.args) == len(spec.defaults or ()) + <NUM_LIT:1>:<EOL><INDENT>if _is_unsafe_name(name):<EOL><INDENT>attrs[name] = LiteralStr('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>attrs[name] = _try_call(lambda: value(object))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>del attrs[name]<EOL><DEDENT><DEDENT>elif hasattr(value, '<STR_LIT>'):<EOL><INDENT>attrs[name] = value = _try_call(lambda: getattr(object, name), return_exceptions=True)<EOL>if isinstance(value, Manager):<EOL><INDENT>attrs[name] = LiteralStr('<STR_LIT>'.format(value.__class__.__name__))<EOL><DEDENT>elif isinstance(value, AttributeError):<EOL><INDENT>del attrs[name]  <EOL><DEDENT>elif isinstance(value, HANDLED_EXCEPTIONS):<EOL><INDENT>attrs[name] = _format_exception(value)<EOL><DEDENT><DEDENT><DEDENT>if getattr(object, '<STR_LIT>', None) is not object.__str__:<EOL><INDENT>attrs['<STR_LIT>'] = _try_call(lambda: smart_str(object))<EOL><DEDENT>elif getattr(object, '<STR_LIT>', None) is not object.__unicode__:<EOL><INDENT>attrs['<STR_LIT>'] = _try_call(lambda: smart_str(object))<EOL><DEDENT>if hasattr(object, '<STR_LIT>'):<EOL><INDENT>attrs['<STR_LIT>'] = LiteralStr('<STR_LIT>')<EOL><DEDENT>if hasattr(object, '<STR_LIT>'):<EOL><INDENT>attrs['<STR_LIT>'] = LiteralStr('<STR_LIT>')<EOL><DEDENT>if hasattr(object, '<STR_LIT>'):<EOL><INDENT>attrs['<STR_LIT>'] = LiteralStr('<STR_LIT>')<EOL><DEDENT>if hasattr(object, '<STR_LIT>'):<EOL><INDENT>attrs['<STR_LIT>'] = len(object)<EOL><DEDENT>if isinstance(object, BaseForm):<EOL><INDENT>for field_name in list(object.fields.keys()):<EOL><INDENT>attrs[field_name] = object[field_name]<EOL><DEDENT>del attrs['<STR_LIT>']<EOL><DEDENT>return _format_dict(attrs)<EOL>

# Instead of just printing <SomeType at 0xfoobar>, expand the fields.


if isinstance(object, QuerySet):<EOL><INDENT>text = '<STR_LIT>'<EOL>lineno = <NUM_LIT:0><EOL>for item in object.all()[:<NUM_LIT>]:<EOL><INDENT>lineno += <NUM_LIT:1><EOL>if lineno >= <NUM_LIT>:<EOL><INDENT>text += u'<STR_LIT>'<EOL>break<EOL><DEDENT>text += u'<STR_LIT>'.format(escape(repr(item)))<EOL><DEDENT>return text<EOL><DEDENT>elif isinstance(object, Manager):<EOL><INDENT>return mark_safe(u'<STR_LIT>')<EOL><DEDENT>elif isinstance(object, six.string_types):<EOL><INDENT>return escape(repr(object))<EOL><DEDENT>elif isinstance(object, Promise):<EOL><INDENT>return escape(_format_lazy(object))<EOL><DEDENT>elif isinstance(object, dict):<EOL><INDENT>return _format_dict(object)<EOL><DEDENT>elif isinstance(object, list):<EOL><INDENT>return _format_list(object)<EOL><DEDENT>elif hasattr(object, '<STR_LIT>'):<EOL><INDENT>return _format_object(object)<EOL><DEDENT>else:<EOL><INDENT>text = DebugPrettyPrinter(width=<NUM_LIT:200>).pformat(object)<EOL>return _style_text(text)<EOL><DEDENT>

Dump a variable to a HTML string with sensible output for template context fields.
It filters out all fields which are not usable in a template context.


def _format(self, object, stream, indent, allowance, context, level):

try:<EOL><INDENT>PrettyPrinter._format(self, object, stream, indent, allowance, context, level)<EOL><DEDENT>except Exception as e:<EOL><INDENT>stream.write(_format_exception(e))<EOL><DEDENT>


def _try_call(func, extra_exceptions=(), return_exceptions=False):

try:<EOL><INDENT>return func()<EOL><DEDENT>except HANDLED_EXCEPTIONS as e:<EOL><INDENT>if return_exceptions:<EOL><INDENT>return e<EOL><DEDENT>else:<EOL><INDENT>return _format_exception(e)<EOL><DEDENT><DEDENT>except extra_exceptions as e:<EOL><INDENT>if return_exceptions:<EOL><INDENT>return e<EOL><DEDENT>else:<EOL><INDENT>return _format_exception(e)<EOL><DEDENT><DEDENT>

Call a method, but
:param func:
:type func:
:param extra_exceptions:
:type extra_exceptions:
:return:
:rtype:


if not hasattr(response, '<STR_LIT>'):<EOL><INDENT>return None, None<EOL><DEDENT>template = response.template_name<EOL>if template is None:<EOL><INDENT>return None, None<EOL><DEDENT>if isinstance(template, (list, tuple)):<EOL><INDENT>if len(template) == <NUM_LIT:1>:<EOL><INDENT>return template[<NUM_LIT:0>], None<EOL><DEDENT>else:<EOL><INDENT>used_name = _get_used_template_name(template)<EOL>return used_name, template<EOL><DEDENT><DEDENT>elif isinstance(template, six.string_types):<EOL><INDENT>return template, None<EOL><DEDENT>else:<EOL><INDENT>filename = _get_template_filename(template)<EOL>template_name = '<STR_LIT>'.format(filename) if filename else '<STR_LIT>'<EOL>return template_name, None<EOL><DEDENT>

Get the template used in a TemplateResponse.
This returns a tuple of "active choice, all choices"


view = context_data.get('<STR_LIT>')<EOL>if not isinstance(view, View):<EOL><INDENT>view = None<EOL><DEDENT>template_context = []<EOL>for key, obj in context_data.items():<EOL><INDENT>if isinstance(obj, (BaseForm, BaseFormSet, Model)):<EOL><INDENT>template_context.append((key, _format_path(obj.__class__)))<EOL><DEDENT><DEDENT>return {<EOL>'<STR_LIT>': _get_view_model(view),<EOL>'<STR_LIT>': _get_form_class(view),<EOL>'<STR_LIT>': template_context,<EOL>}<EOL>

Extract the used view from the TemplateResponse context (ContextMixin)


def csv(self, filename=None, **format_params):

if not self.pretty:<EOL><INDENT>return None  <EOL><DEDENT>if filename:<EOL><INDENT>outfile = open(filename, '<STR_LIT:w>')<EOL><DEDENT>else:<EOL><INDENT>outfile = StringIO()<EOL><DEDENT>writer = UnicodeWriter(outfile, **format_params)<EOL>writer.writerow(self.field_names)<EOL>for row in self:<EOL><INDENT>writer.writerow(row)<EOL><DEDENT>if filename:<EOL><INDENT>outfile.close()<EOL>return CsvResultDescriptor(filename)<EOL><DEDENT>else:<EOL><INDENT>return outfile.getvalue()<EOL><DEDENT>

Generates results in comma-separated form.  Write to ``filename``
        if given. Any other parameter will be passed on to ``csv.writer``.

        :param filename: if given, the CSV will be written to filename.

        Any additional keyword arguments will be passsed
        through to ``csv.writer``.


if not plt:<EOL><INDENT>raise ImportError("<STR_LIT>")<EOL><DEDENT>self.guess_plot_columns()<EOL>self.x = self.x or range(len(self.ys[<NUM_LIT:0>]))<EOL>coords = reduce(operator.add, [(self.x, y) for y in self.ys])<EOL>plot = plt.plot(*coords, **kwargs)<EOL>if hasattr(self.x, '<STR_LIT:name>'):<EOL><INDENT>plt.xlabel(self.x.name)<EOL><DEDENT>ylabel = "<STR_LIT:U+002CU+0020>".join(y.name for y in self.ys)<EOL>plt.title(title or ylabel)<EOL>plt.ylabel(ylabel)<EOL>return plot<EOL>

Generates a pylab plot from the result set.

        ``matplotlib`` must be installed, and in an
        IPython Notebook, inlining must be on::

            %%matplotlib inline

        The first and last columns are taken as the X and Y
        values.  Any columns between are ignored.

        :param title: plot title, defaults to names of Y value columns

        Any additional keyword arguments will be passsed
        through to ``matplotlib.pylab.plot``.


return str(self.pretty or '<STR_LIT>')<EOL>

String representation of a ``ResultSet``.


stats = results.stats<EOL>contains_updates = stats.pop("<STR_LIT>", False) if stats else False<EOL>if not contains_updates:<EOL><INDENT>result = '<STR_LIT>'.format(len(results))<EOL><DEDENT>else:<EOL><INDENT>result = '<STR_LIT>'<EOL>for stat, value in stats.items():<EOL><INDENT>if value:<EOL><INDENT>result = "<STR_LIT>".format(result, value,<EOL>stat.replace("<STR_LIT:_>", "<STR_LIT:U+0020>"))<EOL><DEDENT><DEDENT><DEDENT>return result.strip()<EOL>

Generates the string to be shown as updates after the execution of a
    Cypher query

    :param results: ``ResultSet`` with the raw results of the execution of
                    the Cypher query


def draw(self, directed=True, layout="<STR_LIT>",<EOL>node_label_attr=None, show_node_labels=True,<EOL>edge_label_attr=None, show_edge_labels=True,<EOL>node_size=<NUM_LIT>, node_color='<STR_LIT>', node_alpha=<NUM_LIT>,<EOL>node_text_size=<NUM_LIT:12>,<EOL>edge_color='<STR_LIT>', edge_alpha=<NUM_LIT>, edge_tickness=<NUM_LIT:1>,<EOL>edge_text_pos=<NUM_LIT>,<EOL>text_font='<STR_LIT>', ax=None):

graph = self.get_graph(directed=directed)<EOL>pos = getattr(nx, "<STR_LIT>".format(layout))(graph)<EOL>node_labels = {}<EOL>edge_labels = {}<EOL>node_colors = set()<EOL>if show_node_labels:<EOL><INDENT>for node, props in graph.nodes(data=True):<EOL><INDENT>labels = props.pop('<STR_LIT>', [])<EOL>for label in labels:<EOL><INDENT>node_colors.add(label)<EOL><DEDENT>if node_label_attr is None:<EOL><INDENT>node_labels[node] = "<STR_LIT>".format(<EOL>"<STR_LIT::>".join(labels),<EOL>next(iter(props.values())) if props else "<STR_LIT>",<EOL>)<EOL><DEDENT>else:<EOL><INDENT>props_list = ["<STR_LIT>".format(k, v)<EOL>for k, v in props.items()]<EOL>node_labels[node] = "<STR_LIT>".format(<EOL>"<STR_LIT::>".join(labels), "<STR_LIT:\n>".join(props_list)<EOL>)<EOL><DEDENT><DEDENT><DEDENT>node_color = []<EOL>node_colors = list(node_colors)<EOL>legend_colors = []<EOL>colors = list(plt.matplotlib.colors.ColorConverter().cache.items())[<NUM_LIT:2>:]<EOL>for _, color_rgb in colors[:len(node_colors)]:<EOL><INDENT>node_color.append(color_rgb)<EOL>legend_colors.append(color_rgb)<EOL><DEDENT>if show_edge_labels:<EOL><INDENT>for start, end, props in graph.edges(data=True):<EOL><INDENT>if edge_label_attr is None:<EOL><INDENT>edge_label = props.get("<STR_LIT:type>", '<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>edge_label = props.get(edge_label_attr, '<STR_LIT>')<EOL><DEDENT>edge_labels[(start, end)] = edge_label<EOL><DEDENT><DEDENT>if not ax:<EOL><INDENT>fig = plt.figure()<EOL>ax = fig.add_subplot(<NUM_LIT>)<EOL><DEDENT>nodes = nx.draw_networkx_nodes(<EOL>graph, pos=pos, node_color=node_color,<EOL>node_size=node_size, alpha=node_alpha,<EOL>ax=ax<EOL>)<EOL>nx.draw_networkx_labels(<EOL>graph, pos=pos, labels=node_labels,<EOL>font_size=node_text_size,<EOL>font_family=text_font,<EOL>ax=ax<EOL>)<EOL>nx.draw_networkx_edges(<EOL>graph, pos=pos, width=edge_tickness,<EOL>alpha=edge_alpha, edge_color=edge_color,<EOL>ax=ax<EOL>)<EOL>nx.draw_networkx_edge_labels(<EOL>graph, pos=pos, edge_labels=edge_labels,<EOL>ax=ax<EOL>)<EOL>ax.legend([plt.Line2D([<NUM_LIT:0>], [<NUM_LIT:0>], linestyle="<STR_LIT:none>", marker="<STR_LIT:o>",<EOL>alpha=node_alpha,<EOL>markersize=<NUM_LIT:10>, markerfacecolor=color)<EOL>for color in legend_colors],<EOL>node_colors, loc=(-<NUM_LIT>, <NUM_LIT:1>), numpoints=<NUM_LIT:1>, frameon=False)<EOL>ax.set_axis_off()<EOL>return graph, ax, nodes<EOL>

Plot of a NetworkX multi-graph instance

        :param directed: boolean, optional (default=`True`).
            Whether to return a directed graph or not.
        :param layout: string, optional (default=`"spring"`).
            Layout to apply. Any of the possible NetworkX layouts will work:
            ``'circular_layout'``, ``'random_layout'``, ``'shell_layout'``,
            ``'spring_layout'``, ``'spectral_layout'``,
            or ``'fruchterman_reingold_layout'``.
        :param node_label_attr: string, optional (default=`None`).
            Attribute of the nodes that has to be used as the label.
        :param show_node_labels: boolean, optional (default=`True`).
            Whether to show or not the labels of the nodes.
        :param edge_label_attr: boolean, optional (default=`None`).
            Attribute of the edges that has to be used as the label.
        :param show_edge_labels: . optional (default=`True`).
            Whether to show or not the labels of the edges.
        :param node_size: integer, optional (default=`1600`).
            Desired size for nodes.
        :param node_color: color string, or array of floats, (default=`'blue'`)
            Node color. Can be a single color format string, or a sequence of
            colors with the same length as nodelist. If numeric values are
            specified they will be mapped to colors using the ``cmap`` and
            ``vmin``, ``vmax`` parameters. See ``matplotlib.scatter`` for more
            details.
        :param node_alpha: float, optional (default=`0.3`).
            Between 0 and 1 for transparency of nodes.
        :param node_text_size: integer, optional (default=`12`).
            Size of the node text.
        :param edge_color: color string, or array of floats (default=`'blue'`)
            Edge color. Can be a single color format string, or a sequence of
            colors with the same length as edgelist. If numeric values are
            specified they will be mapped to colors using the ``edge_cmap`` and
            ``edge_vmin``, ``edge_vmax`` parameters.
        :param edge_alpha: float, optional (default=`0.3`)
            Transparency for thee edges.
        :param edge_tickness: float or integer, optional (default=`1`).
            Thickness of the lines drawn for the edges.
        :param edge_text_pos: . Default to optional (d0)=
        :param text_font: . Default to optional (default=`'sans-serif'`).
        :param ax: ``matplotlib.Figure``, optional (default=`None`).
            A ``matplotlib.Figure`` to use when rendering the graph. If `None`,
            a new object is created and returned.----

        :return: a ``matplotlib.Figure`` with the graph rendered.


with self._lock:<EOL><INDENT>try:<EOL><INDENT>del self._data_store[:]<EOL><DEDENT>except Exception:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>

Clears all data associated with the mappers data store


def s_add(self, path, function, method=None, type_cast=None):

with self._lock:<EOL><INDENT>try:<EOL><INDENT>path = '<STR_LIT>'.format(path.lstrip('<STR_LIT:/>'))<EOL>path = '<STR_LIT>'.format(path.rstrip('<STR_LIT:/>'))<EOL>path = path.replace('<STR_LIT:<>', '<STR_LIT>')<EOL>path = path.replace('<STR_LIT:>>', '<STR_LIT>')<EOL>self.add(path, function, method, type_cast)<EOL><DEDENT>except Exception:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>

Function for registering a simple path.

        Args:
            path (str): Path to be matched.
            function (function): Function to associate with this path.
            method (str, optional): Usually used to define one of GET, POST,
                PUT, DELETE. You may use whatever fits your situation though.
                Defaults to None.
            type_cast (dict, optional): Mapping between the param name and
                one of `int`, `float` or `bool`. The value reflected by the
                provided param name will than be casted to the given type.
                Defaults to None.


self._lock = threading.RLock()<EOL>self._data_store = list()<EOL>

Create a new mapper instance.
           Using `Mapper.get()`_ is the prefered way.


return self.offset + <NUM_LIT:1><EOL>

:return: The first "item number", used when displaying messages to the user
like "Displaying items 1 to 10 of 123" - in this example 1 would be returned


def get_full_page_url(self, page_number, scheme=None):

args = dict(<EOL>request.view_args,<EOL>_external=True,<EOL>)<EOL>if scheme is not None:<EOL><INDENT>args['<STR_LIT>'] = scheme<EOL><DEDENT>if page_number != <NUM_LIT:1>:<EOL><INDENT>args['<STR_LIT>'] = page_number<EOL><DEDENT>return url_for(request.endpoint, **args)<EOL>

Get the full, external URL for this page, optinally with the passed in URL scheme


if number is None:<EOL><INDENT>return None<EOL><DEDENT>return '<STR_LIT>'.format(number)<EOL>

Rounds a number and formats it with commas i.e. 123,456,789


stripped = phone_number.replace('<STR_LIT:U+0020>', '<STR_LIT>')<EOL>return stripped.startswith('<STR_LIT>') or stripped.startswith('<STR_LIT>')<EOL>

Is the passed in phone number a uk mobile number


if phone_number is None:<EOL><INDENT>return None<EOL><DEDENT>return PHONE_NUMBER_DISALLOWED_SYMBOLS.sub('<STR_LIT>', phone_number)<EOL>

Removes unnecessary symbols from phone numbers


resp = make_response(csv_data)<EOL>resp.headers['<STR_LIT:Content-Type>'] = '<STR_LIT>'<EOL>resp.headers['<STR_LIT>'] = '<STR_LIT>' % filename<EOL>return resp<EOL>

:param csv_data: A string with the contents of a csv file in it
:param filename: The filename that the file will be saved as when it is downloaded by the user
:return: The response to return to the web connection


for elem in root.getiterator():<EOL><INDENT>if not hasattr(elem.tag, '<STR_LIT>'):<EOL><INDENT>continue<EOL><DEDENT>i = elem.tag.find('<STR_LIT:}>')<EOL>if i >= <NUM_LIT:0>:<EOL><INDENT>elem.tag = elem.tag[i + <NUM_LIT:1>:]<EOL><DEDENT><DEDENT>objectify.deannotate(root, cleanup_namespaces=True)<EOL>

Call this on an lxml.etree document to remove all namespaces


month = date.month<EOL>new_month = month + months<EOL>years = <NUM_LIT:0><EOL>while new_month < <NUM_LIT:1>:<EOL><INDENT>new_month += <NUM_LIT:12><EOL>years -= <NUM_LIT:1><EOL><DEDENT>while new_month > <NUM_LIT:12>:<EOL><INDENT>new_month -= <NUM_LIT:12><EOL>years += <NUM_LIT:1><EOL><DEDENT>year = date.year + years<EOL>try:<EOL><INDENT>return datetime.date(year, new_month, date.day)<EOL><DEDENT>except ValueError:<EOL><INDENT>if months > <NUM_LIT:0>:<EOL><INDENT>new_month += <NUM_LIT:1><EOL>if new_month > <NUM_LIT:12>:<EOL><INDENT>new_month -= <NUM_LIT:12><EOL>year += <NUM_LIT:1><EOL><DEDENT>return datetime.datetime(year, new_month, <NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>new_day = calendar.monthrange(year, new_month)[<NUM_LIT:1>]<EOL>return datetime.datetime(year, new_month, new_day)<EOL><DEDENT><DEDENT>


def enable_celery_email_logging(sender, signal, logger, loglevel, logfile, format, colorize, **kwargs):<EOL><INDENT>from celery import current_app<EOL>log.info('<STR_LIT>'.format(logger.name))<EOL>send_errors = current_app.conf['<STR_LIT>']<EOL>send_warnings = current_app.conf['<STR_LIT>']<EOL>if send_errors or send_warnings:<EOL><INDENT>error_email_subject = '<STR_LIT>'.format(site_name)<EOL>celery_handler = CeleryEmailHandler(from_address, current_app.conf['<STR_LIT>'],<EOL>error_email_subject)<EOL>if send_warnings:<EOL><INDENT>celery_handler.setLevel(logging.WARNING)<EOL><DEDENT>else:<EOL><INDENT>celery_handler.setLevel(logging.ERROR)<EOL><DEDENT>logger.addHandler(celery_handler)<EOL><DEDENT><DEDENT>return enable_celery_email_logging<EOL>

Use this to enable error reporting.  You need to put the following in your tasks.py or wherever you
want to create your celery instance:

celery = Celery(__name__)

enable_celery_email_logging = get_enable_celery_error_reporting_function('My Website [LIVE]', 'errors@mywebsite.com')
after_setup_logger.connect(enable_celery_email_logging)
after_setup_task_logger.connect(enable_celery_email_logging)


celery.conf.update(app.config)<EOL>TaskBase = celery.Task<EOL>class ContextTask(TaskBase):<EOL><INDENT>abstract = True<EOL>def __call__(self, *args, **kwargs):<EOL><INDENT>with app.app_context():<EOL><INDENT>return TaskBase.__call__(self, *args, **kwargs)<EOL><DEDENT><DEDENT><DEDENT>celery.Task = ContextTask<EOL>return celery<EOL>

Initialise Celery and set up logging

:param app: Flask app
:param celery: Celery instance


def resize_crop_image(image, dest_w, dest_h, pad_when_tall=False):

<EOL>dest_w = float(dest_w)<EOL>dest_h = float(dest_h)<EOL>dest_ratio = dest_w / dest_h<EOL>src_w = float(image.size[<NUM_LIT:0>])<EOL>src_h = float(image.size[<NUM_LIT:1>])<EOL>src_ratio = src_w / src_h<EOL>if src_ratio < dest_ratio:<EOL><INDENT>scale = dest_w / src_w<EOL>scaled_w = dest_w<EOL>scaled_h = src_h * scale<EOL>left = <NUM_LIT:0><EOL>right = dest_w<EOL>top = (scaled_h - dest_h) / <NUM_LIT><EOL>bottom = top + dest_h<EOL><DEDENT>else:<EOL><INDENT>scale = dest_h / src_h<EOL>scaled_h = dest_h<EOL>scaled_w = src_w * scale<EOL>left = (scaled_w - dest_w) / <NUM_LIT><EOL>right = left + dest_w<EOL>top = <NUM_LIT:0><EOL>bottom = dest_h<EOL><DEDENT>if pad_when_tall:<EOL><INDENT>if (bottom - top) < (scaled_h * <NUM_LIT>):<EOL><INDENT>log.info('<STR_LIT>')<EOL>return resize_pad_image(image, dest_w, dest_h)<EOL><DEDENT><DEDENT>if src_w > dest_w or src_h > dest_h:<EOL><INDENT>scaled_image = image.resize((int(scaled_w), int(scaled_h)), PIL.Image.ANTIALIAS)<EOL>cropped_image = scaled_image.crop((int(left), int(top), int(right), int(bottom)))<EOL>return cropped_image<EOL><DEDENT>elif scaled_w < src_w or scaled_h < src_h:<EOL><INDENT>cropped_image = image.crop((int(left), int(top), int(right), int(bottom)))<EOL>return cropped_image<EOL><DEDENT>else:<EOL><INDENT>return image<EOL><DEDENT>

:param image: PIL.Image
:param dest_w: Target width
:param dest_h: Target height
:return: Scaled and cropped image


scale_factor = dest_h / image.size[<NUM_LIT:1>]<EOL>dest_w = image.size[<NUM_LIT:0>] * scale_factor<EOL>scaled_image = image.resize((int(dest_w), int(dest_h)), PIL.Image.ANTIALIAS)<EOL>return scaled_image<EOL>

Resize and image to fit the passed in height, keeping the aspect ratio the same

:param image: PIL.Image
:param dest_h: The desired height


def resize_image_to_fit(image, dest_w, dest_h):

dest_w = float(dest_w)<EOL>dest_h = float(dest_h)<EOL>dest_ratio = dest_w / dest_h<EOL>src_w = float(image.size[<NUM_LIT:0>])<EOL>src_h = float(image.size[<NUM_LIT:1>])<EOL>src_ratio = src_w / src_h<EOL>if src_ratio < dest_ratio:<EOL><INDENT>scale = dest_h / src_h<EOL>scaled_h = dest_h<EOL>scaled_w = src_w * scale<EOL><DEDENT>else:<EOL><INDENT>scale = dest_w / src_w<EOL>scaled_w = dest_w<EOL>scaled_h = src_h * scale<EOL><DEDENT>scaled_image = image.resize((int(scaled_w), int(scaled_h)), PIL.Image.ANTIALIAS)<EOL>return scaled_image<EOL>

Resize the image to fit inside dest rectangle.  Resultant image may be smaller than target
:param image: PIL.Image
:param dest_w: Target width
:param dest_h: Target height
:return: Scaled image


scale_factor = dest_w / image.size[<NUM_LIT:0>]<EOL>dest_h = image.size[<NUM_LIT:1>] * scale_factor<EOL>scaled_image = image.resize((int(dest_w), int(dest_h)), PIL.Image.ANTIALIAS)<EOL>return scaled_image<EOL>

Resize and image to fit the passed in width, keeping the aspect ratio the same

:param image: PIL.Image
:param dest_w: The desired width


hash_md5 = hashlib.md5()<EOL>with open(filename, '<STR_LIT:rb>') as f:<EOL><INDENT>for chunk in iter(lambda: f.read(<NUM_LIT> * <NUM_LIT:4>), b'<STR_LIT>'):<EOL><INDENT>hash_md5.update(chunk)<EOL><DEDENT><DEDENT>return hash_md5.hexdigest()<EOL>

:param filename: The filename of the file to process
:returns: The MD5 hash of the file


def queue_email(to_addresses, from_address, subject, body, commit=True, html=True, session=None):

from models import QueuedEmail<EOL>if session is None:<EOL><INDENT>session = _db.session<EOL><DEDENT>log.info('<STR_LIT>' % (to_addresses, subject))<EOL>queued_email = QueuedEmail(html, to_addresses, from_address, subject, body, STATUS_QUEUED)<EOL>session.add(queued_email)<EOL>session.commit()<EOL>return queued_email<EOL>

Add a mail to the queue to be sent.

WARNING: Commits by default!

:param to_addresses: The names and addresses to send the email to, i.e. "Steve<steve@fig14.com>, info@fig14.com"
:param from_address: Who the email is from i.e. "Stephen Brown <s@fig14.com>"
:param subject: The email subject
:param body: The html / text body of the email
:param commit: Whether to commit to the database
:param html: Is this a html email?
:param session: The sqlalchemy session or None to use db.session


global _db, _QueuedEmail<EOL>_db = db<EOL>_QueuedEmail = queued_email_model_class<EOL>

:param db: Flask-SQLAlchemy database object
:param queued_email_model_class: Model class (see above example)


html_colour = html_colour.upper()<EOL>has_hash = False<EOL>if html_colour[<NUM_LIT:0>] == '<STR_LIT:#>':<EOL><INDENT>has_hash = True<EOL>html_colour = html_colour[<NUM_LIT:1>:]<EOL><DEDENT>r_str = html_colour[<NUM_LIT:0>:<NUM_LIT:2>]<EOL>g_str = html_colour[<NUM_LIT:2>:<NUM_LIT:4>]<EOL>b_str = html_colour[<NUM_LIT:4>:<NUM_LIT:6>]<EOL>r = int(r_str, <NUM_LIT:16>)<EOL>g = int(g_str, <NUM_LIT:16>)<EOL>b = int(b_str, <NUM_LIT:16>)<EOL>r = int(alpha * r + (<NUM_LIT:1> - alpha) * <NUM_LIT:255>)<EOL>g = int(alpha * g + (<NUM_LIT:1> - alpha) * <NUM_LIT:255>)<EOL>b = int(alpha * b + (<NUM_LIT:1> - alpha) * <NUM_LIT:255>)<EOL>out = '<STR_LIT>'.format(r, g, b)<EOL>if has_hash:<EOL><INDENT>out = '<STR_LIT:#>' + out<EOL><DEDENT>return out<EOL>

:param html_colour: Colour string like FF552B or #334455
:param alpha: Alpha value
:return: Html colour alpha blended onto white


stack = inspect.stack()[<NUM_LIT:1>]<EOL>debug_print('<STR_LIT>'.format(stack[<NUM_LIT:1>], stack[<NUM_LIT:2>], stack[<NUM_LIT:3>]))<EOL>for k, v in kwargs.items():<EOL><INDENT>lesser_debug_print('<STR_LIT>'.format(k, v))<EOL><DEDENT>

:param kwargs: Pass in the arguments to the function and they will be printed too!


with self.lock:<EOL><INDENT>for key in list(self.attempts.keys()):<EOL><INDENT>log.debug('<STR_LIT>' % key)<EOL>if key in self.attempts:<EOL><INDENT>if self.attempts[key] <= <NUM_LIT:1>:<EOL><INDENT>del self.attempts[key]<EOL><DEDENT>else:<EOL><INDENT>self.attempts[key] -= <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>now = datetime.datetime.utcnow()<EOL>for key in list(self.locks.keys()):<EOL><INDENT>if key in self.locks and self.locks[key] < now:<EOL><INDENT>log.info('<STR_LIT>' % key)<EOL>del self.locks[key]<EOL><DEDENT><DEDENT><DEDENT>

Decrease the countdowns, and remove any expired locks.  Should be called once every <decrease_every> seconds.