belinda thom | 1 Dec 07:54
Favicon

Re: reload question


On Nov 28, 2006, at 10:34 PM, Fernando Perez wrote:

> [snip]
> Ideally, I'd open e.g., foo.py, into xemacs and then (re)load it into
>> iPython (py-shell buffer) via C-c RET. When I do this, it does put
>> foo into the interactive namespace. However, for doctesting, I want
>> the objects inside of foo.py to be in the namespace. To accomplish  
>> this:
>>
>>         from foo import *
>>
>> works. However, if I now modify foo.py in some way, I can't figure
>> out how to reload the objects into the namespace directly.
>>
>> This kind of operation seems like it might be used a lot. Is there
>> some magic command I'm missing?
>
> More like a python trick:
>
> import foo; reload(foo); from foo import *
>
> This will ensure that every time that line is run, you actually see
> the changes in foo.

Here's a related (?) question. I am editing a file in xemacs (call it  
foo.py). I then Ctrl+c Ctrl+m (py-execute-import-or-reload) and   
(all?) the changes are not visible in py-shell.

To be concrete, suppose I just added a new method to class Foo in  
(Continue reading)

belinda thom | 1 Dec 08:05
Favicon

Re: reload question

Please disregard my last followup---I was performing Ctrl+c Ctrl+m on  
another file. (As ipython only reports

## working on region in file /tmp/python<stuff>.py

its a pretty easy mistake to make.
belinda thom | 1 Dec 09:07
Favicon

alias / macro question

I would like to be able to bundle the following commands into a  
single command:

import <foo>; reload(<foo>); from <foo> import *

where <foo> is treated like an argument (its text value replaces foo).

At first I thought %alias might work, but alas, the manual tells me  
it is only for system shell commands.

Macro could be used, but I'd have to "remacro" every time I change  
foo. I'd also not be able to have this behavior bundled up when I  
first start ipython.

This kind of desired behavior seems pretty useful, so I'm wondering  
if ipython (or python) has something in place for this.

I'm still in the process of learning both python and ipython; thanks  
the help.

-b
Ville M. Vainio | 1 Dec 10:55
Picon
Gravatar

Re: alias / macro question

On 12/1/06, belinda thom <bthom <at> cs.hmc.edu> wrote:

> I would like to be able to bundle the following commands into a
> single command:
>
> import <foo>; reload(<foo>); from <foo> import *
>
> where <foo> is treated like an argument (its text value replaces foo).
>
> At first I thought %alias might work, but alas, the manual tells me
> it is only for system shell commands.

You could try something like:

[environmentswitch]|2> m = 'os'
[environmentswitch]|3> _ip.ex('import ' + m)
[environmentswitch]|4> %macro do_imp 3
Macro `do_imp` created. To execute, type its name (without quotes).
Macro contents:
_ip.ex('import ' + m)

[environmentswitch]|6> m = 'sys'
[environmentswitch]|7> do_imp
                   <7> Executing Macro...

--

-- 
Ville M. Vainio - vivainio.googlepages.com
blog=360.yahoo.com/villevainio - g[mail | talk]='vivainio'
Ville M. Vainio | 1 Dec 14:45
Picon
Gravatar

Re: alias / macro question

On 12/1/06, Ville M. Vainio <vivainio <at> gmail.com> wrote:

> > At first I thought %alias might work, but alas, the manual tells me
> > it is only for system shell commands.
>
> You could try something like:

... snip...

Or a slightly more heavyweight solution: define your own magic command:

----- file impall.py --------

import IPython.ipapi
ip = IPython.ipapi.get()

def doimp(self, arg):
    self.api.ex("import %s; reload(%s); from %s import *" % (
    arg,arg,arg)
    )

ip.expose_magic('imp', doimp)

-------------------------

And then add "import impall" to your .ipython/ipy_user_conf.py. After
that, you can do "%imp foo" for the effect you want.

--

-- 
Ville M. Vainio - vivainio.googlepages.com
(Continue reading)

Tony Mannucci | 1 Dec 20:01
Picon
Picon
Favicon

dir format

I have just started using ipython. I am very happy with it so far. 
It's an excellent piece of work.

A significant pet-peeve is the overriding of the python dir(). I use 
dir to see the attributes and classes in a module. For example:

import numpy as N

dir(N)

With python, I get a wide listing that can fit all the attributes on 
one screen. With ipython, each attribute is on a separate line and 
list scrolls off the screen. I cannot access all attributes (my 
screen buffer is not that large). Is there a variable setting to 
change the behavior of dir? Can I get the old dir back?

?N or ??N do not provide the same information as dir. Yes, it's more 
useful in a way, but sometimes I want to quickly find a function name 
without wading through the subpackages. Ultimately, the ipython 
method is better, but in some cases it's slower to use.

It's somewhat worse for pylab, as in:

import pylab as PLT

dir(PLT) scrolls off the screen.

?PLT provides no hint as to how to access the attributes. There are 
no subpackages. In this case, python's dir works better. I can see 
all the attributes in a little more than a screen. In ipython, many 
(Continue reading)

Fernando Perez | 1 Dec 20:11
Picon
Gravatar

Re: dir format

On 12/1/06, Tony Mannucci <Tony.Mannucci <at> jpl.nasa.gov> wrote:
> I have just started using ipython. I am very happy with it so far.
> It's an excellent piece of work.
>
> A significant pet-peeve is the overriding of the python dir(). I use
> dir to see the attributes and classes in a module. For example:
>
> import numpy as N
>
> dir(N)
>
> With python, I get a wide listing that can fit all the attributes on
> one screen. With ipython, each attribute is on a separate line and
> list scrolls off the screen. I cannot access all attributes (my
> screen buffer is not that large). Is there a variable setting to
> change the behavior of dir? Can I get the old dir back?

dir(N) returns a list, and IPython uses 'pretty printing' by default.
You can toggle this with

%Pprint

or permanently set  it in your ~/.ipython/ipythonrc file.

But you may want to use

N.a<TAB>

for inspecting an object, most of us seem to find TAB completion a
faster mechanism for introspection than dir().  Additionally, the ?
(Continue reading)

librik | 2 Dec 03:28
Picon

Re: dir format

Tony Mannucci <Tony.Mannucci <at> jpl.nasa.gov> wrote:
> With python, I get a wide listing that can fit all the attributes on 
> one screen. With ipython, each attribute is on a separate line and 
> list scrolls off the screen. I cannot access all attributes (my 
> screen buffer is not that large). Is there a variable setting to 
> change the behavior of dir? Can I get the old dir back?

This isn't really the fault of dir, but the fault of the simplistic
Python "pretty printing" function.  When it prints a list, it puts
one element on each line!  (Try entering "range(100)" to see what I
mean.)  IPython uses "pretty printing" for anything it prints as the
return value of an evaluation, which is a bit of a pain in this case.

Luckily, IPython does not use pretty printing for the "print" command,
because that's part of the Python implementation itself.  So if you
use "print dir(sys)" you'll get the old default non-pretty printed
output.

- David Librik
librik <at> panix.com
Dave | 4 Dec 01:46
Picon

TraitsUI and ipython

I'm just starting to work with the Enthought Traits module which looks
to have a very handy basic GUI for setting configuration data in
scripts and handling bits of persistent data with a trivial level of
effort.  It seems like it would be a great addition to the
ipython/pylab/YFE (your favotite editor) setup.  However, so far, I
have not been able to get python and pylab to cooperate on OS X.  I'm
using wxpython in pylab.  When I start ipython with -pylab or use
ipython -wthread and then run a TraitUI script the GUI window appears
along with a spinning-disk cursor for several seconds, followed by a
python/wxpython crash.  Here's the console crash report summary:

**********

Host Name:      iMac
Date/Time:      2006-12-03 16:37:23.796 -0800
OS Version:     10.4.8 (Build 8L127)
Report Version: 4

Command: Python
Path:    /Library/Frameworks/Python.framework/Versions/2.4/Resources/Python.app/Contents/MacOS/Python
Parent:  bash [1118]

Version: 2.4a0 (2.4alpha1)

PID:    1145
Thread: 0

Exception:  EXC_BAD_ACCESS (0x0001)
Codes:      KERN_PROTECTION_FAILURE (0x0002) at 0x00000068

(Continue reading)

Fernando Perez | 4 Dec 05:00
Picon
Favicon

Re: TraitsUI and ipython

Robert, I'm not sure why this bounced coming from you; I just added you to the 
  manual whitelist to avoid problems in the future.

########
Subject:
Re: TraitsUI and ipython
From:
Robert Kern <robert.kern <at> gmail.com>
Date:
Sun, 03 Dec 2006 19:03:10 -0600
To:
ipython-user <at> scipy.net

Dave wrote:
 > > I'm just starting to work with the Enthought Traits module which looks
 > > to have a very handy basic GUI for setting configuration data in
 > > scripts and handling bits of persistent data with a trivial level of
 > > effort.  It seems like it would be a great addition to the
 > > ipython/pylab/YFE (your favotite editor) setup.  However, so far, I
 > > have not been able to get python and pylab to cooperate on OS X.  I'm
 > > using wxpython in pylab.  When I start ipython with -pylab or use
 > > ipython -wthread and then run a TraitUI script the GUI window appears
 > > along with a spinning-disk cursor for several seconds, followed by a
 > > python/wxpython crash.

What are you using to bring up the Traits sheet? .configure_traits() is used for
when the Traits sheet *is* the application; i.e., it should not be used when a
wx.App() has already been created like it is with "ipython -wthread". Use
.edit_traits() instead. I apologize for the near-indistinguishable names;
personally, I'd prefer that .configure_traits() be called
(Continue reading)


Gmane