[GRASS-user] a question on how to split python code in multiple modules

Nikos Alexandris nikos.alexandris at felis.uni-freiburg.de
Fri Oct 2 16:53:28 EDT 2009


Pythonians (2),

I am pythonising a rather long grass-bash script.

I try to split it in smaller python modules. Each module starts a
definition of the main function and ends with the 'if __name__ ==
"__main__":' trick (e.g. code below).

* Is this the way to go?

The first independent "module" contains a series of variables required
for the rest of the modules to run. However, I have difficulties to
understand clearly how to load it so the variables are available to the
rest of the modules. It loads fine but the actual variables (such as
literal strings, lists, etc.) are no where to be found. I probably do
some beginner's mistake here. I am sea   

* How do I instruct Python to keep variables in memory after executing
the function?

Also, I've seen in several grass70 python scripts, in the end of the
main() function and before the _trick_, "sys.exit(0)" calls or "return
0" code.

* What does this actually do? Is this mandatory?

I know these questions are generic but I don't know if there are grass-y
details to pay attention to. Regards, Nikos

--%<--variables-->%--
# comments

# define main function
def main():

# define variables, lists, etc.
reference = "reference_map"
high_resolution = 30
low_resolution = 500
thresholds = range(10, 110, 10)
# ...

# make this module both reusable and independent
if __name__ == "__main__":
    options, flags = grass.parser()
    main()

--%<-----end----->%--



More information about the grass-user mailing list