枚举中的腌制物体?
我正在从事一个工作项目,并且在腌制方面有问题。
这是我要做的事情的简化:
import pickle
from enum import Enum
class SiteInformation:
def __init__(self, name, location):
self.name = name
self.location = location
# Would actually be doing a number of calcs with name and location and storing
# as members.
self.time_zone = 'UTC'
class Site(Enum):
GN = SiteInformation('North Site', 'Location String1')
GS = SiteInformation('South Site', 'Location String2')
if __name__ == '__main__':
print('Pickling...')
with open('site_test', 'wb') as f:
pickle.dump(Site.GN, f)
pickle.dump(Site.GS, f)
print('Unpickling...')
with open('site_test', 'rb') as f:
data = pickle.load(f)
print(data)
当我运行此操作时,它在未挑剔的过程中失败:
Traceback (most recent call last):
File "/Users/seb/Development/Scheduler/scripts/pickle_test.py", line 27, in <module>
data = pickle.load(f)
File "/Applications/Anaconda/anaconda3/envs/Scheduler/lib/python3.10/enum.py", line 385, in __call__
return cls.__new__(cls, value)
File "/Applications/Anaconda/anaconda3/envs/Scheduler/lib/python3.10/enum.py", line 710, in __new__
raise ve_exc
ValueError: <__main__.SiteInformation object at 0x7fd77007ed40> is not a valid Site
我认为这与__ INT __ INT __ INT __
在SiteInformation中调用
,但是我不太确定如何解决此问题,因为我在腌制方面几乎没有经验。我尝试在siteInformation
中实现__ epr __
方法,但这无非是使valueerror
更可读。
任何帮助将不胜感激。
I'm working on a project for work and am having an issue with pickling.
Here is a simplification of what I am trying to do:
import pickle
from enum import Enum
class SiteInformation:
def __init__(self, name, location):
self.name = name
self.location = location
# Would actually be doing a number of calcs with name and location and storing
# as members.
self.time_zone = 'UTC'
class Site(Enum):
GN = SiteInformation('North Site', 'Location String1')
GS = SiteInformation('South Site', 'Location String2')
if __name__ == '__main__':
print('Pickling...')
with open('site_test', 'wb') as f:
pickle.dump(Site.GN, f)
pickle.dump(Site.GS, f)
print('Unpickling...')
with open('site_test', 'rb') as f:
data = pickle.load(f)
print(data)
When I run this, it fails during the unpickling:
Traceback (most recent call last):
File "/Users/seb/Development/Scheduler/scripts/pickle_test.py", line 27, in <module>
data = pickle.load(f)
File "/Applications/Anaconda/anaconda3/envs/Scheduler/lib/python3.10/enum.py", line 385, in __call__
return cls.__new__(cls, value)
File "/Applications/Anaconda/anaconda3/envs/Scheduler/lib/python3.10/enum.py", line 710, in __new__
raise ve_exc
ValueError: <__main__.SiteInformation object at 0x7fd77007ed40> is not a valid Site
I assume that this has to do with the __init__
call in SiteInformation
, but I'm not quite sure how to fix this as I have little experience with pickling. I have tried implementing a __repr__
method in SiteInformation
but this did nothing more than make the ValueError
more readable.
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在Python 3.11中,这将按照书面形式工作。在较早的Pythons中,您需要添加
__ eq __
(加上__ Hash __
,如果您想要set> set> set> set()
和dict(dict)
支持 1 )启用siteInformation
的不同实例,其值相同的值相等:注意:
__ eq __ eq __
仅显示的目的。一个“真实”__ eq __
看起来像:__ hash __
看起来像:1:
__ hash ____ ____
,将对象放在set(),或用作
dict()
中的键。In Python 3.11 this will work as written. In earlier Pythons you will need to add an
__eq__
(plus__hash__
if you wantset()
anddict()
support1) to enable different instances ofSiteInformation
with the same values to compare equal:Note: the
__eq__
shown is for example purposes only. A "real"__eq__
would look like:A
__hash__
could look like:1:
__hash__
is needed for an object to be in aset()
, or to be used as a key in adict()
.看来这已经解决了问题,并使代码基库更加清洁(基于
Planet
enum
python文档中的示例):It seems that this has fixed the problem and made the code base considerably cleaner (based off the
Planet
example in theEnum
Python docs):