python - How to use dictionary for simpler invocation of attributes -


i exploring composition(has-a relationship) logic in python , have class student has address . trying store multiple student records using student_dict inside student. have graduatestudent subclassing student. make use of dictionary , set id key , store corresponding details name, age, department value. later if try add student address becomes complicated when tried access them. example: student_dict.get(id)[1])[2]

my question while adding address append existing details of student. adds address tuple , when accessing becomes tedious fetch additional indexing. can refer in add_address() method inside student , get_course() method in graduatestudent. below code , invocation. can have walk through , suggest way such can access values rather multi-dimensional indexing?

class address(object):      def __init__(self, street, pin):         self.street = street         self.pin = pin      def __str__(self):         return "address: {} {}".format(self.street, self.pin)  class student(object):         # composition     student_dict = {}      def add_address(self, id, street, pin):         self.address = address(street, pin)         self.student_dict[id] = self.student_dict[id], self.address      def get_address(self, id):         if (self.is_exists(id)):             return self.get(id)[1]         else:             print "<no student record found>"             return ""      def put(self, id, details):         if id in self.student_dict.keys():             print "student %d exists" %id             return         self.student_dict[id] = details      ...  # class  # class extends student class graduatestudent(student):      def put(self, id, *args):         super(graduatestudent, self).put(id, args)     # def      def get_course(self, id):         if (self.is_exists(id)):             return (self.get(id)[0])[2]         else:             print "<no record found>"             return ""     # def  # class  

invocations:

s = graduatestudent() s.put(1, "swad", 17, "computer science") s.put(2, "vish", 18, "mech.") s.put(3, "vino", 18, "mech.")  s.add_address(1, "11 vvk street", 600015) s.add_address(2, "22 vvk street", 600015) s.add_address(3, "33 vvk street", 600015)  print "\nstudent details::" print 1,": course: ", s.get_course(1), ",", s.get_address(1) print 2,": course: ", s.get_course(2), ",", s.get_address(2) print 3,": course: ", s.get_course(3), ",", s.get_address(3) 

result:

student details:: 1 : course:  computer science , address: 11 vvk street 600015 2 : course:  mech. , address: 22 vvk street 600015 3 : course:  mech. , address: 33 vvk street 600015     

just define details of classes student , graduatestudent attributes. each instance can have unique id defining class attribute studentid:

class student(object):        studentid = -1      def __init__(self, name, dob):         student.studentid += 1         self.id = student.studentid         self.details = dict()         self.name = name         # date of birth more useful age :)         self.dob = dob         self.address = ""      def add_address(self, street, pin):         self.address = address(street, pin)      def get_address(self):         return self.address      def __str__(self):         return "{}: name {}, address: {}".format(self.id, self.name, self.address)  class graduatestudent(student):     def __init__(self, name, dob, course):         super(graduatestudent, self).__init__(name, dob)         self.course = course     def __str__(self):         return "{}: name: {}, course: {}, address: {}".format(self.id, self.name, self.course, self.address) 

to keep track of list of students, can define directory class in add students:

class directory(object):     def __init__(self):         self.student_dict = dict()      def add_student(self, student):         if student.id in self.student_dict:             print "student %d exists" % student.id             return         self.student_dict[student.id] = student         return student.id      def get_student(self, id):         try:             return self.student_dict[id]         except keyerror:             print "<no student record found: %d>" % id             return none      def add_address(self, id, street, pin):         student = self.get_student(id)         if student not none:             student.add_address(street, pin)      def get_address(self, id):         student = self.get_student(id)         if student not none:             return student.get_address()      def print_students(self):         student in self.student_dict.values():             print student 

now manipulate student list, can write:

>>> d = directory() >>> id1 = d.add_student(graduatestudent('swad', 1999, 'computer science')) >>> id2 = d.add_student(graduatestudent('vish', 1998, 'mech.')) >>> id3 = d.add_student(graduatestudent('vino', 1998,  'mech.')) >>> d.add_address(id1, "11 vvk street", 600015) >>> d.add_address(id2, "22 vvk street", 600015) >>> d.add_address(id3, "33 vvk street", 600015) >>> d.print_students() 0: name: swad, course: computer science, address: address: 11 vvk street 600015 1: name: vish, course: mech., address: address: 22 vvk street 600015 2: name: vino, course: mech., address: address: 33 vvk street 600015 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -