如何在Python中序列化哈希对象 [英] How to serialize hash objects in Python
本文介绍了如何在Python中序列化哈希对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何序列化散列对象?我正在使用 shelve 存储大量对象。
层次结构:
- 用户
- 客户
- 好友
user.py:
输入时间
输入hashlib
from localfile import localfile
$ b $ class user(object):
_id = 0
_ip =127.0.0.1
_nick =Unnamed
_files = {}
def __init __(self,ip =127.0.0.1,nick =Unnamed):
self._id = hashlib.sha1(str(time.time() ))
self._ip = ip
self._nick = nick
def add_file(self,localfile):
self._files [localfile.hash] = localfile
def delete_file(self,localfile):
del self._files [localfile.hash]
if __name__ ==__main__:
pass
$ bclient.py:
from用户从朋友导入用户
中导入用户
(self,ip =127.0.0.1,nick =Unnamed):
user .__ init __(self,ip, nick)
@property
def friends(self):
return self._friends
@ friends.setter
def friends(self,value):
self._friends = value
def add_friend(self,client):
self._friends.append(client)
def delete_friend(self,client):
self._friends.remove (客户端)
if __name__ ==__main__:
import shelve
$ bx = shelve.open(localfile,'c')
(朋友(127.0.0.1,Amigo2))
cliente )
x [client] = cliente
print x [client]。friends
$ b x b.close()
错误:
facon @ facon -E1210:〜/ Documentos / workspace / test $ python client.py
Traceback(mos t最近调用最后):
在< module>文件中的client.py,第28行,
x [client] = cliente
文件/usr/lib/python2.7/shelve.py,第132行,在__setitem__
p.dump(value)
文件/usr/lib/python2.7/copy_reg.py,第70行,在_reduce_ex
中引发TypeError,无法腌制%s对象%base .__ name__
TypeError:can not pickle HASH objects
EDITED
添加了user.py。
解决方案由于无法序列化
HASH
具有
shelve
的对象,您将不得不以不同的方式提供相同的信息。例如,您只能保存散列的摘要。How can I serialize hash objects?, I'm using shelve to store a lot of objects.
Hierarchy:
- user - client - friend
user.py:
import time import hashlib from localfile import localfile class user(object): _id = 0 _ip = "127.0.0.1" _nick = "Unnamed" _files = {} def __init__(self, ip="127.0.0.1", nick="Unnamed"): self._id = hashlib.sha1(str(time.time())) self._ip = ip self._nick = nick def add_file(self, localfile): self._files[localfile.hash] = localfile def delete_file(self, localfile): del self._files[localfile.hash] if __name__ == "__main__": pass
client.py:
from user import user from friend import friend class client(user): _friends = [] def __init__(self, ip="127.0.0.1", nick="Unnamed"): user.__init__(self, ip, nick) @property def friends(self): return self._friends @friends.setter def friends(self, value): self._friends = value def add_friend(self, client): self._friends.append(client) def delete_friend(self, client): self._friends.remove(client) if __name__ == "__main__": import shelve x = shelve.open("localfile", 'c') cliente = client() cliente.add_friend(friend("127.0.0.1", "Amigo1")) cliente.add_friend(friend("127.0.0.1", "Amigo2")) x["client"] = cliente print x["client"].friends x.close()
Errors:
facon@facon-E1210:~/Documentos/workspace/test$ python client.py Traceback (most recent call last): File "client.py", line 28, in <module> x["client"] = cliente File "/usr/lib/python2.7/shelve.py", line 132, in __setitem__ p.dump(value) File "/usr/lib/python2.7/copy_reg.py", line 70, in _reduce_ex raise TypeError, "can't pickle %s objects" % base.__name__ TypeError: can't pickle HASH objects
EDITED
Added user.py.
解决方案Since you can't serialize
HASH
objects withshelve
, you would have to provide the same information in a different way. For example, you could only save the digest of the hash.这篇关于如何在Python中序列化哈希对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文