All language subtitles for 4. Challenge - Check if it Will Rain in the Next 12 Hours

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

0 In order to create my rain alert Python script. 1

I want the script to be run every morning at 7:00 AM. 2

Let's say that I'm going to head out of the door at 8:00 AM in order to go to 3

work, I want my script to run at 7:00 AM, 4

check the weather for the next 12 hours, 5

so the time when I'm going to be away from home, and then send me a text message 6

if it's going to rain today 7

so that I can remember to bring an umbrella. That way I'll get notified before I 8

leave home and I'll know how to prepare for the day. 9

The data that we got back from open weather map contains the hourly 10

weather forecast for the next 48 hours. 11

So it starts from zero and goes up to 47. 12

Now we're only interested in the next 12 hours 13

because, if you imagine, this script is run at 7:00 AM 14

then plus 12 hours, that's going to be 7:00 PM. By which time, 15

hopefully I'm already on the way home 16

and I don't need to worry about whether if it rains or not. That's the goal. 17

And if we head back into our code, 18

what we want to do is firstly modify certain parts. 19

Now we know that at the moment, 20

the response code that we're getting back from calling this API is 200. 21

So it's successful. So we're going to call raise for status 22

so that if there is right a problem and we don't get a 200 code that we 23

actually raise an exception. Now, 24

next we want to save our response.json as the weather data. 25

And then we're going to work with this data in order to get the particular 26

pieces that we're interested in. So if we look at this JSON that 27

we get back, firstly, we don't really care about the current weather or 28

the daily weather. 29

So when we look at the API documentation 30

which is probably the hardest part, 31

making sure that you're diligent enough to read the entire documentation, 32

there's often some good nuggets in there. For example, in this case, 33

we can add another parameter called exclude 34

which allows us to exclude some parts of the weather data that comes from the 35

API. This should speed up the API fetching process 36

and it also means that we're transferring less data across the internet. So we 37

can get rid of the current, minutely and daily. 38

We're only interested in the hourly and we have to provide this as a comma- 39

delimited list. So if we take a look at their example, you can see here, 40

they've said exclude and then they've said hourly, daily. 41

And notice how it says without spaces. So as I always say, 42

this is like going into somebody else's house. 43

Everything is different. The way that the washing machine works is different, 44

the way that their dryer works is different. 45

So you really have to look at the API documentation when you were working 46

with a new API just so that you're not caught out 47

and you understand exactly what you have to do in order to be a good API user. 48

Let's go back into our code and let's provide that extra parameter. 49

So that was called exclude. 50

And the thing that we want to exclude is going to be provided as a string. 51

So we want to get rid of current, minutely and daily. 52

So we're going to add that into the string and make sure that we separate each 53

with a comma and without spaces. 54

okay. 55

Now when we hit run 56

and when we get back our weather data and we print it out 57

and we copy this and put it into our online JSON viewer, 58

so let's replace all of the stuff that was there before 59

and you can see the JSON we're getting back is now a lot simpler. 60

We're only getting back the hourly weather 61

forecast. 62

So that's step one done. 63

The next step is to dig through this hourly forecast and get hold of the thing 64

that we're interested in, which is the actual weather condition. 65

The way that weather services tend to provide the weather condition is through 66

an ID. 67

And I know this because I read the API documentation. 68

It's not because I'm some sort of weather geek. Although, I mean, 69

that's not a bad thing to be 70

I guess. If we take a look inside the API key, 71

if we scroll down in this documentation past the examples, 72

you can see it provides all of the fields in the API response. So these are 73

all of the things that we could possibly get back and what they mean. 74

And you can see there's some really interesting things like the UV index or you 75

can get, um, 76

what does the temperature feel like based on the wind chill and actual ground 77

temperature. 78

But what we're mostly interested in is the hourly data. 79

And then inside that the hourly weather data. 80

So this hourly.weather.id is a weather condition id. 81

And when you click on that link, 82

it takes you to this table that shows you all of the weather condition codes 83

that we could possibly get back in this particular field. 84

Now you can see that all the codes that start off with a two means some sort of 85

thunderstorm, and then starting with three 86

that means some sort of drizzling starting, with five means rain, starting with 87

six means snow. And then afterward we have the seven hundreds 88

so these are atmospheric. Thing's like a bit of mist, a bit of smoke, 89

a bit of dust or fog. And this is also incidentally, 90

the reason why this weather key actually has a value that's in the form of 91

a list. You can see that's denoted by the square brackets here. 92

