[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> </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> </DIV>
<DIV><FONT face=3DArial size=3D2>(Comments on the style and code used =
much=20
appreciated)</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>However I was 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> </DIV>
<DIV><FONT face=3DArial size=3D2>Below is the source code =
:-</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>class closedRecords:</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2> def __init__(self, file, filter =
=3D=20
None):<BR> f =3D open(file,'r')<BR> =
lines =3D=20
f.readlines()<BR> f.close()<BR> =
self.records=20
=3D [] <BR> for l in =
lines:<BR> =20
record =3D self.parseLine(l)<BR> if=20
filter:<BR> if =
record["jobname"] =3D=3D=20
filter:<BR> =20
self.records.append(record)<BR> =20
else:<BR> =20
self.records.append(record)</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2> def noRecords(self): # =
Under the=20
assumption that it is better to provide acces via a =
function</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p;  =
; =20
# rather than access the class.records =
directly<BR> =20
return len(self.records)</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2> def =
getRecords(self):<BR> =20
return self.records # This does not seem to be =
working</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2> def parseLine(self, line): # =
line is fixed=20
length, with fixed length records (from a dbase =
file)<BR> dict=20
=3D {}<BR> dict['deleted'] =3D =
line[0:1]<BR> =20
dict['barcode'] =3D line[1:9]<BR> dict['jobcode'] =3D=20
line[9:17]</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> etc =
.....<BR> =20
return dict </FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>if __name__ =3D=3D =
"__main__":</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2> closed =3D =
closedRecords("closed.001",=20
"LIS00139")<BR> print 'No records', closed.noRecords()<BR> =
recs =3D=20
closed.getRecords<BR> print "Records:",recs # Prints out =
what looks=20
like addresses to the function, not the array<BR> for r in=20
recs: &n=
bsp; =20
# Fails with not a sequence error<BR> print=20
r["jobcode"]<BR></FONT></DIV></BODY></HTML>
------=_NextPart_000_001F_01C1BACB.150103C0--