Day 26 - Brandywine Refactoring

I spent a few cycles yesterday refactoring my brandywine code:

Areas to note:

format json response method

def format_json_response(self, response, infotype):
        format the json response from the Rotten Tomatoes API call
        if infotype == 'title':
            movies = []
            for movie in response['movies']:
                movies.append('==> ' + movie['title'] + \
                    ', ID: ' + str(movie['id']) + \
                    ', Rating: ' + str(movie['ratings']['critics_rating']) + \
                    ', Score: ' + str(movie['ratings']['critics_score']))

            # Remove unicode
            movie_titles = [title.encode('utf-8') for title in movies]
            for movie in movie_titles: print movie
        elif infotype == 'score':
            print 'you asked for a score'
            for movie in response['movies']:
                for entry in movie:
                    print entry + ': ' + str(movie[entry])

As I stated in yesterday’s post, I’ve found a useful way to reuse this method to format the data returned from any and all json responses. I’ve only got a few of what I’m calling ‘infotypes’ defined, where ‘infotypes’ define the specific information you’re looking for from the json response. In the code I’ve written so far, the title infotype returns the title of the movie, the critics rating, and the critics score from the json response. I’m hoping to tweak this more as I continue to refine and refactor the code.

Jason T Clark

Jason T Clark

Father. Musician. Gamer. Coder.

comments powered by Disqus
rss facebook twitter github youtube mail spotify instagram linkedin