So there could actually be multiple weather conditions for a particular place 93

at a particular hour. And that's because you could maybe have, um, 94

snow, but you could also have fog at the same time. Now, 95

when I looked through a lot of the examples and the documentation, 96

it seems like the first item in that list is the main condition. 97

So if it's going to rain, then it's going to be in that first item 98

in the list of weather conditions. Inside that list, 99

we have a dictionary or many dictionaries. 100

Each of those contain a weather condition ID, 101

the main condition name and the description. 102

So if we look at this ID code 802, 103

we can decode it in this table and you can see it means scattered clouds, 104

25 to 50% of the sky is covered in clouds basically. 105

Based on this list, 106

we can say that well anything that has a code less than 700, 107

then we probably will need an umbrella. 108

I'm not sure how you stand on the umbrella in snow situation 109

but I personally do like to hold an umbrella when it's snowing, 110

especially because I live in a country where the snow is not crazy. 111

It's just sort of, 112

it's never sort of the beautiful snow where it's thick and it gets caught on 113

your eyelashes. It's is the sort of annoying slush 114

that's just sort of snow, but it sort of like somebody spitting at you. 115

So in my case, I would prefer to have an umbrella if it was going to snow. 116

I'm going to check for the codes that we get back from the open weather map 117

API, and if the code is less than 700, 118

then I'm going to advise my user to bring an umbrella. 119

Here's a challenge for you. 120

Can you figure out how to look through the data that we get back, 121

which remember looks something like this, get hold of 122

the first 12 123

items from that hourly list. 124

And then look at the weather and the first item in the weather list and also the 125

ID. So you are going to go all the way down to this, but remember, 126

you're going to do that for all of the items 127

and you're going to check for the condition code. Now, 128

if any of those ID codes are less than 700 then you want to be able to print 129

out, bring an umbrella. 130

So that's the goal, and the actual implementation 131

I'll leave up to you because there's quite a few ways that you can do this. 132

But I'm sure by now you're well prepared with all of your tools in Python to 133

figure this out. If you want to make sure that your code is actually working, 134

you can switch to a latitude and longitude that is definitely raining. 135

So if you go to ventusky.com, 136

they actually show you the live weather forecast. 137

So we can look at precipitation, which is basically rain. 138

And we can find some sort of unfortunate place 139

which seems to be really heavily raining. For example, 140

this place in Poland, Lodz, 141

and if I spell that name correctly, 142

right, 143

Lodz, and then I'll add the country code Poland. 144

And just to make sure in the map 145

it's actually found the correct place. Yep that looks pretty much it. 146

Then I can switch out my latitude and longitude with this rainy place. 147

And that way I know that at least one of the results I get back is definitely 148

going to contain some sort of rain 149

right. 150

Now if we look at their hourly data, 151

you can see that the weather is basically just rain, rain, rain. 152

So between looking at places which are sunny and places 153

which are rainy, 154

you should be able to get your code to work so that it tells you in the next 12 155

hours 156

if any of those condition codes are less than 700, 157

which means it's got some form of precipitation. 158

Pause the video and give this a go. 159

Now. 160

All right. So let's narrow down into this 161

weather. If we want to get the weather for the next hour, 162

then we have to tap into the first item in our hourly list 163

and then we get hold of the weather 164

and then we get hold of the first item in that list. Under the ID key, 165

we'll get the actual value. 166

We've talked about this in detail in previous lessons. 167

If you've skipped a lot of lessons and you've come here directly, 168

then it's going to be a little bit confusing 169

and I recommend to review the previous lessons before you continue. 170

Instead of printing the weather data, let's drill down. 171

Let's get to the first item here, which is hourly. 172

If we provide a set of square brackets and then we can access the value inside 173

the hourly key like this. 174

And now we've got a list with all of the hourly data and it looks pretty much 175

like this. 176

Now you can confirm this by also pasting this into the JSON viewer, 177

replacing the previous text that was there. You can see 178

we now have our 48 items in this list of hourly data. To drill further down, 179

let's get hold of the first item in that list by providing a square bracket 180

and then the index, which is zero. 181

Now we're into the first item and this is what the data looks like. 182

It's a lot shorter. 183

Now we want to tap into the weather condition. 184

So that means yet another set of square brackets and then the name of the key, 185

which is weather. Now, it's pretty simple. 186

It's simply giving us a list with only one item. 187

Let's tap into that one item by using, again, 188

square brackets, zero. 189

And now we've got just a simple dictionary 190

