Reputation: 1
I need to pass selectedCourses from my getSelectedCourses method in the Student class to the Advisor class method ApproveSelectedCourse. I actually need to use the sendCourseRegistrationRequest method from the Student class to pass the selectedCourses list from the getSelectedCourses method to Advisor class method ApproveSelectedCourse but I can't figure out how to pass the list from one class to another. Or how to even pass it to a separate method in the Student class. I'm out of ideas at this point and out of time too. So this is my last shot here. Thanks for any help!
I've tried adding the Student class as an argument to the Advisor class, I've tried defining a variable outside of all the classes to reference the method from the Student class I'm trying to use. I've tried passing the list variable to a separate method in the Student class. I've tried making the Student method a class method and a static method but I'm not even sure how @classmethod & @staticmethod work exactly or how to use them to help me. At this point I've tried everything I can think of.
import random
class Person:
def __init__(self, name):
self.name = name
def getName(self):
print('Student name is: {}'.format(self.name))
class Student(Person):
def __init__(self, name, stuID):
#print('I am a student')
Person.__init__(self, name)
self.stuID = stuID
self.finishedCourses = []
self.selectedCourses = []
def getStuID(self):
print('Student ID is: {}'.format(self.stuID))
def getSelectedCourse(self, courseList):
SelectedCourse = []
for i in courseList:
if i < 400:
SelectedCourse.append(i)
#print(SelectedCourse)
for key in prerequisiteDict.keys():
if key in SelectedCourse:
SelectedCourse.remove(key)
#print(SelectedCourse)
i = 0
while i <= 2:
finishedCourses = random.choice(SelectedCourse)
self.finishedCourses.append(finishedCourses)
#print(finishedCourses)
i += 1
print('Finished courses: {}'.format(self.finishedCourses))
x = 0
while x <= 2:
selectedCourses = random.choice(courseList)
self.selectedCourses.append(selectedCourses)
x += 1
print('Selected courses: {}'.format(self.selectedCourses))
def sendCourseRegistrationRequest(self):
pass
## for key in prerequisiteDict.keys():
## if key not in self.finishedCourses:
##
## self.finishedCourses.remove(key)
## print('Update selected courses: {}'.format(self.finishedCourses))
def updateSelectedCourse():
pass
class Advisor(Person):
def __init__(self, name):
print("I'm here.")
Person.__init__(self, name)
def approveSelectedCourse(self, Student):
print("I'm here2.")
## Student.__init__(self, finishedCourses)
## self.var1 = Student.finishedCourses
## print('self.var1 is {}: '.format(self.var1))
##
##
## for key in prerequisiteDict.keys():
##
## if key in SelectedCourse:
## return True
## else:
## self.var1.remove(key)
## print('updated course selection: {}'.format(self.selfvar1))
##
##
## for key in prerequisiteDict.keys():
## if key in getSelectedCourse:
##
## getSelectedCourse.remove(key)
## print(getSelectedCourse)
courseList = [110, 213, 315, 316, 412, 121, 223, 326, 328, 422,
136, 238, 335, 336, 432, 140, 243, 345, 346, 448,
150, 253, 355, 356, 452]
prerequisiteDict = {213:110, 412:316, 326:223, 422:328,
238:136, 345:243, 355:253, 452:356}
getSelectedCourse = Student.getSelectedCourse
stuObj = Student('Alice', 98980)
stuObj.getName()
stuObj.getStuID()
stuObj.getSelectedCourse(courseList)
#stuObj.sendCourseRegistrationRequest()
##AdvObj = Advisor('Cindy')
##AdvObj.approveSelectedCourse('Alice')
Basically my code works through the Student getSelectedCourses method the way I'm imagining it should. In my mind the easiest way to tackle my problem is to send the self.selectedCourses = [] to the sendCourseRegistrationRequest Student method which will then pass it to the Advisor method approveSelectedCourse which will then check the selectedCourses list and if one of the keys from the prereq is present in selectedCourses it will remove that course from the selectedCourses list. Ideally I just would like to figure out a way to pass the selectedCourses list from the Student class to the Advisor class but I cannot figure out how.
Upvotes: 0
Views: 65
Reputation: 13106
I think the problem here is organization. Since Advisor
inherits from Person
, there is no self.selectedCourses
attribute to be had. What I would do is one of two things:
Advisor_instance.advisees
attribute, so you can look-up by nameclass Advisor(Person):
def __init__(self, name, advisee=None):
print("I'm here.")
Person.__init__(self, name)
self.advisees = {advisee.name: advisee} if advisee else {}
def add_advisee(self, student_inst):
if student_inst.name in self.advisees:
raise KeyError("Entry already exists")
self.advisees[student_inst.name] = student_inst
# Easy name lookup and returns the list
def get_course_list(self, name):
# Will raise keyerror if name not in advisees
return self.advisees.[name].selectedCourses
alice = Student("Alice", 98980)
cindy = Advisor("Cindy", alice)
cindy.get_course_list('alice')
Personally, I think this first implementation is a bit more intuitive for your use case. An advisor can have many advisees, and storing the instances of Student
allows a simple name lookup, you don't need to pass things around.
Extending this more specifically to what you are looking for:
class Advisor(Person):
def __init__(self, name, advisee=None):
print("I'm here.")
Person.__init__(self, name)
self.advisees = {advisee.name: advisee} if advisee else {}
def add_advisee(self, student_inst):
if student_inst.name in self.advisees:
raise KeyError("Entry already exists")
self.advisees[student_inst.name] = student_inst
def approveSelectedCourse(self, student_name):
# don't set a self attr here
# will still raise a KeyError for missing advisees
var1 = self.advisees[student_name].finishedCourses
# Do things with var1
class Advisor(Person):
def __init__(self, name):
print("I'm here.")
Person.__init__(self, name)
def get_course_list(self, student_inst):
return student_inst.selectedCourses
alice = Student("Alice", 98980)
cindy = Advisor("Cindy")
cindy.get_course_list(alice)
This way would work better if the instance of Advisor
was more general, i.e. there doesn't need to be a specific Advisor
, only that they are able to accomplish Advisor
-like methods. In that case, the name
attribute could drop and the methods could be static, in which case, why have the class?
Upvotes: 1