Skip to content

Instantly share code, notes, and snippets.

@seumasmorrison
Last active December 15, 2015 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seumasmorrison/5284208 to your computer and use it in GitHub Desktop.
Save seumasmorrison/5284208 to your computer and use it in GitHub Desktop.
Example of how to query IPython history, searching using a known string used in a session and then using the session_id from the first query to get the whole of that session.
#Example of searching for a string in IPython sqlite history
from IPython.core.history import HistoryAccessor
history_accessor = HistoryAccessor()
sqlite_cursor = history_accessor.search('*threshold*')
results = sqlite_cursor.fetchall()
for result in results:
print result
#From the first number in the printed results you can see the relevant session
#number, and then below retrieve the whole session using get_range
session_results = history_accessor.get_range(1321)
for session, line_number, command in session_results:
print command
#Test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment