##// END OF EJS Templates
add get_session_info to HistoryManager for querying session table...
MinRK -
Show More
@@ -129,7 +129,8 b' class HistoryManager(Configurable):'
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 b' class HistoryManager(Configurable):'
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
@@ -9,7 +9,7 b''
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 b' 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))
General Comments 0
You need to be logged in to leave comments. Login now