[Tutor] Structuring a class
Lang Hurst
lang at tharin.com
Tue Jun 15 02:08:38 CEST 2010
I'm trying to figure out how to deal with data which will look something
like:
Student:Bob Hurley
ID: 123456
Period:4
Grad_class: 2012
Credits:
Algebra C (20P)
Chapter 1
Date: September 14, 2010
Grade: 87
Credits:1.5
Notes: Probably cheated on final. Really watch on
next quiz/test.
Chapter 2
Date: October 31, 2010
.
. **and so on**
.
Consumer Math (24G)
Module 2
.
. **more information like above**
.
So, I just figured that I would have a couple of nested dictionaries.
bob = Student('123456','Bob Hurley')
bob.period = 4
bob.grad_class = 2010
bob['credits']['Algebra C (20P)']={'Chapter 1':{'Date':'September
12, 2010', 'Grade':'87', **and so on**}}
This works, for the most part, from the command line. So I decided to
set up a class and see if I could work it from that standpoint (I'm
doing this to scratch an itch, and try to learn). My preliminary class
looks like:
class Student:
def __init__(self, ident=' ', name=' ', period=' ', grad_class='
', subject=' ', notes=' ', credit=' '):
self.name = name
self.ident = ident
self.period = period
self.notes = notes
self.grad_class = grad_class
# self.credit = {{}} <--- BAD
self.credit = {}
I'm sure that someone will enlighten me as to where my poor coding
skills are especially weak. It's the last line there (self.credit...)
which I can't figure out. The credits may be in any of about 30
different subjects (teaching at a continuation high school makes for
interesting times).
If I don't set it to anything, ie, like it is, I get an error
Stud instance has no attribute '__getitem__'
If I try to leave it available for adding to somewhat dynamically, I get
a 'wtf' from python.
Sorry for the novel, I'm just wondering if someone would set me
straight. Should I even use a class? If so, how do I set up a class
item which let's me add dictionaries to it? Thanks.
-Lang
--
There are no stupid questions, just stupid people.
More information about the Tutor
mailing list