[Tutor] Function return

Paul Hartley p.hartley@spitech.com
Thu, 21 Feb 2002 11:29:53 +0800


This is a multi-part message in MIME format.

------=_NextPart_000_001F_01C1BACB.150103C0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

I am trying to create a python program to read a text file containing =
fixed length records, splitting the records up into fields.

I read all the lines using readlines() into an array, then parse each =
line into a dictionary and finally append the dictionary for each line =
into an array of all records

(Comments on the style and code used much appreciated)

However I was trying to get at the records and fields and tried to =
return the records array from a function because in prevous languages I =
read that it is better to wrap data in a class around a function rather =
than accessing the data directly and my program fell over when I tried =
to return self.records in the code below (see the function getRecords

Below is the source code :-

class closedRecords:

  def __init__(self, file, filter =3D None):
    f =3D open(file,'r')
    lines =3D f.readlines()
    f.close()
    self.records =3D []=20
    for l in lines:
      record =3D self.parseLine(l)
      if filter:
        if record["jobname"] =3D=3D filter:
          self.records.append(record)
      else:
        self.records.append(record)

  def noRecords(self):  # Under the assumption that it is better to =
provide acces via a function
                                 # rather than access the class.records =
directly
    return len(self.records)

  def getRecords(self):
    return self.records  # This does not seem to be working

  def parseLine(self, line): # line is fixed length, with fixed length =
records (from a dbase file)
    dict =3D {}
    dict['deleted'] =3D line[0:1]
    dict['barcode'] =3D line[1:9]
    dict['jobcode'] =3D line[9:17]
    etc .....
    return dict=20

if __name__ =3D=3D "__main__":

  closed =3D closedRecords("closed.001", "LIS00139")
  print 'No records', closed.noRecords()
  recs =3D closed.getRecords
  print "Records:",recs  # Prints out what looks like addresses to the =
function, not the array
  for r in recs:               # Fails with not a sequence error
    print r["jobcode"]


------=_NextPart_000_001F_01C1BACB.150103C0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2314.1000" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>I am trying to create a python program =
to read a=20
text file containing fixed length records, splitting the records up into =

fields.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I read all the lines using readlines() =
into an=20
array, then parse each line into a dictionary and finally append the =
dictionary=20
for each line into an array of all records</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>(Comments on the style and code used =
much=20
appreciated)</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>However I was&nbsp;trying to get at the =
records and=20
fields and tried to return the records array from a function because in =
prevous=20
languages I read that it is better to wrap data in a class around a =
function=20
rather than accessing the data directly and my program fell over when I =
tried to=20
return self.records in the code below (see the function =
getRecords</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Below is the source code =
:-</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>class closedRecords:</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; def __init__(self, file, filter =
=3D=20
None):<BR>&nbsp;&nbsp;&nbsp; f =3D open(file,'r')<BR>&nbsp;&nbsp;&nbsp; =
lines =3D=20
f.readlines()<BR>&nbsp;&nbsp;&nbsp; f.close()<BR>&nbsp;&nbsp;&nbsp; =
self.records=20
=3D [] <BR>&nbsp;&nbsp;&nbsp; for l in =
lines:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
record =3D self.parseLine(l)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if=20
filter:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if =
record["jobname"] =3D=3D=20
filter:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
self.records.append(record)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
else:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
self.records.append(record)</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; def noRecords(self):&nbsp; # =
Under the=20
assumption that it is better to provide acces via a =
function</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
#&nbsp;rather than access the class.records =
directly<BR>&nbsp;&nbsp;&nbsp;=20
return len(self.records)</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; def =
getRecords(self):<BR>&nbsp;&nbsp;&nbsp;=20
return self.records&nbsp; # This does not seem to be =
working</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; def parseLine(self, line): # =
line is fixed=20
length, with fixed length records (from a dbase =
file)<BR>&nbsp;&nbsp;&nbsp; dict=20
=3D {}<BR>&nbsp;&nbsp;&nbsp; dict['deleted'] =3D =
line[0:1]<BR>&nbsp;&nbsp;&nbsp;=20
dict['barcode'] =3D line[1:9]<BR>&nbsp;&nbsp;&nbsp; dict['jobcode'] =3D=20
line[9:17]</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; etc =
.....<BR>&nbsp;&nbsp;&nbsp;=20
return dict </FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>if __name__ =3D=3D =
"__main__":</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; closed =3D =
closedRecords("closed.001",=20
"LIS00139")<BR>&nbsp; print 'No records', closed.noRecords()<BR>&nbsp; =
recs =3D=20
closed.getRecords<BR>&nbsp; print "Records:",recs&nbsp; # Prints out =
what looks=20
like addresses to the function, not the array<BR>&nbsp; for r in=20
recs:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;=20
# Fails with not a sequence error<BR>&nbsp;&nbsp;&nbsp; print=20
r["jobcode"]<BR></FONT></DIV></BODY></HTML>

------=_NextPart_000_001F_01C1BACB.150103C0--