存储在工具架中的Python实例在关闭后会发生更改



我认为解释这种情况的最好方法是用一个例子:

>>> class Person:
...     def __init__(self, brother=None):
...         self.brother = brother
... 
>>> bob = Person()
>>> alice = Person(brother=bob)
>>> import shelve
>>> db = shelve.open('main.db', writeback=True)
>>> db['bob'] = bob
>>> db['alice'] = alice
>>> db['bob'] is db['alice'].brother
True
>>> db['bob'] == db['alice'].brother
True
>>> db.close()
>>> db = shelve.open('main.db',writeback=True)
>>> db['bob'] is db['alice'].brother
False
>>> db['bob'] == db['alice'].brother
False

两个比较的预期输出再次为True。然而,pickle(由shelve使用)似乎正在分别重新实例化bobalice.brother。如何使用shelve/pickle"修复"此问题?db['alice'].brother有可能指向db['bob']或类似的东西吗?注意,我不想只比较两者,我需要两者实际上是一样的。

根据Blckknht的建议,我尝试一次对整个字典进行pickle,但问题仍然存在,因为它似乎要单独pickle每个键。

我相信您看到的问题来自于shelve模块存储其值的方式。每个值都是独立于工具架中的其他值进行pickle的,这意味着如果将同一对象作为一个值插入多个键下,则在键之间不会保留身份。但是,如果单个值对同一对象有多个引用,则标识将保留在该单个值中。

这里有一个例子:

a = object() # an arbitrary object
db = shelve.open("text.db")
db['a'] = a
db['another_a'] = a
db['two_a_references'] = [a, a]
db.close()
db = shelve.open("text.db") # reopen the db
print(db['a'] is db['another_a']) # prints False
print(db['two_a_references'][0] is db['two_a_references'][1]) # prints True

第一次打印尝试确认插入数据库中的对象a的两个版本的身份,一个直接在密钥'a'下,另一个在'another_a'下。它不起作用,因为单独的值是单独腌制的,因此它们之间的身份丢失了。

第二次打印测试存储在密钥'two_a_references'下的对a的两个引用是否被保持。因为这份名单是一次性腌制的,所以身份被保留了下来。

因此,要解决您的问题,您有几个选择。一种方法是避免测试身份,并在各种对象类型中依赖__eq__方法来确定两个对象在语义上是否相等,即使它们不是同一个对象。另一种方法是将所有数据捆绑到一个对象(例如字典)中,然后用pickle.dump保存并用pickle.load恢复,而不是使用shelve(或者你可以将这个配方调整为一个持久字典,它从shelve文档链接而来,几乎可以做到这一点)。

在Python中,合适的方法是在Person类内部实现__eq____ne__函数,如下所示:

class Person(object):
    def __eq__(self, other):
        return (isinstance(other, self.__class__)
            and self.__dict__ == other.__dict__)
    def __ne__(self, other):
        return not self.__eq__(other)

一般来说,这应该足够了,但如果这些确实是数据库对象并且有主键,那么检查该属性而不是self.__dict__会更有效。

问题

要保留shelve的身份,您需要保留pickle的身份,请阅读本文。

解决方案

此类将所有对象保存在其类站点上,并在标识相同的情况下恢复它们。你应该能够从它派生子类。

>>> class PickleWithIdentity(object):
    identity = None
    identities = dict() # maybe use weakreference dict here
    def __reduce__(self):
        if self.identity is None:
            self.identity = os.urandom(10) # do not use id() because it is only 4 bytes and not random
            self.identities[self.identity] = self
        return open_with_identity, (self.__class__, self.__dict__), self.__dict__

>>> def open_with_identity(cls, dict):
    if dict['identity'] in cls.identities:
        return cls.identities[dict['identity']]
    return cls()
>>> p = PickleWithIdentity()
>>> p.asd = 'asd'
>>> import pickle
>>> import os
>>> pickle.loads(pickle.dumps(p))
<__main__.PickleWithIdentity object at 0x02D2E870>
>>> pickle.loads(pickle.dumps(p)) is p
True

由于状态可能被覆盖,可能会出现进一步的问题

>>> p.asd
'asd'
>>> ps = pickle.dumps(p)
>>> p.asd = 123
>>> pickle.loads(ps)
<__main__.PickleWithIdentity object at 0x02D2E870>
>>> p.asd
'asd'

最新更新