##// 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 def init_db(self):
130 def init_db(self):
131 """Connect to the database, and create tables if necessary."""
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 self.db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer
134 self.db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer
134 primary key autoincrement, start timestamp,
135 primary key autoincrement, start timestamp,
135 end timestamp, num_cmds integer, remark text)""")
136 end timestamp, num_cmds integer, remark text)""")
@@ -213,6 +214,33 b' class HistoryManager(Configurable):'
213 return cur
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 def get_tail(self, n=10, raw=True, output=False, include_latest=False):
244 def get_tail(self, n=10, raw=True, output=False, include_latest=False):
217 """Get the last n lines from the history database.
245 """Get the last n lines from the history database.
218
246
@@ -9,7 +9,7 b''
9 import os
9 import os
10 import sys
10 import sys
11 import unittest
11 import unittest
12
12 from datetime import datetime
13 # third party
13 # third party
14 import nose.tools as nt
14 import nose.tools as nt
15
15
@@ -107,3 +107,8 b' def test_magic_rerun():'
107 nt.assert_equal(ip.user_ns["a"], 11)
107 nt.assert_equal(ip.user_ns["a"], 11)
108 ip.run_cell("%rerun")
108 ip.run_cell("%rerun")
109 nt.assert_equal(ip.user_ns["a"], 12)
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