0

When I try to test my /articles route with the postal code 02138 (or any other code for that matter) I get something along the lines of this:

KeyError: '02138' at line 43:

cache = lookup(str(coordinates))

articles function:

@app.route("/articles")
def articles():
"""Look up articles for geo."""

# cannot use try here unfortunately
if not request.args.get("geo"):
    raise RuntimeError("invalid lat/lng")

coordinates = request.args.get("geo")

# now use lookup (in helpers.py) to return newsfeed
cache = lookup(str(coordinates)) 

return jsonify([cache])

If I didn't know better, I would have thought the error would be with jsonify since I don't know if I entirely understand how it works (I found flask's documentation to be a little cryptic on it), but instead it is this line.

The reason I cast coordinates to a string was to make sure that they are not numeric as per dino's answer here:

pset8 addMarker can't get articles

EDIT

I changed the last line of my code to be

return jsonify(cache)

Based on some answers I read from Dino, but since the problem is with the line previous, it hasn't really fixed anything.

Here is the traceback error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1994, in 
__call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1985, in 
wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1540, in 
handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.4/dist-packages/flask/_compat.py", line 33, 
in reraise
    raise value
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1982, in 
wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1614, in 
full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1517, in 
handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.4/dist-packages/flask/_compat.py", line 33, 
in reraise
    raise value
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1612, in 
full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1598, in 
dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ubuntu/workspace/pset8/mashup/application.py", line 43, in 
articles
    cache = lookup(str(coordinates))
  File "/home/ubuntu/workspace/pset8/mashup/helpers.py", line 9, in lookup
    return lookup.cache[geo]
KeyError: '02138'
6
  • Do I understand correctly: when you request https://ide50-username.cs50.io/articles?geo=02138 in the browser (your username of course), you get a KeyError? Commented Jul 10, 2017 at 12:58
  • Yes you are spot on Commented Jul 10, 2017 at 19:26
  • I do not see any way that request will give that error on that line in that code. And believe me, I've tried. Can you paste the traceback from the error? (If you click on Traceback at the top of the error it will give a Copy/Paste friendly version. ) Commented Jul 11, 2017 at 0:32
  • And just for grins, what is the result for ?geo=London or geo=NJ ? Commented Jul 11, 2017 at 12:06
  • 1
    If this happens with a request to articles, the table is not involved. Notice, the error comes from helpers.py (not articles), line 9. Which begs the question: how does it "find" geo in lookup.cache line 8 if geo in lookup.cache: and then NOT "find" if on line 9?....Aha, found it! Answer forthcoming. Commented Jul 11, 2017 at 20:39

1 Answer 1

1

There's a "problem" in the distro code. I just downloaded it and compared it to my (working) version, which was downloaded.... some time back. Line 8 needs to be uncommented, and line 9 needs to be properly indented. Like so:

# check cache for geo <-- LINE 7
if geo in lookup.cache:
    return lookup.cache[geo]

That appears to be the only difference between the codes.

6
  • Hmm, I'm not sure what version of distribution code I got, it seems to have neither of those errors but instead switched the if condition from true to false like this: # check cache for geo if not geo in lookup.cache: return lookup.cache[geo] Commented Jul 11, 2017 at 20:44
  • Sorry, I don't know how to format code in a comment, but I've tried all of the examples of postal codes in the spec as well as 'NJ' and 'London' and both work as intended now. Thanks for taking so much time to look into this for me. I would have thought that all of the distribution code was normalized though, or do they continue to modify it? Commented Jul 11, 2017 at 20:47
  • Sometimes when there is prep going on for next Harvard session (in this case Fall 2017), there is some "confusion". Commented Jul 11, 2017 at 20:49
  • I guess that would make sense if multiple people are working on the same folder, as I just learned in the git seminar on merge conflicts. Commented Jul 11, 2017 at 20:51
  • Oh yeah, and congrats on 15k rep, you definitely deserve it. Commented Jul 11, 2017 at 20:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .