python - pickle port? loading a pickle with data when I've redefined the object code - Stack Overflow
I've been building "company files" which contain lots of data for specific companies. I then pickle these and keep moving so I can work on one at a time.
As I'm going, I keep adding methods and relative attributes to my working classes. But then when I import from these company objects I get AttributeError.
class Company(object):
def __init__(self):
self._obj_ver = 1.0
Then I do some work and save:
in: company = Company()
### do stuff
in: with open(pkl_co_1, 'wb') as f:
pickle.dump(company, f, pickle.HIGHEST_PROTOCOL)
I added some functionality and stuff while working on company 2+, and added an observer pattern to make data updates faster:
class Company(object, Observable): #<== notice the new inheritance, so great, until it's not
def __init__(self):
self._obj_ver = 2.0
So obviously, when I go back to company 1 my objects are missing some stuff:
#read the pickle file
with open(pkl_co_1, 'rb') as f:
company = pickle.load(f)
try stuff:
in: company._obj_ver
out: 1.0 #<== even though the code __init__ is now 2.0... it's why I have this attribute
in: company.observers #Attribute from Observable class, which only exists in obj_ver 2.0+
out: kablooey!!!
This is all expected behavior at this point. So here's what I'm wondering. Is there a toolset or has someone done a "Port pickle"?
I'm thinking it's some sort of: pickle_port_magician.py
def port(company_v1_obj, newco):
for item in company_v1_obj.__dict__:
newco.getattr(self, item) = company_v1_obj.getattr(copmany_v1_obj?, item)
I'm just really fuzzy on what object I should be using, and I feel like I can't be the only one who's had this issue.
Does anyone have a way to port pickles from an older version of the object definition to a new version of the object definition?
Python 3.12.2
- 芯片缺货严重,真凶或是ABF产能不足
- 都想取代PC 这些产品究竟还欠缺什么?
- 奇虎诉腾讯索赔1.5亿创天价 双方股价昨齐上涨
- delphi - How to set up the properties of the ScrollBars of a TStringGrid? - Stack Overflow
- eslint - How to have 2 no-restricted-globals rules with different severity? - Stack Overflow
- dart - Flutter local notification throws error when i try to setup scheduled Notification - Stack Overflow
- c# - Is there a way to fill scriptable object field with a child class? - Stack Overflow
- differential equations - Calculating a rocket trajectory in Matlab - Stack Overflow
- c++ - templates are instantiated despite having extern template - Stack Overflow
- How to work with output of tsdisplay (capturemodify plots produced by it) in R - Stack Overflow
- c# - How to add image as an input to a ChatMessage with Microsoft.Extensions.AI - Stack Overflow
- kotlin - Android Studio Code Completion not working in test folder in all projects - Stack Overflow
- repeat - I want to implement repeated push (notification), AOS - Stack Overflow
- tensorflow - How to resolved the import error with scipy when using keras_tuner? - Stack Overflow
- Font Size Mismatch When Using Fallback Fonts in FFmpeg ASS Subtitles - Stack Overflow
- ios - Persist overlay view in the detail side of NavigationSplitView - Stack Overflow
- reactjs - React and Electron application suggesting I'm using invalid hook calls - Stack Overflow