ZODB 3 中的持久集
ZODB 提供了一个 PersistentList
和一个 PersistentMapping
,但我想要一个 PersistentSet
。 我写了一个快速类,反映了古老的 PersistentList
来自 ZODB 2。因为 Python 中没有 UserSet
,所以我必须从基于 C 的内置 set< 进行扩展/代码>。
class PersistentSet(UserSet, Persistent):
def __iand__(self, other):
set.__iand__(other)
self._p_changed = 1
...
...
...
def symmetric_difference_update(self, other):
set.symmetric_difference_update(other)
self._p_changed = 1
该代码产生了“多个基础存在实例布局冲突”
class UserSet(set):
def __init__(self):
self.value = set
def __getattribute__(self, name):
return self.value.__getattribute__(name
最后,我导入了 sets.Set
(被内置的 set
取代),但这似乎也是用 C 实现的。 我在 PyPI 上没有找到任何设置的实现,所以我现在陷入了死胡同。
我的选择是什么?我可能必须从头开始实现一组或使用UserDict
并丢弃所有值
。
ZODB provides a PersistentList
and a PersistentMapping
, but I'd like a PersistentSet
. I wrote a quick class that mirrors the ancient PersistentList
from ZODB 2. Because there's no UserSet
in Python, I had to extend from the C-based built-in set
.
class PersistentSet(UserSet, Persistent):
def __iand__(self, other):
set.__iand__(other)
self._p_changed = 1
...
...
...
def symmetric_difference_update(self, other):
set.symmetric_difference_update(other)
self._p_changed = 1
The code produced a "multiple bases have instance lay-out conflict" error. I tried creating a UserSet
wrapper around set
, but that didn't solve the problem either.
class UserSet(set):
def __init__(self):
self.value = set
def __getattribute__(self, name):
return self.value.__getattribute__(name
Finally, I imported sets.Set
(superseded by the built-in set
), but that seems to be implemented in C, too. I didn't find any set implementations on PyPI so I'm at a dead end now.
What are my options? I may have to implement a set from scratch or use UserDict
and throw away all the value
s.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不使用 BTree ZODB 中的库。 有 4 个此类可用。 IITreeSet 和 IOTreeSet 管理整数集,OITreeSet 和 OOTreeSet 管理任意对象集。 它们分别对应于四个BTree类IIBTree、IOBTree、OIBTree和OOBTree。 与 Python 内置的 set 实现相比,它们的优势在于快速查找机制(感谢底层 BTree)和持久性支持。
这是一些示例代码:
Why don't you use the persistent set class provided with the BTree libraries in ZODB. There are 4 such classes available. IITreeSet and IOTreeSet manage sets of integers and OITreeSet and OOTreeSet manage set of arbitrary objects. They correspond to the four BTree classes IIBTree, IOBTree, OIBTree and OOBTree respectively. Their advantages over the set implementation built into Python are their fast lookup mechanism (thanx to the underlying BTree) and their persistence support.
Here is some sample code:
对于未来的阅读,我只是想对已经提出的答案进行一些改进...
自定义持久集类
来自库的持久集类
另请参阅< /em>
For future readings, I just wanted to offer a slight improvement over the already proposed answers...
Custom persistent set class
Persistent set class from the library
See also
将所有属性请求转发到内部集:
Forward all attribute requests to the internal set: