##// END OF EJS Templates
-iplib.py: added InteractiveShell.getapi(). New magics should be...
vivainio -
r152:ea1ac394
parent child
Show More
@@ -1,7 +1,7
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Magic functions for InteractiveShell.
2 """Magic functions for InteractiveShell.
3
3
4 $Id: Magic.py 1089 2006-01-27 19:04:59Z vivainio $"""
4 $Id: Magic.py 1090 2006-01-27 21:24:05Z vivainio $"""
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
@@ -2665,7 +2665,9 Defaulting color scheme to 'NoColor'"""
2665 %store - Show list of all variables and their current values\\
2665 %store - Show list of all variables and their current values\\
2666 %store <var> - Store the *current* value of the variable to disk\\
2666 %store <var> - Store the *current* value of the variable to disk\\
2667 %store -d <var> - Remove the variable and its value from storage\\
2667 %store -d <var> - Remove the variable and its value from storage\\
2668 %store -r - Remove all variables from storage
2668 %store -r - Remove all variables from storage\\
2669 %store foo >a.txt - Store value of foo to new file a.txt\\
2670 %store foo >>a.txt - Append value of foo to file a.txt\\
2669
2671
2670 It should be noted that if you change the value of a variable, you
2672 It should be noted that if you change the value of a variable, you
2671 need to %store it again if you want to persist the new value.
2673 need to %store it again if you want to persist the new value.
@@ -2674,7 +2676,9 Defaulting color scheme to 'NoColor'"""
2674 python types can be safely %stored.
2676 python types can be safely %stored.
2675 """
2677 """
2676
2678
2677 opts,args = self.parse_options(parameter_s,'dr',mode='list')
2679 opts,argsl = self.parse_options(parameter_s,'dr',mode='string')
2680 args = argsl.split(None,1)
2681 ip = self.getapi()
2678 # delete
2682 # delete
2679 if opts.has_key('d'):
2683 if opts.has_key('d'):
2680 try:
2684 try:
@@ -2711,6 +2715,29 Defaulting color scheme to 'NoColor'"""
2711
2715
2712 # default action - store the variable
2716 # default action - store the variable
2713 else:
2717 else:
2718 # %store foo >file.txt or >>file.txt
2719 if len(args) > 1 and args[1].startswith('>'):
2720 fnam = os.path.expanduser(args[1].lstrip('>').lstrip())
2721 if args[1].startswith('>>'):
2722 fil = open(fnam,'a')
2723 else:
2724 fil = open(fnam,'w')
2725 obj = ip.ev(args[0])
2726 print "Writing '%s' (%s) to file '%s'." % (args[0],
2727 obj.__class__.__name__, fnam)
2728
2729
2730 if not isinstance (obj,basestring):
2731 pprint(obj,fil)
2732 else:
2733 fil.write(obj)
2734 if not obj.endswith('\n'):
2735 fil.write('\n')
2736
2737 fil.close()
2738 return
2739
2740 # %store foo
2714 obj = self.shell.user_ns[args[0] ]
2741 obj = self.shell.user_ns[args[0] ]
2715 if isinstance(inspect.getmodule(obj), FakeModule):
2742 if isinstance(inspect.getmodule(obj), FakeModule):
2716 print textwrap.dedent("""\
2743 print textwrap.dedent("""\
@@ -2722,7 +2749,7 Defaulting color scheme to 'NoColor'"""
2722 return
2749 return
2723 pickled = pickle.dumps(obj)
2750 pickled = pickle.dumps(obj)
2724 self.shell.persist[ 'S:' + args[0] ] = pickled
2751 self.shell.persist[ 'S:' + args[0] ] = pickled
2725 print "Stored '%s' (%d bytes)" % (args[0], len(pickled))
2752 print "Stored '%s' (%s, %d bytes)" % (args[0], obj.__class__.__name__,len(pickled))
2726
2753
2727 def magic_bookmark(self, parameter_s=''):
2754 def magic_bookmark(self, parameter_s=''):
2728 """Manage IPython's bookmark system.
2755 """Manage IPython's bookmark system.
@@ -2824,7 +2851,7 Defaulting color scheme to 'NoColor'"""
2824 if l ==sentinel:
2851 if l ==sentinel:
2825 break
2852 break
2826 lines.append(l)
2853 lines.append(l)
2827 block = "\n".join(lines)
2854 block = "\n".join(lines) + '\n'
2828 #print "block:\n",block
2855 #print "block:\n",block
2829 if not par:
2856 if not par:
2830 b = textwrap.dedent(block)
2857 b = textwrap.dedent(block)
@@ -6,7 +6,7 Requires Python 2.3 or newer.
6
6
7 This file contains all the classes and helper functions specific to IPython.
7 This file contains all the classes and helper functions specific to IPython.
8
8
9 $Id: iplib.py 1089 2006-01-27 19:04:59Z vivainio $
9 $Id: iplib.py 1090 2006-01-27 21:24:05Z vivainio $
10 """
10 """
11
11
12 #*****************************************************************************
12 #*****************************************************************************
@@ -2097,6 +2097,18 want to merge them back into the new files.""" % locals()
2097 # If the code compiles ok, we should handle it normally
2097 # If the code compiles ok, we should handle it normally
2098 return self.handle_normal(line,continue_prompt)
2098 return self.handle_normal(line,continue_prompt)
2099
2099
2100 def getapi(self):
2101 """ Get an IPApi object for this shell instance
2102
2103 Getting an IPApi object is always preferable to accessing the shell
2104 directly, but this holds true especially for extensions.
2105
2106 It should always be possible to implement an extension with IPApi
2107 alone. If not, contact maintainer to request an addition.
2108
2109 """
2110 return self.api
2111
2100 def handle_emacs(self,line,continue_prompt=None,
2112 def handle_emacs(self,line,continue_prompt=None,
2101 pre=None,iFun=None,theRest=None):
2113 pre=None,iFun=None,theRest=None):
2102 """Handle input lines marked by python-mode."""
2114 """Handle input lines marked by python-mode."""
@@ -14,6 +14,13
14 applies for magics, 'var = %alias' assigns alias list to var.
14 applies for magics, 'var = %alias' assigns alias list to var.
15
15
16 * ipapi.py: added meta() for accessing extension-usable data store.
16 * ipapi.py: added meta() for accessing extension-usable data store.
17
18 * iplib.py: added InteractiveShell.getapi(). New magics should be
19 written doing self.getapi() instead of using the shell directly.
20
21 * Magic.py: %store now allows doing %store foo > ~/myfoo.txt and
22 %store foo >> ~/myfoo.txt to store variables to files (in clean
23 textual form, not a restorable pickle).
17
24
18 2006-01-25 Fernando Perez <Fernando.Perez@colorado.edu>
25 2006-01-25 Fernando Perez <Fernando.Perez@colorado.edu>
19
26
General Comments 0
You need to be logged in to leave comments. Login now