0

I'm working on pset 8 and I have a two part question. First, I've tried to implement the articles function as instructed in the mashup pset documentation but I get a builtins.TypeError TypeError: 'list' object is not callable when trying to use the example urls such as: https://ide50-username.cs50.io/articles?geo=02138.

This is my code:

@app.route("/articles", methods=["GET", "POST"])
def articles():
    """Look up articles for geo."""
    if request.method == "GET":
        if not request.args.get("geo"):
            raise RuntimeError("Need geo argument")
        else:
            request.args.get("geo")
            articles_location = lookup("geo")
            return articles_location

        return jsonify([articles_location])

My second question is about the search function. This function actually kind of works. I can use the sample urls if they include only one search parameter such as https://ide50-username.cs50.io/search?q=02138 but when I try to search for multiple things at the same time (ex. Cambridge+Massachusetts) I just get an empty JSON back. I think I need to update my select from statement using AND and OR but I don't know how to include all the ANDs and ORs I think I might need without cancelling each other out. For example wouldn't having: SELECT * FROM places WHERE postal_code LIKE :q AND place_name LIKE :q OR place_name LIKE :q result in the search results only being returned where postal_code and place_name are similar to q. I'm not sure and get confused by the wording. Anyway here is the code for my search function:

@app.route("/search")
def search():
    """Search for places that match query."""
    q = request.args.get("q") + "%"
    search_location = db.execute("SELECT * FROM places WHERE postal_code LIKE :q OR place_name LIKE :q OR admin_name1 LIKE :q", q=q)

    return jsonify([search_location])

Thanks in advance!

EDIT: So I edited articles but now I get a ValueError: View function did not return a response. I think I did what you were saying but now I'm not sure what I should do to fix this. I also tried out your suggestion for my search function but it would only work when searching for multiple things at once and even then having multiple search parameters wouldn't narrow down the search responses like it should have. I decided to go with my original code for that part and just to be satisfied that something I wrote actually sort of works for once. I do appreciate your help though. Here is the articles code:

@app.route("/articles", methods=["GET", "POST"])
def articles():
    """Look up articles for geo."""
    if request.method == "GET":
        if not request.args.get("q"):
            raise RuntimeError("Need geo argument")
    else:
        geo = request.args.get("q") + "%"
        articles_location = lookup(request.args.get("geo"))

        return jsonify([articles_location])

1 Answer 1

0

"geo" is a string literal. So articles_location = lookup("geo") isn't what you want. It is essentially calling this "https://news.google.com/news?geo=geo&output=rss". [edit] You want something like lookup(request.args.get("geo")). (Is it possible you forgot the geo = on the line before?)

And beware, program will not reach return jsonify([articles_location]) because it has already returned in the else branch.

The problem with search isn't anding and oring. In the example "Cambridge Massachsetts", the "q" doesn't match postal_code, place_name or admin_name1. As written it should work with input "Cambridge" (that will match place_name) or input "Massachusetts" (admin_name1), but not when the input is combined. You'll have to decide how much parsing of the input you want to do and how many tests in your sql (or how many distinct sqls) you want to include. For instance, how to handle "New York New York"; where does place_name end and admin_name1 begin?

[edit] By "parsing the input" I mean splitting q into the separate parts that would match place_name and admin_name1. Assuming the user enters, "city state", you could split q into separate words (eg names = q.split(" ") and match on the appropriate column in the sql eg:

SELECT * FROM places WHERE postal_code LIKE :q OR place_name LIKE :city OR admin_name1 LIKE :state", q=q, city=names[0], state=names[1])

It starts to get really tricky if the place name is more than one word (Point Pleasant Beach) and/or the state name is more than one word (New Jersey).

If the user enters "city,state" (ie comma delimited) you'd have a fighting chance, because then you could split on the comma.

BTW the staffs implementation returns an empty array when the query string is "Cambridge Massachusetts"

8
  • I changed my code to this where q is query from the script.js similar to what I had in my search: @app.route("/articles", methods=["GET", "POST"]) def articles(): """Look up articles for geo.""" q = request.args.get("q") if request.method == "GET": if not request.args.get("q"): raise RuntimeError("Need geo argument") else: articles_location = lookup("q") return jsonify([articles_location]) But now I get a runtime error
    – Dave G
    Commented Mar 27, 2017 at 22:28
  • Also for search function do you mean that if I wanted to parse more than one input I should add an additional one of these: q = request.args.get("q") + "%" or should I add on to it like this q =request.args.get("q") + "%" + "%"
    – Dave G
    Commented Mar 27, 2017 at 22:34
  • More info added to answer. Commented Mar 28, 2017 at 2:40
  • Edited my original response with updated code
    – Dave G
    Commented Mar 28, 2017 at 19:59
  • The query argument for articles is "geo" not "q". Looks like you left some troubleshooting artifacts in there. Or put another way: There is no "q" in articles :) Commented Mar 28, 2017 at 20:44

You must log in to answer this question.

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