I am going through exercise 52 and was stuck with the ‘add.paths’ function in the ‘Room’ class. I don’t understand the concept of ‘direction’ function either in the planisphere.py ‘room_paths’ about center, west, east and down.
Moreover, I can run the program without issues if I disable the below part -
def add_paths(self, paths):
self.paths.update(paths)
#class room_paths(object):
def direction(self):
start.add_paths({‘west’: east, ‘down’: down})
start = start.go(‘west’).go(‘east’)
Kindly explain the significance of the above code. Thanks!
Please share me your email ID, so that I can send a snippet. Thanks!
I found that without below part, my app.py was not running, and was looking for ‘add_paths’ function.
def add_paths(self, paths):
self.paths.update(paths)
But the section that I am confused about is the ‘direction’, east-west-down stuff.
def direction(self):
start.add_paths({‘west’: east, ‘down’: down})
start = start.go(‘west’).go(‘east’)
Disabling preceding 3 lines, does not make any difference.
You may see a “function direction” variable that gets printed as soon as room_paths is called. But after that , there is no sign of “direction” function. Only the “name_room” print() gets printed.
My output on screen -
(base) Sakshams-MacBook-Pro:gothonweb saksham$ python app.py function direction >>
Serving Flask app “app” (lazy loading)
Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
You are saying that you define a function called ‘direction’ that is never used. I’ve check the html code for this chapter and there isn’t even this function in the code. What source are you using for this? PDF, HTML, book?
Thanks for checking and yes, I think I typed that function incorrectly in Ex52, however, there is a planisphere_test file which has the following code and I am not sure what it’s doing in testing.
def test_room_paths():
12 center = Room(“Center”, “Test room in the center.”)
13 north = Room(“North”, “Test room in the north.”)
14 south = Room(“South”, “Test room in the south.”)
15
16 center.add_paths({‘north’: north, ‘south’: south})
17 assert_equal(center.go(‘north’), north)
18 assert_equal(center.go(‘south’), south)
So, if there is no room_paths class or function, then this test would be irrelevant.
You should have the follow class and methods/functions with your Text-heavy room instances (note the … means some other stuff like code blocks and arguments):
Alright, that clears my doubt of removing direction function. It must have overlapped somewhere. But, in that case, I should remove the add_paths method from testing module.
Thanks!
Take some time to see what is going on here in the tests.
You create an instance of the Room class that includes a name, a description and an empty dictionary called paths.
Your room instance has two methods that can be called on it; ‘go’ - which takes a direction (argument not function) that is a key to return the value in the dictionary. And ‘add-paths’ which just adds to the dictionary.
So when you ‘test_room_paths’ you create an instance of Room (center) with name of ‘Center’ and a description ‘Test room in center.’
This is replicated for north and south. You then add two paths to the ‘Center’ instance dictionary, namely ‘north’ and ‘south’ that correspond to the other instances.
So when you call ‘go’ on the ‘Center’ instance, you give it a ‘direction’ as a key and it should return The value from its dictionary.
So you are asserting (testing) that the value returned from the dictionary matches your expected value - which will be the name of another instance (as this is important later).
Take some time to go back over this. It’s important.
Yes, looks like you got help for this (thanks @gpkesley) but it might help if you built the test in pieces and confirmed it was doing what you think it’s doing. Maybe do just one line of test, then use pdb to walk through it?