[Tutor] How to foreach over a dynamic number of levels

Isaac Eiland-Hall daychilde at gmail.com
Thu Feb 12 00:20:58 CET 2009


I'm probably trying to go about this the wrong way - so I'm asking more for
what direction to head in, at least.

 

The group I'm working with has a python program (the Model Framework) that
runs data through a number of models. For each possible step, there are
multiple possible models. Each run also is for a single date.

 

So the ini for Model Framework might be something like this:

 

step1 = step1model-a

step2 = step2model-a

step3 =  step3model-b

step4 = step4model-a

 

(so for any given step, one or more models exist. For a Model Framework run,
you choose one model for any given step you choose to run)

 

The program I'm working on will drive the framework and run it multiple
times. For example, to run a range of dates; but also, to run a range of
models - so you might want to run models a-h on days Jan1-Jan30 - that would
be 8 times 30 = 240 runs.

 

The program I'm working on needs to be Framework-agnostic. That is, I know
that the framework's ini file consists of a number of key and value pairs,
but my program has no concept of what these values *do*, or which ones
*exist*. The end-user will basically take a Framework ini file, and change
some values from strings to lists, for example:

 

step1=["step1model-a","step1model-b","step1model-c"]

 

My program will parse that, and know that it must iterate three times
because of that line alone.

 

So I do not know in advance how many keys I will have, nor how many of them
will be arrays vs. strings, although my initial thought is that I can treat
strings like single-member arrays - I don't care if I foreach a single time
- execution time on that order of magnitude is irrelevant.

 

I'm really not sure how in the world to accomplish this.

 

The best method I can think of would involve somehow iterating through and
generating code to exec, but frankly I don't even have that as a complete
working solution. The other thought would be a function that called itself,
but that is also very very ugly.

 

I come from PHP, where you have variable variables. I think that would help
me a lot here - I could at least dynamically nest my arrays, and call my
arrays using variable vars.

 

I'm definitely a pretty new programmer. I don't mind reading - but I've
spent about 6 hours today googling for terms like "dynamic nesting" and
found a lot of interesting stuff - but no clear direction to head in.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090211/52a760e5/attachment.htm>


More information about the Tutor mailing list