essentially. So if you want to get hold of the ID, 191

then its the final square brackets and the key 192

which is ID. The current weather data ID code is 500 and that, of course, refers to 193

light rain. 194

The next problem is how do we get hold of the first 12 items that we get back in 195

our weather data? In previous lessons, we covered the Python 196

slice function 197

which could work for this by simply providing a value and slicing a sequence 198

like a list or tuple to get hold of a particular section of that list or 199

tuple. Now you can also use the Python slice operator 200

which is the square bracket and a colon 201

and this is probably a more pythonic way of doing things. 202

And you'll see more people doing this in the wild than using the slice function. 203

So let's do it with this notation. We're going to use this version. 204

So we're going to tap into a list and then provide a colon and then where we 205

want to stop because we want to go from the beginning of the list through to 206

stop-1. 207

We know that it's this part of our code that gets us to our list of hourly 208

weather forecasts. So we want to create a slice from that data. 209

So let's copy that and comment this line out. 210

And then I'm going to create my weather slice 211

which is going to be generated from the weather data under the key called 212

hourly. If I print this, 213

you can see it starts off just simply being a list with all of the weather 214

forecasts. Now, if I go ahead and slice this, 215

I'll use that syntax that you saw earlier on where we have a square bracket, 216

a colon, and then where we want our slice to end. 217

And we want this slice to go from zero all the way up to hour 11, 218

so that's 12 hours in total. So if we want to go up to 11, 219

then we have to put 12 in here because it's going to be whatever number here, 220

minus one. And now if we print this data that we get back, 221

and 222

we replaced the existing text inside our JSON viewer, 223

then we take a look at it 224

you can see we've got the next 12 hours of weather data from 0 to 11. 225

Once we've got that slice, the next step is, well, 226

how do we get the rest of the stuff, right? 227

What if I created a new list that contains the condition codes? 228

We know that we've got a list of all of the weather conditions for the next 12 229

hours. 230

How can we loop through that list to find out the actual ID of the weather 231

condition for each of those hours? Well, 232

we can create a for loop that looks at each of the data from the hour, 233

so for hour_data in weather_slice, 234

so this is going to go through each of the 12 hours. 235

And then for each of those hours, 236

we're going to tap into the hour_data and try to get hold of the item that is in 237

the weather key. Like this. 238

Now I'm going to print each of these and you can see we've now got a list of all 239

of the weather conditions for each of those hours. 240

Now let's narrow down that a little bit further. 241

Let's get hold of the first item of each of these lists and you can see, 242

in fact, there's only one item in all of these lists. 243

It's very rare that you have multiple weather conditions for each hour. 244

Now, that gives us a Python dictionary for each of those hours 245

and we can tap into that final value we're interested in under the key id. 246

So now we've looped through the next 12 hours and got hold of the weather 247

condition id for each of those hours. Now, 248

all we need to do is to save this instead of printing it and we'll call it the 249

condition_code. 250

Yeah. 251

And remember previously we said that if the condition code is less than 700, 252

then we're going to print bring an umbrella. 253

So we can check if condition_code, 254

which remember at this point is still a string 255

so we have to turn that into an integer in order to be able to compare it 256

against another number. 257

If that condition_code as an integer is less than 700, 258

then we're going to print bring an umbrella, 259

right? 260

And you can see that for the next 12 hours 261

there's six hours which are going to rain at this particular place 262

in Lodz in Poland that I've put into that latitude and longitude. 263

Now, if I don't want to call this print statement many times, 264

then we can define a variable outside 265

the for loop called will_rain and we can set that to false 266

to begin with. Now, if during the next 12 hours 267

the condition_code is less than 700, then we'll switch that to equal 268

true instead. Now, 269

after the for loop has completed, 270

then we can check to see if it will rain in the next 12 hours. And if so, 271

we'll print bring an umbrella. This way 272

we'll only get one print statement being called instead of every single time we 273

land on a condition code for rain. 274

So this involved quite a bit of JSON passing and also understanding how to work 275

with parameters in APIs. Now, 276

I hope you managed to get this far by yourself. 277

But if not, be sure to review what I've written and fix your code as required. 278

If any of it was confusing, 279

be sure to review previous lessons before you continue 280

because we're now building heavily on your previous knowledge that you learnt 281

through the course. Now in the next lesson, 282

we're going to be looking at how we can, instead of printing 283

bring an umbrella, to send a SMS text message that notifies you to bring an 284

umbrella instead. So for all of that and more, 285

I'll see you on the next lesson.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.