 Welcome back everybody. My name is Brian and in this video we're going to talk about Pickle everybody's favorite. So What the heck is a pickle? What are we even talking about? This made me giggle so bad when I first was reading about it. So Pickling is a way of saving food. It's a very old way of doing it and we're going to do the same thing with objects So we're going to serialize them which is a more modern term But we're going to preserve an object and that object can be saved and stored in another location For example on a disk So that's right. You can make an object save it start your program back up and reload that object like nothing happened This is incredibly cool. This is called serialization and it gets very very complex Pickle while being great is not perfect. It does have some limitations I would encourage you to go out and research Dill D-I-L-L and there are other serialization tools out there as well to overcome some shortcomings of pickle We're going to go over just the very very basics of Pickling so What can you pickle first off pretty much most Python data types and top level classes meaning if you make a class in a class in a class in a class You're going to have problems. So first things first Let's go ahead and import pickle Okay diving right in here first thing we're going to do is we are going to put in a decorator Same decorator that we've used in the previous videos And if you missed my decorator video hit rewind on the playlist and watch the decorator tutorial In case you missed it all it's going to do is print a line Put the function name all the function and print a line All right So our class is going to be overly simple because I'm focused on serializing and deserializing this We're looking at just the basics of serialization. So we're going to say cat and And then we want to go deaf and I'm going to init and We want to init self with name age and info So name and age seem pretty self-explanatory. We're looking for like a string and an integer. Well, what is info? That's why we want to really dive into this video. Things are not always what they seem so We're gonna say self.name equals name And then we're just going to through the magic of copy and paste Probably best if I just grab it right out of the top there and whoopsie. We don't want to mangle those. There we go Almost created a little headache for us. So we're gonna say self underscore name self underscore age self underscore info again The underscore denotes that these are internal to the class. We don't want other people playing with them So from here, we're going to make a display function and we want to display some type of message Let's go ahead and use our decorator Just to decorate that make it look nice and neat on the screen And then we're going to just print out the message whatever the message is But now we also want to print and we want to put the want to put the name along with the age Is a I should say is years old. Hmm. That doesn't make much sense. Does it Probably help fight spelled all of that correctly. There we go. So name is a age years old cat There we go makes more sense now and then we're going to take info and that's going to be a dictionary item So I'm gonna say for a comma be In self underscore info Items That way we can iterate through those dictionary items and print them out and then we're going to just say That was that pretty self-explanatory. What's going on here? So As you can see, this is not a super super complex class Just wanted to cover the basics of it. So you knew exactly what we were doing I'm gonna go ahead and make an instance of this. I'm gonna say old fellow. This is the name of one of my cats Unfortunately kitty passed away. He was probably the best cat I ever had but I loved him to death. All right, so Othello 15 and then we're gonna make a dictionary and color equal black Wait, whoops wait. He was a very fat cat and he loves Eating it was like his hobby. It was almost like a competitive sport for this cat Then we're gonna say Othello that display just want to test this out Before we do anything else. So I'm gonna say display testing Save and run. Uh-oh self info. What do we got here 38? 9 30 Yes, a little bit of an issue there See the previous tutorial I did on air handling. All right. So let's kind of clear that there we go So function and we can now see our decorator is working as expected. There's our line There's the function name our message and then Othello's 15 year old cat Color black weight loves eating and then in decorator. So everything is now working What we're gonna jump into next is actually serializing and deserializing This object notice how we're not talking about serializing the class Because the class is a blueprint. We're going to serialize the actual object All right fasten your seatbelts. Here comes the pickle I almost feel like I should have like a pickle with a cape on it or something, but so I'm gonna say Let's see which is short for serialized cat and we're gonna say series serialized cat is pickle Always kick all when I say that so we are going to dump s if you remember from a previous video if it ended in s It was a string. So that's exactly what's going on here same convention So we're going to dump to a string and we're gonna dump the Othello instance of that cat to a string Now I'm going to go ahead and just print this out Say run This is what Othello looks like after he's been pickled. Man, that sounds really morbid pickling a cat But so you can see some familiarity here underscore underscore main So you tell exactly where that object is and then it's got like name and then you see Othello and there's some data in here So this is really what pickle is doing is it's dumping it into a pickle format This format is not compatible with applications outside of Python and it's not What you would call backwards compatible Meaning you can't take the newest version of pickle and then serialize something and load it with an older version of pickle That's more advanced video that we'll get into in the future. But just keep that in mind that pickle will try to use the newest version So let's go ahead and save this. So I'm gonna say with open and We want some sort of file names. I'll say cat dot txt and we want to write Binary because this is a binary file. We're gonna write this out as F Remember if we do with it's going to open it give us a variable called F Which stands for the file and it's going to close it automatically when we're done All right, so we've got this and we're gonna say pickle Dump so this is the difference between dump s dumps versus dump with no s dump It's going to say what do you want to dump? I'm going to give it an object and I'm going to tell it where so we're going to dump that to a file Go ahead clear this out and let's run it So now we have this cat file and it says files not displayed in the editor because it uses unsupported text encoding All right, so we're gonna right-click And we're gonna open with and in a previous video I showed you how to install the hex editor but just in case you go out here and you just type in hex and There's a hex editor. You just install it. So flipping back. We're going to actually get rid of that I'm going to right-click Open with hex editor and this is what the serialized object looks like You can see it is verbatim the same thing But we've written these bytes out in a binary file We can now take this file and Say email it or transport it across the network or leave it sit on a hard drive Whatever we wanted to do and have another Python program D serialize or open it back up Okay, now that we've Serialized and we wrapped our head around the serialization or the pickling process here We're going to D serialize which is the exact opposite. It's a reading the information back So I'm gonna say my cat Equals and I want to say pickle That load s because we're gonna load a string and The string is the serialized cat that we did up here. So we're just gonna grab him pop it right there And I'm gonna print From string Just so we know where we are in the console And we can actually just say my cat Display and from string So really what we're doing is we're taking the string representation of that cat and notice how it's got to be in front of it That's denoting that this is binary So it's going to I should say bytes But it's going to take that as a string Put it into pickle and then load it back into a usable object that we can call functions and run codon. This is extremely cool So tada Rum string rum string and it's exactly the way it was hotels a 15 year old cat He's black 15 and loves to eat. So we have revived my cat back from the dead is Gracious that sounds we've unpickled my cat So this is really really cool and just to prove that we can do this from a file. We're going to literally take this Copy paste and we're going to say instead of write binary. It's a read binary as file and we're going to pickle Load instead of load s and we want to load that file And I'm gonna say this Short for disco cat. Why not? and we're gonna take that And then we're gonna say this cat display from disc And so what this goes going to do is gonna go out open this binary file We're gonna open with hex editor. It's going to load the bytes from the file Create an object and then we can now work with that object, but these are now different objects even though They're coming from the same data source from disk Othello color black wait 15 loves eating now if we do something a little bit interesting Just to wrap this up I'm going to print these out. I'm going to print My cat and I'm going to print This cat just to show you what's going on here You can see Hat object at and then they are two different Memory locations meaning these are now two different objects This is one of the little cautionary tales of serializing and deserializing is you can actually Save an object and then reload that object multiple times and you may not want two Othello's personally I'd love to have two of that cat, but it may not be your intent So be a little bit careful when you're deserializing your objects 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 realm's 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 you to me.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