Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Generators and Collections
Lecture: Slicing collections all the way to the database
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
When I think about idioms that are particularly Pythonic, slicing has got to be one of those.
0:07
Slicing lets you take things like strings and lists and so on, and even much more advanced items as we'll see and create very concise syntax
0:16
and very understandable syntax subsets of these items. So, let's go look at this in code.
0:22
So here I have a main method, it's calling a Fibonacci function and it returns set of Fibonacci numbers that are less than 200,
0:29
it returns these as a list, in the beginning I just print them out so you can see what we would have to work with, so here are our Fibonacci numbers,
0:36
and suppose we want the first five, well there is a variety of ways we could do this, we could do a little loop and gather them up and break out of it
0:44
after we get to five and so on, but in Python, we can go to our list, and we can just say: "I would like to go from the zeroth item,
0:53
up to but not including the fifth item", something like this, and then if I print first five, you'll see we actually get the first five.
1:03
Now, we could write it like this, but Python has a lot of conventions around slicing,
1:07
so if you are going to start at the beginning you can say just ":5" and it says go from the beginning to five.
1:14
So we should get the same output, and we do. Down here, if we want to actually go from the second to the seventh item,
1:20
we'll say, just print it out like this, let's be clear what we mean here, we mean the thing at index 2 up to and including the thing at index 7.
1:28
So we'll say 2 and that would give us just the item index 2 or the third item up to, now for slices,
1:34
it does not include the value put here so you want to put one higher, let's just verify this, OK so if we count by index 0, 1, 2 so then 2 is here,
1:45
3, 4, 5, 6 7th index item is 21, so we did get exactly what we were looking for using 2 to 8.
1:54
We can do more interesting stuff, we can also go and get the "n", so I could say nums and I could put something here,
2:01
we could start by saying "len of nums", and then this value, this is not so amazing but we could say like -3 here,
2:09
and this will kind of work, so here we've got the last 3, great, we could use our convention that I pointed out before,
2:16
that if you are at the beginning or the end, you can omit it, so we could write it like this,
2:22
also still get the last three, but in true Pythonic fashion, we could do better than that.
2:27
We can say we'd like to start 3 items back and then go to the end. Beautiful, that way we don't even care,
2:34
have to look or know what the length is, so that's really nice. So let's put this under the absolutely Pythonic "last 3" version.
2:44
All of this stuff is working within memory lists and we could do with strings and other types as well,
2:49
but the title was "Slicing collections and more", so how far can we take this more - well, this is pretty interesting - from the database,
2:58
if we look over here in this slicing support file, we are not going to cover SQLAlchemy in detail in this course,
3:05
but we've got a class that we are mapping to a SQLAlchemy database as an id an x y and a value, these values I believe are between 0 and 1,
3:14
OK and it stores this into a local slicing_db.sqlite file and it connects to it, we are going to import this session_factory here
3:23
to actually create a connection to the database. Now, if we look at the database, OK there is nothing over here, right now,
3:29
but if we go to our project and we drop this in here, you can see we have all this data and the part that we are going to look at is this value,
3:37
so we would like to use slicing to get the top 3 highest values out of this database, OK, so let's see how that works.
3:46
Up at the top, we are importing session_factory and Measurement, so let's create a session I'll say "session_factory", we'll call that,
3:55
and at the end let's remember to close the session, we'll create a query here, we'll say "session.query of Measurement",
4:08
and we want to say "filter(Measurement.value) is greater than .9", we are going to order by Measurement.value.desc().
4:18
OK, so this is great, maybe we could just come over here and do a ".all" to get this back as a list and then I could just print out the query,
4:28
I am not sure it's really a query but whatever, if we run this, you realize what are the challenges - you have to spell "filter" correctly, here we go,
4:42
once that works, you can see we have a bunch of these back from the database and we could even order it.
4:48
Let's go over here to our engine and turn on tracing, what this will do is this will actually show us the SQL commands going against our database,
4:58
so in the end down here you can see basically "SELECT * FROM measurements ORDER by", you know,
5:05
where the value is greater than .9, order by the value descending, OK. Oops, here is the select part, so this is pretty cool,
5:12
how does slicing have anything to do with this, all right, so if let's take away this all, if I want just the top 3 measurements,
5:22
I can actually use slicing to say "give me a subset of the results from the database", all right, so I could say something like this:
5:29
"top 3 = " and I could say "query, go from the beginning up to 3" and that would give me the top 3 records,
5:38
let's just print those out really quick, so look at this, "SELECT * FROM table WHERE value is this, ORDER BY that, LIMIT and OFFSET and the two values
5:49
we're passing are .9 for the WHERE clause and then 3 for the LIMIT and 0 for the OFFSET,
5:56
so first 3 ORDER by descending", so those are going to be the top 3, now we could actually go a little farther and say well,
6:01
what we are really looking for is just the measurements, so I could say "m.value for m in query up to 3"
6:09
like that and then I'll actually show just the values. So those are the highest 3 values and you can see
6:15
they are very high and descending, how amazing is that? So, the reason I took the time to do this SQLAlchemy example
6:23
is slicing is cool and at first it feels like OK, this is a cool way to work with like lists and strings, but slicing is so much more than that,
6:31
slicing is deeply integrated into many of the APIs and packages that you are going to work with, and so while this is really cool, this right here,
6:42
this is super amazing and this really shows the deep power in a breath of slicing. So let's see this in a graphic.
6:51
All right, so here is our numbers again that we want the values at index 2 to 7 or we would say 2:8 remember, the last item is not included,
6:58
we want the last 3, the best way to do that would be "I want to go back 3 and then the end", so -3:end, that worked perfectly,
7:06
we could even create a database query and get to say "give me the top 3 measurements :3 so 0 to 3"
7:13
and you can see that actual query generator is "SELECT * FROM Measurements WHERE measurement... da da da da LIMIT 3 OFFSET 0". Amazing.