[Tutor] Code structure help

Peter Otten __peter__ at web.de
Fri Mar 11 15:57:20 CET 2011

Martin De Kauwe wrote:

> Note I have cross posted this as I have only just found this mailing list
> and perhaps it is the more appropriate place (I am not sure)?

I think your question is appropriate for both lists, it just wasn't sexy 
enough for anyone on c.l.py to answer ;)
> I have been working on re-writing a model in python. However I am not sure
> how easy on the eye my final structure is and would appreciate any
> constructive comments/suggestions. So broadly the model estimates how
> plants grow using a number of related sub functions which I have grouped
> into classes and they all live in separate files. My main issue at the
> moment is I think I have a lot of verbose class instances but I really
> can't see a better way to do it. Is there a better way? How do other
> people do similar things? I am talking largely about the instances in the
> method run_sim

Random remarks:

>         pg = PlantGrowth(self.control, self.params, self.state,
>                                         self.fluxes, self.met_data)

I'd prefer your code to be even more verbose here; no two-letter variables 
for anything that is non-generic. 

>             # plant growth
>             pg.grow(project_day, self.date, leafnc)

With the construction in mind that is actually

>             # plant growth
>             plant_growth.grow(project_day, self.date, leafnc)

but plant_grows.grow() does not make a lot of sense, and the comment is 
superfluous as it just undoes the abbreviation instead of explaining what is 
going on.

>         for i in self.met_data.doy:
>             project_day = i - 1

>             self.increment_date()

I'd iterate over the project_day-s directly if possible

        for project_day in self.project_days():

>             # calculate model decay rates
>             dc.decay_rates()

A lot of methods don't take any arguments and return nothing. I'm guessing 
that they modify the state that you passed to the initializer. I prefer 
these modifications to be explicit if feasible, e. g.

             state = dc.decay_rates(state)

where of course state is a placeholder for the actual variables that are 
necessary to do the calculations.

The big picture? I'll leave that for someone else.

More information about the Tutor mailing list