[Tutor] calling subroutines into program

Liam Clarke cyresse at gmail.com
Mon Feb 7 14:45:37 CET 2005


Hi Karen, 

if I have a file called foo.py =

def la()
   return "la"

x = 15


I can do the following in bar.py =

import foo    #Notice there's no .py extension!

j = foo.la()
print j
print foo.x

> la
> 15

Hope that helps

Liam Clarke


On Mon, 07 Feb 2005 14:40:06 +0100, Karen Leever
<karen.leever at falw.vu.nl> 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
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list