HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Acquiring movie info

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
infoacquiringmovie

Problem

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]

Solution

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:

  • Badly named (it doesn't just return the rating, it returns a dictionary of movie information);



  • Reliant on the global object ia being accessible; and



  • 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.

Code Snippets

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))
if __name__ == '__main__':
    main()
i = 0
for i in a:

Context

StackExchange Code Review Q#88729, answer score: 3

Revisions (0)

No revisions yet.