[Tutor] Python structure advice ?

Dave S pythontut at pusspaws.net
Thu Dec 16 00:03:26 CET 2004


Im sorry to bang on about Python structure, but I do struggle with it, 
having in the past got into very bad habits with loads of BASIC where 
everything was global, and Forth, and hand coded 8031, 8051, 6502 .... I 
cant get my head round how you guys handle a modern structured language 
:-) 

(PS before anyone flames me - I think Python is great and am determined 
to learn it ;-) )

I have ended up with my application in several separate directories.

I have 'live_datad' a demon that extracts web data, at preset times and 
archives it, this will be run as a thread, and possible using a queue 
... (still digesting info from query about IPCing)

I have a 'data_core' which accepts data from either live_datad real time 
or the archive for testing, it builds up a large multi dimensional array 
with various pointers into the array.

I have a statistical module 'data_stats' which analises the array 
pulling various stats.

And finally I have an analytical module 'data_predict' which using the 
output from 'data_stats' & data directly from the 'data_core' outputs 
statistical predictions of future data.

I have written my 'live_datad', I have written my 'data_core' & have a 
fairly good idea how to write the rest.

My problem is that pretty much all the modules need to fix where they 
are when they exit and pick up from that point later on, ie more data 
comes from live_datad, it is passed to 'data_core' which updates the 
matrix, then 'data_stats' then 'data_predict'  all called form the main 
script.  This OK till the main script realizes that more data is 
avalible from 'live_datad', passes it to 'data_core' which must remember 
where it was and move on, and the same for the rest of the modules. To 
make the problem more acute the modules may not be called in exactly the 
same order depending on what I am trying to achieve.

The 'remembering where is was' seems a continuous stumbling block for 
me. I have though of coding each module as a class but this seems like a 
cheat. I could declare copious globals, this seems messy, I could define 
each module as a thread & get them talking via queues, given this 
serious thought but heeded warning in previous posts. I have thought 
about returning an list of saved 'pointers' which would be re-submitted 
when the function is called. I don't know which way to turn.

With my code now running to a few hundred lines (Don't laugh this is BIG 
for me :-D ) I am going to have to make a structure decision and any 
suggestions would be appreciated.

How would you approach it ?

Dave





More information about the Tutor mailing list