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