Skip to content

Instantly share code, notes, and snippets.

@showa-yojyo
Created August 15, 2016 14:51
Show Gist options
  • Save showa-yojyo/c5e23ddb62451ea4e48bfea2e509ac72 to your computer and use it in GitHub Desktop.
Save showa-yojyo/c5e23ddb62451ea4e48bfea2e509ac72 to your computer and use it in GitHub Desktop.
How to use %history and %recall in IPython.
In [12]: %history?
Docstring:
::
%history [-n] [-o] [-p] [-t] [-f FILENAME] [-g [PATTERN [PATTERN ...]]]
[-l [LIMIT]] [-u]
[range [range ...]]
Print input history (_i<n> variables), with most recent last.
By default, input history is printed without line numbers so it can be
directly pasted into an editor. Use -n to show them.
By default, all input history from the current session is displayed.
Ranges of history can be indicated using the syntax:
``4``
Line 4, current session
``4-6``
Lines 4-6, current session
``243/1-5``
Lines 1-5, session 243
``~2/7``
Line 7, session 2 before current
``~8/1-~6/5``
From the first line of 8 sessions ago, to the fifth line of 6
sessions ago.
Multiple ranges can be entered, separated by spaces
The same syntax is used by %macro, %save, %edit, %rerun
Examples
--------
::
In [6]: %history -n 4-6
4:a = 12
5:print a**2
6:%history -n 4-6
positional arguments:
range
optional arguments:
-n print line numbers for each input. This feature is
only available if numbered prompts are in use.
-o also print outputs for each input.
-p print classic '>>>' python prompts before each input.
This is useful for making documentation, and in
conjunction with -o, for producing doctest-ready
output.
-t print the 'translated' history, as IPython understands
it. IPython filters your input and converts it all
into valid Python source before executing it (things
like magics or aliases are turned into function calls,
for example). With this option, you'll see the native
history instead of the user-entered version: '%cd /'
will be seen as 'get_ipython().magic("%cd /")' instead
of '%cd /'.
-f FILENAME FILENAME: instead of printing the output to the
screen, redirect it to the given file. The file is
always overwritten, though *when it can*, IPython asks
for confirmation first. In particular, running the
command 'history -f FILENAME' from the IPython
Notebook interface will replace FILENAME even if it
already exists *without* confirmation.
-g <[PATTERN [PATTERN ...]]>
treat the arg as a glob pattern to search for in
(full) history. This includes the saved history
(almost all commands ever written). The pattern may
contain '?' to match one unknown character and '*' to
match any number of unknown characters. Use '%hist -g'
to show full saved history (may be very long).
-l <[LIMIT]> get the last n lines from all sessions. Specify n as a
single arg, or the default is the last 10 lines.
-u when searching history using `-g`, show only unique
history.
File: d:\miniconda3\lib\site-packages\ipython\core\magics\history.py
In [13]: %history -u -g mjscore
124/2: %run ./bin/mjscore.py -F -Y
130/284: %run ./bin/mjscore.py -F -Y --today
130/288: %run ./bin/mjscore.py -F --today --input='D:\Program Files\mattari09\mjscore-201608a.txt'
130/289: %run ./bin/mjscore.py -F --today --input='D:\\Program Files\\mattari09\\mjscore-201608a.txt'
130/290: %run ./bin/mjscore.py -F --today --input 'D:\\Program Files\\mattari09\\mjscore-201608a.txt'
130/291: %run ./bin/mjscore.py -F --today --input 'D:\\Program\ Files\\mattari09\\mjscore-201608a.txt'
130/292: %run ./bin/mjscore.py -F --today --input D:\\Program\ Files\\mattari09\\mjscore-201608a.txt
138/2: %run ./bin/mjscore.py -F --today
6: %history -g mjscore
8: %history -gu mjscore
9: %history -g -u mjscore
13: %history -u -g mjscore
In [14]: %recall?
Docstring:
Repeat a command, or get command to input line for editing.
%recall and %rep are equivalent.
- %recall (no arguments):
Place a string version of last computation result (stored in the
special '_' variable) to the next input prompt. Allows you to create
elaborate command lines without using copy-paste::
In[1]: l = ["hei", "vaan"]
In[2]: "".join(l)
Out[2]: heivaan
In[3]: %recall
In[4]: heivaan_ <== cursor blinking
%recall 45
Place history line 45 on the next input prompt. Use %hist to find
out the number.
%recall 1-4
Combine the specified lines into one cell, and place it on the next
input prompt. See %history for the slice syntax.
%recall foo+bar
If foo+bar can be evaluated in the user namespace, the result is
placed at the next input prompt. Otherwise, the history is searched
for lines which contain that substring, and the most recent one is
placed at the next input prompt.
File: d:\miniconda3\lib\site-packages\ipython\core\magics\history.py
In [15]: %recall 124/2
In [16]: %run ./bin/mjscore.py -F --today
集計期間 2016/08/15 00:49 - 2016/08/15 02:06
プレイヤー あなた 下家 対面 上家
ゲーム数 21
局数 116
着順データ
着順頻度 5-2-5-9 4-8-2-7 5-5-10-1 7-6-4-4
トップ率 23.81% 19.05% 23.81% 33.33%
ラス率 42.86% 33.33% 4.76% 19.05%
平均着順 2.86 2.57 2.33 2.24
アガリデータ
アガリ率 21.55% 18.97% 26.72% 25.00%
平均得点 5760.00 4827.27 5290.32 6079.31
平均アガリ飜 2.80 2.45 3.03 3.07
平均アガリ巡目 11.20 9.91 11.90 10.48
放銃データ
放銃率 21.55% 12.93% 12.93% 9.48%
平均失点 -4468.00 -4033.33 -4000.00 -3872.73
立直データ
平均使用率 14.66% 18.10% 27.59% 22.41%
鳴きデータ
平均鳴き回数 57.76% 33.62% 32.76% 35.34%
In [17]:
@showa-yojyo
Copy link
Author

In [15] の実行で In [16] のテキストが記入された状態になる。
それを編集して(ここでは -Y を --today に書き換えて)実行した結果が Out [16] だ。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment