Ответ 1
Вы можете эмулировать поведение bash, добавив это в один из сценариев запуска (например, $(ipython locate profile)/startup/log_history.py
:
import atexit
import os
ip = get_ipython()
LIMIT = 1000 # limit the size of the history
def save_history():
"""save the IPython history to a plaintext file"""
histfile = os.path.join(ip.profile_dir.location, "history.txt")
print("Saving plaintext history to %s" % histfile)
lines = []
# get previous lines
# this is only necessary because we truncate the history,
# otherwise we chould just open with mode='a'
if os.path.exists(histfile):
with open(histfile, 'r') as f:
lines = f.readlines()
# add any new lines from this session
lines.extend(record[2] + '\n' for record in ip.history_manager.get_range())
with open(histfile, 'w') as f:
# limit to LIMIT entries
f.writelines(lines[-LIMIT:])
# do the save at exit
atexit.register(save_history)
Обратите внимание, что это эмулирует поведение истории bash/readline в том, что оно потерпит неудачу при сбое интерпретатора и т.д.
update: альтернативный
Если то, что вы на самом деле хотите, состоит в том, чтобы иметь только несколько ручных любимых команд, доступных для чтения (завершение, ^ поиск R и т.д.), которые вы можете контролировать версиями, этот загрузочный файл позволит вам сохранить этот файл самостоятельно, что быть чисто в дополнение к фактической истории команд IPython:
import os
ip = get_ipython()
favfile = "readline_favorites"
def load_readline_favorites():
"""load profile_dir/readline_favorites into the readline history"""
path = os.path.join(ip.profile_dir.location, favfile)
if not os.path.exists(path):
return
with open(path) as f:
for line in f:
ip.readline.add_history(line.rstrip('\n'))
if ip.has_readline:
load_readline_favorites()
Поместите это в свой каталог profile_default/startup/
и отредактируйте profile_default/readline_favorites
, или где угодно, чтобы сохранить этот файл, и он будет отображаться в завершении readline и т.д. на каждом сеансе IPython.