Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: The web is a messy place, let's clean it up
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
Ok, so we've downloaded these and we've printed them out,
0:03
let's go back to printing out the location again,
0:05
and if I go 97201 get the weather in Portland
0:08
you are going to see the print out is not lovely, here we go,
0:11
you can see this huge long stream and lots of new lines,
0:14
is actually partly cloudy bit here,
0:17
so what we want to do is clean this up and now that looks like it printed ok
0:21
but you know, as the page evolves
0:23
maybe it has like white space around the edges
0:25
this is definitely not ok and we don't really need to list the zip code
0:29
because we typed it in we just want this part
0:31
so let's work on cleanup all these pieces of text first
0:34
and then we'll pull out just the city and state.
0:37
So we can come down here and say
0:39
that the location= cleanup_text of the location
0:43
Now this is a method that we are going to write,
0:45
not sure it justifies its own method but maybe it does,
0:49
so we are going to come down here and say
0:51
first of all if you ask us to clean up text
0:53
and the text is none we try to interact with it
0:56
that is going to be a crash so we'll do a little error handle,
1:00
we'll say if not text and use the truthiness of strings,
1:04
remember if they are empty or none
1:07
they will be false so we'll just return text,
1:10
but if they are not empty then we can do some processing,
1:13
we can say text = text.strip() and strip is a built in method to the string class
1:18
that will take all the white space tabs, space, new line, \\ end type of things,
1:24
off of the front and the end, there is also an L strip and R strip, and so on
1:28
so we are going to modify this and then we'll return the text here.
1:32
The other thing that we might want to do is if you are new you are like well,
1:35
how do I know there is a strip,
1:37
when I hit. does it say strip, no,
1:40
so we can use something called type hints in Python
1:43
now these have no effect on the runtime behavior
1:46
but they will give you better intellisense
1:49
if I am certain this is a string I can say colon then a class name,
1:53
I can say str that's the class of a string in Python,
1:56
if there is an int I could say int and so on,
1:59
but I'll say str there is string
2:01
and now when I go down here and interact with this in PyCharm
2:04
it will actually give me things like strip,
2:07
they justify L strip R strip,
2:10
so PyCharm knows now that we told it hey this is a string,
2:13
it's going to give us help as if it were a string.
2:16
Again, I could pass a number here
2:18
and it's not like there will be a compile error,
2:21
remember there is no compiler,
2:23
but it does help people who use this method
2:26
as well as the tooling understand what it is you are trying to work with
2:30
so for sure it's a string we can say str
2:32
and that will give us some help if you want it.
2:35
So let's continue here, and do this for the condition,
2:41
do this for the temp and finally the scale.
2:45
Those look like they are ok
2:48
but you never know what is going to happened to them.
2:50
Now if we run this again, we'll be in a slightly better place.
2:54
Ok, look, partly cloudy, here is the temperature
2:57
the scale and then we still have this,
2:59
but that's much better than that huge long ten line bit of text.
3:04
So the last thing we need to do is actually pull that piece out.
3:08
So we want to go down here and say we want to do one more thing to the location,
3:14
we are going to say find the city and state from location,
3:20
and we'll pass the location here,
3:21
so this is a function we are going to write,
3:26
and let's go ahead and get some help form intellisense
3:29
just so you guys can see it,
3:31
now the location is a string and we are going to use
3:34
this method here on it called split,
3:37
so what split does is it will take some character
3:41
in this case we are going to say \n for the new line,
3:44
but it could be a comma if you had a string
3:47
with a bunch of comma separating them,
3:49
and you want to just the individual words you can say comma
3:52
but words separated by new lines so we are going to say new line
3:56
and what we'll get back is a list that contains just the elements of the string,
4:00
that are separated by these items,
4:02
so we'll call this parts like this, and then we are going to return parts of 0.
4:07
This is assuming that the shape of this thing is correct
4:10
it might not be good if it's not- anyway,
4:12
we are going to return this and one more time we might want to
4:16
strip off the characters on here because there is still maybe
4:18
some white space in between.
4:21
Ok, so let's try this one final time,
4:23
do a little cleanup on the code, run it, all right, go in,
4:28
and perfect look at that, we have partly cloudy,
4:33
the temperature is 8.6 degrees,
4:35
scale is Celsius and the city and state are Portland, Oregon.