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

2009-02-11 Thread Isaac Eiland-Hall
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.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2009-02-11 Thread Isaac Eiland-Hall
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.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] KeyError: 'DEFAULT'

2009-02-19 Thread Isaac Eiland-Hall
http://python.pastebin.com/m26864a1b

 

Traceback (most recent call last):

File "./loopy", line 328, in 

 
set[current_set][current_section][current_key] = current_value

KeyError: 'DEFAULT'

 

 

 

First, apologies for the formatting - I'm teaching myself python after
having taught myself PHP, so I probably make amateur mistakes. I'm currently
working in a 120-character-wide environment for my own purposes, with plans
to perhaps change this later.

 

The purpose of this program, as it stands now, is to read an INI file and
parse it, line by line. Doing this by hand because my program will take the
INI file and generate multiple INI files for another program, and loop
through them (hence "Loopy" - I didn't come up with the name.)

 

My "Loopy" commands are lines in the INI that begin with a colon, e.g.
":SET=somename" starts a new set.

 

Lines 294-335 are where I've determined that the input line contains a
key-value pair. Well, I should backtrack.

 

"set" contains not only the various sets, but I track where I'm at in
set["+current+"] - I have to track the current set and section (INI section,
e.g. [DEFAULT]).

 

So what I'm trying to do on line 328 is to store the current key/value pair
in a dictionary under the proper section, which is in the proper set. Hence,
set[setname][section][key]=value

 

You can see my insanely-verbose log here:

http://python.pastebin.com/m6dcfb96d

 

That was from a run where I commented out line 328. (that dos the actual

 

Googling "KeyError" seems to indicate problems while trying to *retrieve*
values from a dictionary, whereas I'm trying to *set* values.

 

Does anyone have any insight?

 

Also, although I'm not formally requesting feedback on my coding style,
that's mainly because I don't want to waste anyone's time. But if you have
suggestions, please - I'm open. I'm self-taught and I'm sure I'm doing lots
of stupid things. I just hope it's not all bad. ;-)

 

Thank you most sincerely,

-Isaac Eiland-Hall

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor