Python Memory Management and Tips Transcripts
Chapter: Efficient data structures
Lecture: Container sizes, starter data

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's explore this idea of container sizes. So we'll come over here and make a new Python file
0:04 "app_container_sizes_compared". Let's go crazy.
0:09 Make it long. Do our fMain magic on it.
0:11 Go ahead and set it to run when I hit the hotkey, and there we go.
0:15 Okay, so we're gonna do a couple of things. First,
0:18 I want to have the day to feel somewhat real.
0:21 So I've got some fake names. Now,
0:24 we can create fake ages easily,
0:25 but I want to create some names that are,
0:27 like, generally the size of real names.
0:30 And there's enough variability and stuff.
0:32 So I've copied this thing called mock names and check it out.
0:36 It's a bunch of names. It's like 1000 different names.
0:39 What we're gonna do is we're actually gonna write a tiny bit of code that's going
0:43 to take, find all the first names,
0:45 all the last names and then randomly
0:46 create other names out of them.
0:49 Okay, that should be easy enough to do. I also want to use that size utility
0:55 over here. Remember, we started out with this
0:57 size_util right there? To do cool stuff?
1:00 Well, in order to use that I need to come over and write something like
1:05 this "import size_util".
1:09 No problem. It seems fine.
1:11 It's fine because I'm running PyCharm and PyCharm knows all of these blue directories,
1:15 these marked as source roots are candidates for looking for top level things.
1:21 But if you happen to use another editor or you just have this directory here,
1:25 it's going to fail. So let me give you a little bit of help here.
1:28 We're gonna say, we're gonna go back, up a folder, and then add this
1:34 folder to the root. So I'll just import these things that we need here.
1:40 So this will allow us you size_util,
1:42 regardless of how you run it,
1:44 right? I'm running PyCharm
1:45 so I don't really need to do this,
1:47 But just to make life a little bit easier,
1:49 we're gonna do this, alright? To make it less error prone. Next down here,
1:53 we're gonna go to random, random.
1:57 Obviously, this is something going to do a lot with, create a bunch of random things,
2:01 and we're going to seed it to 42 or pick a random,
2:05 just some number. This means it's always going to start from the same place and
2:10 it'll basically be deterministic in what it generates for us.
2:13 So that way we always get exactly the same output in terms of size and name-
2:18 length and whatnot. Then we're gonna have a count of how many times or
2:21 how many names and ages we want to work with, and we're gonna work with 100,000 details
2:27 around people. And then I'm gonna need the ages.
2:31 So I'm gonna have a function that says "generate_ages" and it'll pass the count.
2:37 Now this, this one's pretty easy.
2:41 We want it to generate the ages.
2:43 How are we gonna do that? Well,
2:45 we're just going to return a little list comprehension here.
2:47 We're going to say "return random.randint between 18 and 100, for
2:55 nothing in range from zero to count".
3:00 That's it. And let's just print out ages really quick. Print..
3:04 Maybe not all of them, let's print the top 10 with an "s" There,
3:08 there we go. Those look like realistic ages,
3:11 Right? Okay, so we're generating the ages and let's do the same thing for
3:15 names. Let's be explicit. This is a list of "int",
3:22 and this is going to be a list of "str", and we'll write "names".
3:29 Now, generate_names is a little bit more complicated.
3:31 It's not really that hard, but it's also entirely not the point of what we're
3:35 doing. So I'm gonna just drop this in here and you can see what we're
3:38 gonna get is, I wrapped that around
3:41 so it fits a little better. So what we're gonna do is we're gonna go read this
3:46 and go line by line, pull
3:47 this apart, and we're just gonna give back the name.
3:52 And then for each one of these were going to randomly select one of these out
3:56 of the full names. I guess we're not breaking it apart.
3:58 We're just gonna reuse some of the names,
4:00 but there's 1000 of them, so that should be enough.
4:03 Okay, this really could be just a straight comprehension,
4:06 but that's fine. We'll just leave it like it is.
4:08 And let's do the same thing
4:10 and print out "names" just to see that we're getting some.
4:12 Yeah, Katheryn Orknay and so on, they all look decent to me.
4:20 All right, well, now things are set up,
4:23 everything started. What we have to do is test the container sizes, use various containers.
4:30 So we're going to go and use this "store_ages" in different ways,
4:35 potentially store names in different ways and so on,
4:39 and then when I ask the question
4:40 "well, how much memory does that take, for storing them in classes or for storing them
4:44 in data frames and so on?"