Python for .NET Developers Transcripts
Chapter: Web frameworks
Lecture: Our guitar class

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


Talk Python's Mastodon Michael Kennedy's Mastodon