LPTHW - Ex52 - paths and directions in planisphere.py

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!

Those functions should be configuring the map, so if they’ve been commented out then I think you missed something.

Do you have your code posted somewhere? It’s probably too much to paste here but I can look if you have it somewhere.

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.

Try putting it in here:

https://paste.learnjsthehardway.com/

Then paste the link. But, you really should try to figure this out on your own for a few more days. The question you should be asking is:

Am I actually running this code?

You can answer that by using print() to log what functions are actually running.

Ok, thanks, I will circle back to you after exhausting my resources discovering logging function.

Well, remember “print() to log” just means doing this:

print("somevar=", somevar)

Do that at the top of your functions and you’ll start to see what’s going on.

https://paste.learnjsthehardway.com/zirozalubu.py I pasted the code on this link.

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.
  • Debug mode: off
  • Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
    127.0.0.1 - - [18/Aug/2020 12:21:17] “GET / HTTP/1.1” 302 -
    room_name >> central_corridor
    room>>> <gothonweb.planisphere.Room object at 0x7fe29e359f90>
    127.0.0.1 - - [18/Aug/2020 12:21:17] “GET /game HTTP/1.1” 200 -

planisphere.py is in the link while app.py and other html pages are exactly the same as in Ex52.

Try turning debug on in Flask. Might help see what’s happening…

For Linux, Mac, Linux Subsystem for Windows, Git Bash on Windows, etc.:

export FLASK_ENV=development

For Windows CMD, use set instead of export:

set FLASK_ENV=development

For PowerShell, use $env

$env:FLASK_ENV = "development"

@Saksham What exactly is your question?

@Saksham I get it now…

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.

source is O’reilly book -

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):

class Room(object):
    def __init__(...):
        ...

    def go(...):
        ...

    def add_paths(...): 
        ...

def load_room(...):
    ...

def name_room(...): 
    ...

There is no direction function. add_paths is a method that used in the testing module.

Somewhere you’ve got files mixed up I think. I would delete 99% of the Room instance description text so you can see the structure more clearly.

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!

No because add paths IS on the code and you need that to ‘add paths’!

Ok thanks, just got confused with the North, West, East and Down directions in the add_paths test.

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?

https://docs.python.org/3/library/pdb.html

Thanks a ton @gpkesley for detailed explanation.

Thanks Zed for giving a direction, yeah i will play with this for a while.

1 Like