Python for the .NET developer Transcripts
Chapter: Web frameworks
Lecture: Our guitar class
0:00 Now that we have this route in place
0:02 the next thing to is actually have some real data.
0:05 The ultimate goal will be
0:07 to store this in a database using ORM to query it
0:10 and get it back.
0:11 But for now, we're going to just create a class
0:14 and then some data access layer
0:16 to get that data out of the memory.
0:19 The way we're going to do this in the ORM is
0:20 we're going to create a class that models guitars
0:22 and that also makes sense for us here.
0:24 So, let's create a data folder
0:29 and then a new file called guitar
0:31 then here we'll have a class, capital Guitar.
0:36 And it's going to have a couple of things
0:37 let's say it's going to take a name, which is a str
0:42 a price, which is a float, an image which is a string
0:46 and a style, which is a string.
0:48 Now I could go down here and type self.name = name
0:52 but, PyCharm will do it all for us.
0:55 So we just start knocking them out.
0:58 The only thing I don't like about this is
1:00 that it puts it in reverse order. But, that's okay.
1:04 So here we've defined a guitar class.
1:06 It doesn't really have any behaviors yet
1:08 but remember, the goal is that we're going to adapt this
1:10 to our ORM later
1:11 and it's going to take on more functionality.
1:14 For now, it's just going to be like a data structure
1:16 that holds information about a guitar.
1:18 Now the next thing I want
1:19 to do is add a section I call services.
1:23 In services, these are not like web services
1:26 but these are parts of our application
1:27 that provide services to the other parts.
1:29 So, the part that does all the querying
1:31 and management of data around, say, guitars or
1:34 the catalog or something like that.
1:36 I'm going to create one of those.
1:37 So I'm going to create something called a catalog_service.
1:43 It's a little bit like, maybe the repository pattern
1:45 but, way less structured.
1:47 This is where the data access stuff for guitars
1:49 and other stuff to do with the catalog
1:51 like categories, goes.
1:54 Now we've come to a really big difference in Python
1:56 and C#. If I was doing this in C#, what would I do?
2:00 I would create a CatalogService class
2:02 and I would give it either static or instance methods.
2:05 Especially if I'm giving it only static methods
2:07 what is the purpose of that?
2:09 The purpose is to group a bunch of functions together
2:12 that I can call and I have to do that in a class
2:15 because C# doesn't have standalone functions.
2:18 This thing groups stuff together
2:20 and we can just put functions in it.
2:21 So, we don't actually have to create a class
2:23 but we can just have functions
2:24 and it's every bit as meaningful
2:26 as having a static class with only static variables.
2:30 So, let's go over here and create a function called
2:36 And it's going to take a style, which is a string
2:38 and it's going to return a list of guitar.
2:42 So here we've got to start importing things.
2:44 from typing import List
2:46 and we've got to import guitare data guitar.guitar
2:49 like so. There we go. Perfect.
2:52 And let's ignore the style thing for a moment.
2:54 In fact, so let's say guitars is equal to a list.
2:58 I'm just going to copy some data over
3:00 because it's not worth you watching me type this all
3:02 and it's got to be just right with like, URLs and stuff.
3:05 So, here are some guitars, and notice it's complaining
3:08 that we're not returning the guitars.
3:10 Again, later, this is going to be a database query
3:12 but for now, we're just going to jam it here
3:14 because we want to handle databases separately.
3:17 We also want to show the most expensive ones first.
3:19 So, we'll say guitars.sort
3:22 and use the little lambda expression to do that.
3:23 So we'll say key=lambda g.
3:27 How do we want to sort guitars?
3:28 Let's say we want to show the most expensive ones first.
3:31 How are we going to do that?
3:32 We say, the sorting thing is, we want to have a descending
3:36 so negative is one option, g.price.
3:39 Here, you go, spelling is hard.
3:42 Okay, perfect. So g goes to negative g.price
3:44 or we can sort ascending by price and then say
3:46 reversed, reverse=true. Take your pick.
3:50 Maybe this is a little more explicit at what's going on
3:52 so, I guess we'll go with that.
3:55 So for now, we're going to ignore the style
3:57 and we're just going to return this
3:58 just to see that we're getting something here.
4:01 Let's go to our app and say, up here we're going to say
4:04 from oops, too low. from guitary.services import catalog_service.
4:13 Remember, if this was a static class the benefit would be
4:16 I could go, catalog_service.
4:18 and get a whole list of the things I could do, like this.
4:22 So, here we go.
4:23 And for now we're going to pass None for the style.
4:26 Which I guess we probably want
4:28 to tell it that this is an optional string?
4:33 Which is what that little warning was about there.
4:35 Notice that it's gone now.
4:37 Alright, I think this will do it.
4:39 Let's give it a try.
4:40 Also we made a mistake somewhere.
4:43 Moment of truth.
4:44 When I click this button, what happens?
4:46 Yes! It's incredible! Sort of.
4:50 This are our guitars.
4:51 Remember, that's the default representation
4:54 of a guitar object or in a class.
4:57 It already has a class name, object at some address.
4:59 But there it is!
5:00 It looks like we went to the database, got our guitars
5:02 and were showing them here.
5:04 Obviously, we have some HTML work to do
5:06 but, we're on a good path.