Show More
@@ -1,7 +1,7 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Magic functions for InteractiveShell. |
|
3 | 3 | |
|
4 |
$Id: Magic.py 96 |
|
|
4 | $Id: Magic.py 968 2005-12-29 17:15:38Z fperez $""" | |
|
5 | 5 | |
|
6 | 6 | #***************************************************************************** |
|
7 | 7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
@@ -30,6 +30,7 b' import sys' | |||
|
30 | 30 | import re |
|
31 | 31 | import tempfile |
|
32 | 32 | import time |
|
33 | import cPickle as pickle | |
|
33 | 34 | from cStringIO import StringIO |
|
34 | 35 | from getopt import getopt |
|
35 | 36 | from pprint import pprint, pformat |
@@ -2525,6 +2526,80 b' Defaulting color scheme to \'NoColor\'"""' | |||
|
2525 | 2526 | |
|
2526 | 2527 | self.shell.jobs.new(parameter_s,self.shell.user_ns) |
|
2527 | 2528 | |
|
2529 | def magic_store(self, parameter_s=''): | |
|
2530 | """Lightweight persistence for ipython variables | |
|
2531 | ||
|
2532 | Example: | |
|
2533 | ||
|
2534 | ville@badger[~]|1> A = ['hello',10,'world'] | |
|
2535 | ville@badger[~]|2> %store A | |
|
2536 | ville@badger[~]|3> Exit | |
|
2537 | ||
|
2538 | (IPython session is closed and started again...) | |
|
2539 | ||
|
2540 | ville@badger:~$ ipython -p pysh | |
|
2541 | ville@badger[~]|1> print A | |
|
2542 | ||
|
2543 | ['hello', 10, 'world'] | |
|
2544 | ||
|
2545 | Usage: | |
|
2546 | ||
|
2547 | %store - Show list of all variables and their current values\\ | |
|
2548 | %store <var> - Store the *current* value of the variable to disk\\ | |
|
2549 | %store -d - Remove the variable and its value from storage\\ | |
|
2550 | %store -r - Remove all variables from storage | |
|
2551 | ||
|
2552 | It should be noted that if you change the value of a variable, you | |
|
2553 | need to %store it again if you want to persist the new value. | |
|
2554 | ||
|
2555 | Note also that the variables will need to be pickleable; most basic | |
|
2556 | python types can be safely %stored. | |
|
2557 | """ | |
|
2558 | ||
|
2559 | opts,args = self.parse_options(parameter_s,'dr',mode='list') | |
|
2560 | # delete | |
|
2561 | if opts.has_key('d'): | |
|
2562 | try: | |
|
2563 | todel = args[0] | |
|
2564 | except IndexError: | |
|
2565 | error('You must provide the variable to forget') | |
|
2566 | else: | |
|
2567 | try: | |
|
2568 | del self.shell.persist['S:' + todel] | |
|
2569 | except: | |
|
2570 | error("Can't delete variable '%s'" % todel) | |
|
2571 | # reset | |
|
2572 | elif opts.has_key('r'): | |
|
2573 | for k in self.shell.persist.keys(): | |
|
2574 | if k.startswith('S:'): | |
|
2575 | del self.shell.persist[k] | |
|
2576 | ||
|
2577 | # run without arguments -> list variables & values | |
|
2578 | elif not args: | |
|
2579 | vars = [v[2:] for v in self.shell.persist.keys() if v.startswith('S:')] | |
|
2580 | vars.sort() | |
|
2581 | if vars: | |
|
2582 | size = max(map(len,vars)) | |
|
2583 | else: | |
|
2584 | size = 0 | |
|
2585 | ||
|
2586 | fmt = '%-'+str(size)+'s -> %s' | |
|
2587 | print 'Stored variables and their in-memory values:' | |
|
2588 | for var in vars: | |
|
2589 | # print 30 first characters from every var | |
|
2590 | print fmt % (var,repr(self.shell.user_ns.get(var, '<unavailable>'))[:50]) | |
|
2591 | ||
|
2592 | # default action - store the variable | |
|
2593 | else: | |
|
2594 | pickled = pickle.dumps(self.shell.user_ns[args[0] ]) | |
|
2595 | self.shell.persist[ 'S:' + args[0] ] = pickled | |
|
2596 | print "Stored '%s' (%d bytes)" % (args[0], len(pickled)) | |
|
2597 | ||
|
2598 | ||
|
2599 | ||
|
2600 | ||
|
2601 | ||
|
2602 | ||
|
2528 | 2603 | def magic_bookmark(self, parameter_s=''): |
|
2529 | 2604 | """Manage IPython's bookmark system. |
|
2530 | 2605 |
@@ -6,7 +6,7 b' Requires Python 2.1 or newer.' | |||
|
6 | 6 | |
|
7 | 7 | This file contains all the classes and helper functions specific to IPython. |
|
8 | 8 | |
|
9 |
$Id: iplib.py 96 |
|
|
9 | $Id: iplib.py 968 2005-12-29 17:15:38Z fperez $ | |
|
10 | 10 | """ |
|
11 | 11 | |
|
12 | 12 | #***************************************************************************** |
@@ -691,6 +691,22 b' class InteractiveShell(Magic):' | |||
|
691 | 691 | self.persist = pickle.load(file(self.persist_fname)) |
|
692 | 692 | except: |
|
693 | 693 | self.persist = {} |
|
694 | ||
|
695 | ||
|
696 | for (key, value) in [(k[2:],v) for (k,v) in self.persist.items() if k.startswith('S:')]: | |
|
697 | try: | |
|
698 | obj = pickle.loads(value) | |
|
699 | except: | |
|
700 | ||
|
701 | print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % key | |
|
702 | print "The error was:",sys.exc_info()[0] | |
|
703 | continue | |
|
704 | ||
|
705 | ||
|
706 | self.user_ns[key] = obj | |
|
707 | ||
|
708 | ||
|
709 | ||
|
694 | 710 | |
|
695 | 711 | def set_hook(self,name,hook): |
|
696 | 712 | """set_hook(name,hook) -> sets an internal IPython hook. |
@@ -5,6 +5,10 b'' | |||
|
5 | 5 | time, yet I was still avoiding subclassing the builtin types. No |
|
6 | 6 | more (I'm also starting to use properties, though I won't shift to |
|
7 | 7 | 2.3-specific features quite yet). |
|
8 | (magic_store): added Ville's patch for lightweight variable | |
|
9 | persistence, after a request on the user list by Matt Wilkie | |
|
10 | <maphew at gmail.com>. The new %store magic's docstring has full | |
|
11 | details. | |
|
8 | 12 | |
|
9 | 13 | * IPython/iplib.py (InteractiveShell.post_config_initialization): |
|
10 | 14 | changed the default logfile name from 'ipython.log' to |
General Comments 0
You need to be logged in to leave comments.
Login now