Show More
@@ -1081,6 +1081,54 b' class InteractiveShell(Configurable, Magic):' | |||||
1081 | readline.read_history_file(self.histfile) |
|
1081 | readline.read_history_file(self.histfile) | |
1082 | return wrapper |
|
1082 | return wrapper | |
1083 |
|
1083 | |||
|
1084 | def get_history(self, index=None, raw=False, output=True): | |||
|
1085 | """Get the history list. | |||
|
1086 | ||||
|
1087 | Get the input and output history. | |||
|
1088 | ||||
|
1089 | Parameters | |||
|
1090 | ---------- | |||
|
1091 | index : n or (n1, n2) or None | |||
|
1092 | If n, then the last entries. If a tuple, then all in | |||
|
1093 | range(n1, n2). If None, then all entries. Raises IndexError if | |||
|
1094 | the format of index is incorrect. | |||
|
1095 | raw : bool | |||
|
1096 | If True, return the raw input. | |||
|
1097 | output : bool | |||
|
1098 | If True, then return the output as well. | |||
|
1099 | ||||
|
1100 | Returns | |||
|
1101 | ------- | |||
|
1102 | If output is True, then return a dict of tuples, keyed by the prompt | |||
|
1103 | numbers and with values of (input, output). If output is False, then | |||
|
1104 | a dict, keyed by the prompt number with the values of input. Raises | |||
|
1105 | IndexError if no history is found. | |||
|
1106 | """ | |||
|
1107 | if raw: | |||
|
1108 | input_hist = self.input_hist_raw | |||
|
1109 | else: | |||
|
1110 | input_hist = self.input_hist | |||
|
1111 | if output: | |||
|
1112 | output_hist = self.user_ns['Out'] | |||
|
1113 | n = len(input_hist) | |||
|
1114 | if index is None: | |||
|
1115 | start=0; stop=n | |||
|
1116 | elif isinstance(index, int): | |||
|
1117 | start=n-index; stop=n | |||
|
1118 | elif isinstance(index, tuple) and len(index) == 2: | |||
|
1119 | start=index[0]; stop=index[1] | |||
|
1120 | else: | |||
|
1121 | raise IndexError('Not a valid index for the input history: %r' % index) | |||
|
1122 | hist = {} | |||
|
1123 | for i in range(start, stop): | |||
|
1124 | if output: | |||
|
1125 | hist[i] = (input_hist[i], output_hist.get(i)) | |||
|
1126 | else: | |||
|
1127 | hist[i] = input_hist[i] | |||
|
1128 | if len(hist)==0: | |||
|
1129 | raise IndexError('No history for range of indices: %r' % index) | |||
|
1130 | return hist | |||
|
1131 | ||||
1084 | #------------------------------------------------------------------------- |
|
1132 | #------------------------------------------------------------------------- | |
1085 | # Things related to exception handling and tracebacks (not debugging) |
|
1133 | # Things related to exception handling and tracebacks (not debugging) | |
1086 | #------------------------------------------------------------------------- |
|
1134 | #------------------------------------------------------------------------- |
@@ -55,7 +55,8 b' class Kernel(Configurable):' | |||||
55 |
|
55 | |||
56 | # Build dict of handlers for message types |
|
56 | # Build dict of handlers for message types | |
57 | msg_types = [ 'execute_request', 'complete_request', |
|
57 | msg_types = [ 'execute_request', 'complete_request', | |
58 |
'object_info_request', 'prompt_request' |
|
58 | 'object_info_request', 'prompt_request', | |
|
59 | 'history_request' ] | |||
59 | self.handlers = {} |
|
60 | self.handlers = {} | |
60 | for msg_type in msg_types: |
|
61 | for msg_type in msg_types: | |
61 | self.handlers[msg_type] = getattr(self, msg_type) |
|
62 | self.handlers[msg_type] = getattr(self, msg_type) | |
@@ -214,6 +215,16 b' class Kernel(Configurable):' | |||||
214 | content, parent, ident) |
|
215 | content, parent, ident) | |
215 | print >> sys.__stdout__, msg |
|
216 | print >> sys.__stdout__, msg | |
216 |
|
217 | |||
|
218 | def history_request(self, ident, parent): | |||
|
219 | output = parent['content'].get('output', True) | |||
|
220 | index = parent['content'].get('index') | |||
|
221 | raw = parent['content'].get('raw', False) | |||
|
222 | hist = self.shell.get_history(index=index, raw=raw, output=output) | |||
|
223 | content = {'history' : hist} | |||
|
224 | msg = self.session.send(self.reply_socket, 'history_reply', | |||
|
225 | content, parent, ident) | |||
|
226 | print >> sys.__stdout__, msg | |||
|
227 | ||||
217 | def start(self): |
|
228 | def start(self): | |
218 | while True: |
|
229 | while True: | |
219 | ident = self.reply_socket.recv() |
|
230 | ident = self.reply_socket.recv() |
@@ -372,15 +372,18 b' Message type: ``history_request``::' | |||||
372 |
|
372 | |||
373 | content = { |
|
373 | content = { | |
374 |
|
374 | |||
375 |
# If |
|
375 | # If True, also return output history in the resulting dict. | |
376 | 'output' : bool, |
|
376 | 'output' : bool, | |
377 |
|
377 | |||
378 | # This parameter can be one of: A number, a pair of numbers, 'all' |
|
378 | # If True, return the raw input history, else the transformed input. | |
|
379 | 'raw' : bool, | |||
|
380 | ||||
|
381 | # This parameter can be one of: A number, a pair of numbers, None | |||
379 | # If not given, last 40 are returned. |
|
382 | # If not given, last 40 are returned. | |
380 | # - number n: return the last n entries. |
|
383 | # - number n: return the last n entries. | |
381 | # - pair n1, n2: return entries in the range(n1, n2). |
|
384 | # - pair n1, n2: return entries in the range(n1, n2). | |
382 |
# - |
|
385 | # - None: return all history | |
383 |
'range' : n or (n1, n2) or |
|
386 | 'range' : n or (n1, n2) or None, | |
384 |
|
387 | |||
385 | # If a filter is given, it is treated as a regular expression and only |
|
388 | # If a filter is given, it is treated as a regular expression and only | |
386 | # matching entries are returned. re.search() is used to find matches. |
|
389 | # matching entries are returned. re.search() is used to find matches. | |
@@ -390,14 +393,11 b' Message type: ``history_request``::' | |||||
390 | Message type: ``history_reply``:: |
|
393 | Message type: ``history_reply``:: | |
391 |
|
394 | |||
392 | content = { |
|
395 | content = { | |
393 | # A list of (number, input) pairs |
|
396 | # A dict with prompt numbers as keys and either (input, output) or input | |
394 | 'input' : list, |
|
397 | # as the value depending on whether output was True or False, | |
395 |
|
398 | # respectively. | ||
396 | # A list of (number, output) pairs |
|
399 | 'history' : dict, | |
397 | 'output' : list, |
|
400 | } | |
398 | } |
|
|||
399 |
|
||||
400 |
|
||||
401 | Messages on the PUB/SUB socket |
|
401 | Messages on the PUB/SUB socket | |
402 | ============================== |
|
402 | ============================== | |
403 |
|
403 |
General Comments 0
You need to be logged in to leave comments.
Login now