Skip to content

Instantly share code, notes, and snippets.

@minrk
Created July 15, 2013 20:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minrk/6003365 to your computer and use it in GitHub Desktop.
Save minrk/6003365 to your computer and use it in GitHub Desktop.
"""
Migrate old IPython history (readline) to new IPython history (sqlite)
Author: Min RK (benjaminrk@gmail.com)
License: Public Domain
"""
import os
from IPython.core.interactiveshell import InteractiveShell
from IPython.core.history import HistoryManager
from IPython.utils.path import locate_profile, get_ipython_dir
def migrate_history(old_histfile, new_histfile):
"""populate an IPython sqlite history from an old readline history file"""
# shell = InteractiveShell.instance()
history = HistoryManager(hist_file=new_histfile,
# this line shouldn't be necessary, but it is in 0.13
shell = InteractiveShell.instance()
)
with open(old_histfile) as f:
# iterate through readline history,
# and write to the new history database
for linenum, line in enumerate(f):
history.store_inputs(linenum, line)
if __name__ == '__main__':
# the default location of old history file
old = os.path.join(get_ipython_dir(), "history")
# the default location of the new history file
new = os.path.join(locate_profile(), "history.sqlite")
migrate_history(old, new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment