ZODB 3 中的持久集

发布于 2024-07-20 00:00:55 字数 1109 浏览 9 评论 0原文

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 values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

败给现实 2024-07-27 00:00:55

为什么不使用 BTree ZODB 中的库。 有 4 个此类可用。 IITreeSet 和 IOTreeSet 管理整数集,OITreeSet 和 OOTreeSet 管理任意对象集。 它们分别对应于四个BTree类IIBTree、IOBTree、OIBTree和OOBTree。 与 Python 内置的 set 实现相比,它们的优势在于快速查找机制(感谢底层 BTree)和持久性支持。

这是一些示例代码:

>>> from BTrees.IIBTree import IITreeSet, union, intersection
>>> a = IITreeSet([1,2,3])
>>> a
<BTrees._IIBTree.IITreeSet object at 0x00B3FF18>
>>> b = IITreeSet([4,3,2])
>>> list(a)
[1, 2, 3]
>>> list(b)
[2, 3, 4]
>>> union(a,b)
IISet([1, 2, 3, 4])
>>> intersection(a,b)
IISet([2, 3])

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:

>>> from BTrees.IIBTree import IITreeSet, union, intersection
>>> a = IITreeSet([1,2,3])
>>> a
<BTrees._IIBTree.IITreeSet object at 0x00B3FF18>
>>> b = IITreeSet([4,3,2])
>>> list(a)
[1, 2, 3]
>>> list(b)
[2, 3, 4]
>>> union(a,b)
IISet([1, 2, 3, 4])
>>> intersection(a,b)
IISet([2, 3])
醉南桥 2024-07-27 00:00:55

对于未来的阅读,我只是想对已经提出的答案进行一些改进...

自定义持久集类

class PersistentSet(Persistent):

    def __init__(self, *args, **kwargs):
        self._set = set(*args, **kwargs)

    def __getattr__(self, name):
        return getattr(self._set, name)

来自库的持久集类

from BTrees.OOBTree import OOSet

另请参阅< /em>

For future readings, I just wanted to offer a slight improvement over the already proposed answers...

Custom persistent set class

class PersistentSet(Persistent):

    def __init__(self, *args, **kwargs):
        self._set = set(*args, **kwargs)

    def __getattr__(self, name):
        return getattr(self._set, name)

Persistent set class from the library

from BTrees.OOBTree import OOSet

See also

长梦不多时 2024-07-27 00:00:55

将所有属性请求转发到内部集:

class PersistentSet(Persistent):
    def __init__(self):
        self.inner_set = set()

    def __getattribute__(self, name):
        try:
            inner_set = Persistent.__getattribute__(self, "inner_set")
            output = getattr(inner_set, name)
        except AttributeError:
            output = Persistent.__getattribute__(self, name)

        return output

Forward all attribute requests to the internal set:

class PersistentSet(Persistent):
    def __init__(self):
        self.inner_set = set()

    def __getattribute__(self, name):
        try:
            inner_set = Persistent.__getattribute__(self, "inner_set")
            output = getattr(inner_set, name)
        except AttributeError:
            output = Persistent.__getattribute__(self, name)

        return output
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文