Storing and retrieving Python objects with "pickle"

  Рет қаралды 10,730

Python and Pandas with Reuven Lerner

Python and Pandas with Reuven Lerner

Күн бұрын

Пікірлер: 34
@lordbuckethead6030
@lordbuckethead6030 2 жыл бұрын
This is literally the most useful pickle video i've seen while trying to learn it so far, thanks a ton
@ReuvenLerner
@ReuvenLerner 2 жыл бұрын
I'm so happy to hear you enjoyed it!
@aaronhumanistallen6160
@aaronhumanistallen6160 4 жыл бұрын
I really like you're teaching style and would like to say thanks for this content! Cheers.
@Jerryboy97
@Jerryboy97 2 жыл бұрын
Thank you so much. I was fearing I couldn't save my objects for the future, as they took me nearly an hour to get (65 million iterations of something, yay). I am saved
@ReuvenLerner
@ReuvenLerner 2 жыл бұрын
I'm so glad to hear it helped!
@sakshiwahi2025
@sakshiwahi2025 4 жыл бұрын
Wow. Why aren't there more views on this channel! LOVE the way you teach _/\_
@josephgaviota
@josephgaviota 3 жыл бұрын
Agree; I thought it was very effective too. And the genius of saying "that's an external module, don't worry about that right now" was be very helpful when teaching; often it's easy to get bogged down with the various side-trips which derail the point one is trying to make.
@inzemam5271
@inzemam5271 2 жыл бұрын
When you said, "The world should listen to my suggestions more"... It reminded me of my dad. Great video though.
@ReuvenLerner
@ReuvenLerner 2 жыл бұрын
I hope in a good way!
@inzemam5271
@inzemam5271 2 жыл бұрын
@@ReuvenLerner yes 😂
@Momo-bb2fn
@Momo-bb2fn 3 жыл бұрын
Me: _chillin_ Him: *types at 0:44 Me: 👁👄👁
@ethernaut.7127
@ethernaut.7127 3 жыл бұрын
Reuven Lerner : *I am speed* lmao
@rh.m6660
@rh.m6660 2 ай бұрын
Top stuff sir. 👍
@ReuvenLerner
@ReuvenLerner 2 ай бұрын
Glad it helped!
@Morgan-l2d
@Morgan-l2d 9 күн бұрын
Thank you so much!
@ReuvenLerner
@ReuvenLerner 8 күн бұрын
My pleasure, glad it helped!
@FusionCorporation-jx5ou
@FusionCorporation-jx5ou 2 жыл бұрын
well its a pretty cool stuff, but as you said, pickle sometime do not work for some object. recently i was trying to pickle the object of my stock broker connection and it throwed error that object with the thread lock cannot be pickled
@ReuvenLerner
@ReuvenLerner 2 жыл бұрын
That's another good example of an object that cannot be pickled!
@thiagoamorim9172
@thiagoamorim9172 3 жыл бұрын
Perfect explanation, thank you! I just have one more doubt, how do I make if I wann to store more than one class object in the same file? Example: I have a class "Car", and objects car1 and car2, and my objective is to store car1 and car2 in the "mydata.pkl" file
@ReuvenLerner
@ReuvenLerner 3 жыл бұрын
Pickle itself doesn't handle more than one object in a file. But there are two ways to get around this: (1) Put the two instances of Car in a list. A list is a single object, and it can be pickled. Ta da! Problem solved. (2) Python's standard library comes with "shelf" (docs.python.org/3/library/shelve.html), which lets you store multiple objects. It's more complex, and it requires that you have a key for each stored object, but it might serve your needs.
@Utkarsh_Kumar215
@Utkarsh_Kumar215 3 жыл бұрын
sir.. please help me resolve my errors.type error- thread.rlock object while pickling , save n load the cnn model
@ReuvenLerner
@ReuvenLerner 3 жыл бұрын
I'm not sure what to tell you; I'm not really sure if you can pickle thread objects.
@martinocrespoalvarez4663
@martinocrespoalvarez4663 4 жыл бұрын
I like you're teaching style as well! Without 6:36 I'd have been looking for my problem for ages... What's the point of pickling a class definition if you have to import it for using it in another file after unpickling it?
@ReuvenLerner
@ReuvenLerner 4 жыл бұрын
Well, I don't know if you want to pickle a class definition -- it's more common to pickle an instance. But you'll still need the class definition on the other side. Imagine a situation in which you're storing objects in a database, or a filesystem. For example, you might have a machine-learning model that you spent a lot of time training. You'll store the model using pickle, and then load it whenever you want to run some predictions. That's fine, and it'll work great, but you first need to import the class. If you don't do that, things will break.
@josephgaviota
@josephgaviota 3 жыл бұрын
Thanks for that very interesting vid. I type pretty fast, but man, you're _really_ fast! Question: I pickle and unpickle a LOT, and rather than opening a file; save/load data, close file; or even using "with open" (both of which I can do and have done in the past), I always use: my_dict = pickle.load(open('somefile','rb')) pickle.dump(my_dict,open('otherfile','wb'),-1) So, it's a one-liner. Easy as pie. Do you object to that coding style? I've checked, and it doesn't leave any leftover open filehandles. So, I don't think I'm wasting system resource, and the one-line style seems clear to me. [edit: fix typo]
@ReuvenLerner
@ReuvenLerner 3 жыл бұрын
I think that you're just fine - as you say, there aren't going to be any open file objects when you're done, because there won't be any references to the file you've opened. So looks good to me! As for my typing, blame my parents. They forced me to learn when I was in high school, and it was, hands down (so to speak), the most practical skill I've learned in a long time.
@anwerhassan154
@anwerhassan154 Жыл бұрын
thanks really are wonderful
@ReuvenLerner
@ReuvenLerner Жыл бұрын
Glad you're enjoying!
@sahilmane6139
@sahilmane6139 3 жыл бұрын
How do i resolve 'cant pickle weakref objects'?
@ReuvenLerner
@ReuvenLerner 3 жыл бұрын
I was about to write that you cannot pickle a weakref. But it turns out that you *can*, assuming that the weakref resolves to a working, active object. If the weakref's object has gone away, then you'll run into trouble. How do you avoid this sort of problem? You could check that a weakref's object still exists before running "pickle," but then you'll have a race condition of sorts. So you might just need to avoid doing it altogether.
@Pedro-kw1ce
@Pedro-kw1ce 4 жыл бұрын
Hi there, I appreciate your video. But, I was trying to dump my own class object and this class contains a Simpy environment and I always get "AttributeError: 'Environment' object has no attribute" when I try to load that file to a variable again. I am using tkinter to generate a GUI to dump and load process, it allows me to generate this object too. Do you have any idea on how to figure this out?
@ReuvenLerner
@ReuvenLerner 4 жыл бұрын
Pickle works with all built-in Python classes, other than a few that cannot be saved or represented there. Your own classes, and packages that come from outside of the standard library, need to include support for pickle; it's not automatic. I don't know anything about Simpy, but it's quite possible that it doesn't support being pickled. I would suggest checking with the Simpy documentation and/or developers to see if it's possible.
@galan8115
@galan8115 4 жыл бұрын
And if you got a pickle.py file in your pycharm, like i did, it doenst work because it tries to import it/itself instead of using the library :D. Thats why i had to rename my tutorial file from "pickle.py" to "pikle.py"
@ReuvenLerner
@ReuvenLerner 4 жыл бұрын
Oh, goodness - this is a problem that I'm constantly warning my students about! You have to be careful not to call your program file with the name of a module in the standard library, especially if you're planning to import it. This is, unfortunately, a common mistake. Hmm, maybe I should make a video about it!
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 815 М.
Python Tutorial 14: Saving and Reading Data Files With Pickle
22:57
Paul McWhorter
Рет қаралды 31 М.
Disrespect or Respect 💔❤️
00:27
Thiago Productions
Рет қаралды 40 МЛН
Haunted House 😰😨 LeoNata family #shorts
00:37
LeoNata Family
Рет қаралды 16 МЛН
How Much Tape To Stop A Lamborghini?
00:15
MrBeast
Рет қаралды 201 МЛН
Hoodie gets wicked makeover! 😲
00:47
Justin Flom
Рет қаралды 129 МЛН
Python and Unicode characters
13:10
Python and Pandas with Reuven Lerner
Рет қаралды 6 М.
This INCREDIBLE trick will speed up your data processes.
12:54
Rob Mulla
Рет қаралды 268 М.
Serialize Python Objects With Pickle
11:36
NeuralNine
Рет қаралды 32 М.
How To Use: "@dataclass" In Python (Tutorial 2023)
15:01
Indently
Рет қаралды 29 М.
Python lists vs. arrays: How similar are they?
11:45
Python and Pandas with Reuven Lerner
Рет қаралды 18 М.
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 182 М.
If __name__ == "__main__" for Python Developers
8:47
Python Simplified
Рет қаралды 414 М.
Pickling Data With Python!
6:59
Mark Jay
Рет қаралды 48 М.
Why I prefer attrs over dataclasses
6:21
mCoding
Рет қаралды 65 М.
Disrespect or Respect 💔❤️
00:27
Thiago Productions
Рет қаралды 40 МЛН