On Wed, Nov 3, 2010 at 4:25 AM, Jason Grout <jason-s...@creativetrax.com> wrote: > On 11/3/10 6:03 AM, Andrzej Giniewicz wrote: > >> %pylab (or %sagelab maybe? some functions might work a bit differently >> than with pure PyLab) >> a=zeros(1000) >> a[:100]=1 >> b=fft(a) >> figure() >> grid(True) >> plot(abs(b)) >> show() >> >> do exactly the same, without overwriting the names with definitions >> from outside of given cell other than what is defined inside it, i.e. >> a and b in this example. What do you think? Does it makes some sense >> to have %pylab/sagelab cells? I ask because I'd be willing to give it >> a try, if I have enough spare time on my hands - I think it's doable >> in finite time with only a bit of preparsing - at least it seems so to >> me right now. >> > > Sounds good.
I personally don't think this proposal for new functionality in Sage sounds good. It's a completely different sort of "mode" than all the others in Sage. It will confusingly clutter up the list of modes in the notebook. And one can ask why not do the same with every other library Sage has but doesn't include by default. There are some Pythonic alternatives to the general problem, which aren't so brutal. E.g., define and run a function. This will work now, but you'll get a syntax warning. def f(): from pylab import * a=zeros(1000) a[:100]=1 b=fft(a) figure() grid(True) plot(abs(b)) savefig('test.png') This could be fixed via a decorate (which I haven't written), which could be in Sage, so one could write: @pylab def f(): a=zeros(1000) a[:100]=1 b=fft(a) figure() grid(True) plot(abs(b)) savefig('test.png') The decorator would ensure all the pylab functions are imported inside the module. Another possibility would be a new "with statement" object. I'm pretty sure one could write a pylab (or something) object such that this works: with pylab: a=zeros(1000) a[:100]=1 b=fft(a) figure() grid(True) plot(abs(b)) savefig('test.png') Search google for "python" "with statement" for more about the with statement. Yet another option would be a command that imports pylab, but saves all the globals before they get overwritten, then restores them, e.g., something like pylab_mode() a=zeros(1000) a[:100]=1 b=fft(a) figure() grid(True) plot(abs(b)) savefig('test.png') pylab_mode(False) Anyway, if you want temporary scope, the Python language offers many ways to do this... -- William > The way I'd do it now is: > > %python > from pylab import * > a=zeros(1000) > a[:100]=1 > b=fft(a) > figure() > grid(True) > plot(abs(b)) > savefig('test.png') > > > However, I notice that this still overwrites things for other cells, as now, > even in a normal Sage cell, the plot command refers to the pylab command and > not the sage command. > > I think all you need to do is add an object to Sage called pylab that does > the right thing (see sagenb/sagenb/notebook/worksheet.py, > check_for_system_switching() for example). You could even add it to > system_names in sagenb/sagenb/notebook/notebook.py (IIRC) > > (at least, those are some pointers at where to start looking; sorry I don't > have time to be more thorough in my pointers.) > > Thanks, > > Jason > > -- > To post to this group, send an email to sage-devel@googlegroups.com > To unsubscribe from this group, send an email to > sage-devel+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/sage-devel > URL: http://www.sagemath.org > -- William Stein Professor of Mathematics University of Washington http://wstein.org -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org