[SciPy-User] Porting code from IDL to Python - 'Common block' equivalent?

Robin robince@gmail....
Wed Jul 21 04:09:48 CDT 2010

On Wed, Jul 21, 2010 at 9:18 AM, David Andrews <irbdavid@gmail.com> wrote:
> Hi All,
> I suppose this might not strictly be a scipy type question, but I'll
> ask here as I expect some of you might understand what I'm getting at!
> I'm in the process of porting some code from IDL (Interactive Data
> Language - popular in some fields of science, but largely nowhere
> else) to Python.  Essentially it's just plotting and analyzing time
> series data, and so most of the porting is relatively simple.  The one
> stumbling block - is there an equivalent or useful replacement for the
> "common block" concept in IDL available in Python?
> Common blocks are areas of shared memory held by IDL that can be
> accessed easily from within sub-routines.  So for example, in our IDL
> code, we load data into these common blocks at the start of a session,
> and then perform whatever analysis on it.  In this manner, we do not
> have to continually re-load data every time we re-perform a piece of
> analysis.  They store their contents persistently, for the duration of
> the IDL session.  It's all for academic research purposes, so it's
> very much 'try this / see what happens / alter it, try again' kind of
> work.  The loading and initial processing of data is fairly time
> intensive, so having to reload at each step is a bit frustrating and
> not very productive.
> So, does anyone have any suggestions as to the best way to go about
> porting this sort of behavior?  Pickle seems to be one option, but
> that would involve read/write to disk operations anyway?  Any others?

One way when working interactively (eg in ipython) would be to load
the data in the workspace in single variable (I've found the Bunch
class useful for this) then pass it explicitly to all the analysis

import analysis
data = analysis.load_data()
analysis.do_something(data, params)

Alternatively you could use module level variables - any imported
module provides a scope which can contain variables so using something

def load_data():
     global data
     data = [1,2,3,4]

def print_data():
    global data
    print data

You could do
import analysis
# data available interactively as analysis.data
analysis.print_data() # uses module variable

I prefer the first way - as if I am modifying the code I am using I
can reload the module and use the new functions without having to load
the data again.



More information about the SciPy-User mailing list