##// END OF EJS Templates
run darker
rushabh-v -
Show More
@@ -1,40 +1,40 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 """Extract a session from the IPython input history.
2 """Extract a session from the IPython input history.
3
3
4 Usage:
4 Usage:
5 ipython-get-history.py sessionnumber [outputfile]
5 ipython-get-history.py sessionnumber [outputfile]
6
6
7 If outputfile is not given, the relevant history is written to stdout. If
7 If outputfile is not given, the relevant history is written to stdout. If
8 outputfile has a .py extension, the translated history (without IPython's
8 outputfile has a .py extension, the translated history (without IPython's
9 special syntax) will be extracted.
9 special syntax) will be extracted.
10
10
11 Example:
11 Example:
12 ./ipython-get-history.py 57 record.ipy
12 ./ipython-get-history.py 57 record.ipy
13
13
14
14
15 This script is a simple demonstration of HistoryAccessor. It should be possible
15 This script is a simple demonstration of HistoryAccessor. It should be possible
16 to build much more flexible and powerful tools to browse and pull from the
16 to build much more flexible and powerful tools to browse and pull from the
17 history database.
17 history database.
18 """
18 """
19 import sys
19 import sys
20 from pathlib import Path
20 from pathlib import Path
21
21
22 from IPython.core.history import HistoryAccessor
22 from IPython.core.history import HistoryAccessor
23
23
24 session_number = int(sys.argv[1])
24 session_number = int(sys.argv[1])
25 if len(sys.argv) > 2:
25 if len(sys.argv) > 2:
26 filepath = Path(sys.argv[2])
26 filepath = Path(sys.argv[2])
27 dest = open(filepath, "w")
27 dest = open(filepath, "w")
28 raw = not filepath.name.endswith('.py')
28 raw = not filepath.name.endswith(".py")
29 else:
29 else:
30 dest = sys.stdout
30 dest = sys.stdout
31 raw = True
31 raw = True
32
32
33 with dest:
33 with dest:
34 dest.write("# coding: utf-8\n")
34 dest.write("# coding: utf-8\n")
35
35
36 # Profiles other than 'default' can be specified here with a profile= argument:
36 # Profiles other than 'default' can be specified here with a profile= argument:
37 hist = HistoryAccessor()
37 hist = HistoryAccessor()
38
38
39 for session, lineno, cell in hist.get_range(session=session_number, raw=raw):
39 for session, lineno, cell in hist.get_range(session=session_number, raw=raw):
40 dest.write(cell + '\n')
40 dest.write(cell + '\n')
General Comments 0
You need to be logged in to leave comments. Login now