 Welcome back everyone. This is Brian. We're going to take everything that we've learned so far and make a pet shop application This is going to be like a mini inventory system. So Let's talk about the design real quick before we dive into this thing First thing that's going to happen is Python is going to start our script From there what we're going to do is we're going to go out to the desk and we're going to load any existing file Then we're going to go into a loop and let me find a good representation of a loop here And this loop is simply going to ask them. What do you want to do? It's going to do something like this. You have a few options that you can choose from Any one of those options is going to go right back into the loop. So let me just grab this go right up into here and eventually We will exit the program at which point it will automatically save the progress The end result is going to look something like this Start to load to loop some options and then we're going to save when we exit. Let's dive in and take a look Okay, moving right along. We are going to import. I'm just gonna plop some notes in here So we can kind of see the structure from the image we had in the previous little section We're going to make some imports make a class class is going to have some functions We're gonna have our main function then we're gonna do some testing But first things first we need to import what we're going to use So we're gonna use two things. We're gonna import Jason and OS dot path Jason's going to be used to save and load that file. We want to have a dictionary We want to be able to persist that out to the disk and then import We're going to use the OS dot path. There are more Modern ways of doing this, but we want to be able to check if the file exists So we're gonna use OS dot path because that's kind of like the old school way of doing things Once we have those imports in we're ready to rock and roll Now inventory systems can get a little bit complex So what I want to do is I want to make a class and I want to wrap the functionality in this class So if we had multiple inventories, we could handle it separately some say class Inventory and let's go ahead and make a dictionary And we're just going to make some functions. So we're going to Do our initialization And for these initially I'm just going to put pass because we don't really want any code in here just yet We're going to fill this in as we go We're going to add And we can speed this process up just a little bit here So we're going to add remove I do want to be able to display what's in the inventory We also want to be able to save obviously because we're going to put this out to a file And because we can say we also want to load this Now you may be wondering why am I not putting this into a separate file? I kind of want an all-in-one solution just to show you that we can actually do an all-in-one solution Next step now that we have our classes, we're going to start filling in these functions All right, so far we have a class and this is kind of a high level view of this class It's got init add remove display save and load We're going to fill in init and add so let's go down to our init and Honestly, I want to keep this just ridiculously simple We said in the very beginning as soon as this thing starts up we want to be able to load so I'm going to say Self.load and we're just going to put all that functionality in that function Now add is going to be a little bit different here. I'm going to get rid of this pass And we are going to modify the function itself We want to have a key and a quantity The premise being when we add we're going to add some sort of animal like a cat a dog or something and a quantity That we're going to add into the inventory So I'm going to say the q short version for quantity is going to be zero now if you look at Our dictionary there's absolutely nothing in it. So we can't just go out and grab the key We have to actually test to see if it exists first So if key in self dot pets Then we're going to go ahead and grab the current value like how many we actually have in the inventory self dot pets And we want the key And we're going to update our quantity So we're going to say take that current value and add the quantity to it Now we have the opposite of that It just simply wasn't in the inventory in which case we're just going to say q equals the quantity From here it becomes pretty straightforward. We're going to say self dot pets Whatever the key was And we're just going to update that with our updated quantity From there I'm going to put and just for the sake of time. I'm going to copy and paste Added the quantity and key and the total is now whatever Is actually out in our inventory system So it's pretty simple, but there's a couple key concepts you have to wrap around here First is we don't want to trust that the key is actually in there We want to test to make sure it exists and we want to have some functionality in case it's not there Back at the top of our file. We've done our imports. We've created the class We filled in and it and add now we're going to do remove and display So let's go down here and remove and display just have pass in them So the first thing I'm going to do is I'm going to go to add and I want to kind of copy this That way We have a very similar very structured api To add or remove you just need a key and a quantity. We won't have any weird data that we got to figure out So remove is well the exact opposite here, but we can actually pull parts of this out. So for example We can just take this entire structure Just paste it right in here. So we're going to start off with zero We're going to say if the key exists then go ahead and get the key And then the quantity instead of addition is going to be subtraction And then we want some sort of other value here. So for example I don't really need an else because q is already set to zero. Actually, let me put that on the right line So what we're going to do here is now test to make sure that we didn't subtract too much Say if q is less than zero then We want to make sure that q is equal to zero In other words, you don't want a negative balance in your inventory that just simply wouldn't make any sense And then of course we are updating the inventory and now We've got some sort of display going out to the end user We're going to say removed quantity key the total is now whatever the total is Now for our display, this is going to be just very very simple or key comma value in And we want to go into the self dot pets Dot items and if any of this that I've been talking about is just making you scratch your head I'd highly encourage you to go out and watch the videos where we talked about these key subjects here For example, we've talked about classes. We've talked about dictionaries. We've talked about four loops and things of that nature We've also talked about json and pretty much everything we're talking about we've done in the 32 other videos that we've done here. So And let's go ahead and print this out. So I'm gonna say print And I want to do a formatted print And I want to say the key Equals and then whatever the value is Now what I typically like to do at this point before we start getting into i o is actually run this just to make sure We don't have any crazy errors The script's going to do absolutely nothing. We don't even have a main function We just want to make sure it doesn't have any weird syntax error or anything like that Okay, in our class, we are at the save part and let's kind of go down here all the way to save We're going to handle save and load in separate sections. So we're just going to focus on saving So i'm going to get rid of this pass and I want to print out to the user that we're going to do something because Well, because we're working with python, I'm just going to be brutally honest with you We don't know what sort of end devices is going to be running on it could be a high end blistering fast server It could be a painfully slow embedded device. We simply don't know So I want to tell the user we're going to do something saving in inventory And I typically do this whenever I'm working with anything i o related I will tell the user before and after meaning i'm going to do something and I have done it or I had some error or something like that So I'll start off with a structure like this saving and saved and then I'll actually work with i o the reason being If you're looking at a computer the computer's hard drive is probably really fast But if you get into like an embedded Linux device and the script is running on that It may be really slow or what if the hard drive went to sleep? I'm sure you've had that before where you go to save a file and then you hear the hard drive spinning up We want to make sure the user knows our application didn't freeze up So we're gonna say with open And let's just say inventory dot txt Now this is where we're kind of kind of break our own rules just for the sake of time here We are talking about Putting all this in a class so that it can be reused the problem is now i'm hard coding the file name in there Not a major deal breaker, but if you're going to use this for any sort of production You're going to want to actually be able to dynamically set that string All right, so with open and we're going to open this file in write mode plain text as f F is shorthand for file. We're right here. Now we want to do a jason dot dump Now remember there's dump and dumps You see the s at the end that's we're going to convert it to a string. We don't want to do that We want to actually dump it out to a file So first things first we have to give it the object in this case the self pets And then the file that we're going to dump it to And the great thing about the width keyword we've covered this before in previous videos Is it's going to automatically close that file for us so we don't have to worry about Flushing the contents or closing the file or anything like that. So jason's just going to Take this dictionary convert it into a jason format dump it out to the file And then width is going to close the file for us and then we're just going to print out saved That way the end user knows hey, we have actually done the i o completely and there were no errors We've only got one more the load function. It's helped if I selected it, right? So here's our class I've tried to indent it to make it a little obvious what the functions inside the class are We've got one left load and of course it will load the file. We've already done save So we can just kind of borrow this little structure here. So let's grab this And just paste it And we can say we can spell it right loading inventory And then load it And then with and remember the file name is important. So we have to say with the same file We're going to go ahead and read that as plain text Now we want to do the opposite of a dump. So this is where we need to slow down We've gotten a little ahead of ourselves here, and I'm wondering if you can see the problem And in case you can't With open file, we're just assuming that when we load the file exists And we need to be a little bit careful with that because that could cause some Real problems, but that's why at the very beginning of this We did an import os path because we're going to check to make sure the file exists. So let's go back down here And we'll say if not os.path.exists And here we want to make sure we have the same file name. So I'm literally just going to copy this And paste it Then we want to say print Skipping Nothing to load And then let's go ahead and return out of here We're doing this because when the load function is called It's going to print out loading inventory And if we just you know return it's going to look like the program's hung because it's going to just say loading inventory and stop So we want to make sure the file Exists and if it does not then we're going to say skipping nothing to load that way the user knows That just simply isn't a file there and you could make this a little prettier And then we return out. However, if there is a file we're going to say with open Open that file up in read mode as file and now we need to do a jason load instead of a jason dump I'm going to say self dot pets equals jason dot load And remember there's a loads s if it ends in s it's going to do it to a string and we don't want that So we want load and we're just going to give it our file And then last but not least we're printing out loaded that way the end user knows. Hey We successfully did our iota completion and we didn't have any bad issues Again, I like to run Scripts not going to do anything, but if I have like a major syntax error It'll definitely spit it out and tell me and we're good to go Looking at our little flow here. We've come a long way. We've gone the imports We've done the entire class and now we're down at main We're going to put a main function in here. So I'm going to go all the way to the bottom And I'm going to say Main and it's going to put this in here for me automatically depending on your ide you may have to actually type that out Or saying if the name is main meaning python is running this file directly then I want to Call a main function Now a lot of people will want to just use this as if it is some sort of function itself I don't like doing that. I like actually making my own main function That way I can actually just copy and paste this function and use it in other places. Whatever I want to do So we need to make an instance Of our inventory because remember a class Is a blueprint. It's not actually the object. So now we're going to take that blueprint to tell python take the blueprint And create an object from it and give us that object In the form of a variable named i and v short for inventory Now that we have that what we can do is create a loop And I know in past videos I've said loops are the root of all evil and you should avoid them unless you know what you're doing but If you've been following along we should know what we're doing at this point so First thing we're going to do is prop the user for some type of action So I'm going to say the action equals And we want input And we're going to tell the user hey tell us what you want done And I've already got this keyed up and ready to go and my notes off to the screen here I should say off the side of the screen. So we're going to say actions you can add remove list save or exit And these are the actions we're going to flesh out here So first things first If the action Is equal to exit Then let's go ahead and break out of this loop Notice i'm not returning because I want to actually do something at the end of this and let me go back here And we're going to say i and v dot save So no matter what actions the user takes Once we're done with this loop the inventory is going to save itself before the program exits I'm going to put it right here exit The way python treats a script is once it has nothing else to do it simply exits out of python itself And that's why you see it stopping down here all right, so Once we've got this in place now we just really flesh out the other actions So i'm going to say If and we can actually just copy this make life a little simpler honest Big fan of copy and paste here. I'm going to say if the action is list Then I want i and v dot display if the action is save We can go ahead and say i and v dot save And you could do load and all that other stuff You notice how I didn't put load in there because the inventory is going to load automatically In the constructor But you could you know if you really want to go crazy you could put that action in there Definitely put it in there if you wanted to Now i'm going to put in right here A little bit of logic where we're going to say if the action is add or the action Is remove Then we're going to take pretty much the same Type of functionality here because it doesn't matter if we're adding or removing We have the same api and let's kind of scroll up here. So Remove we want key and quantity add we want key and quantity. So it doesn't matter which one we're doing here So in here i'm going to say the key equals input enter an animal and for the quantity input we'll say enter the quantity Now that we have those two we need to figure out which one was actually called So i'm going to say yeah action Equals add Then do something and if you already know a bit of python You probably know there's better ways of doing this, but we're just using what we've learned so far So i've got a kind of i don't want to say dumb it down, but i've got to make it a little bit rudimentary here So and this is bare we're going to just branch off in our logic and say if it's add do one thing if it's remove do the other There we go Probably help if i actually called remove otherwise we're just going to add it twice This is the beautiful part about having the same type of api if you will Is you can now start wrapping functionality in here and make it super streamlined super easy to follow So following our logic and let me just grab some screen real estate here When we go into the main We're going to create an instance of the inventory and then we're going to start a loop After the loop's completed no matter what the user's done. We're going to save that inventory In the loop, we're going to continuously ask the user What do you want to do and then take action depending on what they entered The only thing left to do is fasten your seatbelts unless test this out. So If you have if underscore underscore name underscore underscore equals main And you've got your main function and you don't have any syntax errors. You should be good to go Let's go ahead and grab Some area here so we can see the program in action. We're going to run this And it says loading inventory skipping nothing to load because we don't have a json file here And it's going to say actions add remove list save and exit. So let's go ahead and add And i want to enter a cat And i want to enter a quantity of six cats because you have to have six cats in life. It's just it's mandatory It's going to say added six cat total equals six. So let's add that And i want to add three cats here So we've added three cats and uh-oh total 63 We have a boo boo in our program. Let's go ahead and let's kill this terminal Let's figure out what we did wrong here Hmm Notice anything funky about the output it said 63 instead of nine. We need to convert this now Do an integer Remember we talked briefly about casting where think of casting like a wizard with a magic wand He's going to cast a spell and convert it from one thing to another Really what we were doing under the hood is we were saying quantity and then action add go up here And it's saying v plus qty. This is the the blessing and curse of python and other Languages like this where you don't have a type It knows that they're both strings. So it just adds them together as a string So a string string Will look like this Which is exactly what we just saw Okay Enough of that. Let's go ahead and rerun this now because we killed our console. We still don't have a json file Which is exactly what we want at this point. So let's run this fresh All right, let's try this again add Cat Six because you have to have six cats in life say it with me we're going to add At And we want to add three Now we have a total of nine. So our logic is now working as expected. Let's go ahead and let's add A dog Let's say we want 99 dogs and then we realize that's a lot of dogs We really can't have 99 dogs and nine cats. So let's just Take that down a notch. Let's remove dogs And uh, man, you know, I like dogs just as much as I like cats. So this is a very difficult time for me um, let's go ahead and remove 98 dogs. I'm very sorry dogs today was not your day We've removed 98 dogs. We now have a total of one Now we could type save but i'm just going to exit Saving inventory save so it automatically saved for us And it put this inventory.txt if we go out here, we have nine cats and one dogs and everything works as expected I'm going to clear our console And rerun this and something magical happens Loading inventory loaded. Let's go ahead and call list We have Nine cats and one dog. So it has now loaded that json file loaded it up into the dictionary And everything's working as expected. I'm gonna go ahead and say just Enter enter enter and you can see because it doesn't have valid input. It just goes back in the loop So I'm going to enter just garbage. It doesn't matter what we do I'm going to go ahead and add Fish and I want 45 fish I'm going to go ahead and intentionally save this Saving inventory saved and I'm going to go ahead and display Basically you can do this all day long, but uh, oh display. I don't want display. I want list You can see how we have nine cats one dog and 45 fish. We could do this all day But really what I'm getting at here is now we have some programming logic And everything's working as expected So I want to go ahead and exit out I'm going to save our inventory we can go ahead and peek at it. Sure enough everything's exactly the way it was when we finished the application All right major takeaways from this video. This is kind of the culmination of everything that we've learned so far We've worked with imports. We've learned classes. We've learned functions. We've learned flow control. We've learned about main We've done some testing. We've learned about jason the os path And kind of breaking this down here Always always always initialize your variables, even if it's just empty because you don't want to have something not defined Instructors are your friends because you can set up some sort of default action And when in doubt always check your data types Make sure you're giving the user some sort of visual feedback And there are times where life is not that simple and you'll need to check to make sure whether a file exists or something like that We've also covered things like the width keyword and how to read and write files in plain text And we I think we did cover binary as well But um one major takeaway some minor tweaks to this is Do not hard code the file name like I did I kind of just did that to save some time But you may want to actually ask like if you wanted to really tweak this Make like another function and put it in the constructor and basically say If we don't have a file What file do you want to work with and just let the user enter it something like that? So I hope you found this educational entertainment drop a comment below and let me know what you think I hope you enjoyed this video you can find the source code out on github.com If you need additional help myself and thousands of other developers are hanging out in the void realms facebook group This is a large group with lots of developers and we talk about everything technology related Not just the technology that you just watched and if you want official training I do develop courses out on udemy.com. This is official classroom style training If you go out there and the course you're looking for is just simply not there drop me a note I'm either working on it or I will actually develop it I will put a link down below for all three of those and as always Help me help you smash that like and subscribe button The more popular these videos become the more I'll create and publish out on youtube. Thank you for watching