Using The Python Pickle Module

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys today i'm going to show you a python module called pickle um the pickle module implements a binary protocol for serializing and deserializing a python object structure so basically you can create a python class object for example and it allows you to package that class object into a file to be accessed at a later date so i'm just going to show you a quick example of how that's how that's done so first we'll import pickle and we'll just create a class called food [Music] and in food we'll have our init statement and in it we'll have self.protein initialized to zero we'll have self.carbs initialized to zero and we'll have self.fat initialized to zero um so let's create a function inside of here to do something with those variables so let's just do uh calc calories [Music] and so with calc calories we will do self dot protein times four self.carbs times four plus self.fat times nine and we will return calories okay so that's our basic class structure um let's create a object called cheese this will be a food object um for cheese we'll have a protein of nine we'll have carbs of one and we'll have a fat of nine whoops okay um okay so now now we want to packages this cheese uh class object um into a file so this is where pickle comes in play so we'll do open with open and here's your file name and your file name can be anything you want it to be the extension could be anything so we'll just do whatever and then we'll do wb for right as file and then we'll do pickle dot dump cheese into file we'll run that and as you see cheese dot whatever has been created so to deserialize cheese dot whatever and use it somewhere else what we'll want to do is import pickle and since cheese dot whatever is the cheese object is dependent on this food class object we'll have to import this food class object so from pickle tutorial import food okay so we'll do a with open statement and we'll get that cheese dot whatever file this time we'll read it so rb as file we'll set it equal to cheese pickle dot load file okay so now we have the cheese object in a separate python file so we'll do let's let's just print just make sure print cheese dot carbs print let's try the calc calories cheese dot calc calories okay we'll run that and as you see we have 1 carbs and 121 calories so it may not be very efficient to save a single uh class object like this in into a file so a clever way you can save multiple is to create a dictionary so i'll just create a dictionary called pantry here so in pantry i'll just store the cheese object so pantry cheese equals cheese and we can create another object we'll just do egg food chi or sorry egg dot protein i don't know five uh egg dot barbs one egg dot fats uh four and then in pantry we'll save the egg object as egg equals egg okay so let's save it as a different file name let's call it pantry we want to dump pantry so pantry is a dictionary object that contains two class objects so we'll run that so pantry.whatever has been created so over in pickle load we will load entry as rb we'll set it equal to pantry and here we want to do cheese is equal to pantry cheese same with egg egg is equal to pantry egg so this will still work here because we've already assigned cheese to that uh class object inside of this dictionary object pantry so we can do the same actually with egg now egg and egg and run that and as you can see we have our variables here printed out
Info
Channel: b001
Views: 9,294
Rating: undefined out of 5
Keywords:
Id: uSGnAy9apRE
Channel Id: undefined
Length: 6min 54sec (414 seconds)
Published: Tue Sep 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.