Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given the python code below: A course registration module for maintaining student and course registrations This demo is a possible implementation of a persistence

Given the python code below:

""" A course registration module for maintaining student and course registrations This demo is a possible implementation of a persistence model for these objects, for discussion and review. Each entity class Courses, Student and Section have a factory method called "make" to use instead of the constructor. Using the factory method puts the objects in a python dictionary, which can be backed up and loaded from file using class methods. You can also find the objects in their dictionaries by using the factory method. """ import datetime class Course: """A course is an abstract class""" def __init__(self, cname, majProgram): """ Provide a name for a course """ self.cname = cname self.department = majProgram self.prerequisites = [] def add(self, preCourse ): """ Add to the list of prerequisites for this course object. """ # Currently, this doesn't make much sense as it only # performs the list append operation. For future use when # prerequisite checks are added to the functionality. self.prerequisites.append(preCourse) return self.prerequisites def __repr__(self): return '{!s}.make({!r},{!r})'.format(self.__class__.__name__, self.cname, self.department) def __str__(self): return self.cname ## factory methods for Course allCourses = {} @classmethod def make(clss, cname, majProgram, withlab=False): """ factory method for Courses, put it in the dictionary """ if not cname in Course.allCourses: newCourse = Course.allCourses[cname] = clss(cname, majProgram) if withlab: # debug # print('making lab' + str(newCourse)) newCourse.lab = Lab.make(cname +"+lab", majProgram) else: if majProgram != Course.allCourses[cname].department: raise Exception('Course in wrong department: ' + str(course)) return Course.allCourses[cname] ## backup methods for course @staticmethod def backup(storefileName='courseStore.txt'): storeFile = open(storefileName,'w') print( Course.allCourses.__repr__(), file=storeFile ) # now list all the prerequisites for c in Course.allCourses.values(): if len(c.prerequisites) > 0: print( "{!r}.prerequisites.extend({!r})".format( c, c.prerequisites ), file=storeFile ) storeFile.close() def load(storefile='courseStore.txt'): for fileline in open(storefile,'r'): eval(fileline) class Lecture(Course): """ A Lecture course may have a lab component """ def __init__(self, cname, majProgram=None, lab=None): super().__init__(cname, majProgram) self.lab = lab def __repr__(self): return '{!s}.make({!r},{!r},{!r})'.format(self.__class__.__name__, self.cname, self.department, self.lab ) class Lab(Course): """ No special methods for Lab course yet. A placeholder for future development """ class Student: """ Information on individual students. """ __maxid__ = 201800100 # track the latest id issued def __init__(self, sname, sid=None): self.sname = sname self.sid=sid if not self.sid: self.sid = Student.__maxid__+1 if self.sid > Student.__maxid__ : Student.__maxid__ = self.sid def __str__(self): return "[" + str(self.sid) + "]" + " " + self.sname def __repr__(self): return '{!s}.make({!r},{!r})'.format(self.__class__.__name__, self.sname, self.sid) ## factory methods for Student allStudents = {} @classmethod def make(clss, sname, sid=None): """ factory method for Student, create and put it in the dictionary if sid already exists, return the student is sid is null or does not exist, create the student object """ if not sid in Student.allStudents: newStud = clss(sname, sid) sid = newStud.sid Student.allStudents[sid] = newStud else: if sname != Student.allStudents[sid].sname: raise Exception('Student has wrong name: ' + str(Student.allStudents[sid])) return Student.allStudents[sid] ## backup methods for Student @staticmethod def backup(storefileName='studentStore.txt'): storeFile = open(storefileName,'w') print( Student.allStudents.__repr__(), file=storeFile ) storeFile.close() def load(storefileName='studentStore.txt'): for fileline in open(storefileName,'r'): eval(fileline) class Section: def __init__(self, course, semester, sectionNo, capacity=100): self.course = course self.semester=semester self.sectionNo=sectionNo if not semester: self.semester = self.currentSemester() self.capacity = capacity self.myStudents = [] @staticmethod def currentSemester(): """ current semester defined to be python date object of sep 1, jan 1 or may 1 most recently past (this year) """ now = datetime.date.today() semesters = [now.replace(month=1,day=1), now.replace(month=5,day=1), now.replace(month=9,day=1)] if now < semesters[1]: return semesters[0] if now < semesters[2]: return semesters[1] return semesters[3] def add(self, onestudent ): """ Add to the list of students registered for this section. """ # Currently, this doesn't make much sense as it only # performs the list append operation. For future use when # waitlist and other checks are added to the functionality. self.myStudents.append(onestudent) return self.myStudents def __str__(self): return str( (self.course, self.semester, self.sectionNo ) ) def __repr__(self): return '{!s}.make({!r},{!r},{!r},{!r})'.format(self.__class__.__name__, self.course, self.semester, self.sectionNo, self.capacity) ## factory methods for Sections allSections = {} @classmethod def make(clss, course, semester=None, section=1, capacity=100 ): """ factory method for Section, create and put it in the dictionary keyed under course name, semester and section number if section already exisits, return the section if you want an automatically numbered new section, then provide a negative section number """ if not semester: semester = Section.currentSemester() forceflag = (section < 1) section = 1 if section < 1 else section dickey = ( course.cname, semester, section ) if not dickey in Section.allSections: # a new section number was supplied Section.allSections[dickey] = clss(course, semester, section, capacity) elif forceflag: # force a new section number while dickey in Section.allSections: sectionNo += 1 dickey = ( course.cname, semester, section ) Section.allSections[dickey] = clss(course, semester, section, capacity) return Section.allSections[dickey] ## backup methods for Section @staticmethod def backup(storefileName='sectionStore.txt'): storeFile = open(storefileName,'w') print( Section.allSections.__repr__(), file=storeFile ) # now list all the students for s in Section.allSections.values(): if len(s.myStudents) > 0: print( "{!r}.myStudents.extend({!r})".format( s, s.myStudents ), file=storeFile ) storeFile.close() def load(storefile='sectionStore.txt'): for fileline in open(storefile,'r'): eval(fileline) def createTestFixture(): # make up some courses for testing purposes a = Lecture.make("COMP2005","Computer Science") b = Lecture.make("COMP1000","Computer Science", True) c = Lecture.make("COMP1001","Computer Science", True) d = Lecture.make("MEDI2005","Meditation", True) a.add(b) a.add(c) # make up some students for testing purposes g = Student.make("John Smith") h = Student.make("Sally Jones") i = Student.make("Ahmed Eid") # make up some sections for testing purposes j = Section.make( a ) j.add( g ) j.add( h ) k = Section.make( b ) k.add( g ) k.add( h ) l = Section.make( b ) l.add( i ) if __name__ == '__main__': createTestFixture() print("----------backing up data--------") print(Course.allCourses) print(Student.allStudents) print(Section.allSections) Course.backup() Student.backup() Section.backup() print('----- killing all lists -------') Course.allCourses = {} Student.allStudents = {} Section.allSections = {} print(Course.allCourses) print(Student.allStudents) print(Section.allSections) print('-----testing after load-------') Course.load() Student.load() Section.load() print(Course.allCourses) print(Student.allStudents) print(Section.allSections) 

