3
\$\begingroup\$

This code gives me info of a movie and the movies directed/acted by corresponding director and actors. It is working as the way I expected but I included lots of try and except statements and as a result my code is slow. Is there anyway to speed this up?

from imdb import IMDb


def rating(name):
    p1 = ia.get_movie(ia.search_movie(str(name))[0].getID())
    return p1
ia = IMDb()
a = ['Gravity', 'Argo']
i = 0
for i in a:
    movie_list = ia.search_movie(i)
    # print movie_list
    k = movie_list[0].getID()
    movie = ia.get_movie(k)
    movie_rating = movie['rating']
    print "Name of the movie: ", movie
    for i in movie['director']:
        print "Director: ", i
        director = ia.search_person(i["name"])[0]
        ia.update(director)
        print "Movies directed by %s:" % director
        for movie_name in director["director movie"]:
            p3 = rating(movie_name)
            try:
                p3['rating']
                print movie_name, "\t", p3['rating'], "\t", p3['year'], "\t", p3['genre'][0]
            except:
                print movie_name, "unrated", "\t", p3['year'], "\t", p3['genre'][0]


    actor = movie['cast']
    print "Cast: "
    for ie in actor[:2]:
        print "\t", ie
        for j in ia.search_person(str(ie))[:1]:
            full_person = ia.get_person(j.getID(), info=["filmography"])
            try:
                # b = full_person["actor"]
                bb = full_person["actor"]

            except:
                bb = full_person["actress"]
                print "Movies Acted by %s:" % j
                for movie_name in bb:
                    p3 = rating(movie_name)
                    try:
                        p3['rating']
                        try:
                            p3['year']
                            try:
                                p3['genre']
                                print movie_name, "\t", p3['rating'], "\t", p3['year'], "\t", p3['genre'][0]
                            except:
                                print movie_name, "\t", p3['rating'], "\t", p3['year'], "\t", "no genre"
                        except:
                            try:
                                p3['genre']
                                print movie_name, "\t", p3['rating'], "\t", "No release year", "\t", p3['genre'][0]
                            except:
                                print movie_name, "\t", p3['rating'], "\t", "No release year", "\t", "no genre"
                    except:
                        try:
                            p3['year']
                            try:
                                p3['genre']
                                print movie_name, "\t", "No rating", "\t", p3['year'], "\t", p3['genre'][0]
                            except:
                                print movie_name, "\t", "No rating", "\t", p3['year'], "\t", "no genre"
                        except:
                            try:
                                p3['genre']
                                print movie_name, "\t", "No rating", "\t", "No release year", "\t", p3['genre'][0]
                            except:
                                print movie_name, "\t", "No rating", "\t", "No release year", "\t", "no genre"
            else:
                print "Movies Acted by %s:" % j
                for movie_name in bb:
                    p3 = rating(movie_name)
                    try:
                        p3['rating']
                        try:
                            p3['year']
                            try:
                                p3['genre']
                                print movie_name, "\t", p3['rating'], "\t", p3['year'], "\t", p3['genre'][0]
                            except:
                                 print movie_name, "\t", p3['rating'], "\t", p3['year'], "\t", "no genre"
                        except:
                            try:
                                p3['genre']
                                print movie_name, "\t", p3['rating'], "\t", "No release year", "\t", p3['genre'][0]
                            except:
                                print movie_name, "\t", p3['rating'], "\t", "No release year", "\t", "no genre"
                    except:
                        try:
                            p3['year']
                            try:
                                p3['genre']
                                print movie_name, "\t", "No rating", "\t", p3['year'], "\t", p3['genre'][0]
                            except:
                                 print movie_name, "\t", "No rating", "\t", p3['year'], "\t", "no genre"
                        except:
                            try:
                                p3['genre']
                                print movie_name, "\t", "No rating", "\t", "No release year", "\t", p3['genre'][0]
                            except:
                                print movie_name, "\t", "No rating", "\t", "No release year", "\t", "no genre"
\$\endgroup\$
3
\$\begingroup\$

In terms of removing the awkward try: except: sections, note that the following achieves the same outcome:

rating = p3.get('rating', 'No rating')
year = p3.get('year', 'No release year')
genre = p3.get('genre', ['No genre'])[0]

print '\t'.join((movie_name, rating, year, genre))

Your rating function is:

  1. Badly named (it doesn't just return the rating, it returns a dictionary of movie information);
  2. Reliant on the global object ia being accessible; and
  3. Lacking any documentation to explain what it does.

You also have similar functionality that isn't, for whatever reason, encapsulated in a function.


You have too much code at the top level of your script. You should wrap it all in a function called e.g. main (as a bare minimum - much better would be restructuring it to multiple short, single-purpose functions) and add:

if __name__ == '__main__':
    main()

to the bottom of the script. This allows you to easily import the functionality you've developed elsewhere later.


Many of your variables are not very clearly named (ie? ia? i? a?) - please adopt meaningful names, it will make your code so much easier to follow and understand (which is good for you, too!)


There is no need to "initialise" i in the following:

i = 0
for i in a:

indeed, if you're going to, 0 is a really bad choice, as all of the actual values i takes are strings, not integers.

\$\endgroup\$
  • \$\begingroup\$ Really appreciate your time for looking into the code and providing insights. I am a newbie to Python and trying to learn. I'll definitely incorporate your suggestions and tips... Thank you \$\endgroup\$ – Prabhat Kumar May 3 '15 at 23:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.