forked from phalt/swapi-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
242 lines (163 loc) · 5.98 KB
/
Copy pathmodels.py
File metadata and controls
242 lines (163 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import ujson as json
import time
import six
try:
from swapi.utils import query
except:
from utils import query
class BaseModel(object):
def __init__(self, raw_data):
json_data = json.loads(raw_data)
for key, value in six.iteritems(json_data):
setattr(self, key, value)
class BaseQuerySet(object):
def __init__(self):
self.items = []
def order_by(self, order_attribute):
''' Return the list of items in a certain order '''
to_return = []
for f in sorted(self.items, key=lambda i: getattr(i, order_attribute)):
to_return.append(f)
return to_return
def count(self):
''' Get the number of items in this queryset'''
return len(self.items)
def iter(self):
''' A generator that returns each resource in self.items '''
for i in self.items:
yield i
class StarshipQuerySet(BaseQuerySet):
def __init__(self, list_of_urls):
super(StarshipQuerySet, self).__init__()
for url in list_of_urls:
response = query(url)
self.items.append(Starship(response.content))
def __repr__(self):
return '<StarshipQuerySet - {0}>'.format(str(len(self.items)))
class Starship(BaseModel):
def __init__(self, raw_data):
super(Starship, self).__init__(raw_data)
def __repr__(self):
return '<Starship - {0}>'.format(self.name)
def get_films(self):
return FilmQuerySet(self.films)
def get_pilots(self):
return PeopleQuerySet(self.pilots)
class VehicleQuerySet(BaseQuerySet):
def __init__(self, list_of_urls):
super(VehicleQuerySet, self).__init__()
for url in list_of_urls:
response = query(url)
self.items.append(Vehicle(response.content))
def __repr__(self):
return '<VehicleQuerySet - {0}>'.format(str(len(self.items)))
class Vehicle(BaseModel):
def __init__(self, raw_data):
super(Vehicle, self).__init__(raw_data)
def __repr__(self):
return '<Vehicle - {0}>'.format(self.name)
def get_films(self):
return FilmQuerySet(self.films)
def get_pilots(self):
return PeopleQuerySet(self.pilots)
class FilmQuerySet(BaseQuerySet):
def __init__(self, list_of_urls):
super(FilmQuerySet, self).__init__()
for url in list_of_urls:
response = query(url)
self.items.append(Film(response.content))
def __repr__(self):
return '<FilmQuerySet - {0}>'.format(str(len(self.items)))
class Film(BaseModel):
def __init__(self, raw_data):
super(Film, self).__init__(raw_data)
def __repr__(self):
return '<Film - {0}>'.format(self.title)
def get_starships(self):
return StarshipQuerySet(self.starships)
def get_characters(self):
return PeopleQuerySet(self.characters)
def get_vehicles(self):
return VehicleQuerySet(self.vehicles)
def get_planets(self):
return PlanetQuerySet(self.planets)
def get_species(self):
return SpeciesQuerySet(self.species)
def gen_opening_crawl(self):
''' Return a generator yielding each line of the opening crawl'''
for line in self.opening_crawl.split('\n'):
yield line
def print_crawl(self):
''' Print the opening crawl one line at a time '''
print("Star Wars")
time.sleep(.5)
print("Episode {0}".format(self.episode_id))
time.sleep(.5)
print("")
time.sleep(.5)
print("{0}".format(self.title))
for line in self.gen_opening_crawl():
time.sleep(.5)
print(line)
class PlanetQuerySet(BaseQuerySet):
def __init__(self, list_of_urls):
super(PlanetQuerySet, self).__init__()
for url in list_of_urls:
response = query(url)
self.items.append(Planet(response.content))
def __repr__(self):
return '<PlanetQuerySet - {0}>'.format(str(len(self.items)))
class Planet(BaseModel):
def __init__(self, raw_data):
super(Planet, self).__init__(raw_data)
def __repr__(self):
return '<Planet - {0}>'.format(self.name)
def get_films(self):
return FilmQuerySet(self.films)
def get_residents(self):
return PeopleQuerySet(self.residents)
class SpeciesQuerySet(BaseQuerySet):
def __init__(self, list_of_urls):
super(SpeciesQuerySet, self).__init__()
for url in list_of_urls:
response = query(url)
self.items.append(Species(response.content))
def __repr__(self):
return '<SpeciesQuerySet - {0}>'.format(str(len(self.items)))
class Species(BaseModel):
def __init__(self, raw_data):
super(Species, self).__init__(raw_data)
def __repr__(self):
return '<Species - {0}>'.format(self.name)
def get_films(self):
return FilmQuerySet(self.films)
def get_people(self):
return PeopleQuerySet(self.people)
def get_homeworld(self):
response = query(self.homeworld)
return Planet(response.content)
class PeopleQuerySet(BaseQuerySet):
def __init__(self, list_of_urls):
super(PeopleQuerySet, self).__init__()
for url in list_of_urls:
response = query(url)
self.items.append(People(response.content))
def __repr__(self):
return '<PeopleQuerySet - {0}>'.format(str(len(self.items)))
class People(BaseModel):
''' Representing a single person '''
def __init__(self, raw_data):
super(People, self).__init__(raw_data)
def __repr__(self):
return '<Person - {0}>'.format(self.name)
def get_starships(self):
return StarshipQuerySet(self.starships)
def get_films(self):
return FilmQuerySet(self.films)
def get_vehicles(self):
return VehicleQuerySet(self.vehicles)
def get_homeworld(self):
response = query(self.homeworld)
return Planet(response.content)
def get_species(self):
return SpeciesQuerySet(self.species)