tags:

views:

85

answers:

6

Hello,

I have a python program/file that I want to run repeatedly and calculate the averages of some variables over these runs. To do so, I thought it might be convenient to convert this program into a function or a class. One way I can think of is to add a

def Main():

line at the top and indent every line manually within it. Is there an easier way? I am using pydev on eclipse.

Thanks.

A: 

Add prints of these variables, collect the outputs from many runs, and compute the average. Or maybe I don't understand the question...

Stefano Borini
A: 

Not really. Any code that is not inside a class/function will be executed on module load time (once).

I don't know about eclipse for sure, but in most sane text editors you can select a block of text and press Tab to indent the block. Some editors have this in menu, something like "Edit->Indent".

Pasi Savolainen
+1  A: 

IMHO, Your own way is good and straightforward.

Also you need not indent each and every line manually.

Most of the editors have support for indenting whole selected block.

TheMachineCharmer
A: 

Put at the top:

while True:

or do what you said.... Or you could turn it into a class.. but that would be quite unnecessary in this case

Taos
+2  A: 

Yes you are on the right track.

Add the def Main(): line

Then in pydev select all the other code and then hit tab which will indent all the code

To make the code runnable from the command line ie let it call the Main function you will need to add some code that is executed when the module is loaded. So at the end of the file add

if __name__ == '__main__':
    Main()

The if command stops the code being run if the file is loaded via an import.

Mark
A: 

What about using class iterators.

Selinap