[IPython-dev] Selecting lines from history

Thomas Kluyver takowl at gmail.com
Sun Feb 6 11:00:28 EST 2011


With the new history system, the history from previous sessions is stored in
the same lists as the history from the current session (input_history_raw,
input_history_parsed). So, when selecting lines with magic commands
%save/%edit/%macro, the numbers didn't match up to the prompt numbers in the
current session (except when you start with no history).

The approach I've taken to dealing with this is to add a session_offset,
which is used by these cases to translate session line numbers (starting
from 1) to indices in the history lists. This has the added benefit that,
using a default offset of -1, we don't need a blank item at the top of the
list to use 1-based indexing. In principle, you can select lines from
previous sessions using negative numbers; in practice, the - is interpreted
to indicate options, and the command fails.

https://github.com/ipython/ipython/pull/261

Other possible approaches I can think of:
- Use a dict instead of a list, with prompt numbers as keys. Previous
sessions could be stored with another form of keys, perhaps tuples (-1, 1)
(=first line of last session), and if desirable, we could come up with some
syntax to let magic commands access these. Separating sessions would also
allow a command line option to repeat the commands saved from the previous
session, e.g. after a power failure.
- Previous sessions go into a separate list from the current session. Again,
some syntax could be devised to allow magic commands to access them.
- Only load previous sessions for readline; don't allow access from magic
commands until they're re-used in the current session. I believe this is
what we did before the new history system.

What was the rationale behind saving and reloading the history as we now do?
Do we want to be able to do something like "%edit x1-x12"? That seems to
have limited value when you can't immediately see the lines you're selecting
(although of course the %hist command could have options to show them). Is
there another reason we store history that I've overlooked?

There's also the mysterious "shadow history" system, which I'm not entirely
clear about the reasons for.

Thanks,
Thomas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20110206/879d3358/attachment.html>


More information about the IPython-dev mailing list