[SciPy-user] reload / auto gui_thread / file striding

Fernando Perez fperez at pizero.colorado.edu
Tue May 7 15:36:24 EDT 2002


> > 1) I have a module which I want to edit and test incrementally
> >    by invoking functions interactively in IDLE after I have
> >    typed "from mymodule import *". Do I really have to quit and
> >    restart IDLE after every change? One of the books mentions
> >    reload, but this just raises a NameError: name 'mymodule' is
> >    not defined. What is the correct way to do edit-run cycles?
> 
> reload works for modules that are imported like
>   import mymodule
> If you do "from mymodule import *" then the modules content is imported to
> the current name space but the module itself is not.
> You could also try
> 
>   import mymodule
>   from mymodule import *
> 
>   # do stuff, edit mymodule.py
> 
>   reload(mymodule)
>   from mymodule import *
> 
>   # do other stuff

Or you could use IPython ;) Honestly, it's a lot more efficient than the
python shell, even though I'm obviously biased having written most of it.

But with IPython you simply say

run myfile.py

and it gets loaded into the current namespace, with all definitions
re-executed each time (an execfile() under the hood). I never use import
anymore when testing code interactively.

> > 2) Is there any way I can get IDLE (or plain python) to execute
> >    "import gui_thread/from scipy import plt/from scipy import *"
> >    automatically when I start it up? Given the fact I have to
> >    quit and restart repeatedly, it is a bit of a pain to have
> >    to retype these statements. (I prefer to use IDLE because the
> >    syntax coloring and function arg hints are really helpful.)
> 
> I have no idea about IDLE but for plain python I have defined
> 
> PYTHONSTARTUP=/home/users/pearu/.pythonrc.py

IPython supports multiple configuration files, so you can have as many things
loaded as you want and have different 'profiles' for different tasks (since
maybe you don't want to always load scipy/gui_thread, for example).

It doesn't do colors and calltips, but it has functions to retrieve lots of
information about any object. Here's a 'screenshot':

In [1]: list2dict2?
Type:           function
Base Class:     <type 'function'>
String Form:    <function list2dict2 at 0x80e3c04>
Namespace:      User-defined configuration
File:           /usr/local/home/fperez/local/python/IPython/genutils.py
Definition:     list2dict2(lst, default='')
Docstring:
    Takes a list and turns it into a dict.
    Much slower than list2dict, but more versatile. This version can take
    lists with sublists of arbitrary length (including scalars).

In [2]: pdef list2dict2
list2dict2(lst, default='')

In [3]: doc list2dict2
Takes a list and turns it into a dict.
Much slower than list2dict, but more versatile. This version can take
lists with sublists of arbitrary length (including scalars).

In [4]: source list2dict2
def list2dict2(lst,default=''):
    """Takes a list and turns it into a dict.
    Much slower than list2dict, but more versatile. This version can take
    lists with sublists of arbitrary length (including scalars)."""

    dic = {}
    for elem in lst:
        if type(elem) in (types.ListType,types.TupleType):
            size = len(elem)
            if  size == 0:
                pass
[snipped rest...]

If you are interested, you can take a look at more information at
http://www-hep.colorado.edu/~fperez/ipython/

Cheers,

f.




More information about the SciPy-User mailing list