PersistentSet in ZODB 3

ZODB provides PersistentListand PersistentMapping, but I need PersistentSet. I wrote a quick class that displays the ancient one PersistentListfrom ZODB 2. Since it is not there UserSetin Python, I had to switch 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

A "multiple base with instance-based conflict" error occurred in the code . I tried to create a wrapper UserSetaround set, but that also did not solve the problem.

class UserSet(set):
    def __init__(self):
        self.value = set
    def __getattribute__(self, name):
        return self.value.__getattribute__(name

Finally, I imported sets.Set(replaced by the built-in set), but it also seems to be implemented in C. I have not found any PyPI implementations, so I'm at a dead end right now.

? , UserDict value s.

+3
3

, BTree ZODB. 4 . IITreeSet IOTreeSet , OITreeSet OOTreeSet . BREI IIBTree, IOBTree, OIBTree OOBTree . , Python, - (thanx 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])
+3

:

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
+1

For future readings, I just wanted to improve the suggested answers a bit ...

Custom Job Class

class PersistentSet(Persistent):

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

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

Library persistent dial class

from BTrees.OOBTree import OOSet

see also

+1
source

Source: https://habr.com/ru/post/1708526/


All Articles