Reputation: 1113
I am writing a small program that uses pickle to let people locally save their timetable/paysheet to their computer.
I have the following class:
class ScheduleDataService:
def create_object_path(self, active_user):
users_object_path = root / "user_objects" / active_user
return users_object_path
def save_users_schedule(self, users_schedule, users_object_path, active_user):
schedule = open(users_object_path, "wb")
pickle.dump(users_schedule, schedule)
schedule.close()
logger.debug("\nYour schedule has been successfully saved \n")
logger.info(active_user + "has successfully saved their schedule.")
def load_users_schedule(self, active_user):
users_object_path = self.create_object_path(active_user)
schedule = open(users_object_path, "rb")
users_schedule = pickle.load(schedule)
schedule.close()
return users_schedule
Inside this class I call the class function "create_object_path" inside of the class function "load_users_schedule".
Inside of the same module I have a second class used for viewing a users current schedule:
class ViewSchedule:
__schedule_data_service: ScheduleDataService
def __init__(self, schedule_data_service):
self.__schedule_data_service = schedule_data_service
def view_day(self, day_to_see, active_user):
users_schedule = self.__schedule_data_service.load_users_schedule(self, active_user)
to_view = []
if day_to_see == "1":
to_view.append("Monday")
elif day_to_see == "2":
to_view.append("Tuesday")
elif day_to_see == "3":
to_view.append("Wednesday")
elif day_to_see == "4":
to_view.append("Thursday")
elif day_to_see == "5":
to_view.append("Friday")
elif day_to_see.lower() == "all":
to_view = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
else:
logger.debug("Sorry that isn't a valid option please try again")
logger.info(active_user + "failed to view " + day_to_see + " from their schedule due to incorrect input")
pass
for day in users_schedule.week:
if day.name in to_view:
logger.debug("")
for session in day.sessions:
logger.debug("Day: " + day.name + " Class Code: " + session.code + " Class length: "
+ session.length)
logger.debug("")
I am trying to give this class is own instance of the class above. I am then trying to call the "load_users_schedule" function to load the users schedule. When I do this I get the following error.
File "generator.py", line 116, in <module>
ViewSchedule(ScheduleDataService).view_day(day_to_see, active_user)
File "/shared/projects/pay_sheet_generator/schedule_data.py", line 249, in view_day
users_schedule = self.__schedule_data_service.load_users_schedule(self, active_user)
File "/shared/projects/pay_sheet_generator/schedule_data.py", line 40, in load_users_schedule
users_object_path = self.create_object_path(active_user)
AttributeError: 'ViewSchedule' object has no attribute 'create_object_path'
When I remove "create_object_path" from "load_users_schedule" and run them independently the code works without issue. What I am looking for is a way to be able to embed functions inside of each other as seen above, or an explanation as to why I shouldn't do it.
Edit:
The call to ViewSchedule looks like this
ViewSchedule(ScheduleDataService).view_day(day_to_see, active_user)
Upvotes: 1
Views: 50
Reputation: 20490
You have a few issues in your code.
On the line users_schedule = self.__schedule_data_service.load_users_schedule(self, active_user)
, you do not need to add self
as the first argument, because when you call an method of a class with the instance, the first argument is automatically inserted the instance of that class
You need to pass an instance of ScheduleDataService
to the instance of ViewSchedule
, so you would want to do ViewSchedule(ScheduleDataService()).view_day(day_to_see, active_user)
instead of ViewSchedule(ScheduleDataService).view_day(day_to_see, active_user)
Also you should be aware than dunder variables are name-mangled in python, hence use them carefully in your class
Upvotes: 1
Reputation: 714
This kinda does what you want. It's helpful if people can copy and run your code. You have several variables and imports not included in your code.
import pickle
import logging
root = ""
logger = logging.getLogger()
class ScheduleDataService:
def create_object_path(self, active_user):
users_object_path = root + "user_objects" + active_user
return users_object_path
def save_users_schedule(self, users_schedule, users_object_path, active_user):
schedule = open(users_object_path, "wb")
pickle.dump(users_schedule, schedule)
schedule.close()
logger.debug("\nYour schedule has been successfully saved \n")
logger.info(active_user + "has successfully saved their schedule.")
def load_users_schedule(self, active_user):
users_object_path = self.create_object_path(active_user)
schedule = open(users_object_path, "rb")
users_schedule = pickle.load(schedule)
schedule.close()
return users_schedule
class ViewSchedule:
# This gets automatically created inside __init__ __schedule_data_service: ScheduleDataService
def __init__(self, schedule_data_service):
self.__schedule_data_service = schedule_data_service
def view_day(self, day_to_see, active_user):
users_schedule = self.__schedule_data_service.load_users_schedule(active_user)
to_view = []
if day_to_see == "1":
to_view.append("Monday")
elif day_to_see == "2":
to_view.append("Tuesday")
elif day_to_see == "3":
to_view.append("Wednesday")
elif day_to_see == "4":
to_view.append("Thursday")
elif day_to_see == "5":
to_view.append("Friday")
elif day_to_see.lower() == "all":
to_view = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
else:
logger.debug("Sorry that isn't a valid option please try again")
logger.info(active_user + "failed to view " + day_to_see + " from their schedule due to incorrect input")
pass
for day in users_schedule.week:
if day.name in to_view:
logger.debug("")
for session in day.sessions:
logger.debug("Day: " + day.name + " Class Code: " + session.code + " Class length: "
+ session.length)
logger.debug("")
active_user = "Bob"
day_to_see = 1
scheduleDataSvc = ScheduleDataService()
vs = ViewSchedule(scheduleDataSvc)
vs.view_day(day_to_see, active_user)
Upvotes: 1