reload / auto gui_thread / file striding
Now that I have SciPy working with python (under NT not 95), and am thoroughly enjoying the experience of strolling up the gentle learning curve, I have a few questions which seasoned users might be able to help me with. (I hope it's not considered poor etiquette to ask multiple questions in a single post!) 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? 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.) 3) I'm extracting 16 bit integers from a binary data file which consists of consecutive frames of 256 digitized channels (which follow a fixed sized header). I want to process each channel in turn. This means repeatedly seeking forward, reading a integer, and appending the value to an array. Then I have to write the processed values back to the file. The way I am doing it is as follows: def ReadChannel(chan): trace = array('d') word = array('H') for i in range(nframes): offset = ( chan + 256*i + 154 ) * 2 f.seek(offset) word.fromfile(f, 1) trace.append(float(word[0])) word.pop() f.close() return trace def WriteChannel(trace, chan): word = array('H', [0]) f = open(filename, 'r+b') for i in range(nframes): offset = ( chan + 256*i + 154 ) * 2 f.seek(offset) word[0] = trace[i] # truncation ok word.tofile(f) f.close() The code for writing is quite intuitive, whereas that for reading is not - because it involves an additional call to 'pop'. It seems strangely asymmetric to me. But is this a good way to handle this situation? Thanks very much in advance! Francis
On Tue, 7 May 2002, Francis Burton wrote:
Now that I have SciPy working with python (under NT not 95), and am thoroughly enjoying the experience of strolling up the gentle learning curve, I have a few questions which seasoned users might be able to help me with. (I hope it's not considered poor etiquette to ask multiple questions in a single post!)
It is fine to have multiple questions, but your questions are off-topic. comp.lang.python would be more appropiate place to ask these questions. Anyway, I can provide few answers below...
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
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 environment variable and the file .pythonrc.py contains stuff that is executed whenever I start python in interactive mode. HTH, Pearu
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.
On Tue, 7 May 2002, Fernando Perez wrote:
IPython supports <stuff snipped>
It doesn't do colors and calltips, but it has functions to retrieve lots of information about any object.
Which gives me an idea that IPython could do colors if you are interested in. Namely you can send colored (+ bolded + underlined + stroked + background colored) output to terminal using ANSI color escape sequences. I got the idea from http://www.livinglogic.de/Python/ansistyle/ but didn't use this ansistyle module because it was to fancy for my apps. Anyway, try print '\x1b[1;32m%s\x1b[0m'% ('Hello, I am bold green') print '\x1b[1;41m%s\x1b[0m'% ('Hello, I am bold white on red background') print '\x1b[4;31m%s\x1b[0m'% ('Hello, I am underlined red') to get an idea. Pearu
It doesn't do colors and calltips, but it has functions to retrieve lots of information about any object.
Which gives me an idea that IPython could do colors if you are interested in. Namely you can send colored (+ bolded + underlined + stroked + background colored) output to terminal using ANSI color escape sequences. I got the idea from http://www.livinglogic.de/Python/ansistyle/ but didn't use this ansistyle module because it was to fancy for my apps.
[snip] Thanks Pearu. In fact, it does a lot of coloring for exception tracebacks in various modes and prompts. I use a simpler scheme than the livinglogic stuff which is overly heavy. The trick is that it doesn't do on-the-fly, as-you-type syntax highlighting. That would require deeply hacking into the keyboard handling, because you'd need to modify the line where the user is currently typing, which is under readline's control. So it's _very_ non-trivial; I'm not trying to rewrite emacs here ;) In the future I plan to reorganize it internally (this has been discussed in the past, mainly in private with Eric and Arnd) so that it can be plugged into graphical shells like idle and pycrust without losing its stand-alone functionality in a text terminal. But that will be later... In the meantime I find it quite useful as it is. I'll soon put out a new release with some significant enhancements, I can announce it here if there's interest (it goes to c.l.py anyway). If you go to the above url you can grab it in current pre-release state (probably what will be the final at this point). Cheers, f.
participants (3)
-
Fernando Perez
-
Francis Burton
-
pearu@scipy.org