[Tutor] calling subroutines into program

Kent Johnson kent37 at tds.net
Mon Feb 7 15:04:54 CET 2005


Karen,

Put all of your code into a function, maybe called import_cobra(). The function should take the path 
to the basic.out file as a parameter and return the array of data. So it will look something like this:

def import_cobra(basicPath):
     cobra_xy_file = open(basicPath)
     # All the same code as below all the way to...
     cobra_xy_file.close()  # Oops, you were missing parentheses here!

     # End with a return statement that passes the array back to the caller
     return arr_xy

If you put this in a file called component.py (you might want to think of a more descriptive name, 
maybe CobraUtils.py?), then in a client program you can do this:

from component import import_cobra
arr_xy = import_cobra

Now you can do what you want with arr_xy.

Kent

Karen Leever wrote:
> Hello,
> 
> I've been writing some small python programs that basically do function 
> analysis on the (x,y) output of a fortran code called COBRA.
> The COBRA output (basin.out) is the shape of a flexed beam, of which I 
> calculate the 0-crossing, x and y of max. amplitude, and cross sectional 
> area between function and y=0.
> 
> I managed to make python calculate these things, so it actually works. 
> No problems there.
> However, I'd like to make the format a bit more elegant. Each python 
> program consists of several components, some of which occur in each of 
> the programs. These components comprise: (1) reading the COBRA output 
> file and write the contents to an array, (2) reading a second COBRA 
> output file for reference values. See below for an example.
> 
> How can I refer in my programs to these components without actually 
> incorporating them in the program?
> I don't see how I could define them as functions.
> And "import 'component'.py" does not work either: I tried this but it 
> will not recognize the array arr_xy (defined in 'component'.py) later on.
> 
> thanks for suggestion or reference,
> 
> Karen
> 
> 
> example of (1):
> ---------------------------------------------------------
> #this part of the program reads the file basin.out (the data we want to 
> analyze) and changes its contents to the array arr_xy
> #layout of basin.out:
> #1  -950.00    10.00 200        > this line contains start, interval and 
> number of x values;
> #    0.000000E+00               > remainder is a column of y values
> #   -1.931787E-07
> #   -5.713295E-07
> #   -9.322559E-07
> #   -1.071361E-06
> #   -7.801342E-07
> #    .....
> 
> import re
> 
> #open the (x,y) output file
> cobra_xy_file = open('/home/tecguest/leek/Suncobra/Screen/basin.out')
> 
> 
> #read first line and change it to a list so it can be read by the program
> firstline = cobra_xy_file.readline()
> p = re.compile(r'\s+')
> list = p.split(firstline)
> 
> 
> #from the list defined above, x-values have to be calculated.
> #the list contains strings that are converted to integers before they 
> can be used in further calculations
> start = int(float(list[1]))
> interval = int(float(list[2]))
> n = int(float(list[3]))
> stop = start + n*interval
> arr_x = range(start, stop, interval)    #the calculated x-values are 
> stored in 1D array arr_x (note, fake array, is really a list)
> 
> 
> #the list of calculated x values, together with the y values in the 
> cobra_xy_file have to be put in an array: arr_xy
> #first define the new array:
> arr_xy = []
> 
> #then fill the array with the x and y values:
> for i in range(0, len(arr_x)):
>     sub_arr_xy = []
>     sub_arr_xy.append(arr_x[i])
>     sub_arr_xy.append(float(cobra_xy_file.readline()))
>     arr_xy.append(sub_arr_xy)
> 
> #print 'These are the first values from the x,y file:'
> #print arr_xy[:5]
> #print
> 
> cobra_xy_file.close
> --------------------------------------------------------
> 
> 
> 
> 
>  >>> please note new phone and fax number <<<
> ----------------------------------------------------
> Karen Leever
> Department of Tectonics
> Faculty of Earth and Life Sciences
> Vrije Universiteit Amsterdam
> De Boelelaan 1085
> 1081 HV Amsterdam
> The Netherlands
> 
> tel: +31 20 598 7278
> fax: +31 20 598 9943
> @: karen.leever at falw.vu.nl
> ----------------------------------------------------
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



More information about the Tutor mailing list