Skip to content
This repository

HistoryManager.get_session_info() #679

Merged
merged 3 commits into from over 2 years ago

3 participants

Min RK Thomas Kluyver Fernando Perez
Min RK
Owner

add simple get_session_info method to HistoryManager for fetching session timestamps, etc.

Also sets detect_types flags on the db connection, so timestamps are retrieved as datetime objects.

The test history file has been moved to a tempdir, so that we don't get test_hist.sqlite files all over the filesystem when running the test suite.

added some commits
Min RK put test_hist.sqlite in tempdir, to prevent polluting filesystem 0279eb0
Min RK add get_session_info to HistoryManager for querying session table
Also set detect_types flags on the connection, so timestamps are
retrieved as datetime objects.

Associated type checking test included.
30c9fcb
Thomas Kluyver
Collaborator

This seems sensible. I'm planning, when I have time, to pull out the methods for reading history into a HistoryAccessor class which can be instantiated without an InteractiveShell, so it can be used by tools for browsing or dumping history. HistoryManager will then subclass that, and add the methods for storing history from the shell.

IPython/testing/tools.py
@@ -170,7 +171,7 @@ def default_config():
170 171
     config.TerminalInteractiveShell.colors = 'NoColor'
171 172
     config.TerminalTerminalInteractiveShell.term_title = False,
172 173
     config.TerminalInteractiveShell.autocall = 0
173  
-    config.HistoryManager.hist_file = u'test_hist.sqlite'
  174
+    config.HistoryManager.hist_file = os.path.join(tempfile.mkdtemp(), u'test_hist.sqlite')
1
Fernando Perez Owner
fperez added a note

This file should be added to the list of files we delete on exit, so that we clean up after ourselves. But instead of making a temp directory, let's simply make a temp file with the suffix 'test_hist.sqlite'. Then the cleanup is simply a matter of removing the file on exit, which can be done by registering it with the ipython instance's .tempfiles attribute, that all get deleted at interpeter shutdown time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Min RK add test history db to shell.tempfiles for cleanup
* end_history_session moved before tempfile cleanup in shell.atexit_operations,
  to prevent access to file after deletion
503aeea
Fernando Perez fperez merged commit 9dfe802 into from
Fernando Perez fperez closed this
Fernando Perez fperez referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 3 unique commits by 1 author.

Aug 04, 2011
Min RK put test_hist.sqlite in tempdir, to prevent polluting filesystem 0279eb0
Min RK add get_session_info to HistoryManager for querying session table
Also set detect_types flags on the connection, so timestamps are
retrieved as datetime objects.

Associated type checking test included.
30c9fcb
Aug 17, 2011
Min RK add test history db to shell.tempfiles for cleanup
* end_history_session moved before tempfile cleanup in shell.atexit_operations,
  to prevent access to file after deletion
503aeea
This page is out of date. Refresh to see the latest.
30  IPython/core/history.py
@@ -129,7 +129,8 @@ def __init__(self, shell, config=None, **traits):
129 129
         
130 130
     def init_db(self):
131 131
         """Connect to the database, and create tables if necessary."""
132  
-        self.db = sqlite3.connect(self.hist_file)
  132
+        # use detect_types so that timestamps return datetime objects
  133
+        self.db = sqlite3.connect(self.hist_file, detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
133 134
         self.db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer
134 135
                         primary key autoincrement, start timestamp,
135 136
                         end timestamp, num_cmds integer, remark text)""")
@@ -213,6 +214,33 @@ def _run_sql(self, sql, params, raw=True, output=False):
213 214
         return cur
214 215
         
215 216
     
  217
+    def get_session_info(self, session=0):
  218
+        """get info about a session
  219
+        
  220
+        Parameters
  221
+        ----------
  222
+        
  223
+        session : int
  224
+            Session number to retrieve. The current session is 0, and negative
  225
+            numbers count back from current session, so -1 is previous session.
  226
+        
  227
+        Returns
  228
+        -------
  229
+        
  230
+        (session_id [int], start [datetime], end [datetime], num_cmds [int], remark [unicode])
  231
+        
  232
+        Sessions that are running or did not exit cleanly will have `end=None`
  233
+        and `num_cmds=None`.
  234
+        
  235
+        """
  236
+        
  237
+        if session <= 0:
  238
+            session += self.session_number
  239
+        
  240
+        query = "SELECT * from sessions where session == ?"
  241
+        return self.db.execute(query, (session,)).fetchone()
  242
+        
  243
+    
216 244
     def get_tail(self, n=10, raw=True, output=False, include_latest=False):
217 245
         """Get the last n lines from the history database.
218 246
         
8  IPython/core/interactiveshell.py
@@ -2540,6 +2540,11 @@ def atexit_operations(self):
2540 2540
         code that has the appropriate information, rather than trying to
2541 2541
         clutter 
2542 2542
         """
  2543
+        # Close the history session (this stores the end time and line count)
  2544
+        # this must be *before* the tempfile cleanup, in case of temporary
  2545
+        # history db
  2546
+        self.history_manager.end_session()
  2547
+        
2543 2548
         # Cleanup all tempfiles left around
2544 2549
         for tfile in self.tempfiles:
2545 2550
             try:
@@ -2547,9 +2552,6 @@ def atexit_operations(self):
2547 2552
             except OSError:
2548 2553
                 pass
2549 2554
         
2550  
-        # Close the history session (this stores the end time and line count)
2551  
-        self.history_manager.end_session()
2552  
-        
2553 2555
         # Clear all user namespaces to release all references cleanly.
2554 2556
         self.reset(new_session=False)
2555 2557
 
7  IPython/core/tests/test_history.py
@@ -9,7 +9,7 @@
9 9
 import os
10 10
 import sys
11 11
 import unittest
12  
-
  12
+from datetime import datetime
13 13
 # third party
14 14
 import nose.tools as nt
15 15
 
@@ -107,3 +107,8 @@ def test_magic_rerun():
107 107
     nt.assert_equal(ip.user_ns["a"], 11)
108 108
     ip.run_cell("%rerun")
109 109
     nt.assert_equal(ip.user_ns["a"], 12)
  110
+
  111
+def test_timestamp_type():
  112
+    ip = get_ipython()
  113
+    info = ip.history_manager.get_session_info()
  114
+    nt.assert_true(isinstance(info[1], datetime))
3  IPython/testing/globalipapp.py
@@ -194,6 +194,9 @@ def start_ipython():
194 194
 
195 195
     # A few more tweaks needed for playing nicely with doctests...
196 196
     
  197
+    # remove history file
  198
+    shell.tempfiles.append(config.HistoryManager.hist_file)
  199
+    
197 200
     # These traps are normally only active for interactive use, set them
198 201
     # permanently since we'll be mocking interactive sessions.
199 202
     shell.builtin_trap.activate()
3  IPython/testing/tools.py
@@ -31,6 +31,7 @@
31 31
 import os
32 32
 import re
33 33
 import sys
  34
+import tempfile
34 35
 
35 36
 from contextlib import contextmanager
36 37
 
@@ -170,7 +171,7 @@ def default_config():
170 171
     config.TerminalInteractiveShell.colors = 'NoColor'
171 172
     config.TerminalTerminalInteractiveShell.term_title = False,
172 173
     config.TerminalInteractiveShell.autocall = 0
173  
-    config.HistoryManager.hist_file = u'test_hist.sqlite'
  174
+    config.HistoryManager.hist_file = tempfile.mktemp(u'test_hist.sqlite')
174 175
     config.HistoryManager.db_cache_size = 10000
175 176
     return config
176 177
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.