[Tutor] Python 3: List Comprehensions + range

Juan Christian juan0christian at gmail.com
Sun Sep 21 16:18:46 CEST 2014


I have a ' self.size = json['number_of_seasons'] ' that equals 5 (already
checked), the problem is that the API have some series with special
seasons, aka, season_number = 0, and I don't want to get those.

I have the following list comp: self.season = [Season(ID, season, self) for
season in range(1, self.size + 1)]

Let's say I want Breaking Bad, 5 seasons, but this serie has a special
season 0. Reading the list comp, I get a range from 1 (in order to ignore
season 0) to 5 (range is half-open, so I need size + 1, maybe later I can
implement a closed_range, but I don't think this will be necessary as this
is the only place I'll ever use this). Then I get the current number and
hold it on the var season, then I call Season() with this number, but I'm
still fetching episodes from the season 0 and I don't know why!

Code:

import requests
from consts import API
from person import Person
from character import Character


class Serie:
def __init__(self, ID):
response = requests.get('{query}/{id}?api_key={key}'.format(query =
API.QUERY_SERIE, id = ID, key = API.KEY))
json = response.json()

self.id = ID
self.title = json['name']
self.date = json['first_air_date']
self.size = json['number_of_seasons']
self.season = [Season(ID, season, self) for season in range(1, self.size +
1)]


class Season:
def __init__(self, ID, season, serie):
response =
requests.get('{query}/{id}/season/{s}?api_key={key}'.format(query =
API.QUERY_SERIE, id = ID, s = season, key = API.KEY))
json = response.json()

self.number = season
self.serie = serie
self.date = json['air_date']
self.episode = [Episode(ID, episode['episode_number'], self) for episode in
json['episodes']]
self.size = len(self.episode)


class Episode:
def __init__(self, ID, episode, season):
response =
requests.get('{query}/{id}/season/{s}/episode/{e}?api_key={key}'.format(query
= API.QUERY_EPISODE, id = ID, s = season, e = episode, key = API.KEY))
json = response.json()
print(json)
print("\n")
self.number = episode
self.title = json['name']
self.date = json['air_date']

response =
requests.get('{query}/{id}/season/{s}/episode/{e}/credits?api_key={key}'.format(query
= API.QUERY_EPISODE, id = ID, s = season, e = episode, key = API.KEY))
json = response.json()

self.cast = [Character(person['character'], Person(person['id'])) for
person in json['cast']]
self.cast_size = len(self.cast)


s = Serie("1396")
print("DEBUG: " + s.title + " : " + s.id + " : " + s.date + " : " + s.size)


print(json) is giving me:

{'vote_average': 0.0, 'id': 62131, 'air_date': '2009-02-17', 'still_path':
'/t729tFVXPetnJlJ2VsUZQz0rX6v.jpg', 'overview
': "Hank and Marie try to spice up their relationship on Valentine's Day.",
'episode_number': 1, 'production_code': None
, 'season_number': 0, 'vote_count': 0, 'name': 'Good Cop Bad Cop'}


{'vote_average': 0.0, 'id': 62133, 'air_date': '2009-02-17', 'still_path':
'/wT62P6ZnjKgZXk0M5hHl4e5zSjB.jpg', 'overview
': 'Walt and Hank have a talk before Hank gets married.', 'episode_number':
2, 'production_code': None, 'season_number':
 0, 'vote_count': 0, 'name': 'Wedding Day'}


{'vote_average': 0.0, 'id': 62132, 'air_date': '2009-02-17', 'still_path':
'/gUHAqFw3Ptzya96JFFE9xVfMjze.jpg', 'overview
': 'Jesse and Badger make a behind the scenes video about their band
"TwaughtHammer" and show a music video for their so
ng "Fallacies."', 'episode_number': 3, 'production_code': None,
'season_number': 0, 'vote_count': 0, 'name': 'TwaughtHam
mer'}

[...]

As you can see, 'season_number': 0, and I want 1 and above only.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140921/92c6dd6f/attachment.html>


More information about the Tutor mailing list