[Tutor] Reading from files problem

spir denis.spir at free.fr
Mon Apr 20 10:17:41 CEST 2009


Le Mon, 20 Apr 2009 07:59:15 +0100,
"Alan Gauld" <alan.gauld at btinternet.com> s'exprima ainsi:

> So I'd change the structure to be like this(pseudo code)
> 
> students = dict()  # empty dict
> for line in gradesfile:
>     line = line.split(',')
>     s = Student()
>     s.id = line[0]
>     s.lastname = line[1]
>     etc....
>     s.hwtotal = sum(hw)
>     etc....
>     students[s.id] = s

If a student type is created, then it's worth having a readDataLine() method (whatever its name) to feed it:

students = dict()  # empty dict
for line in gradestable:	# not a file, but data read from it
    line = line.split(',')
    student = Student()
    student.readDataLine(line)
    id = line[0]
    students[id] = student

[Actually, you wouldn't even need splitting the line before passing it, except here its also a practicle way to extract the id to use it as dict key.]

If ever this is the standard, or even only, way to feed a student object with data, then the data-line-reading method may be __init__ itself. So that the loop becomes:

for line in gradestable:
    line = line.split(',')
    student = Student(line)
    id = line[0]
    students[id] = student

Denis
------
la vita e estrany


More information about the Tutor mailing list