Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Name error on method
-
I’m getting a name error when I try to use the morbid bed_start_time(). The method is so I can Keep track of what time the housekeeper started cleaning the room. I would like the time is added to a dictionary so I can keep track of all the times and room numbers. I would like to try and figure out the rest of the method but I’m stuck on a simple name error. Thanks for any help or advice.
Traceback (most recent call last):
File "/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/worksheet1.py", line 42, in <module>
start_t1 = room_1.bed_start_time()
File "/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/worksheet1.py", line 19, in bed_start_time
room_with_times_dict [self.room_num] = bed_start_time
NameError: name 'self' is not definedimport datetime import time class Room (object): def __init__ (self, room_num, house_keeper_num, room_start_time, room_end_time, room_start_cleaning, iso_type, running_time): self.room_num = room_num self.house_keeper_num = house_keeper_num self.room_start_time = room_start_time self.room_end_time = room_end_time self.iso_type = iso_type self.running_time = running_time def bed_start_time(): room_with_times_dict = {} bed_start_time = datetime.datetime.now() room_with_times_dict [self.room_num] = bed_start_time return bed_start_time def bed_end_time(): bed_end_time = datetime.datetime.now() return bed_end_time def __str__(): pass dirty_rooms = [430, 6218, 520, 452, 444, 423,660,123,877,223,254,220,659,315,550,5228] #if the house keeper is assigned to the room the the rooms times will be used for the cleaning times room_1 = Room room_1.room_num = 430 room_1.iso_type = 'COVID' room_2 = Room room_2.room_num = 6218 room_2.iso_type = 'None' #room start time start_t1 = room_1.bed_start_time() #time delay time.sleep(9) #room end time end_t1 = room_1.bed_end_time() start_t2 = room_2.bed_start_time() time.sleep(12) end_t2 = room_2.bed_end_time() start_t3 = datetime.datetime.now() time.sleep(7) end_t3 = datetime.datetime.now() start_t4 = datetime.datetime.now() time.sleep(1) end_t4 = datetime.datetime.now() #amount of time taken to clean the room elapsed_t1 = end_t1 - start_t1 elapsed_t2 = end_t2- start_t2 elapsed_t3 = end_t3- start_t3 elapsed_t4 = end_t4- start_t4 #total time to clean all rooms total_time = elapsed_t1 + elapsed_t2 + elapsed_t3 + elapsed_t4 #avg time to clean a room avg_time = total_time / 4 #sort list by length of time. longest time is first. sorted_rooms_by_time_ = [] times = [elapsed_t1, elapsed_t2, elapsed_t3, elapsed_t4] sorted_times = sorted(times, reverse=True) print(sorted_times) dddd = {} print('Start Time 1:\n',start_t1.time(),'\n') print('End Time 1:\n',end_t1.time(),'\n') print('Elapsed Time 1:\n',elapsed_t1,'\n') print('Elapsed Time 2:\n', elapsed_t2,'\n') print('Elapsed Time 3:\n', elapsed_t3,'\n') print('Total Time:\n',total_time,'\n') print('Avg Time:\n',avg_time,'\n') print(room_with_times_dict)
-
Don’t forget self in method definitions.
def bed_start_time(): —> def bed_start_time(self):
def bed_end_time(): —> def bed_end_time(self):
def __str__(): —> def __str__(self): -
@ccc I’ve tried putting self but I get this error
Traceback (most recent call last):
File "/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/worksheet1.py", line 42, in <module>
start_t1 = room_1.bed_start_time()
TypeError: bed_start_time() missing 1 required positional argument: 'self'And if I change ....
room_1. bed_start_time()
To....
bed_start_time(Room_1)
I get this error
Traceback (most recent call last):
File "/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/worksheet1.py", line 42, in <module>
start_t1 = bed_start_time(room_1)
NameError: name 'bed_start_time' is not definedAnd I thought the error had to do with
room_with_times_dict [self.room_num] = bed_start_time
But I could be wrong
-
room_1 = Room --> room_1 = Room() # The init parameters are not optional so you will need to provide them.
room_2 = Room --> room_2 = Room() -
@ccc ok. I though something was up with that
When I first tried to make a room object. It was working without the parentheses so I just went with it. I’m gonna go add that and see what I can figure out from there. Thanks. -
@ccc ok so I updated
Method name with self in the ()
Added () to end of the class when creating objects.
And lastly I removed self from the () in datetime.datetime.now(self)
Im having some trouble with another part of the method but I’m going to try to figure it out. I’ll let you guys know if I can’t. Thanks for the help I appreciate it. I wish I had someone I knew locally who could just school me as I try out code.
-
I wish I had someone I knew locally who could just school me as I try out code.
Get a GitHub account and put your code in a repo... Folks can review your code as it changes and help you that way. We all work remote these daze.
-
@ccc ok. I’ll set one up and let you know. Thanks for the suggestion.
-
I’ve got it to where the bed_start_time() of the room is added to a dictionary and I’m working on formatting the time. Trying to figure out where exactly I need to add striftime() on my code. I have to create a dictionary outside of the method for it to recognize the dictionary. If I create a dictionary inside the method it says it’s never been defined. Two things. Why can’t I create dictionaries inside of a class method and is it good practice to create variables and lists or dictionaries at the beginning of my script along with the classes and functions
-
Using f-strings can help you avoid
striftime()
>>> from datetime import datetime >>> f"{datetime.now():%Y-%m-%d %H:%M}" '2020-09-22 12:19'
Nice cheatsheet at https://strftime.org/
-
@ccc ok. I figure out where to put it. Now I’m gonna use f string to format it. Also. Gonna start trying to remember to use f string.
import datetime
import timeroom_with_stimes_dict = {}
class Room (object):
def __init__ (self, room_num = None, house_keeper_num = None, room_start_time = None, room_end_time = None, room_start_cleaning = None, iso_type = None, running_time = None): self.room_num = room_num self.house_keeper_num = house_keeper_num self.room_start_time = room_start_time self.room_end_time = room_end_time self.iso_type = iso_type self.running_time = running_time def bed_start_time(self): bed_start_time = datetime.datetime.now() room_with_stimes_dict [self.room_num] = bed_start_time.strftime('%H:%M:%S') return bed_start_time def bed_end_time(self): bed_end_time = datetime.datetime.now() return bed_end_time def __str__(): pass
dirty_rooms = [430, 6218, 520, 452, 444, 423,660,123,877,223,254,220,659,315,550,5228]
#if the house keeper is assigned to the room the the rooms times will be used for the cleaning times
room_1 = Room()
room_1.room_num = 430
room_1.iso_type = 'COVID'room_2 = Room()
room_2.room_num = 6218
room_2.iso_type = 'None'room_3 = Room()
room_3.room_num = 654
room_3.iso_type = 'influenza'room_4 = Room()
room_4.room_num = 220
room_4.iso_type = 'None'#room start time
start_t1 = room_1.bed_start_time()
#time delay
time.sleep(9)
#room end time
end_t1 = room_1.bed_end_time()start_t2 = room_2.bed_start_time()
time.sleep(12)
end_t2 = room_2.bed_end_time()start_t3 = room_3.bed_start_time()
time.sleep(7)
end_t3 = room_3.bed_end_time()start_t4 = room_4.bed_start_time()
time.sleep(1)
end_t4 = datetime.datetime.now()#amount of time taken to clean the room
elapsed_t1 = end_t1 - start_t1
elapsed_t2 = end_t2- start_t2
elapsed_t3 = end_t3- start_t3
elapsed_t4 = end_t4- start_t4#total time to clean all rooms
total_time = elapsed_t1 + elapsed_t2 + elapsed_t3 + elapsed_t4#avg time to clean a room
avg_time = total_time / 4#sort list by length of time. longest time is first.
sorted_rooms_by_time_ = []
times = [elapsed_t1, elapsed_t2, elapsed_t3, elapsed_t4]sorted_times = sorted(times, reverse=True)
print(sorted_times)print('Start Time 1:\n',start_t1.time(),'\n')
print('End Time 1:\n',end_t1.time(),'\n')
print('Elapsed Time 1:\n',elapsed_t1,'\n')
print('Elapsed Time 2:\n', elapsed_t2,'\n')
print('Elapsed Time 3:\n', elapsed_t3,'\n')
print('Total Time:\n',total_time,'\n')
print('Avg Time:\n',avg_time,'\n')print(room_with_stimes_dict)
-
@ccc I was wondering why you recommend avoiding strftime()
-
This post is deleted!