Also given A,B,C below:

A)Alvin is forcing everyone to use factory methods he has inserted into the defined classes to create objects, instead of the usual constructors for creating objects. This way, he can maintain python dictionaries for all the data, and keep track of all the objects that have been created and need to be persisted. These persistence-related elements (methods and dictionaries) should not be embedded in the objects themselves, persistence should separated as a isolated concern that other team members won't have to worry about, or know how they work.

B)Since the persistence is woven into each class with the backup(), load() and factory methods, it will difficult for the A3 team to change its back-end storage technology later on. The persistence solution should be designed to be replaceable like a component so the A3 team can switch to a database or cloud solution or something more "commercial grade" later on.

C) Every time any A3 team member modifies a class or designs a new one, lots of code in the defined classes will have to be modified to keep persistence working correctly. Once again, dealing with persistence ought to be separated so other team members can do their job without having to work on the persistence model every time they change something.

Answer the following:

Angelina has given you the task of coming up with a new persistence model that improves on points A,B, and C. " Make sure all the persistence concerns are separated into their own module, with a nice simple interface so the rest of the team can do their tasks without messing with the persistence code all the time. Take that extra persistence code out of our designed classes!"

Complete the task Angelina has assigned. Include your design notes for the A3 team, including at least one diagram, and working python code for the demo implementation.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke

4th Edition

0136086535, 9780136086536

More Books

Students also viewed these Databases questions

Question

Explain key aspects of e-learning

Answered: 1 week ago

Question

To what extent can OL ideas help this organization?

Answered: 1 week ago