IBM / elasticsearch-spark-recommender

Use Jupyter Notebooks to demonstrate how to build a Recommender with Apache Spark & Elasticsearch
https://developer.ibm.com/code/patterns/build-a-recommender-with-apache-spark-and-elasticsearch/
Apache License 2.0
838 stars 266 forks source link

Fix unbound local var error for APIKeyError #17

Closed MLnick closed 6 years ago

MLnick commented 6 years ago

Resolves #16

MLnick commented 6 years ago

cc @rhagarty

rhagarty commented 6 years ago

This doesn't fix the issue for me. The API-KEY is not the issue, it's: import tmdbsimple as tmdb

I do not have tmdbsimple installed on my machine. Since loading it is 'optional', we should either 1) handle it not being there 2) make it mandatory that the user install it

Here are the errors I'm getting related to this -

1) error when performing the optional step to load the tmdb api (which is reasonable error): NameError Traceback (most recent call last)

in () 10 movie_poster_url = IMAGE_URL + movie_info['poster_path'] 11 display(Image(movie_poster_url, width=200)) ---> 12 except ModuleNotFoundError: 13 print("Cannot import tmdbsimple, no movie posters will be displayed!") NameError: name 'ModuleNotFoundError' is not defined 2) error in '5(a) find similar' recommended movie: NameError Traceback (most recent call last) in () ----> 1 display_similar(2628, num=5) in display_similar(the_id, q, num, index, dt) 145 """ 146 movie, recs = get_similar(the_id, q, num, index, dt) --> 147 q_im_url = get_poster_url(movie['tmdbId']) 148 if q_im_url == "NA": 149 display(HTML("Cannot import tmdbsimple. No movie posters will be displayed!")) in get_poster_url(id) 13 except APIKeyError as ae: 14 return "KEY_ERR" ---> 15 except ModuleNotFoundError as me: 16 return "NA" 17 NameError: global name 'ModuleNotFoundError' is not defined 3) error in '5(b) find movies' recommended movie: UnboundLocalError Traceback (most recent call last) in () ----> 1 display_user_recs(12, num=5, num_last=5) in display_user_recs(the_id, q, num, num_last, index) 103 # check that posters can be displayed 104 first_movie = user_movies[0] --> 105 first_im_url = get_poster_url(movie['tmdbId']) 106 if q_im_url == "NA": 107 display(HTML("Cannot import tmdbsimple. No movie posters will be displayed!")) UnboundLocalError: local variable 'movie' referenced before assignment
MLnick commented 6 years ago

The 2nd error is a bug - will fix that.

MLnick commented 6 years ago

The 1st error is really weird - it is like it can't actually catch the module not found exception.

What environment are you using (Python version etc)?

The only other solution would be to just catch Exception there instead.

I'd make the API access mandatory - but I actually sort of forgot that you have to fill in that form and wait for an API key... it is some friction that I worry would deter quite a few potential users. So first prize will be to correctly handle it with no API access.

rhagarty commented 6 years ago

I'm using Python 2.7.10 I don't have 3.X installed

rhagarty commented 6 years ago

Yeah, you need to handle just Exception

exception ModuleNotFoundError A subclass of ImportError which is raised by import when a module could not be located. It is also raised when None is found in sys.modules.

New in version 3.6.

rhagarty commented 6 years ago
def get_poster_url(id):
    """Fetch movie poster image URL from TMDb API given a tmdbId"""
    IMAGE_URL = 'https://image.tmdb.org/t/p/w500'
    try:
        import tmdbsimple as tmdb
        from tmdbsimple import APIKeyError
        try:
            movie = tmdb.Movies(id).info()
            poster_url = IMAGE_URL + movie['poster_path'] if 'poster_path' in movie and movie['poster_path'] is not None else ""
            return poster_url
        except APIKeyError as ae:
            return "KEY_ERR"
    except Exception as ex:
        return "NA"

This fixed the 5(a) recommendation errors for me. I see the movie titles display correctly.

Still get the 5(b) errors.

MLnick commented 6 years ago

@rhagarty latest commit should fix the 5(b) errors.

MLnick commented 6 years ago

@rhagarty and also pushed commit to use Exception instead of ModuleNotFoundError.

Hopefully this should all work for you now!