WHAT Is "Pickle" In Python?! (EXTREMELY Useful!)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're going to be talking about something quite interesting in Python and this has to do with pickling which in Python just means serializing an object so that we can save it somewhere and so that we can also unpickle it and retrieve it when we need it and in general when we save some data from a program we usually put it into a Json file or into a text file but what if you have a class and that class has attributes and you've already made modifications and created an instance and you want to save the instance of the class the actual instance well that's one place where we can actually use pickling we can save this instance of that object and load it back into memory so we can use it later so let me show you how this works by importing the pickle module which is a native module to python we all have it so we can just use it by typing import and we will also import Json so I can show you the problem we're trying to avoid so we're going to need a class called fruit and this class is going to have an initializer and inside the initializer we're just going to set this fruit to have a name of type string and it's going to have some calories of type float now the self.name is going to equal the name and the self dot calories are going to equal the calories a basic initializer and we'll have one function that says describe fruit and here we'll print the self dot name self-doubt calories and it's going to have a separator of the colon and a space now just to make sure it actually works correctly we're going to create a fruit instance so fruit of Thai fruit is going to equal this fruit of banana and it's going to have 100 calories then we'll just type in Fruit dot describe fruit and if everything goes well we should have a banana with 100 calories now let's pretend we want to create some modifications to this fruit of course we just type in Fruit dot calories now it has 150 calories for some reason and now we want to save this fruit we want to save the new fruit with 150 calories now one way to do this of course would be to say with open let's say banana dot Json in write mode as file and now that we have the Json file opened we can add data to that Json file so here we can type in that the data is going to equal a dictionary and we'll just type in name is going to equal the fruits dot name and that the calories are going to equal the fruit dot calories then we want to dump this into the Json file as as a dictionary so Json dot dump and we need to pass in the data object into the file which is the Json now if we run this we're going to get a banana.json file with 150 calories and the name of banana and to retrieve it we need to do the exact opposite so we need to create another width open so here we type in with open banana Json now that that exists and we want that to be in read mode as file and then we just want to get that data back so we can say data equals Json dot load and we're going to load that file and we can just print that data to see if we get that data back and if we run it we're going to get that data back as a dictionary now you might be asking why am I putting you through all this basic Json stuff well the answer is quite simple as you could see here we went through a lot of hell just to create that data dump it into a Json file and to get that data back it also took a considerable amount of code and we didn't even get to the point where we have to dump this data into the fruit object once again so we can use that data what I want to show you is how we can literally put this fruit into a file and then we can just load that fruit when we need it so all of this was necessary to show you the trouble that we are going to avoid so instead of having all this nonsense and taking all that time to load a Json file let's just delete all of that now we still have the fruit with the calories set to 150 but here we can type in with open and this time we're going to type in data dot pickle and the extension doesn't really matter it can be any name you want but I would avoid typing.txt or something that's already quite common pickle is a very nice naming convention in case you don't have the creativity to make your own so I would just go with pickle if I were you and here we want to write some bytes so WB and we're going to say as file now a lot like the Json file we can just type in pickle dot dump and inside here we're going to dump the fruits the actual fruit object and it can be any object in Python and it's going to serialize that and serialize just means converting converting an object into a byte stream so here we're going to serialize that object and put it into the file now if we run this we're going to get data.pickle and it's impossible to read that file without opening it so the only way to read from that data is to open it ourselves either with python or some other program so now that we have this and we wrote it down we can delete everything we have here and now we're just going to open that file so with open and we're going to get the test and we're going to get the data dot pickle in read bytes mode as file and here we can say that the fruits of Thai fruit is going to equal pickle dot load and we're going to load that file now the cool thing about this is that we can call fruits and we can describe that fruit and as soon as we run this program we're going to get our banana back with 150 calories and we can continue editing that fruit as we please we can say fruits dot calories is now 200 and so on and we can describe that fruit again so fruits dot describe and it will edit it to 200 calories so we were able to directly load that file from the pickle data and deserialize it so we were unpickling it which is also referred to as deserializing and that just converts it from a byte stream into an actual object that we can use in Python so there were only two methods you need to remember for basic pick link you have the pickle dot dump and you just pass in what object you want to dump such as the fruit we had from earlier into the file of your choice which will be the file you pick here and then you have the pickle.load which takes that pickle data and deserializes it now I have one huge disclaimer before you go around on the internet unpickling random files pickle data can be extremely dangerous because there's no way to understand what's inside the file until you actually run this file so what that practically means is that someone can put a shell command in that pickle data so when you run it it can actually gain access to your computer and that's something you don't want to happen so it's really important that you only load pickle data that you absolutely trust do not load pickle data from the internet without having a verified source and again if you want to save this pickle data just create the file again here with open the file of your choice in Reading bytes mode and then instead of loading the file we just want to dump the file so pickle dot dump the fruits into the file and now we're going to save 200 calories as the base result so something more interesting so something more interesting would be to plus equals it each time we run the program so now if we run that we're going to get an error because this is supposed to be right bytes now we will run that again we get the initial value of 150 and now we saved it with 350. if we run it again we have the initial value of 350 and we're going to save it with a value of 550 because each time we run it we're getting the old fruit object we are editing it and we are saving it in the pickle data so that was a simple example on how useful it can be to pickle data it can really save you a lot of effort when it comes to serialization with objects because of course you don't want to create a text file or a Json file each time you're trying to save an instance and that's really all I wanted to share with you guys today so do let me know in the comment section what you think about it of this some interesting aspect of pickling that I missed that I could explain in a future video perhaps but I'm really curious to hear what you guys have to say about it and with that being said as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 76,265
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: 6Q56r_fVqgw
Channel Id: undefined
Length: 9min 31sec (571 seconds)
Published: Wed Dec 07 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.